]> git.donarmstrong.com Git - lilypond.git/blobdiff - scripts/lilypond-book.py
web-es: link essay PDF.
[lilypond.git] / scripts / lilypond-book.py
index 392ddd061ebcbf3dabdda25cf7367fb787f32026..5dd263b6ac10bae988aa2ef86634c4749f2b4994 100644 (file)
@@ -176,11 +176,28 @@ def get_option_parser ():
                   type="float",
                   default=3.0)
 
+    p.add_option ('--lily-output-dir',
+                  help=_ ("write lily-XXX files to DIR, link into --output dir"),
+                  metavar=_ ("DIR"),
+                  action='store', dest='lily_output_dir',
+                  default=None)
+
     p.add_option ("-o", '--output', help=_ ("write output to DIR"),
                   metavar=_ ("DIR"),
                   action='store', dest='output_dir',
                   default='')
 
+    p.add_option ('--pdf',
+                  action="store_true",
+                  dest="create_pdf",
+                  help=_ ("create PDF files for use with PDFTeX"),
+                  default=False)
+
+    p.add_option ('-P', '--process', metavar=_ ("COMMAND"),
+                  help = _ ("process ly_files using COMMAND FILE..."),
+                  action='store',
+                  dest='process_cmd', default='')
+
     p.add_option ('--skip-lily-check',
                   help=_ ("do not fail if no lilypond output is found"),
                   metavar=_ ("DIR"),
@@ -193,21 +210,9 @@ def get_option_parser ():
                   action='store_true', dest='skip_png_check',
                   default=False)
 
-    p.add_option ('--lily-output-dir',
-                  help=_ ("write lily-XXX files to DIR, link into --output dir"),
-                  metavar=_ ("DIR"),
-                  action='store', dest='lily_output_dir',
-                  default=None)
-
-    p.add_option ('-P', '--process', metavar=_ ("COMMAND"),
-                  help = _ ("process ly_files using COMMAND FILE..."),
-                  action='store',
-                  dest='process_cmd', default='')
-
-    p.add_option ('--pdf',
-                  action="store_true",
-                  dest="create_pdf",
-                  help=_ ("create PDF files for use with PDFTeX"),
+    p.add_option ('--use-source-file-names',
+                  help=_ ("write snippet output files with the same base name as their source file"),
+                  action='store_true', dest='use_source_file_names',
                   default=False)
 
     p.add_option ('-V', '--verbose', help=_ ("be verbose"),
@@ -291,13 +296,19 @@ FILENAME = 'filename'
 ALT = 'alt'
 
 
-# NOTIME has no opposite so it isn't part of this dictionary.
+# NOTIME and NOGETTEXT have no opposite so they aren't part of this
+# dictionary.
 # NOQUOTE is used internally only.
 no_options = {
     NOFRAGMENT: FRAGMENT,
     NOINDENT: INDENT,
 }
 
+# Options that have no impact on processing by lilypond (or --process
+# argument)
+PROCESSING_INDEPENDENT_OPTIONS = (
+    ALT, NOGETTEXT, VERBATIM, ADDVERSION,
+    TEXIDOC, DOCTITLE, VERSION, PRINTFILENAME)
 
 # Recognize special sequences in the input.
 #
@@ -1142,6 +1153,18 @@ class LilypondSnippet (Snippet):
                     self.option_dict[LINE_WIDTH] = "#(- paper-width \
 left-margin-default right-margin-default)"
 
+    def get_option_list (self):
+        if not 'option_list' in self.__dict__:
+            option_list = []
+            for (key, value) in self.option_dict.items ():
+                if value == None:
+                    option_list.append (key)
+                else:
+                    option_list.append (key + '=' + value)
+            option_list.sort ()
+            self.option_list = option_list
+        return self.option_list
+
     def compose_ly (self, code):
         if FRAGMENT in self.option_dict:
             body = FRAGMENT_LY
@@ -1179,11 +1202,10 @@ left-margin-default right-margin-default)"
         override.update (default_ly_options)
 
         option_list = []
-        for (key, value) in self.option_dict.items ():
-            if value == None:
-                option_list.append (key)
-            else:
-                option_list.append (key + '=' + value)
+        for option in self.get_option_list ():
+            for name in PROCESSING_INDEPENDENT_OPTIONS:
+                if not option.startswith (name):
+                    option_list.append (option)
         option_string = ','.join (option_list)
 
         compose_dict = {}
@@ -1191,7 +1213,10 @@ left-margin-default right-margin-default)"
         for a in compose_types:
             compose_dict[a] = []
 
-        for (key, value) in self.option_dict.items ():
+        option_names = self.option_dict.keys ()
+        option_names.sort ()
+        for key in option_names:
+            value = self.option_dict[key]
             (c_key, c_value) = classic_lilypond_book_compatibility (key, value)
             if c_key:
                 if c_value:
@@ -1253,9 +1278,14 @@ left-margin-default right-margin-default)"
             except ImportError:
                 from md5 import md5
 
-            # We only want to calculate the hash based
-            # on the snippet code, not the snippet + preamble
+            # We only want to calculate the hash based on the snippet
+            # code plus fragment options relevant to processing by
+            # lilypond, not the snippet + preamble
             hash = md5 (self.relevant_contents (self.ly ()))
+            for option in self.get_option_list ():
+                for name in PROCESSING_INDEPENDENT_OPTIONS:
+                    if not option.startswith (name):
+                        hash.update (option)
 
             ## let's not create too long names.
             self.checksum = hash.hexdigest ()[:10]
@@ -1264,22 +1294,31 @@ left-margin-default right-margin-default)"
 
     def basename (self):
         cs = self.get_checksum ()
