]> git.donarmstrong.com Git - lilypond.git/blobdiff - scripts/lilypond-book.py
Introduce a maximum depth for markup evaluation
[lilypond.git] / scripts / lilypond-book.py
index aafce6d2be78224d11faccf890e95b4a9ca2c828..0814739b47b890d3278ea66a5d8dc0442fd3fbe4 100644 (file)
@@ -51,6 +51,7 @@ import re
 import stat
 import sys
 import tempfile
+import imp
 from optparse import OptionGroup
 
 
@@ -112,7 +113,7 @@ def warranty ():
 
 %s
 %s
-''' % ( _ ('Copyright (c) %s by') % '2001--2010',
+''' % ( _ ('Copyright (c) %s by') % '2001--2011',
         '\n  '.join (authors),
         _ ("Distributed under terms of the GNU General Public License."),
         _ ("It comes with NO WARRANTY.")))
@@ -163,6 +164,11 @@ def get_option_parser ():
                   action='store', dest='lily_output_dir',
                   default=None)
 
+    p.add_option ('--load-custom-package', help=_ ("Load the additional python PACKAGE (containing e.g. a custom output format)"),
+                  metavar=_ ("PACKAGE"),
+                  action='append', dest='custom_packages',
+                  default=[])
+
     p.add_option ("-o", '--output', help=_ ("write output to DIR"),
                   metavar=_ ("DIR"),
                   action='store', dest='output_dir',
@@ -173,6 +179,16 @@ def get_option_parser ():
                   action='store',
                   dest='process_cmd', default='')
 
+    p.add_option ('--redirect-lilypond-output',
+                  help = _ ("Redirect the lilypond output"),
+                  action='store_true',
+                  dest='redirect_output', default=False)
+
+    p.add_option ('-s', '--safe', help=_ ("Compile snippets in safe mode"),
+                  action="store_true",
+                  default=False,
+                  dest="safe_mode")
+
     p.add_option ('--skip-lily-check',
                   help=_ ("do not fail if no lilypond output is found"),
                   metavar=_ ("DIR"),
@@ -211,6 +227,11 @@ case --pdf option is set instead of pdflatex"),
               metavar=_ ("PROG"),
               action='store', dest='latex_program',
               default='latex')
+    group.add_option ('--texinfo-program',
+              help=_ ("run executable PROG instead of texi2pdf"),
+              metavar=_ ("PROG"),
+              action='store', dest='texinfo_program',
+              default='texi2pdf')
     group.add_option ('--pdf',
               action="store_true",
               dest="create_pdf",
@@ -336,7 +357,7 @@ def find_toplevel_snippets (input_string, formatter):
 
     return snippets
 
-def system_in_directory (cmd, directory):
+def system_in_directory (cmd, directory, logfile):
     """Execute a command in a different directory.
 
     Because of win32 compatibility, we can't simply use subprocess.
@@ -344,7 +365,10 @@ def system_in_directory (cmd, directory):
 
     current = os.getcwd()
     os.chdir (directory)
-    ly.system(cmd, be_verbose=global_options.verbose,
+    ly.system(cmd,
+              be_verbose=global_options.verbose,
+              redirect_output=global_options.redirect_output,
+              log_file=logfile,
               progress_p=1)
     os.chdir (current)
 
@@ -363,11 +387,12 @@ def process_snippets (cmd, snippets,
                           + list (set ([snip.basename() + '.ly' for snip in snippets])))
     name = os.path.join (lily_output_dir,
                          'snippet-names-%d.ly' % checksum)
+    logfile = name.replace('.ly', '')
     file (name, 'wb').write (contents)
 
     system_in_directory (' '.join ([cmd, ly.mkarg (name)]),
-                         lily_output_dir)
-
+                         lily_output_dir,
+                         logfile)
 
 def snippet_list_checksum (snippets):
     return hash (' '.join([l.basename() for l in snippets]))
@@ -481,6 +506,7 @@ def samefile (f1, f2):
 
 def do_file (input_filename, included=False):
     # Ugh.
+    input_absname = input_filename
     if not input_filename or input_filename == '-':
         in_handle = sys.stdin
         input_fullname = '<stdin>'
@@ -490,7 +516,10 @@ def do_file (input_filename, included=False):
         else:
             input_fullname = global_options.formatter.input_fullname (input_filename)
         # Normalize path to absolute path, since we will change cwd to the output dir!
-        input_fullname = os.path.abspath (input_fullname)
+        # Otherwise, "lilypond-book -o out test.tex" will complain that it is
+        # overwriting the input file (which it is actually not), since the
+        # input filename is relative to the CWD...
+        input_absname = os.path.abspath (input_fullname)
 
         note_input_file (input_fullname)
         in_handle = file (input_fullname)
@@ -517,7 +546,7 @@ def do_file (input_filename, included=False):
                                    input_base + global_options.formatter.default_extension)
     if (os.path.exists (input_filename)
         and os.path.exists (output_filename)
-        and samefile (output_filename, input_fullname)):
+        and samefile (output_filename, input_absname)):
      error (
      _ ("Output would overwrite input file; use --output."))
      exit (2)
@@ -527,7 +556,8 @@ def do_file (input_filename, included=False):
         source = in_handle.read ()
         progress ('\n')
 
-        global_options.formatter.init_default_snippet_options (source)
+        if not included:
+            global_options.formatter.init_default_snippet_options (source)
 
 
         progress (_ ("Dissecting..."))
@@ -577,6 +607,14 @@ def do_options ():
 
     global_options.include_path =  map (os.path.abspath, global_options.include_path)
 
+    # Load the python packages (containing e.g. custom formatter classes)
+    # passed on the command line
+    nr = 0
+    for i in global_options.custom_packages:
+        nr += 1
+        print imp.load_source ("book_custom_package%s" % nr, i)
+
+
     if global_options.warranty:
         warranty ()
         exit (0)
@@ -636,6 +674,7 @@ def main ():
     else:
         global_options.lily_output_dir = os.path.abspath(global_options.output_dir)
 
+    relative_output_dir = global_options.output_dir
 
     identify ()
     try:
@@ -648,9 +687,8 @@ def main ():
 
     base_file_name = os.path.splitext (os.path.basename (files[0]))[0]
     dep_file = os.path.join (global_options.output_dir, base_file_name + '.dep')
-    final_output_file = os.path.join (global_options.output_dir,
-                     base_file_name
-                     + '.%s' % global_options.format)
+    final_output_file = os.path.join (relative_output_dir,
+                     base_file_name + global_options.formatter.default_extension)
 
     os.chdir (original_dir)
     file (dep_file, 'w').write ('%s: %s'