blob: 13a2428fe33d5a87178b74a74e0fbd1884655e19 [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))
scottmg@chromium.org69ae86a2014-01-17 03:55:45 +000059 # TODO(maruel): Make sure at least one file is modified first.
60 # TODO(maruel): If only tests are modified, only run them.
agablef39c3332016-09-26 09:35:42 -070061 tests = DepotToolsPylint(input_api, output_api)
scottmg@chromium.org69ae86a2014-01-17 03:55:45 +000062 unit_tests = input_api.canned_checks.GetUnitTestsInDirectory(
63 input_api,
64 output_api,
65 'tests',
66 whitelist=[r'.*test\.py$'],
67 blacklist=tests_to_black_list)
scottmg@chromium.org69ae86a2014-01-17 03:55:45 +000068 if not input_api.platform.startswith(('cygwin', 'win32')):
69 tests.extend(unit_tests)
70 else:
71 print('Warning: not running unit tests on Windows')
Robert Iannucci7d47eb52017-12-06 12:18:18 -080072
73 # Validate CIPD manifests.
74 root = input_api.os_path.normpath(
75 input_api.os_path.abspath(input_api.PresubmitLocalPath()))
76 rel_file = lambda rel: input_api.os_path.join(root, rel)
77 cipd_manifests = set(rel_file(input_api.os_path.join(*x)) for x in (
78 ('cipd_manifest.txt',),
79 ('bootstrap', 'win', 'manifest.txt'),
80 ('bootstrap', 'win', 'manifest_bleeding_edge.txt'),
81
82 # Also generate a file for the cipd client itself.
83 ('cipd_client_version',),
84 ))
85 affected_manifests = input_api.AffectedFiles(
86 include_deletes=False,
87 file_filter=lambda x:
88 input_api.os_path.normpath(x.AbsoluteLocalPath()) in cipd_manifests)
89 for path in affected_manifests:
90 path = path.AbsoluteLocalPath()
91 if path.endswith('.txt'):
92 tests.append(input_api.canned_checks.CheckCIPDManifest(
93 input_api, output_api, path=path))
94 else:
95 pkg = 'infra/tools/cipd/${platform}'
96 ver = input_api.ReadFile(path)
97 tests.append(input_api.canned_checks.CheckCIPDManifest(
98 input_api, output_api, content=ENSURE_FILE_TEMPLATE % (pkg, ver)))
99
scottmg@chromium.org69ae86a2014-01-17 03:55:45 +0000100 results.extend(input_api.RunTests(tests))
maruel@chromium.org3c9f7ca2011-04-01 14:52:38 +0000101 return results
maruel@chromium.org2a74d372011-03-29 19:05:50 +0000102
103
maruel@chromium.orge94aedc2010-12-13 21:11:30 +0000104def CheckChangeOnUpload(input_api, output_api):
maruel@chromium.orgd52417c2011-09-02 20:13:17 +0000105 # Do not run integration tests on upload since they are way too slow.
106 tests_to_black_list = [
107 r'^checkout_test\.py$',
108 r'^gclient_smoketest\.py$',
109 r'^scm_unittest\.py$',
ilevy@chromium.org58565622013-03-17 23:18:37 +0000110 r'^subprocess2_test\.py$',
maruel@chromium.orgd52417c2011-09-02 20:13:17 +0000111 ]
112 return CommonChecks(input_api, output_api, tests_to_black_list)
maruel@chromium.orge94aedc2010-12-13 21:11:30 +0000113
114
maruel@chromium.orgce30ef72009-05-25 15:20:42 +0000115def CheckChangeOnCommit(input_api, output_api):
116 output = []
maruel@chromium.orgd52417c2011-09-02 20:13:17 +0000117 output.extend(CommonChecks(input_api, output_api, []))
maruel@chromium.orge94aedc2010-12-13 21:11:30 +0000118 output.extend(input_api.canned_checks.CheckDoNotSubmit(
119 input_api,
120 output_api))
maruel@chromium.org7b305e82009-05-19 18:24:20 +0000121 return output