Created
September 30, 2016 15:20
-
-
Save Andrei-Stepanov/a9e21e5893a2266e131d710e8486fba4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import zope | |
from zope import interface | |
from zope.interface.interface import adapter_hooks | |
class IOSystem(interface.Interface): | |
req = interface.Attribute("OS req.") | |
@interface.implementer(IOSystem) | |
class OSystem(object): | |
def __init__(self): | |
self.req = {} | |
# Windows | |
class IWindows(IOSystem): | |
pass | |
@interface.implementer(IWindows) | |
class Windows(OSystem): | |
def __init__(self, *args, **kwargs): | |
super(Windows, self).__init__(*args, **kwargs) | |
self.req["os"] = "windows" | |
# Linux | |
class ILinux(IOSystem): | |
pass | |
@interface.implementer(ILinux) | |
class Linux(OSystem): | |
def __init__(self, *args, **kwargs): | |
super(Linux, self).__init__(*args, **kwargs) | |
self.req["os"] = "linux" | |
# Rhel | |
class IRhel(ILinux): | |
pass | |
@interface.implementer(IRhel) | |
class Rhel(Linux): | |
def __init__(self, *args, **kwargs): | |
super(Rhel, self).__init__(*args, **kwargs) | |
self.req["variant"] = "rhel" | |
# Fedora | |
class IFedora(ILinux): | |
pass | |
@interface.implementer(IFedora) | |
class Fedora(Linux): | |
def __init__(self, *args, **kwargs): | |
super(Windows, self).__init__(*args, **kwargs) | |
self.req["variant"] = "fedora" | |
# Rhel6 | |
class IRhel6(IRhel): | |
pass | |
class IRhel7(IRhel): | |
pass | |
class IRhel68(IRhel6): | |
pass | |
class IRhel69(IRhel6): | |
pass | |
class IWindows7(IWindows): | |
pass | |
class IWindows10(IWindows): | |
pass | |
# Arch | |
class IArch(interface.Interface): | |
arch = interface.Attribute("Architecture.") | |
# Arch 32 | |
class IArch32(IArch): | |
pass | |
@interface.implementer(IArch32) | |
class Arch32(object): | |
def __init__(self): | |
self.req = ['x86', '32', 'i586'] | |
# Arch 64 | |
class IArch64(IArch): | |
pass | |
@interface.implementer(IArch64) | |
class Arch64(object): | |
def __init__(self): | |
self.req = ['x64', 'x86_64', 'amd64'] | |
# Version | |
class IVersionMajor(interface.Interface): | |
ver = interface.Attribute("Version.") | |
# Ver6 | |
class IVersionMajor6(IVersionMajor): | |
pass | |
@interface.implementer(IVersionMajor6) | |
class VersionMajor6(object): | |
def __init__(self): | |
self.ver = 6 | |
# Ver7 | |
class IVersionMajor7(IVersionMajor): | |
pass | |
@interface.implementer(IVersionMajor7) | |
class VersionMajor7(object): | |
def __init__(self): | |
self.ver = 7 | |
class IVersionMinor(interface.Interface): | |
ver = interface.Attribute("Version minor.") | |
# Minor 1 | |
class IVersionMinor1(IVersionMinor): | |
pass | |
@interface.implementer(IVersionMinor1) | |
class VersionMinor1(object): | |
def __init__(self): | |
self.ver_minor = 1 | |
from zope.interface.adapter import AdapterRegistry | |
registry = AdapterRegistry() | |
class IVmConfig(interface.Interface): | |
pass | |
@interface.implementer(IVmConfig) | |
class VmConfig(dict): | |
pass | |
class ICommand(interface.Interface): | |
def __call__(*args, **kwargs): | |
"""Some command.""" | |
class ICommandRestart(ICommand): | |
pass | |
class ICommandKill(ICommand): | |
pass | |
class ICommandStart(ICommand): | |
pass | |
def foo_restart_rhel(obj): | |
print "Do restart rhel." | |
def foo_restart_rhel7(obj): | |
print "Do restart rhel7." | |
def foo_restart_win(obj): | |
print "Do restart win." | |
def foo1(obj): | |
print "Only Rhel 7" | |
def foo2(obj): | |
print "Only Rhel" | |
zope.interface.directlyProvides(foo_restart_rhel, ICommandRestart) | |
zope.interface.directlyProvides(foo_restart_rhel7, ICommandRestart) | |
zope.interface.directlyProvides(foo_restart_win, ICommandRestart) | |
class ICommandRestart(interface.Interface): | |
def __call__(vm): | |
"""Restart vm""" | |
def CmdFactoryRestart(smth): | |
pass | |
def CommandRestartImplementer(context): | |
pass | |
class IVm(interface.Interface): | |
pass | |
@interface.implementer(IVm) | |
class Vm(object): | |
def __init__(self, cfg): | |
self.cfg = cfg | |
cfg1 = {'os':'Linux', 'distr':'RHEL', 'ver':'7', 'variant':'Workstation', 'arch':None} | |
cfg2 = {'os':'Windows', 'ver':'10', 'variant':'Professional'} | |
vm1 = Vm(cfg1) | |
vm2 = Vm(cfg2) | |
#registry.register([], IVersionMajor, ver) | |
# return PROXY/ADAPTER, that takes IVM | |
class AdapterVm2Cmd_Factory(object): | |
def __init__(self, req_iface): | |
self.req_iface = req_iface | |
def __call__(self, vm_provider): | |
os = IRhel | |
ver = IVersionMajor7 | |
mver = IVersionMinor1 | |
arch = IArch32 | |
f = registry.lookup([os, ver, mver, arch], self.req_iface) or \ | |
registry.lookup([os, ver, mver], self.req_iface) or \ | |
registry.lookup([os, ver, arch], self.req_iface) or \ | |
registry.lookup([os, ver], self.req_iface) or \ | |
registry.lookup([os], self.req_iface) | |
return f | |
#registry.register([IVm], ICommandRestart, '', AdapterVm2Cmd_Factory(ICommandRestart)) | |
registry.register([IRhel], ICommandRestart, '', foo_restart_rhel) | |
registry.register([IRhel, IVersionMajor7], ICommandRestart, '', foo_restart_rhel7) | |
registry.register([IWindows], ICommandRestart, '', foo_restart_win) | |
#ICommand(test1) | |
#ICommand(test2) | |
#scmd = ICommand(test, "Selenium") # two realization win + linux | |
#sel_cmd = scmd.make() | |
# | |
# | |
# | |
#@interface.implementer(ICommand) | |
#class CmdLineLinux(object): | |
# | |
# def __init__(self, cmdline): | |
# self.cmdline = cmdline | |
# | |
# def mk_cmd(self): | |
# return subprocess.list2cmdline(self.cmdline) | |
# | |
# | |
#def cmdline_invariant(cmdline): | |
# if type(cmdline) not list: | |
# raise CmdlineError(cmdline) | |
# | |
# | |
#class CmdLineError(zope.Interface.Invalid): | |
# """A bogous cmdline.""" | |
# def __repr__(self): | |
# return "Bad cmdline: %r" % self.args | |
# | |
# | |
# defs = interface.Attribute("-Dxxx definitions.") | |
# opts = interface.Attribute("Selenium server options.") | |
# jar_rpath = interface.Attribute("Path to Selenium .jar file.") | |
# java = interface.Attribute("Path to Java interpreter.") | |
# | |
# | |
#class ICmdLine(interface.Interface): | |
# | |
# cmdline = interface.Attribute("Command and its options list.") | |
# | |
# def mk_cmd(): | |
# """Make a string escaping it for passing to os.system() like calls. | |
# """ | |
# | |
# | |
#@interface.implementer(IExternalAPIPort) | |
#class SeleniumLinux(object, Commands): | |
# | |
# def __init__(self): | |
# self.defs = [] | |
# if self.cfg.selenium_driver == 'Firefox': | |
# profile = self.cfg.firefox_profile | |
# if profile: | |
# cmd = "firefox -CreateProfile %s" % profile | |
# output = self.ssn.cmd(cmd) | |
# output = re.findall(r'\'[^\']*\'', output)[1] | |
# output = output.replace("'",'') | |
# output = os.path.dirname(dirname) | |
# self.firefox_profile_dir = output | |
# self.vm.info("Created a new FF profile at: %s", output) | |
# self.defs.append("-Dwebdriver.firefox.profile=%s" % profile) | |
# self.opts = [] | |
# self.opts.append("-port %s" % cfg.selenium_port) | |
# self.opts.append("-trustAllSSLcertificates") | |
# self.java = "java" | |
# selenium = download_asset("selenium", section=self.cfg_vm.selenium_ver) | |
# fname = os.path.basename(selenium) | |
# jar_rpath = os.path.join(self.workdir(), fname) | |
# self.vm.copy_files_to(selenium, jar_rpath) | |
# | |
# def mk_cmd(self): | |
# cmd = "{java} {defs} -jar {dst_fname} {opts}".format(self.java, | |
# self.defs, | |
# self.dst_fname, | |
# self.opts) | |
# self.vm.info("Selenium cmd: %s", cmd) | |
# | |
# def run(self, session): | |
# ssn.sendline(cmd) | |
# | |
# | |
# | |
# | |
# def run(self, ssn): | |
# """Some general info. | |
# There are ways to define Firefox options globaly: | |
# | |
# * /usr/lib64/firefox/defaults/preferences/<anyname>.js | |
# * /etc/firefox/pref/<anyname>.js | |
# | |
# Format is: | |
# | |
# user_pref("some.key", "somevalue"); | |
# pref("some.key", "somevalue"); | |
# | |
# All values are defined at: | |
# | |
# about:config?filter=color | |
# | |
# Also user can define values for specific profile: | |
# | |
# http://kb.mozillazine.org/Profile_folder_-_Firefox#Linux | |
# | |
# For curent profile go to: about:support and press "Open Directory" | |
# | |
# Selenium understands next options: | |
# | |
# -Dwebdriver.firefox.profile=my-profile | |
# -Dwebdriver.firefox.bin=/path/to/firefox | |
# -trustAllSSLcertificates | |
# | |
# Also it is possible to specify Firefox profile in selenium python | |
# bindings: | |
# | |
# FirefoxProfile p = new FirefoxProfile(new File("D:\\profile2")); | |
# p.updateUserPrefs(new File("D:\\t.js")); | |
# | |
# To create a new profile call: | |
# | |
# firefox -CreateProfile <profile name> | |
# | |
# """ | |
# selenium = download_asset("selenium", section=self.cfg_vm.selenium_ver) | |
# fname = os.path.basename(selenium) | |
# dst_fname = os.path.join(self.workdir(), fname) | |
# self.vm.copy_files_to(selenium, dst_fname) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment