-
Notifications
You must be signed in to change notification settings - Fork 0
/
ly_expander.py
102 lines (72 loc) · 3.3 KB
/
ly_expander.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
#!/usr/bin/env python3
import io, os, sys
# Rewrite relative includes ('./', '../') and copy the files into one big file.
# This file is then compilable even after it has been moved around.
def rewrite_local_lilypond_includes( out_file, fname, recursive = True ):
with io.open(fname, 'r', encoding='utf8') as in_file:
for i, line in enumerate(in_file.readlines()):
if line.startswith(u'\ufeff'): continue
if '\\include' in line:
# Rewrite includes to absolute location of file
incline = line.replace('\\include', '').strip('"\'\n ')
# Only copy if it starts with a "./"
if incline.startswith('./') or incline.startswith('../'):
# Make it an absolute location first
inc_fname = os.path.join(os.path.abspath(os.path.dirname(fname)), incline)
try:
with io.open( inc_fname, 'r', encoding='utf8') as inc_file:
out_file.write("% Including Relative File: " + incline + "\n")
if recursive:
rewrite_local_lilypond_includes( out_file, inc_fname, False )
else:
for subline in inc_file.readlines():
out_file.write( subline )
out_file.write("% End of Relative File: " + incline + "\n")
# We are done here
continue
except IOError:
print("{}:{} Error opening File: '{}'".format(fname, i, incline), file=sys.stderr)
out_file.write(line)
if __name__ == "__main__":
from argparse import ArgumentParser
import os
parser = ArgumentParser()
parser.add_argument("files", default='', nargs='+')
command_group = parser.add_mutually_exclusive_group()
command_group.add_argument("-c", "--command", metavar="COMMAND",
dest="runcommand", default=None,
help="Run COMMAND on the generated file")
command_group.add_argument("-l", "--lilypond",
action="store_true", dest="uselilypond", default=False,
help="Run Lilypond on the generated file")
parser.add_argument("-o", "--options", nargs='?',
action="append", dest="options", default=[],
help="Options to pass to lilypond")
parser.add_argument("-k", "--keep",
action="store_false", dest="delete_tempfile", default=True,
help="Keep Temporary File after running COMMAND on it")
args = parser.parse_args()
if args.uselilypond:
args.runcommand = 'lilypond'
if args.runcommand:
import subprocess
import tempfile
else:
import sys
out_file = sys.stdout
for f in args.files:
if args.runcommand:
out_file = tempfile.NamedTemporaryFile( mode='w+', suffix='.ly', delete=args.delete_tempfile, prefix=os.path.basename(f).rsplit('.', 1)[0], dir=os.path.abspath(os.path.dirname(f)) )
out_file.write('\ufeff')
out_file.write("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n")
out_file.write("% This file was preprocessed by a script '" + os.path.basename(__file__) + "'\n")
out_file.write("% that copies all '\\include \"./\"'d files into one file\n")
out_file.write("% such that it can be moved around and still be compiled\n")
out_file.write("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n")
out_file.write("% Original file: " + f + "\n")
out_file.write("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n\n")
rewrite_local_lilypond_includes( out_file, f )
out_file.flush()
if args.runcommand:
print( args.runcommand + " " + " ".join(args.options) + " " + out_file.name )
subprocess.call( args.runcommand + " " + " ".join(args.options) + " " + out_file.name, shell=True )