]> git.donarmstrong.com Git - lilypond.git/blobdiff - scripts/lilypond.py
2003 -> 2004
[lilypond.git] / scripts / lilypond.py
index b53a812cc566f0407d4b217e67ac8c5d0f35c478..773c81d9c63382ae9427990609bcb80146df6c4b 100644 (file)
@@ -2,23 +2,23 @@
 #
 # lilypond.py -- Run LilyPond, add titles to bare score, generate printable
 #              document
-#              Invokes: lilypond, latex (or pdflatex), dvips, ps2pdf, gs
+#              Invokes: lilypond-bin, latex (or pdflatex), dvips, ps2pdf, gs
 # 
 # source file of the GNU LilyPond music typesetter
 # 
-# (c)  1998--2003  Han-Wen Nienhuys <hanwen@cs.uu.nl>
+# (c) 1998--2004  Han-Wen Nienhuys <hanwen@cs.uu.nl>
 #                 Jan Nieuwenhuizen <janneke@gnu.org>
 
 # This is the third incarnation of ly2dvi, now renamed lilypond.
 #
-# Earlier incarnations of ly2dvi were written by
+# Earlier incarnations of lilypond were written by
 # Jeffrey B. Reed<daboys@austin.rr.com> (Python version)
 # Jan Arne Fagertun <Jan.A.Fagertun@@energy.sintef.no> (Bourne shell script)
 #
 
-# Note: gettext work best if we use ' for docstrings and "
+# Note: gettext work best if we use ' for program/docstrings and "
 #       for gettextable strings.
