blob: 0cf0e4b4512dc4e06e8d6acf09281a9f4049526a [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
Dan Harringtonb60e1aa2019-11-20 08:48:545import os
6
[email protected]fd00e492014-02-10 12:44:097ANDROID_WHITELISTED_LICENSES = [
8 'A(pple )?PSL 2(\.0)?',
Peter Wend340c5e2018-07-26 20:55:399 'Android Software Development Kit License',
Andrew Grieved1f9db42019-11-13 16:50:5810 'Apache( License)?,?( Version)? 2(\.0)?',
[email protected]fd00e492014-02-10 12:44:0911 '(New )?([23]-Clause )?BSD( [23]-Clause)?( with advertising clause)?',
Andrew Grieved1f9db42019-11-13 16:50:5812 'GNU Lesser Public License',
Peter Wenee303ae2018-09-12 19:41:3713 'L?GPL ?v?2(\.[01])?( or later)?( with the classpath exception)?',
Andrew Grieved1f9db42019-11-13 16:50:5814 '(The )?MIT(/X11)?(-like)?( License)?',
[email protected]fd00e492014-02-10 12:44:0915 'MPL 1\.1 ?/ ?GPL 2(\.0)? ?/ ?LGPL 2\.1',
16 'MPL 2(\.0)?',
17 'Microsoft Limited Public License',
18 'Microsoft Permissive License',
19 'Public Domain',
20 'Python',
21 'SGI Free Software License B',
22 'University of Illinois\/NCSA Open Source',
23 'X11',
Corentin Wallez955059ff2018-10-21 10:47:3924 'Zlib',
[email protected]fd00e492014-02-10 12:44:0925]
26
27def LicenseIsCompatibleWithAndroid(input_api, license):
28 regex = '^(%s)$' % '|'.join(ANDROID_WHITELISTED_LICENSES)
29 tokens = \
30 [x.strip() for x in input_api.re.split(' and |,', license) if len(x) > 0]
31 has_compatible_license = False
32 for token in tokens:
33 if input_api.re.match(regex, token, input_api.re.IGNORECASE):
34 has_compatible_license = True
35 break
36 return has_compatible_license
37
[email protected]99f1a482011-04-12 00:11:2338def _CheckThirdPartyReadmesUpdated(input_api, output_api):
39 """
40 Checks to make sure that README.chromium files are properly updated
davidben88ace3f2015-11-19 17:09:5041 when dependencies in third_party are modified.
[email protected]99f1a482011-04-12 00:11:2342 """
43 readmes = []
44 files = []
45 errors = []
46 for f in input_api.AffectedFiles():
[email protected]e5dd62f2013-07-31 16:50:2847 local_path = f.LocalPath()
48 if input_api.os_path.dirname(local_path) == 'third_party':
49 continue
Primiano Tuccic2aa2ce2d2015-09-23 16:39:2950 if (local_path.startswith('third_party' + input_api.os_path.sep) and
51 not local_path.startswith('third_party' + input_api.os_path.sep +
davidben88ace3f2015-11-19 17:09:5052 'WebKit' + input_api.os_path.sep) and
53 not local_path.startswith('third_party' + input_api.os_path.sep +
Kent Tamurae9b3a9ec2017-08-31 02:20:1954 'blink' + input_api.os_path.sep) and
55 not local_path.startswith('third_party' + input_api.os_path.sep +
danakjb1423c52015-11-25 22:10:1356 'mojo' + input_api.os_path.sep) and
57 not local_path.startswith('third_party' + input_api.os_path.sep +
Dominic Mazzonib3228412018-05-22 23:29:3758 'boringssl' + input_api.os_path.sep) and
59 not local_path.startswith('third_party' + input_api.os_path.sep +
60 'closure_compiler' + input_api.os_path.sep +
61 'externs' + input_api.os_path.sep) and
62 not local_path.startswith('third_party' + input_api.os_path.sep +
63 'closure_compiler' + input_api.os_path.sep +
Alex Cooper26d47ce2019-04-25 19:51:4464 'interfaces' + input_api.os_path.sep) and
65 not local_path.startswith('third_party' + input_api.os_path.sep +
Dan Harrington4bb752422019-11-21 19:30:3866 'webxr_test_pages' + input_api.os_path.sep) and
67 not local_path.startswith('third_party' + input_api.os_path.sep +
68 'feed_library' + input_api.os_path.sep)):
[email protected]99f1a482011-04-12 00:11:2369 files.append(f)
[email protected]e5dd62f2013-07-31 16:50:2870 if local_path.endswith("README.chromium"):
[email protected]99f1a482011-04-12 00:11:2371 readmes.append(f)
72 if files and not readmes:
73 errors.append(output_api.PresubmitPromptWarning(
74 'When updating or adding third party code the appropriate\n'
75 '\'README.chromium\' file should also be updated with the correct\n'
76 'version and package information.', files))
77 if not readmes:
78 return errors
79
80 name_pattern = input_api.re.compile(
[email protected]31eac5b2012-08-01 15:50:2981 r'^Name: [a-zA-Z0-9_\-\. \(\)]+\r?$',
[email protected]99f1a482011-04-12 00:11:2382 input_api.re.IGNORECASE | input_api.re.MULTILINE)
83 shortname_pattern = input_api.re.compile(
84 r'^Short Name: [a-zA-Z0-9_\-\.]+\r?$',
85 input_api.re.IGNORECASE | input_api.re.MULTILINE)
86 version_pattern = input_api.re.compile(
Andrew Grieved1f9db42019-11-13 16:50:5887 r'^Version: [a-zA-Z0-9_\-\+\.:]+\r?$',
[email protected]99f1a482011-04-12 00:11:2388 input_api.re.IGNORECASE | input_api.re.MULTILINE)
89 release_pattern = input_api.re.compile(
[email protected]fd00e492014-02-10 12:44:0990 r'^Security Critical: (yes|no)\r?$',
[email protected]31eac5b2012-08-01 15:50:2991 input_api.re.IGNORECASE | input_api.re.MULTILINE)
92 license_pattern = input_api.re.compile(
[email protected]fd00e492014-02-10 12:44:0993 r'^License: (.+)\r?$',
94 input_api.re.IGNORECASE | input_api.re.MULTILINE)
Andrew Grieved1f9db42019-11-13 16:50:5895 not_shipped_pattern = input_api.re.compile(
96 r'^License File: NOT_SHIPPED\r?$',
97 input_api.re.IGNORECASE | input_api.re.MULTILINE)
[email protected]fd00e492014-02-10 12:44:0998 license_android_compatible_pattern = input_api.re.compile(
99 r'^License Android Compatible: (yes|no)\r?$',
[email protected]99f1a482011-04-12 00:11:23100 input_api.re.IGNORECASE | input_api.re.MULTILINE)
101
102 for f in readmes:
[email protected]e5dd62f2013-07-31 16:50:28103 if 'D' in f.Action():
104 _IgnoreIfDeleting(input_api, output_api, f, errors)
105 continue
106
[email protected]99f1a482011-04-12 00:11:23107 contents = input_api.ReadFile(f)
108 if (not shortname_pattern.search(contents)
109 and not name_pattern.search(contents)):
110 errors.append(output_api.PresubmitError(
111 'Third party README files should contain either a \'Short Name\' or\n'
112 'a \'Name\' which is the name under which the package is\n'
113 'distributed. Check README.chromium.template for details.',
114 [f]))
115 if not version_pattern.search(contents):
116 errors.append(output_api.PresubmitError(
117 'Third party README files should contain a \'Version\' field.\n'
118 'If the package is not versioned or the version is not known\n'
119 'list the version as \'unknown\'.\n'
120 'Check README.chromium.template for details.',
121 [f]))
122 if not release_pattern.search(contents):
123 errors.append(output_api.PresubmitError(
124 'Third party README files should contain a \'Security Critical\'\n'
125 'field. This field specifies whether the package is built with\n'
126 'Chromium. Check README.chromium.template for details.',
127 [f]))
[email protected]fd00e492014-02-10 12:44:09128 license_match = license_pattern.search(contents)
129 if not license_match:
[email protected]31eac5b2012-08-01 15:50:29130 errors.append(output_api.PresubmitError(
131 'Third party README files should contain a \'License\' field.\n'
132 'This field specifies the license used by the package. Check\n'
133 'README.chromium.template for details.',
134 [f]))
Andrew Grieved1f9db42019-11-13 16:50:58135 not_shipped_match = not_shipped_pattern.search(contents)
136 android_compatible_match = (
137 license_android_compatible_pattern.search(contents))
138 if (not not_shipped_pattern and not android_compatible_match and
139 not LicenseIsCompatibleWithAndroid(input_api, license_match.group(1))):
[email protected]fd00e492014-02-10 12:44:09140 errors.append(output_api.PresubmitPromptWarning(
141 'Cannot determine whether specified license is compatible with\n' +
142 'the Android licensing requirements. Please check that the license\n' +
143 'name is spelled according to third_party/PRESUBMIT.py. Please see\n' +
144 'README.chromium.template for details.',
145 [f]))
[email protected]99f1a482011-04-12 00:11:23146 return errors
147
148
[email protected]e5dd62f2013-07-31 16:50:28149def _IgnoreIfDeleting(input_api, output_api, affected_file, errors):
Dan Harringtonb60e1aa2019-11-20 08:48:54150 third_party_dir = input_api.os_path.dirname(affected_file.LocalPath()) + \
151 os.path.sep
[email protected]e5dd62f2013-07-31 16:50:28152 for f in input_api.AffectedFiles():
153 if f.LocalPath().startswith(third_party_dir):
154 if 'D' not in f.Action():
155 errors.append(output_api.PresubmitError(
156 'Third party README should only be removed when the whole\n'
157 'directory is being removed.\n', [f, affected_file]))
158
159
[email protected]99f1a482011-04-12 00:11:23160def CheckChangeOnUpload(input_api, output_api):
161 results = []
162 results.extend(_CheckThirdPartyReadmesUpdated(input_api, output_api))
163 return results