blob: ebd5c2f0ea88db9cdef47d6c8552fa9c2a01484f [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
Vadim Shtayura09098852018-06-21 22:50:30 +000015# CIPD ensure manifest for checking CIPD client itself.
16CIPD_CLIENT_ENSURE_FILE_TEMPLATE = r'''
17# Full supported.
18$VerifiedPlatform linux-amd64 mac-amd64 windows-amd64 windows-386
19# Best effort support.
20$VerifiedPlatform linux-386 linux-ppc64 linux-ppc64le linux-s390x
21$VerifiedPlatform linux-arm64 linux-armv6l linux-mips64
Robert Iannucci7d47eb52017-12-06 12:18:18 -080022
23%s %s
24'''
25
26
agablef39c3332016-09-26 09:35:42 -070027def DepotToolsPylint(input_api, output_api):
28 """Gather all the pylint logic into one place to make it self-contained."""
29 white_list = [
30 r'^[^/]*\.py$',
31 r'^testing_support/[^/]*\.py$',
32 r'^tests/[^/]*\.py$',
33 r'^recipe_modules/.*\.py$', # Allow recursive search in recipe modules.
tandrii@chromium.org55cb27e2016-05-16 17:54:40 +000034 ]
agablef39c3332016-09-26 09:35:42 -070035 black_list = list(input_api.DEFAULT_BLACK_LIST)
szager@chromium.org34071032014-03-18 17:23:48 +000036 if os.path.exists('.gitignore'):
37 with open('.gitignore') as fh:
38 lines = [l.strip() for l in fh.readlines()]
39 black_list.extend([fnmatch.translate(l) for l in lines if
40 l and not l.startswith('#')])
41 if os.path.exists('.git/info/exclude'):
42 with open('.git/info/exclude') as fh:
43 lines = [l.strip() for l in fh.readlines()]
44 black_list.extend([fnmatch.translate(l) for l in lines if
45 l and not l.startswith('#')])
maruel@chromium.orgff9a2172012-04-24 16:55:32 +000046 disabled_warnings = [
47 'R0401', # Cyclic import
48 'W0613', # Unused argument
49 ]
agablef39c3332016-09-26 09:35:42 -070050 return input_api.canned_checks.GetPylint(
scottmg@chromium.org69ae86a2014-01-17 03:55:45 +000051 input_api,
52 output_api,
agablef39c3332016-09-26 09:35:42 -070053 white_list=white_list,
scottmg@chromium.org69ae86a2014-01-17 03:55:45 +000054 black_list=black_list,
55 disabled_warnings=disabled_warnings)
agablef39c3332016-09-26 09:35:42 -070056
57
58def CommonChecks(input_api, output_api, tests_to_black_list):
59 results = []
60 results.extend(input_api.canned_checks.CheckOwners(input_api, output_api))
Edward Lesmescb62e482018-04-19 18:29:35 -040061 results.extend(input_api.canned_checks.CheckOwnersFormat(
62 input_api, output_api))
scottmg@chromium.org69ae86a2014-01-17 03:55:45 +000063 # TODO(maruel): Make sure at least one file is modified first.
64 # TODO(maruel): If only tests are modified, only run them.
agablef39c3332016-09-26 09:35:42 -070065 tests = DepotToolsPylint(input_api, output_api)
scottmg@chromium.org69ae86a2014-01-17 03:55:45 +000066 unit_tests = input_api.canned_checks.GetUnitTestsInDirectory(
67 input_api,
68 output_api,
69 'tests',
70 whitelist=[r'.*test\.py$'],
71 blacklist=tests_to_black_list)
scottmg@chromium.org69ae86a2014-01-17 03:55:45 +000072 if not input_api.platform.startswith(('cygwin', 'win32')):
73 tests.extend(unit_tests)
74 else:
75 print('Warning: not running unit tests on Windows')
Robert Iannucci7d47eb52017-12-06 12:18:18 -080076
77 # Validate CIPD manifests.
78 root = input_api.os_path.normpath(
79 input_api.os_path.abspath(input_api.PresubmitLocalPath()))
80 rel_file = lambda rel: input_api.os_path.join(root, rel)
81 cipd_manifests = set(rel_file(input_api.os_path.join(*x)) for x in (
82 ('cipd_manifest.txt',),
83 ('bootstrap', 'win', 'manifest.txt'),
84 ('bootstrap', 'win', 'manifest_bleeding_edge.txt'),
85
86 # Also generate a file for the cipd client itself.
87 ('cipd_client_version',),
88 ))
89 affected_manifests = input_api.AffectedFiles(
90 include_deletes=False,
91 file_filter=lambda x:
92 input_api.os_path.normpath(x.AbsoluteLocalPath()) in cipd_manifests)
93 for path in affected_manifests:
94 path = path.AbsoluteLocalPath()
95 if path.endswith('.txt'):
96 tests.append(input_api.canned_checks.CheckCIPDManifest(
97 input_api, output_api, path=path))
98 else:
99 pkg = 'infra/tools/cipd/${platform}'
100 ver = input_api.ReadFile(path)
101 tests.append(input_api.canned_checks.CheckCIPDManifest(
Vadim Shtayura09098852018-06-21 22:50:30 +0000102 input_api, output_api,
103 content=CIPD_CLIENT_ENSURE_FILE_TEMPLATE % (pkg, ver)))
Robert Iannucci7d47eb52017-12-06 12:18:18 -0800104
scottmg@chromium.org69ae86a2014-01-17 03:55:45 +0000105 results.extend(input_api.RunTests(tests))
maruel@chromium.org3c9f7ca2011-04-01 14:52:38 +0000106 return results
maruel@chromium.org2a74d372011-03-29 19:05:50 +0000107
108
maruel@chromium.orge94aedc2010-12-13 21:11:30 +0000109def CheckChangeOnUpload(input_api, output_api):
maruel@chromium.orgd52417c2011-09-02 20:13:17 +0000110 # Do not run integration tests on upload since they are way too slow.
111 tests_to_black_list = [
112 r'^checkout_test\.py$',
113 r'^gclient_smoketest\.py$',
114 r'^scm_unittest\.py$',
ilevy@chromium.org58565622013-03-17 23:18:37 +0000115 r'^subprocess2_test\.py$',
maruel@chromium.orgd52417c2011-09-02 20:13:17 +0000116 ]
117 return CommonChecks(input_api, output_api, tests_to_black_list)
maruel@chromium.orge94aedc2010-12-13 21:11:30 +0000118
119
maruel@chromium.orgce30ef72009-05-25 15:20:42 +0000120def CheckChangeOnCommit(input_api, output_api):
121 output = []
maruel@chromium.orgd52417c2011-09-02 20:13:17 +0000122 output.extend(CommonChecks(input_api, output_api, []))
maruel@chromium.orge94aedc2010-12-13 21:11:30 +0000123 output.extend(input_api.canned_checks.CheckDoNotSubmit(
124 input_api,
125 output_api))
maruel@chromium.org7b305e82009-05-19 18:24:20 +0000126 return output