Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / tools / telemetry / PRESUBMIT.py
blob8c7cd0f8d753fe09837771199828e923271f72d6
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.
6 def _CommonChecks(input_api, output_api):
7 results = []
9 # TODO(nduca): This should call update_docs.IsUpdateDocsNeeded().
10 # Disabled due to crbug.com/255326.
11 if False:
12 update_docs_path = input_api.os_path.join(
13 input_api.PresubmitLocalPath(), 'update_docs')
14 assert input_api.os_path.exists(update_docs_path)
15 results.append(output_api.PresubmitError(
16 'Docs are stale. Please run:\n' +
17 '$ %s' % input_api.os_path.abspath(update_docs_path)))
19 pylint_checks = input_api.canned_checks.GetPylint(
20 input_api, output_api, extra_paths_list=_GetPathsToPrepend(input_api),
21 pylintrc='pylintrc')
23 results.extend(_CheckNoMoreUsageOfDeprecatedCode(
24 input_api, output_api, deprecated_code='GetChromiumSrcDir()',
25 crbug_number=511332))
26 results.extend(input_api.RunTests(pylint_checks))
27 return results
30 def _CheckNoMoreUsageOfDeprecatedCode(
31 input_api, output_api, deprecated_code, crbug_number):
32 results = []
33 # These checks are not perfcet but should be good enough for most of our
34 # usecases.
35 def _IsAddedLine(line):
36 return line.startswith('+') and not line.startswith('+++ ')
37 def _IsRemovedLine(line):
38 return line.startswith('-') and not line.startswith('--- ')
40 presubmit_dir = input_api.os_path.join(
41 input_api.PresubmitLocalPath(), 'PRESUBMIT.py')
43 added_calls = 0
44 removed_calls = 0
45 for affected_file in input_api.AffectedFiles():
46 # Do not do the check on PRESUBMIT.py itself.
47 if affected_file.AbsoluteLocalPath() == presubmit_dir:
48 continue
49 for line in affected_file.GenerateScmDiff().splitlines():
50 if _IsAddedLine(line) and deprecated_code in line:
51 added_calls += 1
52 elif _IsRemovedLine(line) and deprecated_code in line:
53 removed_calls += 1
55 if added_calls > removed_calls:
56 results.append(output_api.PresubmitError(
57 'Your patch adds more instances of %s. Please see crbug.com/%i for'
58 'how to proceed.' % (deprecated_code, crbug_number)))
59 return results
62 def _GetPathsToPrepend(input_api):
63 telemetry_dir = input_api.PresubmitLocalPath()
64 chromium_src_dir = input_api.os_path.join(telemetry_dir, '..', '..')
65 return [
66 telemetry_dir,
67 input_api.os_path.join(telemetry_dir, 'third_party', 'altgraph'),
68 input_api.os_path.join(telemetry_dir, 'third_party', 'mock'),
69 input_api.os_path.join(telemetry_dir, 'third_party', 'modulegraph'),
70 input_api.os_path.join(telemetry_dir, 'third_party', 'pexpect'),
71 input_api.os_path.join(telemetry_dir, 'third_party', 'png'),
72 input_api.os_path.join(telemetry_dir, 'third_party', 'pyserial'),
73 input_api.os_path.join(telemetry_dir, 'third_party', 'typ'),
74 input_api.os_path.join(telemetry_dir, 'third_party', 'webpagereplay'),
75 input_api.os_path.join(telemetry_dir, 'third_party', 'websocket-client'),
77 input_api.os_path.join(chromium_src_dir, 'build', 'android'),
78 input_api.os_path.join(chromium_src_dir,
79 'third_party', 'catapult', 'tracing'),
83 def CheckChangeOnUpload(input_api, output_api):
84 results = []
85 results.extend(_CommonChecks(input_api, output_api))
86 return results
89 def CheckChangeOnCommit(input_api, output_api):
90 results = []
91 results.extend(_CommonChecks(input_api, output_api))
92 return results