-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.py
More file actions
executable file
·84 lines (72 loc) · 2.68 KB
/
build.py
File metadata and controls
executable file
·84 lines (72 loc) · 2.68 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python3
from sys import argv
from subprocess import Popen
from os.path import join, isdir
from os import rename, mkdir
from shutil import copytree, rmtree
PAGE_DIR = "page/"
PUBLISH_DIR = "publish/"
PDF_DIR = join(PUBLISH_DIR, "notater/")
def call_latexes(cmd):
pdfdir = lambda x: join(PDF_DIR,x)
cmd("TFE4101_Krets/", "Krets", pdfdir("krets.pdf"))
cmd("TMA4115_Matte3/", "Matte3", pdfdir("matte3.pdf"))
## === ADD NEW DOCUMENTS HERE ===
def make_publish_dir():
call(["mkdir", "-p", PUBLISH_DIR])
def make_pdf_dir():
call(["mkdir", "-p", PDF_DIR])
def copy_page():
copytree(PAGE_DIR, PUBLISH_DIR, dirs_exist_ok=True)
def call(command, **kwargs):
p = Popen(command, **kwargs)
p.wait()
if p.returncode != 0:
error(f"command {' '.join(command)} failed with return code {p.returncode}")
def compile(folder, name, target):
call(["latexmk", "-pdf", name+".tex"], cwd=folder)
rename(join(folder,name+".pdf"), target)
print(f" ==== Done! Moved {name}.pdf to {target} ==== ")
def clean_latex(folder, name, target):
base = join(folder,name)
extensions = ["aux", "fdb_latexmk", "fls", "log", "toc", "out"]
call(["rm", "-f"] + [f"{base}.{ex}" for ex in extensions])
def error(msg, **kwargs):
print(f"error: {msg}", **kwargs)
exit(-1)
def print_help_text():
print("Build script for compiling pdfs to a static site")
print("Usage: ./build.py [command]")
print()
print("Commands:")
print(f"all (default) Build all latex and copy {PAGE_DIR} into {PUBLISH_DIR}")
print(f"latex [filter] Build all pdfs (optionally with name containing a filter)")
print(f"ls List all pdfs I can build")
print(f"clean Remove all build steps and output")
exit()
if __name__ == "__main__":
args = argv[1:]
if "--help" in args:
print_help_text()
if args == [] or args == ["all"]:
make_publish_dir()
make_pdf_dir()
copy_page()
call_latexes(compile)
elif args[0] == "latex":
if len(args) > 2:
error("Too many args, see --help for usage")
pattern = (args[1:2]+[""])[0]
make_pdf_dir()
def compile_if_match(folder, name, target):
if pattern in target:
compile(folder, name, target)
call_latexes(compile_if_match)
elif args == ["ls"]:
call_latexes(lambda folder,name,target: print(f"{target:<28} built from {join(folder,name)}.tex"))
elif args == ["clean"]:
call(["rm", "-rf", PDF_DIR])
call(["rm", "-rf", PUBLISH_DIR])
call_latexes(clean_latex)
else:
error(f"Unrecognized command: '{' '.join(args)}'. See --help for usage")