Reland the ULONG -> SIZE_T change from 317177
[chromium-blink-merge.git] / tools / auto_bisect / PRESUBMIT.py
blob1e99787715b040f286415abc5e06243edb9e4c0e
1 # Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 """Top-level presubmit script for auto-bisect.
7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts for
8 details on the presubmit API.
9 """
11 import imp
12 import subprocess
13 import os
15 # Paths to bisect config files relative to this script.
16 CONFIG_FILES = [
17 'bisect.cfg',
18 os.path.join(os.path.pardir, 'run-perf-test.cfg'),
21 def CheckChangeOnUpload(input_api, output_api):
22 return _CommonChecks(input_api, output_api)
25 def CheckChangeOnCommit(input_api, output_api):
26 return _CommonChecks(input_api, output_api)
29 def _CommonChecks(input_api, output_api):
30 """Does all presubmit checks for auto-bisect."""
31 results = []
32 results.extend(_CheckAllConfigFiles(input_api, output_api))
33 results.extend(_RunUnitTests(input_api, output_api))
34 results.extend(_RunPyLint(input_api, output_api))
35 return results
38 def _CheckAllConfigFiles(input_api, output_api):
39 """Checks all bisect config files and returns a list of presubmit results."""
40 results = []
41 script_path = input_api.PresubmitLocalPath()
42 for config_file in CONFIG_FILES:
43 file_path = os.path.join(script_path, config_file)
44 results.extend(_CheckConfigFile(file_path, output_api))
45 return results
48 def _CheckConfigFile(file_path, output_api):
49 """Checks one bisect config file and returns a list of presubmit results."""
50 try:
51 config_file = imp.load_source('config', file_path)
52 except IOError as e:
53 warning = 'Failed to read config file %s: %s' % (file_path, str(e))
54 return [output_api.PresubmitError(warning, items=[file_path])]
56 if not hasattr(config_file, 'config'):
57 warning = 'Config file has no "config" global variable: %s' % str(e)
58 return [output_api.PresubmitError(warning, items=[file_path])]
60 if type(config_file.config) is not dict:
61 warning = 'Config file "config" global variable is not dict: %s' % str(e)
62 return [output_api.PresubmitError(warning, items=[file_path])]
64 for k, v in config_file.config.iteritems():
65 if v != '':
66 warning = 'Non-empty value in config dict: %s: %s' % (repr(k), repr(v))
67 warning += ('\nThe bisection config file should only contain a config '
68 'dict with empty fields. Changes to this file should not '
69 'be submitted.')
70 return [output_api.PresubmitError(warning, items=[file_path])]
72 return []
75 def _RunUnitTests(input_api, output_api):
76 """Runs unit tests for auto-bisect."""
77 repo_root = input_api.change.RepositoryRoot()
78 auto_bisect_dir = os.path.join(repo_root, 'tools', 'auto_bisect')
79 test_runner = os.path.join(auto_bisect_dir, 'run_tests')
80 return_code = subprocess.call(['python', test_runner])
81 if return_code:
82 message = 'Auto-bisect unit tests did not all pass.'
83 return [output_api.PresubmitError(message)]
84 return []
87 def _RunPyLint(input_api, output_api):
88 """Runs unit tests for auto-bisect."""
89 telemetry_path = os.path.join(
90 input_api.PresubmitLocalPath(), os.path.pardir, 'telemetry')
91 mock_path = os.path.join(
92 input_api.PresubmitLocalPath(), os.path.pardir, os.path.pardir,
93 'third_party', 'pymock')
94 disabled_warnings = [
95 'relative-import',
97 tests = input_api.canned_checks.GetPylint(
98 input_api, output_api, disabled_warnings=disabled_warnings,
99 extra_paths_list=[telemetry_path, mock_path])
100 return input_api.RunTests(tests)