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?
69 # Handle bug in Python 1.6-2.1
71 # there are recursion limits for some patterns in Python 1.6 til 2.1.
72 # fix this by importing pre instead. Fix by Mats.
74 # todo: should check Python version first.
83 ################################################################
84 # lilylib.py -- options and stuff
86 # source file of the GNU LilyPond music typesetter
90 gettext.bindtextdomain ('lilypond', localedir)
91 gettext.textdomain ('lilypond')
97 program_version = '@TOPLEVEL_VERSION@'
98 if program_version == '@' + 'TOPLEVEL_VERSION' + '@':
99 program_version = '1.5.17'
102 sys.stdout.write ('%s (GNU LilyPond) %s\n' % (program_name, program_version))
106 sys.stdout.write ('\n')
107 sys.stdout.write (_ ('Copyright (c) %s by' % ' 2001--2002'))
108 sys.stdout.write ('\n')
109 sys.stdout.write (' Han-Wen Nienhuys')
110 sys.stdout.write (' Jan Nieuwenhuizen')
111 sys.stdout.write ('\n')
112 sys.stdout.write (_ (r'''
113 Distributed under terms of the GNU General Public License. It comes with
115 sys.stdout.write ('\n')
118 errorport.write (s + '\n')
121 progress (_ ("warning: ") + s)
123 def user_error (s, e=1):
124 errorport.write (program_name + ":" + _ ("error: ") + s + '\n')
130 '''Report the error S. Exit by raising an exception. Please
131 do not abuse by trying to catch this error. If you do not want
132 a stack trace, write to the output directly.
140 progress (_ ("error: ") + s)
141 raise _ ("Exiting ... ")
143 def getopt_args (opts):
144 '''Construct arguments (LONG, SHORT) for getopt from list of options.'''
159 def option_help_str (o):
160 '''Transform one option description (4-tuple ) into neatly formatted string'''
178 return ' ' + sh + sep + long + arg
181 def options_help_str (opts):
182 '''Convert a list of options into a neatly formatted string'''
188 s = option_help_str (o)
189 strs.append ((s, o[3]))
195 str = str + '%s%s%s\n' % (s[0], ' ' * (w - len(s[0]) + 3), s[1])
199 ls = [(_ ("Usage: %s [OPTION]... FILE") % program_name),
205 (options_help_str (option_definitions)),
207 (_ ("Report bugs to %s") % 'bug-lilypond@gnu.org'),
209 map (sys.stdout.write, ls)
213 Create a temporary directory, and return its name.
216 if not keep_temp_dir_p:
217 temp_dir = tempfile.mktemp (program_name)
219 os.mkdir (temp_dir, 0777)
226 def system (cmd, ignore_error = 0):
227 """Run CMD. If IGNORE_ERROR is set, don't complain when CMD returns non zero.
235 progress (_ ("Invoking `%s\'") % cmd)
238 name = re.match ('[ \t]*([^ \t]*)', cmd).group (1)
239 msg = name + ': ' + _ ("command exited with value %d") % st
241 warning (msg + ' ' + _ ("(ignored)") + ' ')
249 if not keep_temp_dir_p:
251 progress (_ ("Cleaning %s...") % temp_dir)
252 shutil.rmtree (temp_dir)
255 def strip_extension (f, ext):
256 (p, e) = os.path.splitext (f)
261 ################################################################
269 # if set, LILYPONDPREFIX must take prevalence
270 # if datadir is not set, we're doing a build and LILYPONDPREFIX
271 datadir = '@datadir@'
274 if os.environ.has_key ('LILYPONDPREFIX') :
275 datadir = os.environ['LILYPONDPREFIX']
277 datadir = '@datadir@'
280 while datadir[-1] == os.sep:
281 datadir= datadir[:-1]
283 program_name = 'ly2dvi'
285 original_dir = os.getcwd ()
286 temp_dir = os.path.join (original_dir, '%s.dir' % program_name)
287 errorport = sys.stderr
293 gettext.bindtextdomain ('lilypond', '@localedir@')
294 gettext.textdomain ('lilypond')
300 # Attempt to fix problems with limited stack size set by Python!
301 # Sets unlimited stack size. Note that the resource module only
302 # is available on UNIX.
305 resource.setrlimit (resource.RLIMIT_STACK, (-1, -1))
309 help_summary = _ ("Generate .dvi with LaTeX for LilyPond")
311 option_definitions = [
312 ('', 'd', 'dependencies', _ ("write Makefile dependencies for every input file")),
313 ('', 'h', 'help', _ ("this help")),
314 (_ ("DIR"), 'I', 'include', _ ("add DIR to LilyPond's search path")),
315 ('', 'k', 'keep', _ ("keep all output, and name the directory %s.dir") % program_name),
316 ('', '', 'no-lily', _ ("don't run LilyPond")),
317 ('', 'm', 'no-paper', _ ("produce MIDI output only")),
318 (_ ("FILE"), 'o', 'output', _ ("write ouput to FILE")),
319 (_ ("FILE"), 'f', 'find-pfa', _ ("find pfa fonts used in FILE")),
321 ('', 'P', 'postscript', _ ("generate PostScript output")),
322 (_ ("KEY=VAL"), 's', 'set', _ ("change global setting KEY to VAL")),
323 ('', 'V', 'verbose', _ ("verbose")),
324 ('', 'v', 'version', _ ("print version number")),
325 ('', 'w', 'warranty', _ ("show warranty and copyright")),
328 layout_fields = ['dedication', 'title', 'subtitle', 'subsubtitle',
329 'footer', 'head', 'composer', 'arranger', 'instrument',
330 'opus', 'piece', 'metre', 'meter', 'poet', 'texttranslator']
333 # init to empty; values here take precedence over values in the file
335 ## TODO: change name.
339 'latexpackages' : ['geometry'],
349 extra_fields = extra_init.keys ()
350 fields = layout_fields + extra_fields
364 track_dependencies_p = 0
365 dependency_files = []
369 kpse = os.popen ('kpsexpand \$TEXMF').read()
370 kpse = re.sub('[ \t\n]+$','', kpse)
371 type1_paths = os.popen ('kpsewhich -expand-path=\$T1FONTS').read ()
374 ## todo: prevent multiple addition.
375 'TEXMF' : "{%s,%s}" % (datadir, kpse) ,
376 'GS_FONTPATH' : type1_paths,
377 'GS_LIB' : datadir + '/ps',
380 # tex needs lots of memory, more than it gets by default on Debian
381 non_path_environment = {
382 'extra_mem_top' : '1000000',
383 'extra_mem_bottom' : '1000000',
384 'pool_size' : '250000',
387 def setup_environment ():
388 # $TEXMF is special, previous value is already taken care of
389 if os.environ.has_key ('TEXM'):
390 del os.environ['TEXMF']
393 for key in environment.keys ():
394 val = environment[key]
395 if os.environ.has_key (key):
396 val = os.environ[key] + os.pathsep + val
397 os.environ[key] = val
399 for key in non_path_environment.keys ():
400 val = non_path_environment[key]
401 os.environ[key] = val
404 def set_setting (dict, key, val):
406 val = string.atof (val)
408 #warning (_ ("invalid value: %s") % `val`)
412 dict[key].append (val)
414 warning (_ ("no such setting: %s") % `key`)
418 def print_environment ():
419 for (k,v) in os.environ.items ():
420 sys.stderr.write ("%s=\"%s\"\n" % (k,v))
422 def run_lilypond (files, outbase, dep_prefix):
424 # opts = opts + '--output=%s.tex' % outbase
425 opts = opts + ' ' + string.join (map (lambda x : '-I ' + x,
428 opts = opts + ' ' + string.join (map (lambda x : '-H ' + x,
431 opts = opts + ' --no-paper'
433 if track_dependencies_p:
434 opts = opts + " --dependencies"
436 opts = opts + ' --dep-prefix=%s' % dep_prefix
438 fs = string.join (files)
441 # cmd = cmd + ' 1> /dev/null 2> /dev/null'
442 progress ( _("Running %s...") % 'LilyPond')
444 opts = opts + ' --verbose'
446 # for better debugging!
449 system ('lilypond %s %s ' % (opts, fs))
451 def analyse_lilypond_output (filename, extra):
454 '''Grep FILENAME for interesting stuff, and
455 put relevant info into EXTRA.'''
456 filename = filename+'.tex'
457 progress (_ ("Analyzing %s...") % filename)
458 s = open (filename).read ()
460 # search only the first 10k
462 for x in extra_fields:
463 m = re.search (r'\\def\\lilypondpaper%s{([^}]*)}'%x, s)
465 set_setting (extra, x, m.group (1))
467 def find_tex_files_for_base (base, extra):
470 Find the \header fields dumped from BASE.
474 for f in layout_fields:
475 if os.path.exists (base + '.' + f):
476 headerfiles[f] = base+'.'+f
478 if os.path.exists (base +'.dep'):
479 dependency_files.append (base + '.dep')
481 for f in extra_fields:
482 if os.path.exists (base + '.' + f):
483 extra[f].append (open (base + '.' + f).read ())
485 return (base +'.tex',headerfiles)
488 def find_tex_files (files, extra):
490 Find all .tex files whose prefixes start with some name in FILES.
499 fname = os.path.basename (f)
500 fname = strip_extension (fname, '.ly')
502 fname = fname + '-%d' % x
504 if os.path.exists (fname + '.tex'):
505 tfiles.append (find_tex_files_for_base (fname, extra))
506 analyse_lilypond_output (fname, extra)
512 fstr = string.join (files, ', ')
513 warning (_ ("no lilypond output found for %s") % fstr)
516 def one_latex_definition (defn, first):
518 for (k,v) in defn[1].items ():
519 val = open (v).read ()
520 if (string.strip (val)):
521 s = s + r'''\def\lilypond%s{%s}''' % (k, val)
523 s = s + r'''\let\lilypond%s\relax''' % k
527 s = s + '\\def\\mustmakelilypondtitle{}\n'
529 s = s + '\\def\\mustmakelilypondpiecetitle{}\n'
531 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.
535 ly_paper_to_latexpaper = {
537 'letter' : 'letterpaper',
540 def global_latex_definition (tfiles, extra):
542 '''construct preamble from EXTRA, dump Latex stuff for each
543 lily output file in TFILES after that, and return the Latex file constructed. '''
547 s = s + '% generation tag\n'
551 if extra['papersize']:
553 options = '%s' % ly_paper_to_latexpaper[extra['papersize'][0]]
555 warning (_ ("invalid value: %s") % `extra['papersize'][0]`)
558 if extra['latexoptions']:
559 options = options + ',' + extra['latexoptions'][-1]
561 s = s + '\\documentclass[%s]{article}\n' % options
563 if extra['language']:
564 s = s + r'\usepackage[%s]{babel}\n' % extra['language'][-1]
567 s = s + '\\usepackage{%s}\n' \
568 % string.join (extra['latexpackages'], ',')
570 if extra['latexheaders']:
571 s = s + '\\include{%s}\n' \
572 % string.join (extra['latexheaders'], '}\n\\include{')
574 unit = extra['unit'][-1]
577 if extra['textheight']:
578 textheight = ',textheight=%f%s' % (extra['textheight'][0], unit)
580 orientation = 'portrait'
581 if extra['orientation']:
582 orientation = extra['orientation'][0]
584 # set sane geometry width (a4-width) for linewidth = -1.
585 maxlw = max (extra['linewidth'] + [-1])
587 # who the hell is 597 ?
590 linewidth = '%d%s' % (maxlw, unit)
591 s = s + '\geometry{width=%s%s,headheight=2mm,footskip=2mm,%s}\n' % (linewidth, textheight, orientation)
593 if extra['latexoptions']:
594 s = s + '\geometry{twosideshift=4mm}\n'
597 \usepackage[latin1]{inputenc}
601 if extra['pagenumber'] and extra['pagenumber'][-1] and extra['pagenumber'][-1] != 'no':
602 s = s + '\setcounter{page}{%s}\n' % (extra['pagenumber'][-1])
603 s = s + '\\pagestyle{plain}\n'
605 s = s + '\\pagestyle{empty}\n'
607 s = s + '\\begin{document}\n'
608 s = s + '\\thispagestyle{firstpage}\n'
612 s = s + one_latex_definition (t, first)
616 s = s + '\\thispagestyle{lastpage}\n'
617 s = s + '\\end{document}'
621 def run_latex (files, outbase, extra):
623 """Construct latex file, for FILES and EXTRA, dump it into
624 OUTBASE.latex. Run LaTeX on it.
630 latex_fn = outbase + '.latex'
632 wfs = find_tex_files (files, extra)
633 s = global_latex_definition (wfs, extra)
635 f = open (latex_fn, 'w')
639 cmd = 'latex \\\\nonstopmode \\\\input %s' % latex_fn
642 progress ( _("Running %s...") % 'LaTeX')
643 cmd = cmd + ' 1> /dev/null 2> /dev/null'
647 def run_dvips (outbase, extra):
650 """Run dvips using the correct options taken from EXTRA,
651 leaving a PS file in OUTBASE.ps
658 if extra['papersize']:
659 opts = opts + ' -t%s' % extra['papersize'][0]
661 if extra['orientation'] and extra['orientation'][0] == 'landscape':
662 opts = opts + ' -tlandscape'
664 cmd = 'dvips %s -o%s %s' % (opts, outbase + '.ps', outbase + '.dvi')
667 progress ( _("Running %s...") % 'dvips')
668 cmd = cmd + ' 1> /dev/null 2> /dev/null'
672 def generate_dependency_file (depfile, outname):
673 df = open (depfile, 'w')
674 df.write (outname + ':' )
676 for d in dependency_files:
678 s = re.sub ('#[^\n]*\n', '', s)
679 s = re.sub (r'\\\n', ' ', s)
680 m = re.search ('.*:(.*)\n', s)
682 # ugh. Different targets?
684 df.write ( m.group (1) + ' ' )
689 def find_file_in_path (path, name):
690 for d in string.split (path, os.pathsep):
691 if name in os.listdir (d):
692 return os.path.join (d, name)
694 # Added as functionality to ly2dvi, because ly2dvi may well need to do this
697 def find_pfa_fonts (name):
698 s = open (name).read ()
699 if s[:len (PS)] != PS:
701 errorport.write (_( "error: ") + _ ("not a PostScript file: `%s\'" % name))
702 errorport.write ('\n')
705 m = re.match ('.*?/(feta[-a-z0-9]+) +findfont', s[here:], re.DOTALL)
709 pfa.append (m.group (1))
710 m = re.match ('.*?/(feta[-a-z0-9]+) +findfont', s[here:], re.DOTALL)
714 (sh, long) = getopt_args (option_definitions)
716 (options, files) = getopt.getopt(sys.argv[1:], sh, long)
717 except getopt.error, s:
718 errorport.write ('\n')
719 errorport.write (_ ("error: ") + _ ("getopt says: `%s\'" % s))
720 errorport.write ('\n')
721 errorport.write ('\n')
731 elif o == '--help' or o == '-h':
734 elif o == '--find-pfa' or o == '-f':
735 fonts = map (lambda x: x + '.pfa', find_pfa_fonts (a))
737 files = map (lambda x:
738 find_file_in_path (os.environ['GS_FONTPATH'], x),
740 print string.join (files, ' ')
742 elif o == '--include' or o == '-I':
743 include_path.append (a)
744 elif o == '--postscript' or o == '-P':
746 elif o == '--keep' or o == '-k':
748 elif o == '--no-lily':
750 elif o == '--no-paper' or o == '-m':
754 elif o == '--output' or o == '-o':
756 elif o == '--set' or o == '-s':
757 ss = string.split (a, '=')
758 set_setting (extra_init, ss[0], ss[1])
759 elif o == '--dependencies' or o == '-d':
760 track_dependencies_p = 1
761 elif o == '--verbose' or o == '-V':
763 elif o == '--version' or o == '-v':
766 elif o == '--warranty' or o == '-w':
767 status = system ('lilypond -w', ignore_error = 1)
774 def cp_to_dir (pattern, dir):
775 "Copy files matching re PATTERN from cwd to DIR"
776 # Duh. Python style portable: cp *.EXT OUTDIR
777 # system ('cp *.%s %s' % (ext, outdir), 1)
778 files = filter (lambda x, p=pattern: re.match (p, x), os.listdir ('.'))
779 map (lambda x, d=dir: shutil.copy2 (x, os.path.join (d, x)), files)
781 # Python < 1.5.2 compatibility
783 # On most platforms, this is equivalent to
784 #`normpath(join(os.getcwd()), PATH)'. *Added in Python version 1.5.2*
785 if os.path.__dict__.has_key ('abspath'):
786 abspath = os.path.abspath
789 return os.path.normpath (os.path.join (os.getcwd (), path))
791 if os.__dict__.has_key ('makedirs'):
792 makedirs = os.makedirs
794 def makedirs (dir, mode=0777):
795 system ('mkdir -p %s' % dir)
797 def mkdir_p (dir, mode=0777):
798 if not os.path.isdir (dir):
801 include_path = map (abspath, include_path)
803 original_output = output_name
806 if files and files[0] != '-':
808 # Ugh, maybe make a setup () function
809 files = map (lambda x: strip_extension (x, '.ly'), files)
811 # hmmm. Wish I'd 've written comments when I wrote this.
812 # now it looks complicated.
814 (outdir, outbase) = ('','')
816 outbase = os.path.basename (files[0])
817 outdir = abspath('.')
818 elif output_name[-1] == os.sep:
819 outdir = abspath (output_name)
820 outbase = os.path.basename (files[0])
822 (outdir, outbase) = os.path.split (abspath (output_name))
824 for i in ('.dvi', '.latex', '.ly', '.ps', '.tex'):
825 output_name = strip_extension (output_name, i)
826 outbase = strip_extension (outbase, i)
827 files = map (abspath, files)
829 for i in files[:] + [output_name]:
830 if string.find (i, ' ') >= 0:
831 user_error (_ ("filename should not contain spaces: `%s'") % i)
833 if os.path.dirname (output_name) != '.':
834 dep_prefix = os.path.dirname (output_name)
838 reldir = os.path.dirname (output_name)
839 if outdir != '.' and (track_dependencies_p or targets.keys ()):
840 mkdir_p (outdir, 0777)
843 tmpdir = setup_temp ()
845 # to be sure, add tmpdir *in front* of inclusion path.
846 #os.environ['TEXINPUTS'] = tmpdir + ':' + os.environ['TEXINPUTS']
851 run_lilypond (files, outbase, dep_prefix)
853 # TODO: friendly message about LilyPond setup/failing?
855 # TODO: lilypond should fail with different
857 # - guile setup/startup failure
858 # - font setup failure
859 # - init.ly setup failure
860 # - parse error in .ly
861 # - unexpected: assert/core dump
863 traceback.print_exc ()
865 if targets.has_key ('DVI') or targets.has_key ('PS'):
867 run_latex (files, outbase, extra_init)
868 # unless: add --tex, or --latex?
872 # TODO: friendly message about TeX/LaTeX setup,
873 # trying to run tex/latex by hand
874 if targets.has_key ('DVI'):
876 if targets.has_key ('PS'):
878 traceback.print_exc ()
880 if targets.has_key ('PS'):
882 run_dvips (outbase, extra_init)
884 if targets.has_key ('PS'):
886 traceback.print_exc ()
888 # add DEP to targets?
889 if track_dependencies_p:
890 depfile = os.path.join (outdir, outbase + '.dep')
891 generate_dependency_file (depfile, depfile)
892 if os.path.isfile (depfile):
893 progress (_ ("dependencies output to `%s'...") % depfile)
895 # Hmm, if this were a function, we could call it the except: clauses
896 for i in targets.keys ():
897 ext = string.lower (i)
898 cp_to_dir ('.*\.%s$' % ext, outdir)
899 outname = outbase + '.' + string.lower (i)
900 abs = os.path.join (outdir, outname)
902 outname = os.path.join (reldir, outname)
903 if os.path.isfile (abs):
904 progress (_ ("%s output to `%s'...") % (i, outname))
906 warning (_ ("can't find file: `%s'") % outname)
908 os.chdir (original_dir)
912 # FIXME: read from stdin when files[0] = '-'
914 user_error (_ ("no files specified on command line."), 2)