cli: add automatic --compat flag, deprecate manual --suffix argument

This commit is contained in:
Fabio Valentini 2023-03-18 20:15:54 +01:00
parent d0d3742a07
commit 26aa680301
No known key found for this signature in database
GPG key ID: 5AC5F572E5D410AF
5 changed files with 61 additions and 3 deletions

View file

@ -18,6 +18,7 @@ from rust2rpm.utils import (
detect_rpmautospec, detect_rpmautospec,
exit_on_common_errors, exit_on_common_errors,
guess_crate_name, guess_crate_name,
package_name_compat,
package_name_suffixed, package_name_suffixed,
) )
@ -57,7 +58,14 @@ def main():
rpm_name = project rpm_name = project
else: else:
base_name = metadata.packages[0].name base_name = metadata.packages[0].name
rpm_name = package_name_suffixed(base_name, args.suffix)
if args.suffix:
log.warn("The '--suffix' argument is deprecated, use the '--compat' flag instead.")
rpm_name = package_name_suffixed(base_name, args.suffix)
elif args.compat:
rpm_name = package_name_compat(base_name, args.version)
else:
rpm_name = package_name_suffixed(base_name, None)
patch_files = ( patch_files = (
f"{base_name}-fix-metadata-auto.diff" if diffs[0] else None, f"{base_name}-fix-metadata-auto.diff" if diffs[0] else None,

View file

@ -87,6 +87,11 @@ def get_parser():
action="store", action="store",
help="Package suffix", help="Package suffix",
) )
parser.add_argument(
"--compat",
action="store_true",
help="Create a compat package with appropriate version suffix",
)
parser.add_argument( parser.add_argument(
"-i", "-i",
"--interactive", "--interactive",

View file

@ -0,0 +1,31 @@
import pytest
from rust2rpm.utils import package_name_suffixed, package_name_compat
@pytest.mark.parametrize(
"crate,suffix,expected",
[
("syn", "0.4", "rust-syn0.4"),
("syn", "1", "rust-syn1"),
("socket2", "0.3", "rust-socket2_0.3"),
("version-compare", "0.0.11", "rust-version-compare0.0.11"),
],
ids=repr,
)
def test_package_name_suffixed(crate: str, suffix: str, expected: str):
package_name_suffixed(crate, suffix) == expected
@pytest.mark.parametrize(
"crate,version,expected",
[
("syn", "0.4.0", "rust-syn0.4"),
("syn", "1.0.109", "rust-syn1"),
("socket2", "0.3.4", "rust-socket2_0.3"),
("version-compare", "0.0.11", "rust-version-compare0.0.11"),
],
ids=repr,
)
def test_package_name_compat(crate: str, version: str, expected: str):
package_name_compat(crate, version) == expected

View file

@ -7,6 +7,9 @@ import shlex
import shutil import shutil
import subprocess import subprocess
import requests import requests
from typing import Optional
from cargo2rpm.metadata import Version
from rust2rpm import log from rust2rpm import log
@ -124,11 +127,22 @@ def guess_crate_name():
return None return None
def package_name_suffixed(name, suffix): def package_name_suffixed(name: str, suffix: Optional[str]):
joiner = "_" if suffix and name[-1].isdigit() else "" joiner = "_" if suffix and name[-1].isdigit() else ""
return "rust-" + name + joiner + (suffix or "") return "rust-" + name + joiner + (suffix or "")
def package_name_compat(name: str, version: str):
sv = Version.parse(version)
if sv.major > 0:
suffix = str(sv.major)
elif sv.minor > 0:
suffix = f"0.{str(sv.minor)}"
else:
suffix = f"0.0.{str(sv.patch)}"
return package_name_suffixed(name, suffix)
def detect_rpmautospec(default_target, spec_file): def detect_rpmautospec(default_target, spec_file):
"""Guess whether %autorelease+%autochangelog should be used """Guess whether %autorelease+%autochangelog should be used

View file

@ -1,5 +1,5 @@
[tox] [tox]
envlist = py310,py311 envlist = py310,py311,py312
skipsdist = True skipsdist = True
[testenv] [testenv]