Mercurial > treepkg > treepkg
view treepkg/run.py @ 557:9824e409388b
Refactor git branching
If a checkout is already available and the branch is changed in
the config git command would always fail because it doesn't know
the branch to track. Therefore always check if the branch is
locally available and if not checkout the remote branch
author | Bjoern Ricks <bricks@intevation.de> |
---|---|
date | Fri, 02 Sep 2011 08:45:28 +0000 |
parents | eacfd3744d16 |
children |
line wrap: on
line source
# Copyright (C) 2007, 2008 by Intevation GmbH # Authors: # Bernhard Herzog <bh@intevation.de> # # This program is free software under the GPL (>=v2) # Read the file COPYING coming with the software for details. """Helper functions to run subprocesses in various ways""" import os import subprocess class SubprocessError(EnvironmentError): def __init__(self, command, returncode, output=None): EnvironmentError.__init__(self, "Command %r finished with return code %d" % (command, returncode)) self.returncode = returncode self.output = output def call(command, suppress_output=False, extra_env=None, inputdata=None, logfile=None, **kw): """Run command as a subprocess and wait until it is finished. The command should be given as a list of strings to avoid problems with shell quoting. If the command exits with a return code other than 0, a SubprocessError is raised. """ if inputdata is not None: kw["stdin"] = subprocess.PIPE if logfile: kw["stdout"] = kw["stderr"] = open(logfile, "w") elif suppress_output: kw["stdout"] = open(os.devnull, "w") kw["stderr"] = open(os.devnull, "w") env = kw.pop("env", None) if extra_env: if env is None: env = os.environ.copy() env.update(extra_env) try: process = subprocess.Popen(command, env=env, **kw) except OSError,e: raise SubprocessError(command, e.errno, e.strerror) if inputdata is not None: process.stdin.write(inputdata) process.stdin.close() ret = process.wait() if ret != 0: raise SubprocessError(command, ret) def capture_output(command, **kw): """Return the stdout and stderr of the command as a string The command should be given as a list of strings to avoid problems with shell quoting. If the command exits with a return code other than 0, a SubprocessError is raised. """ proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, **kw) output = proc.communicate()[0] if proc.returncode != 0: raise SubprocessError(command, proc.returncode, output) return output def capture_stdout(command, **kw): """Return the stdout and stderr of the command as a string The command should be given as a list of strings to avoid problems with shell quoting. If the command exits with a return code other than 0, a SubprocessError is raised. """ proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kw) output, error = proc.communicate() if proc.returncode != 0: raise SubprocessError(command, proc.returncode, error) return output