Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(416)

Side by Side Diff: presubmit_support.py

Issue 74163002: Allow PRESUBMIT.py files to have old and new-style trybot specifications (as long as each file is c… (Closed) Base URL: https://chromium.googlesource.com/chromium/tools/depot_tools.git@master
Patch Set: Address maruel nits. Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | tests/presubmit_unittest.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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.7.0' 9 __version__ = '1.7.0'
10 10
(...skipping 1037 matching lines...) Expand 10 before | Expand all | Expand 10 after
1048 ' format.') 1048 ' format.')
1049 1049
1050 if botname != botname.strip(): 1050 if botname != botname.strip():
1051 raise PresubmitFailure( 1051 raise PresubmitFailure(
1052 'Try slave names cannot start/end with whitespace') 1052 'Try slave names cannot start/end with whitespace')
1053 if ',' in botname: 1053 if ',' in botname:
1054 raise PresubmitFailure( 1054 raise PresubmitFailure(
1055 'Do not use \',\' separated builder or test names: %s' % botname) 1055 'Do not use \',\' separated builder or test names: %s' % botname)
1056 else: 1056 else:
1057 result = [] 1057 result = []
1058
1059 def valid_oldstyle(result):
1060 return all(isinstance(i, basestring) for i in result)
1061
1062 def valid_newstyle(result):
1063 return (all(isinstance(i, tuple) for i in result) and
1064 all(len(i) == 2 for i in result) and
1065 all(isinstance(i[0], basestring) for i in result) and
1066 all(isinstance(i[1], set) for i in result)
1067 )
1068
1069 # Ensure it's either all old-style or all new-style.
1070 if not valid_oldstyle(result) and not valid_newstyle(result):
1071 raise PresubmitFailure(
1072 'PRESUBMIT.py returned invalid trybot specification!')
1073
1058 return result 1074 return result
1059 1075
1060 1076
1061 def DoGetTrySlaves(change, 1077 def DoGetTrySlaves(change,
1062 changed_files, 1078 changed_files,
1063 repository_root, 1079 repository_root,
1064 default_presubmit, 1080 default_presubmit,
1065 project, 1081 project,
1066 verbose, 1082 verbose,
1067 output_stream): 1083 output_stream):
1068 """Get the list of try servers from the presubmit scripts. 1084 """Get the list of try servers from the presubmit scripts.
1069 1085
1070 Args: 1086 Args:
1071 changed_files: List of modified files. 1087 changed_files: List of modified files.
1072 repository_root: The repository root. 1088 repository_root: The repository root.
1073 default_presubmit: A default presubmit script to execute in any case. 1089 default_presubmit: A default presubmit script to execute in any case.
1074 project: Optional name of a project used in selecting trybots. 1090 project: Optional name of a project used in selecting trybots.
1075 verbose: Prints debug info. 1091 verbose: Prints debug info.
1076 output_stream: A stream to write debug output to. 1092 output_stream: A stream to write debug output to.
1077 1093
1078 Return: 1094 Return:
1079 List of try slaves 1095 List of try slaves
1080 """ 1096 """
1081 presubmit_files = ListRelevantPresubmitFiles(changed_files, repository_root) 1097 presubmit_files = ListRelevantPresubmitFiles(changed_files, repository_root)
1082 if not presubmit_files and verbose: 1098 if not presubmit_files and verbose:
1083 output_stream.write("Warning, no presubmit.py found.\n") 1099 output_stream.write("Warning, no presubmit.py found.\n")
1084 results = [] 1100 results = []
1085 executer = GetTrySlavesExecuter() 1101 executer = GetTrySlavesExecuter()
1102
1086 if default_presubmit: 1103 if default_presubmit:
1087 if verbose: 1104 if verbose:
1088 output_stream.write("Running default presubmit script.\n") 1105 output_stream.write("Running default presubmit script.\n")
1089 fake_path = os.path.join(repository_root, 'PRESUBMIT.py') 1106 fake_path = os.path.join(repository_root, 'PRESUBMIT.py')
1090 results += executer.ExecPresubmitScript( 1107 results.extend(executer.ExecPresubmitScript(
1091 default_presubmit, fake_path, project, change) 1108 default_presubmit, fake_path, project, change))
1092 for filename in presubmit_files: 1109 for filename in presubmit_files:
1093 filename = os.path.abspath(filename) 1110 filename = os.path.abspath(filename)
1094 if verbose: 1111 if verbose:
1095 output_stream.write("Running %s\n" % filename) 1112 output_stream.write("Running %s\n" % filename)
1096 # Accept CRLF presubmit script. 1113 # Accept CRLF presubmit script.
1097 presubmit_script = gclient_utils.FileRead(filename, 'rU') 1114 presubmit_script = gclient_utils.FileRead(filename, 'rU')
1098 results += executer.ExecPresubmitScript( 1115 results.extend(executer.ExecPresubmitScript(
1099 presubmit_script, filename, project, change) 1116 presubmit_script, filename, project, change))
1100 1117
1101 if all(isinstance(i, tuple) for i in results): 1118
1102 # New-style [('bot', set(['tests']))] format. 1119 slave_dict = {}
1103 slave_dict = {} 1120 old_style = filter(lambda x: isinstance(x, basestring), results)
1104 for result in results: 1121 new_style = filter(lambda x: isinstance(x, tuple), results)
1105 slave_dict.setdefault(result[0], set()).update(result[1]) 1122
1106 slaves = list(slave_dict.iteritems()) 1123 for result in new_style:
1107 elif all(isinstance(i, basestring) for i in results): 1124 slave_dict.setdefault(result[0], set()).update(result[1])
1108 # Old-style ['bot'] format. 1125 slaves = list(slave_dict.items())
1109 slaves = list(set(results)) 1126
1110 else: 1127 slaves.extend(set(old_style))
1111 raise ValueError('PRESUBMIT.py returned invalid trybot specification!')
1112 1128
1113 if slaves and verbose: 1129 if slaves and verbose:
1114 output_stream.write(', '.join(slaves)) 1130 output_stream.write(', '.join((str(x) for x in slaves)))
1115 output_stream.write('\n') 1131 output_stream.write('\n')
1116 return slaves 1132 return slaves
1117 1133
1118 1134
1119 class PresubmitExecuter(object): 1135 class PresubmitExecuter(object):
1120 def __init__(self, change, committing, rietveld_obj, verbose): 1136 def __init__(self, change, committing, rietveld_obj, verbose):
1121 """ 1137 """
1122 Args: 1138 Args:
1123 change: The Change object. 1139 change: The Change object.
1124 committing: True if 'gcl commit' is running, False if 'gcl upload' is. 1140 committing: True if 'gcl commit' is running, False if 'gcl upload' is.
(...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after
1463 except PresubmitFailure, e: 1479 except PresubmitFailure, e:
1464 print >> sys.stderr, e 1480 print >> sys.stderr, e
1465 print >> sys.stderr, 'Maybe your depot_tools is out of date?' 1481 print >> sys.stderr, 'Maybe your depot_tools is out of date?'
1466 print >> sys.stderr, 'If all fails, contact maruel@' 1482 print >> sys.stderr, 'If all fails, contact maruel@'
1467 return 2 1483 return 2
1468 1484
1469 1485
1470 if __name__ == '__main__': 1486 if __name__ == '__main__':
1471 fix_encoding.fix_encoding() 1487 fix_encoding.fix_encoding()
1472 sys.exit(Main(None)) 1488 sys.exit(Main(None))
OLDNEW
« no previous file with comments | « no previous file | tests/presubmit_unittest.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698