1 # Copyright 2011 The Chromium Authors
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
9 PRESUBMIT_VERSION = '2.0.0'
11 ANDROID_ALLOWED_LICENSES = [
12 'A(pple )?PSL 2(\.0)?',
13 'Android Software Development Kit License',
14 'Apache( License)?,?( Version)? 2(\.0)?',
15 '(New )?([23]-Clause )?BSD( [23]-Clause)?( with advertising clause)?',
16 'GNU Lesser Public License',
17 'L?GPL ?v?2(\.[01])?( or later)?( with the classpath exception)?',
18 '(The )?MIT(/X11)?(-like)?( License)?',
19 'MPL 1\.1 ?/ ?GPL 2(\.0)? ?/ ?LGPL 2\.1',
21 'Microsoft Limited Public License',
22 'Microsoft Permissive License',
25 'SIL Open Font License, Version 1.1',
26 'SGI Free Software License B',
27 'Unicode, Inc. License',
28 'University of Illinois\/NCSA Open Source',
33 def LicenseIsCompatibleWithAndroid(input_api, license):
34 regex = '^(%s)$' % '|'.join(ANDROID_ALLOWED_LICENSES)
36 [x.strip() for x in input_api.re.split(' and |,', license) if len(x) > 0]
37 has_compatible_license = False
39 if input_api.re.match(regex, token, input_api.re.IGNORECASE):
40 has_compatible_license = True
42 return has_compatible_license
44 def CheckThirdPartyReadmesUpdated(input_api, output_api):
46 Checks to make sure that README.chromium files are properly updated
47 when dependencies in third_party are modified.
52 for f in input_api.AffectedFiles():
53 local_path = f.LocalPath()
54 if input_api.os_path.dirname(local_path) == 'third_party':
56 if (local_path.startswith('third_party' + input_api.os_path.sep) and
57 not local_path.startswith('third_party' + input_api.os_path.sep +
58 'blink' + input_api.os_path.sep) and
59 not local_path.startswith('third_party' + input_api.os_path.sep +
60 'boringssl' + input_api.os_path.sep) and
61 not local_path.startswith('third_party' + input_api.os_path.sep +
62 'closure_compiler' + input_api.os_path.sep +
63 'externs' + input_api.os_path.sep) and
64 not local_path.startswith('third_party' + input_api.os_path.sep +
65 'closure_compiler' + input_api.os_path.sep +
66 'interfaces' + 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) and
69 not local_path.startswith('third_party' + input_api.os_path.sep +
70 'ipcz' + input_api.os_path.sep) and
71 # TODO(danakj): We should look for the README.chromium file in
72 # third_party/rust/CRATE_NAME/vVERSION/.
73 not local_path.startswith('third_party' + input_api.os_path.sep +
74 'rust' + input_api.os_path.sep) and
75 not local_path.startswith('third_party' + input_api.os_path.sep +
76 'webxr_test_pages' + input_api.os_path.sep)):
78 if local_path.endswith("README.chromium"):
80 if files and not readmes:
81 errors.append(output_api.PresubmitPromptWarning(
82 'When updating or adding third party code the appropriate\n'
83 '\'README.chromium\' file should also be updated with the correct\n'
84 'version and package information.', files))
88 name_pattern = input_api.re.compile(
89 r'^Name: [a-zA-Z0-9_\-\. \(\)]+\r?$',
90 input_api.re.IGNORECASE | input_api.re.MULTILINE)
91 shortname_pattern = input_api.re.compile(
92 r'^Short Name: [a-zA-Z0-9_\-\.]+\r?$',
93 input_api.re.IGNORECASE | input_api.re.MULTILINE)
94 version_pattern = input_api.re.compile(
95 r'^Version: [a-zA-Z0-9_\-\+\.:/]+\r?$',
96 input_api.re.IGNORECASE | input_api.re.MULTILINE)
97 release_pattern = input_api.re.compile(
98 r'^Security Critical: (yes|no)\r?$',
99 input_api.re.IGNORECASE | input_api.re.MULTILINE)
100 license_pattern = input_api.re.compile(
101 r'^License: (.+)\r?$',
102 input_api.re.IGNORECASE | input_api.re.MULTILINE)
103 not_shipped_pattern = input_api.re.compile(
104 r'^License File: NOT_SHIPPED\r?$',
105 input_api.re.IGNORECASE | input_api.re.MULTILINE)
106 license_android_compatible_pattern = input_api.re.compile(
107 r'^License Android Compatible: (yes|no)\r?$',
108 input_api.re.IGNORECASE | input_api.re.MULTILINE)
111 if 'D' in f.Action():
112 _IgnoreIfDeleting(input_api, output_api, f, errors)
115 contents = input_api.ReadFile(f)
116 if (not shortname_pattern.search(contents)
117 and not name_pattern.search(contents)):
118 errors.append(output_api.PresubmitError(
119 'Third party README files should contain either a \'Short Name\' or\n'
120 'a \'Name\' which is the name under which the package is\n'
121 'distributed. Check README.chromium.template for details.',
123 if not version_pattern.search(contents):
124 errors.append(output_api.PresubmitError(
125 'Third party README files should contain a \'Version\' field.\n'
126 'If the package is not versioned or the version is not known\n'
127 'list the version as \'unknown\'.\n'
128 'Check README.chromium.template for details.',
130 if not release_pattern.search(contents):
131 errors.append(output_api.PresubmitError(
132 'Third party README files should contain a \'Security Critical\'\n'
133 'field. This field specifies whether the package is built with\n'
134 'Chromium. Check README.chromium.template for details.',
136 license_match = license_pattern.search(contents)
137 if not license_match:
138 errors.append(output_api.PresubmitError(
139 'Third party README files should contain a \'License\' field.\n'
140 'This field specifies the license used by the package. Check\n'
141 'README.chromium.template for details.',
143 not_shipped_match = not_shipped_pattern.search(contents)
144 android_compatible_match = (
145 license_android_compatible_pattern.search(contents))
146 if (not not_shipped_match and not android_compatible_match and
147 not LicenseIsCompatibleWithAndroid(input_api, license_match.group(1))):
148 errors.append(output_api.PresubmitPromptWarning(
149 'Cannot determine whether specified license is compatible with\n' +
150 'the Android licensing requirements. Please check that the license\n' +
151 'name is spelled according to third_party/PRESUBMIT.py. Please see\n' +
152 'README.chromium.template for details.',
157 def _IgnoreIfDeleting(input_api, output_api, affected_file, errors):
158 third_party_dir = input_api.os_path.dirname(affected_file.LocalPath()) + \
160 for f in input_api.AffectedFiles():
161 if f.LocalPath().startswith(third_party_dir):
162 if 'D' not in f.Action():
163 errors.append(output_api.PresubmitError(
164 'Third party README should only be removed when the whole\n'
165 'directory is being removed.\n', [f, affected_file]))