Merge #147 Fix bug introduced by recent patches and clean up the code a bit

This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2021-09-30 08:36:41 +00:00
commit f0de38ad72

View file

@ -3,6 +3,7 @@ import configparser
import contextlib import contextlib
from datetime import datetime, timezone from datetime import datetime, timezone
import difflib import difflib
import functools
import itertools import itertools
import os import os
import pathlib import pathlib
@ -44,6 +45,12 @@ LICENSES = re.compile(
r"GNU-.*[0-9].*|GPL-.*[0-9].*|LGPL-.*[0-9].*|MIT-.*[0-9].*|" r"GNU-.*[0-9].*|GPL-.*[0-9].*|LGPL-.*[0-9].*|MIT-.*[0-9].*|"
r"MPL-.*[0-9].*|OFL-.*[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(): def get_default_target():
try: try:
os_release_file = open('/etc/os-release') os_release_file = open('/etc/os-release')
@ -125,6 +132,9 @@ def local_toml(toml, version):
doc_files = get_doc_files(toml) doc_files = get_doc_files(toml)
license_files = get_license_files(toml) license_files = get_license_files(toml)
toml = os.path.join(toml, "Cargo.toml") toml = os.path.join(toml, "Cargo.toml")
else:
doc_files = []
license_files = []
return toml, None, version, 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): def _is_path(path):
return "/" in path or path in {".", ".."} return "/" in path or path in {".", ".."}
@sortify
def get_license_files(path): def get_license_files(path):
license_files = []
exclude = { "vendor", "example", "examples", "_example", "_examples", 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): for root, dirs, files in os.walk(path, topdown=True):
dirs[:] = [d for d in dirs if d not in exclude] 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)]) for f in files:
license_files.sort() if LICENSES.match(f):
return license_files yield os.path.relpath(os.path.join(root, f), path)
@sortify
def get_doc_files(path): def get_doc_files(path):
doc_files = []
matcher = re.compile( matcher = re.compile(
r"(.*\.md|.*\.markdown|.*\.mdown|.*\.mkdn|.*\.rst|.*\.txt|AUTHORS|" r"(.*\.md|.*\.markdown|.*\.mdown|.*\.mkdn|.*\.rst|.*\.txt|AUTHORS|"
r"AUTHORS[\.\-].*|CONTRIBUTORS|CONTRIBUTORS[\.\-].*|README|" r"AUTHORS[\.\-].*|CONTRIBUTORS|CONTRIBUTORS[\.\-].*|README|"
@ -225,9 +235,9 @@ def get_doc_files(path):
matcherex = re.compile(r"CMakeLists\.txt") matcherex = re.compile(r"CMakeLists\.txt")
for root, dirs, files in os.walk(path, topdown=True): for root, dirs, files in os.walk(path, topdown=True):
dirs[:] = [] 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)]) for f in files:
doc_files.sort() if matcher.match(f) and not LICENSES.match(f) and not matcherex.match(f):
return doc_files yield os.path.relpath(os.path.join(root, f), path)
def make_diff_metadata(crate, version, patch=False, store=False): def make_diff_metadata(crate, version, patch=False, store=False):
if _is_path(crate): if _is_path(crate):
@ -245,10 +255,9 @@ def make_diff_metadata(crate, version, patch=False, store=False):
else: else:
cratef, crate, version = download(crate, version) cratef, crate, version = download(crate, version)
with files_from_crate(cratef, crate, version) as files: with files_from_crate(cratef, crate, version) as (toml, doc_files, license_files):
diff = make_patch(files[0], enabled=patch) diff = make_patch(toml, enabled=patch)
metadata = Metadata.from_file(files[0]) metadata = Metadata.from_file(toml)
doc_files, license_files = files[1:3]
if store: if store:
shutil.copy2(cratef, os.path.join(os.getcwd(), f"{metadata.name}-{version}.crate")) shutil.copy2(cratef, os.path.join(os.getcwd(), f"{metadata.name}-{version}.crate"))
return crate, diff, metadata, doc_files, license_files return crate, diff, metadata, doc_files, license_files