Skip to content
Snippets Groups Projects
Commit 6da4acf9 authored by sveseli's avatar sveseli
Browse files

add ability to execute command in a given directory

parent 464d4b29
No related branches found
No related tags found
No related merge requests found
......@@ -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()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment