]> git.donarmstrong.com Git - lilypond.git/blob - scripts/ly2dvi.py
* make/lilypond-vars.make (PYTHONPATH): Add python's outdir to
[lilypond.git] / scripts / ly2dvi.py
1 #!@PYTHON@
2 #
3 # ly2dvi.py -- Run LilyPond, add titles to bare score, generate printable
4 #              document
5 #              Invokes: lilypond, latex (or pdflatex), dvips, ps2pdf, gs
6
7 # source file of the GNU LilyPond music typesetter
8
9 # (c) 1998--2002  Han-Wen Nienhuys <hanwen@cs.uu.nl>
10 #                 Jan Nieuwenhuizen <janneke@gnu.org>
11
12 # This is the third incarnation of ly2dvi.
13 #
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)
17 #
18
19 # Note: gettext work best if we use ' for docstrings and "
20 #       for gettextable strings.
21 #       --> DO NOT USE ''' for docstrings.
22
23
24 '''
25 TODO:
26
27   * figure out which set of command line options should make ly2dvi:
28
29       na: create tex only?  
30       na: create latex only? 
31       na: create tex and latex
32       default: create dvi only
33       na: create tex, latex and dvi
34       -P: create dvi and ps
35       na: * create ps only
36
37      etc.
38
39      for foo.ly, rename ly2dvi.dir to out-ly2dvi, foo.ly2dvi, foo.dir ?
40      
41   * move versatile taglines, 
42   
43      \header {
44         beginfooter=\mutopiaPD
45         endfooter=\tagline  -> 'lily was here <version>'
46      }
47
48      lilytagline (->lily was here), usertagline, copyright etc.
49
50   * head/header tagline/endfooter
51
52   * dvi from lilypond .tex output?  This is hairy, because we create dvi
53     from lilypond .tex *and* header output.
54
55   * multiple \score blocks?
56   
57 '''
58
59 import __main__
60 import operator
61 import re
62 import stat
63 import string
64 import traceback
65
66 ################################################################
67 # Users of python modules should include this snippet
68 # and customize variables below.
69
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)
74
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:
84                 datadir= datadir[:-1]
85
86 sys.path.insert (0, os.path.join (datadir, 'python'))
87
88 # Customize these
89 #if __name__ == '__main__':
90
91 import lilylib as ly
92 global _;_=ly._
93
94 # lilylib globals
95 program_name = 'ly2dvi'
96 verbose_p = 0
97 original_dir = os.getcwd ()
98 temp_dir = os.path.join (original_dir,  '%s.dir' % program_name)
99 keep_temp_dir_p = 0
100
101 ## FIXME
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")
106
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")),
112         ('', 'k', 'keep',
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")),
129         ]
130
131 # other globals
132 preview_p = 0
133 lilypond_error_p = 0
134 preview_resolution = 90
135 pseudo_filter_p = 0
136
137
138 # Pdftex support
139 pdftex_p = 0
140 latex_cmd = 'latex'
141 tex_extension = '.tex'
142
143
144 layout_fields = ['dedication', 'title', 'subtitle', 'subsubtitle',
145           'footer', 'head', 'composer', 'arranger', 'instrument',
146           'opus', 'piece', 'metre', 'meter', 'poet', 'texttranslator']
147
148
149 # init to empty; values here take precedence over values in the file
150
151 ## TODO: change name.
152 extra_init = {
153         'language' : [],
154         'latexheaders' : [],
155         'latexpackages' :  ['geometry'],
156         'latexoptions' : [],
157         'papersize' : [],
158         'pagenumber' : [1],
159         'textheight' : [], 
160         'linewidth' : [],
161         'orientation' : [],
162         'unit' : ['pt'],
163 }
164
165 extra_fields = extra_init.keys ()
166 fields = layout_fields + extra_fields
167
168 include_path = ['.']
169 lily_p = 1
170 paper_p = 1
171
172 output_name = ''
173
174 # Output formats that ly2dvi should create
175 targets = ['DVI', 'LATEX', 'MIDI', 'TEX']
176
177 track_dependencies_p = 0
178 dependency_files = []
179
180 environment = {}
181
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',
187 }
188
189 def setup_environment ():
190         global environment
191
192         kpse = ly.read_pipe ('kpsexpand \$TEXMF')
193         texmf = re.sub ('[ \t\n]+$','', kpse)
194         type1_paths = ly.read_pipe ('kpsewhich -expand-path=\$T1FONTS')
195         
196         environment = {
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',
204                 }
205         
206         # $TEXMF is special, previous value is already taken care of
207         if os.environ.has_key ('TEXMF'):
208                 del os.environ['TEXMF']
209  
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
215
216         for key in non_path_environment.keys ():
217                 val = non_path_environment[key]
218                 os.environ[key] = val
219
220 #what a name.
221 def set_setting (dict, key, val):
222         try:
223                 val = string.atoi (val)
224         except ValueError:
225                 #ly.warning (_ ("invalid value: %s") % `val`)
226                 pass
227
228         if type(val) == type ('hoi'):
229                 try:
230                         val = string.atof (val)
231                 except ValueError:
232                         #ly.warning (_ ("invalid value: %s") % `val`)
233                         pass
234
235         try:
236                 dict[key].append (val)
237         except KeyError:
238                 ly.warning (_ ("no such setting: `%s'") % `key`)
239                 dict[key] = [val]
240
241
242 def print_environment ():
243         for (k,v) in os.environ.items ():
244                 sys.stderr.write ("%s=\"%s\"\n" % (k,v)) 
245
246 def run_lilypond (files, dep_prefix):
247
248         opts = ''
249         opts = opts + ' ' + string.join (map (lambda x : '-I ' + x,
250                                               include_path))
251         if pseudo_filter_p:
252                 opts = opts + ' --output=lelie'
253         if paper_p:
254                 opts = opts + ' ' + string.join (map (lambda x : '-H ' + x,
255                                                       fields))
256         else:
257                 opts = opts + ' --no-paper'
258
259         if pdftex_p:
260                 opts = opts + ' -f pdftex'              
261
262         if track_dependencies_p:
263                 opts = opts + " --dependencies"
264                 if dep_prefix:
265                         opts = opts + ' --dep-prefix=%s' % dep_prefix
266
267         fs = string.join (files)
268
269         global verbose_p
270         if verbose_p:
271                 opts = opts + ' --verbose'
272                 print_environment ()
273
274         cmd = 'lilypond %s %s ' % (opts, fs)
275         
276         # We unset verbose, because we always want to see lily's
277         # progess on stderr
278         save_verbose = verbose_p
279         status = ly.system (cmd, ignore_error = 1)
280         verbose_p = save_verbose
281
282         signal = 0x0f & status
283         exit_status = status >> 8
284
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"))
290                 ly.exit (status)
291                         
292         if status:
293                 sys.stderr.write ('\n')
294                 if len (files) == 1:
295                         ly.error (_ ("LilyPond failed on input file %s (exit status %d)") % (files[0], exit_status))
296                         ly.exit (status)
297                 else:
298                         ly.error (_ ("LilyPond failed on an input file (exit status %d)") % exit_status)
299                         ly.error (_ ("Continuing..."))
300                         global lilypond_error_p
301                         lilypond_error_p = 1
302                 
303
304 def analyse_lilypond_output (filename, extra):
305         
306         # urg
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 ()
312
313         # search only the first 10k
314         s = s[:10240]
315         for x in extra_fields:
316                 m = re.search (r'\\def\\lilypondpaper%s{([^}]*)}'%x, s)
317                 if m:
318                         set_setting (extra, x, m.group (1))
319         ly.progress ('\n')
320
321 def find_tex_files_for_base (base, extra):
322
323         '''
324         Find the \header fields dumped from BASE.
325         '''
326         
327         headerfiles = {}
328         for f in layout_fields:
329                 if os.path.exists (base + '.' + f):
330                         headerfiles[f] = base+'.'+f
331
332         if os.path.exists (base  +'.dep'):
333                 dependency_files.append (base + '.dep')
334
335         for f in extra_fields:
336                 if os.path.exists (base + '.' + f):
337                         extra[f].append (open (base + '.' + f).read ())
338         
339         return (base+tex_extension,headerfiles)
340          
341
342 def find_tex_files (files, extra):
343         '''
344         Find all .tex files whose prefixes start with some name in FILES. 
345
346         '''
347         
348         tfiles = []
349         
350         for f in files:
351                 x = 0
352                 while 1:
353                         fname = os.path.basename (f)
354                         fname = ly.strip_extension (fname, '.ly')
355                         if x:
356                                 fname = fname + '-%d' % x
357
358                         if os.path.exists (fname + tex_extension):
359                                 tfiles.append (find_tex_files_for_base (fname, extra))
360                                 analyse_lilypond_output (fname, extra)
361                         else:
362                                 break
363
364                         x = x + 1
365         if not x:
366                 fstr = string.join (files, ', ')
367                 ly.warning (_ ("no LilyPond output found for `%s'") % fstr)
368         return tfiles
369
370 def one_latex_definition (defn, first):
371         s = '\n'
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)
376                 else:
377                         s = s + r'''\let\lilypond%s\relax''' % k
378                 s = s + '\n'
379
380         if first:
381                 s = s + '\\def\\mustmakelilypondtitle{}\n'
382         else:
383                 s = s + '\\def\\mustmakelilypondpiecetitle{}\n'
384                 
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.
386         return s
387
388
389 ly_paper_to_latexpaper =  {
390         'a4' : 'a4paper',
391         'letter' : 'letterpaper', 
392         'a3' : 'a3paper'
393 }
394
395 #TODO: should set textheight (enlarge) depending on papersize. 
396 def global_latex_preamble (extra):
397         '''construct preamble from EXTRA,'''
398         s = ""
399         s = s + '% generation tag\n'
400
401         options = ''
402
403
404         if extra['papersize']:
405                 try:
406                         options = ly_paper_to_latexpaper[extra['papersize'][0]]
407                 except KeyError:
408                         ly.warning (_ ("invalid value: `%s'") % `extra['papersize'][0]`)
409                         pass
410
411         if extra['latexoptions']:
412                 options = options + ',' + extra['latexoptions'][-1]
413
414         s = s + '\\documentclass[%s]{article}\n' % options
415
416         if extra['language']:
417                 s = s + r'\usepackage[%s]{babel}' % extra['language'][-1] + '\n'
418
419
420         s = s + '\\usepackage{%s}\n' \
421                 % string.join (extra['latexpackages'], ',')
422
423         if extra['latexheaders']:
424                 s = s + '\\include{%s}\n' \
425                         % string.join (extra['latexheaders'], '}\n\\include{')
426
427         unit = extra['unit'][-1]
428
429         textheight = ''
430         if extra['textheight']:
431                 textheight = ',textheight=%f%s' % (extra['textheight'][0], unit)
432
433         orientation = 'portrait'
434         if extra['orientation']:
435                 orientation = extra['orientation'][0]
436
437         # set sane geometry width (a4-width) for linewidth = -1.
438         maxlw = max (extra['linewidth'] + [-1])
439         if maxlw < 0:
440                 # who the hell is 597 ?
441                 linewidth = '597pt'
442         else:
443                 linewidth = '%d%s' % (maxlw, unit)
444         s = s + '\geometry{width=%s%s,headheight=2mm,footskip=2mm,%s}\n' % (linewidth, textheight, orientation)
445
446         if extra['latexoptions']:
447                 s = s + '\geometry{twosideshift=4mm}\n'
448
449         s = s + r'''
450 \usepackage[latin1]{inputenc}
451 \input{titledefs}
452 '''
453         
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'
457         else:
458                 s = s + '\\pagestyle{empty}\n'
459
460
461         return s
462
463         
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.  '''
467
468         
469         s = global_latex_preamble (extra) + '\\begin{document}\n'
470         s = s + '\\parindent 0pt\n'
471         s = s + '\\thispagestyle{firstpage}\n'
472
473         first = 1
474         for t in tfiles:
475                 s = s + one_latex_definition (t, first)
476                 first = 0
477
478
479         s = s + '\\thispagestyle{lastpage}\n'
480         s = s + '\\end{document}'
481
482         return s
483
484 def run_latex (files, outbase, extra):
485
486         '''Construct latex file, for FILES and EXTRA, dump it into
487 OUTBASE.latex. Run LaTeX on it.
488
489 RETURN VALUE
490
491 None
492         '''
493
494         latex_fn = outbase + '.latex'
495         
496         wfs = find_tex_files (files, extra)
497         s = global_latex_definition (wfs, extra)
498
499         f = open (latex_fn, 'w')
500         f.write (s)
501         f.close ()
502
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
507
508         if exit_status:
509
510                 logstr = ''
511                 try:
512                         logstr = open (outbase + '.log').read ()
513                         m = re.search ("\n!", logstr)
514                         start = m.start (0)
515                         logstr = logstr[start:start+200]
516                 except:
517                         pass
518                         
519                 ly.error (_ ("LaTeX failed on the output file."))
520                 ly.error (_ ("The error log is as follows:"))
521                 sys.stderr.write (logstr + '\n')
522                 ly.exit (1)
523         
524         if preview_p:
525                 # make a preview by rendering only the 1st line.
526                 preview_fn = outbase + '.preview.tex'
527                 f = open (preview_fn, 'w')
528                 f.write (r'''
529 %s
530 \input lilyponddefs
531 \pagestyle{empty}
532 \begin{document}
533 \def\interscoreline{\endinput}
534 \input %s
535 \end{document}
536 ''' % (global_latex_preamble (extra), outbase))
537
538                 f.close()
539                 cmd = '%s \\\\nonstopmode \\\\input %s' % (latex_cmd, preview_fn)
540                 ly.system (cmd)
541         
542
543 def run_dvips (outbase, extra):
544
545
546         '''Run dvips using the correct options taken from EXTRA,
547 leaving a PS file in OUTBASE.ps
548
549 RETURN VALUE
550
551 None.
552 '''
553         opts = ''
554         if extra['papersize']:
555                 opts = opts + ' -t%s' % extra['papersize'][0]
556
557         if extra['orientation'] and extra['orientation'][0] == 'landscape':
558                 opts = opts + ' -tlandscape'
559
560         if 'PDF' in targets:
561                 opts = opts + ' -Ppdf -G0 -u lilypond.map'
562                 
563         cmd = 'dvips %s -o%s %s' % (opts, outbase + '.ps', outbase + '.dvi')
564         ly.system (cmd)
565
566         if preview_p:
567                 cmd = 'dvips -E -o%s %s' % ( outbase + '.preview.ps', outbase + '.preview.dvi')         
568                 ly.system (cmd)
569
570         if 'PDF' in targets:
571                 cmd = 'ps2pdf %s.ps %s.pdf' % (outbase , outbase)
572                 ly.system (cmd)
573                 
574 def get_bbox (filename):
575         # cut & paste 
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))
579
580         box = open (filename + '.bbox').read()
581         m = re.match ('^%%BoundingBox: ([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+)', box)
582         gr = []
583         if m:
584                 gr = map (string.atoi, m.groups ())
585         
586         return gr
587
588 #
589 # cut & paste from lilypond-book.
590 #
591 def make_preview (name, extra):
592         bbox = get_bbox (name + '.preview.ps')
593         margin = 0
594         fo = open (name + '.trans.eps' , 'w')
595         fo.write ('%d %d translate\n' % (-bbox[0]+margin, -bbox[1]+margin))
596         fo.close ()
597         
598         x = (2* margin + bbox[2] - bbox[0]) * preview_resolution / 72.
599         y = (2* margin + bbox[3] - bbox[1]) * preview_resolution / 72.
600
601         cmd = r'''gs -g%dx%d -sDEVICE=pgm  -dTextAlphaBits=4 -dGraphicsAlphaBits=4  -q -sOutputFile=- -r%d -dNOPAUSE %s %s -c quit | pnmtopng > %s'''
602         
603         cmd = cmd % (x, y, preview_resolution, name + '.trans.eps',
604                      name + '.preview.ps',name + '.png')
605         ly.system (cmd)
606
607         try:
608                 status = ly.system (cmd)
609         except:
610                 os.unlink (name + '.png')
611                 ly.error (_ ("Removing output file"))
612                 ly.exit (1)
613
614 def generate_dependency_file (depfile, outname):
615         df = open (depfile, 'w')
616         df.write (outname + ':' )
617         
618         for d in dependency_files:
619                 s = open (d).read ()
620                 s = re.sub ('#[^\n]*\n', '', s)
621                 s = re.sub (r'\\\n', ' ', s)
622                 m = re.search ('.*:(.*)\n', s)
623
624                 # ugh. Different targets?
625                 if m:
626                         df.write ( m.group (1)  + ' ' )
627
628         df.write ('\n')
629         df.close ();
630
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)
635
636 # Added as functionality to ly2dvi, because ly2dvi may well need to do this
637 # in future too.
638 PS = '%!PS-Adobe'
639 def find_pfa_fonts (name):
640         s = open (name).read ()
641         if s[:len (PS)] != PS:
642                 # no ps header?
643                 ly.error (_ ("not a PostScript file: `%s\'" % name))
644                 ly.exit (1)
645         here = 0
646         m = re.match ('.*?/(feta[-a-z0-9]+) +findfont', s[here:], re.DOTALL)
647         pfa = []
648         while m:
649                 here = m.end (1)
650                 pfa.append (m.group (1))
651                 m = re.match ('.*?/(feta[-a-z0-9]+) +findfont', s[here:], re.DOTALL)
652         return pfa
653
654         
655 (sh, long) = ly.getopt_args (option_definitions)
656 try:
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')
662         ly.help ()
663         ly.exit (2)
664         
665 for opt in options:
666         o = opt[0]
667         a = opt[1]
668
669         if 0:
670                 pass
671         elif o == '--help' or o == '-h':
672                 ly.help ()
673                 sys.exit (0)
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),
678                              fonts)
679                 print string.join (files, ' ')
680                 sys.exit (0)
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':
689                 keep_temp_dir_p = 1
690         elif o == '--no-lily':
691                 lily_p = 0
692         elif o == '--preview':
693                 preview_p = 1
694                 targets.append ('PNG')
695         elif o == '--preview-resolution':
696                 preview_resolution = string.atoi (a)
697         elif o == '--no-paper' or o == '-m':
698                 targets = ['MIDI'] 
699                 paper_p = 0
700         elif o == '--output' or o == '-o':
701                 output_name = a
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':
708                 verbose_p = 1
709         elif o == '--version' or o == '-v':
710                 ly.identify (sys.stdout)
711                 sys.exit (0)
712         elif o == '--pdftex':
713                 latex_cmd = 'pdflatex'
714                 targets.remove('DVI')
715                 targets.append('PDFTEX')
716                 pdftex_p = 1
717                 tex_extension = '.pdftex'
718         elif o == '--warranty' or o == '-w':
719                 status = os.system ('lilypond -w')
720                 if status:
721                         ly.warranty ()
722
723                 sys.exit (0)
724
725 # Don't convert input files to abspath, rather prepend '.' to include
726 # path.
727 include_path.insert (0, '.')
728
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:
731
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]))
735         
736 include_path = map (ly.abspath, include_path)
737
738 if files and (files[0] == '-' or output_name == '-'):
739         if len (files) == 1:
740                 pseudo_filter_p = 1
741                 output_name = 'lelie'
742                 if verbose_p:
743                         ly.progress (_ ("pseudo filter") + '\n')
744         else:
745                 ly.help ()
746                 ly.error (_ ("pseudo filter only for single input file"))
747                 ly.exit (2)
748                 
749 if not files:
750         ly.help ()
751         ly.error (_ ("no files specified on command line"))
752         ly.exit (2)
753
754 if 1:
755         ly.identify (sys.stderr)
756         original_output = output_name
757         
758         # Ugh, maybe make a setup () function
759         files = map (lambda x: ly.strip_extension (x, '.ly'), files)
760
761         # hmmm. Wish I'd 've written comments when I wrote this.
762         # now it looks complicated.
763         
764         (outdir, outbase) = ('','')
765         if not output_name:
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])
771         else:
772                 (outdir, outbase) = os.path.split (ly.abspath (output_name))
773
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)
777
778         for i in files[:] + [output_name]:
779                 if string.find (i, ' ') >= 0:
780                         ly.error (_ ("filename should not contain spaces: `%s'") %
781                                i)
782                         ly.exit (1)
783                         
784         if os.path.dirname (output_name) != '.':
785                 dep_prefix = os.path.dirname (output_name)
786         else:
787                 dep_prefix = 0
788
789         reldir = os.path.dirname (output_name)
790         if outdir != '.' and (track_dependencies_p or targets):
791                 ly.mkdir_p (outdir, 0777)
792
793         setup_environment ()
794         tmpdir = ly.setup_temp ()
795
796         # to be sure, add tmpdir *in front* of inclusion path.
797         #os.environ['TEXINPUTS'] =  tmpdir + ':' + os.environ['TEXINPUTS']
798         os.chdir (tmpdir)
799
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
803         #   * cleanout tempdir
804         if lily_p:
805                 try:
806                         run_lilypond (files, dep_prefix)
807                 except:
808                         # TODO: friendly message about LilyPond setup/failing?
809                         #
810                         # TODO: lilypond should fail with different
811                         # error codes for:
812                         #   - guile setup/startup failure
813                         #   - font setup failure
814                         #   - init.ly setup failure
815                         #   - parse error in .ly
816                         #   - unexpected: assert/core dump
817                         targets = []
818                         if verbose_p:
819                                 traceback.print_exc ()
820
821         # Our LilyPond pseudo filter always outputs to 'lelie'
822         # have subsequent stages and use 'lelie' output.
823         if pseudo_filter_p:
824                 files[0] = 'lelie'
825                 
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')
830
831         if 'DVI' in targets:
832                 try:
833                         run_latex (files, outbase, extra_init)
834                         # unless: add --tex, or --latex?
835                         targets.remove ('TEX')
836                         targets.remove('LATEX')
837                 except:
838                         # TODO: friendly message about TeX/LaTeX setup,
839                         # trying to run tex/latex by hand
840                         if 'DVI' in targets:
841                                 targets.remove ('DVI')
842                         if 'PS' in targets:
843                                 targets.remove ('PS')
844                         if verbose_p:
845                                 traceback.print_exc ()
846
847         if 'PS' in targets:
848                 try:
849                         run_dvips (outbase, extra_init)
850                 except: 
851                         if 'PS' in targets:
852                                 targets.remove ('PS')
853                         if verbose_p:
854                                 traceback.print_exc ()
855
856         if 'PNG' in  targets:
857                 make_preview (outbase, extra_init)
858
859         if 'PDFTEX' in targets:
860                 try:
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')
868                 except:
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')
873                         if 'PDF' in targets:
874                                 targets.remove ('PDF')
875                         if 'PS' in targets:
876                                 targets.remove ('PS')
877                         if verbose_p:
878                                 traceback.print_exc ()
879
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'...") %
886                                   depfile)
887                         ly.progress ('\n')
888
889         if pseudo_filter_p:
890                 main_target = 0
891                 for i in 'PDF', 'PS', 'PNG', 'DVI', 'LATEX':
892                         if i in targets:
893                                 main_target = i
894                                 break
895
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 ())
900                 elif verbose_p:
901                         ly.warning (_ ("can't find file: `%s'") % outname)
902                 targets = []
903                 ly.progress ('\n')
904                 
905         # Hmm, if this were a function, we could call it the except: clauses
906         for i in targets:
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)
911                 if reldir != '.':
912                         outname = os.path.join (reldir, outname)
913                 if os.path.isfile (abs):
914                         ly.progress (_ ("%s output to `%s'...") % (i, outname))
915                         ly.progress ('\n')
916                 elif verbose_p:
917                         ly.warning (_ ("can't find file: `%s'") % outname)
918                         
919         os.chdir (original_dir)
920         ly.cleanup_temp ()
921
922         sys.exit (lilypond_error_p)