Artem Titov | 739351d | 2018-05-11 12:21:36 +0200 | [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 | |
| 5 | ANDROID_WHITELISTED_LICENSES = [ |
| 6 | 'A(pple )?PSL 2(\.0)?', |
| 7 | 'Apache( Version)? 2(\.0)?', |
| 8 | '(New )?([23]-Clause )?BSD( [23]-Clause)?( with advertising clause)?', |
| 9 | 'L?GPL ?v?2(\.[01])?( or later)?', |
| 10 | 'MIT(/X11)?(-like)?', |
| 11 | 'MPL 1\.1 ?/ ?GPL 2(\.0)? ?/ ?LGPL 2\.1', |
| 12 | 'MPL 2(\.0)?', |
| 13 | 'Microsoft Limited Public License', |
| 14 | 'Microsoft Permissive License', |
| 15 | 'Public Domain', |
| 16 | 'Python', |
| 17 | 'SGI Free Software License B', |
| 18 | 'University of Illinois\/NCSA Open Source', |
| 19 | 'X11', |
| 20 | ] |
| 21 | |
| 22 | def LicenseIsCompatibleWithAndroid(input_api, license): |
| 23 | regex = '^(%s)$' % '|'.join(ANDROID_WHITELISTED_LICENSES) |
| 24 | tokens = \ |
| 25 | [x.strip() for x in input_api.re.split(' and |,', license) if len(x) > 0] |
| 26 | has_compatible_license = False |
| 27 | for token in tokens: |
| 28 | if input_api.re.match(regex, token, input_api.re.IGNORECASE): |
| 29 | has_compatible_license = True |
| 30 | break |
| 31 | return has_compatible_license |
| 32 | |
| 33 | def _CheckThirdPartyReadmesUpdated(input_api, output_api): |
| 34 | """ |
| 35 | Checks to make sure that README.chromium files are properly updated |
| 36 | when dependencies in third_party are modified. |
| 37 | """ |
| 38 | readmes = [] |
| 39 | files = [] |
| 40 | errors = [] |
| 41 | for f in input_api.AffectedFiles(): |
| 42 | local_path = f.LocalPath() |
| 43 | if input_api.os_path.dirname(local_path) == 'third_party': |
| 44 | continue |
| 45 | if (local_path.startswith('third_party' + input_api.os_path.sep) and |
| 46 | not local_path.startswith('third_party' + input_api.os_path.sep + |
| 47 | 'WebKit' + input_api.os_path.sep) and |
| 48 | not local_path.startswith('third_party' + input_api.os_path.sep + |
| 49 | 'blink' + input_api.os_path.sep) and |
| 50 | not local_path.startswith('third_party' + input_api.os_path.sep + |
| 51 | 'mojo' + input_api.os_path.sep) and |
| 52 | not local_path.startswith('third_party' + input_api.os_path.sep + |
Artem Titov | c34e381 | 2018-05-24 14:17:37 +0200 | [diff] [blame] | 53 | 'boringssl' + input_api.os_path.sep) and |
| 54 | not local_path.startswith('third_party' + input_api.os_path.sep + |
| 55 | 'closure_compiler' + input_api.os_path.sep + |
| 56 | 'externs' + input_api.os_path.sep) and |
| 57 | not local_path.startswith('third_party' + input_api.os_path.sep + |
| 58 | 'closure_compiler' + input_api.os_path.sep + |
| 59 | 'interfaces' + input_api.os_path.sep)): |
Artem Titov | 739351d | 2018-05-11 12:21:36 +0200 | [diff] [blame] | 60 | files.append(f) |
| 61 | if local_path.endswith("README.chromium"): |
| 62 | readmes.append(f) |
| 63 | if files and not readmes: |
| 64 | errors.append(output_api.PresubmitPromptWarning( |
| 65 | 'When updating or adding third party code the appropriate\n' |
| 66 | '\'README.chromium\' file should also be updated with the correct\n' |
| 67 | 'version and package information.', files)) |
| 68 | if not readmes: |
| 69 | return errors |
| 70 | |
| 71 | name_pattern = input_api.re.compile( |
| 72 | r'^Name: [a-zA-Z0-9_\-\. \(\)]+\r?$', |
| 73 | input_api.re.IGNORECASE | input_api.re.MULTILINE) |
| 74 | shortname_pattern = input_api.re.compile( |
| 75 | r'^Short Name: [a-zA-Z0-9_\-\.]+\r?$', |
| 76 | input_api.re.IGNORECASE | input_api.re.MULTILINE) |
| 77 | version_pattern = input_api.re.compile( |
| 78 | r'^Version: [a-zA-Z0-9_\-\.:]+\r?$', |
| 79 | input_api.re.IGNORECASE | input_api.re.MULTILINE) |
| 80 | release_pattern = input_api.re.compile( |
| 81 | r'^Security Critical: (yes|no)\r?$', |
| 82 | input_api.re.IGNORECASE | input_api.re.MULTILINE) |
| 83 | license_pattern = input_api.re.compile( |
| 84 | r'^License: (.+)\r?$', |
| 85 | input_api.re.IGNORECASE | input_api.re.MULTILINE) |
| 86 | license_android_compatible_pattern = input_api.re.compile( |
| 87 | r'^License Android Compatible: (yes|no)\r?$', |
| 88 | input_api.re.IGNORECASE | input_api.re.MULTILINE) |
| 89 | |
| 90 | for f in readmes: |
| 91 | if 'D' in f.Action(): |
| 92 | _IgnoreIfDeleting(input_api, output_api, f, errors) |
| 93 | continue |
| 94 | |
| 95 | contents = input_api.ReadFile(f) |
| 96 | if (not shortname_pattern.search(contents) |
| 97 | and not name_pattern.search(contents)): |
| 98 | errors.append(output_api.PresubmitError( |
| 99 | 'Third party README files should contain either a \'Short Name\' or\n' |
| 100 | 'a \'Name\' which is the name under which the package is\n' |
| 101 | 'distributed. Check README.chromium.template for details.', |
| 102 | [f])) |
| 103 | if not version_pattern.search(contents): |
| 104 | errors.append(output_api.PresubmitError( |
| 105 | 'Third party README files should contain a \'Version\' field.\n' |
| 106 | 'If the package is not versioned or the version is not known\n' |
| 107 | 'list the version as \'unknown\'.\n' |
| 108 | 'Check README.chromium.template for details.', |
| 109 | [f])) |
| 110 | if not release_pattern.search(contents): |
| 111 | errors.append(output_api.PresubmitError( |
| 112 | 'Third party README files should contain a \'Security Critical\'\n' |
| 113 | 'field. This field specifies whether the package is built with\n' |
| 114 | 'Chromium. Check README.chromium.template for details.', |
| 115 | [f])) |
| 116 | license_match = license_pattern.search(contents) |
| 117 | if not license_match: |
| 118 | errors.append(output_api.PresubmitError( |
| 119 | 'Third party README files should contain a \'License\' field.\n' |
| 120 | 'This field specifies the license used by the package. Check\n' |
| 121 | 'README.chromium.template for details.', |
| 122 | [f])) |
| 123 | elif not LicenseIsCompatibleWithAndroid(input_api, license_match.group(1)) \ |
| 124 | and not license_android_compatible_pattern.search(contents): |
| 125 | errors.append(output_api.PresubmitPromptWarning( |
| 126 | 'Cannot determine whether specified license is compatible with\n' + |
| 127 | 'the Android licensing requirements. Please check that the license\n' + |
| 128 | 'name is spelled according to third_party/PRESUBMIT.py. Please see\n' + |
| 129 | 'README.chromium.template for details.', |
| 130 | [f])) |
| 131 | return errors |
| 132 | |
| 133 | |
| 134 | def _IgnoreIfDeleting(input_api, output_api, affected_file, errors): |
| 135 | third_party_dir = input_api.os_path.dirname(affected_file.LocalPath()) |
| 136 | for f in input_api.AffectedFiles(): |
| 137 | if f.LocalPath().startswith(third_party_dir): |
| 138 | if 'D' not in f.Action(): |
| 139 | errors.append(output_api.PresubmitError( |
| 140 | 'Third party README should only be removed when the whole\n' |
| 141 | 'directory is being removed.\n', [f, affected_file])) |
| 142 | |
| 143 | |
| 144 | def CheckChangeOnUpload(input_api, output_api): |
| 145 | results = [] |
| 146 | results.extend(_CheckThirdPartyReadmesUpdated(input_api, output_api)) |
| 147 | return results |