blob: 8c4de46e0e17c79f5a1a7f5eb05334eedd0b7a04 [file] [log] [blame]
chrisha@chromium.orgc920ad22012-02-02 18:26:04 +00001# Copyright (c) 2012 The Chromium Authors. All rights reserved.
maruel@chromium.org3d235242009-05-15 12:40:48 +00002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Top-level presubmit script for depot tools.
6
7See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts for
maruel@chromium.org2a74d372011-03-29 19:05:50 +00008details on the presubmit API built into depot_tools.
maruel@chromium.org3d235242009-05-15 12:40:48 +00009"""
10
szager@chromium.org34071032014-03-18 17:23:48 +000011import fnmatch
12import os
13
dpranke@chromium.org627ea672011-03-11 23:29:03 +000014
Robert Iannucci7d47eb52017-12-06 12:18:18 -080015ENSURE_FILE_TEMPLATE = r'''
16$VerifiedPlatform linux-386 linux-amd64 linux-arm64 linux-armv6l linux-mips64
17$VerifiedPlatform linux-ppc64 linux-ppc64le linux-s390x
18$VerifiedPlatform mac-amd64
19$VerifiedPlatform windows-386 windows-amd64
20
21%s %s
22'''
23
24
agablef39c3332016-09-26 09:35:42 -070025def DepotToolsPylint(input_api, output_api):
26 """Gather all the pylint logic into one place to make it self-contained."""
27 white_list = [
28 r'^[^/]*\.py$',
29 r'^testing_support/[^/]*\.py$',
30 r'^tests/[^/]*\.py$',
31 r'^recipe_modules/.*\.py$', # Allow recursive search in recipe modules.
tandrii@chromium.org55cb27e2016-05-16 17:54:40 +000032 ]
agablef39c3332016-09-26 09:35:42 -070033 black_list = list(input_api.DEFAULT_BLACK_LIST)
szager@chromium.org34071032014-03-18 17:23:48 +000034 if os.path.exists('.gitignore'):
35 with open('.gitignore') as fh:
36 lines = [l.strip() for l in fh.readlines()]
37 black_list.extend([fnmatch.translate(l) for l in lines if
38 l and not l.startswith('#')])
39 if os.path.exists('.git/info/exclude'):
40 with open('.git/info/exclude') as fh:
41 lines = [l.strip() for l in fh.readlines()]
42 black_list.extend([fnmatch.translate(l) for l in lines if
43 l and not l.startswith('#')])
maruel@chromium.orgff9a2172012-04-24 16:55:32 +000044 disabled_warnings = [
45 'R0401', # Cyclic import
46 'W0613', # Unused argument
47 ]
agablef39c3332016-09-26 09:35:42 -070048 return input_api.canned_checks.GetPylint(
scottmg@chromium.org69ae86a2014-01-17 03:55:45 +000049 input_api,
50 output_api,
agablef39c3332016-09-26 09:35:42 -070051 white_list=white_list,
scottmg@chromium.org69ae86a2014-01-17 03:55:45 +000052 black_list=black_list,
53 disabled_warnings=disabled_warnings)
agablef39c3332016-09-26 09:35:42 -070054
55
56def CommonChecks(input_api, output_api, tests_to_black_list):
57 results = []
58 results.extend(input_api.canned_checks.CheckOwners(input_api, output_api))
Edward Lesmescb62e482018-04-19 18:29:35 -040059 results.extend(input_api.canned_checks.CheckOwnersFormat(
60 input_api, output_api))
scottmg@chromium.org69ae86a2014-01-17 03:55:45 +000061 # TODO(maruel): Make sure at least one file is modified first.
62 # TODO(maruel): If only tests are modified, only run them.
agablef39c3332016-09-26 09:35:42 -070063 tests = DepotToolsPylint(input_api, output_api)
scottmg@chromium.org69ae86a2014-01-17 03:55:45 +000064 unit_tests = input_api.canned_checks.GetUnitTestsInDirectory(
65 input_api,
66 output_api,
67 'tests',
68 whitelist=[r'.*test\.py$'],
69 blacklist=tests_to_black_list)
scottmg@chromium.org69ae86a2014-01-17 03:55:45 +000070 if not input_api.platform.startswith(('cygwin', 'win32')):
71 tests.extend(unit_tests)
72 else:
73 print('Warning: not running unit tests on Windows')
Robert Iannucci7d47eb52017-12-06 12:18:18 -080074
75 # Validate CIPD manifests.
76 root = input_api.os_path.normpath(
77 input_api.os_path.abspath(input_api.PresubmitLocalPath()))
78 rel_file = lambda rel: input_api.os_path.join(root, rel)
79 cipd_manifests = set(rel_file(input_api.os_path.join(*x)) for x in (
80 ('cipd_manifest.txt',),
81 ('bootstrap', 'win', 'manifest.txt'),
82 ('bootstrap', 'win', 'manifest_bleeding_edge.txt'),
83
84 # Also generate a file for the cipd client itself.
85 ('cipd_client_version',),
86 ))
87 affected_manifests = input_api.AffectedFiles(
88 include_deletes=False,
89 file_filter=lambda x:
90 input_api.os_path.normpath(x.AbsoluteLocalPath()) in cipd_manifests)
91 for path in affected_manifests:
92 path = path.AbsoluteLocalPath()
93 if path.endswith('.txt'):
94 tests.append(input_api.canned_checks.CheckCIPDManifest(
95 input_api, output_api, path=path))
96 else:
97 pkg = 'infra/tools/cipd/${platform}'
98 ver = input_api.ReadFile(path)
99 tests.append(input_api.canned_checks.CheckCIPDManifest(
100 input_api, output_api, content=ENSURE_FILE_TEMPLATE % (pkg, ver)))
101
scottmg@chromium.org69ae86a2014-01-17 03:55:45 +0000102 results.extend(input_api.RunTests(tests))
maruel@chromium.org3c9f7ca2011-04-01 14:52:38 +0000103 return results
maruel@chromium.org2a74d372011-03-29 19:05:50 +0000104
105
maruel@chromium.orge94aedc2010-12-13 21:11:30 +0000106def CheckChangeOnUpload(input_api, output_api):
maruel@chromium.orgd52417c2011-09-02 20:13:17 +0000107 # Do not run integration tests on upload since they are way too slow.
108 tests_to_black_list = [
109 r'^checkout_test\.py$',
110 r'^gclient_smoketest\.py$',
111 r'^scm_unittest\.py$',
ilevy@chromium.org58565622013-03-17 23:18:37 +0000112 r'^subprocess2_test\.py$',
maruel@chromium.orgd52417c2011-09-02 20:13:17 +0000113 ]
114 return CommonChecks(input_api, output_api, tests_to_black_list)
maruel@chromium.orge94aedc2010-12-13 21:11:30 +0000115
116
maruel@chromium.orgce30ef72009-05-25 15:20:42 +0000117def CheckChangeOnCommit(input_api, output_api):
118 output = []
maruel@chromium.orgd52417c2011-09-02 20:13:17 +0000119 output.extend(CommonChecks(input_api, output_api, []))
maruel@chromium.orge94aedc2010-12-13 21:11:30 +0000120 output.extend(input_api.canned_checks.CheckDoNotSubmit(
121 input_api,
122 output_api))
maruel@chromium.org7b305e82009-05-19 18:24:20 +0000123 return output