conf/generator: add support for persisting Cargo.toml patch comments

This commit is contained in:
Fabio Valentini 2024-03-08 15:51:32 +01:00
parent fd2c04784c
commit d5fd8f9517
No known key found for this signature in database
GPG key ID: 5AC5F572E5D410AF
8 changed files with 60 additions and 16 deletions

View file

@ -152,6 +152,13 @@ def main():
log.error(f"This is not supported by rust2rpm; remove the '+{build_meta}' suffix.") log.error(f"This is not supported by rust2rpm; remove the '+{build_meta}' suffix.")
sys.exit(1) 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) warn_if_package_uses_restrictive_dependencies(package)
spec_contents = spec_render_project( spec_contents = spec_render_project(
@ -183,6 +190,13 @@ def main():
log.error("Building library-only crates with vendored dependencies is not supported.") log.error("Building library-only crates with vendored dependencies is not supported.")
sys.exit(1) 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) warn_if_package_uses_restrictive_dependencies(package)
try: try:

View file

@ -45,6 +45,13 @@ TOML_SCHEMA = {
"cargo-install-lib": { "cargo-install-lib": {
"type": "boolean", "type": "boolean",
}, },
# comments for manual Cargo.toml patches (rust2rpm -p)
"cargo-toml-patch-comments": {
"type": "array",
"items": {
"type": "string",
},
},
# additional source files # additional source files
"extra-sources": { "extra-sources": {
"type": "array", "type": "array",
@ -483,6 +490,10 @@ class SourceOrPatch:
def comments(self) -> list[str]: def comments(self) -> list[str]:
return self._data.get("comments") or list() return self._data.get("comments") or list()
@property
def comment_lines(self) -> list[str]:
return conf_comments_to_spec_comments(self.comments)
@property @property
def whitespace(self) -> str: def whitespace(self) -> str:
return " " * (16 - (len("Source") + len(str(self.number)) + 1)) return " " * (16 - (len("Source") + len(str(self.number)) + 1))
@ -547,6 +558,17 @@ class TomlConf:
else: else:
return None 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 @property
def package_extra_sources(self) -> Optional[list[SourceOrPatch]]: def package_extra_sources(self) -> Optional[list[SourceOrPatch]]:
if package := self._package: if package := self._package:
@ -932,10 +954,8 @@ def conf_to_bcond_check(conf: TomlConf) -> bool:
return True return True
def conf_to_test_comments(conf: TomlConf) -> list[str]: def conf_comments_to_spec_comments(comments: Optional[list[str]]) -> list[str]:
if conf.tests_comments: if not comments:
comments = conf.tests_comments
else:
return [] return []
lines = [] lines = []

View file

@ -11,7 +11,7 @@ from cargo2rpm import rpm
import jinja2 import jinja2
from rust2rpm import __version__, log 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.licensing import translate_license
from rust2rpm.metadata import package_uses_rust_1_60_feature_syntax, get_required_features_for_binaries 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 conf_supported_arches = None
rpm_bcond_check = conf_to_bcond_check(tomlconf) 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) cargo_test_args = conf_to_cargo_test_args(tomlconf)
template_args_common = { template_args_common = {
@ -341,6 +341,7 @@ def spec_render_crate(
"rpm_license_comments": rpm_license_comments, "rpm_license_comments": rpm_license_comments,
"rpm_patch_file_automatic": patch_file_automatic, "rpm_patch_file_automatic": patch_file_automatic,
"rpm_patch_file_manual": patch_file_manual, "rpm_patch_file_manual": patch_file_manual,
"rpm_patch_file_comments": tomlconf.package_cargo_toml_patch_comment_lines,
"rpm_buildrequires": rpm_buildrequires, "rpm_buildrequires": rpm_buildrequires,
"rpm_test_requires": rpm_test_requires, "rpm_test_requires": rpm_test_requires,
"rpm_requires": rpm_requires, "rpm_requires": rpm_requires,
@ -527,7 +528,7 @@ def spec_render_project(
conf_supported_arches = None conf_supported_arches = None
rpm_bcond_check = conf_to_bcond_check(tomlconf) 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) cargo_test_args = conf_to_cargo_test_args(tomlconf)
template_args_common = { template_args_common = {
@ -545,6 +546,7 @@ def spec_render_project(
"rpm_license_comments": rpm_license_comments, "rpm_license_comments": rpm_license_comments,
"rpm_patch_file_automatic": patch_file_automatic, "rpm_patch_file_automatic": patch_file_automatic,
"rpm_patch_file_manual": patch_file_manual, "rpm_patch_file_manual": patch_file_manual,
"rpm_patch_file_comments": tomlconf.package_cargo_toml_patch_comment_lines,
"rpm_buildrequires": rpm_buildrequires, "rpm_buildrequires": rpm_buildrequires,
"rpm_test_requires": rpm_test_requires, "rpm_test_requires": rpm_test_requires,
"rpm_license_files": license_files, "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_license_tag, rpm_license_comments = translate_license(target, license_str)
rpm_bcond_check = conf_to_bcond_check(tomlconf) 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) cargo_test_args = conf_to_cargo_test_args(tomlconf)
template_args_common = { template_args_common = {

View file

@ -22,6 +22,7 @@
| `rpm_license_comments` | `Optional[str]` | additional information returned by license string translation | | `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_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_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_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_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 | | `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_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_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_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_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_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 | | `rpm_license_files` | `list[str]` | list of the license files which were detected in crate sources |

View file

@ -64,7 +64,7 @@ Source: %{crates_source}
Source: {{ rpm_vendor_source }} Source: {{ rpm_vendor_source }}
{% endif %} {% endif %}
{% for source in rpm_extra_sources %} {% for source in rpm_extra_sources %}
{% for comment in source.comments %} {% for comment in source.comment_lines %}
# {{ comment }} # {{ comment }}
{% endfor %} {% endfor %}
Source{{ source.number }}:{{ source.whitespace }}{{ source.file }} Source{{ source.number }}:{{ source.whitespace }}{{ source.file }}
@ -79,10 +79,13 @@ Patch: {{ rpm_patch_file_automatic }}
{% else %} {% else %}
# Manually created patch for downstream crate metadata changes # Manually created patch for downstream crate metadata changes
{% endif %} {% endif %}
{% for comment_line in rpm_patch_file_comments %}
{{ comment_line }}
{% endfor %}
Patch: {{ rpm_patch_file_manual }} Patch: {{ rpm_patch_file_manual }}
{% endif %} {% endif %}
{% for patch in rpm_extra_patches %} {% for patch in rpm_extra_patches %}
{% for comment in patch.comments %} {% for comment in patch.comment_lines %}
# {{ comment }} # {{ comment }}
{% endfor %} {% endfor %}
Patch{{ patch.number }}:{{ patch.whitespace }}{{ patch.file }} Patch{{ patch.number }}:{{ patch.whitespace }}{{ patch.file }}

View file

@ -49,7 +49,7 @@ Source: # FIXME
Source: {{ rpm_vendor_source }} Source: {{ rpm_vendor_source }}
{% endif %} {% endif %}
{% for source in rpm_extra_sources %} {% for source in rpm_extra_sources %}
{% for comment in source.comments %} {% for comment in source.comment_lines %}
# {{ comment }} # {{ comment }}
{% endfor %} {% endfor %}
Source{{ source.number }}:{{ source.whitespace }}{{ source.file }} Source{{ source.number }}:{{ source.whitespace }}{{ source.file }}
@ -64,10 +64,13 @@ Patch: {{ rpm_patch_file_automatic }}
{% else %} {% else %}
# Manually created patch for downstream crate metadata changes # Manually created patch for downstream crate metadata changes
{% endif %} {% endif %}
{% for comment_line in rpm_patch_file_comments %}
{{ comment_line }}
{% endfor %}
Patch: {{ rpm_patch_file_manual }} Patch: {{ rpm_patch_file_manual }}
{% endif %} {% endif %}
{% for patch in rpm_extra_patches %} {% for patch in rpm_extra_patches %}
{% for comment in patch.comments %} {% for comment in patch.comment_lines %}
# {{ comment }} # {{ comment }}
{% endfor %} {% endfor %}
Patch{{ patch.number }}:{{ patch.whitespace }}{{ patch.file }} Patch{{ patch.number }}:{{ patch.whitespace }}{{ patch.file }}

View file

@ -36,13 +36,13 @@ Source: # FIXME
Source: {{ rpm_vendor_source }} Source: {{ rpm_vendor_source }}
{% endif %} {% endif %}
{% for source in rpm_extra_sources %} {% for source in rpm_extra_sources %}
{% for comment in source.comments %} {% for comment in source.comment_lines %}
# {{ comment }} # {{ comment }}
{% endfor %} {% endfor %}
Source{{ source.number }}:{{ source.whitespace }}{{ source.file }} Source{{ source.number }}:{{ source.whitespace }}{{ source.file }}
{% endfor %} {% endfor %}
{% for patch in rpm_extra_patches %} {% for patch in rpm_extra_patches %}
{% for comment in patch.comments %} {% for comment in patch.comment_lines %}
# {{ comment }} # {{ comment }}
{% endfor %} {% endfor %}
Patch{{ patch.number }}:{{ patch.whitespace }}{{ patch.file }} Patch{{ patch.number }}:{{ patch.whitespace }}{{ patch.file }}

View file

@ -2,7 +2,7 @@ from importlib import resources
import pytest 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( @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): def test_conf_to_test_comments(filename, features: set[str], expected: str):
path = str(resources.files("rust2rpm.tests.samples").joinpath(filename)) path = str(resources.files("rust2rpm.tests.samples").joinpath(filename))
conf = TomlConf.load(path, features) 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( @pytest.mark.parametrize(