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