From 6da4acf90183b6c44232282e4982da1939e370a5 Mon Sep 17 00:00:00 2001 From: Sinisa Veseli <sveseli@aps.anl.gov> Date: Wed, 22 Feb 2017 17:21:23 +0000 Subject: [PATCH] add ability to execute command in a given directory --- src/python/dm/common/utility/dmSubprocess.py | 31 ++++++++++++++------ 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/python/dm/common/utility/dmSubprocess.py b/src/python/dm/common/utility/dmSubprocess.py index 7d4e5d93..e34d15b9 100755 --- a/src/python/dm/common/utility/dmSubprocess.py +++ b/src/python/dm/common/utility/dmSubprocess.py @@ -26,17 +26,17 @@ class DmSubprocess(subprocess.Popen): # Execute command @classmethod - def executeCommand(cls, command): + def executeCommand(cls, command, cwd=None): """ Create subprocess and run it, return subprocess object. """ - p = cls.getSubprocess(command) + p = cls.getSubprocess(command, cwd) p.run() return p # Execute command, ignore errors. @classmethod - def executeCommandAndIgnoreFailure(cls, command): + def executeCommandAndIgnoreFailure(cls, command, cwd=None): """ Create subprocess, run it, igore any failures, and return subprocess object. """ - p = cls.getSubprocess(command) + p = cls.getSubprocess(command, cwd) try: p.run() except CommandFailed, ex: @@ -44,9 +44,9 @@ class DmSubprocess(subprocess.Popen): return p @classmethod - def executeCommandAndLogToStdOut(cls, command): + def executeCommandAndLogToStdOut(cls, command, cwd=None): """ Execute command, display output to stdout, maintain log file and return subprocess object. """ - p = cls.getSubprocess(command) + p = cls.getSubprocess(command, cwd) p.__commandLog() while True: @@ -76,6 +76,7 @@ class DmSubprocess(subprocess.Popen): self._stdout = None self._stderr = None self._args = args + self._cwd = cwd self.useExceptions = useExceptions self.quietMode = quietMode @@ -125,13 +126,25 @@ class DmSubprocess(subprocess.Popen): def getExitStatus(self): return self.returncode + def toDict(self): + result = { + 'command' : self._args, + 'exitStatus' : self.returncode, + 'stdErr' : self._stderr, + 'stdOut' : self._stdout, + 'workingDir' : self._cwd + } + return result + ####################################################################### # Testing. if __name__ == '__main__': p = DmSubprocess('ls -l', useExceptions=False) + print p.toDict() p.run() - print p.getStdOut() - print p.getStdErr() - print p.getExitStatus() + #print p.getStdOut() + #print p.getStdErr() + #print p.getExitStatus() + print p.toDict() -- GitLab