test_ok2/py/test/dsession/hostmanage.py

110 lines
3.6 KiB
Python
Raw Normal View History

import py
import sys, os
from py.__.test.dsession.masterslave import MasterNode
from py.__.execnet.gwmanage import GatewayManager
from py.__.test import event
def getconfighosts(config):
if config.option.numprocesses:
hosts = ['popen'] * config.option.numprocesses
else:
hosts = config.option.hosts
if not hosts:
hosts = config.getvalue("hosts")
else:
hosts = hosts.split(",")
assert hosts is not None
return hosts
def getconfigroots(config):
roots = config.option.rsyncdirs
if roots:
roots = [py.path.local(x) for x in roots.split(',')]
else:
roots = []
conftestroots = config.getconftest_pathlist("rsyncdirs")
if conftestroots:
roots.extend(conftestroots)
for root in roots:
if not root.check():
raise ValueError("rsyncdir doesn't exist: %r" %(root,))
return roots
class HostManager(object):
def __init__(self, config, hosts=None):
self.config = config
if hosts is None:
hosts = getconfighosts(self.config)
self.roots = getconfigroots(config)
self.gwmanager = GatewayManager(hosts)
def makegateways(self):
old = self.config.topdir.chdir()
try:
self.gwmanager.makegateways()
finally:
old.chdir()
self.trace_hoststatus()
def trace_hoststatus(self):
if self.config.option.debug:
for ch, result in self.gwmanager.multi_exec("""
import sys, os
channel.send((sys.executable, os.getcwd(), sys.path))
""").receive_items():
self.trace("spec %r, execuable %r, cwd %r, syspath %r" %(
ch.gateway.spec, result[0], result[1], result[2]))
def config_getignores(self):
return self.config.getconftest_pathlist("rsyncignore")
def rsync_roots(self):
""" make sure that all remote gateways
have the same set of roots in their
current directory.
"""
# we change to the topdir sot that
# PopenGateways will have their cwd
# such that unpickling configs will
# pick it up as the right topdir
# (for other gateways this chdir is irrelevant)
self.makegateways()
options = {
'ignores': self.config_getignores(),
'verbose': 1, # self.config.option.verbose
}
if self.roots:
# send each rsync root
for root in self.roots:
self.gwmanager.rsync(root, **options)
else:
# we transfer our topdir as the root
self.gwmanager.rsync(self.config.topdir, **options)
# and cd into it
self.gwmanager.multi_chdir(self.config.topdir.basename, inplacelocal=False)
self.config.bus.notify("rsyncfinished", event.RsyncFinished())
def trace(self, msg):
self.config.bus.notify("trace", "testhostmanage", msg)
def setup_hosts(self, putevent):
self.rsync_roots()
nice = self.config.getvalue("dist_nicelevel")
if nice != 0:
self.gwmanager.multi_exec("""
import os
if hasattr(os, 'nice'):
os.nice(%r)
""" % nice).waitclose()
self.trace_hoststatus()
for host, gateway in self.gwmanager.spec2gateway.items():
host.node = MasterNode(host,
gateway,
self.config,
putevent)
def teardown_hosts(self):
self.gwmanager.exit()