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
6See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts for
7details on the presubmit API built into depot_tools.
8"""
9
10
11def _RunPylint(input_api, output_api):
12  return input_api.RunTests(
13      input_api.canned_checks.RunPylint(
14          input_api, output_api, pylintrc='pylintrc'))
15
16
17def _RunUnitTests(input_api, output_api):
18  def J(*dirs):
19    """Returns a path relative to presubmit directory."""
20    return input_api.os_path.join(input_api.PresubmitLocalPath(), 'devil',
21                                  *dirs)
22
23  test_env = dict(input_api.environ)
24  test_env.update({
25      'PYTHONDONTWRITEBYTECODE': '1',
26      'PYTHONPATH': ':'.join([J(), J('..')]),
27  })
28
29  message_type = (output_api.PresubmitError if input_api.is_committing else
30                  output_api.PresubmitPromptWarning)
31
32  return input_api.RunTests([
33      input_api.Command(name='devil/bin/run_py_tests',
34                        cmd=[
35                            input_api.os_path.join(
36                                input_api.PresubmitLocalPath(), 'bin',
37                                'run_py_tests')
38                        ],
39                        kwargs={'env': test_env},
40                        message=message_type),
41      input_api.Command(name='devil/bin/run_py3_tests',
42                        cmd=[
43                            input_api.os_path.join(
44                                input_api.PresubmitLocalPath(), 'bin',
45                                'run_py3_tests')
46                        ],
47                        kwargs={'env': test_env},
48                        message=message_type,
49                        python3=True),
50  ])
51
52
53def _EnsureNoPylibUse(input_api, output_api):
54  def other_python_files(f):
55    this_presubmit_file = input_api.os_path.join(input_api.PresubmitLocalPath(),
56                                                 'PRESUBMIT.py')
57    return (f.LocalPath().endswith('.py')
58            and not f.AbsoluteLocalPath() == this_presubmit_file)
59
60  changed_files = input_api.AffectedSourceFiles(other_python_files)
61  import_error_re = input_api.re.compile(
62      r'(from pylib.* import)|(import pylib)')
63
64  errors = []
65  for f in changed_files:
66    errors.extend('%s:%d' % (f.LocalPath(), line_number)
67                  for line_number, line_text in f.ChangedContents()
68                  if import_error_re.search(line_text))
69
70  if errors:
71    return [
72        output_api.PresubmitError(
73            'pylib modules should not be imported from devil modules.',
74            items=errors)
75    ]
76  return []
77
78
79def CommonChecks(input_api, output_api):
80  output = []
81  output += _RunPylint(input_api, output_api)
82  output += _RunUnitTests(input_api, output_api)
83  output += _EnsureNoPylibUse(input_api, output_api)
84  return output
85
86
87def CheckChangeOnUpload(input_api, output_api):
88  return CommonChecks(input_api, output_api)
89
90
91def CheckChangeOnCommit(input_api, output_api):
92  return CommonChecks(input_api, output_api)
93