From f0de38ad728d4ee0b8244e3438e74b8306b7a74c Mon Sep 17 00:00:00 2001 From: Zbigniew Jędrzejewski-Szmek Date: Sep 30 2021 08:36:41 +0000 Subject: Merge #147 `Fix bug introduced by recent patches and clean up the code a bit` --- diff --git a/rust2rpm/__main__.py b/rust2rpm/__main__.py index b2d82a2..aa8846f 100644 --- a/rust2rpm/__main__.py +++ b/rust2rpm/__main__.py @@ -3,6 +3,7 @@ import configparser import contextlib from datetime import datetime, timezone import difflib +import functools import itertools import os import pathlib @@ -44,6 +45,12 @@ LICENSES = re.compile( r"GNU-.*[0-9].*|GPL-.*[0-9].*|LGPL-.*[0-9].*|MIT-.*[0-9].*|" r"MPL-.*[0-9].*|OFL-.*[0-9].*)") +def sortify(func): + """Return a sorted list from a generator""" + def wrapper(*args, **kwargs): + return sorted(func(*args, **kwargs)) + return functools.update_wrapper(wrapper, func) + def get_default_target(): try: os_release_file = open('/etc/os-release') @@ -125,6 +132,9 @@ def local_toml(toml, version): doc_files = get_doc_files(toml) license_files = get_license_files(toml) toml = os.path.join(toml, "Cargo.toml") + else: + doc_files = [] + license_files = [] return toml, None, version, doc_files, license_files @@ -205,18 +215,18 @@ def make_patch(toml, enabled=True, tmpfile=False): def _is_path(path): return "/" in path or path in {".", ".."} +@sortify def get_license_files(path): - license_files = [] exclude = { "vendor", "example", "examples", "_example", "_examples", - "testdata", "_testdata", ".github", "tests", "test"} + "testdata", "_testdata", ".github", "tests", "test" } for root, dirs, files in os.walk(path, topdown=True): dirs[:] = [d for d in dirs if d not in exclude] - license_files.extend([os.path.relpath(os.path.join(root, f), path) for f in files if LICENSES.match(f)]) - license_files.sort() - return license_files + for f in files: + if LICENSES.match(f): + yield os.path.relpath(os.path.join(root, f), path) +@sortify def get_doc_files(path): - doc_files = [] matcher = re.compile( r"(.*\.md|.*\.markdown|.*\.mdown|.*\.mkdn|.*\.rst|.*\.txt|AUTHORS|" r"AUTHORS[\.\-].*|CONTRIBUTORS|CONTRIBUTORS[\.\-].*|README|" @@ -225,9 +235,9 @@ def get_doc_files(path): matcherex = re.compile(r"CMakeLists\.txt") for root, dirs, files in os.walk(path, topdown=True): dirs[:] = [] - doc_files.extend([os.path.relpath(os.path.join(root, f), path) for f in files if matcher.match(f) and not LICENSES.match(f) and not matcherex.match(f)]) - doc_files.sort() - return doc_files + for f in files: + if matcher.match(f) and not LICENSES.match(f) and not matcherex.match(f): + yield os.path.relpath(os.path.join(root, f), path) def make_diff_metadata(crate, version, patch=False, store=False): if _is_path(crate): @@ -245,10 +255,9 @@ def make_diff_metadata(crate, version, patch=False, store=False): else: cratef, crate, version = download(crate, version) - with files_from_crate(cratef, crate, version) as files: - diff = make_patch(files[0], enabled=patch) - metadata = Metadata.from_file(files[0]) - doc_files, license_files = files[1:3] + with files_from_crate(cratef, crate, version) as (toml, doc_files, license_files): + diff = make_patch(toml, enabled=patch) + metadata = Metadata.from_file(toml) if store: shutil.copy2(cratef, os.path.join(os.getcwd(), f"{metadata.name}-{version}.crate")) return crate, diff, metadata, doc_files, license_files