[email protected] | 99f1a48 | 2011-04-12 00:11:23 | [diff] [blame] | 1 | # 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 Harrington | b60e1aa | 2019-11-20 08:48:54 | [diff] [blame] | 5 | import os |
| 6 | |
[email protected] | fd00e49 | 2014-02-10 12:44:09 | [diff] [blame] | 7 | ANDROID_WHITELISTED_LICENSES = [ |
| 8 | 'A(pple )?PSL 2(\.0)?', |
Peter Wen | d340c5e | 2018-07-26 20:55:39 | [diff] [blame] | 9 | 'Android Software Development Kit License', |
Andrew Grieve | d1f9db4 | 2019-11-13 16:50:58 | [diff] [blame] | 10 | 'Apache( License)?,?( Version)? 2(\.0)?', |
[email protected] | fd00e49 | 2014-02-10 12:44:09 | [diff] [blame] | 11 | '(New )?([23]-Clause )?BSD( [23]-Clause)?( with advertising clause)?', |
Andrew Grieve | d1f9db4 | 2019-11-13 16:50:58 | [diff] [blame] | 12 | 'GNU Lesser Public License', |
Peter Wen | ee303ae | 2018-09-12 19:41:37 | [diff] [blame] | 13 | 'L?GPL ?v?2(\.[01])?( or later)?( with the classpath exception)?', |
Andrew Grieve | d1f9db4 | 2019-11-13 16:50:58 | [diff] [blame] | 14 | '(The )?MIT(/X11)?(-like)?( License)?', |
[email protected] | fd00e49 | 2014-02-10 12:44:09 | [diff] [blame] | 15 | '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 Wallez | 955059ff | 2018-10-21 10:47:39 | [diff] [blame] | 24 | 'Zlib', |
[email protected] | fd00e49 | 2014-02-10 12:44:09 | [diff] [blame] | 25 | ] |
| 26 | |
| 27 | def 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] | 99f1a48 | 2011-04-12 00:11:23 | [diff] [blame] | 38 | def _CheckThirdPartyReadmesUpdated(input_api, output_api): |
| 39 | """ |
| 40 | Checks to make sure that README.chromium files are properly updated |
davidben | 88ace3f | 2015-11-19 17:09:50 | [diff] [blame] | 41 | when dependencies in third_party are modified. |
[email protected] | 99f1a48 | 2011-04-12 00:11:23 | [diff] [blame] | 42 | """ |
| 43 | readmes = [] |
| 44 | files = [] |
| 45 | errors = [] |
| 46 | for f in input_api.AffectedFiles(): |
[email protected] | e5dd62f | 2013-07-31 16:50:28 | [diff] [blame] | 47 | local_path = f.LocalPath() |
| 48 | if input_api.os_path.dirname(local_path) == 'third_party': |
| 49 | continue |
Primiano Tucci | c2aa2ce2d | 2015-09-23 16:39:29 | [diff] [blame] | 50 | if (local_path.startswith('third_party' + input_api.os_path.sep) and |
| 51 | not local_path.startswith('third_party' + input_api.os_path.sep + |
Kent Tamura | e9b3a9ec | 2017-08-31 02:20:19 | [diff] [blame] | 52 | 'blink' + input_api.os_path.sep) and |
| 53 | not local_path.startswith('third_party' + input_api.os_path.sep + |
Dominic Mazzoni | b322841 | 2018-05-22 23:29:37 | [diff] [blame] | 54 | 'boringssl' + input_api.os_path.sep) and |
| 55 | not local_path.startswith('third_party' + input_api.os_path.sep + |
Dominic Mazzoni | b322841 | 2018-05-22 23:29:37 | [diff] [blame] | 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 + |
Alex Cooper | 26d47ce | 2019-04-25 19:51:44 | [diff] [blame] | 60 | 'interfaces' + input_api.os_path.sep) and |
| 61 | not local_path.startswith('third_party' + input_api.os_path.sep + |
Peter Kasting | 8ea44bb | 2020-07-11 20:36:01 | [diff] [blame] | 62 | 'feed_library' + input_api.os_path.sep) and |
Dan Harrington | 4bb75242 | 2019-11-21 19:30:38 | [diff] [blame] | 63 | not local_path.startswith('third_party' + input_api.os_path.sep + |
Peter Kasting | 8ea44bb | 2020-07-11 20:36:01 | [diff] [blame] | 64 | 'mojo' + input_api.os_path.sep) and |
| 65 | not local_path.startswith('third_party' + input_api.os_path.sep + |
| 66 | 'webxr_test_pages' + input_api.os_path.sep)): |
[email protected] | 99f1a48 | 2011-04-12 00:11:23 | [diff] [blame] | 67 | files.append(f) |
[email protected] | e5dd62f | 2013-07-31 16:50:28 | [diff] [blame] | 68 | if local_path.endswith("README.chromium"): |
[email protected] | 99f1a48 | 2011-04-12 00:11:23 | [diff] [blame] | 69 | readmes.append(f) |
| 70 | if files and not readmes: |
| 71 | errors.append(output_api.PresubmitPromptWarning( |
| 72 | 'When updating or adding third party code the appropriate\n' |
| 73 | '\'README.chromium\' file should also be updated with the correct\n' |
| 74 | 'version and package information.', files)) |
| 75 | if not readmes: |
| 76 | return errors |
| 77 | |
| 78 | name_pattern = input_api.re.compile( |
[email protected] | 31eac5b | 2012-08-01 15:50:29 | [diff] [blame] | 79 | r'^Name: [a-zA-Z0-9_\-\. \(\)]+\r?$', |
[email protected] | 99f1a48 | 2011-04-12 00:11:23 | [diff] [blame] | 80 | input_api.re.IGNORECASE | input_api.re.MULTILINE) |
| 81 | shortname_pattern = input_api.re.compile( |
| 82 | r'^Short Name: [a-zA-Z0-9_\-\.]+\r?$', |
| 83 | input_api.re.IGNORECASE | input_api.re.MULTILINE) |
| 84 | version_pattern = input_api.re.compile( |
Andrew Grieve | d1f9db4 | 2019-11-13 16:50:58 | [diff] [blame] | 85 | r'^Version: [a-zA-Z0-9_\-\+\.:]+\r?$', |
[email protected] | 99f1a48 | 2011-04-12 00:11:23 | [diff] [blame] | 86 | input_api.re.IGNORECASE | input_api.re.MULTILINE) |
| 87 | release_pattern = input_api.re.compile( |
[email protected] | fd00e49 | 2014-02-10 12:44:09 | [diff] [blame] | 88 | r'^Security Critical: (yes|no)\r?$', |
[email protected] | 31eac5b | 2012-08-01 15:50:29 | [diff] [blame] | 89 | input_api.re.IGNORECASE | input_api.re.MULTILINE) |
| 90 | license_pattern = input_api.re.compile( |
[email protected] | fd00e49 | 2014-02-10 12:44:09 | [diff] [blame] | 91 | r'^License: (.+)\r?$', |
| 92 | input_api.re.IGNORECASE | input_api.re.MULTILINE) |
Andrew Grieve | d1f9db4 | 2019-11-13 16:50:58 | [diff] [blame] | 93 | not_shipped_pattern = input_api.re.compile( |
| 94 | r'^License File: NOT_SHIPPED\r?$', |
| 95 | input_api.re.IGNORECASE | input_api.re.MULTILINE) |
[email protected] | fd00e49 | 2014-02-10 12:44:09 | [diff] [blame] | 96 | license_android_compatible_pattern = input_api.re.compile( |
| 97 | r'^License Android Compatible: (yes|no)\r?$', |
[email protected] | 99f1a48 | 2011-04-12 00:11:23 | [diff] [blame] | 98 | input_api.re.IGNORECASE | input_api.re.MULTILINE) |
| 99 | |
| 100 | for f in readmes: |
[email protected] | e5dd62f | 2013-07-31 16:50:28 | [diff] [blame] | 101 | if 'D' in f.Action(): |
| 102 | _IgnoreIfDeleting(input_api, output_api, f, errors) |
| 103 | continue |
| 104 | |
[email protected] | 99f1a48 | 2011-04-12 00:11:23 | [diff] [blame] | 105 | contents = input_api.ReadFile(f) |
| 106 | if (not shortname_pattern.search(contents) |
| 107 | and not name_pattern.search(contents)): |
| 108 | errors.append(output_api.PresubmitError( |
| 109 | 'Third party README files should contain either a \'Short Name\' or\n' |
| 110 | 'a \'Name\' which is the name under which the package is\n' |
| 111 | 'distributed. Check README.chromium.template for details.', |
| 112 | [f])) |
| 113 | if not version_pattern.search(contents): |
| 114 | errors.append(output_api.PresubmitError( |
| 115 | 'Third party README files should contain a \'Version\' field.\n' |
| 116 | 'If the package is not versioned or the version is not known\n' |
| 117 | 'list the version as \'unknown\'.\n' |
| 118 | 'Check README.chromium.template for details.', |
| 119 | [f])) |
| 120 | if not release_pattern.search(contents): |
| 121 | errors.append(output_api.PresubmitError( |
| 122 | 'Third party README files should contain a \'Security Critical\'\n' |
| 123 | 'field. This field specifies whether the package is built with\n' |
| 124 | 'Chromium. Check README.chromium.template for details.', |
| 125 | [f])) |
[email protected] | fd00e49 | 2014-02-10 12:44:09 | [diff] [blame] | 126 | license_match = license_pattern.search(contents) |
| 127 | if not license_match: |
[email protected] | 31eac5b | 2012-08-01 15:50:29 | [diff] [blame] | 128 | errors.append(output_api.PresubmitError( |
| 129 | 'Third party README files should contain a \'License\' field.\n' |
| 130 | 'This field specifies the license used by the package. Check\n' |
| 131 | 'README.chromium.template for details.', |
| 132 | [f])) |
Andrew Grieve | d1f9db4 | 2019-11-13 16:50:58 | [diff] [blame] | 133 | not_shipped_match = not_shipped_pattern.search(contents) |
| 134 | android_compatible_match = ( |
| 135 | license_android_compatible_pattern.search(contents)) |
Justin DeWitt | 09be9935 | 2020-07-07 18:18:19 | [diff] [blame] | 136 | if (not not_shipped_match and not android_compatible_match and |
Andrew Grieve | d1f9db4 | 2019-11-13 16:50:58 | [diff] [blame] | 137 | not LicenseIsCompatibleWithAndroid(input_api, license_match.group(1))): |
[email protected] | fd00e49 | 2014-02-10 12:44:09 | [diff] [blame] | 138 | errors.append(output_api.PresubmitPromptWarning( |
| 139 | 'Cannot determine whether specified license is compatible with\n' + |
| 140 | 'the Android licensing requirements. Please check that the license\n' + |
| 141 | 'name is spelled according to third_party/PRESUBMIT.py. Please see\n' + |
| 142 | 'README.chromium.template for details.', |
| 143 | [f])) |
[email protected] | 99f1a48 | 2011-04-12 00:11:23 | [diff] [blame] | 144 | return errors |
| 145 | |
| 146 | |
[email protected] | e5dd62f | 2013-07-31 16:50:28 | [diff] [blame] | 147 | def _IgnoreIfDeleting(input_api, output_api, affected_file, errors): |
Dan Harrington | b60e1aa | 2019-11-20 08:48:54 | [diff] [blame] | 148 | third_party_dir = input_api.os_path.dirname(affected_file.LocalPath()) + \ |
| 149 | os.path.sep |
[email protected] | e5dd62f | 2013-07-31 16:50:28 | [diff] [blame] | 150 | for f in input_api.AffectedFiles(): |
| 151 | if f.LocalPath().startswith(third_party_dir): |
| 152 | if 'D' not in f.Action(): |
| 153 | errors.append(output_api.PresubmitError( |
| 154 | 'Third party README should only be removed when the whole\n' |
| 155 | 'directory is being removed.\n', [f, affected_file])) |
| 156 | |
| 157 | |
[email protected] | 99f1a48 | 2011-04-12 00:11:23 | [diff] [blame] | 158 | def CheckChangeOnUpload(input_api, output_api): |
| 159 | results = [] |
| 160 | results.extend(_CheckThirdPartyReadmesUpdated(input_api, output_api)) |
| 161 | return results |