blob: 14459fcf82a457a613f4f3ddb5b16fe2b78f034d [file] [log] [blame]
Abhishek Pandit-Subedib75bd562021-02-25 15:32:22 -08001#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
George Burgess IV9e0cfde2022-09-27 15:08:15 -07003# Copyright 2021 The ChromiumOS Authors
Abhishek Pandit-Subedib75bd562021-02-25 15:32:22 -08004# 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-Subedie393cb72021-08-22 10:41:13 -07008import argparse
George Burgess IV635f7262022-08-09 21:32:20 -07009import collections
Abhishek Pandit-Subedi5065a0f2021-06-13 20:38:55 +000010import hashlib
Abhishek Pandit-Subedib75bd562021-02-25 15:32:22 -080011import json
12import os
13import pathlib
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -070014import re
Abhishek Pandit-Subedif0eb6e02021-09-24 16:36:12 -070015import shutil
Abhishek Pandit-Subedi5065a0f2021-06-13 20:38:55 +000016import subprocess
George Burgess IV04833702022-08-09 22:00:38 -070017import textwrap
Abhishek Pandit-Subedice0f5b22021-09-10 15:50:08 -070018import toml
Abhishek Pandit-Subedi5065a0f2021-06-13 20:38:55 +000019
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -070020# 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
23DEFAULT_PLATFORM_FILTER = "x86_64-unknown-linux-gnu"
24
George Burgess IV8e2cc042022-10-18 14:50:48 -060025# A series of crates which are to be made empty by having no (non-comment)
26# contents in their `lib.rs`, rather than by inserting a compilation error.
27NOP_EMPTY_CRATES = frozenset({"windows"})
28
29EMPTY_CRATE_BODY = """\
30compile_error!("This crate cannot be built for this configuration.");
31"""
32NOP_EMPTY_CRATE_BODY = "// " + EMPTY_CRATE_BODY
33
Abhishek Pandit-Subedi5065a0f2021-06-13 20:38:55 +000034
35def _rerun_checksums(package_path):
36 """Re-run checksums for given package.
37
38 Writes resulting checksums to $package_path/.cargo-checksum.json.
39 """
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -070040 hashes = dict()
George Burgess IV7dffc252022-08-31 14:37:01 -070041 checksum_path = os.path.join(package_path, ".cargo-checksum.json")
Abhishek Pandit-Subedi5065a0f2021-06-13 20:38:55 +000042 if not pathlib.Path(checksum_path).is_file():
43 return False
44
George Burgess IV7dffc252022-08-31 14:37:01 -070045 with open(checksum_path, "r") as fread:
Abhishek Pandit-Subedi5065a0f2021-06-13 20:38:55 +000046 contents = json.load(fread)
47
48 for root, _, files in os.walk(package_path, topdown=True):
49 for f in files:
50 # Don't checksum an existing checksum file
51 if f == ".cargo-checksum.json":
52 continue
53
54 file_path = os.path.join(root, f)
George Burgess IV7dffc252022-08-31 14:37:01 -070055 with open(file_path, "rb") as frb:
Abhishek Pandit-Subedi5065a0f2021-06-13 20:38:55 +000056 m = hashlib.sha256()
57 m.update(frb.read())
58 d = m.hexdigest()
59
60 # Key is relative to the package path so strip from beginning
61 key = os.path.relpath(file_path, package_path)
62 hashes[key] = d
63
64 if hashes:
George Burgess IV7dffc252022-08-31 14:37:01 -070065 print(
66 "{} regenerated {} hashes".format(package_path, len(hashes.keys()))
67 )
68 contents["files"] = hashes
69 with open(checksum_path, "w") as fwrite:
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -070070 json.dump(contents, fwrite, sort_keys=True)
Abhishek Pandit-Subedi5065a0f2021-06-13 20:38:55 +000071
72 return True
Abhishek Pandit-Subedib75bd562021-02-25 15:32:22 -080073
74
75def _remove_OWNERS_checksum(root):
George Burgess IV7dffc252022-08-31 14:37:01 -070076 """Delete all OWNERS files from the checksum file.
Abhishek Pandit-Subedib75bd562021-02-25 15:32:22 -080077
Abhishek Pandit-Subedi5065a0f2021-06-13 20:38:55 +000078 Args:
79 root: Root directory for the vendored crate.
Abhishek Pandit-Subedib75bd562021-02-25 15:32:22 -080080
Abhishek Pandit-Subedi5065a0f2021-06-13 20:38:55 +000081 Returns:
82 True if OWNERS was found and cleaned up. Otherwise False.
83 """
George Burgess IV7dffc252022-08-31 14:37:01 -070084 checksum_path = os.path.join(root, ".cargo-checksum.json")
Abhishek Pandit-Subedib75bd562021-02-25 15:32:22 -080085 if not pathlib.Path(checksum_path).is_file():
86 return False
87
George Burgess IV7dffc252022-08-31 14:37:01 -070088 with open(checksum_path, "r") as fread:
Abhishek Pandit-Subedib75bd562021-02-25 15:32:22 -080089 contents = json.load(fread)
90
91 del_keys = []
George Burgess IV7dffc252022-08-31 14:37:01 -070092 for cfile in contents["files"]:
93 if "OWNERS" in cfile:
Abhishek Pandit-Subedib75bd562021-02-25 15:32:22 -080094 del_keys.append(cfile)
95
96 for key in del_keys:
George Burgess IV7dffc252022-08-31 14:37:01 -070097 del contents["files"][key]
Abhishek Pandit-Subedib75bd562021-02-25 15:32:22 -080098
99 if del_keys:
George Burgess IV7dffc252022-08-31 14:37:01 -0700100 print("{} deleted: {}".format(root, del_keys))
101 with open(checksum_path, "w") as fwrite:
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700102 json.dump(contents, fwrite, sort_keys=True)
Abhishek Pandit-Subedib75bd562021-02-25 15:32:22 -0800103
104 return bool(del_keys)
105
106
107def cleanup_owners(vendor_path):
George Burgess IV7dffc252022-08-31 14:37:01 -0700108 """Remove owners checksums from the vendor directory.
Abhishek Pandit-Subedib75bd562021-02-25 15:32:22 -0800109
Abhishek Pandit-Subedi5065a0f2021-06-13 20:38:55 +0000110 We currently do not check in the OWNERS files from vendored crates because
111 they interfere with the find-owners functionality in gerrit. This cleanup
112 simply finds all instances of "OWNERS" in the checksum files within and
113 removes them.
Abhishek Pandit-Subedib75bd562021-02-25 15:32:22 -0800114
Abhishek Pandit-Subedi5065a0f2021-06-13 20:38:55 +0000115 Args:
116 vendor_path: Absolute path to vendor directory.
117 """
Abhishek Pandit-Subedib75bd562021-02-25 15:32:22 -0800118 deps_cleaned = []
119 for root, dirs, _ in os.walk(vendor_path):
120 for d in dirs:
121 removed = _remove_OWNERS_checksum(os.path.join(root, d))
122 if removed:
123 deps_cleaned.append(d)
124
125 if deps_cleaned:
George Burgess IV7dffc252022-08-31 14:37:01 -0700126 print("Cleanup owners:\n {}".format("\n".join(deps_cleaned)))
Abhishek Pandit-Subedib75bd562021-02-25 15:32:22 -0800127
128
Abhishek Pandit-Subedi5065a0f2021-06-13 20:38:55 +0000129def apply_single_patch(patch, workdir):
130 """Apply a single patch and return whether it was successful.
131
132 Returns:
133 True if successful. False otherwise.
134 """
George Burgess IV08664ba2022-10-03 11:09:33 -0700135 proc = subprocess.run(
136 [
137 "patch",
138 "-p1",
139 "--no-backup-if-mismatch",
140 "-i",
141 patch,
142 ],
143 cwd=workdir,
144 )
Abhishek Pandit-Subedi5065a0f2021-06-13 20:38:55 +0000145 return proc.returncode == 0
146
147
George Burgess IV30c5c362022-08-19 17:05:02 -0700148def apply_patch_script(script, workdir):
149 """Run the given patch script, returning whether it exited cleanly.
150
151 Returns:
152 True if successful. False otherwise.
153 """
154 return subprocess.run([script], cwd=workdir).returncode == 0
155
156
George Burgess IV635f7262022-08-09 21:32:20 -0700157def determine_vendor_crates(vendor_path):
158 """Returns a map of {crate_name: [directory]} at the given vendor_path."""
159 result = collections.defaultdict(list)
160 for crate_name_plus_ver in os.listdir(vendor_path):
George Burgess IV7dffc252022-08-31 14:37:01 -0700161 name, _ = crate_name_plus_ver.rsplit("-", 1)
George Burgess IV40cc91c2022-08-15 13:07:40 -0700162 result[name].append(crate_name_plus_ver)
George Burgess IV635f7262022-08-09 21:32:20 -0700163
164 for crate_list in result.values():
George Burgess IV40cc91c2022-08-15 13:07:40 -0700165 crate_list.sort()
George Burgess IV635f7262022-08-09 21:32:20 -0700166 return result
167
168
Abhishek Pandit-Subedi5065a0f2021-06-13 20:38:55 +0000169def apply_patches(patches_path, vendor_path):
170 """Finds patches and applies them to sub-folders in the vendored crates.
171
172 Args:
173 patches_path: Path to folder with patches. Expect all patches to be one
174 level down (matching the crate name).
175 vendor_path: Root path to vendored crates directory.
176 """
177 checksums_for = {}
178
179 # Don't bother running if patches directory is empty
180 if not pathlib.Path(patches_path).is_dir():
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700181 return
Abhishek Pandit-Subedi5065a0f2021-06-13 20:38:55 +0000182
George Burgess IV30c5c362022-08-19 17:05:02 -0700183 patches_failed = False
George Burgess IV635f7262022-08-09 21:32:20 -0700184 vendor_crate_map = determine_vendor_crates(vendor_path)
Abhishek Pandit-Subedi5065a0f2021-06-13 20:38:55 +0000185 # Look for all patches and apply them
186 for d in os.listdir(patches_path):
187 dir_path = os.path.join(patches_path, d)
188
189 # We don't process patches in root dir
190 if not os.path.isdir(dir_path):
191 continue
192
George Burgess IV30c5c362022-08-19 17:05:02 -0700193 # We accept one of two forms here:
194 # - direct targets (these name # `${crate_name}-${version}`)
195 # - simply the crate name (which applies to all versions of the
196 # crate)
197 direct_target = os.path.join(vendor_path, d)
198 if os.path.isdir(direct_target):
199 patch_targets = [d]
200 elif d in vendor_crate_map:
201 patch_targets = vendor_crate_map[d]
202 else:
George Burgess IV7dffc252022-08-31 14:37:01 -0700203 raise RuntimeError(f"Unknown crate in {vendor_path}: {d}")
George Burgess IV30c5c362022-08-19 17:05:02 -0700204
George Burgess IV635f7262022-08-09 21:32:20 -0700205 for patch in os.listdir(dir_path):
Abhishek Pandit-Subedi5065a0f2021-06-13 20:38:55 +0000206 file_path = os.path.join(dir_path, patch)
207
208 # Skip if not a patch file
George Burgess IV30c5c362022-08-19 17:05:02 -0700209 if not os.path.isfile(file_path):
Abhishek Pandit-Subedi5065a0f2021-06-13 20:38:55 +0000210 continue
211
George Burgess IV30c5c362022-08-19 17:05:02 -0700212 if patch.endswith(".patch"):
213 apply = apply_single_patch
214 elif os.access(file_path, os.X_OK):
215 apply = apply_patch_script
George Burgess IV635f7262022-08-09 21:32:20 -0700216 else:
George Burgess IV30c5c362022-08-19 17:05:02 -0700217 # Unrecognized. Skip it.
218 continue
219
220 for target_name in patch_targets:
221 checksums_for[target_name] = True
222 target = os.path.join(vendor_path, target_name)
223 print(f"-- Applying {file_path} to {target}")
224 if not apply(file_path, target):
225 print(f"Failed to apply {file_path} to {target}")
226 patches_failed = True
227
228 # Do this late, so we can report all of the failing patches in one
229 # invocation.
230 if patches_failed:
George Burgess IV7dffc252022-08-31 14:37:01 -0700231 raise ValueError("Patches failed; please see above logs")
Abhishek Pandit-Subedi5065a0f2021-06-13 20:38:55 +0000232
Abhishek Pandit-Subedi5065a0f2021-06-13 20:38:55 +0000233 # Re-run checksums for all modified packages since we applied patches.
234 for key in checksums_for.keys():
235 _rerun_checksums(os.path.join(vendor_path, key))
236
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700237
George Burgess IV18af5632022-08-30 14:10:53 -0700238def get_workspace_cargo_toml(working_dir):
George Burgess IV40cc91c2022-08-15 13:07:40 -0700239 """Returns all Cargo.toml files under working_dir."""
George Burgess IV7dffc252022-08-31 14:37:01 -0700240 return [working_dir / "projects" / "Cargo.toml"]
George Burgess IV40cc91c2022-08-15 13:07:40 -0700241
242
Abhishek Pandit-Subedifa902382021-08-20 11:04:33 -0700243def run_cargo_vendor(working_dir):
244 """Runs cargo vendor.
245
246 Args:
247 working_dir: Directory to run inside. This should be the directory where
Abhishek Pandit-Subedice0f5b22021-09-10 15:50:08 -0700248 Cargo.toml is kept.
Abhishek Pandit-Subedifa902382021-08-20 11:04:33 -0700249 """
George Burgess IV635f7262022-08-09 21:32:20 -0700250 # Cargo will refuse to revendor into versioned directories, which leads to
251 # repeated `./vendor.py` invocations trying to apply patches to
252 # already-patched sources. Remove the existing vendor directory to avoid
253 # this.
George Burgess IV7dffc252022-08-31 14:37:01 -0700254 vendor_dir = working_dir / "vendor"
George Burgess IV635f7262022-08-09 21:32:20 -0700255 if vendor_dir.exists():
George Burgess IV40cc91c2022-08-15 13:07:40 -0700256 shutil.rmtree(vendor_dir)
257
George Burgess IV18af5632022-08-30 14:10:53 -0700258 cargo_cmdline = [
George Burgess IV7dffc252022-08-31 14:37:01 -0700259 "cargo",
260 "vendor",
261 "--versioned-dirs",
262 "-v",
263 "--manifest-path=projects/Cargo.toml",
264 "--",
265 "vendor",
George Burgess IV18af5632022-08-30 14:10:53 -0700266 ]
George Burgess IV40cc91c2022-08-15 13:07:40 -0700267 subprocess.check_call(cargo_cmdline, cwd=working_dir)
Abhishek Pandit-Subedi5065a0f2021-06-13 20:38:55 +0000268
Abhishek Pandit-Subedice0f5b22021-09-10 15:50:08 -0700269
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700270def load_metadata(working_dir, filter_platform=DEFAULT_PLATFORM_FILTER):
George Burgess IV40cc91c2022-08-15 13:07:40 -0700271 """Load metadata for all projects under a given directory.
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700272
273 Args:
George Burgess IV40cc91c2022-08-15 13:07:40 -0700274 working_dir: Base directory to run from.
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700275 filter_platform: Filter packages to ones configured for this platform.
276 """
George Burgess IV40cc91c2022-08-15 13:07:40 -0700277 metadata_objects = []
George Burgess IV18af5632022-08-30 14:10:53 -0700278 cmd = [
George Burgess IV7dffc252022-08-31 14:37:01 -0700279 "cargo",
280 "metadata",
281 "--format-version=1",
282 "--manifest-path=projects/Cargo.toml",
George Burgess IV18af5632022-08-30 14:10:53 -0700283 ]
284 # Conditionally add platform filter
285 if filter_platform:
286 cmd += ("--filter-platform", filter_platform)
287 output = subprocess.check_output(cmd, cwd=working_dir)
288 return json.loads(output)
Abhishek Pandit-Subedif0eb6e02021-09-24 16:36:12 -0700289
Abhishek Pandit-Subedif0eb6e02021-09-24 16:36:12 -0700290
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700291class LicenseManager:
George Burgess IV7dffc252022-08-31 14:37:01 -0700292 """Manage consolidating licenses for all packages."""
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700293
George Burgess IV124e6a12022-09-09 10:44:29 -0700294 # These are all the licenses we support. Keys are what is seen in metadata
295 # and values are what is expected by ebuilds.
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700296 SUPPORTED_LICENSES = {
George Burgess IV7dffc252022-08-31 14:37:01 -0700297 "0BSD": "0BSD",
298 "Apache-2.0": "Apache-2.0",
299 "BSD-3-Clause": "BSD-3",
300 "ISC": "ISC",
301 "MIT": "MIT",
302 "MPL-2.0": "MPL-2.0",
303 "unicode": "unicode",
Dan Callaghan91f80542022-09-09 10:57:23 +1000304 "Zlib": "ZLIB",
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700305 }
306
307 # Prefer to take attribution licenses in this order. All these require that
308 # we actually use the license file found in the package so they MUST have
309 # a license file set.
George Burgess IV7dffc252022-08-31 14:37:01 -0700310 PREFERRED_ATTRIB_LICENSE_ORDER = ["MIT", "BSD-3", "ISC"]
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700311
312 # If Apache license is found, always prefer it (simplifies attribution)
George Burgess IV7dffc252022-08-31 14:37:01 -0700313 APACHE_LICENSE = "Apache-2.0"
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700314
315 # Regex for license files found in the vendored directories. Search for
316 # these files with re.IGNORECASE.
317 #
318 # These will be searched in order with the earlier entries being preferred.
319 LICENSE_NAMES_REGEX = [
George Burgess IV7dffc252022-08-31 14:37:01 -0700320 r"^license-mit$",
321 r"^copyright$",
322 r"^licen[cs]e.*$",
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700323 ]
324
325 # Some crates have their license file in other crates. This usually occurs
326 # because multiple crates are published from the same git repository and the
327 # license isn't updated in each sub-crate. In these cases, we can just
328 # ignore these packages.
329 MAP_LICENSE_TO_OTHER = {
George Burgess IV7dffc252022-08-31 14:37:01 -0700330 "failure_derive": "failure",
331 "grpcio-compiler": "grpcio",
332 "grpcio-sys": "grpcio",
333 "rustyline-derive": "rustyline",
Nicholas Bishop7d4433a2022-10-07 12:38:26 -0400334 "uefi-macros": "uefi",
335 "uefi-services": "uefi",
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700336 }
337
338 # Map a package to a specific license and license file. Only use this if
339 # a package doesn't have an easily discoverable license or exports its
340 # license in a weird way. Prefer to patch the project with a license and
341 # upstream the patch instead.
342 STATIC_LICENSE_MAP = {
343 # "package name": ( "license name", "license file relative location")
George Burgess IVf4a5e362022-08-30 14:30:36 -0700344 # Patch for adding this is upstream, but the patch application doesn't
345 # apply to `cargo metadata`. This is presumably because it can't detect
346 # our vendor directory.
347 # https://gitlab.freedesktop.org/slirp/libslirp-sys/-/merge_requests/6
George Burgess IV7dffc252022-08-31 14:37:01 -0700348 "libslirp-sys": ("MIT", "LICENSE"),
Dan Callaghan91f80542022-09-09 10:57:23 +1000349 # Upstream prefers to embed license text inside README.md:
350 "riscv": ("ISC", "README.md"),
351 "riscv-rt": ("ISC", "README.md"),
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700352 }
353
354 def __init__(self, working_dir, vendor_dir):
355 self.working_dir = working_dir
356 self.vendor_dir = vendor_dir
357
358 def _find_license_in_dir(self, search_dir):
359 for p in os.listdir(search_dir):
360 # Ignore anything that's not a file
361 if not os.path.isfile(os.path.join(search_dir, p)):
362 continue
363
364 # Now check if the name matches any of the regexes
365 # We'll return the first matching file.
366 for regex in self.LICENSE_NAMES_REGEX:
367 if re.search(regex, p, re.IGNORECASE):
368 yield os.path.join(search_dir, p)
369 break
370
371 def _guess_license_type(self, license_file):
George Burgess IV7dffc252022-08-31 14:37:01 -0700372 if "-MIT" in license_file:
373 return "MIT"
374 elif "-APACHE" in license_file:
375 return "APACHE"
376 elif "-BSD" in license_file:
377 return "BSD-3"
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700378
George Burgess IV7dffc252022-08-31 14:37:01 -0700379 with open(license_file, "r") as f:
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700380 lines = f.read()
George Burgess IV7dffc252022-08-31 14:37:01 -0700381 if "MIT" in lines:
382 return "MIT"
383 elif "Apache" in lines:
384 return "APACHE"
385 elif "BSD 3-Clause" in lines:
386 return "BSD-3"
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700387
George Burgess IV7dffc252022-08-31 14:37:01 -0700388 return ""
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700389
George Burgess IV7dffc252022-08-31 14:37:01 -0700390 def generate_license(
391 self, skip_license_check, print_map_to_file, license_shorthand_file
392 ):
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700393 """Generate single massive license file from metadata."""
George Burgess IV18af5632022-08-30 14:10:53 -0700394 metadata = load_metadata(self.working_dir)
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700395
396 has_license_types = set()
397 bad_licenses = {}
398
399 # Keep license map ordered so it generates a consistent license map
400 license_map = {}
401
402 skip_license_check = skip_license_check or []
George Burgess IV4ae42062022-08-15 18:54:51 -0700403 has_unicode_license = False
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700404
George Burgess IV18af5632022-08-30 14:10:53 -0700405 for package in metadata["packages"]:
George Burgess IV40cc91c2022-08-15 13:07:40 -0700406 # Skip the synthesized Cargo.toml packages that exist solely to
407 # list dependencies.
George Burgess IV7dffc252022-08-31 14:37:01 -0700408 if "path+file:///" in package["id"]:
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700409 continue
410
George Burgess IV7dffc252022-08-31 14:37:01 -0700411 pkg_name = package["name"]
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700412 if pkg_name in skip_license_check:
413 print(
George Burgess IV7dffc252022-08-31 14:37:01 -0700414 "Skipped license check on {}. Reason: Skipped from command line".format(
415 pkg_name
416 )
417 )
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700418 continue
419
420 if pkg_name in self.MAP_LICENSE_TO_OTHER:
421 print(
George Burgess IV7dffc252022-08-31 14:37:01 -0700422 "Skipped license check on {}. Reason: License already in {}".format(
423 pkg_name, self.MAP_LICENSE_TO_OTHER[pkg_name]
424 )
425 )
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700426 continue
427
428 # Check if we have a static license map for this package. Use the
429 # static values if we have it already set.
430 if pkg_name in self.STATIC_LICENSE_MAP:
431 (license, license_file) = self.STATIC_LICENSE_MAP[pkg_name]
432 license_map[pkg_name] = {
433 "license": license,
434 "license_file": license_file,
435 }
436 continue
437
438 license_files = []
George Burgess IV93ba4732022-08-13 14:10:10 -0700439 # use `or ''` instead of get's default, since `package` may have a
440 # None value for 'license'.
George Burgess IV7dffc252022-08-31 14:37:01 -0700441 license = package.get("license") or ""
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700442
443 # We ignore the metadata for license file because most crates don't
444 # have it set. Just scan the source for licenses.
George Burgess IV7dffc252022-08-31 14:37:01 -0700445 pkg_version = package["version"]
446 license_files = list(
447 self._find_license_in_dir(
448 os.path.join(self.vendor_dir, f"{pkg_name}-{pkg_version}")
449 )
450 )
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700451
George Burgess IV4ae42062022-08-15 18:54:51 -0700452 # FIXME(b/240953811): The code later in this loop is only
453 # structured to handle ORs, not ANDs. Fortunately, this license in
454 # particular is `AND`ed between a super common license (Apache) and
455 # a more obscure one (unicode). This hack is specifically intended
456 # for the `unicode-ident` crate, though no crate name check is
457 # made, since it's OK other crates happen to have this license.
George Burgess IV7dffc252022-08-31 14:37:01 -0700458 if license == "(MIT OR Apache-2.0) AND Unicode-DFS-2016":
George Burgess IV4ae42062022-08-15 18:54:51 -0700459 has_unicode_license = True
460 # We'll check later to be sure MIT or Apache-2.0 is represented
461 # properly.
462 for x in license_files:
George Burgess IV7dffc252022-08-31 14:37:01 -0700463 if os.path.basename(x) == "LICENSE-UNICODE":
George Burgess IV4ae42062022-08-15 18:54:51 -0700464 license_file = x
465 break
466 else:
George Burgess IV7dffc252022-08-31 14:37:01 -0700467 raise ValueError(
468 "No LICENSE-UNICODE found in " f"{license_files}"
469 )
George Burgess IV4ae42062022-08-15 18:54:51 -0700470 license_map[pkg_name] = {
471 "license": license,
472 "license_file": license_file,
473 }
George Burgess IV7dffc252022-08-31 14:37:01 -0700474 has_license_types.add("unicode")
George Burgess IV4ae42062022-08-15 18:54:51 -0700475 continue
476
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700477 # If there are multiple licenses, they are delimited with "OR" or "/"
George Burgess IV7dffc252022-08-31 14:37:01 -0700478 delim = " OR " if " OR " in license else "/"
George Burgess IV40cc91c2022-08-15 13:07:40 -0700479 found = [x.strip() for x in license.split(delim)]
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700480
481 # Filter licenses to ones we support
482 licenses_or = [
George Burgess IV7dffc252022-08-31 14:37:01 -0700483 self.SUPPORTED_LICENSES[f]
484 for f in found
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700485 if f in self.SUPPORTED_LICENSES
486 ]
487
488 # If apache license is found, always prefer it because it simplifies
489 # license attribution (we can use existing Apache notice)
490 if self.APACHE_LICENSE in licenses_or:
491 has_license_types.add(self.APACHE_LICENSE)
George Burgess IV7dffc252022-08-31 14:37:01 -0700492 license_map[pkg_name] = {"license": self.APACHE_LICENSE}
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700493
494 # Handle single license that has at least one license file
495 # We pick the first license file and the license
496 elif len(licenses_or) == 1:
497 if license_files:
498 l = licenses_or[0]
499 lf = license_files[0]
500
501 has_license_types.add(l)
502 license_map[pkg_name] = {
George Burgess IV7dffc252022-08-31 14:37:01 -0700503 "license": l,
504 "license_file": os.path.relpath(lf, self.working_dir),
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700505 }
506 else:
507 bad_licenses[pkg_name] = "{} missing license file".format(
George Burgess IV7dffc252022-08-31 14:37:01 -0700508 licenses_or[0]
509 )
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700510 # Handle multiple licenses
511 elif len(licenses_or) > 1:
512 # Check preferred licenses in order
513 license_found = False
514 for l in self.PREFERRED_ATTRIB_LICENSE_ORDER:
515 if not l in licenses_or:
516 continue
517
518 for f in license_files:
519 if self._guess_license_type(f) == l:
520 license_found = True
521 has_license_types.add(l)
522 license_map[pkg_name] = {
George Burgess IV7dffc252022-08-31 14:37:01 -0700523 "license": l,
524 "license_file": os.path.relpath(
525 f, self.working_dir
526 ),
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700527 }
528 break
529
530 # Break out of loop if license is found
531 if license_found:
532 break
533 else:
534 bad_licenses[pkg_name] = license
535
536 # If we had any bad licenses, we need to abort
537 if bad_licenses:
538 for k in bad_licenses.keys():
George Burgess IV7dffc252022-08-31 14:37:01 -0700539 print(
540 "{} had no acceptable licenses: {}".format(
541 k, bad_licenses[k]
542 )
543 )
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700544 raise Exception("Bad licenses in vendored packages.")
545
546 # Write license map to file
547 if print_map_to_file:
George Burgess IV7dffc252022-08-31 14:37:01 -0700548 with open(
549 os.path.join(self.working_dir, print_map_to_file), "w"
550 ) as lfile:
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700551 json.dump(license_map, lfile, sort_keys=True)
552
553 # Raise missing licenses unless we have a valid reason to ignore them
554 raise_missing_license = False
555 for name, v in license_map.items():
George Burgess IV7dffc252022-08-31 14:37:01 -0700556 if (
557 "license_file" not in v
558 and v.get("license", "") != self.APACHE_LICENSE
559 ):
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700560 raise_missing_license = True
George Burgess IV7dffc252022-08-31 14:37:01 -0700561 print(
562 " {}: Missing license file. Fix or add to ignorelist.".format(
563 name
564 )
565 )
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700566
567 if raise_missing_license:
568 raise Exception(
569 "Unhandled missing license file. "
George Burgess IV7dffc252022-08-31 14:37:01 -0700570 "Make sure all are accounted for before continuing."
571 )
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700572
George Burgess IV4ae42062022-08-15 18:54:51 -0700573 if has_unicode_license:
574 if self.APACHE_LICENSE not in has_license_types:
George Burgess IV7dffc252022-08-31 14:37:01 -0700575 raise ValueError(
576 "Need the apache license; currently have: "
577 f"{sorted(has_license_types)}"
578 )
George Burgess IV4ae42062022-08-15 18:54:51 -0700579
George Burgess IV04833702022-08-09 22:00:38 -0700580 sorted_licenses = sorted(has_license_types)
George Burgess IV124e6a12022-09-09 10:44:29 -0700581 print("The following licenses are in use:", sorted_licenses)
George Burgess IV7dffc252022-08-31 14:37:01 -0700582 header = textwrap.dedent(
583 """\
George Burgess IV04833702022-08-09 22:00:38 -0700584 # File to describe the licenses used by this registry.
Daniel Verkampd9d085b2022-09-07 10:52:27 -0700585 # Used so it's easy to automatically verify ebuilds are updated.
George Burgess IV04833702022-08-09 22:00:38 -0700586 # Each line is a license. Lines starting with # are comments.
George Burgess IV7dffc252022-08-31 14:37:01 -0700587 """
588 )
589 with open(license_shorthand_file, "w", encoding="utf-8") as f:
George Burgess IV04833702022-08-09 22:00:38 -0700590 f.write(header)
George Burgess IV7dffc252022-08-31 14:37:01 -0700591 f.write("\n".join(sorted_licenses))
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700592
593
Abhishek Pandit-Subedice0f5b22021-09-10 15:50:08 -0700594# TODO(abps) - This needs to be replaced with datalog later. We should compile
595# all crab files into datalog and query it with our requirements
596# instead.
597class CrabManager:
598 """Manage audit files."""
George Burgess IV7dffc252022-08-31 14:37:01 -0700599
Abhishek Pandit-Subedice0f5b22021-09-10 15:50:08 -0700600 def __init__(self, working_dir, crab_dir):
601 self.working_dir = working_dir
602 self.crab_dir = crab_dir
603
604 def _check_bad_traits(self, crabdata):
605 """Checks that a package's crab audit meets our requirements.
606
607 Args:
608 crabdata: Dict with crab keys in standard templated format.
609 """
George Burgess IV7dffc252022-08-31 14:37:01 -0700610 common = crabdata["common"]
Abhishek Pandit-Subedice0f5b22021-09-10 15:50:08 -0700611 # TODO(b/200578411) - Figure out what conditions we should enforce as
612 # part of the audit.
613 conditions = [
George Burgess IV7dffc252022-08-31 14:37:01 -0700614 common.get("deny", None),
Abhishek Pandit-Subedice0f5b22021-09-10 15:50:08 -0700615 ]
616
617 # If any conditions are true, this crate is not acceptable.
618 return any(conditions)
619
620 def verify_traits(self):
George Burgess IV7dffc252022-08-31 14:37:01 -0700621 """Verify that all required CRAB traits for this repository are met."""
George Burgess IV18af5632022-08-30 14:10:53 -0700622 metadata = load_metadata(self.working_dir)
Abhishek Pandit-Subedice0f5b22021-09-10 15:50:08 -0700623
624 failing_crates = {}
625
626 # Verify all packages have a CRAB file associated with it and they meet
627 # all our required traits
George Burgess IV18af5632022-08-30 14:10:53 -0700628 for package in metadata["packages"]:
George Burgess IV40cc91c2022-08-15 13:07:40 -0700629 # Skip the synthesized Cargo.toml packages that exist solely to
630 # list dependencies.
George Burgess IV7dffc252022-08-31 14:37:01 -0700631 if "path+file:///" in package["id"]:
Abhishek Pandit-Subedice0f5b22021-09-10 15:50:08 -0700632 continue
633
George Burgess IV7dffc252022-08-31 14:37:01 -0700634 crabname = "{}-{}".format(package["name"], package["version"])
Abhishek Pandit-Subedice0f5b22021-09-10 15:50:08 -0700635 filename = os.path.join(self.crab_dir, "{}.toml".format(crabname))
636
637 # If crab file doesn't exist, the crate fails
638 if not os.path.isfile(filename):
639 failing_crates[crabname] = "No crab file".format(filename)
640 continue
641
George Burgess IV7dffc252022-08-31 14:37:01 -0700642 with open(filename, "r") as f:
Abhishek Pandit-Subedice0f5b22021-09-10 15:50:08 -0700643 crabdata = toml.loads(f.read())
644
645 # If crab file's crate_name and version keys don't match this
646 # package, it also fails. This is just housekeeping...
George Burgess IV7dffc252022-08-31 14:37:01 -0700647 if (
648 package["name"] != crabdata["crate_name"]
649 or package["version"] != crabdata["version"]
650 ):
Abhishek Pandit-Subedice0f5b22021-09-10 15:50:08 -0700651 failing_crates[crabname] = "Crate name or version don't match"
652 continue
653
654 if self._check_bad_traits(crabdata):
655 failing_crates[crabname] = "Failed bad traits check"
656
George Burgess IV9e0cfde2022-09-27 15:08:15 -0700657 # If we had any failing crates, list them now, and exit with an error.
Abhishek Pandit-Subedice0f5b22021-09-10 15:50:08 -0700658 if failing_crates:
George Burgess IV7dffc252022-08-31 14:37:01 -0700659 print("Failed CRAB audit:")
Abhishek Pandit-Subedice0f5b22021-09-10 15:50:08 -0700660 for k, v in failing_crates.items():
George Burgess IV9e0cfde2022-09-27 15:08:15 -0700661 print(f" {k}: {v}")
662 raise ValueError("CRAB audit did not complete successfully.")
Abhishek Pandit-Subedice0f5b22021-09-10 15:50:08 -0700663
664
George Burgess IVd0261472022-10-17 18:59:10 -0600665def clean_source_related_lines_in_place(cargo_toml):
666 """Removes all [[bin]] (and similar) sections in `cargo_toml`."""
667 cargo_toml.pop("bench", None)
668 cargo_toml.pop("bin", None)
669 cargo_toml.pop("examples", None)
670 cargo_toml.pop("test", None)
671
672 lib = cargo_toml.get("lib")
673 if lib:
674 lib.pop("path", None)
675
676 package = cargo_toml.get("package")
677 if package:
678 package.pop("build", None)
679 package.pop("default-run", None)
680 package.pop("include", None)
681
682
George Burgess IVd4ff0502022-08-14 23:27:57 -0700683def clean_features_in_place(cargo_toml):
684 """Removes all side-effects of features in `cargo_toml`."""
George Burgess IV7dffc252022-08-31 14:37:01 -0700685 features = cargo_toml.get("features")
George Burgess IVd4ff0502022-08-14 23:27:57 -0700686 if not features:
687 return
688
George Burgess IVd0261472022-10-17 18:59:10 -0600689 for name in features:
690 features[name] = []
George Burgess IVd4ff0502022-08-14 23:27:57 -0700691
692
George Burgess IVd0261472022-10-17 18:59:10 -0600693def remove_all_dependencies_in_place(cargo_toml):
George Burgess IVd4ff0502022-08-14 23:27:57 -0700694 """Removes all `target.*.dependencies` from `cargo_toml`."""
George Burgess IVd0261472022-10-17 18:59:10 -0600695 cargo_toml.pop("build-dependencies", None)
696 cargo_toml.pop("dependencies", None)
697 cargo_toml.pop("dev-dependencies", None)
698
George Burgess IV7dffc252022-08-31 14:37:01 -0700699 target = cargo_toml.get("target")
George Burgess IVd4ff0502022-08-14 23:27:57 -0700700 if not target:
701 return
George Burgess IV0313d782022-08-15 23:45:44 -0700702
George Burgess IVd4ff0502022-08-14 23:27:57 -0700703 empty_keys = []
704 for key, values in target.items():
George Burgess IVd0261472022-10-17 18:59:10 -0600705 values.pop("build-dependencies", None)
George Burgess IV7dffc252022-08-31 14:37:01 -0700706 values.pop("dependencies", None)
707 values.pop("dev-dependencies", None)
George Burgess IVd4ff0502022-08-14 23:27:57 -0700708 if not values:
709 empty_keys.append(key)
George Burgess IV0313d782022-08-15 23:45:44 -0700710
George Burgess IVd4ff0502022-08-14 23:27:57 -0700711 if len(empty_keys) == len(target):
George Burgess IV7dffc252022-08-31 14:37:01 -0700712 del cargo_toml["target"]
George Burgess IVd4ff0502022-08-14 23:27:57 -0700713 else:
714 for key in empty_keys:
715 del target[key]
George Burgess IV0313d782022-08-15 23:45:44 -0700716
717
George Burgess IV7dffc252022-08-31 14:37:01 -0700718class CrateDestroyer:
Abhishek Pandit-Subedif0eb6e02021-09-24 16:36:12 -0700719 def __init__(self, working_dir, vendor_dir):
720 self.working_dir = working_dir
721 self.vendor_dir = vendor_dir
722
723 def _modify_cargo_toml(self, pkg_path):
George Burgess IV7dffc252022-08-31 14:37:01 -0700724 with open(os.path.join(pkg_path, "Cargo.toml"), "r") as cargo:
Abhishek Pandit-Subedif0eb6e02021-09-24 16:36:12 -0700725 contents = toml.load(cargo)
726
George Burgess IV7dffc252022-08-31 14:37:01 -0700727 package = contents["package"]
George Burgess IVd4ff0502022-08-14 23:27:57 -0700728
Abhishek Pandit-Subedif0eb6e02021-09-24 16:36:12 -0700729 # Change description, license and delete license key
George Burgess IV7dffc252022-08-31 14:37:01 -0700730 package["description"] = "Empty crate that should not build."
731 package["license"] = "Apache-2.0"
George Burgess IVd4ff0502022-08-14 23:27:57 -0700732
George Burgess IV7dffc252022-08-31 14:37:01 -0700733 package.pop("license_file", None)
George Burgess IVd4ff0502022-08-14 23:27:57 -0700734 # If there's no build.rs but we specify `links = "foo"`, Cargo gets
735 # upset.
George Burgess IV7dffc252022-08-31 14:37:01 -0700736 package.pop("links", None)
Abhishek Pandit-Subedif0eb6e02021-09-24 16:36:12 -0700737
George Burgess IV0313d782022-08-15 23:45:44 -0700738 # Some packages have cfg-specific dependencies. Remove them here; we
739 # don't care about the dependencies of an empty package.
740 #
741 # This is a load-bearing optimization: `dev-python/toml` doesn't
742 # always round-trip dumps(loads(x)) correctly when `x` has keys with
743 # strings (b/242589711#comment3). The place this has bitten us so far
744 # is target dependencies, which can be harmlessly removed for now.
George Burgess IVd4ff0502022-08-14 23:27:57 -0700745 #
746 # Cleaning features in-place is also necessary, since we're removing
747 # dependencies, and a feature can enable features in dependencies.
748 # Cargo errors out on `[features] foo = "bar/baz"` if `bar` isn't a
749 # dependency.
750 clean_features_in_place(contents)
George Burgess IVd0261472022-10-17 18:59:10 -0600751 remove_all_dependencies_in_place(contents)
752
753 # Since we're removing all source files, also be sure to remove
754 # source-related keys.
755 clean_source_related_lines_in_place(contents)
George Burgess IV0313d782022-08-15 23:45:44 -0700756
Abhishek Pandit-Subedif0eb6e02021-09-24 16:36:12 -0700757 with open(os.path.join(pkg_path, "Cargo.toml"), "w") as cargo:
758 toml.dump(contents, cargo)
759
George Burgess IV8e2cc042022-10-18 14:50:48 -0600760 def _replace_source_contents(self, package_path, compile_error):
Abhishek Pandit-Subedif0eb6e02021-09-24 16:36:12 -0700761 # First load the checksum file before starting
762 checksum_file = os.path.join(package_path, ".cargo-checksum.json")
George Burgess IV7dffc252022-08-31 14:37:01 -0700763 with open(checksum_file, "r") as csum:
Abhishek Pandit-Subedif0eb6e02021-09-24 16:36:12 -0700764 checksum_contents = json.load(csum)
765
766 # Also load the cargo.toml file which we need to write back
767 cargo_file = os.path.join(package_path, "Cargo.toml")
George Burgess IV7dffc252022-08-31 14:37:01 -0700768 with open(cargo_file, "rb") as cfile:
George Burgess IV3e344e42022-08-09 21:07:04 -0700769 cargo_contents = cfile.read()
Abhishek Pandit-Subedif0eb6e02021-09-24 16:36:12 -0700770
771 shutil.rmtree(package_path)
772
773 # Make package and src dirs and replace lib.rs
774 os.makedirs(os.path.join(package_path, "src"), exist_ok=True)
775 with open(os.path.join(package_path, "src", "lib.rs"), "w") as librs:
George Burgess IV8e2cc042022-10-18 14:50:48 -0600776 librs.write(
777 EMPTY_CRATE_BODY if compile_error else NOP_EMPTY_CRATE_BODY
778 )
Abhishek Pandit-Subedif0eb6e02021-09-24 16:36:12 -0700779
780 # Restore cargo.toml
George Burgess IV7dffc252022-08-31 14:37:01 -0700781 with open(cargo_file, "wb") as cfile:
George Burgess IV3e344e42022-08-09 21:07:04 -0700782 cfile.write(cargo_contents)
Abhishek Pandit-Subedif0eb6e02021-09-24 16:36:12 -0700783
784 # Restore checksum
George Burgess IV7dffc252022-08-31 14:37:01 -0700785 with open(checksum_file, "w") as csum:
Abhishek Pandit-Subedif0eb6e02021-09-24 16:36:12 -0700786 json.dump(checksum_contents, csum)
787
788 def destroy_unused_crates(self):
George Burgess IV18af5632022-08-30 14:10:53 -0700789 metadata = load_metadata(self.working_dir, filter_platform=None)
George Burgess IV7dffc252022-08-31 14:37:01 -0700790 used_packages = {
791 p["name"] for p in load_metadata(self.working_dir)["packages"]
792 }
Abhishek Pandit-Subedif0eb6e02021-09-24 16:36:12 -0700793
794 cleaned_packages = []
George Burgess IV40cc91c2022-08-15 13:07:40 -0700795 # Since we're asking for _all_ metadata packages, we may see
796 # duplication.
George Burgess IV18af5632022-08-30 14:10:53 -0700797 for package in metadata["packages"]:
Abhishek Pandit-Subedif0eb6e02021-09-24 16:36:12 -0700798 # Skip used packages
George Burgess IV8e2cc042022-10-18 14:50:48 -0600799 package_name = package["name"]
800 if package_name in used_packages:
Abhishek Pandit-Subedif0eb6e02021-09-24 16:36:12 -0700801 continue
802
803 # Detect the correct package path to destroy
George Burgess IV7dffc252022-08-31 14:37:01 -0700804 pkg_path = os.path.join(
805 self.vendor_dir,
George Burgess IV8e2cc042022-10-18 14:50:48 -0600806 "{}-{}".format(package_name, package["version"]),
George Burgess IV7dffc252022-08-31 14:37:01 -0700807 )
Abhishek Pandit-Subedif0eb6e02021-09-24 16:36:12 -0700808 if not os.path.isdir(pkg_path):
George Burgess IV8e2cc042022-10-18 14:50:48 -0600809 print(f"Crate {package_name} not found at {pkg_path}")
George Burgess IV635f7262022-08-09 21:32:20 -0700810 continue
Abhishek Pandit-Subedif0eb6e02021-09-24 16:36:12 -0700811
George Burgess IV8e2cc042022-10-18 14:50:48 -0600812 self._replace_source_contents(
813 pkg_path, compile_error=package_name not in NOP_EMPTY_CRATES
814 )
Abhishek Pandit-Subedif0eb6e02021-09-24 16:36:12 -0700815 self._modify_cargo_toml(pkg_path)
816 _rerun_checksums(pkg_path)
817 cleaned_packages.append(package["name"])
818
819 for pkg in cleaned_packages:
George Burgess IV635f7262022-08-09 21:32:20 -0700820 print("Removed unused crate", pkg)
Abhishek Pandit-Subedif0eb6e02021-09-24 16:36:12 -0700821
George Burgess IV7dffc252022-08-31 14:37:01 -0700822
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700823def main(args):
Abhishek Pandit-Subedib75bd562021-02-25 15:32:22 -0800824 current_path = pathlib.Path(__file__).parent.absolute()
Abhishek Pandit-Subedi5065a0f2021-06-13 20:38:55 +0000825 patches = os.path.join(current_path, "patches")
826 vendor = os.path.join(current_path, "vendor")
Abhishek Pandit-Subedice0f5b22021-09-10 15:50:08 -0700827 crab_dir = os.path.join(current_path, "crab", "crates")
George Burgess IV04833702022-08-09 22:00:38 -0700828 license_shorthand_file = os.path.join(current_path, "licenses_used.txt")
Abhishek Pandit-Subedib75bd562021-02-25 15:32:22 -0800829
Abhishek Pandit-Subedifa902382021-08-20 11:04:33 -0700830 # First, actually run cargo vendor
831 run_cargo_vendor(current_path)
832
Abhishek Pandit-Subedi5065a0f2021-06-13 20:38:55 +0000833 # Order matters here:
834 # - Apply patches (also re-calculates checksums)
835 # - Cleanup any owners files (otherwise, git check-in or checksums are
836 # unhappy)
Abhishek Pandit-Subedif0eb6e02021-09-24 16:36:12 -0700837 # - Destroy unused crates
Abhishek Pandit-Subedi5065a0f2021-06-13 20:38:55 +0000838 apply_patches(patches, vendor)
839 cleanup_owners(vendor)
Abhishek Pandit-Subedif0eb6e02021-09-24 16:36:12 -0700840 destroyer = CrateDestroyer(current_path, vendor)
841 destroyer.destroy_unused_crates()
Abhishek Pandit-Subedib75bd562021-02-25 15:32:22 -0800842
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700843 # Combine license file and check for any bad licenses
844 lm = LicenseManager(current_path, vendor)
George Burgess IV7dffc252022-08-31 14:37:01 -0700845 lm.generate_license(
846 args.skip_license_check, args.license_map, license_shorthand_file
847 )
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700848
Abhishek Pandit-Subedice0f5b22021-09-10 15:50:08 -0700849 # Run crab audit on all packages
850 crab = CrabManager(current_path, crab_dir)
851 crab.verify_traits()
852
Abhishek Pandit-Subedib75bd562021-02-25 15:32:22 -0800853
George Burgess IV7dffc252022-08-31 14:37:01 -0700854if __name__ == "__main__":
855 parser = argparse.ArgumentParser(description="Vendor packages properly")
856 parser.add_argument(
857 "--skip-license-check",
858 "-s",
859 help="Skip the license check on a specific package",
860 action="append",
861 )
862 parser.add_argument("--license-map", help="Write license map to this file")
Abhishek Pandit-Subedie393cb72021-08-22 10:41:13 -0700863 args = parser.parse_args()
864
865 main(args)