PRESUBMIT.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # Copyright 2022 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. PRESUBMIT_VERSION = '2.0.0'
  5. # This line is 'magic' in that git-cl looks for it to decide whether to
  6. # use Python3 instead of Python2 when running the code in this file.
  7. USE_PYTHON3 = True
  8. import textwrap
  9. def CheckNoBadDeps(input_api, output_api):
  10. """Prevent additions of bad dependencies from the //build prefix."""
  11. build_file_patterns = [
  12. r'(.+/)?BUILD\.gn',
  13. r'.+\.gni',
  14. ]
  15. blocklist_pattern = input_api.re.compile(
  16. r'^[^#]*//(?:base|third_party|components)')
  17. allowlist_pattern = input_api.re.compile(r'^[^#]*//third_party/junit')
  18. warning_message = textwrap.dedent("""
  19. The //build directory is meant to be as hermetic as possible so that
  20. other projects (webrtc, v8, angle) can make use of it. If you are adding
  21. a new dep from //build onto another directory, you should consider:
  22. 1) Can that dep live within //build?
  23. 2) Can the dep be guarded by "build_with_chromium"?
  24. 3) Have you made this new dep easy to pull in for other projects (ideally
  25. a matter of adding a DEPS entry).:""")
  26. def FilterFile(affected_file):
  27. return input_api.FilterSourceFile(affected_file,
  28. files_to_check=build_file_patterns)
  29. problems = []
  30. for f in input_api.AffectedSourceFiles(FilterFile):
  31. local_path = f.LocalPath()
  32. for line_number, line in f.ChangedContents():
  33. if blocklist_pattern.search(line) and not allowlist_pattern.search(line):
  34. problems.append('%s:%d\n %s' %
  35. (local_path, line_number, line.strip()))
  36. if problems:
  37. return [output_api.PresubmitPromptOrNotify(warning_message, problems)]
  38. else:
  39. return []