From 323dbea4b6dbfe60f650ab654dfc7f16f3aad9e4 Mon Sep 17 00:00:00 2001 From: Fabio Valentini Date: Thu, 23 Nov 2023 19:28:29 +0100 Subject: [PATCH] crate: error out if project arg looks like a path but is not a path --- rust2rpm/__main__.py | 6 +++--- rust2rpm/crate.py | 34 +++++++++++++++++++--------------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/rust2rpm/__main__.py b/rust2rpm/__main__.py index 5dc5023..b803fca 100644 --- a/rust2rpm/__main__.py +++ b/rust2rpm/__main__.py @@ -8,7 +8,7 @@ from cargo2rpm.semver import Version from rust2rpm import log from rust2rpm.cli import get_parser from rust2rpm.conf import load_config -from rust2rpm.crate import InvalidRequestError, process_project +from rust2rpm.crate import InvalidProjectError, process_project from rust2rpm.cratesio import NoVersionsError from rust2rpm.distgit import get_package_info from rust2rpm.generator import spec_render_crate, spec_render_project, spec_render_workspace @@ -53,8 +53,8 @@ def main(): except NoVersionsError: log.error(f"No versions are available for crate {args.crate!r}.") sys.exit(1) - except InvalidRequestError as exc: - log.error(str(exc)) + except InvalidProjectError: + log.error(f"Invalid argument: {args.crate!r} is not a local file or directory") sys.exit(1) if metadata.is_workspace(): diff --git a/rust2rpm/crate.py b/rust2rpm/crate.py index 6ad6947..61e4d59 100644 --- a/rust2rpm/crate.py +++ b/rust2rpm/crate.py @@ -54,7 +54,7 @@ DOC_FILE_PATTERN = re.compile( DOC_FILE_EXCLUDES = re.compile(r"CMakeLists\.txt|versions\.txt|.*\.tpl|.*\.in") -class InvalidRequestError(ValueError): +class InvalidProjectError(ValueError): pass @@ -259,22 +259,26 @@ def process_project( ) -> tuple[ str, str, tuple[Optional[list[str]], Optional[list[str]]], Metadata, list[str], list[str], bool, Optional[str] ]: - if project_is_path(project) and not project.endswith(".crate"): - # project points into unpacked sources: - if store_crate: - raise InvalidRequestError("The '--store-crate' / '-s' flag cannot be used for unpacked sources.") + if project_is_path(project): + if not os.path.exists(project): + raise InvalidProjectError(project) - # process unpacked sources - name, version, diffs, metadata, doc_files, license_files, vendor_tarball = process_project_local( - project, patch, patch_foreign, vendor - ) - return name, version, diffs, metadata, doc_files, license_files, True, vendor_tarball + if project.endswith(".crate"): + # project points at a local .crate file + crate_file_path = project - if project_is_path(project) and project.endswith(".crate"): - # project points at a local .crate file - crate_file_path = project - # determine name and version from the filename - name, version = parse_crate_file_name(project) + # determine name and version from the filename + name, version = parse_crate_file_name(project) + + else: + # project points at unpacked sources + if store_crate: + log.warn("The '--store-crate' flag has no effect for unpacked sources.") + + name, version, diffs, metadata, doc_files, license_files, vendor_tarball = process_project_local( + project, patch, patch_foreign, vendor + ) + return name, version, diffs, metadata, doc_files, license_files, True, vendor_tarball else: # project is just a crate name