| Chih-Hung Hsieh | 3d24aed | 2020-10-05 15:29:11 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # Copyright (C) 2020 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | """Add files to a Rust package for third party review.""" |
| 17 | |
| Thiébaud Weksteen | 8da4911 | 2021-02-19 11:59:49 +0100 | [diff] [blame] | 18 | import collections |
| Chih-Hung Hsieh | 3d24aed | 2020-10-05 15:29:11 -0700 | [diff] [blame] | 19 | import datetime |
| Thiébaud Weksteen | 8da4911 | 2021-02-19 11:59:49 +0100 | [diff] [blame] | 20 | import enum |
| Chih-Hung Hsieh | 03f14e4 | 2020-10-19 18:38:30 -0700 | [diff] [blame] | 21 | import glob |
| Chih-Hung Hsieh | 3d24aed | 2020-10-05 15:29:11 -0700 | [diff] [blame] | 22 | import json |
| 23 | import os |
| 24 | import pathlib |
| 25 | import re |
| 26 | |
| 27 | # patterns to match keys in Cargo.toml |
| 28 | NAME_PATTERN = r"^name *= *\"(.+)\"" |
| 29 | NAME_MATCHER = re.compile(NAME_PATTERN) |
| 30 | VERSION_PATTERN = r"^version *= *\"(.+)\"" |
| 31 | VERSION_MATCHER = re.compile(VERSION_PATTERN) |
| 32 | DESCRIPTION_PATTERN = r"^description *= *(\".+\")" |
| 33 | DESCRIPTION_MATCHER = re.compile(DESCRIPTION_PATTERN) |
| 34 | # NOTE: This description one-liner pattern fails to match |
| 35 | # multi-line descriptions in some Rust crates, e.g. shlex. |
| Chih-Hung Hsieh | 03f14e4 | 2020-10-19 18:38:30 -0700 | [diff] [blame] | 36 | LICENSE_PATTERN = r"^license *= *\"(.+)\"" |
| 37 | LICENSE_MATCHER = re.compile(LICENSE_PATTERN) |
| Chih-Hung Hsieh | 3d24aed | 2020-10-05 15:29:11 -0700 | [diff] [blame] | 38 | |
| 39 | # patterns to match year/month/day in METADATA |
| 40 | YMD_PATTERN = r"^ +(year|month|day): (.+)$" |
| 41 | YMD_MATCHER = re.compile(YMD_PATTERN) |
| 42 | YMD_LINE_PATTERN = r"^.* year: *([^ ]+) +month: *([^ ]+) +day: *([^ ]+).*$" |
| 43 | YMD_LINE_MATCHER = re.compile(YMD_LINE_PATTERN) |
| 44 | |
| Matt Schulte | 38d199e | 2023-12-20 10:05:57 -0800 | [diff] [blame] | 45 | # patterns to match different licence types in LICENSE* |
| Chih-Hung Hsieh | 3d24aed | 2020-10-05 15:29:11 -0700 | [diff] [blame] | 46 | APACHE_PATTERN = r"^.*Apache License.*$" |
| 47 | APACHE_MATCHER = re.compile(APACHE_PATTERN) |
| 48 | MIT_PATTERN = r"^.*MIT License.*$" |
| 49 | MIT_MATCHER = re.compile(MIT_PATTERN) |
| 50 | BSD_PATTERN = r"^.*BSD .*License.*$" |
| 51 | BSD_MATCHER = re.compile(BSD_PATTERN) |
| Matt Schulte | 055ccb3 | 2023-10-30 14:07:27 -0700 | [diff] [blame] | 52 | MPL_PATTERN = r"^.Mozilla Public License.*$" |
| 53 | MPL_MATCHER = re.compile(MPL_PATTERN) |
| Matt Schulte | b4fa3db | 2023-12-21 08:06:49 -0800 | [diff] [blame] | 54 | UNLICENSE_PATTERN = r"^.*unlicense\.org.*$" |
| 55 | UNLICENSE_MATCHER = re.compile(UNLICENSE_PATTERN) |
| Matt Schulte | 38d199e | 2023-12-20 10:05:57 -0800 | [diff] [blame] | 56 | ZERO_BSD_PATTERN = r"^.*Zero-Clause BSD.*$" |
| 57 | ZERO_BSD_MATCHER = re.compile(ZERO_BSD_PATTERN) |
| Thiébaud Weksteen | 8da4911 | 2021-02-19 11:59:49 +0100 | [diff] [blame] | 58 | MULTI_LICENSE_COMMENT = ("# Dual-licensed, using the least restrictive " |
| 59 | "per go/thirdpartylicenses#same.\n ") |
| Chih-Hung Hsieh | 3d24aed | 2020-10-05 15:29:11 -0700 | [diff] [blame] | 60 | |
| 61 | # default owners added to OWNERS |
| Stephen Hines | ce488a7 | 2023-10-19 00:34:53 -0700 | [diff] [blame] | 62 | DEFAULT_OWNERS = "include platform/prebuilts/rust:main:/OWNERS\n" |
| Chih-Hung Hsieh | 3d24aed | 2020-10-05 15:29:11 -0700 | [diff] [blame] | 63 | |
| 64 | # See b/159487435 Official policy for rust imports METADATA URLs. |
| 65 | # "license_type: NOTICE" might be optional, |
| 66 | # but it is already used in most rust crate METADATA. |
| 67 | # This line format should match the output of external_updater. |
| Thiébaud Weksteen | 8da4911 | 2021-02-19 11:59:49 +0100 | [diff] [blame] | 68 | METADATA_CONTENT = """name: "{name}" |
| 69 | description: {description} |
| Chih-Hung Hsieh | 3d24aed | 2020-10-05 15:29:11 -0700 | [diff] [blame] | 70 | third_party {{ |
| Jeongik Cha | 4e8edb4 | 2023-08-29 11:26:17 +0900 | [diff] [blame] | 71 | identifier {{ |
| 72 | type: "crates.io" |
| Thiébaud Weksteen | 8da4911 | 2021-02-19 11:59:49 +0100 | [diff] [blame] | 73 | value: "https://crates.io/crates/{name}" |
| Chih-Hung Hsieh | 3d24aed | 2020-10-05 15:29:11 -0700 | [diff] [blame] | 74 | }} |
| Jeongik Cha | 4e8edb4 | 2023-08-29 11:26:17 +0900 | [diff] [blame] | 75 | identifier {{ |
| 76 | type: "Archive" |
| Thiébaud Weksteen | 8da4911 | 2021-02-19 11:59:49 +0100 | [diff] [blame] | 77 | value: "https://static.crates.io/crates/{name}/{name}-{version}.crate" |
| Chih-Hung Hsieh | 3d24aed | 2020-10-05 15:29:11 -0700 | [diff] [blame] | 78 | }} |
| Thiébaud Weksteen | 8da4911 | 2021-02-19 11:59:49 +0100 | [diff] [blame] | 79 | version: "{version}" |
| Matt Schulte | 055ccb3 | 2023-10-30 14:07:27 -0700 | [diff] [blame] | 80 | {license_comment}license_type: {license_type} |
| Chih-Hung Hsieh | 3d24aed | 2020-10-05 15:29:11 -0700 | [diff] [blame] | 81 | last_upgrade_date {{ |
| Thiébaud Weksteen | 8da4911 | 2021-02-19 11:59:49 +0100 | [diff] [blame] | 82 | year: {year} |
| 83 | month: {month} |
| 84 | day: {day} |
| Chih-Hung Hsieh | 3d24aed | 2020-10-05 15:29:11 -0700 | [diff] [blame] | 85 | }} |
| 86 | }} |
| 87 | """ |
| 88 | |
| 89 | |
| 90 | def get_metadata_date(): |
| 91 | """Return last_upgrade_date in METADATA or today.""" |
| 92 | # When applied to existing directories to normalize METADATA, |
| 93 | # we don't want to change the last_upgrade_date. |
| 94 | year, month, day = "", "", "" |
| 95 | if os.path.exists("METADATA"): |
| 96 | with open("METADATA", "r") as inf: |
| 97 | for line in inf: |
| 98 | match = YMD_MATCHER.match(line) |
| 99 | if match: |
| 100 | if match.group(1) == "year": |
| 101 | year = match.group(2) |
| 102 | elif match.group(1) == "month": |
| 103 | month = match.group(2) |
| 104 | elif match.group(1) == "day": |
| 105 | day = match.group(2) |
| 106 | else: |
| 107 | match = YMD_LINE_MATCHER.match(line) |
| 108 | if match: |
| 109 | year, month, day = match.group(1), match.group(2), match.group(3) |
| 110 | if year and month and day: |
| 111 | print("### Reuse date in METADATA:", year, month, day) |
| 112 | return int(year), int(month), int(day) |
| 113 | today = datetime.date.today() |
| 114 | return today.year, today.month, today.day |
| 115 | |
| 116 | |
| Matt Schulte | 055ccb3 | 2023-10-30 14:07:27 -0700 | [diff] [blame] | 117 | def add_metadata(name, version, description, license_group, multi_license): |
| Chih-Hung Hsieh | 3d24aed | 2020-10-05 15:29:11 -0700 | [diff] [blame] | 118 | """Update or add METADATA file.""" |
| 119 | if os.path.exists("METADATA"): |
| 120 | print("### Updating METADATA") |
| 121 | else: |
| 122 | print("### Adding METADATA") |
| 123 | year, month, day = get_metadata_date() |
| Thiébaud Weksteen | 8da4911 | 2021-02-19 11:59:49 +0100 | [diff] [blame] | 124 | license_comment = "" |
| 125 | if multi_license: |
| 126 | license_comment = MULTI_LICENSE_COMMENT |
| Chih-Hung Hsieh | 3d24aed | 2020-10-05 15:29:11 -0700 | [diff] [blame] | 127 | with open("METADATA", "w") as outf: |
| 128 | outf.write(METADATA_CONTENT.format( |
| Thiébaud Weksteen | 8da4911 | 2021-02-19 11:59:49 +0100 | [diff] [blame] | 129 | name=name, description=description, version=version, |
| Matt Schulte | 055ccb3 | 2023-10-30 14:07:27 -0700 | [diff] [blame] | 130 | license_comment=license_comment, license_type=license_group, year=year, month=month, day=day)) |
| Chih-Hung Hsieh | 3d24aed | 2020-10-05 15:29:11 -0700 | [diff] [blame] | 131 | |
| 132 | |
| 133 | def grep_license_keyword(license_file): |
| 134 | """Find familiar patterns in a file and return the type.""" |
| 135 | with open(license_file, "r") as input_file: |
| 136 | for line in input_file: |
| 137 | if APACHE_MATCHER.match(line): |
| Matt Schulte | 055ccb3 | 2023-10-30 14:07:27 -0700 | [diff] [blame] | 138 | return License(LicenseType.APACHE2, LicenseGroup.NOTICE, license_file) |
| Chih-Hung Hsieh | 3d24aed | 2020-10-05 15:29:11 -0700 | [diff] [blame] | 139 | if MIT_MATCHER.match(line): |
| Matt Schulte | 055ccb3 | 2023-10-30 14:07:27 -0700 | [diff] [blame] | 140 | return License(LicenseType.MIT, LicenseGroup.NOTICE, license_file) |
| Chih-Hung Hsieh | 3d24aed | 2020-10-05 15:29:11 -0700 | [diff] [blame] | 141 | if BSD_MATCHER.match(line): |
| Matt Schulte | 055ccb3 | 2023-10-30 14:07:27 -0700 | [diff] [blame] | 142 | return License(LicenseType.BSD_LIKE, LicenseGroup.NOTICE, license_file) |
| Matt Schulte | 362d7f4 | 2023-12-20 07:54:03 -0800 | [diff] [blame] | 143 | if MPL_MATCHER.match(line): |
| Matt Schulte | 055ccb3 | 2023-10-30 14:07:27 -0700 | [diff] [blame] | 144 | return License(LicenseType.MPL, LicenseGroup.RECIPROCAL, license_file) |
| Matt Schulte | b4fa3db | 2023-12-21 08:06:49 -0800 | [diff] [blame] | 145 | if UNLICENSE_MATCHER.match(line): |
| 146 | return License(LicenseType.UNLICENSE, LicenseGroup.PERMISSIVE, license_file) |
| Matt Schulte | 38d199e | 2023-12-20 10:05:57 -0800 | [diff] [blame] | 147 | if ZERO_BSD_MATCHER.match(line): |
| 148 | return License(LicenseType.ZERO_BSD, LicenseGroup.PERMISSIVE, license_file) |
| Chih-Hung Hsieh | 3d24aed | 2020-10-05 15:29:11 -0700 | [diff] [blame] | 149 | print("ERROR: cannot decide license type in", license_file, |
| Thiébaud Weksteen | 8da4911 | 2021-02-19 11:59:49 +0100 | [diff] [blame] | 150 | "assume BSD_LIKE") |
| Matt Schulte | 055ccb3 | 2023-10-30 14:07:27 -0700 | [diff] [blame] | 151 | return License(LicenseType.BSD_LIKE, LicenseGroup.NOTICE, license_file) |
| Thiébaud Weksteen | 8da4911 | 2021-02-19 11:59:49 +0100 | [diff] [blame] | 152 | |
| 153 | |
| 154 | class LicenseType(enum.IntEnum): |
| 155 | """A type of license. |
| 156 | |
| 157 | An IntEnum is used to be able to sort by preference. This is mainly the case |
| 158 | for dual-licensed Apache/MIT code, for which we prefer the Apache license. |
| 159 | The enum name is used to generate the corresponding MODULE_LICENSE_* file. |
| 160 | """ |
| 161 | APACHE2 = 1 |
| 162 | MIT = 2 |
| 163 | BSD_LIKE = 3 |
| 164 | ISC = 4 |
| Matt Schulte | 055ccb3 | 2023-10-30 14:07:27 -0700 | [diff] [blame] | 165 | MPL = 5 |
| Matt Schulte | 38d199e | 2023-12-20 10:05:57 -0800 | [diff] [blame] | 166 | ZERO_BSD = 6 |
| Matt Schulte | b4fa3db | 2023-12-21 08:06:49 -0800 | [diff] [blame] | 167 | UNLICENSE = 7 |
| Matt Schulte | 055ccb3 | 2023-10-30 14:07:27 -0700 | [diff] [blame] | 168 | |
| 169 | class LicenseGroup(enum.Enum): |
| 170 | """A group of license as defined by go/thirdpartylicenses#types |
| 171 | |
| 172 | Note, go/thirdpartylicenses#types calls them "types". But LicenseType was |
| 173 | already taken so this script calls them groups. |
| 174 | """ |
| 175 | RESTRICTED = 1 |
| 176 | RESTRICTED_IF_STATICALLY_LINKED = 2 |
| 177 | RECIPROCAL = 3 |
| 178 | NOTICE = 4 |
| 179 | PERMISSIVE = 5 |
| 180 | BY_EXCEPTION_ONLY = 6 |
| Thiébaud Weksteen | 8da4911 | 2021-02-19 11:59:49 +0100 | [diff] [blame] | 181 | |
| 182 | |
| Matt Schulte | 055ccb3 | 2023-10-30 14:07:27 -0700 | [diff] [blame] | 183 | License = collections.namedtuple('License', ['type', 'group', 'filename']) |
| Chih-Hung Hsieh | 3d24aed | 2020-10-05 15:29:11 -0700 | [diff] [blame] | 184 | |
| 185 | |
| Chih-Hung Hsieh | 03f14e4 | 2020-10-19 18:38:30 -0700 | [diff] [blame] | 186 | def decide_license_type(cargo_license): |
| Thiébaud Weksteen | 8da4911 | 2021-02-19 11:59:49 +0100 | [diff] [blame] | 187 | """Check LICENSE* files to determine the license type. |
| 188 | |
| 189 | Returns: A list of Licenses. The first element is the license we prefer. |
| 190 | """ |
| Chih-Hung Hsieh | 3d24aed | 2020-10-05 15:29:11 -0700 | [diff] [blame] | 191 | # Most crates.io packages have both APACHE and MIT. |
| Chih-Hung Hsieh | 03f14e4 | 2020-10-19 18:38:30 -0700 | [diff] [blame] | 192 | # Some crate like time-macros-impl uses lower case names like LICENSE-Apache. |
| Thiébaud Weksteen | 8da4911 | 2021-02-19 11:59:49 +0100 | [diff] [blame] | 193 | licenses = [] |
| 194 | license_file = None |
| Matt Schulte | b4fa3db | 2023-12-21 08:06:49 -0800 | [diff] [blame] | 195 | for license_file in glob.glob("LICENSE*") + glob.glob("COPYING*") + glob.glob("UNLICENSE"): |
| Chih-Hung Hsieh | 03f14e4 | 2020-10-19 18:38:30 -0700 | [diff] [blame] | 196 | lowered_name = license_file.lower() |
| 197 | if lowered_name == "license-apache": |
| Matt Schulte | 055ccb3 | 2023-10-30 14:07:27 -0700 | [diff] [blame] | 198 | licenses.append(License(LicenseType.APACHE2, LicenseGroup.NOTICE, license_file)) |
| Chih-Hung Hsieh | 03f14e4 | 2020-10-19 18:38:30 -0700 | [diff] [blame] | 199 | elif lowered_name == "license-mit": |
| Matt Schulte | 055ccb3 | 2023-10-30 14:07:27 -0700 | [diff] [blame] | 200 | licenses.append(License(LicenseType.MIT, LicenseGroup.NOTICE, license_file)) |
| Matt Schulte | 38d199e | 2023-12-20 10:05:57 -0800 | [diff] [blame] | 201 | elif lowered_name == "license-0bsd": |
| 202 | licenses.append(License(LicenseType.ZERO_BSD, LicenseGroup.PERMISSIVE, license_file)) |
| Matt Schulte | b4fa3db | 2023-12-21 08:06:49 -0800 | [diff] [blame] | 203 | elif lowered_name == "unlicense": |
| 204 | licenses.append(License(LicenseType.UNLICENSE, LicenseGroup.PERMISSIVE, license_file)) |
| Thiébaud Weksteen | 8da4911 | 2021-02-19 11:59:49 +0100 | [diff] [blame] | 205 | if licenses: |
| 206 | licenses.sort(key=lambda l: l.type) |
| 207 | return licenses |
| 208 | if not license_file: |
| 209 | raise FileNotFoundError("No license file has been found.") |
| Matthew Maurer | 51ec016 | 2022-08-10 15:29:24 -0700 | [diff] [blame] | 210 | # There is a LICENSE* or COPYING* file, use cargo_license found in |
| Thiébaud Weksteen | 8da4911 | 2021-02-19 11:59:49 +0100 | [diff] [blame] | 211 | # Cargo.toml. |
| Chih-Hung Hsieh | 03f14e4 | 2020-10-19 18:38:30 -0700 | [diff] [blame] | 212 | if "Apache" in cargo_license: |
| Matt Schulte | 055ccb3 | 2023-10-30 14:07:27 -0700 | [diff] [blame] | 213 | return [License(LicenseType.APACHE2, LicenseGroup.NOTICE, license_file)] |
| Chih-Hung Hsieh | 03f14e4 | 2020-10-19 18:38:30 -0700 | [diff] [blame] | 214 | if "MIT" in cargo_license: |
| Matt Schulte | 055ccb3 | 2023-10-30 14:07:27 -0700 | [diff] [blame] | 215 | return [License(LicenseType.MIT, LicenseGroup.NOTICE, license_file)] |
| Matt Schulte | 38d199e | 2023-12-20 10:05:57 -0800 | [diff] [blame] | 216 | if "0BSD" in cargo_license: |
| 217 | return [License(LicenseType.ZERO_BSD, LicenseGroup.PERMISSIVE, license_file)] |
| Chih-Hung Hsieh | 03f14e4 | 2020-10-19 18:38:30 -0700 | [diff] [blame] | 218 | if "BSD" in cargo_license: |
| Matt Schulte | 055ccb3 | 2023-10-30 14:07:27 -0700 | [diff] [blame] | 219 | return [License(LicenseType.BSD_LIKE, LicenseGroup.NOTICE, license_file)] |
| Chih-Hung Hsieh | 03f14e4 | 2020-10-19 18:38:30 -0700 | [diff] [blame] | 220 | if "ISC" in cargo_license: |
| Matt Schulte | 055ccb3 | 2023-10-30 14:07:27 -0700 | [diff] [blame] | 221 | return [License(LicenseType.ISC, LicenseGroup.NOTICE, license_file)] |
| 222 | if "MPL" in cargo_license: |
| 223 | return [License(LicenseType.MPL, LicenseGroup.RECIPROCAL, license_file)] |
| Matt Schulte | b4fa3db | 2023-12-21 08:06:49 -0800 | [diff] [blame] | 224 | if "Unlicense" in cargo_license: |
| 225 | return [License(LicenseType.UNLICENSE, LicenseGroup.PERMISSIVE, license_file)] |
| Thiébaud Weksteen | 8da4911 | 2021-02-19 11:59:49 +0100 | [diff] [blame] | 226 | return [grep_license_keyword(license_file)] |
| Chih-Hung Hsieh | 3d24aed | 2020-10-05 15:29:11 -0700 | [diff] [blame] | 227 | |
| 228 | |
| 229 | def add_notice(): |
| 230 | if not os.path.exists("NOTICE"): |
| 231 | if os.path.exists("LICENSE"): |
| 232 | os.symlink("LICENSE", "NOTICE") |
| 233 | print("Created link from NOTICE to LICENSE") |
| 234 | else: |
| 235 | print("ERROR: missing NOTICE and LICENSE") |
| 236 | |
| 237 | |
| Chih-Hung Hsieh | 03f14e4 | 2020-10-19 18:38:30 -0700 | [diff] [blame] | 238 | def check_license_link(target): |
| 239 | """Check the LICENSE link, must bet the given target.""" |
| Chih-Hung Hsieh | 3d24aed | 2020-10-05 15:29:11 -0700 | [diff] [blame] | 240 | if not os.path.islink("LICENSE"): |
| 241 | print("ERROR: LICENSE file is not a link") |
| 242 | return |
| Chih-Hung Hsieh | 03f14e4 | 2020-10-19 18:38:30 -0700 | [diff] [blame] | 243 | found_target = os.readlink("LICENSE") |
| 244 | if target != found_target and found_target != "LICENSE.txt": |
| 245 | print("ERROR: found LICENSE link to", found_target, |
| 246 | "but expected", target) |
| Chih-Hung Hsieh | 3d24aed | 2020-10-05 15:29:11 -0700 | [diff] [blame] | 247 | |
| 248 | |
| Chih-Hung Hsieh | 03f14e4 | 2020-10-19 18:38:30 -0700 | [diff] [blame] | 249 | def add_license(target): |
| 250 | """Add LICENSE link to give target.""" |
| Chih-Hung Hsieh | 3d24aed | 2020-10-05 15:29:11 -0700 | [diff] [blame] | 251 | if os.path.exists("LICENSE"): |
| 252 | if os.path.islink("LICENSE"): |
| Chih-Hung Hsieh | 03f14e4 | 2020-10-19 18:38:30 -0700 | [diff] [blame] | 253 | check_license_link(target) |
| Chih-Hung Hsieh | 3d24aed | 2020-10-05 15:29:11 -0700 | [diff] [blame] | 254 | else: |
| Thiébaud Weksteen | 8da4911 | 2021-02-19 11:59:49 +0100 | [diff] [blame] | 255 | print("NOTE: found LICENSE and it is not a link.") |
| Chih-Hung Hsieh | 3d24aed | 2020-10-05 15:29:11 -0700 | [diff] [blame] | 256 | return |
| Chih-Hung Hsieh | 3d24aed | 2020-10-05 15:29:11 -0700 | [diff] [blame] | 257 | print("### Creating LICENSE link to", target) |
| Chih-Hung Hsieh | 03f14e4 | 2020-10-19 18:38:30 -0700 | [diff] [blame] | 258 | os.symlink(target, "LICENSE") |
| Chih-Hung Hsieh | 3d24aed | 2020-10-05 15:29:11 -0700 | [diff] [blame] | 259 | |
| 260 | |
| 261 | def add_module_license(license_type): |
| 262 | """Touch MODULE_LICENSE_type file.""" |
| 263 | # Do not change existing MODULE_* files. |
| Matt Schulte | b4fa3db | 2023-12-21 08:06:49 -0800 | [diff] [blame] | 264 | for suffix in ["MIT", "APACHE", "APACHE2", "BSD_LIKE", "MPL", "0BSD", "UNLICENSE"]: |
| Chih-Hung Hsieh | 3d24aed | 2020-10-05 15:29:11 -0700 | [diff] [blame] | 265 | module_file = "MODULE_LICENSE_" + suffix |
| 266 | if os.path.exists(module_file): |
| Thiébaud Weksteen | 8da4911 | 2021-02-19 11:59:49 +0100 | [diff] [blame] | 267 | if license_type.name != suffix: |
| 268 | raise Exception("Found unexpected license " + module_file) |
| Chih-Hung Hsieh | 3d24aed | 2020-10-05 15:29:11 -0700 | [diff] [blame] | 269 | return |
| Thiébaud Weksteen | 8da4911 | 2021-02-19 11:59:49 +0100 | [diff] [blame] | 270 | module_file = "MODULE_LICENSE_" + license_type.name.upper() |
| Chih-Hung Hsieh | 3d24aed | 2020-10-05 15:29:11 -0700 | [diff] [blame] | 271 | pathlib.Path(module_file).touch() |
| 272 | print("### Touched", module_file) |
| 273 | |
| 274 | |
| 275 | def found_line(file_name, line): |
| 276 | """Returns true if the given line is found in a file.""" |
| 277 | with open(file_name, "r") as input_file: |
| 278 | return line in input_file |
| 279 | |
| 280 | |
| 281 | def add_owners(): |
| 282 | """Create or append OWNERS with the default owner line.""" |
| 283 | # Existing OWNERS file might contain more than the default owners. |
| 284 | # Only append missing default owners to existing OWNERS. |
| 285 | if os.path.isfile("OWNERS"): |
| 286 | if found_line("OWNERS", DEFAULT_OWNERS): |
| 287 | print("### No change to OWNERS, which has already default owners.") |
| 288 | return |
| 289 | else: |
| 290 | print("### Append default owners to OWNERS") |
| 291 | mode = "a" |
| 292 | else: |
| 293 | print("### Creating OWNERS with default owners") |
| 294 | mode = "w" |
| 295 | with open("OWNERS", mode) as outf: |
| 296 | outf.write(DEFAULT_OWNERS) |
| 297 | |
| 298 | |
| 299 | def toml2json(line): |
| 300 | """Convert a quoted toml string to a json quoted string for METADATA.""" |
| 301 | if line.startswith("\"\"\""): |
| 302 | return "\"()\"" # cannot handle broken multi-line description |
| 303 | # TOML string escapes: \b \t \n \f \r \" \\ (no unicode escape) |
| 304 | line = line[1:-1].replace("\\\\", "\n").replace("\\b", "") |
| 305 | line = line.replace("\\t", " ").replace("\\n", " ").replace("\\f", " ") |
| 306 | line = line.replace("\\r", "").replace("\\\"", "\"").replace("\n", "\\") |
| 307 | # replace a unicode quotation mark, used in the libloading crate |
| 308 | line = line.replace("’", "'") |
| 309 | # strip and escape single quotes |
| 310 | return json.dumps(line.strip()).replace("'", "\\'") |
| 311 | |
| 312 | |
| 313 | def parse_cargo_toml(cargo): |
| Chih-Hung Hsieh | 03f14e4 | 2020-10-19 18:38:30 -0700 | [diff] [blame] | 314 | """get name, version, description, license string from Cargo.toml.""" |
| Chih-Hung Hsieh | 3d24aed | 2020-10-05 15:29:11 -0700 | [diff] [blame] | 315 | name = "" |
| 316 | version = "" |
| 317 | description = "" |
| Chih-Hung Hsieh | 03f14e4 | 2020-10-19 18:38:30 -0700 | [diff] [blame] | 318 | cargo_license = "" |
| Chih-Hung Hsieh | 3d24aed | 2020-10-05 15:29:11 -0700 | [diff] [blame] | 319 | with open(cargo, "r") as toml: |
| 320 | for line in toml: |
| Chih-Hung Hsieh | 03f14e4 | 2020-10-19 18:38:30 -0700 | [diff] [blame] | 321 | if not name and NAME_MATCHER.match(line): |
| 322 | name = NAME_MATCHER.match(line).group(1) |
| 323 | elif not version and VERSION_MATCHER.match(line): |
| 324 | version = VERSION_MATCHER.match(line).group(1) |
| 325 | elif not description and DESCRIPTION_MATCHER.match(line): |
| 326 | description = toml2json(DESCRIPTION_MATCHER.match(line).group(1)) |
| 327 | elif not cargo_license and LICENSE_MATCHER.match(line): |
| 328 | cargo_license = LICENSE_MATCHER.match(line).group(1) |
| 329 | if name and version and description and cargo_license: |
| Chih-Hung Hsieh | 3d24aed | 2020-10-05 15:29:11 -0700 | [diff] [blame] | 330 | break |
| Chih-Hung Hsieh | 03f14e4 | 2020-10-19 18:38:30 -0700 | [diff] [blame] | 331 | return name, version, description, cargo_license |
| Chih-Hung Hsieh | 3d24aed | 2020-10-05 15:29:11 -0700 | [diff] [blame] | 332 | |
| 333 | |
| 334 | def main(): |
| 335 | """Add 3rd party review files.""" |
| 336 | cargo = "Cargo.toml" |
| 337 | if not os.path.isfile(cargo): |
| 338 | print("ERROR: ", cargo, "is not found") |
| 339 | return |
| 340 | if not os.access(cargo, os.R_OK): |
| 341 | print("ERROR: ", cargo, "is not readable") |
| 342 | return |
| Chih-Hung Hsieh | 03f14e4 | 2020-10-19 18:38:30 -0700 | [diff] [blame] | 343 | name, version, description, cargo_license = parse_cargo_toml(cargo) |
| Chih-Hung Hsieh | 3d24aed | 2020-10-05 15:29:11 -0700 | [diff] [blame] | 344 | if not name or not version or not description: |
| 345 | print("ERROR: Cannot find name, version, or description in", cargo) |
| 346 | return |
| Chih-Hung Hsieh | 03f14e4 | 2020-10-19 18:38:30 -0700 | [diff] [blame] | 347 | print("### Cargo.toml license:", cargo_license) |
| Thiébaud Weksteen | 8da4911 | 2021-02-19 11:59:49 +0100 | [diff] [blame] | 348 | licenses = decide_license_type(cargo_license) |
| 349 | preferred_license = licenses[0] |
| Matt Schulte | 055ccb3 | 2023-10-30 14:07:27 -0700 | [diff] [blame] | 350 | add_metadata(name, version, description, preferred_license.group.name, len(licenses) > 1) |
| Chih-Hung Hsieh | 3d24aed | 2020-10-05 15:29:11 -0700 | [diff] [blame] | 351 | add_owners() |
| Thiébaud Weksteen | 8da4911 | 2021-02-19 11:59:49 +0100 | [diff] [blame] | 352 | add_license(preferred_license.filename) |
| 353 | add_module_license(preferred_license.type) |
| Chih-Hung Hsieh | 3d24aed | 2020-10-05 15:29:11 -0700 | [diff] [blame] | 354 | # It is unclear yet if a NOTICE file is required. |
| 355 | # add_notice() |
| 356 | |
| 357 | |
| 358 | if __name__ == "__main__": |
| 359 | main() |