[svn r62994] introducing internal MultiGateway class

--HG--
branch : trunk
This commit is contained in:
hpk
2009-03-17 12:53:09 +01:00
parent 7ed26c2929
commit 8a8ae5fe5d
5 changed files with 43 additions and 34 deletions

View File

@@ -94,31 +94,46 @@ class MultiChannel:
for ch in self._channels:
ch.waitclose()
class MultiGateway:
def __init__(self, gateways):
self.gateways = gateways
def remote_exec(self, source):
channels = []
for gw in self.gateways:
channels.append(gw.remote_exec(source))
return MultiChannel(channels)
class GatewayManager:
def __init__(self, specs):
self.spec2gateway = {}
for spec in specs:
self.spec2gateway[GatewaySpec(spec)] = None
self.specs = [GatewaySpec(spec) for spec in specs]
self.gateways = []
def trace(self, msg):
py._com.pyplugins.notify("trace", "gatewaymanage", msg)
def makegateways(self):
for spec, value in self.spec2gateway.items():
assert value is None
assert not self.gateways
for spec in self.specs:
self.trace("makegateway %s" %(spec))
self.spec2gateway[spec] = spec.makegateway()
self.gateways.append(spec.makegateway())
def getgateways(self, remote=True, inplacelocal=True):
l = []
for gw in self.gateways:
if gw.spec.inplacelocal():
if inplacelocal:
l.append(gw)
else:
if remote:
l.append(gw)
return MultiGateway(gateways=l)
def multi_exec(self, source, inplacelocal=True):
""" remote execute code on all gateways.
@param inplacelocal=False: don't send code to inplacelocal hosts.
"""
source = py.code.Source(source)
channels = []
for spec, gw in self.spec2gateway.items():
if inplacelocal or not spec.inplacelocal():
channels.append(gw.remote_exec(source))
return MultiChannel(channels)
multigw = self.getgateways(inplacelocal=inplacelocal)
return multigw.remote_exec(source)
def multi_chdir(self, basename, inplacelocal=True):
""" perform a remote chdir to the given path, may be relative.
@@ -132,7 +147,8 @@ class GatewayManager:
"""
rsync = HostRSync(source, verbose=verbose, ignores=ignores)
added = False
for spec, gateway in self.spec2gateway.items():
for gateway in self.gateways:
spec = gateway.spec
if not spec.inplacelocal():
self.trace("add_target_host %r" %(gateway,))
def finished():
@@ -148,8 +164,8 @@ class GatewayManager:
self.trace("rsync: nothing to do.")
def exit(self):
while self.spec2gateway:
spec, gw = self.spec2gateway.popitem()
while self.gateways:
gw = self.gateways.pop()
self.trace("exiting gateway %s" % gw)
gw.exit()

View File

@@ -106,21 +106,21 @@ class TestGatewayManagerPopen:
def test_hostmanager_popen_makegateway(self):
hm = GatewayManager(["popen"] * 2)
hm.makegateways()
assert len(hm.spec2gateway) == 2
assert len(hm.gateways) == 2
hm.exit()
assert not len(hm.spec2gateway)
assert not len(hm.gateways)
def test_hostmanager_popens_rsync(self, source):
hm = GatewayManager(["popen"] * 2)
hm.makegateways()
assert len(hm.spec2gateway) == 2
for gw in hm.spec2gateway.values():
assert len(hm.gateways) == 2
for gw in hm.gateways:
gw.remote_exec = None
l = []
hm.rsync(source, notify=lambda *args: l.append(args))
assert not l
hm.exit()
assert not len(hm.spec2gateway)
assert not len(hm.gateways)
def test_hostmanager_rsync_popen_with_path(self, source, dest):
hm = GatewayManager(["popen:%s" %dest] * 1)
@@ -129,7 +129,7 @@ class TestGatewayManagerPopen:
l = []
hm.rsync(source, notify=lambda *args: l.append(args))
assert len(l) == 1
assert l[0] == ("rsyncrootready", hm.spec2gateway.keys()[0], source)
assert l[0] == ("rsyncrootready", hm.gateways[0].spec, source)
hm.exit()
dest = dest.join(source.basename)
assert dest.join("dir1").check()