From d1d25eb6840a33bc529c9e00813e0408ad20e732 Mon Sep 17 00:00:00 2001 From: Fabio Valentini Date: Fri, 8 Mar 2024 16:56:31 +0100 Subject: [PATCH] conf: warn when extra sources or patches use conflicting numbers --- rust2rpm/__main__.py | 68 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/rust2rpm/__main__.py b/rust2rpm/__main__.py index 6606434..9842475 100644 --- a/rust2rpm/__main__.py +++ b/rust2rpm/__main__.py @@ -1,12 +1,13 @@ import os import pathlib import sys +from typing import Optional from cargo2rpm.semver import Version from rust2rpm import log from rust2rpm.cli import get_parser -from rust2rpm.conf import load_config +from rust2rpm.conf import load_config, TomlConf from rust2rpm.crate import InvalidProjectError, InvalidVersionError, process_project from rust2rpm.cratesio import NoVersionsError from rust2rpm.distgit import get_package_info @@ -23,6 +24,68 @@ from rust2rpm.utils import ( ) +def validate_extra_sources_patches( + tomlconf: TomlConf, vendor_tarball: Optional[str], patch_files: tuple[Optional[str], Optional[str]] +): + if extra_sources := tomlconf.package_extra_sources: + min_source_number = 2 if vendor_tarball else 1 + + for extra_source in extra_sources: + if extra_source.number == 0: + log.error( + f"Extra source {extra_source.file!r} conflicts with the primary tarball. " + f"Use a number larger than {min_source_number} instead." + ) + sys.exit(1) + if extra_source.number == 1 and vendor_tarball: + log.error( + f"Extra source {extra_source.file!r} conflicts with the vendor tarball. " + f"Use a number larger than {min_source_number} instead." + ) + sys.exit(1) + + # no conflict now, but maybe later + if extra_source.number < 2: + log.warn( + f"Extra source {extra_source.file!r} might conflict with other files in the future. " + "Using source numbers >= 2 is recommended." + ) + + if extra_patches := tomlconf.package_extra_patches: + min_patch_number = 0 + if patch_files[0]: + min_patch_number += 1 + if patch_files[1]: + min_patch_number += 1 + + for extra_patch in extra_patches: + if extra_patch.number == 0 and patch_files[0]: + log.error( + f"Extra patch {extra_patch.file!r} conflicts with the automatically generated patch. " + f"Use a number larger than {min_patch_number} instead." + ) + sys.exit(1) + if extra_patch.number == 0 and patch_files[1]: + log.error( + f"Extra patch {extra_patch.file!r} conflicts with the manually created patch. " + f"Use a number larger than {min_patch_number} instead." + ) + sys.exit(1) + if extra_patch.number == 1 and patch_files[0] and patch_files[1]: + log.error( + f"Extra patch {extra_patch.file!r} conflicts with the manually created patch. " + f"Use a number larger than {min_patch_number} instead." + ) + sys.exit(1) + + # no conflict now, but maybe later + if extra_patch.number < 2: + log.warn( + f"Extra patch {extra_patch.file!r} might conflict with other patches in the future. " + "Using patch numbers >= 2 is recommended." + ) + + @exit_on_common_errors() def main(): parser = get_parser() @@ -121,6 +184,9 @@ def main(): else: tomlconf = load_config(set(), args.target) + # basic validation for extra sources and patches + validate_extra_sources_patches(tomlconf, vendor_tarball, patch_files) + # package for a workspace project if metadata.is_workspace(): try: