crate: exit gracefully on nonsensical command line arguments

This commit is contained in:
Fabio Valentini 2023-10-10 00:07:19 +02:00
parent 7ce12d552c
commit 95622b6198
No known key found for this signature in database
GPG key ID: 5AC5F572E5D410AF
2 changed files with 9 additions and 2 deletions

View file

@ -8,7 +8,7 @@ from cargo2rpm.semver import Version
from rust2rpm import log from rust2rpm import log
from rust2rpm.cli import get_parser from rust2rpm.cli import get_parser
from rust2rpm.conf import load_config from rust2rpm.conf import load_config
from rust2rpm.crate import process_project from rust2rpm.crate import InvalidRequestError, process_project
from rust2rpm.cratesio import NoVersionsError from rust2rpm.cratesio import NoVersionsError
from rust2rpm.distgit import get_package_info from rust2rpm.distgit import get_package_info
from rust2rpm.generator import spec_render_crate, spec_render_project, spec_render_workspace from rust2rpm.generator import spec_render_crate, spec_render_project, spec_render_workspace
@ -53,6 +53,9 @@ def main():
except NoVersionsError: except NoVersionsError:
log.error(f"No versions are available for crate {args.crate!r}.") log.error(f"No versions are available for crate {args.crate!r}.")
sys.exit(1) sys.exit(1)
except InvalidRequestError as exc:
log.error(str(exc))
sys.exit(1)
if metadata.is_workspace(): if metadata.is_workspace():
base_name = project base_name = project

View file

@ -54,6 +54,10 @@ DOC_FILE_PATTERN = re.compile(
DOC_FILE_EXCLUDES = re.compile(r"CMakeLists\.txt|versions\.txt|.*\.tpl|.*\.in") DOC_FILE_EXCLUDES = re.compile(r"CMakeLists\.txt|versions\.txt|.*\.tpl|.*\.in")
class InvalidRequestError(ValueError):
pass
def local_toml_file(toml_path: str) -> tuple[str, list[str], list[str]]: def local_toml_file(toml_path: str) -> tuple[str, list[str], list[str]]:
assert os.path.isfile(toml_path) assert os.path.isfile(toml_path)
assert os.path.basename(toml_path) == "Cargo.toml" assert os.path.basename(toml_path) == "Cargo.toml"
@ -256,7 +260,7 @@ def process_project(
if project_is_path(project) and not project.endswith(".crate"): if project_is_path(project) and not project.endswith(".crate"):
# project points into unpacked sources: # project points into unpacked sources:
if store_crate: if store_crate:
raise ValueError("The '--store-crate' / '-s' flag cannot be used for unpacked sources.") raise InvalidRequestError("The '--store-crate' / '-s' flag cannot be used for unpacked sources.")
# process unpacked sources # process unpacked sources
name, version, diffs, metadata, doc_files, license_files, vendor_tarball = process_project_local( name, version, diffs, metadata, doc_files, license_files, vendor_tarball = process_project_local(