-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbuild_with_fetched_fonts.py
More file actions
70 lines (57 loc) · 2.27 KB
/
Copy pathbuild_with_fetched_fonts.py
File metadata and controls
70 lines (57 loc) · 2.27 KB
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
#!/usr/bin/env python3
import subprocess
import sys
import os
# Repository root and script paths
REPO_ROOT = os.path.dirname(os.path.abspath(__file__))
FETCH_FONTS_SCRIPT = os.path.join(REPO_ROOT, "fetch-fonts.py")
RESTORE_FONTS_SCRIPT = os.path.join(REPO_ROOT, "restore-fonts.py")
def run(cmd, description=None, exit_on_fail=True):
# Run a command and print status. Exit if requested on failure
if description:
print(f"{description}")
try:
subprocess.run(cmd, check=True)
print(f"✅ Done: {' '.join(cmd)}")
return True
except subprocess.CalledProcessError as e:
print(f"❌ Command failed: {' '.join(cmd)}")
if exit_on_fail:
sys.exit(e.returncode)
return False
def main():
# Require the root TeX file as input (e.g., main.tex or subfile.tex)
if len(sys.argv) < 2:
print("Usage: python3 build_with_fetched_fonts.py <main-tex-file>")
sys.exit(1)
main_tex = sys.argv[1]
try:
# Step 1: Try to fetch fonts from Overleaf repository
# If fetching fails (no permissions, offline, etc.), use fallback fonts
run(["python3", FETCH_FONTS_SCRIPT], description="Setting official or fallback fonts...", exit_on_fail=False)
# Step 2: Compile LaTeX document with latexmk
latex_cmd = [
"latexmk",
"-cd",
"-r",
"../latexmkrc",
"-outdir=./out",
"-pdf",
"-shell-escape",
"-halt-on-error", # Stops on the first error
"-interaction=nonstopmode", # Avoids interactive prompts
main_tex
]
print(f"Compiling LaTeX: {' '.join(latex_cmd)}")
try:
subprocess.run(latex_cmd, check=True)
print("✅ LaTeX compilation finished successfully.")
except subprocess.CalledProcessError as e:
print(f"❌ LaTeX compilation failed with exit code {e.returncode}.")
sys.exit(e.returncode)
finally:
# Step 3: Always restore the original font configuration,
# regardless of whether compilation succeeded or failed
run(["python3", RESTORE_FONTS_SCRIPT], description="Restoring official fonts if needed...", exit_on_fail=False)
if __name__ == "__main__":
main()