diff options
author | Hans Ulrich Niedermann <hun@n-dimensional.de> | 2008-06-23 01:46:32 +0200 |
---|---|---|
committer | Hans Ulrich Niedermann <hun@n-dimensional.de> | 2008-07-15 12:28:52 +0200 |
commit | 41a34022fc7ee1831d20d5e13c459c33fc001732 (patch) | |
tree | 510faa9b313fb6df6c50c09638a833fe62515eb7 /src/nbblib/progutils.py | |
parent | 62819016f187acd945fad5b77efde78f88235555 (diff) | |
download | nbb-41a34022fc7ee1831d20d5e13c459c33fc001732.tar.gz nbb-41a34022fc7ee1831d20d5e13c459c33fc001732.tar.xz nbb-41a34022fc7ee1831d20d5e13c459c33fc001732.zip |
Rename nbb/ subdir to src/
Diffstat (limited to 'src/nbblib/progutils.py')
-rw-r--r-- | src/nbblib/progutils.py | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/nbblib/progutils.py b/src/nbblib/progutils.py new file mode 100644 index 0000000..456405e --- /dev/null +++ b/src/nbblib/progutils.py @@ -0,0 +1,55 @@ +######################################################################## +# Utility functions +######################################################################## + + +import os +import subprocess + + +def prog_stdout(call_list): + """Run program and return stdout (similar to shell backticks)""" + p = subprocess.Popen(call_list, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + stdout, stderr = p.communicate(input=None) + return stdout.strip() + + +def prog_retstd(call_list): + """Run program and return stdout (similar to shell backticks)""" + p = subprocess.Popen(call_list, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + stdout, stderr = p.communicate(input=None) + return (p.returncode, stdout.strip(), stderr.strip()) + + +class ProgramRunError(Exception): + """A program run returns a retcode != 0""" + def __init__(self, call_list, retcode, cwd=None): + self.call_list = call_list + self.retcode = retcode + if cwd: + self.cwd = cwd + else: + self.cwd = os.getcwd() + def __str__(self): + return ("Error running program (%s, retcode=%d, cwd=%s)" + % (repr(self.call_list), + self.retcode, + repr(self.cwd))) + + +def prog_run(call_list, context): + """Run program showing its output. Raise exception if retcode != 0.""" + print "RUN:", call_list + print " in", os.getcwd() + if context.dry_run: + return None + p = subprocess.Popen(call_list) + stdout, stderr = p.communicate(input=None) + if p.returncode != 0: + raise ProgramRunError(call_list, p.returncode, os.getcwd()) + return p.returncode + |