From d5fd8f95175258e7ebadb324b77323179ab84000 Mon Sep 17 00:00:00 2001 From: Fabio Valentini Date: Fri, 8 Mar 2024 15:51:32 +0100 Subject: [PATCH] conf/generator: add support for persisting Cargo.toml patch comments --- rust2rpm/__main__.py | 14 ++++++++++++++ rust2rpm/conf.py | 28 ++++++++++++++++++++++++---- rust2rpm/generator.py | 10 ++++++---- rust2rpm/templates/README.md | 2 ++ rust2rpm/templates/crate.spec | 7 +++++-- rust2rpm/templates/project.spec | 7 +++++-- rust2rpm/templates/workspace.spec | 4 ++-- rust2rpm/tests/test_conf.py | 4 ++-- 8 files changed, 60 insertions(+), 16 deletions(-) diff --git a/rust2rpm/__main__.py b/rust2rpm/__main__.py index 9b7cb14..6606434 100644 --- a/rust2rpm/__main__.py +++ b/rust2rpm/__main__.py @@ -152,6 +152,13 @@ def main(): log.error(f"This is not supported by rust2rpm; remove the '+{build_meta}' suffix.") sys.exit(1) + if tomlconf.package_cargo_toml_patch_comments and patch_files[1] is None: + log.error( + "The rust2rpm.toml configuration file specifies comments for a Cargo.toml patch, " + "but Cargo.toml was not patched." + ) + sys.exit(1) + warn_if_package_uses_restrictive_dependencies(package) spec_contents = spec_render_project( @@ -183,6 +190,13 @@ def main(): log.error("Building library-only crates with vendored dependencies is not supported.") sys.exit(1) + if tomlconf.package_cargo_toml_patch_comments and patch_files[1] is None: + log.error( + "The rust2rpm.toml configuration file specifies comments for a Cargo.toml patch, " + "but Cargo.toml was not patched." + ) + sys.exit(1) + warn_if_package_uses_restrictive_dependencies(package) try: diff --git a/rust2rpm/conf.py b/rust2rpm/conf.py index 44dd380..f7372df 100644 --- a/rust2rpm/conf.py +++ b/rust2rpm/conf.py @@ -45,6 +45,13 @@ TOML_SCHEMA = { "cargo-install-lib": { "type": "boolean", }, + # comments for manual Cargo.toml patches (rust2rpm -p) + "cargo-toml-patch-comments": { + "type": "array", + "items": { + "type": "string", + }, + }, # additional source files "extra-sources": { "type": "array", @@ -483,6 +490,10 @@ class SourceOrPatch: def comments(self) -> list[str]: return self._data.get("comments") or list() + @property + def comment_lines(self) -> list[str]: + return conf_comments_to_spec_comments(self.comments) + @property def whitespace(self) -> str: return " " * (16 - (len("Source") + len(str(self.number)) + 1)) @@ -547,6 +558,17 @@ class TomlConf: else: return None + @property + def package_cargo_toml_patch_comments(self) -> Optional[list[str]]: + if package := self._package: + return package.get("cargo-toml-patch-comments") + else: + return None + + @property + def package_cargo_toml_patch_comment_lines(self) -> Optional[list[str]]: + return conf_comments_to_spec_comments(self.package_cargo_toml_patch_comments) + @property def package_extra_sources(self) -> Optional[list[SourceOrPatch]]: if package := self._package: @@ -932,10 +954,8 @@ def conf_to_bcond_check(conf: TomlConf) -> bool: return True -def conf_to_test_comments(conf: TomlConf) -> list[str]: - if conf.tests_comments: - comments = conf.tests_comments - else: +def conf_comments_to_spec_comments(comments: Optional[list[str]]) -> list[str]: + if not comments: return [] lines = [] diff --git a/rust2rpm/generator.py b/rust2rpm/generator.py index 52ee782..3feddcb 100644 --- a/rust2rpm/generator.py +++ b/rust2rpm/generator.py @@ -11,7 +11,7 @@ from cargo2rpm import rpm import jinja2 from rust2rpm import __version__, log -from rust2rpm.conf import TomlConf, conf_to_bcond_check, conf_to_test_comments, conf_to_cargo_test_args +from rust2rpm.conf import TomlConf, conf_to_bcond_check, conf_comments_to_spec_comments, conf_to_cargo_test_args from rust2rpm.licensing import translate_license from rust2rpm.metadata import package_uses_rust_1_60_feature_syntax, get_required_features_for_binaries @@ -324,7 +324,7 @@ def spec_render_crate( conf_supported_arches = None rpm_bcond_check = conf_to_bcond_check(tomlconf) - rpm_test_comments = conf_to_test_comments(tomlconf) + rpm_test_comments = conf_comments_to_spec_comments(tomlconf.tests_comments) cargo_test_args = conf_to_cargo_test_args(tomlconf) template_args_common = { @@ -341,6 +341,7 @@ def spec_render_crate( "rpm_license_comments": rpm_license_comments, "rpm_patch_file_automatic": patch_file_automatic, "rpm_patch_file_manual": patch_file_manual, + "rpm_patch_file_comments": tomlconf.package_cargo_toml_patch_comment_lines, "rpm_buildrequires": rpm_buildrequires, "rpm_test_requires": rpm_test_requires, "rpm_requires": rpm_requires, @@ -527,7 +528,7 @@ def spec_render_project( conf_supported_arches = None rpm_bcond_check = conf_to_bcond_check(tomlconf) - rpm_test_comments = conf_to_test_comments(tomlconf) + rpm_test_comments = conf_comments_to_spec_comments(tomlconf.tests_comments) cargo_test_args = conf_to_cargo_test_args(tomlconf) template_args_common = { @@ -545,6 +546,7 @@ def spec_render_project( "rpm_license_comments": rpm_license_comments, "rpm_patch_file_automatic": patch_file_automatic, "rpm_patch_file_manual": patch_file_manual, + "rpm_patch_file_comments": tomlconf.package_cargo_toml_patch_comment_lines, "rpm_buildrequires": rpm_buildrequires, "rpm_test_requires": rpm_test_requires, "rpm_license_files": license_files, @@ -719,7 +721,7 @@ def spec_render_workspace( rpm_license_tag, rpm_license_comments = translate_license(target, license_str) rpm_bcond_check = conf_to_bcond_check(tomlconf) - rpm_test_comments = conf_to_test_comments(tomlconf) + rpm_test_comments = conf_comments_to_spec_comments(tomlconf.tests_comments) cargo_test_args = conf_to_cargo_test_args(tomlconf) template_args_common = { diff --git a/rust2rpm/templates/README.md b/rust2rpm/templates/README.md index 16a72f7..115eb0f 100644 --- a/rust2rpm/templates/README.md +++ b/rust2rpm/templates/README.md @@ -22,6 +22,7 @@ | `rpm_license_comments` | `Optional[str]` | additional information returned by license string translation | | `rpm_patch_file_automatic` | `Optional[str]` | name of the automatically generated patch file | | `rpm_patch_file_manual` | `Optional[str]` | name of the manually generated patch file | +| `rpm_patch_file_comments` | `list[str]` | additional lines of comments for the manually generated patch file | | `rpm_buildrequires` | `list[str]` | list of RPM `BuildRequires` | | `rpm_test_requires` | `list[str]` | list of RPM `BuildRequires` that are gated by an `%if %{with check}` conditional | | `rpm_requires` | `dict[str, list[str]]` | map of feature names to lists of RPM `Requires` for library sub-packages | @@ -126,6 +127,7 @@ | `rpm_license_comments` | `Optional[str]` | additional information returned by license string translation | | `rpm_patch_file_automatic` | `Optional[str]` | name of the automatically generated patch file | | `rpm_patch_file_manual` | `Optional[str]` | name of the manually generated patch file | +| `rpm_patch_file_comments` | `list[str]` | additional lines of comments for the manually generated patch file | | `rpm_buildrequires` | `list[str]` | list of RPM `BuildRequires` | | `rpm_test_requires` | `list[str]` | list of RPM `BuildRequires` that are gated by an `%if %{with check}` conditional | | `rpm_license_files` | `list[str]` | list of the license files which were detected in crate sources | diff --git a/rust2rpm/templates/crate.spec b/rust2rpm/templates/crate.spec index 86a75e2..5de80cb 100644 --- a/rust2rpm/templates/crate.spec +++ b/rust2rpm/templates/crate.spec @@ -64,7 +64,7 @@ Source: %{crates_source} Source: {{ rpm_vendor_source }} {% endif %} {% for source in rpm_extra_sources %} -{% for comment in source.comments %} +{% for comment in source.comment_lines %} # {{ comment }} {% endfor %} Source{{ source.number }}:{{ source.whitespace }}{{ source.file }} @@ -79,10 +79,13 @@ Patch: {{ rpm_patch_file_automatic }} {% else %} # Manually created patch for downstream crate metadata changes {% endif %} +{% for comment_line in rpm_patch_file_comments %} +{{ comment_line }} +{% endfor %} Patch: {{ rpm_patch_file_manual }} {% endif %} {% for patch in rpm_extra_patches %} -{% for comment in patch.comments %} +{% for comment in patch.comment_lines %} # {{ comment }} {% endfor %} Patch{{ patch.number }}:{{ patch.whitespace }}{{ patch.file }} diff --git a/rust2rpm/templates/project.spec b/rust2rpm/templates/project.spec index 3280d5d..b619401 100644 --- a/rust2rpm/templates/project.spec +++ b/rust2rpm/templates/project.spec @@ -49,7 +49,7 @@ Source: # FIXME Source: {{ rpm_vendor_source }} {% endif %} {% for source in rpm_extra_sources %} -{% for comment in source.comments %} +{% for comment in source.comment_lines %} # {{ comment }} {% endfor %} Source{{ source.number }}:{{ source.whitespace }}{{ source.file }} @@ -64,10 +64,13 @@ Patch: {{ rpm_patch_file_automatic }} {% else %} # Manually created patch for downstream crate metadata changes {% endif %} +{% for comment_line in rpm_patch_file_comments %} +{{ comment_line }} +{% endfor %} Patch: {{ rpm_patch_file_manual }} {% endif %} {% for patch in rpm_extra_patches %} -{% for comment in patch.comments %} +{% for comment in patch.comment_lines %} # {{ comment }} {% endfor %} Patch{{ patch.number }}:{{ patch.whitespace }}{{ patch.file }} diff --git a/rust2rpm/templates/workspace.spec b/rust2rpm/templates/workspace.spec index b2479b3..94205fd 100644 --- a/rust2rpm/templates/workspace.spec +++ b/rust2rpm/templates/workspace.spec @@ -36,13 +36,13 @@ Source: # FIXME Source: {{ rpm_vendor_source }} {% endif %} {% for source in rpm_extra_sources %} -{% for comment in source.comments %} +{% for comment in source.comment_lines %} # {{ comment }} {% endfor %} Source{{ source.number }}:{{ source.whitespace }}{{ source.file }} {% endfor %} {% for patch in rpm_extra_patches %} -{% for comment in patch.comments %} +{% for comment in patch.comment_lines %} # {{ comment }} {% endfor %} Patch{{ patch.number }}:{{ patch.whitespace }}{{ patch.file }} diff --git a/rust2rpm/tests/test_conf.py b/rust2rpm/tests/test_conf.py index dbf756f..4393a23 100644 --- a/rust2rpm/tests/test_conf.py +++ b/rust2rpm/tests/test_conf.py @@ -2,7 +2,7 @@ from importlib import resources import pytest -from rust2rpm.conf import IniConf, TomlConf, to_list, conf_to_test_comments, conf_to_cargo_test_args +from rust2rpm.conf import IniConf, TomlConf, to_list, conf_comments_to_spec_comments, conf_to_cargo_test_args @pytest.mark.parametrize( @@ -336,7 +336,7 @@ def test_conf_toml_from_ini( def test_conf_to_test_comments(filename, features: set[str], expected: str): path = str(resources.files("rust2rpm.tests.samples").joinpath(filename)) conf = TomlConf.load(path, features) - assert conf_to_test_comments(conf) == expected + assert conf_comments_to_spec_comments(conf.tests_comments) == expected @pytest.mark.parametrize(