fedora: check existing spec file for rpmautospec

If we have a new file: assume yes. If there was an old file, convert
only when the old file had %autochangelog.

The regexp for %autochangelog is taken from
https://pagure.io/fedora-infra/rpmautospec/pull-request/219.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2021-09-20 11:59:17 +02:00
parent 31afa28c74
commit 7e0afd5da2

View file

@ -5,6 +5,7 @@ from datetime import datetime, timezone
import difflib
import itertools
import os
import pathlib
import re
import shlex
import shutil
@ -258,9 +259,24 @@ def to_list(s):
return []
return list(filter(None, (l.strip() for l in s.splitlines())))
def detect_rpmautospec(default_target, spec_file):
# We default to on only for selected distros for now…
if default_target not in {"fedora"}:
return False
try:
text = spec_file.read_text()
except FileNotFoundError:
# A new file, let's try the new thing.
return True
# We are redoing an existing spec file. Figure out if it had
# %autochangelog enabled before.
autochangelog_re = r"^\s*%(?:autochangelog|\{\??autochangelog\})\b"
return any(re.match(autochangelog_re, line) for line in text.splitlines())
def main():
default_target = get_default_target()
default_rpmautospec = default_target in {"fedora"}
parser = argparse.ArgumentParser("rust2rpm",
formatter_class=argparse.RawTextHelpFormatter)
@ -279,7 +295,7 @@ def main():
help="Do initial patching of Cargo.toml")
parser.add_argument("-s", "--store-crate", action="store_true",
help="Store crate in current directory")
parser.add_argument("-a", "--rpmautospec", action="store_true", default=default_rpmautospec,
parser.add_argument("-a", "--rpmautospec", action="store_true",
help="Use autorelease and autochangelog features")
parser.add_argument("--all-features", action="store_true",
help="Activate all available features")
@ -340,8 +356,22 @@ def main():
raise ValueError("No bins and no libs")
kwargs["include_devel"] = is_lib
if args.suffix is not None:
if metadata.name[-1].isdigit():
suffix = f'-{args.suffix}'
else:
suffix = args.suffix
else:
suffix = ""
kwargs["pkg_suffix"] = suffix
spec_file = pathlib.Path(f"rust-{metadata.name}{suffix}.spec")
kwargs["auto_changelog_entry"] = not args.no_auto_changelog_entry
kwargs["rpmautospec"] = args.rpmautospec
if args.rpmautospec:
kwargs["rpmautospec"] = True
else:
kwargs["rpmautospec"] = detect_rpmautospec(default_target, spec_file)
if args.target in {"fedora", "mageia", "opensuse"}:
kwargs["include_build_requires"] = True
@ -394,15 +424,6 @@ def main():
kwargs["distconf"] = conf[args.target]
if args.suffix is not None:
if metadata.name[-1].isdigit():
suffix = f'-{args.suffix}'
else:
suffix = args.suffix
else:
suffix = ""
kwargs["pkg_suffix"] = suffix
spec_file = f"rust-{metadata.name}{suffix}.spec"
spec_contents = template.render(md=metadata, patch_file=patch_file, **kwargs)
if args.stdout:
print(f"# {spec_file}")