Skip to content

Instantly share code, notes, and snippets.

@jkbjh
Created November 10, 2022 16:08
Show Gist options
  • Save jkbjh/34cdfb2d4bdb25f1bab63b1f154faea7 to your computer and use it in GitHub Desktop.
Save jkbjh/34cdfb2d4bdb25f1bab63b1f154faea7 to your computer and use it in GitHub Desktop.
helper util to package arxiv submissions
#!/usr/bin/env python
import argparse
import subprocess
import shlex
import shutil
import sys
def check_programs(*progs):
all_success = True
for program in progs:
if not shutil.which(program):
print(f"FATAL: could not find the command '{program}'")
all_success = False
return all_success
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("rootfile", type=str)
parser.add_argument("outfile", type=str)
parser.add_argument("--strip", nargs="*", default=[])
parser.add_argument("--add", nargs='*', help="Additional files to be added to the arxiv", default=[])
args = parser.parse_args()
BUILDCOMMAND = "latexmk --pdf -g {TEXFILE}"
DESTFILE = "arxiv.tar.gz"
TMPDEST = "arxiv.tar"
args.rootfile = shlex.quote(args.rootfile)
args.outfile = shlex.quote(args.outfile)
args.add = [shlex.quote(filename) for filename in args.add]
args.strip = [shlex.quote(filename) for filename in args.strip]
assert args.rootfile != args.outfile
print(f"args.rootfile: {args.rootfile}")
print(f"args.outfile: {args.outfile}")
print(f"args.add: {args.add}")
print(f"args.strip: {args.strip}")
if not check_programs("latexpand", "sed", "sponge", "arxiv-collector", "gunzip", "tar", "gzip"):
sys.exit(1)
subprocess.check_call(f"latexpand --in-encoding 'encoding(UTF-8)' --makeatletter {args.rootfile} > {args.outfile}", shell=True)
subprocess.check_call(BUILDCOMMAND.format(TEXFILE=args.outfile), shell=True)
subprocess.check_call(r"sed '1N;N;/^\n\n$/ { N;s/^\n\n//;N;D; };P;D' " + args.outfile + "| sponge " + args.outfile, shell=True)
subprocess.check_call(f"arxiv-collector --dest {DESTFILE} {args.outfile}", shell=True)
strip_string = ""
if args.strip:
strip_string = f"--delete {' '.join(args.strip)}"
if args.add or args.strip:
subprocess.check_call(f"gunzip -c {DESTFILE} > {TMPDEST} && "
f"tar -rf {TMPDEST} {' '.join(args.add)} && "
f"tar -f {TMPDEST} {strip_string} && "
f"gzip -c {TMPDEST} > {DESTFILE}",
shell=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment