2009-03-20 21:22:07 +08:00
|
|
|
|
|
|
|
import py
|
|
|
|
|
|
|
|
class XSpec:
|
|
|
|
""" Execution Specification: key1=value1//key2=value2 ...
|
|
|
|
* keys need to be unique within the specification scope
|
|
|
|
* neither key nor value are allowed to contain "//"
|
|
|
|
* keys are not allowed to contain "="
|
|
|
|
* keys are not allowed to start with underscore
|
|
|
|
* if no "=value" is given, assume a boolean True value
|
|
|
|
"""
|
2009-03-21 00:28:14 +08:00
|
|
|
# XXX for now we are very restrictive about actually allowed key-values
|
2009-03-23 04:44:45 +08:00
|
|
|
popen = ssh = socket = python = chdir = nice = None
|
2009-03-20 21:22:07 +08:00
|
|
|
|
2009-03-21 00:28:14 +08:00
|
|
|
def __init__(self, string):
|
|
|
|
self._spec = string
|
|
|
|
for keyvalue in string.split("//"):
|
|
|
|
i = keyvalue.find("=")
|
|
|
|
if i == -1:
|
|
|
|
key, value = keyvalue, True
|
|
|
|
else:
|
|
|
|
key, value = keyvalue[:i], keyvalue[i+1:]
|
|
|
|
# XXX be restrictive for now
|
|
|
|
if key not in XSpec.__dict__:
|
2009-03-21 00:58:32 +08:00
|
|
|
raise AttributeError("%r not a valid XSpec key" % key)
|
2009-03-21 00:28:14 +08:00
|
|
|
setattr(self, key, value)
|
|
|
|
|
2009-03-21 00:58:32 +08:00
|
|
|
def __repr__(self):
|
|
|
|
return "<XSpec %r>" %(self._spec,)
|
|
|
|
|
2009-03-21 00:28:14 +08:00
|
|
|
def __hash__(self):
|
|
|
|
return hash(self._spec)
|
|
|
|
def __eq__(self, other):
|
|
|
|
return self._spec == getattr(other, '_spec', None)
|
|
|
|
def __ne__(self, other):
|
|
|
|
return self._spec != getattr(other, '_spec', None)
|
|
|
|
|
|
|
|
#def __getattr__(self, name):
|
|
|
|
# if name[0] == "_":
|
|
|
|
# raise AttributeError(name)
|
|
|
|
# return None
|
2009-03-20 21:22:07 +08:00
|
|
|
|
2009-03-20 22:20:40 +08:00
|
|
|
def _samefilesystem(self):
|
2009-03-21 00:28:14 +08:00
|
|
|
return bool(self.popen and not self.chdir)
|
2009-03-20 21:22:07 +08:00
|
|
|
|
2009-03-20 22:20:40 +08:00
|
|
|
def makegateway(spec):
|
|
|
|
if not isinstance(spec, XSpec):
|
|
|
|
spec = XSpec(spec)
|
|
|
|
if spec.popen:
|
|
|
|
gw = py.execnet.PopenGateway(python=spec.python)
|
2009-03-20 23:36:45 +08:00
|
|
|
elif spec.ssh:
|
|
|
|
gw = py.execnet.SshGateway(spec.ssh, remotepython=spec.python)
|
|
|
|
elif spec.socket:
|
|
|
|
assert not spec.python, "socket: specifying python executables not supported"
|
|
|
|
hostport = spec.socket.split(":")
|
|
|
|
gw = py.execnet.SocketGateway(*hostport)
|
2009-03-20 22:20:40 +08:00
|
|
|
gw.spec = spec
|
2009-03-20 23:52:39 +08:00
|
|
|
# XXX events
|
2009-03-23 04:44:45 +08:00
|
|
|
if spec.chdir or spec.nice:
|
|
|
|
channel = gw.remote_exec("""
|
2009-03-20 23:52:39 +08:00
|
|
|
import os
|
2009-03-23 04:44:45 +08:00
|
|
|
path, nice = channel.receive()
|
|
|
|
if path:
|
|
|
|
if not os.path.exists(path):
|
|
|
|
os.mkdir(path)
|
2009-03-20 23:52:39 +08:00
|
|
|
os.chdir(path)
|
2009-03-23 04:44:45 +08:00
|
|
|
if nice and hasattr(os, 'nice'):
|
|
|
|
os.nice(nice)
|
|
|
|
""")
|
|
|
|
nice = spec.nice and int(spec.nice) or 0
|
|
|
|
channel.send((spec.chdir, nice))
|
|
|
|
channel.waitclose()
|
2009-03-20 22:20:40 +08:00
|
|
|
return gw
|