blob: f3db829147d9cde9a672fd918dea9a98d68ca1a5 [file] [log] [blame]
[email protected]99f1a482011-04-12 00:11:231# Copyright (c) 2011 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
[email protected]fd00e492014-02-10 12:44:095ANDROID_WHITELISTED_LICENSES = [
6 'A(pple )?PSL 2(\.0)?',
Peter Wend340c5e2018-07-26 20:55:397 'Android Software Development Kit License',
[email protected]fd00e492014-02-10 12:44:098 'Apache( Version)? 2(\.0)?',
9 '(New )?([23]-Clause )?BSD( [23]-Clause)?( with advertising clause)?',
Peter Wenee303ae2018-09-12 19:41:3710 'L?GPL ?v?2(\.[01])?( or later)?( with the classpath exception)?',
[email protected]fd00e492014-02-10 12:44:0911 'MIT(/X11)?(-like)?',
12 'MPL 1\.1 ?/ ?GPL 2(\.0)? ?/ ?LGPL 2\.1',
13 'MPL 2(\.0)?',
14 'Microsoft Limited Public License',
15 'Microsoft Permissive License',
16 'Public Domain',
17 'Python',
18 'SGI Free Software License B',
19 'University of Illinois\/NCSA Open Source',
20 'X11',
21]
22
23def LicenseIsCompatibleWithAndroid(input_api, license):
24 regex = '^(%s)$' % '|'.join(ANDROID_WHITELISTED_LICENSES)
25 tokens = \
26 [x.strip() for x in input_api.re.split(' and |,', license) if len(x) > 0]
27 has_compatible_license = False
28 for token in tokens:
29 if input_api.re.match(regex, token, input_api.re.IGNORECASE):
30 has_compatible_license = True
31 break
32 return has_compatible_license
33
[email protected]99f1a482011-04-12 00:11:2334def _CheckThirdPartyReadmesUpdated(input_api, output_api):
35 """
36 Checks to make sure that README.chromium files are properly updated
davidben88ace3f2015-11-19 17:09:5037 when dependencies in third_party are modified.
[email protected]99f1a482011-04-12 00:11:2338 """
39 readmes = []
40 files = []
41 errors = []
42 for f in input_api.AffectedFiles():
[email protected]e5dd62f2013-07-31 16:50:2843 local_path = f.LocalPath()
44 if input_api.os_path.dirname(local_path) == 'third_party':
45 continue
Primiano Tuccic2aa2ce2d2015-09-23 16:39:2946 if (local_path.startswith('third_party' + input_api.os_path.sep) and
47 not local_path.startswith('third_party' + input_api.os_path.sep +
davidben88ace3f2015-11-19 17:09:5048 'WebKit' + input_api.os_path.sep) and
49 not local_path.startswith('third_party' + input_api.os_path.sep +
Kent Tamurae9b3a9ec2017-08-31 02:20:1950 'blink' + input_api.os_path.sep) and
51 not local_path.startswith('third_party' + input_api.os_path.sep +
danakjb1423c52015-11-25 22:10:1352 'mojo' + input_api.os_path.sep) and
53 not local_path.startswith('third_party' + input_api.os_path.sep +
Dominic Mazzonib3228412018-05-22 23:29:3754 'boringssl' + input_api.os_path.sep) and
55 not local_path.startswith('third_party' + input_api.os_path.sep +
56 'closure_compiler' + input_api.os_path.sep +
57 'externs' + input_api.os_path.sep) and
58 not local_path.startswith('third_party' + input_api.os_path.sep +
59 'closure_compiler' + input_api.os_path.sep +
60 'interfaces' + input_api.os_path.sep)):
[email protected]99f1a482011-04-12 00:11:2361 files.append(f)
[email protected]e5dd62f2013-07-31 16:50:2862 if local_path.endswith("README.chromium"):
[email protected]99f1a482011-04-12 00:11:2363 readmes.append(f)
64 if files and not readmes:
65 errors.append(output_api.PresubmitPromptWarning(
66 'When updating or adding third party code the appropriate\n'
67 '\'README.chromium\' file should also be updated with the correct\n'
68 'version and package information.', files))
69 if not readmes:
70 return errors
71
72 name_pattern = input_api.re.compile(
[email protected]31eac5b2012-08-01 15:50:2973 r'^Name: [a-zA-Z0-9_\-\. \(\)]+\r?$',
[email protected]99f1a482011-04-12 00:11:2374 input_api.re.IGNORECASE | input_api.re.MULTILINE)
75 shortname_pattern = input_api.re.compile(
76 r'^Short Name: [a-zA-Z0-9_\-\.]+\r?$',
77 input_api.re.IGNORECASE | input_api.re.MULTILINE)
78 version_pattern = input_api.re.compile(
[email protected]0fcbfe482011-04-29 17:52:3979 r'^Version: [a-zA-Z0-9_\-\.:]+\r?$',
[email protected]99f1a482011-04-12 00:11:2380 input_api.re.IGNORECASE | input_api.re.MULTILINE)
81 release_pattern = input_api.re.compile(
[email protected]fd00e492014-02-10 12:44:0982 r'^Security Critical: (yes|no)\r?$',
[email protected]31eac5b2012-08-01 15:50:2983 input_api.re.IGNORECASE | input_api.re.MULTILINE)
84 license_pattern = input_api.re.compile(
[email protected]fd00e492014-02-10 12:44:0985 r'^License: (.+)\r?$',
86 input_api.re.IGNORECASE | input_api.re.MULTILINE)
87 license_android_compatible_pattern = input_api.re.compile(
88 r'^License Android Compatible: (yes|no)\r?$',
[email protected]99f1a482011-04-12 00:11:2389 input_api.re.IGNORECASE | input_api.re.MULTILINE)
90
91 for f in readmes:
[email protected]e5dd62f2013-07-31 16:50:2892 if 'D' in f.Action():
93 _IgnoreIfDeleting(input_api, output_api, f, errors)
94 continue
95
[email protected]99f1a482011-04-12 00:11:2396 contents = input_api.ReadFile(f)
97 if (not shortname_pattern.search(contents)
98 and not name_pattern.search(contents)):
99 errors.append(output_api.PresubmitError(
100 'Third party README files should contain either a \'Short Name\' or\n'
101 'a \'Name\' which is the name under which the package is\n'
102 'distributed. Check README.chromium.template for details.',
103 [f]))
104 if not version_pattern.search(contents):
105 errors.append(output_api.PresubmitError(
106 'Third party README files should contain a \'Version\' field.\n'
107 'If the package is not versioned or the version is not known\n'
108 'list the version as \'unknown\'.\n'
109 'Check README.chromium.template for details.',
110 [f]))
111 if not release_pattern.search(contents):
112 errors.append(output_api.PresubmitError(
113 'Third party README files should contain a \'Security Critical\'\n'
114 'field. This field specifies whether the package is built with\n'
115 'Chromium. Check README.chromium.template for details.',
116 [f]))
[email protected]fd00e492014-02-10 12:44:09117 license_match = license_pattern.search(contents)
118 if not license_match:
[email protected]31eac5b2012-08-01 15:50:29119 errors.append(output_api.PresubmitError(
120 'Third party README files should contain a \'License\' field.\n'
121 'This field specifies the license used by the package. Check\n'
122 'README.chromium.template for details.',
123 [f]))
[email protected]fd00e492014-02-10 12:44:09124 elif not LicenseIsCompatibleWithAndroid(input_api, license_match.group(1)) \
125 and not license_android_compatible_pattern.search(contents):
126 errors.append(output_api.PresubmitPromptWarning(
127 'Cannot determine whether specified license is compatible with\n' +
128 'the Android licensing requirements. Please check that the license\n' +
129 'name is spelled according to third_party/PRESUBMIT.py. Please see\n' +
130 'README.chromium.template for details.',
131 [f]))
[email protected]99f1a482011-04-12 00:11:23132 return errors
133
134
[email protected]e5dd62f2013-07-31 16:50:28135def _IgnoreIfDeleting(input_api, output_api, affected_file, errors):
136 third_party_dir = input_api.os_path.dirname(affected_file.LocalPath())
137 for f in input_api.AffectedFiles():
138 if f.LocalPath().startswith(third_party_dir):
139 if 'D' not in f.Action():
140 errors.append(output_api.PresubmitError(
141 'Third party README should only be removed when the whole\n'
142 'directory is being removed.\n', [f, affected_file]))
143
144
[email protected]99f1a482011-04-12 00:11:23145def CheckChangeOnUpload(input_api, output_api):
146 results = []
147 results.extend(_CheckThirdPartyReadmesUpdated(input_api, output_api))
148 return results