Skip to content

Commit

Permalink
Extends the function so it can return stderr and the process return
Browse files Browse the repository at this point in the history
code as well. Its interface remains compatible.

git-svn-id: svn://cherokee-project.com/cherokee/trunk@5470 5dc97367-97f1-0310-9951-d761b3857238
  • Loading branch information
alobbs committed Sep 9, 2010
1 parent 5dc720f commit d4853a1
Showing 1 changed file with 46 additions and 6 deletions.
52 changes: 46 additions & 6 deletions admin/util.py
Expand Up @@ -34,12 +34,52 @@
#
# Deal with os.popen and subprocess issues in Python 2.4
#
def run (command):
p = subprocess.Popen (command, shell=True, stdout=subprocess.PIPE, close_fds=True)
output = p.stdout.read()
p.stdout.close()
p.poll()
return output
def run (command, stdout=True, stderr=False, retcode=False):
returns = 0

args = {'shell': True, 'close_fds': True}
if stdout:
args['stdout'] = subprocess.PIPE
returns += 1
if stderr:
args['stderr'] = subprocess.PIPE
returns += 1
if retcode:
returns += 1

p = subprocess.Popen (command, **args)

if stdout:
stdout_output = p.stdout.read()
if stderr:
stderr_output = p.stderr.read()

if stdout:
p.stdout.close()
if stderr:
p.stderr.close()

ret = p.poll()

if returns > 1:
d = {}
if stdout:
d['stdout'] = stdout_output
if stderr:
d['stderr'] = stderr_output
if retcode:
d['retcode'] = ret
return d

if stdout:
return stdout_output
elif stderr:
return stderr_output
elif retcode:
return ret

assert False, "Should not happened"


#
# Strings
Expand Down

0 comments on commit d4853a1

Please sign in to comment.