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