From 120d609f588444279f7d4617f7022b36c7461a3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Tue, 28 Sep 2021 13:08:31 +0200 Subject: [PATCH 1/3] Fix crash when path to a toml file is given Fixes a programming error introduced in b284d61ee7, the variables were not defined. --- rust2rpm/__main__.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/rust2rpm/__main__.py b/rust2rpm/__main__.py index b2d82a2..b57e312 100644 --- a/rust2rpm/__main__.py +++ b/rust2rpm/__main__.py @@ -125,6 +125,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 From bdaa087c170d5afe5a9df9f2e8ca5d5d6e691e23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Tue, 28 Sep 2021 13:19:03 +0200 Subject: [PATCH 2/3] Refactor the code to generate file lists The code was very hard to read because it was doing explicit lists concatenations. Let's make the code more python by using 'yield' on the interesting items, and hide the creation of the list in a decorator. --- rust2rpm/__main__.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/rust2rpm/__main__.py b/rust2rpm/__main__.py index b57e312..65a9491 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') @@ -208,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|" @@ -228,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): From a2b42c056c3451f9fea9d2361a7113d0ab9750e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Tue, 28 Sep 2021 13:20:26 +0200 Subject: [PATCH 3/3] Rector explicit indexing into a tuple unpacking --- rust2rpm/__main__.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/rust2rpm/__main__.py b/rust2rpm/__main__.py index 65a9491..aa8846f 100644 --- a/rust2rpm/__main__.py +++ b/rust2rpm/__main__.py @@ -255,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