From fada06f52260bc78ae38aebd558101b916cf934a Mon Sep 17 00:00:00 2001 From: Jesse Keating Date: Wed, 23 Jun 2010 23:25:18 -0700 Subject: Add a function to run commands This saves some code duplication and allows us to actually stream output as it happens, when on a real tty. Woo! --- src/pyfedpkg/__init__.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/pyfedpkg/__init__.py b/src/pyfedpkg/__init__.py index a42bc51..2ced0cc 100644 --- a/src/pyfedpkg/__init__.py +++ b/src/pyfedpkg/__init__.py @@ -10,6 +10,7 @@ # the full text of the license. import os +import sys #import pycurl import subprocess import hashlib @@ -66,6 +67,61 @@ def _hash_file(file, hashtype): input.close() return sum.hexdigest() +def _run_command(cmd, shell=False, env=None): + """Run the given command. + + Will determine if caller is on a real tty and if so stream to the tty + + Or else will run and log output. + + cmd is a list of the command and arguments + + shell is whether to run in a shell or not, defaults to False + + env is a dict of environment variables to use (if any) + + Raises on error, or returns nothing. + + """ + + # Process any environment vairables. + environ = os.environ + if env: + for item in env.keys(): + environ[item] = env[item] + # Check if we're supposed to be on a shell. If so, the command must + # be a string, and not a list. + command = cmd + if shell: + command = ' '.join(cmd) + # Check to see if we're on a real tty, if so, stream it baby! + if sys.stdout.isatty(): + try: + subprocess.check_call(command, env=environ, stdout=sys.stdout, + stderr=sys.stderr, shell=shell) + except subprocess.CalledProcessError, e: + raise FedpkgError(e) + except KeyboardInterrupt: + raise FedpkgError() + else: + # Ok, we're not on a live tty, so pipe and log. + try: + proc = subprocess.Popen(command, env=environ, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, shell=shell) + output, error = proc.communicate() + except OSError, e: + raise FedpkgError(e) + log.info(output) + if error: + log.error(error) + if proc.returncode: + raise FedpkgError('Command %s returned code %s with error: %s' % + (subprocess.list2cmdline(cmd), + proc.returncode, + error)) + return + def _verify_file(file, hash, hashtype): """Given a file, a hash of that file, and a hashtype, verify. -- cgit