[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 | |
[email protected] | fd00e49 | 2014-02-10 12:44:09 | [diff] [blame] | 5 | ANDROID_WHITELISTED_LICENSES = [ |
| 6 | 'A(pple )?PSL 2(\.0)?', |
Peter Wen | d340c5e | 2018-07-26 20:55:39 | [diff] [blame] | 7 | 'Android Software Development Kit License', |
[email protected] | fd00e49 | 2014-02-10 12:44:09 | [diff] [blame] | 8 | 'Apache( Version)? 2(\.0)?', |
| 9 | '(New )?([23]-Clause )?BSD( [23]-Clause)?( with advertising clause)?', |
Peter Wen | ee303ae | 2018-09-12 19:41:37 | [diff] [blame^] | 10 | 'L?GPL ?v?2(\.[01])?( or later)?( with the classpath exception)?', |
[email protected] | fd00e49 | 2014-02-10 12:44:09 | [diff] [blame] | 11 | '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 | |
| 23 | def 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] | 99f1a48 | 2011-04-12 00:11:23 | [diff] [blame] | 34 | def _CheckThirdPartyReadmesUpdated(input_api, output_api): |
| 35 | """ |
| 36 | Checks to make sure that README.chromium files are properly updated |
davidben | 88ace3f | 2015-11-19 17:09:50 | [diff] [blame] | 37 | when dependencies in third_party are modified. |
[email protected] | 99f1a48 | 2011-04-12 00:11:23 | [diff] [blame] | 38 | """ |
| 39 | readmes = [] |
| 40 | files = [] |
| 41 | errors = [] |
| 42 | for f in input_api.AffectedFiles(): |
[email protected] | e5dd62f | 2013-07-31 16:50:28 | [diff] [blame] | 43 | local_path = f.LocalPath() |
| 44 | if input_api.os_path.dirname(local_path) == 'third_party': |
| 45 | continue |
Primiano Tucci | c2aa2ce2d | 2015-09-23 16:39:29 | [diff] [blame] | 46 | if (local_path.startswith('third_party' + input_api.os_path.sep) and |
| 47 | not local_path.startswith('third_party' + input_api.os_path.sep + |
davidben | 88ace3f | 2015-11-19 17:09:50 | [diff] [blame] | 48 | 'WebKit' + input_api.os_path.sep) and |
| 49 | not local_path.startswith('third_party' + input_api.os_path.sep + |
Kent Tamura | e9b3a9ec | 2017-08-31 02:20:19 | [diff] [blame] | 50 | 'blink' + input_api.os_path.sep) and |
| 51 | not local_path.startswith('third_party' + input_api.os_path.sep + |
danakj | b1423c5 | 2015-11-25 22:10:13 | [diff] [blame] | 52 | 'mojo' + 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 + |
| 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] | 99f1a48 | 2011-04-12 00:11:23 | [diff] [blame] | 61 | files.append(f) |
[email protected] | e5dd62f | 2013-07-31 16:50:28 | [diff] [blame] | 62 | if local_path.endswith("README.chromium"): |
[email protected] | 99f1a48 | 2011-04-12 00:11:23 | [diff] [blame] | 63 | 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] | 31eac5b | 2012-08-01 15:50:29 | [diff] [blame] | 73 | r'^Name: [a-zA-Z0-9_\-\. \(\)]+\r?$', |
[email protected] | 99f1a48 | 2011-04-12 00:11:23 | [diff] [blame] | 74 | 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] | 0fcbfe48 | 2011-04-29 17:52:39 | [diff] [blame] | 79 | r'^Version: [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 | release_pattern = input_api.re.compile( |
[email protected] | fd00e49 | 2014-02-10 12:44:09 | [diff] [blame] | 82 | r'^Security Critical: (yes|no)\r?$', |
[email protected] | 31eac5b | 2012-08-01 15:50:29 | [diff] [blame] | 83 | input_api.re.IGNORECASE | input_api.re.MULTILINE) |
| 84 | license_pattern = input_api.re.compile( |
[email protected] | fd00e49 | 2014-02-10 12:44:09 | [diff] [blame] | 85 | 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] | 99f1a48 | 2011-04-12 00:11:23 | [diff] [blame] | 89 | input_api.re.IGNORECASE | input_api.re.MULTILINE) |
| 90 | |
| 91 | for f in readmes: |
[email protected] | e5dd62f | 2013-07-31 16:50:28 | [diff] [blame] | 92 | if 'D' in f.Action(): |
| 93 | _IgnoreIfDeleting(input_api, output_api, f, errors) |
| 94 | continue |
| 95 | |
[email protected] | 99f1a48 | 2011-04-12 00:11:23 | [diff] [blame] | 96 | 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] | fd00e49 | 2014-02-10 12:44:09 | [diff] [blame] | 117 | license_match = license_pattern.search(contents) |
| 118 | if not license_match: |
[email protected] | 31eac5b | 2012-08-01 15:50:29 | [diff] [blame] | 119 | 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] | fd00e49 | 2014-02-10 12:44:09 | [diff] [blame] | 124 | 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] | 99f1a48 | 2011-04-12 00:11:23 | [diff] [blame] | 132 | return errors |
| 133 | |
| 134 | |
[email protected] | e5dd62f | 2013-07-31 16:50:28 | [diff] [blame] | 135 | def _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] | 99f1a48 | 2011-04-12 00:11:23 | [diff] [blame] | 145 | def CheckChangeOnUpload(input_api, output_api): |
| 146 | results = [] |
| 147 | results.extend(_CheckThirdPartyReadmesUpdated(input_api, output_api)) |
| 148 | return results |