]> git.donarmstrong.com Git - lilypond.git/commitdiff
Issue 3572: convert-ly should produce several backup files for each invokation
authorDavid Kastrup <dak@gnu.org>
Fri, 4 Oct 2013 15:55:24 +0000 (17:55 +0200)
committerDavid Kastrup <dak@gnu.org>
Fri, 11 Oct 2013 11:43:44 +0000 (13:43 +0200)
This supports and documents the -b option for numbered backups.  This
was actually written primarily by Eluze.

Documentation/usage/updating.itely
scripts/convert-ly.py

index a32489c39dfa5f51aa8da079a6abd8e85e4d682f..06a960ac8a866faff27da9fece4208a96d395a78 100644 (file)
@@ -156,7 +156,13 @@ the version will reflect the last @emph{attempted} conversion.
 
 @item -e, --edit
 Apply the conversions direct to the input file, modifying it
-in-place.
+in-place.  The original file is renamed as @file{myfile.ly~}.  This
+backup file may be a hidden file on some operating systems.
+
+@item -b, --backup-numbered
+When used with the @samp{-e} option, number the backup files so that
+no previous version is overwritten.  The backup files may be hidden
+on some operating systems.
 
 @item -f, --from=@var{from-patchlevel}
 Set the version to convert from.  If this is not set, @command{convert-ly}
index f3304315428bcce5811b620a656c7d226f728495..641d763713c979c06dfe0621e19959303765403d 100644 (file)
@@ -24,6 +24,7 @@
 import os
 import sys
 import re
+import shutil
 
 """
 @relocate-preamble@
@@ -129,18 +130,25 @@ def get_option_parser ():
               action='store_true',
               dest='diff_version_update',
               default=False)
-    
+
     p.add_option ("-s", '--show-rules',
               help=_ ("show rules [default: -f 0, -t %s]") % program_version,
               dest='show_rules',
               action='store_true', default=False)
-    
+
     p.add_option ('-t', '--to',
               help=_ ("convert to VERSION [default: %s]") % program_version,
               metavar=_ ('VERSION'),
               action='store',
               dest="to_version",
               default='')
+
+    p.add_option ('-b', '--backup-numbered',
+              help=_ ("make a numbered backup [default: filename.ext~]"),
+              action='store_true',
+              dest="backup_numbered",
+              default='')
+
     p.add_option ('-w', '--warranty', help=_ ("show warranty and copyright"),
            action='store_true',
            ),
@@ -152,8 +160,6 @@ def get_option_parser ():
     
     return p
 
-
-
 def str_to_tuple (s):
     return tuple ([int(n) for n in s.split ('.')])
 
@@ -213,8 +219,6 @@ string and the number of errors."""
 
     return (last_conversion, last_change, str, errors)
 
-
-
 def guess_lilypond_version (input):
     m = lilypond_version_strict_re.search (input)
     if m:
@@ -235,6 +239,19 @@ class InvalidVersion (Exception):
     def __init__ (self, version):
       self.version = version
 
+def back_up (file, numbered):
+    if numbered:
+      n = 0
+      while True:
+        n = n + 1
+        back_up = file + '.~' + str(n) + '~'
+        if not os.path.exists (back_up):
+            break
+    else:
+      back_up = file + '~'
+    shutil.copy2 (file, back_up)
+    return back_up
+
 def do_one_file (infile_name):
     ly.progress (_ (u"Processing `%s\'... ") % infile_name, True)
 
@@ -297,11 +314,7 @@ def do_one_file (infile_name):
     ly.progress ('\n')
 
     if global_options.edit:
-        try:
-            os.remove (infile_name + '~')
-        except:
-            pass
-        os.rename (infile_name, infile_name + '~')
+        backup = back_up (infile_name, global_options.backup_numbered)
         outfile = open (infile_name, 'w')
     else:
         outfile = sys.stdout
@@ -373,5 +386,4 @@ def main ():
             "There were %d errors.", errors) % errors)
         sys.exit (1)
 
-
 main ()