| 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 = {} |
| 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 try: |
| 892 result = eval(function_name + '(' + repr(project) + ')', context) |
| 893 except TypeError: |
| 894 result = eval(function_name + '()', context) |
| 890 if not isinstance(result, types.ListType): | 895 if not isinstance(result, types.ListType): |
| 891 raise PresubmitFailure( | 896 raise PresubmitFailure( |
| 892 'Presubmit functions must return a list, got a %s instead: %s' % | 897 'Presubmit functions must return a list, got a %s instead: %s' % |
| 893 (type(result), str(result))) | 898 (type(result), str(result))) |
| 894 for item in result: | 899 for item in result: |
| 895 if not isinstance(item, basestring): | 900 if not isinstance(item, basestring): |
| 896 raise PresubmitFailure('All try slaves names must be strings.') | 901 raise PresubmitFailure('All try slaves names must be strings.') |
| 897 if item != item.strip(): | 902 if item != item.strip(): |
| 898 raise PresubmitFailure( | 903 raise PresubmitFailure( |
| 899 'Try slave names cannot start/end with whitespace') | 904 'Try slave names cannot start/end with whitespace') |
| 900 else: | 905 else: |
| 901 result = [] | 906 result = [] |
| 902 return result | 907 return result |
| 903 | 908 |
| 904 | 909 |
| 905 def DoGetTrySlaves(changed_files, | 910 def DoGetTrySlaves(changed_files, |
| 906 repository_root, | 911 repository_root, |
| 907 default_presubmit, | 912 default_presubmit, |
| 913 project, |
| 908 verbose, | 914 verbose, |
| 909 output_stream): | 915 output_stream): |
| 910 """Get the list of try servers from the presubmit scripts. | 916 """Get the list of try servers from the presubmit scripts. |
| 911 | 917 |
| 912 Args: | 918 Args: |
| 913 changed_files: List of modified files. | 919 changed_files: List of modified files. |
| 914 repository_root: The repository root. | 920 repository_root: The repository root. |
| 915 default_presubmit: A default presubmit script to execute in any case. | 921 default_presubmit: A default presubmit script to execute in any case. |
| 922 project: Optional name of a project used in selecting trybots. |
| 916 verbose: Prints debug info. | 923 verbose: Prints debug info. |
| 917 output_stream: A stream to write debug output to. | 924 output_stream: A stream to write debug output to. |
| 918 | 925 |
| 919 Return: | 926 Return: |
| 920 List of try slaves | 927 List of try slaves |
| 921 """ | 928 """ |
| 922 presubmit_files = ListRelevantPresubmitFiles(changed_files, repository_root) | 929 presubmit_files = ListRelevantPresubmitFiles(changed_files, repository_root) |
| 923 if not presubmit_files and verbose: | 930 if not presubmit_files and verbose: |
| 924 output_stream.write("Warning, no presubmit.py found.\n") | 931 output_stream.write("Warning, no presubmit.py found.\n") |
| 925 results = [] | 932 results = [] |
| 926 executer = GetTrySlavesExecuter() | 933 executer = GetTrySlavesExecuter() |
| 927 if default_presubmit: | 934 if default_presubmit: |
| 928 if verbose: | 935 if verbose: |
| 929 output_stream.write("Running default presubmit script.\n") | 936 output_stream.write("Running default presubmit script.\n") |
| 930 fake_path = os.path.join(repository_root, 'PRESUBMIT.py') | 937 fake_path = os.path.join(repository_root, 'PRESUBMIT.py') |
| 931 results += executer.ExecPresubmitScript(default_presubmit, fake_path) | 938 results += executer.ExecPresubmitScript( |
| 939 default_presubmit, fake_path, project) |
| 932 for filename in presubmit_files: | 940 for filename in presubmit_files: |
| 933 filename = os.path.abspath(filename) | 941 filename = os.path.abspath(filename) |
| 934 if verbose: | 942 if verbose: |
| 935 output_stream.write("Running %s\n" % filename) | 943 output_stream.write("Running %s\n" % filename) |
| 936 # Accept CRLF presubmit script. | 944 # Accept CRLF presubmit script. |
| 937 presubmit_script = gclient_utils.FileRead(filename, 'rU') | 945 presubmit_script = gclient_utils.FileRead(filename, 'rU') |
| 938 results += executer.ExecPresubmitScript(presubmit_script, filename) | 946 results += executer.ExecPresubmitScript( |
| 947 presubmit_script, filename, project) |
| 939 | 948 |
| 940 slaves = list(set(results)) | 949 slaves = list(set(results)) |
| 941 if slaves and verbose: | 950 if slaves and verbose: |
| 942 output_stream.write(', '.join(slaves)) | 951 output_stream.write(', '.join(slaves)) |
| 943 output_stream.write('\n') | 952 output_stream.write('\n') |
| 944 return slaves | 953 return slaves |
| 945 | 954 |
| 946 | 955 |
| 947 class PresubmitExecuter(object): | 956 class PresubmitExecuter(object): |
| 948 def __init__(self, change, committing, tbr, rietveld, verbose): | 957 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: | 1236 except PresubmitFailure, e: |
| 1228 print >> sys.stderr, e | 1237 print >> sys.stderr, e |
| 1229 print >> sys.stderr, 'Maybe your depot_tools is out of date?' | 1238 print >> sys.stderr, 'Maybe your depot_tools is out of date?' |
| 1230 print >> sys.stderr, 'If all fails, contact maruel@' | 1239 print >> sys.stderr, 'If all fails, contact maruel@' |
| 1231 return 2 | 1240 return 2 |
| 1232 | 1241 |
| 1233 | 1242 |
| 1234 if __name__ == '__main__': | 1243 if __name__ == '__main__': |
| 1235 fix_encoding.fix_encoding() | 1244 fix_encoding.fix_encoding() |
| 1236 sys.exit(Main(None)) | 1245 sys.exit(Main(None)) |
| OLD | NEW |