summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarkus Lehtonen <markus.lehtonen@linux.intel.com>2012-09-18 16:31:58 +0300
committerMarkus Lehtonen <markus.lehtonen@linux.intel.com>2012-09-18 19:02:51 +0300
commitd38df5e4914d62f636e280464ead7cb544618dd6 (patch)
tree4056821e917dffb9d80e50da1988ddfc816fe6af
parent51afd693e20fb1f410235684be64ccb6d953d457 (diff)
downloadgit-buildpackage-d38df5e4914d62f636e280464ead7cb544618dd6.tar.gz
git-buildpackage-d38df5e4914d62f636e280464ead7cb544618dd6.tar.bz2
git-buildpackage-d38df5e4914d62f636e280464ead7cb544618dd6.zip
GitRepository/_git_inout: implement support for filtering
Support filtering of the output of git command through a python function. Signed-off-by: Markus Lehtonen <markus.lehtonen@linux.intel.com>
-rw-r--r--gbp/git/repository.py32
1 files changed, 26 insertions, 6 deletions
diff --git a/gbp/git/repository.py b/gbp/git/repository.py
index b1e79059..130b9010 100644
--- a/gbp/git/repository.py
+++ b/gbp/git/repository.py
@@ -131,9 +131,11 @@ class GitRepository(object):
return output, popen.returncode
def _git_inout(self, command, args, input=None, extra_env=None, cwd=None,
- capture_stderr=False):
+ capture_stderr=False, output_f=None, filter_fn=None,
+ filter_kwargs={}):
"""
- Run a git command with input and return output
+ Run a git command with input and return output. Alternatively write
+ the output to a file.
@param command: git command to run
@type command: C{str}
@@ -145,24 +147,42 @@ class GitRepository(object):
@type extra_env: C{dict}
@param capture_stderr: whether to capture stderr
@type capture_stderr: C{bool}
+ @param output_f: file object where to write git output
+ @type output_f: C{file}
+ @param filter_fn: function throught which to filter the git output
+ @type filter_fn: C{function}
+ @param filter_kwargs: arguments to pass to the filter fn
+ @type filter_kwargs: C{dict}
@return: stdout, stderr, return code
@rtype: C{tuple} of C{str}, C{str}, C{int}
"""
if not cwd:
cwd = self.path
- stderr_arg = subprocess.PIPE if capture_stderr else None
-
env = self.__build_env(extra_env)
cmd = ['git', command] + args
- log.debug(cmd)
+ debug_msg = " ".join(cmd)
+ if output_f:
+ debug_msg += " > %s" % output_f.name
+ if filter_fn:
+ debug_msg += ", filtering through %s()" % filter_fn.__name__
+ log.debug(debug_msg)
+
+ if not output_f:
+ output_f = subprocess.PIPE
+ stdout_arg = subprocess.PIPE if filter_fn else output_f
+ stderr_arg = subprocess.PIPE if capture_stderr else None
popen = subprocess.Popen(cmd,
stdin=subprocess.PIPE,
- stdout=subprocess.PIPE,
+ stdout=stdout_arg,
stderr=stderr_arg,
env=env,
cwd=cwd)
+ if filter_fn:
+ if filter_fn(popen.stdout, output_f, **filter_kwargs):
+ raise GitRepositoryError("Filtering git output in failed!")
(stdout, stderr) = popen.communicate(input)
+
return stdout, stderr, popen.returncode
def _git_command(self, command, args=[], extra_env=None):