1
0
Fork 0
mirror of https://github.com/kastdeur/pipeband-music.git synced 2025-05-16 21:09:18 +02:00

End of SXC

This commit is contained in:
Eric Teunis de Boone 2016-02-19 14:39:53 +01:00
parent 792d8f563e
commit 96724469f8
94 changed files with 2794 additions and 333 deletions

View file

@ -11,20 +11,23 @@ from argparse import ArgumentParser
class MakeDrum:
LILYPOND = 'lilypond'
VERSION = '0.9.5'
TMP_DIR = os.path.join(os.path.abspath(os.curdir),'tmp')
TMP_DIR = './tmp'
TMP_PREFIX = 'tmp_'
MASTER_DIR = os.path.dirname(os.path.abspath(__file__))
RUN_DIR = os.path.abspath(os.curdir)
def __init__(self):
parser = ArgumentParser(__file__)
# Gather options and create the template file
usage = __file__
parser = ArgumentParser(usage)
parser.add_argument('--version', action='version', version=self.VERSION)
parser.add_argument('--version',
action='store_true', dest='show_version', default=False,
help='show makeDrum version and exit')
parser.add_argument('--lilyversion',
action='store_true', dest='show_lilyversion', default=False,
help='show Lilypond version and exit')
# options for inclusion of files
parser.add_argument('-x', '--drumfile',
dest='lilydrum', default='lilydrum.ly',
help='Use the specified file for drums')
@ -35,7 +38,6 @@ class MakeDrum:
dest='includes', nargs='*', default=[],action='append',
help='Include the specified file for compiling')
# options for lilypond
parser.add_argument('-p', '--paper-size',
dest='papersize', default='a4',
help='Paper size. Default: A4')
@ -44,20 +46,16 @@ class MakeDrum:
help='Paper orientation. Default: landscape')
parser.add_argument('-s', '--staff-size',
dest='staffsize', default='20',
help='Staff size. Default: 20pt.')
help='Staff size. Default: 16pt.')
parser.add_argument('-w', '--view-spacing',
action='store_const', dest='view_spacing', default='##f', const='##t',
action='store_true', dest='view_spacing', default=False,
help='Turn on "Paper.annotatespacing".')
parser.add_argument('-l', '--line-break',
action='store_const', dest='line_break', default='##t', const='##f',
help='Turn off explicit linebreaks".')
# options for generating and compiling
parser.add_argument('-g','--generated',
dest='gen_out', default=self.TMP_DIR,
help='Put generated lilyfiles in $gen_out')
parser.add_argument('--no-compile',
action='store_false', dest='compile', default=True,
parser.add_argument('--no-compile', default=True,
action='store_false', dest='compile',
help='Do not compile generated Lilypond files')
parser.add_argument('--no-log',
action='store_false', dest='log', default=True,
@ -67,64 +65,72 @@ class MakeDrum:
help='Leave all temporary files in place')
parser.add_argument('-d', '--out_dir',
dest='out_dir', default='pdf',
help='Output dir for the lilypond process. If it doesn\'t exist, try to create it')
# the file(s) to process
help='Output dir, for lilypond. If it doesn\'t exist, try to create it')
parser.add_argument('music_file',
default='', nargs='*',
help='file to process')
parser.add_argument('-@', '--list_file',
dest='list_file', default='',
help='file containing the list of files to process')
help='list of files to process')
self.args = parser.parse_args()
if self.args.show_version:
print(__name__, ' ', self.VERSION)
return
if self.args.show_lilyversion:
print(os.system(self.LILYPOND+' --version'))
return
if self.args.view_spacing:
self.args.view_spacing = "##t"
else:
self.args.view_spacing = "##f"
# Input files
if self.args.list_file != '':
self.args.music_file.append(open(self.args.list_file, 'r').readlines())
close(self.args.list_file)
# Check if there are any files
# Check for files
if not self.args.music_file:
parser.print_usage()
return
# Check for inclusion options
# Check for files to include
self.args.includes = [el for elements in self.args.includes for el in elements]
# Whether to clean up tmp_dir if possible
# Clean up of files
self.remove_tmp_dir = self.args.clean
# are TMP_DIR, out_dir dirs?
if not os.path.exists(self.TMP_DIR):
try: os.makedirs(self.TMP_DIR)
if not os.path.exists(os.path.join(os.path.curdir, self.TMP_DIR)):
try: os.makedirs(os.path.join(os.path.curdir, self.TMP_DIR))
except:
print('Seems like no temporary directory can be created')
return
if not os.path.exists(self.args.out_dir):
try: os.makedirs(self.args.out_dir)
if not os.path.exists(os.path.join(os.path.curdir, self.args.out_dir)):
try: os.makedirs(os.path.join(os.path.curdir, self.args.out_dir))
except:
print('Seems like no output directory can be created')
return
# do the work!
os.chdir(self.MASTER_DIR)
for file_path in self.args.music_file:
self.process_it(file_path)
self.processit(self.TMP_DIR, os.path.join(self.RUN_DIR, file_path), self.args.gen_out, self.args.compile)
#if dir is empty:
#if not
#os.rmdir(self.TMP_DIR)
def process_it(self, file):
tmp_file = self.maketemplate(file)
os.chdir(self.RUN_DIR)
if self.args.gen_out is not None and self.args.gen_out != self.TMP_DIR:
def processit(self, tmp_dir, file, gen_out, compile):
tmp_file = self.maketemplate(tmp_dir, file)
if gen_out is not None and gen_out != tmp_dir:
new_tmp_file = os.path.basename(tmp_file).replace(self.TMP_PREFIX, '');
print ('Moving ', tmp_file, ' to ', new_tmp_file, end=' ', flush=True)
gen_dir = os.path.join(self.RUN_DIR, self.args.gen_out);
gen_dir = os.path.join(self.RUN_DIR, gen_out);
# if not dir $gen_out, make it
if not os.path.exists(gen_dir):
try: os.makedirs(gen_dir)
@ -137,7 +143,7 @@ class MakeDrum:
tmp_file = new_tmp_file
print('[OK]')
if self.args.compile:
if compile:
if self.args.log:
logfile = os.path.join(self.TMP_DIR, os.path.relpath(file).replace(".ly", '').replace('/', '-')+'.log')
log = ' > '+logfile+' 2>&1'
@ -147,7 +153,7 @@ class MakeDrum:
print ('Compiling ', file, end=' ', flush=True)
if not self.args.log:
print()
lilyout = os.path.join(self.args.out_dir, os.path.basename(tmp_file).replace(self.TMP_PREFIX, '').replace(".ly", ''))
lilyout = os.path.join(self.RUN_DIR, self.args.out_dir, os.path.basename(tmp_file).replace(self.TMP_PREFIX, '').replace(".ly", ''))
print (lilyout)
lilycmd = self.LILYPOND+' --pdf --output='+lilyout+' '+tmp_file+log
@ -158,14 +164,14 @@ class MakeDrum:
print (' ! Did not compile, please see the log at ', logfile)
else :
print ('[OK]')
if self.args.clean:
#remove files
if self.args.log:
os.remove(logfile)
os.remove(tmp_file)
def maketemplate(self, file):
def maketemplate(self, tmp_dir, file):
lily_includes = ''
include_drum_file = False
include_pipe_file = False
@ -189,46 +195,33 @@ class MakeDrum:
for f in self.args.includes:
lily_includes = lily_includes + "\n\\include \"{}\"".format(f)
# Set up a tmp file with template and file combined
tmp_file = self.TMP_PREFIX + os.path.relpath(file).replace('/', '-').replace('..', '').replace('//','').lstrip('-')
tmp_file = os.path.join(self.TMP_DIR, tmp_file)
# set up a tmp file with template and file combined
tmp_file = os.path.join(tmp_dir, self.TMP_PREFIX + os.path.relpath(file).replace('/', '-'))
out_file = codecs.open(tmp_file, 'w+', 'utf8')
out_file.write(u'\ufeff')
# Write the file
out_file.write(
u"""% Generated from {filename} by {script} Version {version}
out_file.write(u"""
% Generated from """+file+""" by """+__file__+""" version """+self.VERSION+"""
\\version "2.18.0"
#(ly:set-option 'point-and-click #f)
{lily_includes}
"""+ lily_includes +"""
#(set-global-staff-size {staffsize})
#(set-default-paper-size \"{papersize}\" '{orientation})
\\paper {{
annotatespacing = {view_spacing}
}}
\layout {{
\context {{
\Score {{
\override NonMusicalPaperColumn #'line-break-permission = {line_break}
}}
}}
}}
#(set-global-staff-size """+self.args.staffsize+""")
#(set-default-paper-size \""""+self.args.papersize+"""\" '"""+self.args.orientation+""")
%\layout {
% \context {
% \Score {
% \override NonMusicalPaperColumn #'line-break-permission = ##f
% }
% }
%}
% The tune to generate.
""".format(
filename=file,
script=__file__,
version=self.VERSION,
lily_includes=lily_includes,
staffsize=self.args.staffsize,
papersize=self.args.papersize,
orientation=self.args.orientation,
view_spacing=self.args.view_spacing,
line_break=self.args.line_break
)
)
""")
# Read lily file into tmp file
music = codecs.open(file, 'r', 'utf8').read()
@ -236,10 +229,8 @@ u"""% Generated from {filename} by {script} Version {version}
music = music.split(u'\n')
printit = 1
for line in music:
if line.startswith(u'\\include "lilydrum.ly"'): continue
if line.startswith(u'\\include'):
if line.startswith(u'\\include "lilydrum.ly"'): continue
if line.startswith(u'\\include "bagpipe.ly"'): continue
# Rewrite includes to absolute location of file
incline = line.replace('\\include', '').strip('"\' ')
if not incline.startswith('\\'): #already absolute
@ -251,6 +242,4 @@ u"""% Generated from {filename} by {script} Version {version}
# Return tmp_file_path
return tmp_file
if __name__ == "__main__":
MakeDrum();
MakeDrum();