• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4
5"""Top-level presubmit script for auto-bisect.
6
7See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts for
8details on the presubmit API.
9"""
10
11import imp
12import subprocess
13import os
14
15# Paths to bisect config files relative to src/tools.
16CONFIG_FILES = [
17    'auto_bisect/config.cfg',
18    'run-perf-test.cfg'
19]
20
21def CheckChangeOnUpload(input_api, output_api):
22  return _CommonChecks(input_api, output_api)
23
24
25def CheckChangeOnCommit(input_api, output_api):
26  return _CommonChecks(input_api, output_api)
27
28
29def _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
36
37
38def _CheckAllConfigFiles(input_api, output_api):
39  """Checks all bisect config files and returns a list of presubmit results."""
40  results = []
41  for f in input_api.AffectedFiles():
42    for config_file in CONFIG_FILES:
43      if f.LocalPath().endswith(config_file):
44        results.extend(_CheckConfigFile(config_file, output_api))
45  return results
46
47
48def _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])]
55
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])]
59
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])]
63
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])]
71
72  return []
73
74
75def _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 []
85
86
87def _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  tests = input_api.canned_checks.GetPylint(
92      input_api, output_api, extra_paths_list=[telemetry_path])
93  return input_api.RunTests(tests)
94