"""List an arXiv e-print (gzip, maybe tar) in memory and copy its text files out. Usage: python3 -I read_eprint.py Nothing is extracted as-is and nothing is compiled or run: members are read in memory, only regular files with text extensions are written, under their base name (no paths), as UTF-8 with LF endings. Other members are only listed. """ import gzip import hashlib import io import os import sys import tarfile TEXT_EXT = {".tex", ".bbl", ".bib", ".txt", ".sty", ".cls", ".bst", ".dat", ".csv"} def main(): src, outdir = sys.argv[1], sys.argv[2] os.makedirs(outdir, exist_ok=True) raw = open(src, "rb").read() data = gzip.decompress(raw) listing = [] try: tf = tarfile.open(fileobj=io.BytesIO(data), mode="r:") except tarfile.ReadError: name = "main.tex" with open(os.path.join(outdir, name), "w", encoding="utf-8", newline="\n") as f: f.write(data.decode("utf-8", errors="replace").replace("\r\n", "\n")) print("single file (not tar), written as", name, len(data)) return for m in tf.getmembers(): kind = "file" if m.isfile() else ("dir" if m.isdir() else "other") ext = os.path.splitext(m.name)[1].lower() line = f"{kind}\t{m.size}\t{m.name}" if m.isfile(): b = tf.extractfile(m).read() line += "\t" + hashlib.sha256(b).hexdigest() if ext in TEXT_EXT: base = os.path.basename(m.name).replace("..", "_") outname = base if base else "unnamed.txt" with open(os.path.join(outdir, outname), "w", encoding="utf-8", newline="\n") as f: f.write(b.decode("utf-8", errors="replace").replace("\r\n", "\n")) line += "\twritten:" + outname listing.append(line) with open(os.path.join(outdir, "_listing.tsv"), "w", encoding="utf-8", newline="\n") as f: f.write("kind\tsize\tname\tsha256\tnote\n" + "\n".join(listing) + "\n") print("\n".join(listing)) if __name__ == "__main__": main()