Resolves cwd problem

This commit is contained in:
Anthony Sottile
2014-03-29 23:23:43 -07:00
parent 6f0d566199
commit 216b5c6ab1
13 changed files with 212 additions and 107 deletions

View File

@@ -17,26 +17,42 @@ class PrefixedCommandRunner(object):
will run ['/tmpl/foo/foo.sh', 'bar', 'baz']
"""
def __init__(self, prefix_dir, popen=subprocess.Popen):
def __init__(self, prefix_dir, popen=subprocess.Popen, makedirs=os.makedirs):
self.prefix_dir = prefix_dir.rstrip(os.sep) + os.sep
self.__popen = popen
self.__makedirs = makedirs
def run(self, cmd, stdin=None):
def _create_path_if_not_exists(self):
if not os.path.exists(self.prefix_dir):
self.__makedirs(self.prefix_dir)
def run(self, cmd, retcode=0, stdin=None, **kwargs):
self._create_path_if_not_exists()
replaced_cmd = _replace_cmd(cmd, prefix=self.prefix_dir)
proc = self.__popen(
replaced_cmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
**kwargs
)
stdout, stderr = proc.communicate(stdin)
returncode = proc.returncode
if retcode is not None and retcode != returncode:
raise subprocess.CalledProcessError(
returncode, replaced_cmd, output=(stdout, stderr),
)
return proc.returncode, stdout, stderr
def path(self, path_end):
path = os.path.join(self.prefix_dir, path_end)
def path(self, *parts):
path = os.path.join(self.prefix_dir, *parts)
return os.path.normpath(path)
def exists(self, *parts):
return os.path.exists(self.path(*parts))
@classmethod
def from_command_runner(cls, command_runner, path_end):
"""Constructs a new command runner from an existing one by appending