Coverage for rust2rpm/vendor.py: 100%
5 statements
« prev ^ index » next coverage.py v7.6.7, created at 2024-11-22 18:36 +0100
« prev ^ index » next coverage.py v7.6.7, created at 2024-11-22 18:36 +0100
1"""Module containing functionality for handling tarballs containing vendored sources."""
3import shutil
4import subprocess
5from pathlib import Path
7from rust2rpm import log
8from rust2rpm.cli import VendorMode
11def generate_vendor_tarball(
12 toml_path: Path,
13 project_name: str,
14 project_version: str,
15 dest_dir: Path,
16 mode: VendorMode,
17) -> str | None: # pragma nocover: requires internet access
18 """Return a default name for a tarball containing vendored sources.
20 There are three different modes of operation depending on the `VendorMode`:
22 - `OFF`: do nothing and return `None`
23 - `MANUAL`: do nothing and return a file name in the default format
24 - `AUTO`: create a tarball containing vendored sources, with default settings,
25 move it to `dest_dir`, and return its file name
27 Arguments:
28 toml_path: Path to the Cargo.toml file of the project for which
29 a tarball with vendored sources will be generated.
30 project_name: Name of the project used in the default filename.
31 project_version: Version of the project used in the default filename.
32 dest_dir: Output directory for the generated vendor tarball.
33 mode: Mode of operation.
35 Returns:
36 File name of the generated tarball or `None` if no tarball is used.
37 Returns `None` in `VendorMode` is `OFF`, and a file name in default
38 format for other modes.
40 """
41 if mode == VendorMode.OFF:
42 return None
44 tarball_name = f"{project_name}-{project_version}-vendor.tar.xz"
45 tarball_dest = dest_dir / tarball_name
47 if mode == VendorMode.MANUAL:
48 log.info(f"Assuming manually created vendor tarball named {tarball_name}")
49 return tarball_name
51 # mode == VendorMode.AUTO:
53 if tarball_dest.is_file():
54 log.info(f"Vendor tarball already exists: {tarball_name}")
55 log.info("To force re-generation of the vendor tarball, remove this file.")
56 return tarball_name
58 log.info("Generating vendor tarball ...")
60 # S603: these commands to not take inputs that would need to be validated
61 # S607: allowing use of "cargo" from $PATH is intentional
63 subprocess.run( # noqa: S603
64 ["cargo", "vendor", "--versioned-dirs", "--manifest-path", toml_path, "vendor/"], # noqa: S607
65 capture_output=True,
66 check=True,
67 )
69 log.info("Compressing vendor tarball ...")
71 subprocess.run( # noqa: S603
72 ["/usr/bin/tar", "-cJvf", tarball_name, "vendor/"],
73 capture_output=True,
74 check=True,
75 )
77 shutil.rmtree("vendor/")
79 if dest_dir != Path.cwd():
80 Path(tarball_name).rename(tarball_dest)
82 return tarball_name