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.6.2' | 9 __version__ = '1.7.0' |
10 | 10 |
11 # TODO(joi) Add caching where appropriate/needed. The API is designed to allow | 11 # TODO(joi) Add caching where appropriate/needed. The API is designed to allow |
12 # caching (between all different invocations of presubmit scripts for a given | 12 # caching (between all different invocations of presubmit scripts for a given |
13 # change). We should add it as our presubmit scripts start feeling slow. | 13 # change). We should add it as our presubmit scripts start feeling slow. |
14 | 14 |
15 import cpplint | 15 import cpplint |
16 import cPickle # Exposed through the API. | 16 import cPickle # Exposed through the API. |
17 import cStringIO # Exposed through the API. | 17 import cStringIO # Exposed through the API. |
18 import collections | 18 import collections |
19 import contextlib | 19 import contextlib |
(...skipping 1004 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1024 result = get_preferred_try_slaves(project) | 1024 result = get_preferred_try_slaves(project) |
1025 elif len(function_info[0]) == 2: | 1025 elif len(function_info[0]) == 2: |
1026 result = get_preferred_try_slaves(project, change) | 1026 result = get_preferred_try_slaves(project, change) |
1027 else: | 1027 else: |
1028 result = get_preferred_try_slaves() | 1028 result = get_preferred_try_slaves() |
1029 if not isinstance(result, types.ListType): | 1029 if not isinstance(result, types.ListType): |
1030 raise PresubmitFailure( | 1030 raise PresubmitFailure( |
1031 'Presubmit functions must return a list, got a %s instead: %s' % | 1031 'Presubmit functions must return a list, got a %s instead: %s' % |
1032 (type(result), str(result))) | 1032 (type(result), str(result))) |
1033 for item in result: | 1033 for item in result: |
1034 if not isinstance(item, basestring): | 1034 if isinstance(item, basestring): |
1035 raise PresubmitFailure('All try slaves names must be strings.') | 1035 # Old-style ['bot'] format. |
1036 if item != item.strip(): | 1036 botname = item |
| 1037 elif isinstance(item, tuple): |
| 1038 # New-style [('bot', set(['tests']))] format. |
| 1039 botname = item[0] |
| 1040 else: |
| 1041 raise PresubmitFailure('PRESUBMIT.py returned invalid tryslave/test' |
| 1042 ' format.') |
| 1043 |
| 1044 if botname != botname.strip(): |
1037 raise PresubmitFailure( | 1045 raise PresubmitFailure( |
1038 'Try slave names cannot start/end with whitespace') | 1046 'Try slave names cannot start/end with whitespace') |
1039 if ',' in item: | 1047 if ',' in botname: |
1040 raise PresubmitFailure( | 1048 raise PresubmitFailure( |
1041 'Do not use \',\' separated builder or test names: %s' % item) | 1049 'Do not use \',\' separated builder or test names: %s' % botname) |
1042 else: | 1050 else: |
1043 result = [] | 1051 result = [] |
1044 return result | 1052 return result |
1045 | 1053 |
1046 | 1054 |
1047 def DoGetTrySlaves(change, | 1055 def DoGetTrySlaves(change, |
1048 changed_files, | 1056 changed_files, |
1049 repository_root, | 1057 repository_root, |
1050 default_presubmit, | 1058 default_presubmit, |
1051 project, | 1059 project, |
(...skipping 25 matching lines...) Expand all Loading... |
1077 default_presubmit, fake_path, project, change) | 1085 default_presubmit, fake_path, project, change) |
1078 for filename in presubmit_files: | 1086 for filename in presubmit_files: |
1079 filename = os.path.abspath(filename) | 1087 filename = os.path.abspath(filename) |
1080 if verbose: | 1088 if verbose: |
1081 output_stream.write("Running %s\n" % filename) | 1089 output_stream.write("Running %s\n" % filename) |
1082 # Accept CRLF presubmit script. | 1090 # Accept CRLF presubmit script. |
1083 presubmit_script = gclient_utils.FileRead(filename, 'rU') | 1091 presubmit_script = gclient_utils.FileRead(filename, 'rU') |
1084 results += executer.ExecPresubmitScript( | 1092 results += executer.ExecPresubmitScript( |
1085 presubmit_script, filename, project, change) | 1093 presubmit_script, filename, project, change) |
1086 | 1094 |
1087 slaves = list(set(results)) | 1095 if all(isinstance(i, tuple) for i in results): |
| 1096 # New-style [('bot', set(['tests']))] format. |
| 1097 slave_dict = {} |
| 1098 for result in results: |
| 1099 slave_dict.setdefault(result[0], set()).update(result[1]) |
| 1100 slaves = list(slave_dict.iteritems()) |
| 1101 elif all(isinstance(i, basestring) for i in results): |
| 1102 # Old-style ['bot'] format. |
| 1103 slaves = list(set(results)) |
| 1104 else: |
| 1105 raise ValueError('PRESUBMIT.py returned invalid trybot specification!') |
| 1106 |
1088 if slaves and verbose: | 1107 if slaves and verbose: |
1089 output_stream.write(', '.join(slaves)) | 1108 output_stream.write(', '.join(slaves)) |
1090 output_stream.write('\n') | 1109 output_stream.write('\n') |
1091 return slaves | 1110 return slaves |
1092 | 1111 |
1093 | 1112 |
1094 class PresubmitExecuter(object): | 1113 class PresubmitExecuter(object): |
1095 def __init__(self, change, committing, rietveld_obj, verbose): | 1114 def __init__(self, change, committing, rietveld_obj, verbose): |
1096 """ | 1115 """ |
1097 Args: | 1116 Args: |
(...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1427 except PresubmitFailure, e: | 1446 except PresubmitFailure, e: |
1428 print >> sys.stderr, e | 1447 print >> sys.stderr, e |
1429 print >> sys.stderr, 'Maybe your depot_tools is out of date?' | 1448 print >> sys.stderr, 'Maybe your depot_tools is out of date?' |
1430 print >> sys.stderr, 'If all fails, contact maruel@' | 1449 print >> sys.stderr, 'If all fails, contact maruel@' |
1431 return 2 | 1450 return 2 |
1432 | 1451 |
1433 | 1452 |
1434 if __name__ == '__main__': | 1453 if __name__ == '__main__': |
1435 fix_encoding.fix_encoding() | 1454 fix_encoding.fix_encoding() |
1436 sys.exit(Main(None)) | 1455 sys.exit(Main(None)) |
OLD | NEW |