From 04eb5b2e7f4f91be8330bf106d0529534ddecda7 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 5 May 2017 12:02:13 +0300 Subject: [PATCH] optimize_upip.py: Rework inclusion/exclusion logic. We should include files by default, as a package may contain arbitrary files, e.g. as resources. So, rework inclusion/exlusion logic to work using incremental refinement. --- optimize_upip.py | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/optimize_upip.py b/optimize_upip.py index fd895c7c..69d4ea6f 100644 --- a/optimize_upip.py +++ b/optimize_upip.py @@ -49,8 +49,12 @@ def recompress_latest(dir): recompress(latest) -EXCLUDE = [r".+/setup.py"] -INCLUDE = [r".+\.py", r".+\.egg-info/(PKG-INFO|requires\.txt)"] +FILTERS = [ + # include, exclude, repeat + (r".+\.egg-info/(PKG-INFO|requires\.txt)", r"setup.py$"), + (r".+\.py$", r"[^/]+$"), + (None, r".+\.egg-info/.+"), +] outbuf = io.BytesIO() @@ -60,18 +64,29 @@ def filter_tar(name): fout = tarfile.open(fileobj=outbuf, mode="w") for info in fin: # print(info) - include = None - for p in EXCLUDE: - if re.match(p, info.name): - include = False - break - if include is None: - for p in INCLUDE: - if re.match(p, info.name): - include = True - print("Including:", info.name) - if not include: + if not "/" in info.name: continue + fname = info.name.split("/", 1)[1] + include = None + + for inc_re, exc_re in FILTERS: + if include is None and inc_re: + if re.match(inc_re, fname): + include = True + + if include is None and exc_re: + if re.match(exc_re, fname): + include = False + + if include is None: + include = True + + if include: + print("Including:", fname) + else: + print("Excluding:", fname) + continue + farch = fin.extractfile(info) fout.addfile(info, farch) fout.close()