Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 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.1' | 9 __version__ = '1.6.1' |
| 10 | 10 |
| (...skipping 851 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 862 p = os.path.join(directory, 'PRESUBMIT.py') | 862 p = os.path.join(directory, 'PRESUBMIT.py') |
| 863 if os.path.isfile(p): | 863 if os.path.isfile(p): |
| 864 results.append(p) | 864 results.append(p) |
| 865 | 865 |
| 866 logging.debug('Presubmit files: %s' % ','.join(results)) | 866 logging.debug('Presubmit files: %s' % ','.join(results)) |
| 867 return results | 867 return results |
| 868 | 868 |
| 869 | 869 |
| 870 class GetTrySlavesExecuter(object): | 870 class GetTrySlavesExecuter(object): |
| 871 @staticmethod | 871 @staticmethod |
| 872 def ExecPresubmitScript(script_text, presubmit_path): | 872 def ExecPresubmitScript(script_text, presubmit_path, project): |
| 873 """Executes GetPreferredTrySlaves() from a single presubmit script. | 873 """Executes GetPreferredTrySlaves() from a single presubmit script. |
| 874 | 874 |
| 875 Args: | 875 Args: |
| 876 script_text: The text of the presubmit script. | 876 script_text: The text of the presubmit script. |
| 877 presubmit_path: Project script to run. | |
| 878 project: Project name to pass to presubmit script for bot selection. | |
| 877 | 879 |
| 878 Return: | 880 Return: |
| 879 A list of try slaves. | 881 A list of try slaves. |
| 880 """ | 882 """ |
| 881 context = {} | 883 context = {'project': project} |
|
M-A Ruel
2011/05/24 17:29:42
I don't like relying on a global variable. I'd pre
| |
| 882 try: | 884 try: |
| 883 exec script_text in context | 885 exec script_text in context |
| 884 except Exception, e: | 886 except Exception, e: |
| 885 raise PresubmitFailure('"%s" had an exception.\n%s' % (presubmit_path, e)) | 887 raise PresubmitFailure('"%s" had an exception.\n%s' % (presubmit_path, e)) |
| 886 | 888 |
| 887 function_name = 'GetPreferredTrySlaves' | 889 function_name = 'GetPreferredTrySlaves' |
| 888 if function_name in context: | 890 if function_name in context: |
| 889 result = eval(function_name + '()', context) | 891 result = eval(function_name + '()', context) |
| 890 if not isinstance(result, types.ListType): | 892 if not isinstance(result, types.ListType): |
| 891 raise PresubmitFailure( | 893 raise PresubmitFailure( |
| 892 'Presubmit functions must return a list, got a %s instead: %s' % | 894 'Presubmit functions must return a list, got a %s instead: %s' % |
| 893 (type(result), str(result))) | 895 (type(result), str(result))) |
| 894 for item in result: | 896 for item in result: |
| 895 if not isinstance(item, basestring): | 897 if not isinstance(item, basestring): |
| 896 raise PresubmitFailure('All try slaves names must be strings.') | 898 raise PresubmitFailure('All try slaves names must be strings.') |
| 897 if item != item.strip(): | 899 if item != item.strip(): |
| 898 raise PresubmitFailure( | 900 raise PresubmitFailure( |
| 899 'Try slave names cannot start/end with whitespace') | 901 'Try slave names cannot start/end with whitespace') |
| 900 else: | 902 else: |
| 901 result = [] | 903 result = [] |
| 902 return result | 904 return result |
| 903 | 905 |
| 904 | 906 |
| 905 def DoGetTrySlaves(changed_files, | 907 def DoGetTrySlaves(changed_files, |
| 906 repository_root, | 908 repository_root, |
| 907 default_presubmit, | 909 default_presubmit, |
| 910 project, | |
| 908 verbose, | 911 verbose, |
| 909 output_stream): | 912 output_stream): |
| 910 """Get the list of try servers from the presubmit scripts. | 913 """Get the list of try servers from the presubmit scripts. |
| 911 | 914 |
| 912 Args: | 915 Args: |
| 913 changed_files: List of modified files. | 916 changed_files: List of modified files. |
| 914 repository_root: The repository root. | 917 repository_root: The repository root. |
| 915 default_presubmit: A default presubmit script to execute in any case. | 918 default_presubmit: A default presubmit script to execute in any case. |
| 919 project: Optional name of a project used in selecting trybots. | |
| 916 verbose: Prints debug info. | 920 verbose: Prints debug info. |
| 917 output_stream: A stream to write debug output to. | 921 output_stream: A stream to write debug output to. |
| 918 | 922 |
| 919 Return: | 923 Return: |
| 920 List of try slaves | 924 List of try slaves |
| 921 """ | 925 """ |
| 922 presubmit_files = ListRelevantPresubmitFiles(changed_files, repository_root) | 926 presubmit_files = ListRelevantPresubmitFiles(changed_files, repository_root) |
| 923 if not presubmit_files and verbose: | 927 if not presubmit_files and verbose: |
| 924 output_stream.write("Warning, no presubmit.py found.\n") | 928 output_stream.write("Warning, no presubmit.py found.\n") |
| 925 results = [] | 929 results = [] |
| 926 executer = GetTrySlavesExecuter() | 930 executer = GetTrySlavesExecuter() |
| 927 if default_presubmit: | 931 if default_presubmit: |
| 928 if verbose: | 932 if verbose: |
| 929 output_stream.write("Running default presubmit script.\n") | 933 output_stream.write("Running default presubmit script.\n") |
| 930 fake_path = os.path.join(repository_root, 'PRESUBMIT.py') | 934 fake_path = os.path.join(repository_root, 'PRESUBMIT.py') |
| 931 results += executer.ExecPresubmitScript(default_presubmit, fake_path) | 935 results += executer.ExecPresubmitScript( |
| 936 default_presubmit, fake_path, project) | |
| 932 for filename in presubmit_files: | 937 for filename in presubmit_files: |
| 933 filename = os.path.abspath(filename) | 938 filename = os.path.abspath(filename) |
| 934 if verbose: | 939 if verbose: |
| 935 output_stream.write("Running %s\n" % filename) | 940 output_stream.write("Running %s\n" % filename) |
| 936 # Accept CRLF presubmit script. | 941 # Accept CRLF presubmit script. |
| 937 presubmit_script = gclient_utils.FileRead(filename, 'rU') | 942 presubmit_script = gclient_utils.FileRead(filename, 'rU') |
| 938 results += executer.ExecPresubmitScript(presubmit_script, filename) | 943 results += executer.ExecPresubmitScript( |
| 944 presubmit_script, filename, project) | |
| 939 | 945 |
| 940 slaves = list(set(results)) | 946 slaves = list(set(results)) |
| 941 if slaves and verbose: | 947 if slaves and verbose: |
| 942 output_stream.write(', '.join(slaves)) | 948 output_stream.write(', '.join(slaves)) |
| 943 output_stream.write('\n') | 949 output_stream.write('\n') |
| 944 return slaves | 950 return slaves |
| 945 | 951 |
| 946 | 952 |
| 947 class PresubmitExecuter(object): | 953 class PresubmitExecuter(object): |
| 948 def __init__(self, change, committing, tbr, rietveld, verbose): | 954 def __init__(self, change, committing, tbr, rietveld, verbose): |
| (...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1227 except PresubmitFailure, e: | 1233 except PresubmitFailure, e: |
| 1228 print >> sys.stderr, e | 1234 print >> sys.stderr, e |
| 1229 print >> sys.stderr, 'Maybe your depot_tools is out of date?' | 1235 print >> sys.stderr, 'Maybe your depot_tools is out of date?' |
| 1230 print >> sys.stderr, 'If all fails, contact maruel@' | 1236 print >> sys.stderr, 'If all fails, contact maruel@' |
| 1231 return 2 | 1237 return 2 |
| 1232 | 1238 |
| 1233 | 1239 |
| 1234 if __name__ == '__main__': | 1240 if __name__ == '__main__': |
| 1235 fix_encoding.fix_encoding() | 1241 fix_encoding.fix_encoding() |
| 1236 sys.exit(Main(None)) | 1242 sys.exit(Main(None)) |
| OLD | NEW |