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