summaryrefslogtreecommitdiff
path: root/run_latex.py
blob: 64220c0e2b67cd93cd5fa0175fc1bcec99665938 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python

"""Build a LaTeX project.

Two invocations are supported:

1) run_latex.py [texlive] [latexrun] [jobname] [mainfile].tex [outfile].pdf [sources...]
2) run_latex.py [texlive] [latexrun] [jobname] [mainfile].tex -- [sources...] -- [command...]

The first will build [outfile].pdf from [mainfile].tex and the [sources...].
This is used to build the PDF file for the [name]_getpdf rules.

The second will build the paper, place [jobname].bbl in the current directory,
then call [command...]. This is used to build the bbl file for the
[name]_getarxivable rules.
"""

import glob
import os
import shutil
import subprocess
import sys

# Copy any existing build cache here.
cache_dir = "/tmp/bazel-latex.cache"
if os.path.exists(cache_dir) and os.path.isdir(cache_dir):
    shutil.copytree(cache_dir, "latex.out")

texlive, latexrun, job_name, main_file, output_file = sys.argv[1:6]
sources = sys.argv[6:]
if output_file == "--":
    run_after = sources[sources.index("--"):][1:]
    sources = sources[:sources.index("--")]

env = dict(os.environ)
# Generated files (eg. outputs of pdfcrop) are placed under bazel-out/*/bin.
# This references the bin directory so pdflatex can find them. There is
# probably a better way of doing this.
bin_dirs = set()
for source in sources:
    if source.startswith("bazel-out"):
        bin_dirs.add("%s/%s" % (os.getcwd(), "/".join(source.split("/")[:3])))
env["TEXINPUTS"] = ".:%s:" % ":".join(sorted(bin_dirs))

env["PATH"] = "%s:%s" % (os.path.abspath("%s/bin/x86_64" % texlive), env["PATH"])
env["PATH"] = "%s:%s" % (os.path.abspath(texlive), env["PATH"])
env["TEXMFHOME"] = os.path.abspath(texlive)
env["TEXMFVAR"] = os.path.abspath(texlive)
env["SOURCE_DATE_EPOCH"] = "0"

return_code = subprocess.call(
    args=[
        "python3",
        latexrun,
        "--latex-args=-jobname=" + job_name,
        "--latex-cmd=pdflatex",
        "--bibtex-cmd=bibtex",
        "-Wall",
        main_file,
    ],
    env=env,
)

# Copy back to the build cache.
if os.path.exists(cache_dir):
    shutil.rmtree(cache_dir)
shutil.copytree("latex.out", cache_dir)

if return_code != 0:
    sys.exit(return_code)

if output_file != "--":
    os.rename(job_name + ".pdf", output_file)
else:
    # Run the run_after script.
    os.rename("latex.out/" + job_name + ".bbl", job_name + ".bbl")
    return_code = subprocess.call(
        args=run_after,
        env=env,
    )
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback