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) 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 # Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are # modification, are permitted provided that the following conditions are
@@ -31,232 +30,251 @@
# 'identical' (determined via git-patch-id) and reapply reviews onto the new # 'identical' (determined via git-patch-id) and reapply reviews onto the new
# patchset from the previous patchset. # patchset from the previous patchset.
# Get usage and help info by running: ./trivial_rebase.py --help # Get usage and help info by running: trivial-rebase --help
# Documentation is available here: https://www.codeaurora.org/xwiki/bin/QAEP/Gerrit # Documentation is available here:
# https://www.codeaurora.org/xwiki/bin/QAEP/Gerrit
import json import json
import optparse
import subprocess import subprocess
from sys import exit import sys
class SilentOptionParser(optparse.OptionParser):
"""Make OptionParser silently swallow unrecognized options."""
def _process_args(self, largs, rargs, values):
while rargs:
try:
optparse.OptionParser._process_args(self, largs, rargs, values)
except (optparse.AmbiguousOptionError,
optparse.BadOptionError) as e:
largs.append(e.opt_str)
from optparse import OptionParser as _realOptionParser, AmbiguousOptionError, \
BadOptionError
class OptionParser(_realOptionParser):
"""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:
largs.append(e.opt_str)
class CheckCallError(OSError): class CheckCallError(OSError):
"""CheckCall() returned non-0.""" """CheckCall() returned non-0."""
def __init__(self, command, cwd, retcode, stdout, stderr=None): def __init__(self, command, cwd, retcode, stdout, stderr=None):
OSError.__init__(self, command, cwd, retcode, stdout, stderr) OSError.__init__(self, command, cwd, retcode, stdout, stderr)
self.command = command self.command = command
self.cwd = cwd self.cwd = cwd
self.retcode = retcode self.retcode = retcode
self.stdout = stdout self.stdout = stdout
self.stderr = stderr self.stderr = stderr
def CheckCall(command, cwd=None): def CheckCall(command, cwd=None):
"""Like subprocess.check_call() but returns stdout. """Like subprocess.check_call() but returns stdout.
Works on python 2.4
"""
try:
process = subprocess.Popen(command, cwd=cwd, stdout=subprocess.PIPE)
std_out, std_err = process.communicate()
except OSError, e:
raise CheckCallError(command, cwd, e.errno, None)
if process.returncode:
raise CheckCallError(command, cwd, process.returncode,
std_out, std_err)
return std_out, std_err
Works on python 2.4
"""
try:
process = subprocess.Popen(command, cwd=cwd, stdout=subprocess.PIPE)
std_out, std_err = process.communicate()
except OSError, e:
raise CheckCallError(command, cwd, e.errno, None)
if process.returncode:
raise CheckCallError(command, cwd, process.returncode, std_out, std_err)
return std_out, std_err
def Gssh(options, api_command): def Gssh(options, api_command):
"""Makes a Gerrit API call via SSH and returns the stdout results.""" """Makes a Gerrit API call via SSH and returns the stdout results."""
ssh_cmd = ['ssh', ssh_cmd = ['ssh',
'-l', 'Gerrit Code Review', '-l', 'Gerrit Code Review',
'-p', options.port, '-p', options.port,
'-i', options.private_key_path, '-i', options.private_key_path,
options.server, options.server,
api_command] api_command]
try: try:
return CheckCall(ssh_cmd)[0] return CheckCall(ssh_cmd)[0]
except CheckCallError, e: except CheckCallError as e:
import sys err_template = "call: %s\nreturn code: %s\nstdout: %s\nstderr: %s\n"
err_template = "call: %s\nreturn code: %s\nstdout: %s\nstderr: %s\n" sys.stderr.write(err_template % (ssh_cmd,
sys.stderr.write(err_template%(ssh_cmd, e.retcode, e.stdout, e.stderr)) e.retcode,
raise e.stdout,
e.stderr))
raise
def GsqlQuery(sql_query, options): def GsqlQuery(sql_query, options):
"""Runs a gerrit gsql query and returns the result""" """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) gsql_out = Gssh(options, gsql_cmd)
new_out = gsql_out.replace('}}\n', '}}\nsplit here\n') new_out = gsql_out.replace('}}\n', '}}\nsplit here\n')
return new_out.split('split here\n') return new_out.split('split here\n')
def FindPrevRev(options): def FindPrevRev(options):
"""Finds the revision of the previous patch set on the change""" """Finds the revision of the previous patch set on the change"""
sql_query = ("\"SELECT revision FROM patch_sets,changes WHERE " sql_query = ("\"SELECT revision FROM patch_sets,changes WHERE "
"patch_sets.change_id = changes.change_id AND " "patch_sets.change_id = changes.change_id AND "
"patch_sets.patch_set_id = %s AND " "patch_sets.patch_set_id = %s AND "
"changes.change_key = \'%s\'\"" % ((options.patchset - 1), "changes.change_key = \'%s\'\"" % ((options.patchset - 1),
options.changeId)) options.changeId))
revisions = GsqlQuery(sql_query, options) revisions = GsqlQuery(sql_query, options)
json_dict = json.loads(revisions[0], strict=False)
return json_dict["columns"]["revision"]
json_dict = json.loads(revisions[0], strict=False)
return json_dict["columns"]["revision"]
def GetApprovals(options): def GetApprovals(options):
"""Get all the approvals on a specific patch set """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\""
% ((options.patchset - 1), options.changeId))
gsql_out = GsqlQuery(sql_query, options)
approvals = []
for json_str in gsql_out:
dict = json.loads(json_str, strict=False)
if dict["type"] == "row":
approvals.append(dict["columns"])
return approvals
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\""
% ((options.patchset - 1), options.changeId))
gsql_out = GsqlQuery(sql_query, options)
approvals = []
for json_str in gsql_out:
dict = json.loads(json_str, strict=False)
if dict["type"] == "row":
approvals.append(dict["columns"])
return approvals
def GetPatchId(revision, consider_whitespace=False): def GetPatchId(revision, consider_whitespace=False):
git_show_cmd = ['git', 'show', revision] git_show_cmd = ['git', 'show', revision]
patch_id_cmd = ['git', 'patch-id'] patch_id_cmd = ['git', 'patch-id']
patch_id_process = subprocess.Popen(patch_id_cmd, stdout=subprocess.PIPE, patch_id_process = subprocess.Popen(patch_id_cmd, stdout=subprocess.PIPE,
stdin=subprocess.PIPE) stdin=subprocess.PIPE)
git_show_process = subprocess.Popen(git_show_cmd, stdout=subprocess.PIPE) git_show_process = subprocess.Popen(git_show_cmd, stdout=subprocess.PIPE)
if consider_whitespace: if consider_whitespace:
# This matches on change lines in the patch (those starting with "+" # This matches on change lines in the patch (those starting with "+"
# or "-" but not followed by another of the same), then replaces any # or "-" but not followed by another of the same), then replaces any
# space or tab characters with "%" before calculating a patch-id. # space or tab characters with "%" before calculating a patch-id.
replace_ws_cmd = ['sed', r'/^\(+[^+]\|-[^-]\)/y/ \t/%%/'] replace_ws_cmd = ['sed', r'/^\(+[^+]\|-[^-]\)/y/ \t/%%/']
replace_ws_process = subprocess.Popen(replace_ws_cmd, replace_ws_process = subprocess.Popen(replace_ws_cmd,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stdin=subprocess.PIPE) stdin=subprocess.PIPE)
return patch_id_process.communicate( git_show_output = git_show_process.communicate()[0]
replace_ws_process.communicate(git_show_process.communicate()[0])[0] replace_ws_output = replace_ws_process.communicate(git_show_output)[0]
)[0] return patch_id_process.communicate(replace_ws_output)[0]
else: 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): 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) Gssh(options, suexec_cmd)
def DiffCommitMessages(commit1, commit2): def DiffCommitMessages(commit1, commit2):
log_cmd1 = ['git', 'log', '--pretty=format:"%an %ae%n%s%n%b"', log_cmd1 = ['git', 'log', '--pretty=format:"%an %ae%n%s%n%b"',
commit1 + '^!'] commit1 + '^!']
commit1_log = CheckCall(log_cmd1) commit1_log = CheckCall(log_cmd1)
log_cmd2 = ['git', 'log', '--pretty=format:"%an %ae%n%s%n%b"', log_cmd2 = ['git', 'log', '--pretty=format:"%an %ae%n%s%n%b"',
commit2 + '^!'] commit2 + '^!']
commit2_log = CheckCall(log_cmd2) commit2_log = CheckCall(log_cmd2)
if commit1_log != commit2_log: if commit1_log != commit2_log:
return True return True
return False return False
def main(): def main():
usage = "usage: %prog <required options> [optional options]" 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("--change", dest="changeId", help="Change identifier")
parser.add_option("--project", help="Project path in Gerrit") parser.add_option("--project", help="Project path in Gerrit")
parser.add_option("--commit", help="Git commit-ish for this patchset") parser.add_option("--commit", help="Git commit-ish for this patchset")
parser.add_option("--patchset", type="int", help="The patchset number") parser.add_option("--patchset", type="int", help="The patchset number")
parser.add_option("--role-user", dest="role_user", parser.add_option("--role-user", dest="role_user",
help="E-mail/ID of user commenting on commit messages") help="E-mail/ID of user commenting on commit messages")
parser.add_option("--private-key-path", dest="private_key_path", parser.add_option("--private-key-path", dest="private_key_path",
help="Full path to Gerrit SSH daemon's private host key") help="Full path to Gerrit SSH daemon's private host key")
parser.add_option("--server-port", dest="port", default='29418', parser.add_option("--server-port", dest="port", default='29418',
help="Port to connect to Gerrit's SSH daemon " help="Port to connect to Gerrit's SSH daemon "
"[default: %default]") "[default: %default]")
parser.add_option("--server", dest="server", default="localhost", parser.add_option("--server", dest="server", default="localhost",
help="Server name/address for Gerrit's SSH daemon " help="Server name/address for Gerrit's SSH daemon "
"[default: %default]") "[default: %default]")
parser.add_option("--whitespace", action="store_true", parser.add_option("--whitespace", action="store_true",
help="Treat whitespace as significant") help="Treat whitespace as significant")
(options, args) = parser.parse_args() (options, args) = parser.parse_args()
if not options.changeId: if not options.changeId:
parser.print_help() parser.print_help()
exit(0) sys.exit(0)
if options.patchset == 1: if options.patchset == 1:
# Nothing to detect on first patchset # Nothing to detect on first patchset
exit(0) sys.exit(0)
prev_revision = None prev_revision = None
prev_revision = FindPrevRev(options) prev_revision = FindPrevRev(options)
if not prev_revision: if not prev_revision:
# Couldn't find a previous revision # Couldn't find a previous revision
exit(0) sys.exit(0)
prev_patch_id = GetPatchId(prev_revision) prev_patch_id = GetPatchId(prev_revision)
cur_patch_id = GetPatchId(options.commit) cur_patch_id = GetPatchId(options.commit)
if cur_patch_id.split()[0] != prev_patch_id.split()[0]: if cur_patch_id.split()[0] != prev_patch_id.split()[0]:
# patch-ids don't match # patch-ids don't match
exit(0) sys.exit(0)
# Patch ids match. This is a trivial rebase. # Patch ids match. This is a trivial rebase.
# In addition to patch-id we should check if whitespace content changed. Some # In addition to patch-id we should check if whitespace content changed.
# languages are more sensitive to whitespace than others, and some changes # Some languages are more sensitive to whitespace than others, and some
# may either introduce or be intended to fix style problems specifically # changes may either introduce or be intended to fix style problems
# involving whitespace as well. # specifically involving whitespace as well.
if options.whitespace: if options.whitespace:
prev_patch_ws = GetPatchId(prev_revision, consider_whitespace=True) prev_patch_ws = GetPatchId(prev_revision, consider_whitespace=True)
cur_patch_ws = GetPatchId(options.commit, consider_whitespace=True) cur_patch_ws = GetPatchId(options.commit, consider_whitespace=True)
if cur_patch_ws.split()[0] != prev_patch_ws.split()[0]: if cur_patch_ws.split()[0] != prev_patch_ws.split()[0]:
# Insert a comment into the change letting the approvers know only the # Insert a comment into the change letting the approvers know
# whitespace changed # only the whitespace changed
comment_msg = "\"New patchset patch-id matches previous patchset, " \ comment_msg = ("\"New patchset patch-id matches previous patchset,"
"but whitespace content has changed.\"" " but whitespace content has changed.\"")
comment_cmd = ['gerrit', 'approve', '--project', options.project, comment_cmd = ['gerrit', 'approve', '--project', options.project,
'--message', comment_msg, options.commit] '--message', comment_msg, options.commit]
SuExec(options, options.role_user, ' '.join(comment_cmd)) 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 # We should also check if the commit message changed. Most approvers would
# want to re-review changes when the commit message changes. # want to re-review changes when the commit message changes.
changed = DiffCommitMessages(prev_revision, options.commit) changed = DiffCommitMessages(prev_revision, options.commit)
if changed: if changed:
# Insert a comment into the change letting the approvers know only the # Insert a comment into the change letting the approvers know only the
# commit message changed # commit message changed
comment_msg = "\"New patchset patch-id matches previous patchset, " \ comment_msg = ("\"New patchset patch-id matches previous patchset,"
"but commit message has changed.\"" " but commit message has changed.\"")
comment_cmd = ['gerrit', 'approve', '--project', options.project, comment_cmd = ['gerrit', 'approve', '--project', options.project,
'--message', comment_msg, options.commit] '--message', comment_msg, options.commit]
SuExec(options, options.role_user, ' '.join(comment_cmd)) 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 # Need to get all approvals on prior patch set, then suexec them onto
# this patchset. # this patchset.
approvals = GetApprovals(options) approvals = GetApprovals(options)
gerrit_approve_msg = ("\'Automatically re-added by Gerrit trivial rebase " gerrit_approve_msg = ("\'Automatically re-added by Gerrit trivial rebase "
"detection script.\'") "detection script.\'")
for approval in approvals: for approval in approvals:
# Note: Sites with different 'copy_min_score' values in the # Note: Sites with different 'copy_min_score' values in the
# approval_categories DB table might want different behavior here. # approval_categories DB table might want different behavior here.
# Additional categories should also be added if desired. # Additional categories should also be added if desired.
if approval["category_id"] == "CRVW": if approval["category_id"] == "CRVW":
approve_category = '--code-review' approve_category = '--code-review'
elif approval["category_id"] == "VRIF": elif approval["category_id"] == "VRIF":
# Don't re-add verifies # Don't re-add verifies
#approve_category = '--verified' #approve_category = '--verified'
continue continue
elif approval["category_id"] == "SUBM": elif approval["category_id"] == "SUBM":
# We don't care about previous submit attempts # We don't care about previous submit attempts
continue continue
elif approval["category_id"] == "APRV": elif approval["category_id"] == "APRV":
# Similarly squash old approvals # Similarly squash old approvals
continue continue
else: else:
print "Unsupported category: %s" % approval print "Unsupported category: %s" % approval
exit(0) sys.exit(0)
score = approval["value"] score = approval["value"]
gerrit_approve_cmd = ['gerrit', 'approve', '--project', options.project, gerrit_approve_cmd = ['gerrit', 'approve',
'--message', gerrit_approve_msg, approve_category, '--project', options.project,
score, options.commit] '--message', gerrit_approve_msg,
SuExec(options, approval["account_id"], ' '.join(gerrit_approve_cmd)) approve_category, score, options.commit]
exit(0) SuExec(options, approval["account_id"], ' '.join(gerrit_approve_cmd))
sys.exit(0)