Add a _run_command() function that executes shell commands.
BUG=None
TEST=Uploaded this CL
Change-Id: Ifec031352cd1d6ec90ad3e523cf08b61e1315269
Reviewed-on: http://gerrit.chromium.org/gerrit/375
Reviewed-by: Mandeep Singh Baines <msb@chromium.org>
Tested-by: Ryan Cui <rcui@chromium.org>
diff --git a/pre-upload.py b/pre-upload.py
index b12c89f..2f70579 100644
--- a/pre-upload.py
+++ b/pre-upload.py
@@ -35,10 +35,14 @@
MIN_GIT_VERSION = [1, 7, 2]
+def _run_command(cmd):
+ """Executes the passed in command and returns raw stdout output."""
+ return subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
+
def _get_hooks_dir():
"""Returns the absolute path to the repohooks directory."""
cmd = ['repo', 'forall', 'chromiumos/repohooks', '-c', 'pwd']
- return subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0].strip()
+ return _run_command(cmd).strip()
def _match_regex_list(subject, expressions):
"""Try to match a list of regular expressions to a string.
@@ -94,6 +98,7 @@
# Git Helpers
+
def _check_git_version():
"""Checks the git version installed, dies if it is insufficient"""
cmd = ['git', '--version']
@@ -114,13 +119,11 @@
def _get_diff(commit):
"""Returns the diff for this commit."""
- cmd = ['git', 'show', commit]
- return subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
+ return _run_command(['git', 'show', commit])
def _get_file_diff(file, commit):
"""Returns a list of (linenum, lines) tuples that the commit touched."""
- cmd = ['git', 'show', '-p', '--no-ext-diff', commit, file]
- output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
+ output = _run_command(['git', 'show', '-p', '--no-ext-diff', commit, file])
new_lines = []
line_num = 0
@@ -137,8 +140,7 @@
def _get_affected_files(commit):
"""Returns list of absolute filepaths that were modified/added."""
- cmd = ['git', 'diff', '--name-status', commit + '^!']
- output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
+ output = _run_command(['git', 'diff', '--name-status', commit + '^!'])
files = []
for statusline in output.splitlines():
m = re.match('^(\w)+\t(.+)$', statusline.rstrip())
@@ -151,13 +153,11 @@
def _get_commits():
"""Returns a list of commits for this review."""
cmd = ['git', 'log', 'm/master..', '--format=%H']
- commits = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
- return commits.split()
+ return _run_command(cmd).split()
def _get_commit_desc(commit):
"""Returns the full commit message of a commit."""
- cmd = ['git', 'log', '--format=%B', commit + '^!']
- return subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
+ return _run_command(['git', 'log', '--format=%B', commit + '^!'])
# Common Hooks
@@ -319,9 +319,7 @@
def _run_project_hooks(project, hooks):
"""For each project run its project specific hook from the hooks dictionary"""
- cmd = ['repo', 'forall', project, '-c', 'pwd']
- proj_dir = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
- proj_dir = proj_dir.strip()
+ proj_dir = _run_command(['repo', 'forall', project, '-c', 'pwd']).strip()
pwd = os.getcwd()
# hooks assume they are run from the root of the project
os.chdir(proj_dir)
@@ -341,6 +339,7 @@
raise
os.chdir(pwd)
+
# Main
def main(project_list, **kwargs):