diff --git a/rust2rpm/__main__.py b/rust2rpm/__main__.py index 2bb25cb..f258e29 100644 --- a/rust2rpm/__main__.py +++ b/rust2rpm/__main__.py @@ -15,29 +15,18 @@ import shutil import sys import tarfile import tempfile -import time import subprocess -import jinja2 import requests import tqdm -from . import Metadata, licensing, __version__ -from .metadata import normalize_deps +from . import Metadata, licensing, generator DEFAULT_EDITOR = "vi" XDG_CACHE_HOME = os.getenv("XDG_CACHE_HOME", os.path.expanduser("~/.cache")) CACHEDIR = os.path.join(XDG_CACHE_HOME, "rust2rpm") API_URL = "https://crates.io/api/v1/" DIST_GIT_URL = "https://src.fedoraproject.org/api/0/" -JINJA_ENV = jinja2.Environment( - loader=jinja2.ChoiceLoader([ - jinja2.FileSystemLoader(["/"]), - jinja2.PackageLoader("rust2rpm", "templates"), - ]), - extensions=["jinja2.ext.do"], - trim_blocks=True, - lstrip_blocks=True) LICENSES = re.compile(r""" COPYING(?:[.-].*)?|COPYRIGHT(?:[.-].*)?| EULA(?:[.-].*)?|[Ll]icen[cs]e|[Ll]icen[cs]e.*| @@ -333,10 +322,6 @@ def make_diff_metadata(args, crate, version): shutil.copy2(cratef, os.path.join(os.getcwd(), f"{metadata.name}-{version}.crate")) return crate, diff, metadata, doc_files, license_files -def to_list(s): - if not s: - return [] - return list(filter(None, (l.strip() for l in s.splitlines()))) def detect_rpmautospec(default_target, spec_file): """Guess whether %autorelease+%autochangelog should be used @@ -486,34 +471,9 @@ def main(): crate, diff, metadata, doc_files, license_files = make_diff_metadata(args, args.crate, args.version) - JINJA_ENV.globals["normalize_deps"] = normalize_deps - JINJA_ENV.globals["to_list"] = to_list - template = JINJA_ENV.get_template("main.spec") + patch_file = f"{metadata.name}-fix-metadata.diff" if diff else None - if args.patch and len(diff) > 0: - patch_file = f"{metadata.name}-fix-metadata.diff" - else: - patch_file = None - - kwargs = {} - kwargs["generator_version"] = __version__ - kwargs["crate"] = crate - kwargs["target"] = args.target - - bins = [tgt for tgt in metadata.targets if tgt.kind == "bin"] - libs = [tgt for tgt in metadata.targets if tgt.kind in {"lib", "rlib", "proc-macro"}] - is_bin = len(bins) > 0 - is_lib = len(libs) > 0 - if is_bin: - kwargs["include_main"] = True - kwargs["bins"] = bins - elif is_lib: - kwargs["include_main"] = False - else: - raise ValueError("No bins and no libs") - kwargs["include_devel"] = is_lib - - kwargs["pkg_name"] = pkg_name = package_name_suffixed(metadata.name, args.suffix) + pkg_name = package_name_suffixed(metadata.name, args.suffix) spec_file = pathlib.Path(f"{pkg_name}.spec") if args.target in {"fedora"} and args.existence_check and not os.path.exists(spec_file): @@ -524,58 +484,13 @@ def main(): print("Re-run with --no-existence-check if you still want to convert it.") sys.exit(1) - kwargs["auto_changelog_entry"] = args.auto_changelog_entry - - rpmautospec = args.rpmautospec - if rpmautospec is None: - rpmautospec = detect_rpmautospec(args.target, spec_file) - kwargs["rpmautospec"] = rpmautospec - - kwargs["relative_license_paths"] = args.relative_license_paths - - if args.target in {"fedora", "mageia", "opensuse"}: - kwargs["include_build_requires"] = True - kwargs["include_provides"] = False - kwargs["include_requires"] = False - elif args.target == "plain": - kwargs["include_build_requires"] = True - kwargs["include_provides"] = True - kwargs["include_requires"] = True - else: - assert False, f"Unknown target {args.target!r}" - - if args.target == "mageia": - kwargs["pkg_release"] = "%mkrel 1" - kwargs["rust_group"] = "Development/Rust" - elif args.target == "opensuse": - kwargs["spec_copyright_year"] = time.strftime("%Y") - kwargs["pkg_release"] = "0" - kwargs["rust_group"] = "Development/Libraries/Rust" - elif rpmautospec: - kwargs["pkg_release"] = "%autorelease" - else: - kwargs["pkg_release"] = "1%{?dist}" + if args.rpmautospec is None: + args.rpmautospec = detect_rpmautospec(args.target, spec_file) if args.dynamic_buildrequires is None: args.dynamic_buildrequires = args.target == "fedora" - kwargs["generate_buildrequires"] = args.dynamic_buildrequires - - if args.target in {"opensuse"}: - kwargs["date"] = time.strftime("%a %b %d %T %Z %Y") - else: - kwargs["date"] = time.strftime("%a %b %d %Y") packager_identity = detect_packager() - if packager_identity is not None: - kwargs["packager"] = packager_identity - - if metadata.license is not None: - license, comments = licensing.translate_license(args.target, metadata.license) - kwargs["license"] = license - kwargs["license_comments"] = comments - - kwargs["doc_files"] = doc_files - kwargs["license_files"] = license_files conf = configparser.ConfigParser(interpolation=configparser.ExtendedInterpolation()) confs = conf.read([".rust2rpm.conf", "_rust2rpm.conf", "rust2rpm.conf"]) @@ -597,19 +512,24 @@ def main(): if args.target not in conf: conf.add_section(args.target) - kwargs["distconf"] = conf[args.target] - conf_all_features = conf[args.target].getboolean("all-features") - kwargs["all_features"] = conf_all_features or args.all_features - if conf_all_features is False and args.all_features: print("WARNING: Conflicting settings for enabling all features: The setting is \"false\"") print(" in rust2rpm.conf but it was enabled with the \"--all-features\" CLI flag.") - spec_contents = template.render( - metadata=metadata, - patch_file=patch_file, - **kwargs) + spec_contents = generator.spec_file_render( + args = args, + pkg_name = pkg_name, + crate = crate, + metadata = metadata, + patch_file = patch_file, + packager_identity = packager_identity, + doc_files = doc_files, + license_files = license_files, + distconf = conf[args.target], + all_features = conf_all_features or args.all_features, + ) + if args.stdout: print(f"# {spec_file}") print(spec_contents) diff --git a/rust2rpm/generator.py b/rust2rpm/generator.py new file mode 100644 index 0000000..8e2bf6c --- /dev/null +++ b/rust2rpm/generator.py @@ -0,0 +1,111 @@ +import time + +import jinja2 + +from . import __version__, metadata, licensing + +def to_list(s): + if not s: + return [] + return list(filter(None, (l.strip() for l in s.splitlines()))) + +def spec_file_template(): + env = jinja2.Environment( + loader=jinja2.ChoiceLoader([ + jinja2.FileSystemLoader(['/']), + jinja2.PackageLoader('rust2rpm', 'templates'), + ]), + extensions=['jinja2.ext.do'], + trim_blocks=True, + lstrip_blocks=True) + + env.globals['normalize_deps'] = metadata.normalize_deps + env.globals['to_list'] = to_list + + return env.get_template('main.spec') + +def spec_file_render( + args, + pkg_name, + crate, + metadata, + patch_file, + packager_identity, + doc_files, + license_files, + distconf, + all_features, +): + template = spec_file_template() + + bins = [tgt for tgt in metadata.targets if tgt.kind == "bin"] + libs = [tgt for tgt in metadata.targets if tgt.kind in {"lib", "rlib", "proc-macro"}] + is_bin = len(bins) > 0 + is_lib = len(libs) > 0 + + kwargs = { + 'generator_version': __version__, + 'target': args.target, + 'crate': crate, + 'include_devel': is_lib, + 'pkg_name': pkg_name, + 'auto_changelog_entry': args.auto_changelog_entry, + 'rpmautospec': args.rpmautospec, + 'relative_license_paths': args.relative_license_paths, + 'generate_buildrequires': args.dynamic_buildrequires, + 'doc_files': doc_files, + 'license_files': license_files, + 'distconf': distconf, + 'all_features': all_features, + } + + if is_bin: + kwargs['include_main'] = True + kwargs['bins'] = bins + elif is_lib: + kwargs['include_main'] = False + else: + raise ValueError('No bins and no libs') + + if args.target in {'fedora', 'mageia', 'opensuse'}: + kwargs['include_build_requires'] = True + kwargs['include_provides'] = False + kwargs['include_requires'] = False + elif args.target == 'plain': + kwargs['include_build_requires'] = True + kwargs['include_provides'] = True + kwargs['include_requires'] = True + else: + assert False, f'Unknown target {args.target!r}' + + if args.target == 'mageia': + kwargs['pkg_release'] = '%mkrel 1' + kwargs['rust_group'] = 'Development/Rust' + elif args.target == 'opensuse': + kwargs['spec_copyright_year'] = time.strftime('%Y') + kwargs['pkg_release'] = '0' + kwargs['rust_group'] = 'Development/Libraries/Rust' + elif args.rpmautospec: + kwargs['pkg_release'] = '%autorelease' + else: + kwargs['pkg_release'] = '1%{?dist}' + + if args.target in {'opensuse'}: + kwargs['date'] = time.strftime('%a %b %d %T %Z %Y') + else: + kwargs['date'] = time.strftime('%a %b %d %Y') + + if packager_identity is not None: + kwargs['packager'] = packager_identity + + if metadata.license is not None: + license, comments = licensing.translate_license(args.target, metadata.license) + kwargs["license"] = license + kwargs["license_comments"] = comments + + spec_contents = template.render( + metadata=metadata, + patch_file=patch_file, + **kwargs) + + return spec_contents