aboutsummaryrefslogtreecommitdiff
path: root/catapult/devil/PRESUBMIT.py
blob: ec48dddf993b58bec7c1c4849bb4986d25861dd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Presubmit script for devil.

See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts for
details on the presubmit API built into depot_tools.
"""


def _RunPylint(input_api, output_api):
  return input_api.RunTests(
      input_api.canned_checks.RunPylint(
          input_api, output_api, pylintrc='pylintrc'))


def _RunUnitTests(input_api, output_api):
  def J(*dirs):
    """Returns a path relative to presubmit directory."""
    return input_api.os_path.join(input_api.PresubmitLocalPath(), 'devil',
                                  *dirs)

  test_env = dict(input_api.environ)
  test_env.update({
      'PYTHONDONTWRITEBYTECODE': '1',
      'PYTHONPATH': ':'.join([J(), J('..')]),
  })

  message_type = (output_api.PresubmitError if input_api.is_committing else
                  output_api.PresubmitPromptWarning)

  return input_api.RunTests([
      input_api.Command(name='devil/bin/run_py_tests',
                        cmd=[
                            input_api.os_path.join(
                                input_api.PresubmitLocalPath(), 'bin',
                                'run_py_tests')
                        ],
                        kwargs={'env': test_env},
                        message=message_type),
      input_api.Command(name='devil/bin/run_py3_tests',
                        cmd=[
                            input_api.os_path.join(
                                input_api.PresubmitLocalPath(), 'bin',
                                'run_py3_tests')
                        ],
                        kwargs={'env': test_env},
                        message=message_type,
                        python3=True),
  ])


def _EnsureNoPylibUse(input_api, output_api):
  def other_python_files(f):
    this_presubmit_file = input_api.os_path.join(input_api.PresubmitLocalPath(),
                                                 'PRESUBMIT.py')
    return (f.LocalPath().endswith('.py')
            and not f.AbsoluteLocalPath() == this_presubmit_file)

  changed_files = input_api.AffectedSourceFiles(other_python_files)
  import_error_re = input_api.re.compile(
      r'(from pylib.* import)|(import pylib)')

  errors = []
  for f in changed_files:
    errors.extend('%s:%d' % (f.LocalPath(), line_number)
                  for line_number, line_text in f.ChangedContents()
                  if import_error_re.search(line_text))

  if errors:
    return [
        output_api.PresubmitError(
            'pylib modules should not be imported from devil modules.',
            items=errors)
    ]
  return []


def CommonChecks(input_api, output_api):
  output = []
  output += _RunPylint(input_api, output_api)
  output += _RunUnitTests(input_api, output_api)
  output += _EnsureNoPylibUse(input_api, output_api)
  return output


def CheckChangeOnUpload(input_api, output_api):
  return CommonChecks(input_api, output_api)


def CheckChangeOnCommit(input_api, output_api):
  return CommonChecks(input_api, output_api)