Avi Drissman | 73a09d1 | 2022-09-08 20:33:38 | [diff] [blame] | 1 | # Copyright 2022 The Chromium Authors |
Mohamed Heikal | 48cca32 | 2022-04-12 18:51:31 | [diff] [blame] | 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_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. |
| 9 | USE_PYTHON3 = True |
| 10 | |
| 11 | import textwrap |
| 12 | |
| 13 | |
| 14 | def 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 Grieve | 68549948 | 2022-09-09 23:14:08 | [diff] [blame] | 20 | blocklist_pattern = input_api.re.compile(r'^[^#]*"//(?!build).+?/.*"') |
| 21 | allowlist_pattern = input_api.re.compile(r'^[^#]*"//third_party/junit') |
Mohamed Heikal | 48cca32 | 2022-04-12 18:51:31 | [diff] [blame] | 22 | |
| 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 Grieve | c0f8e8a | 2022-06-07 20:01:52 | [diff] [blame] | 40 | if blocklist_pattern.search(line) and not allowlist_pattern.search(line): |
Mohamed Heikal | 48cca32 | 2022-04-12 18:51:31 | [diff] [blame] | 41 | 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 Grieve | 68549948 | 2022-09-09 23:14:08 | [diff] [blame] | 47 | |
| 48 | |
| 49 | def 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)) |