Cleaned up pep8 and pyflakes on trivial_rebase.

This commit is contained in:
Monty Taylor
2012-11-22 11:07:34 -08:00
parent a2b0de65dd
commit bb1e652313

View File

@@ -1,6 +1,5 @@
#!/usr/bin/env python2.6
# Copyright (c) 2010, Code Aurora Forum. All rights reserved.
# Copyright (c) 2012, Hewlett-Packard Development Company, L.P.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
@@ -31,24 +30,27 @@
# 'identical' (determined via git-patch-id) and reapply reviews onto the new
# patchset from the previous patchset.
# Get usage and help info by running: ./trivial_rebase.py --help
# Documentation is available here: https://www.codeaurora.org/xwiki/bin/QAEP/Gerrit
# Get usage and help info by running: trivial-rebase --help
# Documentation is available here:
# https://www.codeaurora.org/xwiki/bin/QAEP/Gerrit
import json
import optparse
import subprocess
from sys import exit
import sys
from optparse import OptionParser as _realOptionParser, AmbiguousOptionError, \
BadOptionError
class OptionParser(_realOptionParser):
class SilentOptionParser(optparse.OptionParser):
"""Make OptionParser silently swallow unrecognized options."""
def _process_args(self, largs, rargs, values):
while rargs:
try:
_realOptionParser._process_args(self, largs, rargs, values)
except (AmbiguousOptionError, BadOptionError), e:
optparse.OptionParser._process_args(self, largs, rargs, values)
except (optparse.AmbiguousOptionError,
optparse.BadOptionError) as e:
largs.append(e.opt_str)
class CheckCallError(OSError):
"""CheckCall() returned non-0."""
def __init__(self, command, cwd, retcode, stdout, stderr=None):
@@ -59,6 +61,7 @@ class CheckCallError(OSError):
self.stdout = stdout
self.stderr = stderr
def CheckCall(command, cwd=None):
"""Like subprocess.check_call() but returns stdout.
@@ -70,9 +73,11 @@ def CheckCall(command, cwd=None):
except OSError, e:
raise CheckCallError(command, cwd, e.errno, None)
if process.returncode:
raise CheckCallError(command, cwd, process.returncode, std_out, std_err)
raise CheckCallError(command, cwd, process.returncode,
std_out, std_err)
return std_out, std_err
def Gssh(options, api_command):
"""Makes a Gerrit API call via SSH and returns the stdout results."""
ssh_cmd = ['ssh',
@@ -83,19 +88,23 @@ def Gssh(options, api_command):
api_command]
try:
return CheckCall(ssh_cmd)[0]
except CheckCallError, e:
import sys
except CheckCallError as e:
err_template = "call: %s\nreturn code: %s\nstdout: %s\nstderr: %s\n"
sys.stderr.write(err_template%(ssh_cmd, e.retcode, e.stdout, e.stderr))
sys.stderr.write(err_template % (ssh_cmd,
e.retcode,
e.stdout,
e.stderr))
raise
def GsqlQuery(sql_query, options):
"""Runs a gerrit gsql query and returns the result"""
gsql_cmd = "gerrit gsql --format JSON -c %s"%sql_query
gsql_cmd = "gerrit gsql --format JSON -c %s" % sql_query
gsql_out = Gssh(options, gsql_cmd)
new_out = gsql_out.replace('}}\n', '}}\nsplit here\n')
return new_out.split('split here\n')
def FindPrevRev(options):
"""Finds the revision of the previous patch set on the change"""
sql_query = ("\"SELECT revision FROM patch_sets,changes WHERE "
@@ -108,13 +117,16 @@ def FindPrevRev(options):
json_dict = json.loads(revisions[0], strict=False)
return json_dict["columns"]["revision"]
def GetApprovals(options):
"""Get all the approvals on a specific patch set
Returns a list of approval dicts"""
sql_query = ("\"SELECT value,account_id,category_id FROM patch_set_approvals "
"WHERE patch_set_id = %s AND change_id = (SELECT change_id FROM "
"changes WHERE change_key = \'%s\') AND value <> 0\""
sql_query = ("\"SELECT value,account_id,category_id"
" FROM patch_set_approvals"
" WHERE patch_set_id = %s"
" AND change_id = (SELECT change_id FROM"
" changes WHERE change_key = \'%s\') AND value <> 0\""
% ((options.patchset - 1), options.changeId))
gsql_out = GsqlQuery(sql_query, options)
approvals = []
@@ -124,6 +136,7 @@ def GetApprovals(options):
approvals.append(dict["columns"])
return approvals
def GetPatchId(revision, consider_whitespace=False):
git_show_cmd = ['git', 'show', revision]
patch_id_cmd = ['git', 'patch-id']
@@ -138,16 +151,19 @@ def GetPatchId(revision, consider_whitespace=False):
replace_ws_process = subprocess.Popen(replace_ws_cmd,
stdout=subprocess.PIPE,
stdin=subprocess.PIPE)
return patch_id_process.communicate(
replace_ws_process.communicate(git_show_process.communicate()[0])[0]
)[0]
git_show_output = git_show_process.communicate()[0]
replace_ws_output = replace_ws_process.communicate(git_show_output)[0]
return patch_id_process.communicate(replace_ws_output)[0]
else:
return patch_id_process.communicate(git_show_process.communicate()[0])[0]
return patch_id_process.communicate(
git_show_process.communicate()[0])[0]
def SuExec(options, as_user, cmd):
suexec_cmd = "suexec --as %s -- %s"%(as_user, cmd)
suexec_cmd = "suexec --as %s -- %s" % (as_user, cmd)
Gssh(options, suexec_cmd)
def DiffCommitMessages(commit1, commit2):
log_cmd1 = ['git', 'log', '--pretty=format:"%an %ae%n%s%n%b"',
commit1 + '^!']
@@ -159,9 +175,10 @@ def DiffCommitMessages(commit1, commit2):
return True
return False
def main():
usage = "usage: %prog <required options> [optional options]"
parser = OptionParser(usage=usage)
parser = SilentOptionParser(usage=usage)
parser.add_option("--change", dest="changeId", help="Change identifier")
parser.add_option("--project", help="Project path in Gerrit")
parser.add_option("--commit", help="Git commit-ish for this patchset")
@@ -183,38 +200,38 @@ def main():
if not options.changeId:
parser.print_help()
exit(0)
sys.exit(0)
if options.patchset == 1:
# Nothing to detect on first patchset
exit(0)
sys.exit(0)
prev_revision = None
prev_revision = FindPrevRev(options)
if not prev_revision:
# Couldn't find a previous revision
exit(0)
sys.exit(0)
prev_patch_id = GetPatchId(prev_revision)
cur_patch_id = GetPatchId(options.commit)
if cur_patch_id.split()[0] != prev_patch_id.split()[0]:
# patch-ids don't match
exit(0)
sys.exit(0)
# Patch ids match. This is a trivial rebase.
# In addition to patch-id we should check if whitespace content changed. Some
# languages are more sensitive to whitespace than others, and some changes
# may either introduce or be intended to fix style problems specifically
# involving whitespace as well.
# In addition to patch-id we should check if whitespace content changed.
# Some languages are more sensitive to whitespace than others, and some
# changes may either introduce or be intended to fix style problems
# specifically involving whitespace as well.
if options.whitespace:
prev_patch_ws = GetPatchId(prev_revision, consider_whitespace=True)
cur_patch_ws = GetPatchId(options.commit, consider_whitespace=True)
if cur_patch_ws.split()[0] != prev_patch_ws.split()[0]:
# Insert a comment into the change letting the approvers know only the
# whitespace changed
comment_msg = "\"New patchset patch-id matches previous patchset, " \
"but whitespace content has changed.\""
# Insert a comment into the change letting the approvers know
# only the whitespace changed
comment_msg = ("\"New patchset patch-id matches previous patchset,"
" but whitespace content has changed.\"")
comment_cmd = ['gerrit', 'approve', '--project', options.project,
'--message', comment_msg, options.commit]
SuExec(options, options.role_user, ' '.join(comment_cmd))
exit(0)
sys.exit(0)
# We should also check if the commit message changed. Most approvers would
# want to re-review changes when the commit message changes.
@@ -222,12 +239,12 @@ def main():
if changed:
# Insert a comment into the change letting the approvers know only the
# commit message changed
comment_msg = "\"New patchset patch-id matches previous patchset, " \
"but commit message has changed.\""
comment_msg = ("\"New patchset patch-id matches previous patchset,"
" but commit message has changed.\"")
comment_cmd = ['gerrit', 'approve', '--project', options.project,
'--message', comment_msg, options.commit]
SuExec(options, options.role_user, ' '.join(comment_cmd))
exit(0)
sys.exit(0)
# Need to get all approvals on prior patch set, then suexec them onto
# this patchset.
@@ -252,11 +269,12 @@ def main():
continue
else:
print "Unsupported category: %s" % approval
exit(0)
sys.exit(0)
score = approval["value"]
gerrit_approve_cmd = ['gerrit', 'approve', '--project', options.project,
'--message', gerrit_approve_msg, approve_category,
score, options.commit]
gerrit_approve_cmd = ['gerrit', 'approve',
'--project', options.project,
'--message', gerrit_approve_msg,
approve_category, score, options.commit]
SuExec(options, approval["account_id"], ' '.join(gerrit_approve_cmd))
exit(0)
sys.exit(0)