|
| 1 | +# This module is part of Pycopy https://github.com/pfalcon/pycopy |
| 2 | +# and pycopy-lib https://github.com/pfalcon/pycopy-lib, projects to |
| 3 | +# create a (very) lightweight full-stack Python distribution. |
| 4 | +# |
| 5 | +# Copyright (c) 2016-2019 Paul Sokolovsky |
| 6 | +# Licence: MIT |
| 7 | +# |
| 8 | +# This module overrides distutils (also compatible with setuptools) "sdist" |
| 9 | +# command to perform pre- and post-processing as required for Pycopy's |
| 10 | +# upip package manager. |
| 11 | +# |
| 12 | +# Preprocessing steps: |
| 13 | +# * Creation of Python resource module (R.py) from each top-level package's |
| 14 | +# resources. |
| 15 | +# Postprocessing steps: |
| 16 | +# * Removing metadata files not used by upip (this includes setup.py) |
| 17 | +# * Recompressing gzip archive with 4K dictionary size so it can be |
| 18 | +# installed even on low-heap targets. |
| 19 | +# |
| 20 | +import sys |
| 21 | +import os |
| 22 | +import zlib |
| 23 | +from subprocess import Popen, PIPE |
| 24 | +import glob |
| 25 | +import tarfile |
| 26 | +import re |
| 27 | +import io |
| 28 | + |
| 29 | +from distutils.filelist import FileList |
| 30 | +from setuptools.command.sdist import sdist as _sdist |
| 31 | + |
| 32 | + |
| 33 | +def gzip_4k(inf, fname): |
| 34 | + comp = zlib.compressobj(level=9, wbits=16 + 12) |
| 35 | + with open(fname + ".out", "wb") as outf: |
| 36 | + while 1: |
| 37 | + data = inf.read(1024) |
| 38 | + if not data: |
| 39 | + break |
| 40 | + outf.write(comp.compress(data)) |
| 41 | + outf.write(comp.flush()) |
| 42 | + os.rename(fname, fname + ".orig") |
| 43 | + os.rename(fname + ".out", fname) |
| 44 | + |
| 45 | + |
| 46 | +FILTERS = [ |
| 47 | + # include, exclude, repeat |
| 48 | + (r".+\.egg-info/(PKG-INFO|requires\.txt)", r"setup.py$"), |
| 49 | + (r".+\.py$", r"[^/]+$"), |
| 50 | + (None, r".+\.egg-info/.+"), |
| 51 | +] |
| 52 | + |
| 53 | + |
| 54 | +outbuf = io.BytesIO() |
| 55 | + |
| 56 | +def filter_tar(name): |
| 57 | + fin = tarfile.open(name, "r:gz") |
| 58 | + fout = tarfile.open(fileobj=outbuf, mode="w") |
| 59 | + for info in fin: |
| 60 | +# print(info) |
| 61 | + if not "/" in info.name: |
| 62 | + continue |
| 63 | + fname = info.name.split("/", 1)[1] |
| 64 | + include = None |
| 65 | + |
| 66 | + for inc_re, exc_re in FILTERS: |
| 67 | + if include is None and inc_re: |
| 68 | + if re.match(inc_re, fname): |
| 69 | + include = True |
| 70 | + |
| 71 | + if include is None and exc_re: |
| 72 | + if re.match(exc_re, fname): |
| 73 | + include = False |
| 74 | + |
| 75 | + if include is None: |
| 76 | + include = True |
| 77 | + |
| 78 | + if include: |
| 79 | + print("including:", fname) |
| 80 | + else: |
| 81 | + print("excluding:", fname) |
| 82 | + continue |
| 83 | + |
| 84 | + farch = fin.extractfile(info) |
| 85 | + fout.addfile(info, farch) |
| 86 | + fout.close() |
| 87 | + fin.close() |
| 88 | + |
| 89 | + |
| 90 | +def make_resource_module(manifest_files): |
| 91 | + resources = [] |
| 92 | + # Any non-python file included in manifest is resource |
| 93 | + for fname in manifest_files: |
| 94 | + ext = fname.rsplit(".", 1) |
| 95 | + if len(ext) > 1: |
| 96 | + ext = ext[1] |
| 97 | + else: |
| 98 | + ext = "" |
| 99 | + if ext != "py": |
| 100 | + resources.append(fname) |
| 101 | + |
| 102 | + if resources: |
| 103 | + print("creating resource module R.py") |
| 104 | + resources.sort() |
| 105 | + last_pkg = None |
| 106 | + r_file = None |
| 107 | + for fname in resources: |
| 108 | + try: |
| 109 | + pkg, res_name = fname.split("/", 1) |
| 110 | + except ValueError: |
| 111 | + print("not treating %s as a resource" % fname) |
| 112 | + continue |
| 113 | + if last_pkg != pkg: |
| 114 | + last_pkg = pkg |
| 115 | + if r_file: |
| 116 | + r_file.write("}\n") |
| 117 | + r_file.close() |
| 118 | + r_file = open(pkg + "/R.py", "w") |
| 119 | + r_file.write("R = {\n") |
| 120 | + |
| 121 | + with open(fname, "rb") as f: |
| 122 | + r_file.write("%r: %r,\n" % (res_name, f.read())) |
| 123 | + |
| 124 | + if r_file: |
| 125 | + r_file.write("}\n") |
| 126 | + r_file.close() |
| 127 | + |
| 128 | + |
| 129 | +class sdist(_sdist): |
| 130 | + |
| 131 | + def run(self): |
| 132 | + self.filelist = FileList() |
| 133 | + self.get_file_list() |
| 134 | + make_resource_module(self.filelist.files) |
| 135 | + |
| 136 | + r = super().run() |
| 137 | + |
| 138 | + assert len(self.archive_files) == 1 |
| 139 | + print("filtering files and recompressing with 4K dictionary") |
| 140 | + filter_tar(self.archive_files[0]) |
| 141 | + outbuf.seek(0) |
| 142 | + gzip_4k(outbuf, self.archive_files[0]) |
| 143 | + |
| 144 | + return r |
| 145 | + |
| 146 | + |
| 147 | +# For testing only |
| 148 | +if __name__ == "__main__": |
| 149 | + filter_tar(sys.argv[1]) |
| 150 | + outbuf.seek(0) |
| 151 | + gzip_4k(outbuf, sys.argv[1]) |
0 commit comments