PRESUBMIT.py 4.63 KB
Newer Older
1
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 3 4 5 6 7
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Top-level presubmit script for depot tools.

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

11 12 13
import fnmatch
import os

14

15 16 17 18 19 20
# CIPD ensure manifest for checking CIPD client itself.
CIPD_CLIENT_ENSURE_FILE_TEMPLATE = r'''
# Full supported.
$VerifiedPlatform linux-amd64 mac-amd64 windows-amd64 windows-386
# Best effort support.
$VerifiedPlatform linux-386 linux-ppc64 linux-ppc64le linux-s390x
21 22
$VerifiedPlatform linux-arm64 linux-armv6l
$VerifiedPlatform linux-mips64 linux-mips64le linux-mipsle
23 24 25 26 27

%s %s
'''


28 29 30 31 32 33 34
def DepotToolsPylint(input_api, output_api):
  """Gather all the pylint logic into one place to make it self-contained."""
  white_list = [
    r'^[^/]*\.py$',
    r'^testing_support/[^/]*\.py$',
    r'^tests/[^/]*\.py$',
    r'^recipe_modules/.*\.py$',  # Allow recursive search in recipe modules.
35
  ]
36
  black_list = list(input_api.DEFAULT_BLACK_LIST)
37 38 39 40 41 42 43 44 45 46
  if os.path.exists('.gitignore'):
    with open('.gitignore') as fh:
      lines = [l.strip() for l in fh.readlines()]
      black_list.extend([fnmatch.translate(l) for l in lines if
                         l and not l.startswith('#')])
  if os.path.exists('.git/info/exclude'):
    with open('.git/info/exclude') as fh:
      lines = [l.strip() for l in fh.readlines()]
      black_list.extend([fnmatch.translate(l) for l in lines if
                         l and not l.startswith('#')])
47 48 49 50
  disabled_warnings = [
    'R0401',  # Cyclic import
    'W0613',  # Unused argument
  ]
51
  return input_api.canned_checks.GetPylint(
52 53
      input_api,
      output_api,
54
      white_list=white_list,
55 56
      black_list=black_list,
      disabled_warnings=disabled_warnings)
57 58 59 60 61


def CommonChecks(input_api, output_api, tests_to_black_list):
  results = []
  results.extend(input_api.canned_checks.CheckOwners(input_api, output_api))
62 63
  results.extend(input_api.canned_checks.CheckOwnersFormat(
      input_api, output_api))
64 65
  results.extend(input_api.canned_checks.CheckJsonParses(
      input_api, output_api))
66 67 68 69 70 71 72

  # Run only selected tests on Windows.
  tests_to_white_list = [r'.*test\.py$']
  if input_api.platform.startswith(('cygwin', 'win32')):
    print('Warning: skipping most unit tests on Windows')
    tests_to_white_list = [r'.*cipd_bootstrap_test\.py$']

73 74
  # TODO(maruel): Make sure at least one file is modified first.
  # TODO(maruel): If only tests are modified, only run them.
75
  tests = DepotToolsPylint(input_api, output_api)
76
  tests.extend(input_api.canned_checks.GetUnitTestsInDirectory(
77 78 79
      input_api,
      output_api,
      'tests',
80 81
      whitelist=tests_to_white_list,
      blacklist=tests_to_black_list))
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

  # Validate CIPD manifests.
  root = input_api.os_path.normpath(
    input_api.os_path.abspath(input_api.PresubmitLocalPath()))
  rel_file = lambda rel: input_api.os_path.join(root, rel)
  cipd_manifests = set(rel_file(input_api.os_path.join(*x)) for x in (
    ('cipd_manifest.txt',),
    ('bootstrap', 'win', 'manifest.txt'),
    ('bootstrap', 'win', 'manifest_bleeding_edge.txt'),

    # Also generate a file for the cipd client itself.
    ('cipd_client_version',),
  ))
  affected_manifests = input_api.AffectedFiles(
    include_deletes=False,
    file_filter=lambda x:
      input_api.os_path.normpath(x.AbsoluteLocalPath()) in cipd_manifests)
  for path in affected_manifests:
    path = path.AbsoluteLocalPath()
    if path.endswith('.txt'):
      tests.append(input_api.canned_checks.CheckCIPDManifest(
          input_api, output_api, path=path))
    else:
      pkg = 'infra/tools/cipd/${platform}'
      ver = input_api.ReadFile(path)
      tests.append(input_api.canned_checks.CheckCIPDManifest(
108 109
          input_api, output_api,
          content=CIPD_CLIENT_ENSURE_FILE_TEMPLATE % (pkg, ver)))
110 111
      tests.append(input_api.canned_checks.CheckCIPDClientDigests(
          input_api, output_api, client_version_file=path))
112

113
  results.extend(input_api.RunTests(tests))
114
  return results
115 116


117
def CheckChangeOnUpload(input_api, output_api):
118 119 120
  # Do not run integration tests on upload since they are way too slow.
  tests_to_black_list = [
      r'^checkout_test\.py$',
121
      r'^cipd_bootstrap_test\.py$',
122 123
      r'^gclient_smoketest\.py$',
      r'^scm_unittest\.py$',
124
      r'^subprocess2_test\.py$',
125
  ]
126
  return CommonChecks(input_api, output_api, tests_to_black_list)
127 128


129
def CheckChangeOnCommit(input_api, output_api):
130
  output = []
131
  output.extend(CommonChecks(input_api, output_api, []))
132 133 134
  output.extend(input_api.canned_checks.CheckDoNotSubmit(
      input_api,
      output_api))
135
  return output