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
40 * for foo.ly, rename ly2dvi.dir to out-ly2dvi, foo.ly2dvi, foo.dir ?
43 * move versatile taglines,
46 beginfooter=\mutopiaPD
47 endfooter=\tagline -> 'lily was here <version>'
50 lilytagline (->lily was here), usertagline, copyright, lily-version
53 * head/header tagline/endfooter
55 * dvi from lilypond .tex output? This is hairy, because we create dvi
56 from lilypond .tex *and* header output.
58 * multiple \score blocks?
60 * Introduce verbosity levels
62 0 = QUIET: mute all command output, no ly2dvi progress
63 1 = BRIEF: mute all command output, only ly2dvi progress
64 2a = NORMAL: show only LilyPond command output, show ly2dvi progress
65 2b = NORMAL: show command output, show ly2dvi progress
66 3 = VERBOSE: show command output, run lilypond --verbose
67 4 = DEBUGGING: show all command output, run lilypond --verbose, print
68 environment and all kinds of client side debugging stuff
70 Currently, we only have 1 and 4, but we kludge to have 2a and 4.
78 ################################################################
79 # Users of python modules should include this snippet
80 # and customize variables below.
82 # We'll suffer this path init stuff as long as we don't install our
83 # python packages in <prefix>/lib/pythonx.y (and don't kludge around
84 # it as we do with teTeX on Red Hat Linux: set some environment var
85 # (PYTHONPATH) in profile)
87 # If set, LILYPONDPREFIX must take prevalence
88 # if datadir is not set, we're doing a build and LILYPONDPREFIX
89 import getopt, os, sys
90 datadir = '@local_lilypond_datadir@'
91 if not os.path.isdir (datadir):
92 datadir = '@lilypond_datadir@'
93 if os.environ.has_key ('LILYPONDPREFIX') :
94 datadir = os.environ['LILYPONDPREFIX']
95 while datadir[-1] == os.sep:
98 sys.path.insert (0, os.path.join (datadir, 'python'))
101 #if __name__ == '__main__':
108 program_name = 'ly2dvi'
111 original_dir = os.getcwd ()
112 temp_dir = os.path.join (original_dir, '%s.dir' % program_name)
114 preview_resolution = 90
118 ## ly2dvi: silly name?
119 ## do -P or -p by default?
120 ##help_summary = _ ("Run LilyPond using LaTeX for titling")
121 help_summary = _ ("Run LilyPond, add titles, generate printable document")
122 copyright = ('Han-Wen Nienhuys <hanwen@cs.uu.nl',
123 'Jan Nieuwenhuizen <janneke@gnu.org')
125 option_definitions = [
126 ('', 'd', 'dependencies',
127 _ ("write Makefile dependencies for every input file")),
128 ('', 'h', 'help', _ ("this help")),
129 ('', '', 'debug', _ ("print even more output")),
130 (_ ("DIR"), 'I', 'include', _ ("add DIR to LilyPond's search path")),
132 _ ("keep all output, output to directory %s.dir") % program_name),
133 ('', '', 'no-lily', _ ("don't run LilyPond")),
134 ('', 'm', 'no-paper', _ ("produce MIDI output only")),
135 (_ ("FILE"), 'o', 'output', _ ("write ouput to FILE")),
136 (_ ("FILE"), 'f', 'find-pfa', _ ("find pfa fonts used in FILE")),
137 (_ ('RES'), '', 'preview-resolution',
138 _ ("set the resolution of the preview to RES")),
139 ('', 'P', 'postscript', _ ("generate PostScript output")),
140 ('', 'p', 'pdf', _ ("generate PDF output")),
141 ('', '', 'pdftex', _ ("use pdflatex to generate a PDF output")),
142 # FIXME: preview, picture; to indicate creation of a PNG?
143 ('', '', 'preview', _ ("make a picture of the first system")),
144 (_ ("KEY=VAL"), 's', 'set', _ ("change global setting KEY to VAL")),
145 ('', 'V', 'verbose', _ ("verbose")),
146 ('', 'v', 'version', _ ("print version number")),
147 ('', 'w', 'warranty', _ ("show warranty and copyright")),
157 tex_extension = '.tex'
159 #lilypond_binary = 'valgrind --suppressions=%(home)s/usr/src/guile-1.6.supp --num-callers=10 %(home)s/usr/src/lilypond/lily/out/lilypond '% { 'home' : '/home/hanwen' }
161 lilypond_binary = os.path.join ('@bindir@', 'lilypond')
163 # only use installed binary when we're installed too.
164 if '@bindir@' == ('@' + 'bindir@') or not os.path.exists (lilypond_binary):
165 lilypond_binary = 'lilypond'
168 layout_fields = ['dedication', 'title', 'subtitle', 'subsubtitle',
169 'footer', 'head', 'composer', 'arranger', 'instrument',
170 'opus', 'piece', 'metre', 'meter', 'poet', 'texttranslator']
173 # init to empty; values here take precedence over values in the file
175 ## TODO: change name.
179 'latexpackages' : ['geometry'],
189 extra_fields = extra_init.keys ()
190 fields = layout_fields + extra_fields
198 # Output formats that ly2dvi should create
199 targets = ['DVI', 'LATEX', 'MIDI', 'TEX']
201 track_dependencies_p = 0
202 dependency_files = []
205 def set_setting (dict, key, val):
207 val = string.atoi (val)
209 #ly.warning (_ ("invalid value: %s") % `val`)
212 if type(val) == type ('hoi'):
214 val = string.atof (val)
216 #ly.warning (_ ("invalid value: %s") % `val`)
220 dict[key].append (val)
222 ly.warning (_ ("no such setting: `%s'") % `key`)
226 def run_lilypond (files, dep_prefix):
229 opts = opts + ' ' + string.join (map (lambda x : '-I ' + x,
232 opts = opts + ' --output=lelie'
234 opts = opts + ' ' + string.join (map (lambda x : '-H ' + x,
237 opts = opts + ' --no-paper'
240 opts = opts + ' -f pdftex'
242 if track_dependencies_p:
243 opts = opts + " --dependencies"
245 opts = opts + ' --dep-prefix=%s' % dep_prefix
247 fs = string.join (files)
251 opts = opts + ' --verbose'
254 ly.print_environment ()
256 cmd = string.join ((lilypond_binary, opts, fs))
257 status = ly.system (cmd, ignore_error = 1, progress_p = 1)
258 signal = 0x0f & status
259 exit_status = status >> 8
261 # 2 == user interrupt.
262 if signal and signal != 2:
263 sys.stderr.write ('\n\n')
264 ly.error (_ ("LilyPond crashed (signal %d).") % signal)
265 ly.error (_ ("Please submit a bug report to bug-lilypond@gnu.org"))
269 sys.stderr.write ('\n')
271 ly.error (_ ("LilyPond failed on input file %s (exit status %d)") % (files[0], exit_status))
274 ly.error (_ ("LilyPond failed on an input file (exit status %d)") % exit_status)
275 ly.error (_ ("Continuing..."))
276 global lilypond_error_p
280 def analyse_lilypond_output (filename, extra):
283 '''Grep FILENAME for interesting stuff, and
284 put relevant info into EXTRA.'''
285 filename = filename+tex_extension
286 ly.progress (_ ("Analyzing %s...") % filename)
287 s = open (filename).read ()
289 # search only the first 10k
291 for x in extra_fields:
292 m = re.search (r'\\def\\lilypondpaper%s{([^}]*)}'%x, s)
294 set_setting (extra, x, m.group (1))
297 def find_tex_files_for_base (base, extra):
300 Find the \header fields dumped from BASE.
304 for f in layout_fields:
305 if os.path.exists (base + '.' + f):
306 headerfiles[f] = base+'.'+f
308 if os.path.exists (base +'.dep'):
309 dependency_files.append (base + '.dep')
311 for f in extra_fields:
312 if os.path.exists (base + '.' + f):
313 extra[f].append (open (base + '.' + f).read ())
315 return (base+tex_extension,headerfiles)
318 def find_tex_files (files, extra):
320 Find all .tex files whose prefixes start with some name in FILES.
329 fname = os.path.basename (f)
330 fname = ly.strip_extension (fname, '.ly')
332 fname = fname + '-%d' % x
334 if os.path.exists (fname + tex_extension):
335 tfiles.append (find_tex_files_for_base (fname, extra))
336 analyse_lilypond_output (fname, extra)
342 fstr = string.join (files, ', ')
343 ly.warning (_ ("no LilyPond output found for `%s'") % fstr)
346 def one_latex_definition (defn, first):
348 for (k,v) in defn[1].items ():
349 val = open (v).read ()
350 if (string.strip (val)):
351 s = s + r'''\def\lilypond%s{%s}''' % (k, val)
353 s = s + r'''\let\lilypond%s\relax''' % k
357 s = s + '\\def\\mustmakelilypondtitle{}\n'
359 s = s + '\\def\\mustmakelilypondpiecetitle{}\n'
361 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.
365 ly_paper_to_latexpaper = {
366 'letter' : 'letterpaper',
373 #TODO: should set textheight (enlarge) depending on papersize.
374 def global_latex_preamble (extra):
375 '''construct preamble from EXTRA,'''
377 s = s + '% generation tag\n'
382 if extra['papersize']:
384 options = ly_paper_to_latexpaper[extra['papersize'][0]]
386 ly.warning (_ ("invalid value: `%s'") % `extra['papersize'][0]`)
389 if extra['latexoptions']:
390 options = options + ',' + extra['latexoptions'][-1]
392 s = s + '\\documentclass[%s]{article}\n' % options
394 if extra['language']:
395 s = s + r'\usepackage[%s]{babel}' % extra['language'][-1] + '\n'
398 s = s + '\\usepackage{%s}\n' \
399 % string.join (extra['latexpackages'], ',')
401 if extra['latexheaders']:
402 s = s + '\\include{%s}\n' \
403 % string.join (extra['latexheaders'], '}\n\\include{')
405 unit = extra['unit'][-1]
408 if extra['textheight']:
409 textheight = ',textheight=%f%s' % (extra['textheight'][0], unit)
411 orientation = 'portrait'
412 if extra['orientation']:
413 orientation = extra['orientation'][0]
415 # set sane geometry width (a4-width) for linewidth = -1.
416 maxlw = max (extra['linewidth'] + [-1])
418 # who the hell is 597 ?
421 linewidth = '%d%s' % (maxlw, unit)
422 s = s + '\geometry{width=%s%s,headheight=2mm,footskip=2mm,%s}\n' % (linewidth, textheight, orientation)
424 if extra['latexoptions']:
425 s = s + '\geometry{twosideshift=4mm}\n'
428 \usepackage[latin1]{inputenc}
432 if extra['pagenumber'] and extra['pagenumber'][-1] and extra['pagenumber'][-1] != 'no':
433 s = s + '\setcounter{page}{%d}\n' % (extra['pagenumber'][-1])
434 s = s + '\\pagestyle{plain}\n'
436 s = s + '\\pagestyle{empty}\n'
442 def global_latex_definition (tfiles, extra):
443 '''construct preamble from EXTRA, dump Latex stuff for each
444 lily output file in TFILES after that, and return the Latex file constructed. '''
447 s = global_latex_preamble (extra) + '\\begin{document}\n'
448 s = s + '\\parindent 0pt\n'
449 s = s + '\\thispagestyle{firstpage}\n'
453 s = s + one_latex_definition (t, first)
457 s = s + '\\thispagestyle{lastpage}\n'
458 s = s + '\\end{document}'
462 def run_latex (files, outbase, extra):
464 '''Construct latex file, for FILES and EXTRA, dump it into
465 OUTBASE.latex. Run LaTeX on it.
472 latex_fn = outbase + '.latex'
474 wfs = find_tex_files (files, extra)
475 s = global_latex_definition (wfs, extra)
477 f = open (latex_fn, 'w')
481 cmd = latex_cmd + ' \\\\nonstopmode \\\\input %s' % latex_fn
482 # Ugh. (La)TeX writes progress and error messages on stdout
484 cmd += ' 1>/dev/stderr'
485 status = ly.system (cmd, ignore_error = 1)
486 signal = 0xf & status
487 exit_status = status >> 8
493 logstr = open (outbase + '.log').read ()
494 m = re.search ("\n!", logstr)
496 logstr = logstr[start:start+200]
500 ly.error (_ ("LaTeX failed on the output file."))
501 ly.error (_ ("The error log is as follows:"))
502 sys.stderr.write (logstr + '\n')
506 # make a preview by rendering only the 1st line.
507 preview_fn = outbase + '.preview.tex'
508 f = open (preview_fn, 'w')
509 wfs = find_tex_files (files, extra)
510 s = global_latex_definition (wfs, extra)
512 s = re.sub ('thispagestyle{firstpage}', r'''thispagestyle{empty}%
513 \\def\\interscoreline{\\endinput}''',s )
514 s = re.sub ('thispagestyle{lastpage}', r'''thispagestyle{empty}%
515 \\def\\interscoreline{\\endinput}''',s )
518 cmd = '%s \\\\nonstopmode \\\\input %s' % (latex_cmd, preview_fn)
522 def run_dvips (outbase, extra):
525 '''Run dvips using the correct options taken from EXTRA,
526 leaving a PS file in OUTBASE.ps
533 if extra['papersize']:
534 opts = opts + ' -t%s' % extra['papersize'][0]
536 if extra['orientation'] and extra['orientation'][0] == 'landscape':
537 opts = opts + ' -tlandscape'
541 where = ly.read_pipe ('kpsewhich feta20.pfa').strip()
546 pfa_file = open (where, 'r')
551 opts = opts + ' -Ppdf -G0 -u lilypond.map'
553 ly.warning (_ ('''Trying create PDF, but no PFA fonts found.
554 Using bitmap fonts instead. This will look bad.'''))
556 cmd = 'dvips %s -o%s %s' % (opts, outbase + '.ps', outbase + '.dvi')
560 cmd = 'dvips -E -o%s %s' % ( outbase + '.preview.ps', outbase + '.preview.dvi')
564 cmd = 'ps2pdf %s.ps %s.pdf' % (outbase , outbase)
567 def generate_dependency_file (depfile, outname):
568 df = open (depfile, 'w')
569 df.write (outname + ':' )
571 for d in dependency_files:
573 s = re.sub ('#[^\n]*\n', '', s)
574 s = re.sub (r'\\\n', ' ', s)
575 m = re.search ('.*:(.*)\n', s)
577 # ugh. Different targets?
579 df.write ( m.group (1) + ' ' )
584 def find_file_in_path (path, name):
585 for d in string.split (path, os.pathsep):
586 if name in os.listdir (d):
587 return os.path.join (d, name)
589 # Added as functionality to ly2dvi, because ly2dvi may well need to do this
592 def find_pfa_fonts (name):
593 s = open (name).read ()
594 if s[:len (PS)] != PS:
596 ly.error (_ ("not a PostScript file: `%s\'" % name))
599 m = re.match ('.*?/(feta[-a-z0-9]+) +findfont', s[here:], re.DOTALL)
603 pfa.append (m.group (1))
604 m = re.match ('.*?/(feta[-a-z0-9]+) +findfont', s[here:], re.DOTALL)
608 (sh, long) = ly.getopt_args (option_definitions)
610 (options, files) = getopt.getopt (sys.argv[1:], sh, long)
611 except getopt.error, s:
612 sys.stderr.write ('\n')
613 ly.error (_ ("getopt says: `%s\'" % s))
614 sys.stderr.write ('\n')
624 elif o == '--help' or o == '-h':
627 elif o == '--find-pfa' or o == '-f':
628 fonts = map (lambda x: x + '.pfa', find_pfa_fonts (a))
629 files = map (lambda x:
630 find_file_in_path (os.environ['GS_FONTPATH'], x),
632 print string.join (files, ' ')
634 elif o == '--include' or o == '-I':
635 include_path.append (a)
636 elif o == '--postscript' or o == '-P':
637 targets.append ('PS')
638 elif o == '--pdf' or o == '-p':
639 targets.append ('PS')
640 targets.append ('PDF')
641 elif o == '--keep' or o == '-k':
646 elif o == '--no-lily':
648 elif o == '--preview':
650 targets.append ('PNG')
651 elif o == '--preview-resolution':
652 preview_resolution = string.atoi (a)
653 elif o == '--no-paper' or o == '-m':
656 elif o == '--output' or o == '-o':
658 elif o == '--set' or o == '-s':
659 ss = string.split (a, '=')
660 set_setting (extra_init, ss[0], ss[1])
661 elif o == '--dependencies' or o == '-d':
662 track_dependencies_p = 1
663 elif o == '--verbose' or o == '-V':
665 elif o == '--version' or o == '-v':
666 ly.identify (sys.stdout)
668 elif o == '--pdftex':
669 latex_cmd = 'pdflatex'
670 targets.remove('DVI')
671 targets.append('PDFTEX')
673 tex_extension = '.pdftex'
674 elif o == '--warranty' or o == '-w':
675 status = os.system ('%s -w' % lilypond_binary)
680 unimplemented_option () # signal programming error
682 # Don't convert input files to abspath, rather prepend '.' to include
684 include_path.insert (0, '.')
686 # As a neat trick, add directory part of first input file
687 # to include path. That way you can do without the clumsy -I in:
689 # ly2dvi -I foe/bar/baz foo/bar/baz/baz.ly
690 if files and files[0] != '-' and os.path.dirname (files[0]) != '.':
691 include_path.append (os.path.dirname (files[0]))
693 include_path = map (ly.abspath, include_path)
695 if files and (files[0] == '-' or output_name == '-'):
698 output_name = 'lelie'
700 ly.progress (_ ("pseudo filter") + '\n')
703 ly.error (_ ("pseudo filter only for single input file"))
708 ly.error (_ ("no files specified on command line"))
712 ly.identify (sys.stderr)
713 original_output = output_name
715 # Ugh, maybe make a setup () function
716 files = map (lambda x: ly.strip_extension (x, '.ly'), files)
718 # hmmm. Wish I'd 've written comments when I wrote this.
719 # now it looks complicated.
721 (outdir, outbase) = ('','')
723 outbase = os.path.basename (files[0])
724 outdir = ly.abspath ('.')
725 elif output_name[-1] == os.sep:
726 outdir = ly.abspath (output_name)
727 outbase = os.path.basename (files[0])
729 (outdir, outbase) = os.path.split (ly.abspath (output_name))
731 for i in ('.dvi', '.latex', '.ly', '.ps', '.tex', '.pdftex'):
732 output_name = ly.strip_extension (output_name, i)
733 outbase = ly.strip_extension (outbase, i)
735 for i in files[:] + [output_name]:
736 if string.find (i, ' ') >= 0:
737 ly.error (_ ("filename should not contain spaces: `%s'") %
741 if os.path.dirname (output_name) != '.':
742 dep_prefix = os.path.dirname (output_name)
746 reldir = os.path.dirname (output_name)
747 if outdir != '.' and (track_dependencies_p or targets):
748 ly.mkdir_p (outdir, 0777)
750 tmpdir = ly.setup_temp ()
751 ly.setup_environment ()
753 # to be sure, add tmpdir *in front* of inclusion path.
754 #os.environ['TEXINPUTS'] = tmpdir + ':' + os.environ['TEXINPUTS']
757 # We catch all exceptions, because we need to do stuff at exit:
758 # * copy any successfully generated stuff from tempdir and
759 # notify user of that
763 run_lilypond (files, dep_prefix)
765 ### ARGH. This also catches python programming errors.
766 ### this should only catch lilypond nonzero exit status
770 # TODO: friendly message about LilyPond setup/failing?
774 traceback.print_exc ()
776 ly.warning (_("Running LilyPond failed. Rerun with --verbose for a trace."))
778 # Our LilyPond pseudo filter always outputs to 'lelie'
779 # have subsequent stages and use 'lelie' output.
783 if 'PNG' in targets and 'PS' not in targets:
784 targets.append ('PS')
785 if 'PS' in targets and 'DVI' not in targets:
786 targets.append('DVI')
790 run_latex (files, outbase, extra_init)
791 # unless: add --tex, or --latex?
792 targets.remove ('TEX')
793 targets.remove('LATEX')
795 # TODO: friendly message about TeX/LaTeX setup,
796 # trying to run tex/latex by hand
798 targets.remove ('DVI')
800 targets.remove ('PS')
802 traceback.print_exc ()
806 run_dvips (outbase, extra_init)
810 targets.remove ('PS')
812 traceback.print_exc ()
814 ly.warning (_("Failed to make PS file. Rerun with --verbose for a trace."))
817 ly.make_preview (outbase)
819 if 'PDFTEX' in targets:
821 run_latex (files, outbase, extra_init)
822 # unless: add --tex, or --latex?
823 targets.remove ('TEX')
824 targets.remove ('LATEX')
825 targets.remove ('PDFTEX')
826 if 'PDF' not in targets:
827 targets.append('PDF')
829 # TODO: friendly message about TeX/LaTeX setup,
830 # trying to run tex/latex by hand
831 if 'PDFTEX' in targets:
832 targets.remove ('PDFTEX')
834 targets.remove ('PDF')
836 targets.remove ('PS')
838 traceback.print_exc ()
840 ly.warning (_("Running LaTeX falied. Rerun with --verbose for a trace."))
843 # add DEP to targets?
844 if track_dependencies_p:
845 depfile = os.path.join (outdir, outbase + '.dep')
846 generate_dependency_file (depfile, depfile)
847 if os.path.isfile (depfile):
848 ly.progress (_ ("dependencies output to `%s'...") %
854 for i in 'PDF', 'PS', 'PNG', 'DVI', 'LATEX':
859 ly.progress (_ ("%s output to <stdout>...") % i)
860 outname = outbase + '.' + string.lower (main_target)
861 if os.path.isfile (outname):
862 sys.stdout.write (open (outname).read ())
864 ly.warning (_ ("can't find file: `%s'") % outname)
868 # Hmm, if this were a function, we could call it the except: clauses
870 ext = string.lower (i)
871 ly.cp_to_dir ('.*\.%s$' % ext, outdir)
872 outname = outbase + '.' + string.lower (i)
873 abs = os.path.join (outdir, outname)
875 outname = os.path.join (reldir, outname)
876 if os.path.isfile (abs):
877 ly.progress (_ ("%s output to `%s'...") % (i, outname))
880 ly.warning (_ ("can't find file: `%s'") % outname)
882 os.chdir (original_dir)
885 sys.exit (lilypond_error_p)