blob: fba4d3288767e9effa10c52401050d893381dd66 [file] [log] [blame]
Avi Drissman73a09d12022-09-08 20:33:381# Copyright 2022 The Chromium Authors
Mohamed Heikal48cca322022-04-12 18:51:312# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5PRESUBMIT_VERSION = '2.0.0'
6
7# This line is 'magic' in that git-cl looks for it to decide whether to
8# use Python3 instead of Python2 when running the code in this file.
9USE_PYTHON3 = True
10
11import textwrap
12
13
14def CheckNoBadDeps(input_api, output_api):
15 """Prevent additions of bad dependencies from the //build prefix."""
16 build_file_patterns = [
17 r'(.+/)?BUILD\.gn',
18 r'.+\.gni',
19 ]
Andrew Grieve685499482022-09-09 23:14:0820 blocklist_pattern = input_api.re.compile(r'^[^#]*"//(?!build).+?/.*"')
21 allowlist_pattern = input_api.re.compile(r'^[^#]*"//third_party/junit')
Mohamed Heikal48cca322022-04-12 18:51:3122
23 warning_message = textwrap.dedent("""
24 The //build directory is meant to be as hermetic as possible so that
25 other projects (webrtc, v8, angle) can make use of it. If you are adding
26 a new dep from //build onto another directory, you should consider:
27 1) Can that dep live within //build?
28 2) Can the dep be guarded by "build_with_chromium"?
29 3) Have you made this new dep easy to pull in for other projects (ideally
30 a matter of adding a DEPS entry).:""")
31
32 def FilterFile(affected_file):
33 return input_api.FilterSourceFile(affected_file,
34 files_to_check=build_file_patterns)
35
36 problems = []
37 for f in input_api.AffectedSourceFiles(FilterFile):
38 local_path = f.LocalPath()
39 for line_number, line in f.ChangedContents():
Andrew Grievec0f8e8a2022-06-07 20:01:5240 if blocklist_pattern.search(line) and not allowlist_pattern.search(line):
Mohamed Heikal48cca322022-04-12 18:51:3141 problems.append('%s:%d\n %s' %
42 (local_path, line_number, line.strip()))
43 if problems:
44 return [output_api.PresubmitPromptOrNotify(warning_message, problems)]
45 else:
46 return []
Andrew Grieve685499482022-09-09 23:14:0847
48
49def CheckPythonTests(input_api, output_api):
50 return input_api.RunTests(
51 input_api.canned_checks.GetUnitTestsInDirectory(
52 input_api,
53 output_api,
54 input_api.PresubmitLocalPath(),
55 files_to_check=[r'.+_(?:unit)?test\.py$'],
56 run_on_python2=False,
57 run_on_python3=True))