3 # ly2dvi.py -- Run LilyPond, add titles to bare score, generate printable
5 # Invokes: lilypond, latex (or pdflatex), dvips, ps2pdf, gs
7 # source file of the GNU LilyPond music typesetter
9 # (c) 1998--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
10 # Jan Nieuwenhuizen <janneke@gnu.org>
12 # This is the third incarnation of ly2dvi.
14 # Earlier incarnations of ly2dvi were written by
15 # Jeffrey B. Reed<daboys@austin.rr.com> (Python version)
16 # Jan Arne Fagertun <Jan.A.Fagertun@@energy.sintef.no> (Bourne shell script)
19 # Note: gettext work best if we use ' for docstrings and "
20 # for gettextable strings.
21 # --> DO NOT USE ''' for docstrings.
27 * figure out which set of command line options should make ly2dvi:
30 na: create latex only?
31 na: create tex and latex
32 default: create dvi only
33 na: create tex, latex and dvi
39 for foo.ly, rename ly2dvi.dir to out-ly2dvi, foo.ly2dvi, foo.dir ?
41 * move versatile taglines,
44 beginfooter=\mutopiaPD
45 endfooter=\tagline -> 'lily was here <version>'
48 lilytagline (->lily was here), usertagline, copyright etc.
50 * head/header tagline/endfooter
52 * dvi from lilypond .tex output? This is hairy, because we create dvi
53 from lilypond .tex *and* header output.
55 * multiple \score blocks?
66 ################################################################
67 # Users of python modules should include this snippet
68 # and customize variables below.
70 # We'll suffer this path init stuff as long as we don't install our
71 # python packages in <prefix>/lib/pythonx.y (and don't kludge around
72 # it as we do with teTeX on Red Hat Linux: set some environment var
73 # (PYTHONPATH) in profile)
75 # If set, LILYPONDPREFIX must take prevalence
76 # if datadir is not set, we're doing a build and LILYPONDPREFIX
77 import getopt, os, sys
78 datadir = '@local_lilypond_datadir@'
79 if not os.path.isdir (datadir):
80 datadir = '@lilypond_datadir@'
81 if os.environ.has_key ('LILYPONDPREFIX') :
82 datadir = os.environ['LILYPONDPREFIX']
83 while datadir[-1] == os.sep:
86 sys.path.insert (0, os.path.join (datadir, 'python'))
89 #if __name__ == '__main__':
95 program_name = 'ly2dvi'
97 original_dir = os.getcwd ()
98 temp_dir = os.path.join (original_dir, '%s.dir' % program_name)
102 ## ly2dvi: silly name?
103 ## do -P or -p by default?
104 ##help_summary = _ ("Run LilyPond using LaTeX for titling")
105 help_summary = _ ("Run LilyPond, add titles, generate printable document")
107 option_definitions = [
108 ('', 'd', 'dependencies',
109 _ ("write Makefile dependencies for every input file")),
110 ('', 'h', 'help', _ ("this help")),
111 (_ ("DIR"), 'I', 'include', _ ("add DIR to LilyPond's search path")),
113 _ ("keep all output, output to directory %s.dir") % program_name),
114 ('', '', 'no-lily', _ ("don't run LilyPond")),
115 ('', 'm', 'no-paper', _ ("produce MIDI output only")),
116 (_ ("FILE"), 'o', 'output', _ ("write ouput to FILE")),
117 (_ ("FILE"), 'f', 'find-pfa', _ ("find pfa fonts used in FILE")),
118 (_ ('RES'), '', 'preview-resolution',
119 _ ("set the resolution of the preview to RES")),
120 ('', 'P', 'postscript', _ ("generate PostScript output")),
121 ('', 'p', 'pdf', _ ("generate PDF output")),
122 ('', '', 'pdftex', _ ("use pdflatex to generate a PDF output")),
123 # FIXME: preview, picture; to indicate creation of a PNG?
124 ('', '', 'preview', _ ("make a picture of the first system")),
125 (_ ("KEY=VAL"), 's', 'set', _ ("change global setting KEY to VAL")),
126 ('', 'V', 'verbose', _ ("verbose")),
127 ('', 'v', 'version', _ ("print version number")),
128 ('', 'w', 'warranty', _ ("show warranty and copyright")),
134 preview_resolution = 90
141 tex_extension = '.tex'
144 layout_fields = ['dedication', 'title', 'subtitle', 'subsubtitle',
145 'footer', 'head', 'composer', 'arranger', 'instrument',
146 'opus', 'piece', 'metre', 'meter', 'poet', 'texttranslator']
149 # init to empty; values here take precedence over values in the file
151 ## TODO: change name.
155 'latexpackages' : ['geometry'],
165 extra_fields = extra_init.keys ()
166 fields = layout_fields + extra_fields
174 # Output formats that ly2dvi should create
175 targets = ['DVI', 'LATEX', 'MIDI', 'TEX']
177 track_dependencies_p = 0
178 dependency_files = []
182 # tex needs lots of memory, more than it gets by default on Debian
183 non_path_environment = {
184 'extra_mem_top' : '1000000',
185 'extra_mem_bottom' : '1000000',
186 'pool_size' : '250000',
189 def setup_environment ():
192 kpse = ly.read_pipe ('kpsexpand \$TEXMF')
193 texmf = re.sub ('[ \t\n]+$','', kpse)
194 type1_paths = ly.read_pipe ('kpsewhich -expand-path=\$T1FONTS')
197 # TODO: * prevent multiple addition.
198 # * clean TEXINPUTS, MFINPUTS, TFMFONTS,
199 # as these take prevalence over $TEXMF
200 # and thus may break tex run?
201 'TEXMF' : "{%s,%s}" % (datadir, texmf) ,
202 'GS_FONTPATH' : type1_paths,
203 'GS_LIB' : datadir + '/ps',
206 # $TEXMF is special, previous value is already taken care of
207 if os.environ.has_key ('TEXMF'):
208 del os.environ['TEXMF']
210 for key in environment.keys ():
211 val = environment[key]
212 if os.environ.has_key (key):
213 val = os.environ[key] + os.pathsep + val
214 os.environ[key] = val
216 for key in non_path_environment.keys ():
217 val = non_path_environment[key]
218 os.environ[key] = val
221 def set_setting (dict, key, val):
223 val = string.atoi (val)
225 #ly.warning (_ ("invalid value: %s") % `val`)
228 if type(val) == type ('hoi'):
230 val = string.atof (val)
232 #ly.warning (_ ("invalid value: %s") % `val`)
236 dict[key].append (val)
238 ly.warning (_ ("no such setting: `%s'") % `key`)
242 def print_environment ():
243 for (k,v) in os.environ.items ():
244 sys.stderr.write ("%s=\"%s\"\n" % (k,v))
246 def run_lilypond (files, dep_prefix):
249 opts = opts + ' ' + string.join (map (lambda x : '-I ' + x,
252 opts = opts + ' --output=lelie'
254 opts = opts + ' ' + string.join (map (lambda x : '-H ' + x,
257 opts = opts + ' --no-paper'
260 opts = opts + ' -f pdftex'
262 if track_dependencies_p:
263 opts = opts + " --dependencies"
265 opts = opts + ' --dep-prefix=%s' % dep_prefix
267 fs = string.join (files)
271 opts = opts + ' --verbose'
274 cmd = 'lilypond %s %s ' % (opts, fs)
276 # We unset verbose, because we always want to see lily's
278 save_verbose = verbose_p
279 status = ly.system (cmd, ignore_error = 1)
280 verbose_p = save_verbose
282 signal = 0x0f & status
283 exit_status = status >> 8
285 # 2 == user interrupt.
286 if signal and signal != 2:
287 sys.stderr.write ('\n\n')
288 ly.error (_ ("LilyPond crashed (signal %d).") % signal)
289 ly.error (_ ("Please submit a bug report to bug-lilypond@gnu.org"))
293 sys.stderr.write ('\n')
295 ly.error (_ ("LilyPond failed on input file %s (exit status %d)") % (files[0], exit_status))
298 ly.error (_ ("LilyPond failed on an input file (exit status %d)") % exit_status)
299 ly.error (_ ("Continuing..."))
300 global lilypond_error_p
304 def analyse_lilypond_output (filename, extra):
307 '''Grep FILENAME for interesting stuff, and
308 put relevant info into EXTRA.'''
309 filename = filename+tex_extension
310 ly.progress (_ ("Analyzing %s...") % filename)
311 s = open (filename).read ()
313 # search only the first 10k
315 for x in extra_fields:
316 m = re.search (r'\\def\\lilypondpaper%s{([^}]*)}'%x, s)
318 set_setting (extra, x, m.group (1))
321 def find_tex_files_for_base (base, extra):
324 Find the \header fields dumped from BASE.
328 for f in layout_fields:
329 if os.path.exists (base + '.' + f):
330 headerfiles[f] = base+'.'+f
332 if os.path.exists (base +'.dep'):
333 dependency_files.append (base + '.dep')
335 for f in extra_fields:
336 if os.path.exists (base + '.' + f):
337 extra[f].append (open (base + '.' + f).read ())
339 return (base+tex_extension,headerfiles)
342 def find_tex_files (files, extra):
344 Find all .tex files whose prefixes start with some name in FILES.
353 fname = os.path.basename (f)
354 fname = ly.strip_extension (fname, '.ly')
356 fname = fname + '-%d' % x
358 if os.path.exists (fname + tex_extension):
359 tfiles.append (find_tex_files_for_base (fname, extra))
360 analyse_lilypond_output (fname, extra)
366 fstr = string.join (files, ', ')
367 ly.warning (_ ("no LilyPond output found for `%s'") % fstr)
370 def one_latex_definition (defn, first):
372 for (k,v) in defn[1].items ():
373 val = open (v).read ()
374 if (string.strip (val)):
375 s = s + r'''\def\lilypond%s{%s}''' % (k, val)
377 s = s + r'''\let\lilypond%s\relax''' % k
381 s = s + '\\def\\mustmakelilypondtitle{}\n'
383 s = s + '\\def\\mustmakelilypondpiecetitle{}\n'
385 s = s + '\\input %s\n' % defn[0] # The final \n seems important here. It ensures that the footers and taglines end up on the right page.
389 ly_paper_to_latexpaper = {
391 'letter' : 'letterpaper',
395 #TODO: should set textheight (enlarge) depending on papersize.
396 def global_latex_preamble (extra):
397 '''construct preamble from EXTRA,'''
399 s = s + '% generation tag\n'
404 if extra['papersize']:
406 options = ly_paper_to_latexpaper[extra['papersize'][0]]
408 ly.warning (_ ("invalid value: `%s'") % `extra['papersize'][0]`)
411 if extra['latexoptions']:
412 options = options + ',' + extra['latexoptions'][-1]
414 s = s + '\\documentclass[%s]{article}\n' % options
416 if extra['language']:
417 s = s + r'\usepackage[%s]{babel}' % extra['language'][-1] + '\n'
420 s = s + '\\usepackage{%s}\n' \
421 % string.join (extra['latexpackages'], ',')
423 if extra['latexheaders']:
424 s = s + '\\include{%s}\n' \
425 % string.join (extra['latexheaders'], '}\n\\include{')
427 unit = extra['unit'][-1]
430 if extra['textheight']:
431 textheight = ',textheight=%f%s' % (extra['textheight'][0], unit)
433 orientation = 'portrait'
434 if extra['orientation']:
435 orientation = extra['orientation'][0]
437 # set sane geometry width (a4-width) for linewidth = -1.
438 maxlw = max (extra['linewidth'] + [-1])
440 # who the hell is 597 ?
443 linewidth = '%d%s' % (maxlw, unit)
444 s = s + '\geometry{width=%s%s,headheight=2mm,footskip=2mm,%s}\n' % (linewidth, textheight, orientation)
446 if extra['latexoptions']:
447 s = s + '\geometry{twosideshift=4mm}\n'
450 \usepackage[latin1]{inputenc}
454 if extra['pagenumber'] and extra['pagenumber'][-1] and extra['pagenumber'][-1] != 'no':
455 s = s + '\setcounter{page}{%d}\n' % (extra['pagenumber'][-1])
456 s = s + '\\pagestyle{plain}\n'
458 s = s + '\\pagestyle{empty}\n'
464 def global_latex_definition (tfiles, extra):
465 '''construct preamble from EXTRA, dump Latex stuff for each
466 lily output file in TFILES after that, and return the Latex file constructed. '''
469 s = global_latex_preamble (extra) + '\\begin{document}\n'
470 s = s + '\\parindent 0pt\n'
471 s = s + '\\thispagestyle{firstpage}\n'
475 s = s + one_latex_definition (t, first)
479 s = s + '\\thispagestyle{lastpage}\n'
480 s = s + '\\end{document}'
484 def run_latex (files, outbase, extra):
486 '''Construct latex file, for FILES and EXTRA, dump it into
487 OUTBASE.latex. Run LaTeX on it.
494 latex_fn = outbase + '.latex'
496 wfs = find_tex_files (files, extra)
497 s = global_latex_definition (wfs, extra)
499 f = open (latex_fn, 'w')
503 cmd = latex_cmd + ' \\\\nonstopmode \\\\input %s' % latex_fn
504 status = ly.system (cmd, ignore_error = 1)
505 signal = 0xf & status
506 exit_status = status >> 8
512 logstr = open (outbase + '.log').read ()
513 m = re.search ("\n!", logstr)
515 logstr = logstr[start:start+200]
519 ly.error (_ ("LaTeX failed on the output file."))
520 ly.error (_ ("The error log is as follows:"))
521 sys.stderr.write (logstr + '\n')
525 # make a preview by rendering only the 1st line.
526 preview_fn = outbase + '.preview.tex'
527 f = open (preview_fn, 'w')
533 \def\interscoreline{\endinput}
536 ''' % (global_latex_preamble (extra), outbase))
539 cmd = '%s \\\\nonstopmode \\\\input %s' % (latex_cmd, preview_fn)
543 def run_dvips (outbase, extra):
546 '''Run dvips using the correct options taken from EXTRA,
547 leaving a PS file in OUTBASE.ps
554 if extra['papersize']:
555 opts = opts + ' -t%s' % extra['papersize'][0]
557 if extra['orientation'] and extra['orientation'][0] == 'landscape':
558 opts = opts + ' -tlandscape'
561 opts = opts + ' -Ppdf -G0 -u lilypond.map'
563 cmd = 'dvips %s -o%s %s' % (opts, outbase + '.ps', outbase + '.dvi')
567 cmd = 'dvips -E -o%s %s' % ( outbase + '.preview.ps', outbase + '.preview.dvi')
571 cmd = 'ps2pdf %s.ps %s.pdf' % (outbase , outbase)
574 def get_bbox (filename):
576 ####ly.system ('gs -sDEVICE=bbox -q -sOutputFile=- -dNOPAUSE %s -c quit > %s.bbox 2>&1 ' % (filename, filename))
577 #### FIXME: 2>&1 ? --jcn
578 ly.system ('gs -sDEVICE=bbox -q -sOutputFile=- -dNOPAUSE %s -c quit > %s.bbox' % (filename, filename))
580 box = open (filename + '.bbox').read()
581 m = re.match ('^%%BoundingBox: ([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+)', box)
584 gr = map (string.atoi, m.groups ())
589 # cut & paste from lilypond-book.
591 def make_preview (name, extra):
592 bbox = get_bbox (name + '.preview.ps')
594 fo = open (name + '.trans.eps' , 'w')
595 fo.write ('%d %d translate\n' % (-bbox[0]+margin, -bbox[1]+margin))
598 x = (2* margin + bbox[2] - bbox[0]) * preview_resolution / 72.
599 y = (2* margin + bbox[3] - bbox[1]) * preview_resolution / 72.
601 cmd = r'''gs -g%dx%d -sDEVICE=pgm -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -q -sOutputFile=- -r%d -dNOPAUSE %s %s -c quit | pnmtopng > %s'''
603 cmd = cmd % (x, y, preview_resolution, name + '.trans.eps',
604 name + '.preview.ps',name + '.png')
608 status = ly.system (cmd)
610 os.unlink (name + '.png')
611 ly.error (_ ("Removing output file"))
614 def generate_dependency_file (depfile, outname):
615 df = open (depfile, 'w')
616 df.write (outname + ':' )
618 for d in dependency_files:
620 s = re.sub ('#[^\n]*\n', '', s)
621 s = re.sub (r'\\\n', ' ', s)
622 m = re.search ('.*:(.*)\n', s)
624 # ugh. Different targets?
626 df.write ( m.group (1) + ' ' )
631 def find_file_in_path (path, name):
632 for d in string.split (path, os.pathsep):
633 if name in os.listdir (d):
634 return os.path.join (d, name)
636 # Added as functionality to ly2dvi, because ly2dvi may well need to do this
639 def find_pfa_fonts (name):
640 s = open (name).read ()
641 if s[:len (PS)] != PS:
643 ly.error (_ ("not a PostScript file: `%s\'" % name))
646 m = re.match ('.*?/(feta[-a-z0-9]+) +findfont', s[here:], re.DOTALL)
650 pfa.append (m.group (1))
651 m = re.match ('.*?/(feta[-a-z0-9]+) +findfont', s[here:], re.DOTALL)
655 (sh, long) = ly.getopt_args (option_definitions)
657 (options, files) = getopt.getopt (sys.argv[1:], sh, long)
658 except getopt.error, s:
659 sys.stderr.write ('\n')
660 ly.error (_ ("getopt says: `%s\'" % s))
661 sys.stderr.write ('\n')
671 elif o == '--help' or o == '-h':
674 elif o == '--find-pfa' or o == '-f':
675 fonts = map (lambda x: x + '.pfa', find_pfa_fonts (a))
676 files = map (lambda x:
677 find_file_in_path (os.environ['GS_FONTPATH'], x),
679 print string.join (files, ' ')
681 elif o == '--include' or o == '-I':
682 include_path.append (a)
683 elif o == '--postscript' or o == '-P':
684 targets.append ('PS')
685 elif o == '--pdf' or o == '-p':
686 targets.append ('PS')
687 targets.append ('PDF')
688 elif o == '--keep' or o == '-k':
690 elif o == '--no-lily':
692 elif o == '--preview':
694 targets.append ('PNG')
695 elif o == '--preview-resolution':
696 preview_resolution = string.atoi (a)
697 elif o == '--no-paper' or o == '-m':
700 elif o == '--output' or o == '-o':
702 elif o == '--set' or o == '-s':
703 ss = string.split (a, '=')
704 set_setting (extra_init, ss[0], ss[1])
705 elif o == '--dependencies' or o == '-d':
706 track_dependencies_p = 1
707 elif o == '--verbose' or o == '-V':
709 elif o == '--version' or o == '-v':
710 ly.identify (sys.stdout)
712 elif o == '--pdftex':
713 latex_cmd = 'pdflatex'
714 targets.remove('DVI')
715 targets.append('PDFTEX')
717 tex_extension = '.pdftex'
718 elif o == '--warranty' or o == '-w':
719 status = os.system ('lilypond -w')
725 # Don't convert input files to abspath, rather prepend '.' to include
727 include_path.insert (0, '.')
729 # As a neat trick, add directory part of first input file
730 # to include path. That way you can do without the clumsy -I in:
732 # ly2dvi -I foe/bar/baz foo/bar/baz/baz.ly
733 if files and files[0] != '-' and os.path.dirname (files[0]) != '.':
734 include_path.append (os.path.dirname (files[0]))
736 include_path = map (ly.abspath, include_path)
738 if files and (files[0] == '-' or output_name == '-'):
741 output_name = 'lelie'
743 ly.progress (_ ("pseudo filter") + '\n')
746 ly.error (_ ("pseudo filter only for single input file"))
751 ly.error (_ ("no files specified on command line"))
755 ly.identify (sys.stderr)
756 original_output = output_name
758 # Ugh, maybe make a setup () function
759 files = map (lambda x: ly.strip_extension (x, '.ly'), files)
761 # hmmm. Wish I'd 've written comments when I wrote this.
762 # now it looks complicated.
764 (outdir, outbase) = ('','')
766 outbase = os.path.basename (files[0])
767 outdir = ly.abspath ('.')
768 elif output_name[-1] == os.sep:
769 outdir = ly.abspath (output_name)
770 outbase = os.path.basename (files[0])
772 (outdir, outbase) = os.path.split (ly.abspath (output_name))
774 for i in ('.dvi', '.latex', '.ly', '.ps', '.tex', '.pdftex'):
775 output_name = ly.strip_extension (output_name, i)
776 outbase = ly.strip_extension (outbase, i)
778 for i in files[:] + [output_name]:
779 if string.find (i, ' ') >= 0:
780 ly.error (_ ("filename should not contain spaces: `%s'") %
784 if os.path.dirname (output_name) != '.':
785 dep_prefix = os.path.dirname (output_name)
789 reldir = os.path.dirname (output_name)
790 if outdir != '.' and (track_dependencies_p or targets):
791 ly.mkdir_p (outdir, 0777)
794 tmpdir = ly.setup_temp ()
796 # to be sure, add tmpdir *in front* of inclusion path.
797 #os.environ['TEXINPUTS'] = tmpdir + ':' + os.environ['TEXINPUTS']
800 # We catch all exceptions, because we need to do stuff at exit:
801 # * copy any successfully generated stuff from tempdir and
802 # notify user of that
806 run_lilypond (files, dep_prefix)
808 # TODO: friendly message about LilyPond setup/failing?
810 # TODO: lilypond should fail with different
812 # - guile setup/startup failure
813 # - font setup failure
814 # - init.ly setup failure
815 # - parse error in .ly
816 # - unexpected: assert/core dump
819 traceback.print_exc ()
821 # Our LilyPond pseudo filter always outputs to 'lelie'
822 # have subsequent stages and use 'lelie' output.
826 if 'PNG' in targets and 'PS' not in targets:
827 targets.append ('PS')
828 if 'PS' in targets and 'DVI' not in targets:
829 targets.append('DVI')
833 run_latex (files, outbase, extra_init)
834 # unless: add --tex, or --latex?
835 targets.remove ('TEX')
836 targets.remove('LATEX')
838 # TODO: friendly message about TeX/LaTeX setup,
839 # trying to run tex/latex by hand
841 targets.remove ('DVI')
843 targets.remove ('PS')
845 traceback.print_exc ()
849 run_dvips (outbase, extra_init)
852 targets.remove ('PS')
854 traceback.print_exc ()
857 make_preview (outbase, extra_init)
859 if 'PDFTEX' in targets:
861 run_latex (files, outbase, extra_init)
862 # unless: add --tex, or --latex?
863 targets.remove ('TEX')
864 targets.remove ('LATEX')
865 targets.remove ('PDFTEX')
866 if 'PDF' not in targets:
867 targets.append('PDF')
869 # TODO: friendly message about TeX/LaTeX setup,
870 # trying to run tex/latex by hand
871 if 'PDFTEX' in targets:
872 targets.remove ('PDFTEX')
874 targets.remove ('PDF')
876 targets.remove ('PS')
878 traceback.print_exc ()
880 # add DEP to targets?
881 if track_dependencies_p:
882 depfile = os.path.join (outdir, outbase + '.dep')
883 generate_dependency_file (depfile, depfile)
884 if os.path.isfile (depfile):
885 ly.progress (_ ("dependencies output to `%s'...") %
891 for i in 'PDF', 'PS', 'PNG', 'DVI', 'LATEX':
896 ly.progress (_ ("%s output to <stdout>...") % i)
897 outname = outbase + '.' + string.lower (main_target)
898 if os.path.isfile (outname):
899 sys.stdout.write (open (outname).read ())
901 ly.warning (_ ("can't find file: `%s'") % outname)
905 # Hmm, if this were a function, we could call it the except: clauses
907 ext = string.lower (i)
908 ly.cp_to_dir ('.*\.%s$' % ext, outdir)
909 outname = outbase + '.' + string.lower (i)
910 abs = os.path.join (outdir, outname)
912 outname = os.path.join (reldir, outname)
913 if os.path.isfile (abs):
914 ly.progress (_ ("%s output to `%s'...") % (i, outname))
917 ly.warning (_ ("can't find file: `%s'") % outname)
919 os.chdir (original_dir)
922 sys.exit (lilypond_error_p)