OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Enables directory-specific presubmit checks to run at upload and/or commit. | 6 """Enables directory-specific presubmit checks to run at upload and/or commit. |
7 """ | 7 """ |
8 | 8 |
9 __version__ = '1.8.0' | 9 __version__ = '1.8.0' |
10 | 10 |
(...skipping 1125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1136 function_name = 'GetPreferredTryMasters' | 1136 function_name = 'GetPreferredTryMasters' |
1137 if function_name not in context: | 1137 if function_name not in context: |
1138 return {} | 1138 return {} |
1139 get_preferred_try_masters = context[function_name] | 1139 get_preferred_try_masters = context[function_name] |
1140 if not len(inspect.getargspec(get_preferred_try_masters)[0]) == 2: | 1140 if not len(inspect.getargspec(get_preferred_try_masters)[0]) == 2: |
1141 raise PresubmitFailure( | 1141 raise PresubmitFailure( |
1142 'Expected function "GetPreferredTryMasters" to take two arguments.') | 1142 'Expected function "GetPreferredTryMasters" to take two arguments.') |
1143 return get_preferred_try_masters(project, change) | 1143 return get_preferred_try_masters(project, change) |
1144 | 1144 |
1145 | 1145 |
| 1146 class GetPostUploadExecuter(object): |
| 1147 @staticmethod |
| 1148 def ExecPresubmitScript(script_text, presubmit_path, cl, change): |
| 1149 """Executes PostUploadHook() from a single presubmit script. |
| 1150 |
| 1151 Args: |
| 1152 script_text: The text of the presubmit script. |
| 1153 presubmit_path: Project script to run. |
| 1154 cl: The Changelist object. |
| 1155 change: The Change object. |
| 1156 |
| 1157 Return: |
| 1158 A list of results objects. |
| 1159 """ |
| 1160 context = {} |
| 1161 try: |
| 1162 exec script_text in context |
| 1163 except Exception, e: |
| 1164 raise PresubmitFailure('"%s" had an exception.\n%s' |
| 1165 % (presubmit_path, e)) |
| 1166 |
| 1167 function_name = 'PostUploadHook' |
| 1168 if function_name not in context: |
| 1169 return {} |
| 1170 post_upload_hook = context[function_name] |
| 1171 if not len(inspect.getargspec(post_upload_hook)[0]) == 3: |
| 1172 raise PresubmitFailure( |
| 1173 'Expected function "PostUploadHook" to take three arguments.') |
| 1174 return post_upload_hook(cl, change, OutputApi(False)) |
| 1175 |
| 1176 |
1146 def DoGetTrySlaves(change, | 1177 def DoGetTrySlaves(change, |
1147 changed_files, | 1178 changed_files, |
1148 repository_root, | 1179 repository_root, |
1149 default_presubmit, | 1180 default_presubmit, |
1150 project, | 1181 project, |
1151 verbose, | 1182 verbose, |
1152 output_stream): | 1183 output_stream): |
1153 """Get the list of try servers from the presubmit scripts (deprecated). | 1184 """Get the list of try servers from the presubmit scripts (deprecated). |
1154 | 1185 |
1155 Args: | 1186 Args: |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1256 # Make sets to lists again for later JSON serialization. | 1287 # Make sets to lists again for later JSON serialization. |
1257 for builders in results.itervalues(): | 1288 for builders in results.itervalues(): |
1258 for builder in builders: | 1289 for builder in builders: |
1259 builders[builder] = list(builders[builder]) | 1290 builders[builder] = list(builders[builder]) |
1260 | 1291 |
1261 if results and verbose: | 1292 if results and verbose: |
1262 output_stream.write('%s\n' % str(results)) | 1293 output_stream.write('%s\n' % str(results)) |
1263 return results | 1294 return results |
1264 | 1295 |
1265 | 1296 |
| 1297 def DoPostUploadExecuter(change, |
| 1298 cl, |
| 1299 repository_root, |
| 1300 verbose, |
| 1301 output_stream): |
| 1302 """Execute the post upload hook. |
| 1303 |
| 1304 Args: |
| 1305 change: The Change object. |
| 1306 cl: The Changelist object. |
| 1307 repository_root: The repository root. |
| 1308 verbose: Prints debug info. |
| 1309 output_stream: A stream to write debug output to. |
| 1310 """ |
| 1311 presubmit_files = ListRelevantPresubmitFiles( |
| 1312 change.LocalPaths(), repository_root) |
| 1313 if not presubmit_files and verbose: |
| 1314 output_stream.write("Warning, no PRESUBMIT.py found.\n") |
| 1315 results = [] |
| 1316 executer = GetPostUploadExecuter() |
| 1317 # The root presubmit file should be executed after the ones in subdirectories. |
| 1318 # i.e. the specific post upload hooks should run before the general ones. |
| 1319 # Thus, reverse the order provided by ListRelevantPresubmitFiles. |
| 1320 presubmit_files.reverse() |
| 1321 |
| 1322 for filename in presubmit_files: |
| 1323 filename = os.path.abspath(filename) |
| 1324 if verbose: |
| 1325 output_stream.write("Running %s\n" % filename) |
| 1326 # Accept CRLF presubmit script. |
| 1327 presubmit_script = gclient_utils.FileRead(filename, 'rU') |
| 1328 results.extend(executer.ExecPresubmitScript( |
| 1329 presubmit_script, filename, cl, change)) |
| 1330 output_stream.write('\n') |
| 1331 if results: |
| 1332 output_stream.write('** Post Upload Hook Messages **\n') |
| 1333 for result in results: |
| 1334 result.handle(output_stream) |
| 1335 output_stream.write('\n') |
| 1336 |
| 1337 return results |
| 1338 |
| 1339 |
1266 class PresubmitExecuter(object): | 1340 class PresubmitExecuter(object): |
1267 def __init__(self, change, committing, rietveld_obj, verbose): | 1341 def __init__(self, change, committing, rietveld_obj, verbose): |
1268 """ | 1342 """ |
1269 Args: | 1343 Args: |
1270 change: The Change object. | 1344 change: The Change object. |
1271 committing: True if 'gcl commit' is running, False if 'gcl upload' is. | 1345 committing: True if 'gcl commit' is running, False if 'gcl upload' is. |
1272 rietveld_obj: rietveld.Rietveld client object. | 1346 rietveld_obj: rietveld.Rietveld client object. |
1273 """ | 1347 """ |
1274 self.change = change | 1348 self.change = change |
1275 self.committing = committing | 1349 self.committing = committing |
(...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1669 except PresubmitFailure, e: | 1743 except PresubmitFailure, e: |
1670 print >> sys.stderr, e | 1744 print >> sys.stderr, e |
1671 print >> sys.stderr, 'Maybe your depot_tools is out of date?' | 1745 print >> sys.stderr, 'Maybe your depot_tools is out of date?' |
1672 print >> sys.stderr, 'If all fails, contact maruel@' | 1746 print >> sys.stderr, 'If all fails, contact maruel@' |
1673 return 2 | 1747 return 2 |
1674 | 1748 |
1675 | 1749 |
1676 if __name__ == '__main__': | 1750 if __name__ == '__main__': |
1677 fix_encoding.fix_encoding() | 1751 fix_encoding.fix_encoding() |
1678 sys.exit(Main(None)) | 1752 sys.exit(Main(None)) |
OLD | NEW |