From c561328ce17d0c94d073d41bf45d2422fdf57ce7 Mon Sep 17 00:00:00 2001 From: Benjamin Herrenschmidt Date: Aug 06 2024 05:03:34 +0000 Subject: Add an action to retrieve the SPDX license string from a crate This is useful for a script I'm writing to collect all the bundled crate licenses for centos-style vendored crates Signed-off-by: Benjamin Herrenschmidt --- diff --git a/cargo2rpm/__main__.py b/cargo2rpm/__main__.py index 635eb31..9cbd9bb 100644 --- a/cargo2rpm/__main__.py +++ b/cargo2rpm/__main__.py @@ -170,6 +170,23 @@ def main(): break_the_build(str(exc)) exit(1) + case "license": + try: + metadata = Metadata.from_cargo(args.path) + + if metadata.is_workspace(): + print("Cannot determine crate license from a cargo workspace.", file=sys.stderr) + # exit code 1 will fail package scriptlets + exit(1) + + print(metadata.packages[0].license) + exit(0) + + except subprocess.CalledProcessError as exc: + # catch the exception to ensure stdout and stderr are printed + print(pretty_called_process_error(exc), file=sys.stderr) + exit(1) + case "parse-vendor-manifest": try: action_parse_vendor_manifest(args) diff --git a/cargo2rpm/cli.py b/cargo2rpm/cli.py index 27bc057..b50a0a8 100644 --- a/cargo2rpm/cli.py +++ b/cargo2rpm/cli.py @@ -59,6 +59,9 @@ def get_args(args=None): ) action_parse_vendor_manifest.set_defaults(action="parse-vendor-manifest") + action_license = action_parsers.add_parser("license", help="Print the license of the current crate.") + action_license.set_defaults(action="license") + if args: return parser.parse_args(args) else: # pragma nocover