-#       --> DO NOT USE ''' for docstrings.
+#       USE ''' for docstrings.
 
 
 '''
@@ -56,10 +56,10 @@ TODO:
 
   * Introduce verbosity levels
   
-     0  = QUIET: mute all command output, no ly2dvi progress
-     1  = BRIEF: mute all command output, only ly2dvi progress
-     2a = NORMAL: show only LilyPond command output, show ly2dvi progress
-     2b = NORMAL: show command output, show ly2dvi progress
+     0  = QUIET: mute all command output, no lilypond progress
+     1  = BRIEF: mute all command output, only lilypond progress
+     2a = NORMAL: show only LilyPond command output, show lilypond progress
+     2b = NORMAL: show command output, show lilypond progress
      3  = VERBOSE: show command output, run lilypond --verbose
      4  = DEBUGGING: show all command output, run lilypond --verbose, print
                    environment and all kinds of client side debugging stuff
@@ -115,7 +115,6 @@ preview_resolution = 90
 debug_p = 0
 
 ## FIXME
-## ly2dvi: silly name?
 ## do -P or -p by default?
 ##help_summary = _ ("Run LilyPond using LaTeX for titling")
 help_summary = _ ("Run LilyPond, add titles, generate printable document.")
@@ -127,30 +126,33 @@ option_definitions = [
         _ ("write Makefile dependencies for every input file")),
        ('', 'h', 'help', _ ("print this help")),
        ('', '', 'debug', _ ("print even more output")),
+       (_ ("FILE"), 'f', 'find-pfa', _ ("find pfa fonts used in FILE")),
+       ('','', 'html', _("make HTML file with links to all output")),
        (_ ("DIR"), 'I', 'include', _ ("add DIR to LilyPond's search path")),
        ('', 'k', 'keep',
         _ ("keep all output, output to directory %s.dir") % program_name),
        ('', '', 'no-lily', _ ("don't run LilyPond")),
        ('', 'm', 'no-paper', _ ("produce MIDI output only")),
        (_ ("FILE"), 'o', 'output', _ ("write output to FILE")),
-       (_ ("FILE"), 'f', 'find-pfa', _ ("find pfa fonts used in FILE")),
        (_ ('RES'), '', 'preview-resolution',
         _ ("set the resolution of the preview to RES")),
+       ('', '', 'no-pdf', _ ("do not generate PDF output")),
+       ('', '', 'no-ps', _ ("do not generate PostScript outpug")),
+       ('', 'p', 'pdf', _ ("generate PDF output")),
        ('', 'P', 'postscript', _ ("generate PostScript output")),
-       ('', '', 'png', _("generate PNG page images")),
-       ('', '', 'psgz', _("generate PS.GZ")),
-       # separate options for PDF  / PS / DVI ?  
        ('', '', 'pdftex', _ ("use pdflatex to generate a PDF output")),
-       # FIXME: preview, picture; to indicate creation of a PNG?
+       ('', '', 'png', _("generate PNG page images")),
        ('', '', 'preview', _ ("make a picture of the first system")),
-       ('','', 'html', _("make HTML file with links to all output")),
-       (_ ("KEY=VAL"), 's', 'set', _ ("change global setting KEY to VAL")),
+       ('', '', 'psgz', _ ("generate PS.GZ")),
+       ('', 's', 'safe-mode', _ ("run in safe-mode")),
+       (_ ("KEY=VAL"), 'S', 'set', _ ("change global setting KEY to VAL")),
        ('', 'V', 'verbose', _ ("be verbose")),
        ('', 'v', 'version', _ ("print version number")),
        ('', 'w', 'warranty', _ ("show warranty and copyright")),
        ]
 
 # other globals
+safe_mode_p = 0
 preview_p = 0
 page_images_p = 0
 lilypond_error_p = 0
@@ -205,7 +207,7 @@ paper_p = 1
 
 output_name = ''
 
-# Output formats that ly2dvi should create
+# Output formats that lilypond should create
 targets = ['DVI', 'LATEX', 'MIDI', 'TEX', 'PDF', 'PS']
 
 track_dependencies_p = 0
@@ -233,11 +235,16 @@ def set_setting (dict, key, val):
                dict[key] = [val]
 
 
+def escape_shell (x):
+       return re.sub ("(\s|[`'\"\\\\])", r'\\\1',x)
+
+
 def run_lilypond (files, dep_prefix):
+       def make_include_option (x):
+               return '-I %s' %   escape_shell (x)
 
        opts = ''
-       opts = opts + ' ' + string.join (map (lambda x : '-I ' + x,
-                                             include_path))
+       opts = opts + ' ' + string.join (map (make_include_option, include_path))
        if pseudo_filter_p:
                opts = opts + ' --output=lelie'
        if paper_p:
@@ -249,12 +256,15 @@ def run_lilypond (files, dep_prefix):
        if pdftex_p:
                opts = opts + ' -f pdftex'              
 
+       if safe_mode_p:
+               opts = opts + ' --safe-mode'
+
        if track_dependencies_p:
                opts = opts + " --dependencies"
                if dep_prefix:
                        opts = opts + ' --dep-prefix=%s' % dep_prefix
 
-       fs = string.join (files)
+       fs = string.join (map (escape_shell, files))
 
        global verbose_p
        if verbose_p:
@@ -379,6 +389,8 @@ ly_paper_to_latexpaper =  {
        'a4' : 'a4paper',
        'a5' : 'a5paper',
        'a6' : 'a6paper',
+       'legal' : 'legalpaper', 
+       'tabloid' : 'papersize={11in,17in}', 
 }
 
 #TODO: should set textheight (enlarge) depending on papersize. 
@@ -389,22 +401,17 @@ def global_latex_preamble (extra):
 
        options = ''
 
-
-       if extra['papersize']:
-               try:
-                       options = ly_paper_to_latexpaper[extra['papersize'][0]]
-               except KeyError:
-                       ly.warning (_ ("invalid value: `%s'") % `extra['papersize'][0]`)
-                       pass
-
        if extra['latexoptions']:
                options = options + ',' + extra['latexoptions'][-1]
 
        s = s + '\\documentclass[%s]{article}\n' % options
 
-       if extra['language']:
-               s = s + r'\usepackage[%s]{babel}' % extra['language'][-1] + '\n'
+       if safe_mode_p:
+               s = s + '\\nofiles\n'
 
+       if extra['language']:
+               s = s + r'\usepackage[%s]{babel}' \
+                   % extra['language'][-1] + '\n'
 
        s = s + '\\usepackage{%s}\n' \
                % string.join (extra['latexpackages'], ',')
@@ -415,6 +422,15 @@ def global_latex_preamble (extra):
 
        unit = extra['unit'][-1]
 
+
+       papersize = ''
+       if extra['papersize']:
+               try:
+                       papersize = ly_paper_to_latexpaper[extra['papersize'][0]] + ','
+               except KeyError:
+                       ly.warning (_ ("invalid value: `%s'") % `extra['papersize'][0]`)
+                       pass
+
        textheight = ''
        if extra['textheight']:
                textheight = ',textheight=%f%s' % (extra['textheight'][0], unit)
@@ -430,7 +446,7 @@ def global_latex_preamble (extra):
                linewidth = '597pt'
        else:
                linewidth = '%d%s' % (maxlw, unit)
-       s = s + '\geometry{width=%s%s,headheight=2mm,footskip=2mm,%s}\n' % (linewidth, textheight, orientation)
+       s = s + '\geometry{%swidth=%s%s,bottom=11mm,headsep=2mm,top=12mm,headheight=2mm,footskip=5mm,%s}\n' % (papersize, linewidth, textheight, orientation)
 
 
        if 'twoside' in  extra['latexoptions'] :
@@ -466,7 +482,7 @@ lily output file in TFILES after that, and return the Latex file constructed.  '
                first = 0
 
 
-       s = s + '\\thispagestyle{lastpage}\n'
+       s = s + '\n\\thispagestyle{lastpage}\n'
        s = s + '\\end{document}'
 
        return s
@@ -576,7 +592,7 @@ Using bitmap fonts instead. This will look bad.'''))
        if preview_p:
                for score in find_tex_files (files, extra):
                        preview_base = ly.strip_extension (score[0], '.tex')
