]> git.donarmstrong.com Git - lilypond.git/blobdiff - python/book_snippets.py
Issue 5167/6: Changes: show \markup xxx = ... \etc assignments
[lilypond.git] / python / book_snippets.py
index e3fc2eff950ff6034ca41296ea7488ca192f0942..4bc4252e79da3bad9cb15876eb2f4218a4f24d2f 100644 (file)
@@ -7,11 +7,8 @@ import re
 import os
 import copy
 import shutil
-# TODO: We are using os.popen3, which has been deprecated since python 2.6. The
-# suggested replacement is the Popen function of the subprocess module.
-# Unfortunately, on windows this needs the msvcrt module, which doesn't seem
-# to be available in GUB?!?!?!
-# from subprocess import Popen, PIPE
+import subprocess
+import sys
 
 progress = ly.progress
 warning = ly.warning
@@ -109,11 +106,19 @@ snippet_options = {
     },
 
     ##
+    # TODO: Remove the 1mm additional padding in the line-width
+    #       once lilypond creates tighter cropped images!
     PAPER: {
         PAPERSIZE: r'''#(set-paper-size "%(papersize)s")''',
         INDENT: r'''indent = %(indent)s''',
-        LINE_WIDTH: r'''line-width = %(line-width)s''',
-        QUOTE: r'''line-width = %(line-width)s - 2.0 * %(exampleindent)s''',
+        LINE_WIDTH: r'''line-width = %(line-width)s
+  %% offset the left padding, also add 1mm as lilypond creates cropped
+  %% images with a little space on the right
+  line-width = #(- line-width (* mm  %(padding_mm)f) (* mm 1))''',
+        QUOTE: r'''line-width = %(line-width)s - 2.0 * %(exampleindent)s
+  %% offset the left padding, also add 1mm as lilypond creates cropped
+  %% images with a little space on the right
+  line-width = #(- line-width (* mm  %(padding_mm)f) (* mm 1))''',
         RAGGED_RIGHT: r'''ragged-right = ##t''',
         NORAGGED_RIGHT: r'''ragged-right = ##f''',
     },
@@ -164,8 +169,6 @@ def classic_lilypond_book_compatibility (key, value):
     return (None, None)
 
 
