3 # Run lilypond, latex, dvips.
5 # This is the third incarnation of ly2dvi.
7 # Earlier incarnations of ly2dvi were written by
8 # Jeffrey B. Reed<daboys@austin.rr.com> (Python version)
9 # Jan Arne Fagertun <Jan.A.Fagertun@@energy.sintef.no> (Bourne shell script)
13 # Note: gettext work best if we use ' for docstrings and "
14 # for gettextable strings.
15 # --> DO NOT USE """ for docstrings.
20 * figure out which set of command line options should make ly2dvi:
23 na: create latex only?
24 na: create tex and latex
25 default: create dvi only
26 na: create tex, latex and dvi
32 for foo.ly, rename ly2dvi.dir to out-ly2dvi, foo.ly2dvi, foo.dir ?
34 * move versatile taglines,
37 beginfooter=\mutopiaPD
38 endfooter=\tagline -> 'lily was here <version>'
41 lilytagline (->lily was here), usertagline, copyright etc.
43 * head/header tagline/endfooter
45 * dvi from lilypond .tex output? This is hairy, because we create dvi
46 from lilypond .tex *and* header output.
48 * multiple \score blocks?
66 ################################################################
67 # lilylib.py -- options and stuff
69 # source file of the GNU LilyPond music typesetter
71 # Handle bug in Python 1.6-2.1
73 # there are recursion limits for some patterns in Python 1.6 til 2.1.
74 # fix this by importing pre instead. Fix by Mats.
76 # todo: should check Python version first.
84 # Attempt to fix problems with limited stack size set by Python!
85 # Sets unlimited stack size. Note that the resource module only
86 # is available on UNIX.
89 resource
.setrlimit (resource
.RLIMIT_STACK
, (-1, -1))
93 localedir
= '@localedir@'
96 gettext
.bindtextdomain ('lilypond', localedir
)
97 gettext
.textdomain ('lilypond')
103 program_version
= '@TOPLEVEL_VERSION@'
104 if program_version
== '@' + 'TOPLEVEL_VERSION' + '@':
105 program_version
= '1.5.54'
108 sys
.stdout
.write ('%s (GNU LilyPond) %s\n' % (program_name
, program_version
))
112 sys
.stdout
.write ('\n')
113 sys
.stdout
.write (_ ('Copyright (c) %s by' % ' 2001--2003'))
114 sys
.stdout
.write ('\n')
115 sys
.stdout
.write (' Han-Wen Nienhuys')
116 sys
.stdout
.write (' Jan Nieuwenhuizen')
117 sys
.stdout
.write ('\n\n')
118 sys
.stdout
.write ('\n')
119 sys
.stdout
.write (_ ("Distributed under terms of the GNU General Public License. It comes with NO WARRANTY."))
120 sys
.stdout
.write ('\n')
123 errorport
.write (s
+ '\n')
126 progress (_ ("warning: ") + s
)
128 def user_error (s
, e
=1):
129 errorport
.write (program_name
+ ":" + _ ("error: ") + s
+ '\n')
134 '''Report the error S.
136 If verbose is set, exit by raising an exception. Otherwise,
139 Please do not abuse by trying to catch this error. If you do
140 not want a stack trace, write to the output directly.
148 progress (_ ("error: ") + s
)
150 raise _ ("Exiting ... ")
154 def getopt_args (opts
):
155 '''Construct arguments (LONG, SHORT) for getopt from list of options.'''
170 def option_help_str (o
):
171 '''Transform one option description (4-tuple ) into neatly formatted string'''
189 return ' ' + sh
+ sep
+ long + arg
192 def options_help_str (opts
):
193 '''Convert a list of options into a neatly formatted string'''
199 s
= option_help_str (o
)
200 strs
.append ((s
, o
[3]))
206 str = str + '%s%s%s\n' % (s
[0], ' ' * (w
- len(s
[0]) + 3), s
[1])
210 ls
= [(_ ("Usage: %s [OPTION]... FILE") % program_name
),
216 (options_help_str (option_definitions
)),
218 (_ ("Report bugs to %s") % 'bug-lilypond@gnu.org'),
220 map (sys
.stdout
.write
, ls
)
224 Create a temporary directory, and return its name.
227 if not keep_temp_dir_p
:
228 temp_dir
= tempfile
.mktemp (program_name
)
230 os
.mkdir (temp_dir
, 0777)
237 def system (cmd
, ignore_error
= 0, quiet
=0):
238 """Run CMD. If IGNORE_ERROR is set, don't complain when CMD returns non zero.
246 progress (_ ("Invoking `%s\'") % cmd
)
250 name
= re
.match ('[ \t]*([^ \t]*)', cmd
).group (1)
251 msg
= name
+ ': ' + _ ("command exited with value %d") % st
253 if not quiet
or verbose_p
:
254 warning (msg
+ ' ' + _ ("(ignored)") + ' ')
262 if not keep_temp_dir_p
:
264 progress (_ ("Cleaning %s...") % temp_dir
)
265 shutil
.rmtree (temp_dir
)
268 def strip_extension (f
, ext
):
269 (p
, e
) = os
.path
.splitext (f
)
275 def cp_to_dir (pattern
, dir):
276 "Copy files matching re PATTERN from cwd to DIR"
277 # Duh. Python style portable: cp *.EXT OUTDIR
278 # system ('cp *.%s %s' % (ext, outdir), 1)
279 files
= filter (lambda x
, p
=pattern
: re
.match (p
, x
), os
.listdir ('.'))
280 map (lambda x
, d
=dir: shutil
.copy2 (x
, os
.path
.join (d
, x
)), files
)
283 # Python < 1.5.2 compatibility
285 # On most platforms, this is equivalent to
286 #`normpath(join(os.getcwd()), PATH)'. *Added in Python version 1.5.2*
287 if os
.path
.__dict
__.has_key ('abspath'):
288 abspath
= os
.path
.abspath
291 return os
.path
.normpath (os
.path
.join (os
.getcwd (), path
))
293 if os
.__dict
__.has_key ('makedirs'):
294 makedirs
= os
.makedirs
296 def makedirs (dir, mode
=0777):
297 system ('mkdir -p %s' % dir)
300 def mkdir_p (dir, mode
=0777):
301 if not os
.path
.isdir (dir):
305 # if set, LILYPONDPREFIX must take prevalence
306 # if datadir is not set, we're doing a build and LILYPONDPREFIX
307 datadir
= '@local_lilypond_datadir@'
309 if os
.environ
.has_key ('LILYPONDPREFIX') :
310 datadir
= os
.environ
['LILYPONDPREFIX']
312 datadir
= '@local_lilypond_datadir@'
315 while datadir
[-1] == os
.sep
:
316 datadir
= datadir
[:-1]
318 sys
.path
.insert (0, os
.path
.join (datadir
, 'python'))
320 ################################################################
324 program_name
= 'ly2dvi'
326 original_dir
= os
.getcwd ()
327 temp_dir
= os
.path
.join (original_dir
, '%s.dir' % program_name
)
328 errorport
= sys
.stderr
333 preview_resolution
= 90
336 tex_extension
= '.tex'
339 help_summary
= _ ("Run LilyPond using LaTeX for titling")
341 option_definitions
= [
342 ('', 'd', 'dependencies',
343 _ ("write Makefile dependencies for every input file")),
344 ('', 'h', 'help', _ ("this help")),
345 (_ ("DIR"), 'I', 'include', _ ("add DIR to LilyPond's search path")),
347 _ ("keep all output, output to directory %s.dir") % program_name
),
348 ('', '', 'no-lily', _ ("don't run LilyPond")),
349 ('', 'm', 'no-paper', _ ("produce MIDI output only")),
350 (_ ("FILE"), 'o', 'output', _ ("write ouput to FILE")),
351 (_ ("FILE"), 'f', 'find-pfa', _ ("find pfa fonts used in FILE")),
352 (_ ('RES'), '', 'preview-resolution',
353 _ ("set the resolution of the preview to RES")),
354 ('', 'P', 'postscript', _ ("generate PostScript output")),
355 ('', 'p', 'pdf', _ ("generate PDF output")),
356 ('', '', 'pdftex', _ ("use pdflatex to generate a PDF output")),
357 # FIXME: preview, picture; to indicate creation of a PNG?
358 ('', '', 'preview', _ ("make a picture of the first system")),
359 (_ ("KEY=VAL"), 's', 'set', _ ("change global setting KEY to VAL")),
360 ('', 'V', 'verbose', _ ("verbose")),
361 ('', 'v', 'version', _ ("print version number")),
362 ('', 'w', 'warranty', _ ("show warranty and copyright")),
365 layout_fields
= ['dedication', 'title', 'subtitle', 'subsubtitle',
366 'footer', 'head', 'composer', 'arranger', 'instrument',
367 'opus', 'piece', 'metre', 'meter', 'poet', 'texttranslator']
370 # init to empty; values here take precedence over values in the file
372 ## TODO: change name.
376 'latexpackages' : ['geometry'],
379 'latexoptions' : ['compat2'],
388 extra_fields
= extra_init
.keys ()
389 fields
= layout_fields
+ extra_fields
397 # Output formats that ly2dvi should create
398 targets
= ['DVI', 'LATEX', 'MIDI', 'TEX']
400 track_dependencies_p
= 0
401 dependency_files
= []
405 # tex needs lots of memory, more than it gets by default on Debian
406 non_path_environment
= {
407 'extra_mem_top' : '1000000',
408 'extra_mem_bottom' : '1000000',
409 'pool_size' : '250000',
412 def command_name (cmd
):
413 return re
.match ('^[ \t]*([^ \t]*)', cmd
).group (1)
415 def error_log (name
):
416 return os
.path
.join (__main__
.temp_dir
, '%s.errorlog' % name
)
418 def read_pipe (cmd
, mode
= 'r'):
421 progress (_ ("Opening pipe `%s\'") % cmd
)
422 redirect
= ' 2>%s' % error_log (command_name (cmd
))
423 pipe
= os
.popen (cmd
+ redirect
, mode
)
424 output
= pipe
.read ()
425 status
= pipe
.close ()
426 # successful pipe close returns 'None'
429 signal
= 0x0f & status
430 exit_status
= status
>> 8
433 sys
.stderr
.write (_ ("`%s\' failed (%d)") % (cmd
, exit_status
))
435 sys
.stderr
.write (_ ("The error log is as follows:"))
436 sys
.stderr
.write (open (error_log (command_name (cmd
)).read ()))
438 if __main__
.verbose_p
:
442 def setup_environment ():
445 kpse
= read_pipe ('kpsexpand \$TEXMF')
446 texmf
= re
.sub ('[ \t\n]+$','', kpse
)
447 type1_paths
= read_pipe ('kpsewhich -expand-path=\$T1FONTS')
450 # TODO: * prevent multiple addition.
451 # * clean TEXINPUTS, MFINPUTS, TFMFONTS,
452 # as these take prevalence over $TEXMF
453 # and thus may break tex run?
454 'TEXMF' : "{%s,%s}" % (datadir
, texmf
) ,
455 'GS_FONTPATH' : type1_paths
,
456 'GS_LIB' : datadir
+ '/ps',
459 # $TEXMF is special, previous value is already taken care of
460 if os
.environ
.has_key ('TEXMF'):
461 del os
.environ
['TEXMF']
463 for key
in environment
.keys ():
464 val
= environment
[key
]
465 if os
.environ
.has_key (key
):
466 val
= os
.environ
[key
] + os
.pathsep
+ val
467 os
.environ
[key
] = val
469 for key
in non_path_environment
.keys ():
470 val
= non_path_environment
[key
]
471 os
.environ
[key
] = val
474 def set_setting (dict, key
, val
):
476 val
= string
.atoi (val
)
478 #warning (_ ("invalid value: %s") % `val`)
481 if type(val
) == type ('hoi'):
483 val
= string
.atof (val
)
485 #warning (_ ("invalid value: %s") % `val`)
489 dict[key
].append (val
)
491 warning (_ ("no such setting: `%s'") % `key`
)
495 def print_environment ():
496 for (k
,v
) in os
.environ
.items ():
497 sys
.stderr
.write ("%s=\"%s\"\n" % (k
,v
))
499 def quiet_system (cmd
, name
, ignore_error
= 0):
501 progress ( _("Running %s...") % name
)
502 cmd
= cmd
+ ' 1> /dev/null 2> /dev/null'
503 elif pseudo_filter_p
:
504 cmd
= cmd
+ ' 1> /dev/null'
506 return system (cmd
, ignore_error
, quiet
= 1)
509 def run_lilypond (files
, dep_prefix
):
512 opts
= opts
+ ' ' + string
.join (map (lambda x
: '-I ' + x
,
515 opts
= opts
+ ' --output=lelie'
517 opts
= opts
+ ' ' + string
.join (map (lambda x
: '-H ' + x
,
520 opts
= opts
+ ' --no-paper'
523 opts
= opts
+ ' -f pdftex'
525 if track_dependencies_p
:
526 opts
= opts
+ " --dependencies"
528 opts
= opts
+ ' --dep-prefix=%s' % dep_prefix
530 fs
= string
.join (files
)
533 # cmd = cmd + ' 1> /dev/null 2> /dev/null'
534 progress ( _("Running %s...") % 'LilyPond')
536 opts
= opts
+ ' --verbose'
538 # for better debugging!
541 cmd
= 'lilypond %s %s ' % (opts
, fs
)
543 progress ("Invoking `%s'"% cmd
)
544 status
= os
.system (cmd
)
546 signal
= 0x0f & status
547 exit_status
= status
>> 8
549 # 2 == user interrupt.
550 if signal
and signal
!= 2:
551 error ("\n\n" + _ ("LilyPond crashed (signal %d).") % signal \
552 + _ ("Please submit a bug report to bug-lilypond@gnu.org") + "\n")
555 sys
.stderr
.write ( "\n" \
556 + _ ("LilyPond failed on an input file (exit status %d).") % exit_status
+ "\n")
557 sys
.stderr
.write (_("Trying to salvage the rest.") +'\n\n')
559 global lilypond_error_p
563 def analyse_lilypond_output (filename
, extra
):
566 '''Grep FILENAME for interesting stuff, and
567 put relevant info into EXTRA.'''
568 filename
= filename
+tex_extension
569 progress (_ ("Analyzing %s...") % filename
)
570 s
= open (filename
).read ()
572 # search only the first 10k
574 for x
in extra_fields
:
575 m
= re
.search (r
'\\def\\lilypondpaper%s{([^}]*)}'%x, s
)
577 set_setting (extra
, x
, m
.group (1))
579 def find_tex_files_for_base (base
, extra
):
582 Find the \header fields dumped from BASE.
586 for f
in layout_fields
:
587 if os
.path
.exists (base
+ '.' + f
):
588 headerfiles
[f
] = base
+'.'+f
590 if os
.path
.exists (base
+'.dep'):
591 dependency_files
.append (base
+ '.dep')
593 for f
in extra_fields
:
594 if os
.path
.exists (base
+ '.' + f
):
595 extra
[f
].append (open (base
+ '.' + f
).read ())
597 return (base
+tex_extension
,headerfiles
)
600 def find_tex_files (files
, extra
):
602 Find all .tex files whose prefixes start with some name in FILES.
611 fname
= os
.path
.basename (f
)
612 fname
= strip_extension (fname
, '.ly')
614 fname
= fname
+ '-%d' % x
616 if os
.path
.exists (fname
+ tex_extension
):
617 tfiles
.append (find_tex_files_for_base (fname
, extra
))
618 analyse_lilypond_output (fname
, extra
)
624 fstr
= string
.join (files
, ', ')
625 warning (_ ("no LilyPond output found for `%s'") % fstr
)
628 def one_latex_definition (defn
, first
):
630 for (k
,v
) in defn
[1].items ():
631 val
= open (v
).read ()
632 if (string
.strip (val
)):
633 s
= s
+ r
'''\def\lilypond%s{%s}''' % (k
, val
)
635 s
= s
+ r
'''\let\lilypond%s\relax''' % k
639 s
= s
+ '\\def\\mustmakelilypondtitle{}\n'
641 s
= s
+ '\\def\\mustmakelilypondpiecetitle{}\n'
643 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.
647 ly_paper_to_latexpaper
= {
649 'letter' : 'letterpaper',
653 #TODO: should set textheight (enlarge) depending on papersize.
654 def global_latex_preamble (extra
):
655 '''construct preamble from EXTRA,'''
657 s
= s
+ '% generation tag\n'
662 if extra
['papersize']:
664 options
= ly_paper_to_latexpaper
[extra
['papersize'][0]]
666 warning (_ ("invalid value: `%s'") % `extra
['papersize'][0]`
)
669 if extra
['latexoptions']:
670 options
= options
+ ',' + extra
['latexoptions'][-1]
672 s
= s
+ '\\documentclass[%s]{article}\n' % options
674 if extra
['language']:
675 s
= s
+ r
'\usepackage[%s]{babel}' % extra
['language'][-1] + '\n'
678 s
= s
+ '\\usepackage{%s}\n' \
679 % string
.join (extra
['latexpackages'], ',')
681 if extra
['latexheaders']:
682 s
= s
+ '\\include{%s}\n' \
683 % string
.join (extra
['latexheaders'], '}\n\\include{')
685 unit
= extra
['unit'][-1]
688 if extra
['textheight']:
689 textheight
= ',textheight=%f%s' % (extra
['textheight'][0], unit
)
691 orientation
= 'portrait'
692 if extra
['orientation']:
693 orientation
= extra
['orientation'][0]
695 # set sane geometry width (a4-width) for linewidth = -1.
696 maxlw
= max (extra
['linewidth'] + [-1])
698 # who the hell is 597 ?
701 linewidth
= '%d%s' % (maxlw
, unit
)
702 s
= s
+ '\geometry{width=%s%s,headheight=2mm,footskip=2mm,%s}\n' % (linewidth
, textheight
, orientation
)
704 if extra
['latexoptions']:
705 s
= s
+ '\geometry{twosideshift=4mm}\n'
708 \usepackage[latin1]{inputenc}
712 if extra
['pagenumber'] and extra
['pagenumber'][-1] and extra
['pagenumber'][-1] != 'no':
713 s
= s
+ '\setcounter{page}{%d}\n' % (extra
['pagenumber'][-1])
714 s
= s
+ '\\pagestyle{plain}\n'
716 s
= s
+ '\\pagestyle{empty}\n'
722 def global_latex_definition (tfiles
, extra
):
723 '''construct preamble from EXTRA, dump Latex stuff for each
724 lily output file in TFILES after that, and return the Latex file constructed. '''
727 s
= global_latex_preamble (extra
) + '\\begin{document}\n'
728 s
= s
+ '\\parindent 0pt\n'
729 s
= s
+ '\\thispagestyle{firstpage}\n'
733 s
= s
+ one_latex_definition (t
, first
)
737 s
= s
+ '\\thispagestyle{lastpage}\n'
738 s
= s
+ '\\end{document}'
742 def run_latex (files
, outbase
, extra
):
744 """Construct latex file, for FILES and EXTRA, dump it into
745 OUTBASE.latex. Run LaTeX on it.
752 latex_fn
= outbase
+ '.latex'
754 wfs
= find_tex_files (files
, extra
)
755 s
= global_latex_definition (wfs
, extra
)
757 f
= open (latex_fn
, 'w')
761 cmd
= latex_cmd
+ ' \\\\nonstopmode \\\\input %s' % latex_fn
762 status
= quiet_system (cmd
, 'LaTeX', ignore_error
= 1)
764 signal
= 0xf & status
765 exit_stat
= status
>> 8
768 logstr
= open (outbase
+ '.log').read()
769 m
= re
.search ("\n!", logstr
)
771 logstr
= logstr
[start
:start
+200]
773 user_error (_ ("LaTeX failed on the output file."), 0)
774 sys
.stderr
.write ("\n")
775 user_error (_ ("The error log is as follows:"), 0)
776 sys
.stderr
.write ("\n")
777 sys
.stderr
.write (logstr
)
778 sys
.stderr
.write ("\n")
782 # make a preview by rendering only the 1st line.
783 preview_fn
= outbase
+ '.preview.tex'
784 f
= open (preview_fn
, 'w')
790 \def\interscoreline{\endinput}
793 ''' % (global_latex_preamble (extra
), outbase
))
796 cmd
= '%s \\\\nonstopmode \\\\input %s' % (latex_cmd
, preview_fn
)
797 quiet_system (cmd
, '%s for preview' % latex_cmd
)
800 def run_dvips (outbase
, extra
):
803 """Run dvips using the correct options taken from EXTRA,
804 leaving a PS file in OUTBASE.ps
811 if extra
['papersize']:
812 opts
= opts
+ ' -t%s' % extra
['papersize'][0]
814 if extra
['orientation'] and extra
['orientation'][0] == 'landscape':
815 opts
= opts
+ ' -tlandscape'
818 opts
= opts
+ ' -Ppdf -G0 -u +lilypond.map'
820 cmd
= 'dvips %s -o%s %s' % (opts
, outbase
+ '.ps', outbase
+ '.dvi')
821 quiet_system (cmd
, 'dvips')
824 cmd
= 'dvips -E -o%s %s' % ( outbase
+ '.preview.ps', outbase
+ '.preview.dvi')
825 quiet_system (cmd
, 'dvips for preview')
828 cmd
= 'ps2pdf %s.ps %s.pdf' % (outbase
, outbase
)
829 quiet_system (cmd
, 'ps2pdf')
831 def get_bbox (filename
):
833 system ('gs -sDEVICE=bbox -q -sOutputFile=- -dNOPAUSE %s -c quit > %s.bbox 2>&1 ' % (filename
, filename
))
835 box
= open (filename
+ '.bbox').read()
836 m
= re
.match ('^%%BoundingBox: ([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+)', box
)
839 gr
= map (string
.atoi
, m
.groups ())
844 # cut & paste from lilypond-book.
846 def make_preview (name
, extra
):
847 bbox
= get_bbox (name
+ '.preview.ps')
849 fo
= open (name
+ '.trans.eps' , 'w')
850 fo
.write ('%d %d translate\n' % (-bbox
[0]+margin
, -bbox
[1]+margin
))
853 x
= (2* margin
+ bbox
[2] - bbox
[0]) * preview_resolution
/ 72.
854 y
= (2* margin
+ bbox
[3] - bbox
[1]) * preview_resolution
/ 72.
856 cmd
= r
'''gs -g%dx%d -sDEVICE=pgm -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -q -sOutputFile=- -r%d -dNOPAUSE %s %s -c quit | pnmtopng > %s'''
858 cmd
= cmd
% (x
, y
, preview_resolution
, name
+ '.trans.eps', name
+ '.preview.ps',name
+ '.png')
859 quiet_system (cmd
, 'gs')
862 status
= system (cmd
)
864 os
.unlink (name
+ '.png')
865 error ("Removing output file")
869 def generate_dependency_file (depfile
, outname
):
870 df
= open (depfile
, 'w')
871 df
.write (outname
+ ':' )
873 for d
in dependency_files
:
875 s
= re
.sub ('#[^\n]*\n', '', s
)
876 s
= re
.sub (r
'\\\n', ' ', s
)
877 m
= re
.search ('.*:(.*)\n', s
)
879 # ugh. Different targets?
881 df
.write ( m
.group (1) + ' ' )
886 def find_file_in_path (path
, name
):
887 for d
in string
.split (path
, os
.pathsep
):
888 if name
in os
.listdir (d
):
889 return os
.path
.join (d
, name
)
891 # Added as functionality to ly2dvi, because ly2dvi may well need to do this
894 def find_pfa_fonts (name
):
895 s
= open (name
).read ()
896 if s
[:len (PS
)] != PS
:
898 user_error (_ ("not a PostScript file: `%s\'" % name
))
900 m
= re
.match ('.*?/(feta[-a-z0-9]+) +findfont', s
[here
:], re
.DOTALL
)
904 pfa
.append (m
.group (1))
905 m
= re
.match ('.*?/(feta[-a-z0-9]+) +findfont', s
[here
:], re
.DOTALL
)
909 (sh
, long) = getopt_args (option_definitions
)
911 (options
, files
) = getopt
.getopt(sys
.argv
[1:], sh
, long)
912 except getopt
.error
, s
:
913 errorport
.write ('\n')
914 user_error (_ ("getopt says: `%s\'" % s
), 0)
915 errorport
.write ('\n')
925 elif o
== '--help' or o
== '-h':
928 elif o
== '--find-pfa' or o
== '-f':
929 fonts
= map (lambda x
: x
+ '.pfa', find_pfa_fonts (a
))
930 files
= map (lambda x
:
931 find_file_in_path (os
.environ
['GS_FONTPATH'], x
),
933 print string
.join (files
, ' ')
935 elif o
== '--include' or o
== '-I':
936 include_path
.append (a
)
937 elif o
== '--postscript' or o
== '-P':
938 targets
.append ('PS')
939 elif o
== '--pdf' or o
== '-p':
940 targets
.append ('PS')
941 targets
.append ('PDF')
942 elif o
== '--keep' or o
== '-k':
944 elif o
== '--no-lily':
946 elif o
== '--preview':
948 targets
.append ('PNG')
949 elif o
== '--preview-resolution':
950 preview_resolution
= string
.atoi (a
)
951 elif o
== '--no-paper' or o
== '-m':
954 elif o
== '--output' or o
== '-o':
956 elif o
== '--set' or o
== '-s':
957 ss
= string
.split (a
, '=')
958 set_setting (extra_init
, ss
[0], ss
[1])
959 elif o
== '--dependencies' or o
== '-d':
960 track_dependencies_p
= 1
961 elif o
== '--verbose' or o
== '-V':
963 elif o
== '--version' or o
== '-v':
966 elif o
== '--pdftex':
967 latex_cmd
= 'pdflatex'
968 targets
.remove('DVI')
969 targets
.append('PDFTEX')
971 tex_extension
= '.pdftex'
972 elif o
== '--warranty' or o
== '-w':
973 status
= system ('lilypond -w', ignore_error
= 1)
979 # Don't convert input files to abspath, rather prepend '.' to include
981 include_path
.insert (0, '.')
983 # As a neat trick, add directory part of first input file
984 # to include path. That way you can do without the clumsy -I in:
986 # ly2dvi -I foe/bar/baz foo/bar/baz/baz.ly
987 if files
and files
[0] != '-' and os
.path
.dirname (files
[0]) != '.':
988 include_path
.append (os
.path
.dirname (files
[0]))
990 include_path
= map (abspath
, include_path
)
992 if files
and (files
[0] == '-' or output_name
== '-'):
995 output_name
= 'lelie'
997 progress (_ ("pseudo filter"))
1000 user_error (_ ("pseudo filter only for single input file"), 2)
1003 original_output
= output_name
1007 # Ugh, maybe make a setup () function
1008 files
= map (lambda x
: strip_extension (x
, '.ly'), files
)
1010 # hmmm. Wish I'd 've written comments when I wrote this.
1011 # now it looks complicated.
1013 (outdir
, outbase
) = ('','')
1015 outbase
= os
.path
.basename (files
[0])
1016 outdir
= abspath ('.')
1017 elif output_name
[-1] == os
.sep
:
1018 outdir
= abspath (output_name
)
1019 outbase
= os
.path
.basename (files
[0])
1021 (outdir
, outbase
) = os
.path
.split (abspath (output_name
))
1023 for i
in ('.dvi', '.latex', '.ly', '.ps', '.tex', '.pdftex'):
1024 output_name
= strip_extension (output_name
, i
)
1025 outbase
= strip_extension (outbase
, i
)
1027 for i
in files
[:] + [output_name
]:
1028 if string
.find (i
, ' ') >= 0:
1029 user_error (_ ("filename should not contain spaces: `%s'") % i
)
1031 if os
.path
.dirname (output_name
) != '.':
1032 dep_prefix
= os
.path
.dirname (output_name
)
1036 reldir
= os
.path
.dirname (output_name
)
1037 if outdir
!= '.' and (track_dependencies_p
or targets
):
1038 mkdir_p (outdir
, 0777)
1040 tmpdir
= setup_temp ()
1041 setup_environment ()
1043 # to be sure, add tmpdir *in front* of inclusion path.
1044 #os.environ['TEXINPUTS'] = tmpdir + ':' + os.environ['TEXINPUTS']
1049 run_lilypond (files
, dep_prefix
)
1051 # TODO: friendly message about LilyPond setup/failing?
1053 # TODO: lilypond should fail with different
1055 # - guile setup/startup failure
1056 # - font setup failure
1057 # - init.ly setup failure
1058 # - parse error in .ly
1059 # - unexpected: assert/core dump
1061 traceback
.print_exc ()
1063 # Our LilyPond pseudo filter always outputs to 'lelie'
1064 # have subsequent stages and use 'lelie' output.
1068 if 'PNG' in targets
and 'PS' not in targets
:
1069 targets
.append ('PS')
1070 if 'PS' in targets
and 'DVI' not in targets
:
1071 targets
.append('DVI')
1073 if 'DVI' in targets
:
1075 run_latex (files
, outbase
, extra_init
)
1076 # unless: add --tex, or --latex?
1077 targets
.remove ('TEX')
1078 targets
.remove('LATEX')
1080 # TODO: friendly message about TeX/LaTeX setup,
1081 # trying to run tex/latex by hand
1082 if 'DVI' in targets
:
1083 targets
.remove ('DVI')
1085 targets
.remove ('PS')
1086 traceback
.print_exc ()
1090 run_dvips (outbase
, extra_init
)
1093 targets
.remove ('PS')
1094 traceback
.print_exc ()
1096 if 'PNG' in targets
:
1097 make_preview (outbase
, extra_init
)
1099 if 'PDFTEX' in targets
:
1101 run_latex (files
, outbase
, extra_init
)
1102 # unless: add --tex, or --latex?
1103 targets
.remove ('TEX')
1104 targets
.remove ('LATEX')
1105 targets
.remove ('PDFTEX')
1106 if 'PDF' not in targets
:
1107 targets
.append('PDF')
1109 # TODO: friendly message about TeX/LaTeX setup,
1110 # trying to run tex/latex by hand
1111 if 'PDFTEX' in targets
:
1112 targets
.remove ('PDFTEX')
1113 if 'PDF' in targets
:
1114 targets
.remove ('PDF')
1116 targets
.remove ('PS')
1117 traceback
.print_exc ()
1120 # add DEP to targets?
1121 if track_dependencies_p
:
1122 depfile
= os
.path
.join (outdir
, outbase
+ '.dep')
1123 generate_dependency_file (depfile
, depfile
)
1124 if os
.path
.isfile (depfile
):
1125 progress (_ ("dependencies output to `%s'...") %
1130 for i
in 'PDF', 'PS', 'PNG', 'DVI', 'LATEX':
1135 outname
= outbase
+ '.' + string
.lower (main_target
)
1136 if os
.path
.isfile (outname
):
1137 sys
.stdout
.write (open (outname
).read ())
1139 warning (_ ("can't find file: `%s'") % outname
)
1142 # Hmm, if this were a function, we could call it the except: clauses
1144 ext
= string
.lower (i
)
1145 cp_to_dir ('.*\.%s$' % ext
, outdir
)
1146 outname
= outbase
+ '.' + string
.lower (i
)
1147 abs = os
.path
.join (outdir
, outname
)
1149 outname
= os
.path
.join (reldir
, outname
)
1150 if os
.path
.isfile (abs):
1151 progress (_ ("%s output to `%s'...") % (i
, outname
))
1153 warning (_ ("can't find file: `%s'") % outname
)
1155 os
.chdir (original_dir
)
1158 sys
.exit (lilypond_error_p
)
1161 user_error (_ ("no files specified on command line"), 2)