/
build.py
executable file
·136 lines (120 loc) · 3.64 KB
/
build.py
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/python3
"""
Build script for bachelor thesis presentation
Andreas Linz
admin@klingt.net
HTWK - 10INB-T
"""
import sys
import os
from shutil import copyfile
import argparse
from subprocess import check_call, call
debug = False
compiler = "xelatex"
build_dir = ".build"
passes = 1
path_to_texfile = ""
pdf = ""
texfile = ""
root_dir = ""
def main():
# set commandline arguments and parse them
argparser = argparse.ArgumentParser()
argparser.add_argument("file", help="the texfile of your presentation")
argparser.add_argument("--debug", help="enables debug output", action="store_true")
argparser.add_argument("--passes", type=int, default=2, choices=range(1, 5), help="number of build passes")
args = argparser.parse_args()
if args.debug:
global debug
debug = True
print("debug output enabled!")
if debug:
print_debug("arguments: "+str(args))
if args.passes:
global passes
passes = args.passes
print("Passes: {}").format(passes)
# get directory of current script, used for copying output pdf
global root_dir
root_dir = os.path.dirname(os.path.abspath(__file__))
if debug:
print_debug("script root: "+str(root_dir))
# start the build script
try:
with open(args.file): # with automatically closes the file
pass
except IOError as e:
print(e)
else:
process_filearg(args.file)
start()
def start():
if not check_dir(build_dir):
if debug:
print_debug(build_dir)
os.mkdir(build_dir)
sync_build_dir()
build_pdf()
def build_pdf():
wd = [os.getcwd()] # working directory
wd.append(build_dir)
wd.append(path_to_texfile)
wd_str = "/".join(wd)
if debug:
print_debug("working directory: "+str(wd_str))
try:
os.chdir(wd_str)
except OSError as e:
print(e)
else:
logfile_str = root_dir+"/"+"build.log"
print("logfile: {}").format(logfile_str)
try:
with open(logfile_str, "w") as logfile:
for i in range(passes):
print("build pass {} ...").format(i)
if i < passes-1:
check_call(build_call(compiler, "-interaction=nonstopmode", "-no-pdf",texfile), stdout=logfile)
else:
check_call(build_call(compiler, "-interaction=nonstopmode", texfile), stdout=logfile)
copyfile(wd_str+"/"+pdf, root_dir+"/"+pdf)
except IOError as e:
print(e)
def sync_build_dir():
"""
synchronizes the build directory with the content directory
"""
source_dir = path_to_texfile
dest_dir = build_dir
return call(build_call("rsync", ["--recursive", "--verbose"], source_dir, dest_dir))
def build_call(*args): # *.foo ellipsis
"""
returns the arguments as flattened list
"""
call = []
for arg in args:
if type(arg) is list:
for element in arg:
call.append(element)
else:
call.append(arg)
if debug:
print_debug("flattened call: "+str(call))
return call
def check_dir(dir):
return os.path.exists(dir)
def process_filearg(arg):
pieces = arg.split("/")
if debug:
print_debug("splitted file argument: "+str(pieces))
global texfile
texfile = pieces.pop()
global path_to_texfile
path_to_texfile = "/".join(pieces)
global pdf
pdf = texfile.replace(".tex", ".pdf")
def print_debug(msg):
print("DEBUG {}").format(msg)
if __name__ == "__main__":
main()