PRESUBMIT.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. # Copyright (c) 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. """Top-level presubmit script for depot tools.
  5. See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts for
  6. details on the presubmit API built into depot_tools.
  7. """
  8. PRESUBMIT_VERSION = '2.0.0'
  9. import fnmatch
  10. import os
  11. import sys
  12. # Whether to run the checks under Python2 or Python3.
  13. # TODO: Uncomment this to run the checks under Python3, and change the tests
  14. # in _CommonChecks in this file and the values and tests in
  15. # //tests/PRESUBMIT.py as well to keep the test coverage of all three cases
  16. # (true, false, and default/not specified).
  17. # USE_PYTHON3 = False
  18. # CIPD ensure manifest for checking CIPD client itself.
  19. CIPD_CLIENT_ENSURE_FILE_TEMPLATE = r'''
  20. # Full supported.
  21. $VerifiedPlatform linux-amd64 mac-amd64 windows-amd64 windows-386
  22. # Best effort support.
  23. $VerifiedPlatform linux-386 linux-ppc64 linux-ppc64le linux-s390x
  24. $VerifiedPlatform linux-arm64 linux-armv6l
  25. $VerifiedPlatform linux-mips64 linux-mips64le linux-mipsle
  26. %s %s
  27. '''
  28. # Timeout for a test to be executed.
  29. TEST_TIMEOUT_S = 330 # 5m 30s
  30. def CheckPylint(input_api, output_api):
  31. """Gather all the pylint logic into one place to make it self-contained."""
  32. files_to_check = [
  33. r'^[^/]*\.py$',
  34. r'^testing_support/[^/]*\.py$',
  35. r'^tests/[^/]*\.py$',
  36. r'^recipe_modules/.*\.py$', # Allow recursive search in recipe modules.
  37. ]
  38. files_to_skip = list(input_api.DEFAULT_FILES_TO_SKIP)
  39. if os.path.exists('.gitignore'):
  40. with open('.gitignore') as fh:
  41. lines = [l.strip() for l in fh.readlines()]
  42. files_to_skip.extend([fnmatch.translate(l) for l in lines if
  43. l and not l.startswith('#')])
  44. if os.path.exists('.git/info/exclude'):
  45. with open('.git/info/exclude') as fh:
  46. lines = [l.strip() for l in fh.readlines()]
  47. files_to_skip.extend([fnmatch.translate(l) for l in lines if
  48. l and not l.startswith('#')])
  49. disabled_warnings = [
  50. 'R0401', # Cyclic import
  51. 'W0613', # Unused argument
  52. ]
  53. return input_api.RunTests(input_api.canned_checks.GetPylint(
  54. input_api,
  55. output_api,
  56. files_to_check=files_to_check,
  57. files_to_skip=files_to_skip,
  58. disabled_warnings=disabled_warnings))
  59. def CheckRecipes(input_api, output_api):
  60. file_filter = lambda x: x.LocalPath() == 'infra/config/recipes.cfg'
  61. return input_api.canned_checks.CheckJsonParses(input_api, output_api,
  62. file_filter=file_filter)
  63. def CheckPythonVersion(input_api, output_api):
  64. # The tests here are assuming this is not defined, so raise an error
  65. # if it is.
  66. if 'USE_PYTHON3' in globals():
  67. return [output_api.PresubmitError(
  68. 'USE_PYTHON3 is defined; update the tests in //PRESUBMIT.py and '
  69. '//tests/PRESUBMIT.py.')]
  70. if sys.version_info.major != 2:
  71. return [output_api.PresubmitError(
  72. 'Did not use Python2 for //PRESUBMIT.py by default.')]
  73. return []
  74. def CheckJsonFiles(input_api, output_api):
  75. return input_api.canned_checks.CheckJsonParses(
  76. input_api, output_api)
  77. def CheckUnitTestsOnCommit(input_api, output_api):
  78. # Do not run integration tests on upload since they are way too slow.
  79. input_api.SetTimeout(TEST_TIMEOUT_S)
  80. # Run only selected tests on Windows.
  81. test_to_run_list = [r'.*test\.py$']
  82. tests_to_skip_list = []
  83. if input_api.platform.startswith(('cygwin', 'win32')):
  84. print('Warning: skipping most unit tests on Windows')
  85. tests_to_skip_list.extend([
  86. r'.*auth_test\.py$',
  87. r'.*git_common_test\.py$',
  88. r'.*git_hyper_blame_test\.py$',
  89. r'.*git_map_test\.py$',
  90. r'.*ninjalog_uploader_test\.py$',
  91. r'.*recipes_test\.py$',
  92. ])
  93. py2_only_tests = [
  94. 'recipes_test.py',
  95. ]
  96. py3_only_tests = ['ninjalog_uploader_test.py']
  97. tests = input_api.canned_checks.GetUnitTestsInDirectory(
  98. input_api,
  99. output_api,
  100. 'tests',
  101. files_to_check=test_to_run_list,
  102. files_to_skip=tests_to_skip_list + py2_only_tests + py3_only_tests,
  103. run_on_python3=True)
  104. # TODO: once py3 compatbile, remove those tests
  105. tests.extend(
  106. input_api.canned_checks.GetUnitTestsInDirectory(
  107. input_api,
  108. output_api,
  109. 'tests',
  110. files_to_check=py2_only_tests,
  111. files_to_skip=tests_to_skip_list,
  112. run_on_python3=False))
  113. # TODO: use this for all tests when py2 support is dropped.
  114. tests.extend(
  115. input_api.canned_checks.GetUnitTestsInDirectory(
  116. input_api,
  117. output_api,
  118. 'tests',
  119. files_to_check=py3_only_tests,
  120. files_to_skip=tests_to_skip_list,
  121. run_on_python3=True,
  122. run_on_python2=False))
  123. return input_api.RunTests(tests)
  124. def CheckCIPDManifest(input_api, output_api):
  125. # Validate CIPD manifests.
  126. root = input_api.os_path.normpath(
  127. input_api.os_path.abspath(input_api.PresubmitLocalPath()))
  128. rel_file = lambda rel: input_api.os_path.join(root, rel)
  129. cipd_manifests = set(rel_file(input_api.os_path.join(*x)) for x in (
  130. ('cipd_manifest.txt',),
  131. ('bootstrap', 'manifest.txt'),
  132. ('bootstrap', 'manifest_bleeding_edge.txt'),
  133. # Also generate a file for the cipd client itself.
  134. ('cipd_client_version',),
  135. ))
  136. affected_manifests = input_api.AffectedFiles(
  137. include_deletes=False,
  138. file_filter=lambda x:
  139. input_api.os_path.normpath(x.AbsoluteLocalPath()) in cipd_manifests)
  140. tests = []
  141. for path in affected_manifests:
  142. path = path.AbsoluteLocalPath()
  143. if path.endswith('.txt'):
  144. tests.append(input_api.canned_checks.CheckCIPDManifest(
  145. input_api, output_api, path=path))
  146. else:
  147. pkg = 'infra/tools/cipd/${platform}'
  148. ver = input_api.ReadFile(path)
  149. tests.append(input_api.canned_checks.CheckCIPDManifest(
  150. input_api, output_api,
  151. content=CIPD_CLIENT_ENSURE_FILE_TEMPLATE % (pkg, ver)))
  152. tests.append(input_api.canned_checks.CheckCIPDClientDigests(
  153. input_api, output_api, client_version_file=path))
  154. return input_api.RunTests(tests)
  155. def CheckOwnersFormat(input_api, output_api):
  156. return input_api.canned_checks.CheckOwnersFormat(input_api, output_api)
  157. def CheckOwnersOnUpload(input_api, output_api):
  158. return input_api.canned_checks.CheckOwners(input_api, output_api,
  159. allow_tbr=False)
  160. def CheckDoNotSubmitOnCommit(input_api, output_api):
  161. return input_api.canned_checks.CheckDoNotSubmit(input_api, output_api)