PATCH: Tweak Host.Run interface
Mark Mitchell
mark at codesourcery.com
Fri Aug 18 00:42:15 UTC 2006
The Host extension classes allows you to run programs on remote
machines via the Run method. The "path" argument is interpreted
differently depending on its form: absolute paths are absolute (of
course), relative paths with at least one directory seperator are
relative to the default directory, and relative paths without a
directory separator search the PATH. (That was the pre-patch
situation.)
However, that gives no easy way to run a program in the default
directory. You have to explicitly say "./foo", which is cumbersome
and means the caller has to form OS-specific pathnames (e.g., change
"/" for "\" if running from UNIX to Windows). So, this patch adds an
explicit "relative" parameter to Run; now if you set Relative to true,
the path is always considered to be relative to the default directory,
and it's the Host extensions responsibility to form a path which makes
sense on the remote machine.
Applied.
--
Mark Mitchell
CodeSourcery
mark at codesourcery.com
(650) 331-3385 x713
2006-08-17 Mark Mitchell <mark at codesourcery.com>
* qm/host.py (Host.Run): Add 'relative' parameter.
(Host.UploadAndRun): Use it.
* qm/test/classes/ssh_host.py (SSHHost.Run): Add 'relative'
parameter.
Index: qm/host.py
===================================================================
RCS file: /home/qm/Repository/qm/qm/host.py,v
retrieving revision 1.3
diff -c -5 -p -r1.3 host.py
*** qm/host.py 15 Jun 2006 13:40:14 -0000 1.3
--- qm/host.py 18 Aug 2006 00:35:18 -0000
*************** import os.path
*** 25,35 ****
class Host(Extension):
"""A 'Host' is a logical machine.
Each logical machine has a default directory. When a file is
! uploaded to or downloaded from the machine, and a relative patch
is specified, the patch is relative to the default directory.
Similarly, when a program is run on the remote machine, its
initial working directory is the default directory.
The interface presented by 'Host' is a lowest common
--- 25,35 ----
class Host(Extension):
"""A 'Host' is a logical machine.
Each logical machine has a default directory. When a file is
! uploaded to or downloaded from the machine, and a relative path
is specified, the patch is relative to the default directory.
Similarly, when a program is run on the remote machine, its
initial working directory is the default directory.
The interface presented by 'Host' is a lowest common
*************** class Host(Extension):
*** 57,74 ****
return None
! def Run(self, path, arguments, environment = None, timeout = -1):
"""Run a program on the remote host.
'path' -- The name of the program to run, on the remote host.
! If 'path' is an absolute path or contains no directory
! separators it is used unmodified; otherwise (i.e., if it is a
! relative path containing at least one separator) it is
! interpreted relative to the default directory.
'arguments' -- The sequence of arguments that should be passed
to the program.
'environment' -- If not 'None', a dictionary of pairs of
--- 57,75 ----
return None
! def Run(self, path, arguments, environment = None, timeout = -1,
! relative = False):
"""Run a program on the remote host.
'path' -- The name of the program to run, on the remote host.
! If 'relative' is true, or if 'path' is not an absolute path
! but does contain at least one directory separator, then 'path'
! is interpreted relative to the default directory. Otherwise,
! 'path' is used unmodified.
'arguments' -- The sequence of arguments that should be passed
to the program.
'environment' -- If not 'None', a dictionary of pairs of
*************** class Host(Extension):
*** 92,101 ****
--- 93,104 ----
if environment is not None:
new_environment = os.environ.copy()
new_environment.update(environment)
environment = new_environment
executable = self.Executable(timeout)
+ if relative:
+ path = os.path.join(os.curdir, path)
status = executable.Run([path] + arguments, environment)
return (status, executable.stdout)
def UploadFile(self, local_file, remote_file = None):
*************** class Host(Extension):
*** 149,168 ****
The program is uploaded to the default directory on the remote
host, run, and then deleted."""
self.UploadFile(path)
! result = self.Run(os.path.join(os.path.curdir,
! os.path.basename(path)),
arguments,
environment,
! timeout)
self.DeleteFile(path)
return result
-
def DeleteFile(self, remote_file):
"""Delete the 'remote_file'.
'remote_file' -- A relative path to the file to be deleted."""
--- 152,170 ----
The program is uploaded to the default directory on the remote
host, run, and then deleted."""
self.UploadFile(path)
! result = self.Run(os.path.basename(path),
arguments,
environment,
! timeout,
! relative = True)
self.DeleteFile(path)
return result
def DeleteFile(self, remote_file):
"""Delete the 'remote_file'.
'remote_file' -- A relative path to the file to be deleted."""
Index: qm/test/classes/ssh_host.py
===================================================================
RCS file: /home/qm/Repository/qm/qm/test/classes/ssh_host.py,v
retrieving revision 1.2
diff -c -5 -p -r1.2 ssh_host.py
*** qm/test/classes/ssh_host.py 15 Jun 2006 13:40:14 -0000 1.2
--- qm/test/classes/ssh_host.py 18 Aug 2006 00:35:18 -0000
*************** class SSHHost(Host):
*** 69,85 ****
If not empty, the user name that should be used when
connecting to the remote host."""
)
! def Run(self, path, arguments, environment = None, timeout = -1):
! if self.default_dir and not os.path.isabs(path):
! if (path.find(os.path.sep) != -1
! or (os.path.altsep
! and path.find(os.path.altsep) != -1)):
! path = os.path.join(self.default_dir, path)
path, arguments = self._FormSSHCommandLine(path, arguments,
environment)
return super(SSHHost, self).Run(path, arguments, None, timeout)
--- 69,90 ----
If not empty, the user name that should be used when
connecting to the remote host."""
)
! def Run(self, path, arguments, environment = None, timeout = -1,
! relative = False):
! default_dir = self.default_dir
! if not default_dir:
! default_dir = os.curdir
! if (relative
! or (not os.path.isabs(path)
! and (path.find(os.path.sep) != -1
! or (os.path.altsep
! and path.find(os.path.altsep) != -1)))):
! path = os.path.join(default_dir, path)
path, arguments = self._FormSSHCommandLine(path, arguments,
environment)
return super(SSHHost, self).Run(path, arguments, None, timeout)
More information about the qmtest
mailing list