From 64de44f9c840da0d51e9a91da08e3abb043d235a Mon Sep 17 00:00:00 2001 From: Jesse Keating Date: Fri, 20 Aug 2010 14:42:29 -0700 Subject: Add support for running a command from a specific dir --- src/pyfedpkg/__init__.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/pyfedpkg/__init__.py b/src/pyfedpkg/__init__.py index 53d218d..f4a02bf 100644 --- a/src/pyfedpkg/__init__.py +++ b/src/pyfedpkg/__init__.py @@ -91,7 +91,7 @@ def _hash_file(file, hashtype): input.close() return sum.hexdigest() -def _run_command(cmd, shell=False, env=None, pipe=[]): +def _run_command(cmd, shell=False, env=None, pipe=[], cwd=None): """Run the given command. Will determine if caller is on a real tty and if so stream to the tty @@ -106,6 +106,8 @@ def _run_command(cmd, shell=False, env=None, pipe=[]): pipe is a command to pipe the output of cmd into + cwd is the optional directory to run the command from + Raises on error, or returns nothing. """ @@ -136,15 +138,18 @@ def _run_command(cmd, shell=False, env=None, pipe=[]): if pipe: proc = subprocess.Popen(command, env=environ, stdout=subprocess.PIPE, - stderr=sys.stderr, shell=shell) + stderr=sys.stderr, shell=shell, + cwd=cwd) subprocess.check_call(pipecmd, env=environ, stdout=sys.stdout, stderr=sys.stderr, stdin=proc.stdout, - shell=shell) + shell=shell, + cwd=cwd) else: subprocess.check_call(command, env=environ, stdout=sys.stdout, - stderr=sys.stderr, shell=shell) + stderr=sys.stderr, shell=shell, + cwd=cwd) except (subprocess.CalledProcessError, OSError), e: raise FedpkgError(e) @@ -163,16 +168,19 @@ def _run_command(cmd, shell=False, env=None, pipe=[]): if pipe: proc1 = subprocess.Popen(command, env=environ, stdout=subprocess.PIPE, - stderr=subprocess.PIPE, shell=shell) + stderr=subprocess.PIPE, shell=shell, + cwd=cwd) proc2 = subprocess.Popen(pipecmd, env=environ, stdin=proc1.stdout, stdout=subprocess.PIPE, - stderr=subprocess.PIPE, shell=shell) + stderr=subprocess.PIPE, shell=shell, + cwd=cwd) output, error = proc2.communicate() else: proc = subprocess.Popen(command, env=environ, stdout=subprocess.PIPE, - stderr=subprocess.PIPE, shell=shell) + stderr=subprocess.PIPE, shell=shell, + cwd=cwd) output, error = proc.communicate() except OSError, e: raise FedpkgError(e) -- cgit