2015-08-21 01:05:14 +02:00
|
|
|
#!/usr/bin/python3.4
|
|
|
|
|
|
|
|
## Generate pdf form lilypond file
|
|
|
|
## by using standard command
|
|
|
|
##
|
|
|
|
## Most of programming was done by Sven Axelsson, http://svenax.net/
|
|
|
|
|
|
|
|
import codecs, os
|
|
|
|
from argparse import ArgumentParser
|
|
|
|
|
|
|
|
class MakeDrum:
|
|
|
|
LILYPOND = 'lilypond'
|
2015-11-30 22:37:29 +01:00
|
|
|
VERSION = '0.9.5'
|
2015-10-16 23:25:22 +02:00
|
|
|
MASTER_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
RUN_DIR = os.path.abspath(os.curdir)
|
2016-02-21 09:10:00 +01:00
|
|
|
TMP_DIR = './tmp'
|
|
|
|
TMP_PREFIX = 'tmp_'
|
2015-08-21 01:05:14 +02:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
# Gather options and create the template file
|
|
|
|
usage = __file__
|
|
|
|
parser = ArgumentParser(usage)
|
|
|
|
|
|
|
|
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')
|
|
|
|
|
2015-10-16 23:25:22 +02:00
|
|
|
parser.add_argument('-x', '--drumfile',
|
2016-02-21 09:10:00 +01:00
|
|
|
dest='lilydrum', default=os.path.join(self.MASTER_DIR,'lilydrum.ly'),
|
2015-10-16 23:25:22 +02:00
|
|
|
help='Use the specified file for drums')
|
|
|
|
parser.add_argument('-c', '--pipefile',
|
|
|
|
dest='lilypipe', default='bagpipe.ly',
|
|
|
|
help='Use the specified file for pipes')
|
2015-12-18 13:03:14 +01:00
|
|
|
parser.add_argument('-i', '--include',
|
|
|
|
dest='includes', nargs='*', default=[],action='append',
|
|
|
|
help='Include the specified file for compiling')
|
2015-10-16 23:25:22 +02:00
|
|
|
|
2015-08-21 01:05:14 +02:00
|
|
|
parser.add_argument('-p', '--paper-size',
|
|
|
|
dest='papersize', default='a4',
|
|
|
|
help='Paper size. Default: A4')
|
|
|
|
parser.add_argument('-o', '--orientation',
|
|
|
|
dest='orientation', default='landscape',
|
|
|
|
help='Paper orientation. Default: landscape')
|
|
|
|
parser.add_argument('-s', '--staff-size',
|
2015-10-16 23:25:22 +02:00
|
|
|
dest='staffsize', default='20',
|
2015-08-21 01:05:14 +02:00
|
|
|
help='Staff size. Default: 16pt.')
|
|
|
|
parser.add_argument('-w', '--view-spacing',
|
|
|
|
action='store_true', dest='view_spacing', default=False,
|
|
|
|
help='Turn on "Paper.annotatespacing".')
|
|
|
|
|
2015-12-11 11:25:33 +01:00
|
|
|
parser.add_argument('-g','--generated',
|
|
|
|
dest='gen_out', default=self.TMP_DIR,
|
|
|
|
help='Put generated lilyfiles in $gen_out')
|
|
|
|
parser.add_argument('--no-compile', default=True,
|
|
|
|
action='store_false', dest='compile',
|
|
|
|
help='Do not compile generated Lilypond files')
|
2015-08-21 01:05:14 +02:00
|
|
|
parser.add_argument('--no-log',
|
|
|
|
action='store_false', dest='log', default=True,
|
|
|
|
help='Do not generate log files.')
|
|
|
|
parser.add_argument('--no-cleanup',
|
|
|
|
action='store_false', dest='clean', default=True,
|
|
|
|
help='Leave all temporary files in place')
|
2015-10-16 23:25:22 +02:00
|
|
|
parser.add_argument('-d', '--out_dir',
|
|
|
|
dest='out_dir', default='pdf',
|
|
|
|
help='Output dir, for lilypond. If it doesn\'t exist, try to create it')
|
2015-08-21 01:05:14 +02:00
|
|
|
parser.add_argument('music_file',
|
2015-12-11 11:25:33 +01:00
|
|
|
default='', nargs='*',
|
2015-08-21 01:05:14 +02:00
|
|
|
help='file to process')
|
|
|
|
parser.add_argument('-@', '--list_file',
|
|
|
|
dest='list_file', default='',
|
|
|
|
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)
|
2015-12-18 13:03:14 +01:00
|
|
|
|
2015-12-11 11:25:33 +01:00
|
|
|
# Check for files
|
|
|
|
if not self.args.music_file:
|
|
|
|
parser.print_usage()
|
|
|
|
return
|
|
|
|
|
2015-12-18 13:03:14 +01:00
|
|
|
# Check for files to include
|
|
|
|
self.args.includes = [el for elements in self.args.includes for el in elements]
|
2015-08-21 01:05:14 +02:00
|
|
|
|
|
|
|
# Clean up of files
|
|
|
|
self.remove_tmp_dir = self.args.clean
|
|
|
|
|
|
|
|
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
|
2015-10-16 23:25:22 +02:00
|
|
|
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
|
2015-08-21 01:05:14 +02:00
|
|
|
|
|
|
|
for file_path in self.args.music_file:
|
2015-12-11 11:25:33 +01:00
|
|
|
self.processit(self.TMP_DIR, os.path.join(self.RUN_DIR, file_path), self.args.gen_out, self.args.compile)
|
2015-08-21 01:05:14 +02:00
|
|
|
|
|
|
|
#if not
|
|
|
|
#os.rmdir(self.TMP_DIR)
|
|
|
|
|
2015-10-16 23:25:22 +02:00
|
|
|
|
2015-12-11 11:25:33 +01:00
|
|
|
def processit(self, tmp_dir, file, gen_out, compile):
|
2015-12-18 13:03:14 +01:00
|
|
|
tmp_file = self.maketemplate(tmp_dir, file)
|
2015-08-21 01:05:14 +02:00
|
|
|
|
2015-12-11 11:25:33 +01:00
|
|
|
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, gen_out);
|
|
|
|
# if not dir $gen_out, make it
|
|
|
|
if not os.path.exists(gen_dir):
|
|
|
|
try: os.makedirs(gen_dir)
|
|
|
|
except:
|
|
|
|
print('[Error]')
|
|
|
|
print(' ! Seems like the {} directory cannot be created'.format(gen_dir))
|
|
|
|
return
|
|
|
|
# mv file to dir, remove self.TMP_PREFIX
|
|
|
|
os.rename(tmp_file, os.path.join(gen_dir, new_tmp_file))
|
|
|
|
tmp_file = new_tmp_file
|
|
|
|
print('[OK]')
|
|
|
|
|
|
|
|
if compile:
|
2015-10-16 23:25:22 +02:00
|
|
|
if self.args.log:
|
2015-12-11 11:25:33 +01:00
|
|
|
logfile = os.path.join(self.TMP_DIR, os.path.relpath(file).replace(".ly", '').replace('/', '-')+'.log')
|
|
|
|
log = ' > '+logfile+' 2>&1'
|
|
|
|
else:
|
|
|
|
log = ''
|
|
|
|
|
|
|
|
print ('Compiling ', file, end=' ', flush=True)
|
|
|
|
if not self.args.log:
|
|
|
|
print()
|
|
|
|
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
|
|
|
|
|
|
|
|
if os.system(lilycmd) != 0:
|
|
|
|
self.remove_tmp_dir = False
|
|
|
|
print ('[Error]')
|
2015-10-16 23:25:22 +02:00
|
|
|
if self.args.log:
|
2015-12-11 11:25:33 +01:00
|
|
|
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)
|
2015-08-21 01:05:14 +02:00
|
|
|
|
2015-12-18 13:03:14 +01:00
|
|
|
def maketemplate(self, tmp_dir, file):
|
2015-10-16 23:25:22 +02:00
|
|
|
lily_includes = ''
|
|
|
|
include_drum_file = False
|
|
|
|
include_pipe_file = False
|
|
|
|
# find out whether drum, pipes, or full score
|
2015-11-09 05:59:05 +01:00
|
|
|
for ext in ['full', 'side', 'tenor', 'bass', 'drum', 'snare']:
|
2015-10-16 23:25:22 +02:00
|
|
|
if ext in file:
|
|
|
|
include_drum_file = True
|
2015-12-18 13:03:14 +01:00
|
|
|
break
|
|
|
|
|
2015-11-09 05:59:05 +01:00
|
|
|
for ext in ['full', 'pipes']:
|
2015-10-16 23:25:22 +02:00
|
|
|
if ext in file:
|
|
|
|
include_pipe_file = True
|
2015-12-18 13:03:14 +01:00
|
|
|
break
|
|
|
|
|
2015-10-16 23:25:22 +02:00
|
|
|
if include_drum_file:
|
2015-12-18 13:03:14 +01:00
|
|
|
self.args.includes.insert(0,self.args.lilydrum)
|
|
|
|
|
2015-10-16 23:25:22 +02:00
|
|
|
if include_pipe_file:
|
2015-12-18 13:03:14 +01:00
|
|
|
self.args.includes.insert(0, self.args.lilypipe)
|
|
|
|
|
|
|
|
for f in self.args.includes:
|
|
|
|
lily_includes = lily_includes + "\n\\include \"{}\"".format(f)
|
2015-10-16 23:25:22 +02:00
|
|
|
|
2015-08-21 01:05:14 +02:00
|
|
|
# set up a tmp file with template and file combined
|
2016-02-21 09:10:00 +01:00
|
|
|
tmp_file = os.path.join(tmp_dir, self.TMP_PREFIX + os.path.relpath(file).replace('../music-','',1).replace('/', '-'))
|
2015-08-21 01:05:14 +02:00
|
|
|
|
2016-02-21 09:10:00 +01:00
|
|
|
print(file + ' is to be templated')
|
|
|
|
print(tmp_file + ' is templated file')
|
2015-08-21 01:05:14 +02:00
|
|
|
out_file = codecs.open(tmp_file, 'w+', 'utf8')
|
|
|
|
out_file.write(u'\ufeff')
|
|
|
|
|
|
|
|
out_file.write(u"""
|
2015-12-11 11:25:33 +01:00
|
|
|
% Generated from """+file+""" by """+__file__+""" version """+self.VERSION+"""
|
2015-08-21 01:05:14 +02:00
|
|
|
|
|
|
|
\\version "2.18.0"
|
|
|
|
|
|
|
|
#(ly:set-option 'point-and-click #f)
|
2015-10-16 23:25:22 +02:00
|
|
|
"""+ lily_includes +"""
|
2015-11-05 01:39:52 +01:00
|
|
|
|
2015-08-21 01:05:14 +02:00
|
|
|
|
2015-11-05 03:28:25 +01:00
|
|
|
#(set-global-staff-size """+self.args.staffsize+""")
|
|
|
|
#(set-default-paper-size \""""+self.args.papersize+"""\" '"""+self.args.orientation+""")
|
|
|
|
|
2015-12-18 13:03:14 +01:00
|
|
|
%\layout {
|
|
|
|
% \context {
|
|
|
|
% \Score {
|
|
|
|
% \override NonMusicalPaperColumn #'line-break-permission = ##f
|
|
|
|
% }
|
|
|
|
% }
|
|
|
|
%}
|
2015-08-21 01:05:14 +02:00
|
|
|
% The tune to generate.
|
|
|
|
""")
|
|
|
|
|
|
|
|
# Read lily file into tmp file
|
|
|
|
music = codecs.open(file, 'r', 'utf8').read()
|
|
|
|
if music.startswith(u'\ufeff'): music = music[1:]
|
|
|
|
music = music.split(u'\n')
|
|
|
|
printit = 1
|
|
|
|
for line in music:
|
2015-11-05 01:39:52 +01:00
|
|
|
if line.startswith(u'\\include "lilydrum.ly"'): continue
|
2015-11-06 15:13:11 +01:00
|
|
|
if line.startswith(u'\\include'):
|
|
|
|
# Rewrite includes to absolute location of file
|
|
|
|
incline = line.replace('\\include', '').strip('"\' ')
|
|
|
|
if not incline.startswith('\\'): #already absolute
|
2015-12-11 11:25:33 +01:00
|
|
|
incline = os.path.join(os.path.abspath(os.path.dirname(file)), incline)
|
2015-11-06 15:13:11 +01:00
|
|
|
line = "\\include \""+incline+"\""
|
2015-08-21 01:05:14 +02:00
|
|
|
if printit:
|
|
|
|
out_file.write(line.replace('\r', '')+'\n')
|
|
|
|
out_file.close()
|
|
|
|
|
|
|
|
# Return tmp_file_path
|
|
|
|
return tmp_file
|
2015-10-16 23:25:22 +02:00
|
|
|
MakeDrum();
|