-                       cmd = 'dvips -E -o%s %s' \
+                       cmd = 'dvips -E -Ppdf -u+lilypond.map -o%s %s' \
                              % (preview_base + '.preview.ps',
                                 preview_base + '.preview.dvi')
                        ly.system (cmd)
@@ -607,7 +623,7 @@ def find_file_in_path (path, name):
                if name in os.listdir (d):
                        return os.path.join (d, name)
 
-# Added as functionality to ly2dvi, because ly2dvi may well need to do this
+# Added as functionality to lilypond, because lilypond may well need to do this
 # in future too.
 PS = '%!PS-Adobe'
 def find_pfa_fonts (name):
@@ -696,10 +712,21 @@ for opt in options:
        elif o == '--include' or o == '-I':
                include_path.append (a)
        elif o == '--postscript' or o == '-P':
-               targets.append ('PS')
+               if 'PDF' in targets:
+                       targets.remove ('PDF')
+               if 'PS' not in targets:
+                       targets.append ('PS')
+       elif o == '--pdf' or o == '-p':
+               if 'PDF' not in targets:
+                       targets.append ('PDF')
+       elif o == '--no-pdf':
+               if 'PDF' in targets:
+                       targets.remove ('PDF')
        elif o == '--no-ps':
-               targets.remove ('PS')
-               targets.remove ('PDF')
+               if 'PS' in targets:
+                       targets.remove ('PS')
+               if 'PDF' in targets:
+                       targets.remove ('PDF')
        elif o == '--keep' or o == '-k':
                keep_temp_dir_p = 1
        elif o == '--debug':
@@ -718,7 +745,9 @@ for opt in options:
                paper_p = 0
        elif o == '--output' or o == '-o':
                output_name = a
-       elif o == '--set' or o == '-s':
+       elif o == '--safe-mode' or o == '-s':
+               safe_mode_p = 1
+       elif o == '--set' or o == '-S':
                ss = string.split (a, '=')
                set_setting (extra_init, ss[0], ss[1])
        elif o == '--dependencies' or o == '-d':
@@ -730,8 +759,8 @@ for opt in options:
                sys.exit (0)
        elif o == '--pdftex':
                latex_cmd = 'pdflatex'
-               targets.remove('DVI')
-               targets.append('PDFTEX')
+               targets.remove ('DVI')
+               targets.append ('PDFTEX')
                pdftex_p = 1
                tex_extension = '.pdftex'
        elif o == '--warranty' or o == '-w':
@@ -747,6 +776,8 @@ for opt in options:
                        targets.append ('PNG')
        elif o == '--psgz':
                targets.append ('PS.GZ')
+               if 'PS' not in targets:
+                       targets.append ('PS')
        else:
                unimplemented_option () # signal programming error
 
@@ -757,7 +788,7 @@ include_path.insert (0, '.')
 # As a neat trick, add directory part of first input file
 # to include path.  That way you can do without the clumsy -I in:
 
-#    ly2dvi -I foe/bar/baz foo/bar/baz/baz.ly
+#    lilypond -I foe/bar/baz foo/bar/baz/baz.ly
 if files and files[0] != '-' and os.path.dirname (files[0]) != '.':
        include_path.append (os.path.dirname (files[0]))
        
@@ -806,9 +837,9 @@ if 1:
                outbase = ly.strip_extension (outbase, i)
 
        for i in files[:] + [output_name]:
-               if string.find (i, ' ') >= 0:
-                       ly.error (_ ("filename should not contain spaces: `%s'") %
-                              i)
+               b = os.path.basename (i)
+               if string.find (b, ' ') >= 0:
+                       ly.error (_ ("filename should not contain spaces: `%s'") % b)
                        ly.exit (1)
                        
        if os.path.dirname (output_name) != '.':
@@ -822,6 +853,8 @@ if 1:
 
        tmpdir = ly.setup_temp ()
        ly.setup_environment ()
+       if safe_mode_p:
+               os.environ['openout_any'] = 'p'
 
        # to be sure, add tmpdir *in front* of inclusion path.
        #os.environ['TEXINPUTS'] =  tmpdir + ':' + os.environ['TEXINPUTS']