-        name = '%s/lily-%s' % (cs[:2], cs[2:10])
+        name = '%s/lily-%s' % (cs[:2], cs[2:])
         return name
 
+    final_basename = basename
+
     def write_ly (self):
         base = self.basename ()
         path = os.path.join (global_options.lily_output_dir, base)
         directory = os.path.split(path)[0]
         if not os.path.isdir (directory):
             os.makedirs (directory)
-        out = file (path + '.ly', 'w')
-        out.write (self.full_ly ())
-        file (path + '.txt', 'w').write ('image of music')
+        filename = path + '.ly'
+        if os.path.exists (filename):
+            diff_against_existing = filter_pipe (self.full_ly (), 'diff -u %s -' % filename)
+            if diff_against_existing:
+                warning ("%s: duplicate filename but different contents of orginal file,\n\
+printing diff against existing file." % filename)
+                ly.stderr_write (diff_against_existing)
+        else:
+            out = file (filename, 'w')
+            out.write (self.full_ly ())
+            file (path + '.txt', 'w').write ('image of music')
 
     def relevant_contents (self, ly):
-        return re.sub (r'\\(version|sourcefileline|sourcefilename)[^\n]*\n|' +
-                       NOGETTEXT + '[,\]]', '', ly)
+        return re.sub (r'\\(version|sourcefileline|sourcefilename)[^\n]*\n', '', ly)
 
     def link_all_output_files (self, output_dir, output_dir_files, destination):
         existing, missing = self.all_output_files (output_dir, output_dir_files)
@@ -1287,13 +1326,25 @@ left-margin-default right-margin-default)"
             print '\nMissing', missing
             raise CompileError(self.basename())
         for name in existing:
+            if (global_options.use_source_file_names
+                and isinstance (self, LilypondFileSnippet)):
+                base, ext = os.path.splitext (name)
+                components = base.split ('-')
+                # ugh, assume filenames with prefix with one dash (lily-xxxx)
+                if len (components) > 2:
+                    base_suffix = '-' + components[-1]
+                else:
+                    base_suffix = ''
+                final_name = self.final_basename () + base_suffix + ext
+            else:
+                final_name = name
             try:
-                os.unlink (os.path.join (destination, name))
+                os.unlink (os.path.join (destination, final_name))
             except OSError:
                 pass
 
             src = os.path.join (output_dir, name)
-            dst = os.path.join (destination, name)
+            dst = os.path.join (destination, final_name)
             dst_path = os.path.split(dst)[0]
             if not os.path.isdir (dst_path):
                 os.makedirs (dst_path)
@@ -1394,7 +1445,7 @@ left-margin-default right-margin-default)"
         return func (self)
 
     def get_images (self):
-        base = self.basename ()
+        base = self.final_basename ()
 
         single = '%(base)s.png' % vars ()
         multiple = '%(base)s-page1.png' % vars ()
@@ -1411,7 +1462,7 @@ left-margin-default right-margin-default)"
 
     def output_docbook (self):
         str = ''
-        base = self.basename ()
+        base = self.final_basename ()
         for image in self.get_images ():
             (base, ext) = os.path.splitext (image)
             str += output[DOCBOOK][OUTPUT] % vars ()
@@ -1427,7 +1478,7 @@ left-margin-default right-margin-default)"
 
     def output_html (self):
         str = ''
-        base = self.basename ()
+        base = self.final_basename ()
         if self.format == HTML:
             str += self.output_print_filename (HTML)
             if VERBATIM in self.option_dict:
@@ -1456,13 +1507,13 @@ left-margin-default right-margin-default)"
             info_image_path = os.path.join (global_options.info_images_dir, base)
             str += output[TEXINFO][OUTPUTIMAGE] % vars ()
 
-        base = self.basename ()
+        base = self.final_basename ()
         str += output[self.format][OUTPUT] % vars ()
         return str
 
     def output_latex (self):
         str = ''
-        base = self.basename ()
+        base = self.final_basename ()
         if self.format == LATEX:
             str += self.output_print_filename (LATEX)
             if VERBATIM in self.option_dict:
@@ -1483,7 +1534,7 @@ left-margin-default right-margin-default)"
     def output_print_filename (self, format):
         str = ''
         if PRINTFILENAME in self.option_dict:
-            base = self.basename ()
+            base = self.final_basename ()
             filename = os.path.basename (self.substring ('filename'))
             str = output[format][PRINTFILENAME] % vars ()
 
@@ -1491,7 +1542,7 @@ left-margin-default right-margin-default)"
 
     def output_texinfo (self):
         str = self.output_print_filename (TEXINFO)
-        base = self.basename ()
+        base = self.final_basename ()
         if DOCTITLE in self.option_dict:
             doctitle = base + '.doctitle'
             translated_doctitle = doctitle + document_language
@@ -1554,6 +1605,13 @@ class LilypondFileSnippet (LilypondSnippet):
         return ('\\sourcefilename \"%s\"\n\\sourcefileline 0\n%s'
                 % (name, self.contents))
 
+    def final_basename (self):
+        if global_options.use_source_file_names:
+            base = os.path.splitext (os.path.basename (self.substring ('filename')))[0]
+            return base
+        else:
+            return self.basename ()
+
 
 class LilyPondVersionString (Snippet):
     """A string that does not require extra memory."""
@@ -1722,7 +1780,7 @@ def process_snippets (cmd, snippets,
 
     checksum = snippet_list_checksum (snippets)
     contents = '\n'.join (['snippet-map-%d.ly' % checksum]
-                          + [snip.basename() + '.ly' for snip in snippets])
+                          + list (set ([snip.basename() + '.ly' for snip in snippets])))
     name = os.path.join (lily_output_dir,
                          'snippet-names-%d.ly' % checksum)
     file (name, 'wb').write (contents)