Abhishek Pandit-Subedi | b75bd56 | 2021-02-25 15:32:22 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # -*- coding: utf-8 -*- |
George Burgess IV | 9e0cfde | 2022-09-27 15:08:15 -0700 | [diff] [blame] | 3 | # Copyright 2021 The ChromiumOS Authors |
Abhishek Pandit-Subedi | b75bd56 | 2021-02-25 15:32:22 -0800 | [diff] [blame] | 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | """ This script cleans up the vendor directory. |
| 7 | """ |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 8 | import argparse |
George Burgess IV | 635f726 | 2022-08-09 21:32:20 -0700 | [diff] [blame] | 9 | import collections |
Abhishek Pandit-Subedi | 5065a0f | 2021-06-13 20:38:55 +0000 | [diff] [blame] | 10 | import hashlib |
Abhishek Pandit-Subedi | b75bd56 | 2021-02-25 15:32:22 -0800 | [diff] [blame] | 11 | import json |
| 12 | import os |
| 13 | import pathlib |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 14 | import re |
Abhishek Pandit-Subedi | f0eb6e0 | 2021-09-24 16:36:12 -0700 | [diff] [blame] | 15 | import shutil |
Abhishek Pandit-Subedi | 5065a0f | 2021-06-13 20:38:55 +0000 | [diff] [blame] | 16 | import subprocess |
George Burgess IV | 0483370 | 2022-08-09 22:00:38 -0700 | [diff] [blame] | 17 | import textwrap |
Abhishek Pandit-Subedi | ce0f5b2 | 2021-09-10 15:50:08 -0700 | [diff] [blame] | 18 | import toml |
Abhishek Pandit-Subedi | 5065a0f | 2021-06-13 20:38:55 +0000 | [diff] [blame] | 19 | |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 20 | # We only care about crates we're actually going to use and that's usually |
| 21 | # limited to ones with cfg(linux). For running `cargo metadata`, limit results |
| 22 | # to only this platform |
| 23 | DEFAULT_PLATFORM_FILTER = "x86_64-unknown-linux-gnu" |
| 24 | |
George Burgess IV | 8e2cc04 | 2022-10-18 14:50:48 -0600 | [diff] [blame] | 25 | # A series of crates which are to be made empty by having no (non-comment) |
| 26 | # contents in their `lib.rs`, rather than by inserting a compilation error. |
| 27 | NOP_EMPTY_CRATES = frozenset({"windows"}) |
| 28 | |
| 29 | EMPTY_CRATE_BODY = """\ |
| 30 | compile_error!("This crate cannot be built for this configuration."); |
| 31 | """ |
| 32 | NOP_EMPTY_CRATE_BODY = "// " + EMPTY_CRATE_BODY |
| 33 | |
Abhishek Pandit-Subedi | 5065a0f | 2021-06-13 20:38:55 +0000 | [diff] [blame] | 34 | |
| 35 | def _rerun_checksums(package_path): |
| 36 | """Re-run checksums for given package. |
| 37 | |
| 38 | Writes resulting checksums to $package_path/.cargo-checksum.json. |
| 39 | """ |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 40 | hashes = dict() |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 41 | checksum_path = os.path.join(package_path, ".cargo-checksum.json") |
Abhishek Pandit-Subedi | 5065a0f | 2021-06-13 20:38:55 +0000 | [diff] [blame] | 42 | if not pathlib.Path(checksum_path).is_file(): |
| 43 | return False |
| 44 | |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 45 | with open(checksum_path, "r") as fread: |
Abhishek Pandit-Subedi | 5065a0f | 2021-06-13 20:38:55 +0000 | [diff] [blame] | 46 | contents = json.load(fread) |
| 47 | |
| 48 | for root, _, files in os.walk(package_path, topdown=True): |
| 49 | for f in files: |
| 50 | # Don't checksum an existing checksum file |
| 51 | if f == ".cargo-checksum.json": |
| 52 | continue |
| 53 | |
| 54 | file_path = os.path.join(root, f) |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 55 | with open(file_path, "rb") as frb: |
Abhishek Pandit-Subedi | 5065a0f | 2021-06-13 20:38:55 +0000 | [diff] [blame] | 56 | m = hashlib.sha256() |
| 57 | m.update(frb.read()) |
| 58 | d = m.hexdigest() |
| 59 | |
| 60 | # Key is relative to the package path so strip from beginning |
| 61 | key = os.path.relpath(file_path, package_path) |
| 62 | hashes[key] = d |
| 63 | |
| 64 | if hashes: |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 65 | print( |
| 66 | "{} regenerated {} hashes".format(package_path, len(hashes.keys())) |
| 67 | ) |
| 68 | contents["files"] = hashes |
| 69 | with open(checksum_path, "w") as fwrite: |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 70 | json.dump(contents, fwrite, sort_keys=True) |
Abhishek Pandit-Subedi | 5065a0f | 2021-06-13 20:38:55 +0000 | [diff] [blame] | 71 | |
| 72 | return True |
Abhishek Pandit-Subedi | b75bd56 | 2021-02-25 15:32:22 -0800 | [diff] [blame] | 73 | |
| 74 | |
| 75 | def _remove_OWNERS_checksum(root): |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 76 | """Delete all OWNERS files from the checksum file. |
Abhishek Pandit-Subedi | b75bd56 | 2021-02-25 15:32:22 -0800 | [diff] [blame] | 77 | |
Abhishek Pandit-Subedi | 5065a0f | 2021-06-13 20:38:55 +0000 | [diff] [blame] | 78 | Args: |
| 79 | root: Root directory for the vendored crate. |
Abhishek Pandit-Subedi | b75bd56 | 2021-02-25 15:32:22 -0800 | [diff] [blame] | 80 | |
Abhishek Pandit-Subedi | 5065a0f | 2021-06-13 20:38:55 +0000 | [diff] [blame] | 81 | Returns: |
| 82 | True if OWNERS was found and cleaned up. Otherwise False. |
| 83 | """ |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 84 | checksum_path = os.path.join(root, ".cargo-checksum.json") |
Abhishek Pandit-Subedi | b75bd56 | 2021-02-25 15:32:22 -0800 | [diff] [blame] | 85 | if not pathlib.Path(checksum_path).is_file(): |
| 86 | return False |
| 87 | |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 88 | with open(checksum_path, "r") as fread: |
Abhishek Pandit-Subedi | b75bd56 | 2021-02-25 15:32:22 -0800 | [diff] [blame] | 89 | contents = json.load(fread) |
| 90 | |
| 91 | del_keys = [] |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 92 | for cfile in contents["files"]: |
| 93 | if "OWNERS" in cfile: |
Abhishek Pandit-Subedi | b75bd56 | 2021-02-25 15:32:22 -0800 | [diff] [blame] | 94 | del_keys.append(cfile) |
| 95 | |
| 96 | for key in del_keys: |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 97 | del contents["files"][key] |
Abhishek Pandit-Subedi | b75bd56 | 2021-02-25 15:32:22 -0800 | [diff] [blame] | 98 | |
| 99 | if del_keys: |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 100 | print("{} deleted: {}".format(root, del_keys)) |
| 101 | with open(checksum_path, "w") as fwrite: |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 102 | json.dump(contents, fwrite, sort_keys=True) |
Abhishek Pandit-Subedi | b75bd56 | 2021-02-25 15:32:22 -0800 | [diff] [blame] | 103 | |
| 104 | return bool(del_keys) |
| 105 | |
| 106 | |
| 107 | def cleanup_owners(vendor_path): |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 108 | """Remove owners checksums from the vendor directory. |
Abhishek Pandit-Subedi | b75bd56 | 2021-02-25 15:32:22 -0800 | [diff] [blame] | 109 | |
Abhishek Pandit-Subedi | 5065a0f | 2021-06-13 20:38:55 +0000 | [diff] [blame] | 110 | We currently do not check in the OWNERS files from vendored crates because |
| 111 | they interfere with the find-owners functionality in gerrit. This cleanup |
| 112 | simply finds all instances of "OWNERS" in the checksum files within and |
| 113 | removes them. |
Abhishek Pandit-Subedi | b75bd56 | 2021-02-25 15:32:22 -0800 | [diff] [blame] | 114 | |
Abhishek Pandit-Subedi | 5065a0f | 2021-06-13 20:38:55 +0000 | [diff] [blame] | 115 | Args: |
| 116 | vendor_path: Absolute path to vendor directory. |
| 117 | """ |
Abhishek Pandit-Subedi | b75bd56 | 2021-02-25 15:32:22 -0800 | [diff] [blame] | 118 | deps_cleaned = [] |
| 119 | for root, dirs, _ in os.walk(vendor_path): |
| 120 | for d in dirs: |
| 121 | removed = _remove_OWNERS_checksum(os.path.join(root, d)) |
| 122 | if removed: |
| 123 | deps_cleaned.append(d) |
| 124 | |
| 125 | if deps_cleaned: |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 126 | print("Cleanup owners:\n {}".format("\n".join(deps_cleaned))) |
Abhishek Pandit-Subedi | b75bd56 | 2021-02-25 15:32:22 -0800 | [diff] [blame] | 127 | |
| 128 | |
Abhishek Pandit-Subedi | 5065a0f | 2021-06-13 20:38:55 +0000 | [diff] [blame] | 129 | def apply_single_patch(patch, workdir): |
| 130 | """Apply a single patch and return whether it was successful. |
| 131 | |
| 132 | Returns: |
| 133 | True if successful. False otherwise. |
| 134 | """ |
George Burgess IV | 08664ba | 2022-10-03 11:09:33 -0700 | [diff] [blame] | 135 | proc = subprocess.run( |
| 136 | [ |
| 137 | "patch", |
| 138 | "-p1", |
| 139 | "--no-backup-if-mismatch", |
| 140 | "-i", |
| 141 | patch, |
| 142 | ], |
| 143 | cwd=workdir, |
| 144 | ) |
Abhishek Pandit-Subedi | 5065a0f | 2021-06-13 20:38:55 +0000 | [diff] [blame] | 145 | return proc.returncode == 0 |
| 146 | |
| 147 | |
George Burgess IV | 30c5c36 | 2022-08-19 17:05:02 -0700 | [diff] [blame] | 148 | def apply_patch_script(script, workdir): |
| 149 | """Run the given patch script, returning whether it exited cleanly. |
| 150 | |
| 151 | Returns: |
| 152 | True if successful. False otherwise. |
| 153 | """ |
| 154 | return subprocess.run([script], cwd=workdir).returncode == 0 |
| 155 | |
| 156 | |
George Burgess IV | 635f726 | 2022-08-09 21:32:20 -0700 | [diff] [blame] | 157 | def determine_vendor_crates(vendor_path): |
| 158 | """Returns a map of {crate_name: [directory]} at the given vendor_path.""" |
| 159 | result = collections.defaultdict(list) |
George Burgess IV | 76b60d0 | 2022-10-26 17:44:48 -0600 | [diff] [blame] | 160 | crate_version_re = re.compile(r"-\d+\.\d+\.\d+(:?[+-]|$)") |
George Burgess IV | 635f726 | 2022-08-09 21:32:20 -0700 | [diff] [blame] | 161 | for crate_name_plus_ver in os.listdir(vendor_path): |
George Burgess IV | 76b60d0 | 2022-10-26 17:44:48 -0600 | [diff] [blame] | 162 | version = crate_version_re.search(crate_name_plus_ver) |
| 163 | assert version, crate_name_plus_ver |
| 164 | name = crate_name_plus_ver[: version.start()] |
George Burgess IV | 40cc91c | 2022-08-15 13:07:40 -0700 | [diff] [blame] | 165 | result[name].append(crate_name_plus_ver) |
George Burgess IV | 635f726 | 2022-08-09 21:32:20 -0700 | [diff] [blame] | 166 | |
| 167 | for crate_list in result.values(): |
George Burgess IV | 40cc91c | 2022-08-15 13:07:40 -0700 | [diff] [blame] | 168 | crate_list.sort() |
George Burgess IV | 635f726 | 2022-08-09 21:32:20 -0700 | [diff] [blame] | 169 | return result |
| 170 | |
| 171 | |
Abhishek Pandit-Subedi | 5065a0f | 2021-06-13 20:38:55 +0000 | [diff] [blame] | 172 | def apply_patches(patches_path, vendor_path): |
| 173 | """Finds patches and applies them to sub-folders in the vendored crates. |
| 174 | |
| 175 | Args: |
| 176 | patches_path: Path to folder with patches. Expect all patches to be one |
| 177 | level down (matching the crate name). |
| 178 | vendor_path: Root path to vendored crates directory. |
| 179 | """ |
| 180 | checksums_for = {} |
| 181 | |
| 182 | # Don't bother running if patches directory is empty |
| 183 | if not pathlib.Path(patches_path).is_dir(): |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 184 | return |
Abhishek Pandit-Subedi | 5065a0f | 2021-06-13 20:38:55 +0000 | [diff] [blame] | 185 | |
George Burgess IV | 30c5c36 | 2022-08-19 17:05:02 -0700 | [diff] [blame] | 186 | patches_failed = False |
George Burgess IV | 635f726 | 2022-08-09 21:32:20 -0700 | [diff] [blame] | 187 | vendor_crate_map = determine_vendor_crates(vendor_path) |
Abhishek Pandit-Subedi | 5065a0f | 2021-06-13 20:38:55 +0000 | [diff] [blame] | 188 | # Look for all patches and apply them |
| 189 | for d in os.listdir(patches_path): |
| 190 | dir_path = os.path.join(patches_path, d) |
| 191 | |
| 192 | # We don't process patches in root dir |
| 193 | if not os.path.isdir(dir_path): |
| 194 | continue |
| 195 | |
George Burgess IV | 30c5c36 | 2022-08-19 17:05:02 -0700 | [diff] [blame] | 196 | # We accept one of two forms here: |
| 197 | # - direct targets (these name # `${crate_name}-${version}`) |
| 198 | # - simply the crate name (which applies to all versions of the |
| 199 | # crate) |
| 200 | direct_target = os.path.join(vendor_path, d) |
| 201 | if os.path.isdir(direct_target): |
| 202 | patch_targets = [d] |
| 203 | elif d in vendor_crate_map: |
| 204 | patch_targets = vendor_crate_map[d] |
| 205 | else: |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 206 | raise RuntimeError(f"Unknown crate in {vendor_path}: {d}") |
George Burgess IV | 30c5c36 | 2022-08-19 17:05:02 -0700 | [diff] [blame] | 207 | |
George Burgess IV | 635f726 | 2022-08-09 21:32:20 -0700 | [diff] [blame] | 208 | for patch in os.listdir(dir_path): |
Abhishek Pandit-Subedi | 5065a0f | 2021-06-13 20:38:55 +0000 | [diff] [blame] | 209 | file_path = os.path.join(dir_path, patch) |
| 210 | |
| 211 | # Skip if not a patch file |
George Burgess IV | 30c5c36 | 2022-08-19 17:05:02 -0700 | [diff] [blame] | 212 | if not os.path.isfile(file_path): |
Abhishek Pandit-Subedi | 5065a0f | 2021-06-13 20:38:55 +0000 | [diff] [blame] | 213 | continue |
| 214 | |
George Burgess IV | 30c5c36 | 2022-08-19 17:05:02 -0700 | [diff] [blame] | 215 | if patch.endswith(".patch"): |
| 216 | apply = apply_single_patch |
| 217 | elif os.access(file_path, os.X_OK): |
| 218 | apply = apply_patch_script |
George Burgess IV | 635f726 | 2022-08-09 21:32:20 -0700 | [diff] [blame] | 219 | else: |
George Burgess IV | 30c5c36 | 2022-08-19 17:05:02 -0700 | [diff] [blame] | 220 | # Unrecognized. Skip it. |
| 221 | continue |
| 222 | |
| 223 | for target_name in patch_targets: |
| 224 | checksums_for[target_name] = True |
| 225 | target = os.path.join(vendor_path, target_name) |
| 226 | print(f"-- Applying {file_path} to {target}") |
| 227 | if not apply(file_path, target): |
| 228 | print(f"Failed to apply {file_path} to {target}") |
| 229 | patches_failed = True |
| 230 | |
| 231 | # Do this late, so we can report all of the failing patches in one |
| 232 | # invocation. |
| 233 | if patches_failed: |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 234 | raise ValueError("Patches failed; please see above logs") |
Abhishek Pandit-Subedi | 5065a0f | 2021-06-13 20:38:55 +0000 | [diff] [blame] | 235 | |
Abhishek Pandit-Subedi | 5065a0f | 2021-06-13 20:38:55 +0000 | [diff] [blame] | 236 | # Re-run checksums for all modified packages since we applied patches. |
| 237 | for key in checksums_for.keys(): |
| 238 | _rerun_checksums(os.path.join(vendor_path, key)) |
| 239 | |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 240 | |
George Burgess IV | 18af563 | 2022-08-30 14:10:53 -0700 | [diff] [blame] | 241 | def get_workspace_cargo_toml(working_dir): |
George Burgess IV | 40cc91c | 2022-08-15 13:07:40 -0700 | [diff] [blame] | 242 | """Returns all Cargo.toml files under working_dir.""" |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 243 | return [working_dir / "projects" / "Cargo.toml"] |
George Burgess IV | 40cc91c | 2022-08-15 13:07:40 -0700 | [diff] [blame] | 244 | |
| 245 | |
Abhishek Pandit-Subedi | fa90238 | 2021-08-20 11:04:33 -0700 | [diff] [blame] | 246 | def run_cargo_vendor(working_dir): |
| 247 | """Runs cargo vendor. |
| 248 | |
| 249 | Args: |
| 250 | working_dir: Directory to run inside. This should be the directory where |
Abhishek Pandit-Subedi | ce0f5b2 | 2021-09-10 15:50:08 -0700 | [diff] [blame] | 251 | Cargo.toml is kept. |
Abhishek Pandit-Subedi | fa90238 | 2021-08-20 11:04:33 -0700 | [diff] [blame] | 252 | """ |
George Burgess IV | 635f726 | 2022-08-09 21:32:20 -0700 | [diff] [blame] | 253 | # Cargo will refuse to revendor into versioned directories, which leads to |
| 254 | # repeated `./vendor.py` invocations trying to apply patches to |
| 255 | # already-patched sources. Remove the existing vendor directory to avoid |
| 256 | # this. |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 257 | vendor_dir = working_dir / "vendor" |
George Burgess IV | 635f726 | 2022-08-09 21:32:20 -0700 | [diff] [blame] | 258 | if vendor_dir.exists(): |
George Burgess IV | 40cc91c | 2022-08-15 13:07:40 -0700 | [diff] [blame] | 259 | shutil.rmtree(vendor_dir) |
| 260 | |
George Burgess IV | 18af563 | 2022-08-30 14:10:53 -0700 | [diff] [blame] | 261 | cargo_cmdline = [ |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 262 | "cargo", |
| 263 | "vendor", |
| 264 | "--versioned-dirs", |
| 265 | "-v", |
| 266 | "--manifest-path=projects/Cargo.toml", |
| 267 | "--", |
| 268 | "vendor", |
George Burgess IV | 18af563 | 2022-08-30 14:10:53 -0700 | [diff] [blame] | 269 | ] |
George Burgess IV | 40cc91c | 2022-08-15 13:07:40 -0700 | [diff] [blame] | 270 | subprocess.check_call(cargo_cmdline, cwd=working_dir) |
Abhishek Pandit-Subedi | 5065a0f | 2021-06-13 20:38:55 +0000 | [diff] [blame] | 271 | |
Abhishek Pandit-Subedi | ce0f5b2 | 2021-09-10 15:50:08 -0700 | [diff] [blame] | 272 | |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 273 | def load_metadata(working_dir, filter_platform=DEFAULT_PLATFORM_FILTER): |
George Burgess IV | 40cc91c | 2022-08-15 13:07:40 -0700 | [diff] [blame] | 274 | """Load metadata for all projects under a given directory. |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 275 | |
| 276 | Args: |
George Burgess IV | 40cc91c | 2022-08-15 13:07:40 -0700 | [diff] [blame] | 277 | working_dir: Base directory to run from. |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 278 | filter_platform: Filter packages to ones configured for this platform. |
| 279 | """ |
George Burgess IV | 40cc91c | 2022-08-15 13:07:40 -0700 | [diff] [blame] | 280 | metadata_objects = [] |
George Burgess IV | 18af563 | 2022-08-30 14:10:53 -0700 | [diff] [blame] | 281 | cmd = [ |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 282 | "cargo", |
| 283 | "metadata", |
| 284 | "--format-version=1", |
| 285 | "--manifest-path=projects/Cargo.toml", |
George Burgess IV | 18af563 | 2022-08-30 14:10:53 -0700 | [diff] [blame] | 286 | ] |
| 287 | # Conditionally add platform filter |
| 288 | if filter_platform: |
| 289 | cmd += ("--filter-platform", filter_platform) |
| 290 | output = subprocess.check_output(cmd, cwd=working_dir) |
| 291 | return json.loads(output) |
Abhishek Pandit-Subedi | f0eb6e0 | 2021-09-24 16:36:12 -0700 | [diff] [blame] | 292 | |
Abhishek Pandit-Subedi | f0eb6e0 | 2021-09-24 16:36:12 -0700 | [diff] [blame] | 293 | |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 294 | class LicenseManager: |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 295 | """Manage consolidating licenses for all packages.""" |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 296 | |
George Burgess IV | 124e6a1 | 2022-09-09 10:44:29 -0700 | [diff] [blame] | 297 | # These are all the licenses we support. Keys are what is seen in metadata |
| 298 | # and values are what is expected by ebuilds. |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 299 | SUPPORTED_LICENSES = { |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 300 | "0BSD": "0BSD", |
| 301 | "Apache-2.0": "Apache-2.0", |
George Burgess IV | b16816a | 2022-10-26 17:55:48 -0600 | [diff] [blame] | 302 | "BSD-2-Clause": "BSD-2", |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 303 | "BSD-3-Clause": "BSD-3", |
| 304 | "ISC": "ISC", |
| 305 | "MIT": "MIT", |
| 306 | "MPL-2.0": "MPL-2.0", |
| 307 | "unicode": "unicode", |
Dan Callaghan | 91f8054 | 2022-09-09 10:57:23 +1000 | [diff] [blame] | 308 | "Zlib": "ZLIB", |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | # Prefer to take attribution licenses in this order. All these require that |
| 312 | # we actually use the license file found in the package so they MUST have |
| 313 | # a license file set. |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 314 | PREFERRED_ATTRIB_LICENSE_ORDER = ["MIT", "BSD-3", "ISC"] |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 315 | |
| 316 | # If Apache license is found, always prefer it (simplifies attribution) |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 317 | APACHE_LICENSE = "Apache-2.0" |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 318 | |
| 319 | # Regex for license files found in the vendored directories. Search for |
| 320 | # these files with re.IGNORECASE. |
| 321 | # |
| 322 | # These will be searched in order with the earlier entries being preferred. |
| 323 | LICENSE_NAMES_REGEX = [ |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 324 | r"^license-mit$", |
| 325 | r"^copyright$", |
| 326 | r"^licen[cs]e.*$", |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 327 | ] |
| 328 | |
| 329 | # Some crates have their license file in other crates. This usually occurs |
| 330 | # because multiple crates are published from the same git repository and the |
| 331 | # license isn't updated in each sub-crate. In these cases, we can just |
| 332 | # ignore these packages. |
| 333 | MAP_LICENSE_TO_OTHER = { |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 334 | "failure_derive": "failure", |
| 335 | "grpcio-compiler": "grpcio", |
| 336 | "grpcio-sys": "grpcio", |
| 337 | "rustyline-derive": "rustyline", |
Nicholas Bishop | 7d4433a | 2022-10-07 12:38:26 -0400 | [diff] [blame] | 338 | "uefi-macros": "uefi", |
| 339 | "uefi-services": "uefi", |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | # Map a package to a specific license and license file. Only use this if |
| 343 | # a package doesn't have an easily discoverable license or exports its |
| 344 | # license in a weird way. Prefer to patch the project with a license and |
| 345 | # upstream the patch instead. |
| 346 | STATIC_LICENSE_MAP = { |
George Burgess IV | b16816a | 2022-10-26 17:55:48 -0600 | [diff] [blame] | 347 | # "package name": ("license name", "license file relative location") |
George Burgess IV | 2664287 | 2022-10-18 19:46:58 -0600 | [diff] [blame] | 348 | # Patch for adding these are upstream, but the patch application |
| 349 | # doesn't apply to `cargo metadata`. This is presumably because it |
| 350 | # can't detect our vendor directory. |
George Burgess IV | f4a5e36 | 2022-08-30 14:30:36 -0700 | [diff] [blame] | 351 | # https://gitlab.freedesktop.org/slirp/libslirp-sys/-/merge_requests/6 |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 352 | "libslirp-sys": ("MIT", "LICENSE"), |
George Burgess IV | 2664287 | 2022-10-18 19:46:58 -0600 | [diff] [blame] | 353 | # https://gitlab.freedesktop.org/anholt/deqp-runner/-/merge_requests/48 |
| 354 | "deqp-runner": ("MIT", "LICENSE"), |
Dan Callaghan | 91f8054 | 2022-09-09 10:57:23 +1000 | [diff] [blame] | 355 | # Upstream prefers to embed license text inside README.md: |
| 356 | "riscv": ("ISC", "README.md"), |
| 357 | "riscv-rt": ("ISC", "README.md"), |
George Burgess IV | b16816a | 2022-10-26 17:55:48 -0600 | [diff] [blame] | 358 | "zerocopy": ("BSD-2", "LICENSE"), |
| 359 | "zerocopy-derive": ("BSD-2", "LICENSE"), |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | def __init__(self, working_dir, vendor_dir): |
| 363 | self.working_dir = working_dir |
| 364 | self.vendor_dir = vendor_dir |
| 365 | |
| 366 | def _find_license_in_dir(self, search_dir): |
| 367 | for p in os.listdir(search_dir): |
| 368 | # Ignore anything that's not a file |
| 369 | if not os.path.isfile(os.path.join(search_dir, p)): |
| 370 | continue |
| 371 | |
| 372 | # Now check if the name matches any of the regexes |
| 373 | # We'll return the first matching file. |
| 374 | for regex in self.LICENSE_NAMES_REGEX: |
| 375 | if re.search(regex, p, re.IGNORECASE): |
| 376 | yield os.path.join(search_dir, p) |
| 377 | break |
| 378 | |
| 379 | def _guess_license_type(self, license_file): |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 380 | if "-MIT" in license_file: |
| 381 | return "MIT" |
| 382 | elif "-APACHE" in license_file: |
| 383 | return "APACHE" |
| 384 | elif "-BSD" in license_file: |
| 385 | return "BSD-3" |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 386 | |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 387 | with open(license_file, "r") as f: |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 388 | lines = f.read() |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 389 | if "MIT" in lines: |
| 390 | return "MIT" |
| 391 | elif "Apache" in lines: |
| 392 | return "APACHE" |
| 393 | elif "BSD 3-Clause" in lines: |
| 394 | return "BSD-3" |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 395 | |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 396 | return "" |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 397 | |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 398 | def generate_license( |
| 399 | self, skip_license_check, print_map_to_file, license_shorthand_file |
| 400 | ): |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 401 | """Generate single massive license file from metadata.""" |
George Burgess IV | 18af563 | 2022-08-30 14:10:53 -0700 | [diff] [blame] | 402 | metadata = load_metadata(self.working_dir) |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 403 | |
George Burgess IV | b16816a | 2022-10-26 17:55:48 -0600 | [diff] [blame] | 404 | special_unicode_license = "(MIT OR Apache-2.0) AND Unicode-DFS-2016" |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 405 | bad_licenses = {} |
| 406 | |
| 407 | # Keep license map ordered so it generates a consistent license map |
| 408 | license_map = {} |
| 409 | |
| 410 | skip_license_check = skip_license_check or [] |
George Burgess IV | 4ae4206 | 2022-08-15 18:54:51 -0700 | [diff] [blame] | 411 | has_unicode_license = False |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 412 | |
George Burgess IV | 18af563 | 2022-08-30 14:10:53 -0700 | [diff] [blame] | 413 | for package in metadata["packages"]: |
George Burgess IV | 40cc91c | 2022-08-15 13:07:40 -0700 | [diff] [blame] | 414 | # Skip the synthesized Cargo.toml packages that exist solely to |
| 415 | # list dependencies. |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 416 | if "path+file:///" in package["id"]: |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 417 | continue |
| 418 | |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 419 | pkg_name = package["name"] |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 420 | if pkg_name in skip_license_check: |
| 421 | print( |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 422 | "Skipped license check on {}. Reason: Skipped from command line".format( |
| 423 | pkg_name |
| 424 | ) |
| 425 | ) |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 426 | continue |
| 427 | |
| 428 | if pkg_name in self.MAP_LICENSE_TO_OTHER: |
| 429 | print( |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 430 | "Skipped license check on {}. Reason: License already in {}".format( |
| 431 | pkg_name, self.MAP_LICENSE_TO_OTHER[pkg_name] |
| 432 | ) |
| 433 | ) |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 434 | continue |
| 435 | |
| 436 | # Check if we have a static license map for this package. Use the |
| 437 | # static values if we have it already set. |
| 438 | if pkg_name in self.STATIC_LICENSE_MAP: |
George Burgess IV | b16816a | 2022-10-26 17:55:48 -0600 | [diff] [blame] | 439 | license, license_file = self.STATIC_LICENSE_MAP[pkg_name] |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 440 | license_map[pkg_name] = { |
| 441 | "license": license, |
| 442 | "license_file": license_file, |
| 443 | } |
| 444 | continue |
| 445 | |
| 446 | license_files = [] |
George Burgess IV | 93ba473 | 2022-08-13 14:10:10 -0700 | [diff] [blame] | 447 | # use `or ''` instead of get's default, since `package` may have a |
| 448 | # None value for 'license'. |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 449 | license = package.get("license") or "" |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 450 | |
| 451 | # We ignore the metadata for license file because most crates don't |
| 452 | # have it set. Just scan the source for licenses. |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 453 | pkg_version = package["version"] |
| 454 | license_files = list( |
| 455 | self._find_license_in_dir( |
| 456 | os.path.join(self.vendor_dir, f"{pkg_name}-{pkg_version}") |
| 457 | ) |
| 458 | ) |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 459 | |
George Burgess IV | 4ae4206 | 2022-08-15 18:54:51 -0700 | [diff] [blame] | 460 | # FIXME(b/240953811): The code later in this loop is only |
| 461 | # structured to handle ORs, not ANDs. Fortunately, this license in |
| 462 | # particular is `AND`ed between a super common license (Apache) and |
| 463 | # a more obscure one (unicode). This hack is specifically intended |
| 464 | # for the `unicode-ident` crate, though no crate name check is |
| 465 | # made, since it's OK other crates happen to have this license. |
George Burgess IV | b16816a | 2022-10-26 17:55:48 -0600 | [diff] [blame] | 466 | if license == special_unicode_license: |
George Burgess IV | 4ae4206 | 2022-08-15 18:54:51 -0700 | [diff] [blame] | 467 | has_unicode_license = True |
| 468 | # We'll check later to be sure MIT or Apache-2.0 is represented |
| 469 | # properly. |
| 470 | for x in license_files: |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 471 | if os.path.basename(x) == "LICENSE-UNICODE": |
George Burgess IV | 4ae4206 | 2022-08-15 18:54:51 -0700 | [diff] [blame] | 472 | license_file = x |
| 473 | break |
| 474 | else: |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 475 | raise ValueError( |
| 476 | "No LICENSE-UNICODE found in " f"{license_files}" |
| 477 | ) |
George Burgess IV | 4ae4206 | 2022-08-15 18:54:51 -0700 | [diff] [blame] | 478 | license_map[pkg_name] = { |
| 479 | "license": license, |
| 480 | "license_file": license_file, |
| 481 | } |
George Burgess IV | 4ae4206 | 2022-08-15 18:54:51 -0700 | [diff] [blame] | 482 | continue |
| 483 | |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 484 | # If there are multiple licenses, they are delimited with "OR" or "/" |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 485 | delim = " OR " if " OR " in license else "/" |
George Burgess IV | 40cc91c | 2022-08-15 13:07:40 -0700 | [diff] [blame] | 486 | found = [x.strip() for x in license.split(delim)] |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 487 | |
| 488 | # Filter licenses to ones we support |
| 489 | licenses_or = [ |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 490 | self.SUPPORTED_LICENSES[f] |
| 491 | for f in found |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 492 | if f in self.SUPPORTED_LICENSES |
| 493 | ] |
| 494 | |
| 495 | # If apache license is found, always prefer it because it simplifies |
| 496 | # license attribution (we can use existing Apache notice) |
| 497 | if self.APACHE_LICENSE in licenses_or: |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 498 | license_map[pkg_name] = {"license": self.APACHE_LICENSE} |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 499 | |
| 500 | # Handle single license that has at least one license file |
| 501 | # We pick the first license file and the license |
| 502 | elif len(licenses_or) == 1: |
| 503 | if license_files: |
| 504 | l = licenses_or[0] |
| 505 | lf = license_files[0] |
| 506 | |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 507 | license_map[pkg_name] = { |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 508 | "license": l, |
| 509 | "license_file": os.path.relpath(lf, self.working_dir), |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 510 | } |
| 511 | else: |
| 512 | bad_licenses[pkg_name] = "{} missing license file".format( |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 513 | licenses_or[0] |
| 514 | ) |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 515 | # Handle multiple licenses |
| 516 | elif len(licenses_or) > 1: |
| 517 | # Check preferred licenses in order |
| 518 | license_found = False |
| 519 | for l in self.PREFERRED_ATTRIB_LICENSE_ORDER: |
| 520 | if not l in licenses_or: |
| 521 | continue |
| 522 | |
| 523 | for f in license_files: |
| 524 | if self._guess_license_type(f) == l: |
| 525 | license_found = True |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 526 | license_map[pkg_name] = { |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 527 | "license": l, |
| 528 | "license_file": os.path.relpath( |
| 529 | f, self.working_dir |
| 530 | ), |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 531 | } |
| 532 | break |
| 533 | |
| 534 | # Break out of loop if license is found |
| 535 | if license_found: |
| 536 | break |
| 537 | else: |
| 538 | bad_licenses[pkg_name] = license |
| 539 | |
| 540 | # If we had any bad licenses, we need to abort |
| 541 | if bad_licenses: |
| 542 | for k in bad_licenses.keys(): |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 543 | print( |
| 544 | "{} had no acceptable licenses: {}".format( |
| 545 | k, bad_licenses[k] |
| 546 | ) |
| 547 | ) |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 548 | raise Exception("Bad licenses in vendored packages.") |
| 549 | |
| 550 | # Write license map to file |
| 551 | if print_map_to_file: |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 552 | with open( |
| 553 | os.path.join(self.working_dir, print_map_to_file), "w" |
| 554 | ) as lfile: |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 555 | json.dump(license_map, lfile, sort_keys=True) |
| 556 | |
| 557 | # Raise missing licenses unless we have a valid reason to ignore them |
| 558 | raise_missing_license = False |
| 559 | for name, v in license_map.items(): |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 560 | if ( |
| 561 | "license_file" not in v |
| 562 | and v.get("license", "") != self.APACHE_LICENSE |
| 563 | ): |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 564 | raise_missing_license = True |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 565 | print( |
| 566 | " {}: Missing license file. Fix or add to ignorelist.".format( |
| 567 | name |
| 568 | ) |
| 569 | ) |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 570 | |
| 571 | if raise_missing_license: |
| 572 | raise Exception( |
| 573 | "Unhandled missing license file. " |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 574 | "Make sure all are accounted for before continuing." |
| 575 | ) |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 576 | |
George Burgess IV | b16816a | 2022-10-26 17:55:48 -0600 | [diff] [blame] | 577 | has_license_types = {x["license"] for x in license_map.values()} |
George Burgess IV | 4ae4206 | 2022-08-15 18:54:51 -0700 | [diff] [blame] | 578 | if has_unicode_license: |
George Burgess IV | b16816a | 2022-10-26 17:55:48 -0600 | [diff] [blame] | 579 | # Replace this license with the actual SPDX license we plan to use. |
| 580 | has_license_types.remove(special_unicode_license) |
| 581 | has_license_types.add("unicode") |
George Burgess IV | 4ae4206 | 2022-08-15 18:54:51 -0700 | [diff] [blame] | 582 | if self.APACHE_LICENSE not in has_license_types: |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 583 | raise ValueError( |
| 584 | "Need the apache license; currently have: " |
| 585 | f"{sorted(has_license_types)}" |
| 586 | ) |
George Burgess IV | 4ae4206 | 2022-08-15 18:54:51 -0700 | [diff] [blame] | 587 | |
George Burgess IV | 0483370 | 2022-08-09 22:00:38 -0700 | [diff] [blame] | 588 | sorted_licenses = sorted(has_license_types) |
George Burgess IV | 124e6a1 | 2022-09-09 10:44:29 -0700 | [diff] [blame] | 589 | print("The following licenses are in use:", sorted_licenses) |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 590 | header = textwrap.dedent( |
| 591 | """\ |
George Burgess IV | 0483370 | 2022-08-09 22:00:38 -0700 | [diff] [blame] | 592 | # File to describe the licenses used by this registry. |
Daniel Verkamp | d9d085b | 2022-09-07 10:52:27 -0700 | [diff] [blame] | 593 | # Used so it's easy to automatically verify ebuilds are updated. |
George Burgess IV | 0483370 | 2022-08-09 22:00:38 -0700 | [diff] [blame] | 594 | # Each line is a license. Lines starting with # are comments. |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 595 | """ |
| 596 | ) |
| 597 | with open(license_shorthand_file, "w", encoding="utf-8") as f: |
George Burgess IV | 0483370 | 2022-08-09 22:00:38 -0700 | [diff] [blame] | 598 | f.write(header) |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 599 | f.write("\n".join(sorted_licenses)) |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 600 | |
| 601 | |
Abhishek Pandit-Subedi | ce0f5b2 | 2021-09-10 15:50:08 -0700 | [diff] [blame] | 602 | # TODO(abps) - This needs to be replaced with datalog later. We should compile |
| 603 | # all crab files into datalog and query it with our requirements |
| 604 | # instead. |
| 605 | class CrabManager: |
| 606 | """Manage audit files.""" |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 607 | |
Abhishek Pandit-Subedi | ce0f5b2 | 2021-09-10 15:50:08 -0700 | [diff] [blame] | 608 | def __init__(self, working_dir, crab_dir): |
| 609 | self.working_dir = working_dir |
| 610 | self.crab_dir = crab_dir |
| 611 | |
| 612 | def _check_bad_traits(self, crabdata): |
| 613 | """Checks that a package's crab audit meets our requirements. |
| 614 | |
| 615 | Args: |
| 616 | crabdata: Dict with crab keys in standard templated format. |
| 617 | """ |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 618 | common = crabdata["common"] |
Abhishek Pandit-Subedi | ce0f5b2 | 2021-09-10 15:50:08 -0700 | [diff] [blame] | 619 | # TODO(b/200578411) - Figure out what conditions we should enforce as |
| 620 | # part of the audit. |
| 621 | conditions = [ |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 622 | common.get("deny", None), |
Abhishek Pandit-Subedi | ce0f5b2 | 2021-09-10 15:50:08 -0700 | [diff] [blame] | 623 | ] |
| 624 | |
| 625 | # If any conditions are true, this crate is not acceptable. |
| 626 | return any(conditions) |
| 627 | |
| 628 | def verify_traits(self): |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 629 | """Verify that all required CRAB traits for this repository are met.""" |
George Burgess IV | 18af563 | 2022-08-30 14:10:53 -0700 | [diff] [blame] | 630 | metadata = load_metadata(self.working_dir) |
Abhishek Pandit-Subedi | ce0f5b2 | 2021-09-10 15:50:08 -0700 | [diff] [blame] | 631 | |
| 632 | failing_crates = {} |
| 633 | |
| 634 | # Verify all packages have a CRAB file associated with it and they meet |
| 635 | # all our required traits |
George Burgess IV | 18af563 | 2022-08-30 14:10:53 -0700 | [diff] [blame] | 636 | for package in metadata["packages"]: |
George Burgess IV | 40cc91c | 2022-08-15 13:07:40 -0700 | [diff] [blame] | 637 | # Skip the synthesized Cargo.toml packages that exist solely to |
| 638 | # list dependencies. |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 639 | if "path+file:///" in package["id"]: |
Abhishek Pandit-Subedi | ce0f5b2 | 2021-09-10 15:50:08 -0700 | [diff] [blame] | 640 | continue |
| 641 | |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 642 | crabname = "{}-{}".format(package["name"], package["version"]) |
Abhishek Pandit-Subedi | ce0f5b2 | 2021-09-10 15:50:08 -0700 | [diff] [blame] | 643 | filename = os.path.join(self.crab_dir, "{}.toml".format(crabname)) |
| 644 | |
| 645 | # If crab file doesn't exist, the crate fails |
| 646 | if not os.path.isfile(filename): |
| 647 | failing_crates[crabname] = "No crab file".format(filename) |
| 648 | continue |
| 649 | |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 650 | with open(filename, "r") as f: |
Abhishek Pandit-Subedi | ce0f5b2 | 2021-09-10 15:50:08 -0700 | [diff] [blame] | 651 | crabdata = toml.loads(f.read()) |
| 652 | |
| 653 | # If crab file's crate_name and version keys don't match this |
| 654 | # package, it also fails. This is just housekeeping... |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 655 | if ( |
| 656 | package["name"] != crabdata["crate_name"] |
| 657 | or package["version"] != crabdata["version"] |
| 658 | ): |
Abhishek Pandit-Subedi | ce0f5b2 | 2021-09-10 15:50:08 -0700 | [diff] [blame] | 659 | failing_crates[crabname] = "Crate name or version don't match" |
| 660 | continue |
| 661 | |
| 662 | if self._check_bad_traits(crabdata): |
| 663 | failing_crates[crabname] = "Failed bad traits check" |
| 664 | |
George Burgess IV | 9e0cfde | 2022-09-27 15:08:15 -0700 | [diff] [blame] | 665 | # If we had any failing crates, list them now, and exit with an error. |
Abhishek Pandit-Subedi | ce0f5b2 | 2021-09-10 15:50:08 -0700 | [diff] [blame] | 666 | if failing_crates: |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 667 | print("Failed CRAB audit:") |
Abhishek Pandit-Subedi | ce0f5b2 | 2021-09-10 15:50:08 -0700 | [diff] [blame] | 668 | for k, v in failing_crates.items(): |
George Burgess IV | 9e0cfde | 2022-09-27 15:08:15 -0700 | [diff] [blame] | 669 | print(f" {k}: {v}") |
| 670 | raise ValueError("CRAB audit did not complete successfully.") |
Abhishek Pandit-Subedi | ce0f5b2 | 2021-09-10 15:50:08 -0700 | [diff] [blame] | 671 | |
| 672 | |
George Burgess IV | d026147 | 2022-10-17 18:59:10 -0600 | [diff] [blame] | 673 | def clean_source_related_lines_in_place(cargo_toml): |
| 674 | """Removes all [[bin]] (and similar) sections in `cargo_toml`.""" |
| 675 | cargo_toml.pop("bench", None) |
| 676 | cargo_toml.pop("bin", None) |
| 677 | cargo_toml.pop("examples", None) |
| 678 | cargo_toml.pop("test", None) |
| 679 | |
| 680 | lib = cargo_toml.get("lib") |
| 681 | if lib: |
| 682 | lib.pop("path", None) |
| 683 | |
| 684 | package = cargo_toml.get("package") |
| 685 | if package: |
| 686 | package.pop("build", None) |
| 687 | package.pop("default-run", None) |
| 688 | package.pop("include", None) |
| 689 | |
| 690 | |
George Burgess IV | d4ff050 | 2022-08-14 23:27:57 -0700 | [diff] [blame] | 691 | def clean_features_in_place(cargo_toml): |
| 692 | """Removes all side-effects of features in `cargo_toml`.""" |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 693 | features = cargo_toml.get("features") |
George Burgess IV | d4ff050 | 2022-08-14 23:27:57 -0700 | [diff] [blame] | 694 | if not features: |
| 695 | return |
| 696 | |
George Burgess IV | d026147 | 2022-10-17 18:59:10 -0600 | [diff] [blame] | 697 | for name in features: |
| 698 | features[name] = [] |
George Burgess IV | d4ff050 | 2022-08-14 23:27:57 -0700 | [diff] [blame] | 699 | |
| 700 | |
George Burgess IV | d026147 | 2022-10-17 18:59:10 -0600 | [diff] [blame] | 701 | def remove_all_dependencies_in_place(cargo_toml): |
George Burgess IV | d4ff050 | 2022-08-14 23:27:57 -0700 | [diff] [blame] | 702 | """Removes all `target.*.dependencies` from `cargo_toml`.""" |
George Burgess IV | d026147 | 2022-10-17 18:59:10 -0600 | [diff] [blame] | 703 | cargo_toml.pop("build-dependencies", None) |
| 704 | cargo_toml.pop("dependencies", None) |
| 705 | cargo_toml.pop("dev-dependencies", None) |
| 706 | |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 707 | target = cargo_toml.get("target") |
George Burgess IV | d4ff050 | 2022-08-14 23:27:57 -0700 | [diff] [blame] | 708 | if not target: |
| 709 | return |
George Burgess IV | 0313d78 | 2022-08-15 23:45:44 -0700 | [diff] [blame] | 710 | |
George Burgess IV | d4ff050 | 2022-08-14 23:27:57 -0700 | [diff] [blame] | 711 | empty_keys = [] |
| 712 | for key, values in target.items(): |
George Burgess IV | d026147 | 2022-10-17 18:59:10 -0600 | [diff] [blame] | 713 | values.pop("build-dependencies", None) |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 714 | values.pop("dependencies", None) |
| 715 | values.pop("dev-dependencies", None) |
George Burgess IV | d4ff050 | 2022-08-14 23:27:57 -0700 | [diff] [blame] | 716 | if not values: |
| 717 | empty_keys.append(key) |
George Burgess IV | 0313d78 | 2022-08-15 23:45:44 -0700 | [diff] [blame] | 718 | |
George Burgess IV | d4ff050 | 2022-08-14 23:27:57 -0700 | [diff] [blame] | 719 | if len(empty_keys) == len(target): |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 720 | del cargo_toml["target"] |
George Burgess IV | d4ff050 | 2022-08-14 23:27:57 -0700 | [diff] [blame] | 721 | else: |
| 722 | for key in empty_keys: |
| 723 | del target[key] |
George Burgess IV | 0313d78 | 2022-08-15 23:45:44 -0700 | [diff] [blame] | 724 | |
| 725 | |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 726 | class CrateDestroyer: |
Abhishek Pandit-Subedi | f0eb6e0 | 2021-09-24 16:36:12 -0700 | [diff] [blame] | 727 | def __init__(self, working_dir, vendor_dir): |
| 728 | self.working_dir = working_dir |
| 729 | self.vendor_dir = vendor_dir |
| 730 | |
| 731 | def _modify_cargo_toml(self, pkg_path): |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 732 | with open(os.path.join(pkg_path, "Cargo.toml"), "r") as cargo: |
Abhishek Pandit-Subedi | f0eb6e0 | 2021-09-24 16:36:12 -0700 | [diff] [blame] | 733 | contents = toml.load(cargo) |
| 734 | |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 735 | package = contents["package"] |
George Burgess IV | d4ff050 | 2022-08-14 23:27:57 -0700 | [diff] [blame] | 736 | |
Abhishek Pandit-Subedi | f0eb6e0 | 2021-09-24 16:36:12 -0700 | [diff] [blame] | 737 | # Change description, license and delete license key |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 738 | package["description"] = "Empty crate that should not build." |
| 739 | package["license"] = "Apache-2.0" |
George Burgess IV | d4ff050 | 2022-08-14 23:27:57 -0700 | [diff] [blame] | 740 | |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 741 | package.pop("license_file", None) |
George Burgess IV | d4ff050 | 2022-08-14 23:27:57 -0700 | [diff] [blame] | 742 | # If there's no build.rs but we specify `links = "foo"`, Cargo gets |
| 743 | # upset. |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 744 | package.pop("links", None) |
Abhishek Pandit-Subedi | f0eb6e0 | 2021-09-24 16:36:12 -0700 | [diff] [blame] | 745 | |
George Burgess IV | 0313d78 | 2022-08-15 23:45:44 -0700 | [diff] [blame] | 746 | # Some packages have cfg-specific dependencies. Remove them here; we |
| 747 | # don't care about the dependencies of an empty package. |
| 748 | # |
| 749 | # This is a load-bearing optimization: `dev-python/toml` doesn't |
| 750 | # always round-trip dumps(loads(x)) correctly when `x` has keys with |
| 751 | # strings (b/242589711#comment3). The place this has bitten us so far |
| 752 | # is target dependencies, which can be harmlessly removed for now. |
George Burgess IV | d4ff050 | 2022-08-14 23:27:57 -0700 | [diff] [blame] | 753 | # |
| 754 | # Cleaning features in-place is also necessary, since we're removing |
| 755 | # dependencies, and a feature can enable features in dependencies. |
| 756 | # Cargo errors out on `[features] foo = "bar/baz"` if `bar` isn't a |
| 757 | # dependency. |
| 758 | clean_features_in_place(contents) |
George Burgess IV | d026147 | 2022-10-17 18:59:10 -0600 | [diff] [blame] | 759 | remove_all_dependencies_in_place(contents) |
| 760 | |
| 761 | # Since we're removing all source files, also be sure to remove |
| 762 | # source-related keys. |
| 763 | clean_source_related_lines_in_place(contents) |
George Burgess IV | 0313d78 | 2022-08-15 23:45:44 -0700 | [diff] [blame] | 764 | |
Abhishek Pandit-Subedi | f0eb6e0 | 2021-09-24 16:36:12 -0700 | [diff] [blame] | 765 | with open(os.path.join(pkg_path, "Cargo.toml"), "w") as cargo: |
| 766 | toml.dump(contents, cargo) |
| 767 | |
George Burgess IV | 8e2cc04 | 2022-10-18 14:50:48 -0600 | [diff] [blame] | 768 | def _replace_source_contents(self, package_path, compile_error): |
Abhishek Pandit-Subedi | f0eb6e0 | 2021-09-24 16:36:12 -0700 | [diff] [blame] | 769 | # First load the checksum file before starting |
| 770 | checksum_file = os.path.join(package_path, ".cargo-checksum.json") |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 771 | with open(checksum_file, "r") as csum: |
Abhishek Pandit-Subedi | f0eb6e0 | 2021-09-24 16:36:12 -0700 | [diff] [blame] | 772 | checksum_contents = json.load(csum) |
| 773 | |
| 774 | # Also load the cargo.toml file which we need to write back |
| 775 | cargo_file = os.path.join(package_path, "Cargo.toml") |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 776 | with open(cargo_file, "rb") as cfile: |
George Burgess IV | 3e344e4 | 2022-08-09 21:07:04 -0700 | [diff] [blame] | 777 | cargo_contents = cfile.read() |
Abhishek Pandit-Subedi | f0eb6e0 | 2021-09-24 16:36:12 -0700 | [diff] [blame] | 778 | |
| 779 | shutil.rmtree(package_path) |
| 780 | |
| 781 | # Make package and src dirs and replace lib.rs |
| 782 | os.makedirs(os.path.join(package_path, "src"), exist_ok=True) |
| 783 | with open(os.path.join(package_path, "src", "lib.rs"), "w") as librs: |
George Burgess IV | 8e2cc04 | 2022-10-18 14:50:48 -0600 | [diff] [blame] | 784 | librs.write( |
| 785 | EMPTY_CRATE_BODY if compile_error else NOP_EMPTY_CRATE_BODY |
| 786 | ) |
Abhishek Pandit-Subedi | f0eb6e0 | 2021-09-24 16:36:12 -0700 | [diff] [blame] | 787 | |
| 788 | # Restore cargo.toml |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 789 | with open(cargo_file, "wb") as cfile: |
George Burgess IV | 3e344e4 | 2022-08-09 21:07:04 -0700 | [diff] [blame] | 790 | cfile.write(cargo_contents) |
Abhishek Pandit-Subedi | f0eb6e0 | 2021-09-24 16:36:12 -0700 | [diff] [blame] | 791 | |
| 792 | # Restore checksum |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 793 | with open(checksum_file, "w") as csum: |
Abhishek Pandit-Subedi | f0eb6e0 | 2021-09-24 16:36:12 -0700 | [diff] [blame] | 794 | json.dump(checksum_contents, csum) |
| 795 | |
| 796 | def destroy_unused_crates(self): |
George Burgess IV | 18af563 | 2022-08-30 14:10:53 -0700 | [diff] [blame] | 797 | metadata = load_metadata(self.working_dir, filter_platform=None) |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 798 | used_packages = { |
| 799 | p["name"] for p in load_metadata(self.working_dir)["packages"] |
| 800 | } |
Abhishek Pandit-Subedi | f0eb6e0 | 2021-09-24 16:36:12 -0700 | [diff] [blame] | 801 | |
| 802 | cleaned_packages = [] |
George Burgess IV | 40cc91c | 2022-08-15 13:07:40 -0700 | [diff] [blame] | 803 | # Since we're asking for _all_ metadata packages, we may see |
| 804 | # duplication. |
George Burgess IV | 18af563 | 2022-08-30 14:10:53 -0700 | [diff] [blame] | 805 | for package in metadata["packages"]: |
Abhishek Pandit-Subedi | f0eb6e0 | 2021-09-24 16:36:12 -0700 | [diff] [blame] | 806 | # Skip used packages |
George Burgess IV | 8e2cc04 | 2022-10-18 14:50:48 -0600 | [diff] [blame] | 807 | package_name = package["name"] |
| 808 | if package_name in used_packages: |
Abhishek Pandit-Subedi | f0eb6e0 | 2021-09-24 16:36:12 -0700 | [diff] [blame] | 809 | continue |
| 810 | |
| 811 | # Detect the correct package path to destroy |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 812 | pkg_path = os.path.join( |
| 813 | self.vendor_dir, |
George Burgess IV | 8e2cc04 | 2022-10-18 14:50:48 -0600 | [diff] [blame] | 814 | "{}-{}".format(package_name, package["version"]), |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 815 | ) |
Abhishek Pandit-Subedi | f0eb6e0 | 2021-09-24 16:36:12 -0700 | [diff] [blame] | 816 | if not os.path.isdir(pkg_path): |
George Burgess IV | 8e2cc04 | 2022-10-18 14:50:48 -0600 | [diff] [blame] | 817 | print(f"Crate {package_name} not found at {pkg_path}") |
George Burgess IV | 635f726 | 2022-08-09 21:32:20 -0700 | [diff] [blame] | 818 | continue |
Abhishek Pandit-Subedi | f0eb6e0 | 2021-09-24 16:36:12 -0700 | [diff] [blame] | 819 | |
George Burgess IV | 8e2cc04 | 2022-10-18 14:50:48 -0600 | [diff] [blame] | 820 | self._replace_source_contents( |
| 821 | pkg_path, compile_error=package_name not in NOP_EMPTY_CRATES |
| 822 | ) |
Abhishek Pandit-Subedi | f0eb6e0 | 2021-09-24 16:36:12 -0700 | [diff] [blame] | 823 | self._modify_cargo_toml(pkg_path) |
| 824 | _rerun_checksums(pkg_path) |
| 825 | cleaned_packages.append(package["name"]) |
| 826 | |
| 827 | for pkg in cleaned_packages: |
George Burgess IV | 635f726 | 2022-08-09 21:32:20 -0700 | [diff] [blame] | 828 | print("Removed unused crate", pkg) |
Abhishek Pandit-Subedi | f0eb6e0 | 2021-09-24 16:36:12 -0700 | [diff] [blame] | 829 | |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 830 | |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 831 | def main(args): |
Abhishek Pandit-Subedi | b75bd56 | 2021-02-25 15:32:22 -0800 | [diff] [blame] | 832 | current_path = pathlib.Path(__file__).parent.absolute() |
Abhishek Pandit-Subedi | 5065a0f | 2021-06-13 20:38:55 +0000 | [diff] [blame] | 833 | patches = os.path.join(current_path, "patches") |
| 834 | vendor = os.path.join(current_path, "vendor") |
Abhishek Pandit-Subedi | ce0f5b2 | 2021-09-10 15:50:08 -0700 | [diff] [blame] | 835 | crab_dir = os.path.join(current_path, "crab", "crates") |
George Burgess IV | 0483370 | 2022-08-09 22:00:38 -0700 | [diff] [blame] | 836 | license_shorthand_file = os.path.join(current_path, "licenses_used.txt") |
Abhishek Pandit-Subedi | b75bd56 | 2021-02-25 15:32:22 -0800 | [diff] [blame] | 837 | |
Abhishek Pandit-Subedi | fa90238 | 2021-08-20 11:04:33 -0700 | [diff] [blame] | 838 | # First, actually run cargo vendor |
| 839 | run_cargo_vendor(current_path) |
| 840 | |
Abhishek Pandit-Subedi | 5065a0f | 2021-06-13 20:38:55 +0000 | [diff] [blame] | 841 | # Order matters here: |
| 842 | # - Apply patches (also re-calculates checksums) |
| 843 | # - Cleanup any owners files (otherwise, git check-in or checksums are |
| 844 | # unhappy) |
Abhishek Pandit-Subedi | f0eb6e0 | 2021-09-24 16:36:12 -0700 | [diff] [blame] | 845 | # - Destroy unused crates |
Abhishek Pandit-Subedi | 5065a0f | 2021-06-13 20:38:55 +0000 | [diff] [blame] | 846 | apply_patches(patches, vendor) |
| 847 | cleanup_owners(vendor) |
Abhishek Pandit-Subedi | f0eb6e0 | 2021-09-24 16:36:12 -0700 | [diff] [blame] | 848 | destroyer = CrateDestroyer(current_path, vendor) |
| 849 | destroyer.destroy_unused_crates() |
Abhishek Pandit-Subedi | b75bd56 | 2021-02-25 15:32:22 -0800 | [diff] [blame] | 850 | |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 851 | # Combine license file and check for any bad licenses |
| 852 | lm = LicenseManager(current_path, vendor) |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 853 | lm.generate_license( |
| 854 | args.skip_license_check, args.license_map, license_shorthand_file |
| 855 | ) |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 856 | |
Abhishek Pandit-Subedi | ce0f5b2 | 2021-09-10 15:50:08 -0700 | [diff] [blame] | 857 | # Run crab audit on all packages |
| 858 | crab = CrabManager(current_path, crab_dir) |
| 859 | crab.verify_traits() |
| 860 | |
Abhishek Pandit-Subedi | b75bd56 | 2021-02-25 15:32:22 -0800 | [diff] [blame] | 861 | |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame] | 862 | if __name__ == "__main__": |
| 863 | parser = argparse.ArgumentParser(description="Vendor packages properly") |
| 864 | parser.add_argument( |
| 865 | "--skip-license-check", |
| 866 | "-s", |
| 867 | help="Skip the license check on a specific package", |
| 868 | action="append", |
| 869 | ) |
| 870 | parser.add_argument("--license-map", help="Write license map to this file") |
Abhishek Pandit-Subedi | e393cb7 | 2021-08-22 10:41:13 -0700 | [diff] [blame] | 871 | args = parser.parse_args() |
| 872 | |
| 873 | main(args) |