1# Copyright 2012 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"""Presubmit script for changes affecting tools/perf/.
6
7See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
8for more details about the presubmit API built into gcl (and git-cl).
9"""
10
11import os
12import sys
13
14PYLINT_BLACKLIST = []
15PYLINT_DISABLED_WARNINGS = [
16    'R0923',  # Interface not implemented
17    'R0201',  # Method could be a function
18    'E1101',  # Non-existent member is accessed.
19]
20
21
22def _CommonChecks(input_api, output_api):
23  """Performs common checks, which includes running pylint."""
24  results = []
25  old_sys_path = sys.path
26  try:
27    # Modules in tools/perf depend on telemetry.
28    sys.path = [os.path.join(os.pardir, 'telemetry')] + sys.path
29    results.extend(input_api.canned_checks.RunPylint(
30        input_api, output_api,
31        black_list=PYLINT_BLACKLIST,
32        disabled_warnings=PYLINT_DISABLED_WARNINGS))
33    results.extend(_CheckJson(input_api, output_api))
34  finally:
35    sys.path = old_sys_path
36  return results
37
38
39def _CheckJson(input_api, output_api):
40  """Checks whether JSON files in this change can be parsed."""
41  for affected_file in input_api.AffectedFiles(include_deletes=False):
42    filename = affected_file.AbsoluteLocalPath()
43    if os.path.splitext(filename)[1] != '.json':
44      continue
45    try:
46      input_api.json.load(open(filename))
47    except ValueError:
48      return [output_api.PresubmitError('Error parsing JSON in %s!' % filename)]
49  return []
50
51
52def CheckChangeOnUpload(input_api, output_api):
53  report = []
54  report.extend(_CommonChecks(input_api, output_api))
55  return report
56
57
58def CheckChangeOnCommit(input_api, output_api):
59  report = []
60  report.extend(_CommonChecks(input_api, output_api))
61  return report
62