PRESUBMIT.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Copyright 2015 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. USE_PYTHON3 = True
  5. _IGNORE_FREEZE_FOOTER = 'Ignore-Freeze'
  6. # The time module's handling of timezones is abysmal, so the boundaries are
  7. # precomputed in UNIX time
  8. _FREEZE_START = 1639641600 # 2021/12/16 00:00 -0800
  9. _FREEZE_END = 1641196800 # 2022/01/03 00:00 -0800
  10. def CheckFreeze(input_api, output_api):
  11. if _FREEZE_START <= input_api.time.time() < _FREEZE_END:
  12. footers = input_api.change.GitFootersFromDescription()
  13. if _IGNORE_FREEZE_FOOTER not in footers:
  14. def convert(t):
  15. ts = input_api.time.localtime(t)
  16. return input_api.time.strftime('%Y/%m/%d %H:%M %z', ts)
  17. return [
  18. output_api.PresubmitError(
  19. 'There is a prod freeze in effect from {} until {},'
  20. ' files in //tools/mb cannot be modified'.format(
  21. convert(_FREEZE_START), convert(_FREEZE_END)))
  22. ]
  23. return []
  24. def CheckTests(input_api, output_api):
  25. glob = input_api.os_path.join(input_api.PresubmitLocalPath(), '*_test.py')
  26. tests = input_api.canned_checks.GetUnitTests(input_api,
  27. output_api,
  28. input_api.glob(glob),
  29. run_on_python2=False,
  30. run_on_python3=True,
  31. skip_shebang_check=True)
  32. return input_api.RunTests(tests)
  33. def _CommonChecks(input_api, output_api):
  34. results = []
  35. # Run Pylint over the files in the directory.
  36. pylint_checks = input_api.canned_checks.GetPylint(
  37. input_api,
  38. output_api,
  39. version='2.7',
  40. # pylint complains about Checkfreeze not being defined, its probably
  41. # finding a different PRESUBMIT.py. Note that this warning only appears if
  42. # the number of Pylint jobs is greater than one.
  43. files_to_skip=['PRESUBMIT_test.py'],
  44. # Disabling this warning because this pattern - involving ToSrcRelPath -
  45. # seems intrinsic to how mb_unittest.py is implemented.
  46. disabled_warnings=[
  47. 'attribute-defined-outside-init',
  48. ],
  49. )
  50. results.extend(input_api.RunTests(pylint_checks))
  51. # Run the MB unittests.
  52. results.extend(
  53. input_api.canned_checks.RunUnitTestsInDirectory(input_api,
  54. output_api,
  55. '.',
  56. [r'^.+_unittest\.py$'],
  57. run_on_python2=False,
  58. run_on_python3=True,
  59. skip_shebang_check=True))
  60. # Validate the format of the mb_config.pyl file.
  61. cmd = [input_api.python3_executable, 'mb.py', 'validate']
  62. kwargs = {'cwd': input_api.PresubmitLocalPath()}
  63. results.extend(input_api.RunTests([
  64. input_api.Command(name='mb_validate',
  65. cmd=cmd, kwargs=kwargs,
  66. message=output_api.PresubmitError)]))
  67. results.extend(CheckFreeze(input_api, output_api))
  68. results.extend(CheckTests(input_api, output_api))
  69. return results
  70. def CheckChangeOnUpload(input_api, output_api):
  71. return _CommonChecks(input_api, output_api)
  72. def CheckChangeOnCommit(input_api, output_api):
  73. return _CommonChecks(input_api, output_api)