blob: d26fa58c28509b298912fae21a54caa84299bfee [file] [log] [blame]
Brad Larsonc575f032010-06-08 17:24:37 -05001#!/usr/bin/env python
Chirayu Desai4c5ee482013-05-13 13:48:43 +05302
3from __future__ import print_function
4
5import subprocess
Brad Larsonc575f032010-06-08 17:24:37 -05006import getopt
7import sys
8
9
10SSH_USER = 'bot'
11SSH_HOST = 'localhost'
12SSH_PORT = 29418
13SSH_COMMAND = 'ssh %s@%s -p %d gerrit approve ' % (SSH_USER, SSH_HOST, SSH_PORT)
14FAILURE_SCORE = '--code-review=-2'
15FAILURE_MESSAGE = 'This commit message does not match the standard.' \
16 + ' Please correct the commit message and upload a replacement patch.'
17PASS_SCORE = '--code-review=0'
18PASS_MESSAGE = ''
19
20def main():
21 change = None
22 project = None
23 branch = None
24 commit = None
25 patchset = None
26
27 try:
David Pursehouse686bfea2014-12-04 14:23:30 +090028 opts, _args = getopt.getopt(sys.argv[1:], '', \
Brad Larsonc575f032010-06-08 17:24:37 -050029 ['change=', 'project=', 'branch=', 'commit=', 'patchset='])
Chirayu Desai4c5ee482013-05-13 13:48:43 +053030 except getopt.GetoptError as err:
31 print('Error: %s' % (err))
Brad Larsonc575f032010-06-08 17:24:37 -050032 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 Desai4c5ee482013-05-13 13:48:43 +053047 print('Error: option %s not recognized' % (arg))
Brad Larsonc575f032010-06-08 17:24:37 -050048 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 Desai4c5ee482013-05-13 13:48:43 +053057 status, output = subprocess.getstatusoutput(command)
Brad Larsonc575f032010-06-08 17:24:37 -050058
59 if status != 0:
Chirayu Desai4c5ee482013-05-13 13:48:43 +053060 print('Error running \'%s\'. status: %s, output:\n\n%s' % \
61 (command, status, output))
Brad Larsonc575f032010-06-08 17:24:37 -050062 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
79def usage():
Chirayu Desai4c5ee482013-05-13 13:48:43 +053080 print('Usage:\n')
81 print(sys.argv[0] + ' --change <change id> --project <project name> ' \
82 + '--branch <branch> --commit <sha1> --patchset <patchset id>')
Brad Larsonc575f032010-06-08 17:24:37 -050083
84def fail( commit, message ):
85 command = SSH_COMMAND + FAILURE_SCORE + ' -m \\\"' \
86 + _shell_escape( FAILURE_MESSAGE + '\n\n' + message) \
87 + '\\\" ' + commit
Chirayu Desai4c5ee482013-05-13 13:48:43 +053088 subprocess.getstatusoutput(command)
Brad Larsonc575f032010-06-08 17:24:37 -050089 sys.exit(1)
90
91def passes( commit ):
92 command = SSH_COMMAND + PASS_SCORE + ' -m \\\"' \
93 + _shell_escape(PASS_MESSAGE) + ' \\\" ' + commit
Chirayu Desai4c5ee482013-05-13 13:48:43 +053094 subprocess.getstatusoutput(command)
Brad Larsonc575f032010-06-08 17:24:37 -050095
96def _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
105if __name__ == '__main__':
106 main()
107