2 # Run lilypond, latex, dvips.
4 # This is the third incarnation of ly2dvi.
6 # Earlier incarnations of ly2dvi were written by
7 # Jeffrey B. Reed<daboys@austin.rr.com> (Python version)
8 # Jan Arne Fagertun <Jan.A.Fagertun@@energy.sintef.no> (Bourne shell script)
13 # TODO: should allow to set a central pk cache directory from the command line.
14 # TODO: should allow to switch off pk cache.
18 # Note: gettext work best if we use ' for docstrings and "
19 # for gettextable strings.
20 # --> DO NOT USE """ for docstrings.
25 * figure out which set of command line options should make ly2dvi:
28 na: create latex only?
29 na: create tex and latex
30 default: create dvi only
31 na: create tex, latex and dvi
37 for foo.ly, rename ly2dvi.dir to out-ly2dvi, foo.ly2dvi, foo.dir ?
39 * move versatile taglines,
42 beginfooter=\mutopiaPD
43 endfooter=\tagline -> 'lily was here <version>'
46 lilytagline (->lily was here), usertagline, copyright etc.
48 * head/header tagline/endfooter
50 * dvi from lilypond .tex output? This is hairy, because we create dvi
51 from lilypond .tex *and* header output.
53 * multiple \score blocks?
70 # if set, LILYPONDPREFIX must take prevalence
71 # if datadir is not set, we're doing a build and LILYPONDPREFIX
73 if os.environ.has_key ('LILYPONDPREFIX') \
74 or '@datadir@' == '@' + 'datadir' + '@':
75 datadir = os.environ['LILYPONDPREFIX']
79 sys.path.append (os.path.join (datadir, 'python'))
80 sys.path.append (os.path.join (datadir, 'python/out'))
82 program_name = 'ly2dvi'
83 program_version = '@TOPLEVEL_VERSION@'
84 original_dir = os.getcwd ()
85 temp_dir = os.path.join (original_dir, '%s.dir' % program_name)
86 errorport = sys.stderr
92 gettext.bindtextdomain ('lilypond', '@localedir@')
93 gettext.textdomain ('lilypond')
99 # Attempt to fix problems with limited stack size set by Python!
100 # Sets unlimited stack size. Note that the resource module only
101 # is available on UNIX.
104 resource.setrlimit (resource.RLIMIT_STACK, (-1, -1))
108 help_summary = _ ("Generate .dvi with LaTeX for LilyPond")
110 option_definitions = [
111 ('', 'd', 'dependencies', _ ("write Makefile dependencies for every input file")),
112 ('', 'h', 'help', _ ("this help")),
113 (_ ("DIR"), 'I', 'include', _ ("add DIR to LilyPond's search path")),
114 ('', 'k', 'keep', _ ("keep all output, and name the directory %s.dir") % program_name),
115 ('', '', 'no-lily', _ ("don't run LilyPond")),
116 ('', 'm', 'no-paper', _ ("produce MIDI output only")),
117 (_ ("FILE"), 'o', 'output', _ ("write ouput to FILE")),
118 (_ ("FILE"), 'f', 'find-pfa', _ ("find pfa fonts used in FILE")),
120 ('', 'P', 'postscript', _ ("generate PostScript output")),
121 (_ ("KEY=VAL"), 's', 'set', _ ("change global setting KEY to VAL")),
122 ('', 'V', 'verbose', _ ("verbose")),
123 ('', 'v', 'version', _ ("print version number")),
124 ('', 'w', 'warranty', _ ("show warranty and copyright")),
127 from lilylib import *
129 layout_fields = ['dedication', 'title', 'subtitle', 'subsubtitle',
130 'footer', 'head', 'composer', 'arranger', 'instrument',
131 'opus', 'piece', 'metre', 'meter', 'poet', 'texttranslator']
134 # init to empty; values here take precedence over values in the file
136 ## TODO: change name.
140 'latexpackages' : ['geometry'],
149 extra_fields = extra_init.keys ()
150 fields = layout_fields + extra_fields
157 PK_PATTERN='feta.*\.[0-9]+pk'
167 track_dependencies_p = 0
168 dependency_files = []
172 # Try to cater for bad installations of LilyPond, that have
173 # broken TeX setup. Just hope this doesn't hurt good TeX
174 # setups. Maybe we should check if kpsewhich can find
175 # feta16.{afm,mf,tex,tfm}, and only set env upon failure.
178 'MFINPUTS' : datadir + '/mf' + ':',
179 'TEXINPUTS': datadir + '/tex:' + datadir + '/ps:' + '.:'
181 'TFMFONTS' : datadir + '/tfm' + ':',
182 'GS_FONTPATH' : datadir + '/afm:' + datadir + '/pfa',
183 'GS_LIB' : datadir + '/ps',
187 def setup_environment ():
188 for key in environment.keys ():
189 val = environment[key]
190 if os.environ.has_key (key):
191 val = os.environ[key] + os.pathsep + val
192 os.environ[key] = val
195 def set_setting (dict, key, val):
197 val = string.atof (val)
199 #warning (_ ("invalid value: %s") % `val`)
203 dict[key].append (val)
205 warning (_ ("no such setting: %s") % `key`)
209 def print_environment ():
210 for (k,v) in os.environ.items ():
211 sys.stderr.write ("%s=\"%s\"\n" % (k,v))
213 def run_lilypond (files, outbase, dep_prefix):
215 # opts = opts + '--output=%s.tex' % outbase
216 opts = opts + ' ' + string.join (map (lambda x : '-I ' + x,
219 opts = opts + ' ' + string.join (map (lambda x : '-H ' + x,
222 opts = opts + ' --no-paper'
224 if track_dependencies_p:
225 opts = opts + " --dependencies"
227 opts = opts + ' --dep-prefix=%s' % dep_prefix
229 fs = string.join (files)
232 # cmd = cmd + ' 1> /dev/null 2> /dev/null'
233 progress ( _("Running %s...") % 'LilyPond')
235 opts = opts + ' --verbose'
237 # for better debugging!
240 system ('lilypond %s %s ' % (opts, fs))
242 def analyse_lilypond_output (filename, extra):
245 '''Grep FILENAME for interesting stuff, and
246 put relevant info into EXTRA.'''
247 filename = filename+'.tex'
248 progress (_ ("Analyzing %s...") % filename)
249 s = open (filename).read ()
251 # search only the first 10k
253 for x in ('textheight', 'linewidth', 'papersize', 'orientation'):
254 m = re.search (r'\\def\\lilypondpaper%s{([^}]*)}'%x, s)
256 set_setting (extra, x, m.group (1))
258 def find_tex_files_for_base (base, extra):
261 Find the \header fields dumped from BASE.
265 for f in layout_fields:
266 if os.path.exists (base + '.' + f):
267 headerfiles[f] = base+'.'+f
269 if os.path.exists (base +'.dep'):
270 dependency_files.append (base + '.dep')
272 for f in extra_fields:
273 if os.path.exists (base + '.' + f):
274 extra[f].append (open (base + '.' + f).read ())
276 return (base +'.tex',headerfiles)
279 def find_tex_files (files, extra):
281 Find all .tex files whose prefixes start with some name in FILES.
290 fname = os.path.basename (f)
291 fname = strip_extension (fname, '.ly')
293 fname = fname + '-%d' % x
295 if os.path.exists (fname + '.tex'):
296 tfiles.append (find_tex_files_for_base (fname, extra))
297 analyse_lilypond_output (fname, extra)
303 fstr = string.join (files, ', ')
304 warning (_ ("no lilypond output found for %s") % fstr)
307 def one_latex_definition (defn, first):
309 for (k,v) in defn[1].items ():
310 val = open (v).read ()
311 if (string.strip (val)):
312 s = s + r'''\def\lilypond%s{%s}''' % (k, val)
314 s = s + r'''\let\lilypond%s\relax''' % k
318 s = s + '\\def\\mustmakelilypondtitle{}\n'
320 s = s + '\\def\\mustmakelilypondpiecetitle{}\n'
322 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.
326 ly_paper_to_latexpaper = {
328 'letter' : 'letterpaper',
331 def global_latex_definition (tfiles, extra):
333 '''construct preamble from EXTRA, dump Latex stuff for each
334 lily output file in TFILES after that, and return the Latex file constructed. '''
338 s = s + '% generation tag\n'
342 if extra['papersize']:
344 options = '%s' % ly_paper_to_latexpaper[extra['papersize'][0]]
346 warning (_ ("invalid value: %s") % `extra['papersize'][0]`)
349 if extra['latexoptions']:
350 options = options + ',' + extra['latexoptions'][-1]
352 s = s + '\\documentclass[%s]{article}\n' % options
354 if extra['language']:
355 s = s + r'\usepackage[%s]{babel}\n' % extra['language'][-1]
358 s = s + '\\usepackage{%s}\n' \
359 % string.join (extra['latexpackages'], ',')
361 if extra['latexheaders']:
362 s = s + '\\include{%s}\n' \
363 % string.join (extra['latexheaders'], '}\n\\include{')
366 if extra['textheight']:
367 textheight = ',textheight=%fpt' % extra['textheight'][0]
369 orientation = 'portrait'
370 if extra['orientation']:
371 orientation = extra['orientation'][0]
373 # set sane geometry width (a4-width) for linewidth = -1.
374 maxlw = max (extra['linewidth'] + [-1])
376 # who the hell is 597 ?
380 s = s + '\geometry{width=%spt%s,headheight=2mm,footskip=2mm,%s}\n' % (linewidth, textheight, orientation)
382 if extra['latexoptions']:
383 s = s + '\geometry{twosideshift=4mm}\n'
386 \usepackage[latin1]{inputenc}
390 if extra['pagenumber'] and extra['pagenumber'][-1] and extra['pagenumber'][-1] != 'no':
391 s = s + '\setcounter{page}{%s}\n' % (extra['pagenumber'][-1])
392 s = s + '\\pagestyle{plain}\n'
394 s = s + '\\pagestyle{empty}\n'
396 s = s + '\\begin{document}\n'
397 s = s + '\\thispagestyle{firstpage}\n'
401 s = s + one_latex_definition (t, first)
405 s = s + '\\thispagestyle{lastpage}\n'
406 s = s + '\\end{document}'
410 def run_latex (files, outbase, extra):
412 """Construct latex file, for FILES and EXTRA, dump it into
413 OUTBASE.latex. Run LaTeX on it.
419 latex_fn = outbase + '.latex'
421 wfs = find_tex_files (files, extra)
422 s = global_latex_definition (wfs, extra)
424 f = open (latex_fn, 'w')
428 cmd = 'latex \\\\nonstopmode \\\\input %s' % latex_fn
431 progress ( _("Running %s...") % 'LaTeX')
432 cmd = cmd + ' 1> /dev/null 2> /dev/null'
436 def run_dvips (outbase, extra):
439 """Run dvips using the correct options taken from EXTRA,
440 leaving a PS file in OUTBASE.ps
447 if extra['papersize']:
448 opts = opts + ' -t%s' % extra['papersize'][0]
450 if extra['orientation'] and extra['orientation'][0] == 'landscape':
451 opts = opts + ' -tlandscape'
453 cmd = 'dvips %s -o%s %s' % (opts, outbase + '.ps', outbase + '.dvi')
456 progress ( _("Running %s...") % 'dvips')
457 cmd = cmd + ' 1> /dev/null 2> /dev/null'
461 def generate_dependency_file (depfile, outname):
462 df = open (depfile, 'w')
463 df.write (outname + ':' )
465 for d in dependency_files:
467 s = re.sub ('#[^\n]*\n', '', s)
468 s = re.sub (r'\\\n', ' ', s)
469 m = re.search ('.*:(.*)\n', s)
471 # ugh. Different targets?
473 df.write ( m.group (1) + ' ' )
478 def find_file_in_path (path, name):
479 for d in string.split (path, os.pathsep):
480 if name in os.listdir (d):
481 return os.path.join (d, name)
483 # Added as functionality to ly2dvi, because ly2dvi may well need to do this
486 def find_pfa_fonts (name):
487 s = open (name).read ()
488 if s[:len (PS)] != PS:
490 errorport.write (_( "error: ") + _ ("not a PostScript file: `%s\'" % name))
491 errorport.write ('\n')
494 m = re.match ('.*?/(feta[-a-z0-9]+) +findfont', s[here:], re.DOTALL)
498 pfa.append (m.group (1))
499 m = re.match ('.*?/(feta[-a-z0-9]+) +findfont', s[here:], re.DOTALL)
503 (sh, long) = getopt_args (option_definitions)
505 (options, files) = getopt.getopt(sys.argv[1:], sh, long)
506 except getopt.error, s:
507 errorport.write ('\n')
508 errorport.write (_ ("error: ") + _ ("getopt says: `%s\'" % s))
509 errorport.write ('\n')
510 errorport.write ('\n')
520 elif o == '--help' or o == '-h':
523 elif o == '--find-pfa' or o == '-f':
524 fonts = map (lambda x: x + '.pfa', find_pfa_fonts (a))
525 files = map (lambda x:
526 find_file_in_path (os.environ['GS_FONTPATH'], x),
528 print string.join (files, ' ')
530 elif o == '--include' or o == '-I':
531 include_path.append (a)
532 elif o == '--postscript' or o == '-P':
534 elif o == '--keep' or o == '-k':
536 elif o == '--no-lily':
538 elif o == '--no-paper' or o == '-m':
542 elif o == '--output' or o == '-o':
544 elif o == '--set' or o == '-s':
545 ss = string.split (a, '=')
546 set_setting (extra_init, ss[0], ss[1])
547 elif o == '--dependencies' or o == '-d':
548 track_dependencies_p = 1
549 elif o == '--verbose' or o == '-V':
551 elif o == '--version' or o == '-v':
554 elif o == '--warranty' or o == '-w':
555 status = system ('lilypond -w', ignore_error = 1)
562 def cp_to_dir (pattern, dir):
563 "Copy files matching re PATTERN from cwd to DIR"
564 # Duh. Python style portable: cp *.EXT OUTDIR
565 # system ('cp *.%s %s' % (ext, outdir), 1)
566 files = filter (lambda x, p=pattern: re.match (p, x), os.listdir ('.'))
567 map (lambda x, d=dir: shutil.copy2 (x, os.path.join (d, x)), files)
569 # Python < 1.5.2 compatibility
571 # On most platforms, this is equivalent to
572 #`normpath(join(os.getcwd()), PATH)'. *Added in Python version 1.5.2*
573 if os.path.__dict__.has_key ('abspath'):
574 abspath = os.path.abspath
577 return os.path.normpath (os.path.join (os.getcwd (), path))
579 if os.__dict__.has_key ('makedirs'):
580 makedirs = os.makedirs
582 def makedirs (dir, mode=0777):
583 system ('mkdir -p %s' % dir)
585 def mkdir_p (dir, mode=0777):
586 if not os.path.isdir (dir):
589 include_path = map (abspath, include_path)
591 original_output = output_name
594 if files and files[0] != '-':
596 # Ugh, maybe make a setup () function
597 files = map (lambda x: strip_extension (x, '.ly'), files)
599 (outdir, outbase) = ('','')
601 outbase = os.path.basename (files[0])
602 outdir = abspath('.')
603 elif output_name[-1] == os.sep:
604 outdir = abspath (output_name)
605 outbase = os.path.basename (files[0])
607 (outdir, outbase) = os.path.split (abspath (output_name))
609 for i in ('.dvi', '.latex', '.ly', '.ps', '.tex'):
610 output_name = strip_extension (output_name, i)
612 files = map (abspath, files)
614 if os.path.dirname (output_name) != '.':
615 dep_prefix = os.path.dirname (output_name)
619 reldir = os.path.dirname (output_name)
620 if outdir != '.' and (track_dependencies_p or targets.keys ()):
621 mkdir_p (outdir, 0777)
624 tmpdir = setup_temp ()
627 cp_to_dir (PK_PATTERN, tmpdir)
629 # to be sure, add tmpdir *in front* of inclusion path.
630 #os.environ['TEXINPUTS'] = tmpdir + ':' + os.environ['TEXINPUTS']
635 run_lilypond (files, outbase, dep_prefix)
637 # TODO: friendly message about LilyPond setup/failing?
639 # TODO: lilypond should fail with different
641 # - guile setup/startup failure
642 # - font setup failure
643 # - init.ly setup failure
644 # - parse error in .ly
645 # - unexpected: assert/core dump
647 traceback.print_exc ()
649 if targets.has_key ('DVI') or targets.has_key ('PS'):
651 run_latex (files, outbase, extra_init)
652 # unless: add --tex, or --latex?
656 # TODO: friendly message about TeX/LaTeX setup,
657 # trying to run tex/latex by hand
658 if targets.has_key ('DVI'):
660 if targets.has_key ('PS'):
662 traceback.print_exc ()
664 if targets.has_key ('PS'):
666 run_dvips (outbase, extra_init)
668 if targets.has_key ('PS'):
670 traceback.print_exc ()
672 # add DEP to targets?
673 if track_dependencies_p:
674 depfile = os.path.join (outdir, outbase + '.dep')
675 generate_dependency_file (depfile, depfile)
676 if os.path.isfile (depfile):
677 progress (_ ("dependencies output to `%s'...") % depfile)
679 # Hmm, if this were a function, we could call it the except: clauses
680 for i in targets.keys ():
681 ext = string.lower (i)
682 cp_to_dir ('.*\.%s$' % ext, outdir)
683 outname = outbase + '.' + string.lower (i)
684 abs = os.path.join (outdir, outname)
686 outname = os.path.join (reldir, outname)
687 if os.path.isfile (abs):
688 progress (_ ("%s output to `%s'...") % (i, outname))
690 warning (_ ("can't find file: `%s'") % outname)
693 cp_to_dir (PK_PATTERN, outdir)
695 os.chdir (original_dir)
699 # FIXME: read from stdin when files[0] = '-'
701 errorport.write (program_name + ":" + _ ("error: ") + _ ("no files specified on command line.") + '\n')