]> git.donarmstrong.com Git - lilypond.git/blobdiff - scripts/auxiliar/makelsr.py
Correct makelsr handling of local update and tags
[lilypond.git] / scripts / auxiliar / makelsr.py
index 61526963bed97875e7b1bb1fb4a61bdc678a3d46..24dd0d900f2c12f5cca630571fd2cf676e8a43bd 100755 (executable)
@@ -13,15 +13,17 @@ lys_from_lsr = os.path.join ('Documentation', 'snippets')
 new_lys = os.path.join ('Documentation', 'snippets', 'new')
 ly_output = os.path.join (tempfile.gettempdir (), 'lsrtest')
 
-# which convert-ly to use
-if os.path.isfile ("out/bin/convert-ly"):
-    conv_path = "out/bin/"
+# which convert-ly and lilypond to use
+P = os.path.join (os.environ.get ("LILYPOND_BUILD_DIR", ""),
+                  "out/bin/convert-ly")
+if os.path.isfile (P):
+    conv_path = os.path.dirname (P)
 elif os.path.isfile ("build/out/bin/convert-ly"):
     conv_path = "build/out/bin/"
 else:
     conv_path=''
-convert_ly = conv_path + 'convert-ly'
-lilypond_bin = conv_path + 'lilypond'
+convert_ly = os.path.join (conv_path, 'convert-ly')
+lilypond_bin = os.path.join (conv_path, 'lilypond')
 
 
 
@@ -76,7 +78,7 @@ options_parser.add_option ('-p', '--path',
                            dest="bin_path",
                            action="store",
                            metavar="LY_PATH",
-                           default="out/bin",
+                           default=conv_path,
                            help="directory where looking for LilyPond binaries")
 
 options_parser.add_option ('-c', '--convert-ly',
@@ -147,6 +149,7 @@ def exit_with_usage (n=0):
     options_parser.print_help (sys.stderr)
     sys.exit (n)
 
+tags=[]
 if len (args):
     in_dir = args[0]
     if not (os.path.isdir (in_dir)):
@@ -154,6 +157,9 @@ if len (args):
         sys.exit (4)
     if len (args) > 1:
         exit_with_usage (2)
+    tags = os.listdir (in_dir)
+    ## Make sure all users get the same ordering of tags
+    tags.sort()
 else:
     in_dir = '.'
 
@@ -161,20 +167,18 @@ if options.convert_ly == "LY_PATH/convert-ly":
     convert_ly = os.path.join (options.bin_path, "convert-ly")
 else:
     convert_ly = options.convert_ly
-if not os.path.isfile (convert_ly):
-    sys.stderr.write ("Warning: %s: no such file")
+if not os.path.exists (convert_ly):
+    sys.stderr.write ("Warning: %s: no such file\n" % convert_ly)
     convert_ly = "convert-ly"
 if options.lilypond_bin == "LY_PATH/lilypond":
     lilypond_bin = os.path.join (options.bin_path, "lilypond")
 else:
     lilypond_bin = options.lilypond_bin
-if not os.path.isfile (lilypond_bin):
-    sys.stderr.write ("Warning: %s: no such file")
+if not os.path.exists (lilypond_bin):
+    sys.stderr.write ("Warning: %s: no such file\n" % lilypond_bin)
     lilypond_bin = "lilypond"
 sys.stderr.write ("Using %s, %s\n" % (convert_ly, lilypond_bin))
 
-tags = os.listdir (in_dir)
-
 unsafe = []
 unconverted = []
 notags_files = []
@@ -200,6 +204,7 @@ lsr_comment_re = re.compile (r'\s*%+\s*LSR.*')
 begin_header_re = re.compile (r'\\header\s*{', re.M)
 ly_new_version_re = re.compile (r'\\version\s*"(.+?)"')
 strip_white_spaces_re = re.compile (r'[ \t]+(?=\n)')
+final_empty_lines_re = re.compile (r'\n{2,}$')
 
 # add tags to ly files from LSR
 def add_tags (ly_code, tags):
@@ -242,6 +247,7 @@ def copy_ly (srcdir, name, tags):
     s = mark_verbatim_section (s)
     s = lsr_comment_re.sub ('', s)
     s = strip_white_spaces_re.sub ('', s)
+    s = final_empty_lines_re.sub ('\n', s)
     s = escape_backslashes_in_header (s)
     sys.stderr.write ("makelsr.py: writing %s\n" % dest)
     open (dest, 'w').write (s)
@@ -301,9 +307,13 @@ def dump_file_list (file, file_list, update=False):
     f = open (file, 'w')
     f.write ('\n'.join (sorted (new_list)) + '\n')
 
-## clean out existing lys and generated files
-map (os.remove, glob.glob (os.path.join (lys_from_lsr, '*.ly')) +
-     glob.glob (os.path.join (lys_from_lsr, '*.snippet-list')))
+## clean out existing lys and generated files - but only when we're
+## completely recreating them from the tarball.  Otherwise
+## tags will be empty and so we can use this to skip this step
+
+if len(tags) > 0:
+    map (os.remove, glob.glob (os.path.join (lys_from_lsr, '*.ly')) +
+        glob.glob (os.path.join (lys_from_lsr, '*.snippet-list')))
 
 # read LSR source where tags are defined by subdirs
 snippets, tag_lists = read_source_with_dirs (in_dir)
@@ -314,8 +324,8 @@ snippets.update (s)
 for t in tags:
     tag_lists[t].update (l[t])
 
-for (name, (srcdir, tags)) in snippets.items ():
-    copy_ly (srcdir, name, tags)
+for (name, (srcdir, file_tags)) in snippets.items ():
+    copy_ly (srcdir, name, file_tags)
 for (tag, file_set) in tag_lists.items ():
     dump_file_list (os.path.join (lys_from_lsr, tag + '.snippet-list'),
                     file_set, update=not(in_dir))