-# TODO: Remove the 1mm additional padding in the line-width, once lilypond
-#       creates tighter cropped images!
 PREAMBLE_LY = '''%%%% Generated by %(program_name)s
 %%%% Options: [%(option_string)s]
 \\include "lilypond-book-preamble.ly"
@@ -179,9 +182,6 @@ PREAMBLE_LY = '''%%%% Generated by %(program_name)s
 
 \paper {
   %(paper_string)s
-  %% offset the left padding, also add 1mm as lilypond creates cropped
-  %% images with a little space on the right
-  line-width = #(- line-width (* mm  %(padding_mm)f) (* mm 1))
 }
 
 \layout {
@@ -329,6 +329,8 @@ class IncludeSnippet (Snippet):
 class LilypondSnippet (Snippet):
     def __init__ (self, type, match, formatter, line_number, global_options):
         Snippet.__init__ (self, type, match, formatter, line_number, global_options)
+        self.filename = ''
+        self.ext = '.ly'
         os = match.group ('options')
         self.parse_snippet_options (os, self.type)
 
@@ -498,6 +500,7 @@ class LilypondSnippet (Snippet):
         override[EXAMPLEINDENT] = r'0.4\in'
         override[LINE_WIDTH] = '5\\in'
         override.update (self.formatter.default_snippet_options)
+        override['padding_mm'] = self.global_options.padding_mm
 
         option_string = ','.join (self.get_outputrelevant_option_strings ())
         compose_dict = {}
@@ -601,7 +604,7 @@ class LilypondSnippet (Snippet):
             existing = open (filename, 'r').read ()
 
             if self.relevant_contents (existing) != self.relevant_contents (self.full_ly ()):
-                warning ("%s: duplicate filename but different contents of orginal file,\n\
+                warning ("%s: duplicate filename but different contents of original file,\n\
 printing diff against existing file." % filename)
                 ly.stderr_write (self.filter_pipe (self.full_ly (), 'diff -u %s -' % filename))
         else:
@@ -615,7 +618,7 @@ printing diff against existing file." % filename)
     def link_all_output_files (self, output_dir, output_dir_files, destination):
         existing, missing = self.all_output_files (output_dir, output_dir_files)
         if missing:
-            print '\nMissing', missing
+            error (_ ('Missing files: %s') % ', '.join (missing))
             raise CompileError(self.basename())
         for name in existing:
             if (self.global_options.use_source_file_names
@@ -641,17 +644,30 @@ printing diff against existing file." % filename)
             if not os.path.isdir (dst_path):
                 os.makedirs (dst_path)
             try:
-                os.link (src, dst)
-            except AttributeError:
-                shutil.copyfile (src, dst)
-            except OSError:
-                print '\nCould not overwrite file', dst
+                if (self.global_options.use_source_file_names
+                        and isinstance (self, LilypondFileSnippet)):
+                    fout = open (dst, 'w')
+                    fin = open (src, 'r')
+                    for line in fin.readlines ():
+                        fout.write (line.replace (self.basename (), self.final_basename ()))
+                    fout.close ()
+                    fin.close ()
+                else:
+                    try:
+                        os.link (src, dst)
+                    except AttributeError:
+                        shutil.copyfile (src, dst)
+            except (IOError, OSError):
+                error (_ ('Could not overwrite file %s') % dst)
                 raise CompileError(self.basename())
 
     def additional_files_to_consider (self, base, full):
         return []
     def additional_files_required (self, base, full):
-        return []
+        result = [];
+        if self.ext != '.ly':
+            result.append (base + self.ext)
+        return result
 
 
     def all_output_files (self, output_dir, output_dir_files):
@@ -688,6 +704,7 @@ printing diff against existing file." % filename)
 
         map (consider_file, [base + '.tex',
                              base + '.eps',
+                             base + '.pdf',
                              base + '.texidoc',
                              base + '.doctitle',
                              base + '-systems.texi',
@@ -730,10 +747,11 @@ printing diff against existing file." % filename)
 
         debug (_ ("Running through filter `%s'") % cmd, True)
 
-        # TODO: Use Popen once we resolve the problem with msvcrt in Windows:
-        (stdin, stdout, stderr) = os.popen3 (cmd)
-        # p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)
-        # (stdin, stdout, stderr) = (p.stdin, p.stdout, p.stderr)
+        closefds = True
+        if (sys.platform == "mingw32"):
+            closefds = False
+        p = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=closefds)
+        (stdin, stdout, stderr) = (p.stdin, p.stdout, p.stderr)
         stdin.write (input)
         status = stdin.close ()
 
@@ -803,8 +821,8 @@ class LilypondFileSnippet (LilypondSnippet):
     def __init__ (self, type, match, formatter, line_number, global_options):
         LilypondSnippet.__init__ (self, type, match, formatter, line_number, global_options)
         self.filename = self.substring ('filename')
-        self.ext = os.path.splitext (os.path.basename (self.filename))[1]
-        self.contents = file (BookBase.find_file (self.filename, global_options.include_path)).read ()
+        self.contents = file (BookBase.find_file (self.filename,
+            global_options.include_path, global_options.original_dir)).read ()
 
     def get_snippet_code (self):
         return self.contents;
@@ -837,6 +855,7 @@ class MusicXMLFileSnippet (LilypondFileSnippet):
         LilypondFileSnippet.__init__ (self, type, match, formatter, line_number, global_options)
         self.compressed = False
         self.converted_ly = None
+        self.ext = os.path.splitext (os.path.basename (self.filename))[1]
         self.musicxml_options_dict = {
            'verbose': '--verbose',
            'lxml': '--lxml',
@@ -880,14 +899,6 @@ class MusicXMLFileSnippet (LilypondFileSnippet):
         return ('\\sourcefilename \"%s\"\n\\sourcefileline 0\n%s'
                 % (name, self.converted_ly))
 
-    def additional_files_required (self, base, full):
-        result = [];
-        if self.compressed:
-            result.append (base + '.mxl')
-        else:
-            result.append (base + '.xml')
-        return result
-
     def write_ly (self):
         base = self.basename ()
         path = os.path.join (self.global_options.lily_output_dir, base)
@@ -903,7 +914,7 @@ class MusicXMLFileSnippet (LilypondFileSnippet):
         if os.path.exists (xmlfilename):
             diff_against_existing = self.filter_pipe (self.contents, 'diff -u %s - ' % xmlfilename)
             if diff_against_existing:
-                warning (_ ("%s: duplicate filename but different contents of orginal file,\n\
+                warning (_ ("%s: duplicate filename but different contents of original file,\n\
 printing diff against existing file.") % xmlfilename)
                 ly.stderr_write (diff_against_existing)
         else: