Brad Larson | c575f03 | 2010-06-08 17:24:37 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
Chirayu Desai | 4c5ee48 | 2013-05-13 13:48:43 +0530 | [diff] [blame] | 2 | |
| 3 | from __future__ import print_function |
| 4 | |
| 5 | import subprocess |
Brad Larson | c575f03 | 2010-06-08 17:24:37 -0500 | [diff] [blame] | 6 | import getopt |
| 7 | import sys |
| 8 | |
| 9 | |
| 10 | SSH_USER = 'bot' |
| 11 | SSH_HOST = 'localhost' |
| 12 | SSH_PORT = 29418 |
| 13 | SSH_COMMAND = 'ssh %s@%s -p %d gerrit approve ' % (SSH_USER, SSH_HOST, SSH_PORT) |
| 14 | FAILURE_SCORE = '--code-review=-2' |
| 15 | FAILURE_MESSAGE = 'This commit message does not match the standard.' \ |
| 16 | + ' Please correct the commit message and upload a replacement patch.' |
| 17 | PASS_SCORE = '--code-review=0' |
| 18 | PASS_MESSAGE = '' |
| 19 | |
| 20 | def main(): |
| 21 | change = None |
| 22 | project = None |
| 23 | branch = None |
| 24 | commit = None |
| 25 | patchset = None |
| 26 | |
| 27 | try: |
| 28 | opts, args = getopt.getopt(sys.argv[1:], '', \ |
| 29 | ['change=', 'project=', 'branch=', 'commit=', 'patchset=']) |
Chirayu Desai | 4c5ee48 | 2013-05-13 13:48:43 +0530 | [diff] [blame] | 30 | except getopt.GetoptError as err: |
| 31 | print('Error: %s' % (err)) |
Brad Larson | c575f03 | 2010-06-08 17:24:37 -0500 | [diff] [blame] | 32 | usage() |
| 33 | sys.exit(-1) |
| 34 | |
| 35 | for arg, value in opts: |
| 36 | if arg == '--change': |
| 37 | change = value |
| 38 | elif arg == '--project': |
| 39 | project = value |
| 40 | elif arg == '--branch': |
| 41 | branch = value |
| 42 | elif arg == '--commit': |
| 43 | commit = value |
| 44 | elif arg == '--patchset': |
| 45 | patchset = value |
| 46 | else: |
Chirayu Desai | 4c5ee48 | 2013-05-13 13:48:43 +0530 | [diff] [blame] | 47 | print('Error: option %s not recognized' % (arg)) |
Brad Larson | c575f03 | 2010-06-08 17:24:37 -0500 | [diff] [blame] | 48 | usage() |
| 49 | sys.exit(-1) |
| 50 | |
| 51 | if change == None or project == None or branch == None \ |
| 52 | or commit == None or patchset == None: |
| 53 | usage() |
| 54 | sys.exit(-1) |
| 55 | |
| 56 | command = 'git cat-file commit %s' % (commit) |
Chirayu Desai | 4c5ee48 | 2013-05-13 13:48:43 +0530 | [diff] [blame] | 57 | status, output = subprocess.getstatusoutput(command) |
Brad Larson | c575f03 | 2010-06-08 17:24:37 -0500 | [diff] [blame] | 58 | |
| 59 | if status != 0: |
Chirayu Desai | 4c5ee48 | 2013-05-13 13:48:43 +0530 | [diff] [blame] | 60 | print('Error running \'%s\'. status: %s, output:\n\n%s' % \ |
| 61 | (command, status, output)) |
Brad Larson | c575f03 | 2010-06-08 17:24:37 -0500 | [diff] [blame] | 62 | sys.exit(-1) |
| 63 | |
| 64 | commitMessage = output[(output.find('\n\n')+2):] |
| 65 | commitLines = commitMessage.split('\n') |
| 66 | |
| 67 | if len(commitLines) > 1 and len(commitLines[1]) != 0: |
| 68 | fail(commit, 'Invalid commit summary. The summary must be ' \ |
| 69 | + 'one line followed by a blank line.') |
| 70 | |
| 71 | i = 0 |
| 72 | for line in commitLines: |
| 73 | i = i + 1 |
| 74 | if len(line) > 80: |
| 75 | fail(commit, 'Line %d is over 80 characters.' % i) |
| 76 | |
| 77 | passes(commit) |
| 78 | |
| 79 | def usage(): |
Chirayu Desai | 4c5ee48 | 2013-05-13 13:48:43 +0530 | [diff] [blame] | 80 | print('Usage:\n') |
| 81 | print(sys.argv[0] + ' --change <change id> --project <project name> ' \ |
| 82 | + '--branch <branch> --commit <sha1> --patchset <patchset id>') |
Brad Larson | c575f03 | 2010-06-08 17:24:37 -0500 | [diff] [blame] | 83 | |
| 84 | def fail( commit, message ): |
| 85 | command = SSH_COMMAND + FAILURE_SCORE + ' -m \\\"' \ |
| 86 | + _shell_escape( FAILURE_MESSAGE + '\n\n' + message) \ |
| 87 | + '\\\" ' + commit |
Chirayu Desai | 4c5ee48 | 2013-05-13 13:48:43 +0530 | [diff] [blame] | 88 | subprocess.getstatusoutput(command) |
Brad Larson | c575f03 | 2010-06-08 17:24:37 -0500 | [diff] [blame] | 89 | sys.exit(1) |
| 90 | |
| 91 | def passes( commit ): |
| 92 | command = SSH_COMMAND + PASS_SCORE + ' -m \\\"' \ |
| 93 | + _shell_escape(PASS_MESSAGE) + ' \\\" ' + commit |
Chirayu Desai | 4c5ee48 | 2013-05-13 13:48:43 +0530 | [diff] [blame] | 94 | subprocess.getstatusoutput(command) |
Brad Larson | c575f03 | 2010-06-08 17:24:37 -0500 | [diff] [blame] | 95 | |
| 96 | def _shell_escape(x): |
| 97 | s = '' |
| 98 | for c in x: |
| 99 | if c in '\n': |
| 100 | s = s + '\\\"$\'\\n\'\\\"' |
| 101 | else: |
| 102 | s = s + c |
| 103 | return s |
| 104 | |
| 105 | if __name__ == '__main__': |
| 106 | main() |
| 107 | |