-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
executable file
·85 lines (62 loc) · 2.16 KB
/
run.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
#!/usr/bin/env python3
import os
import shutil
import subprocess
import sys
from scripts.Config import Config
from scripts.SongFile import SongFile
BUILD_DIR = "build"
def prepare_build_directory():
try:
shutil.rmtree(BUILD_DIR)
os.mkdir(BUILD_DIR)
except FileExistsError:
pass
try:
os.mkdir(os.path.join(BUILD_DIR, "source"))
except FileExistsError:
pass
def transform(path, destdir, config):
filename = os.path.basename(path)
destfile = os.path.join(destdir, filename)
with open(path, "r") as fin:
with open(destfile, "w") as fout:
line = fin.readline()
fout.write(line)
if config.capo:
fout.write("\\capo{" + str(config.capo) + "}\n")
if config.transpose:
fout.write("\\transpose{" + str(config.transpose) + "}\n")
fout.write(fin.read())
def fix_init_tex(config):
with open("akordy/init.tex", "r") as fin:
with open(os.path.join(BUILD_DIR, "init.tex"), "w") as fout:
fout.write(fin.readline())
fout.write("\\usepackage{ifpdf}\n")
fout.write(fin.read())
if config.page_numbers:
fout.write("\\pagestyle{plain}\n")
def copy_scripts():
shutil.copy('akordy/maketoc.py', BUILD_DIR)
shutil.copy('akordy/merge.py', BUILD_DIR)
def main():
if len(sys.argv) < 2:
cfg_file = "config.yml"
else:
cfg_file = sys.argv[1]
prepare_build_directory()
c = Config(cfg_file)
songs = [ SongFile(os.path.join("zpevnik/nowtex", x)) for x in os.listdir("zpevnik/nowtex")]
for s in songs:
conf = c.get_config(s)
if conf is None:
continue
transform(s.path, os.path.join(BUILD_DIR, "source"), conf)
fix_init_tex(c)
copy_scripts()
subprocess.run(["python3", "maketoc.py", "source", "titleidx.sbx", "authidx.sbx"], cwd=BUILD_DIR)
subprocess.run(["python3", "merge.py", "source", "zpevnik.tex"], cwd=BUILD_DIR)
subprocess.run(["pdflatex", "zpevnik.tex"], cwd=BUILD_DIR)
subprocess.run(["pdflatex", "zpevnik.tex"], cwd=BUILD_DIR)
if __name__ == "__main__":
main()