PRESUBMIT.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. """Presubmit script for devil.
  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. def _RunPylint(input_api, output_api):
  9. return input_api.RunTests(input_api.canned_checks.RunPylint(
  10. input_api, output_api, pylintrc='pylintrc'))
  11. def _RunUnitTests(input_api, output_api):
  12. def J(*dirs):
  13. """Returns a path relative to presubmit directory."""
  14. return input_api.os_path.join(
  15. input_api.PresubmitLocalPath(), 'devil', *dirs)
  16. test_env = dict(input_api.environ)
  17. test_env.update({
  18. 'PYTHONDONTWRITEBYTECODE': '1',
  19. 'PYTHONPATH': ':'.join([J(), J('..')]),
  20. })
  21. message_type = (output_api.PresubmitError if input_api.is_committing
  22. else output_api.PresubmitPromptWarning)
  23. return input_api.RunTests([
  24. input_api.Command(
  25. name='devil/bin/run_py_tests',
  26. cmd=[
  27. input_api.os_path.join(
  28. input_api.PresubmitLocalPath(), 'bin', 'run_py_tests')],
  29. kwargs={'env': test_env},
  30. message=message_type)])
  31. def _EnsureNoPylibUse(input_api, output_api):
  32. def other_python_files(f):
  33. this_presubmit_file = input_api.os_path.join(
  34. input_api.PresubmitLocalPath(), 'PRESUBMIT.py')
  35. return (f.LocalPath().endswith('.py')
  36. and not f.AbsoluteLocalPath() == this_presubmit_file)
  37. changed_files = input_api.AffectedSourceFiles(other_python_files)
  38. import_error_re = input_api.re.compile(
  39. r'(from pylib.* import)|(import pylib)')
  40. errors = []
  41. for f in changed_files:
  42. errors.extend(
  43. '%s:%d' % (f.LocalPath(), line_number)
  44. for line_number, line_text in f.ChangedContents()
  45. if import_error_re.search(line_text))
  46. if errors:
  47. return [output_api.PresubmitError(
  48. 'pylib modules should not be imported from devil modules.',
  49. items=errors)]
  50. return []
  51. def CommonChecks(input_api, output_api):
  52. output = []
  53. output += _RunPylint(input_api, output_api)
  54. output += _RunUnitTests(input_api, output_api)
  55. output += _EnsureNoPylibUse(input_api, output_api)
  56. return output
  57. def CheckChangeOnUpload(input_api, output_api):
  58. return CommonChecks(input_api, output_api)
  59. def CheckChangeOnCommit(input_api, output_api):
  60. return CommonChecks(input_api, output_api)