rename directories and files on upgrades from before 0.8.4
authorgc <gc>
Sat, 19 Nov 2005 14:23:26 +0000 (14:23 +0000)
committergc <gc>
Sat, 19 Nov 2005 14:23:26 +0000 (14:23 +0000)
bin/booh
lib/booh/booh-lib.rb

index 747a53e..fe30d52 100755 (executable)
--- a/bin/booh
+++ b/bin/booh
@@ -2366,6 +2366,30 @@ def open_file(filename)
     if $xmldoc.root.attributes['version'] != $VERSION
         msg 2, _("File's version %s, booh version now %s, marking dirty") % [ $xmldoc.root.attributes['version'], $VERSION ]
         mark_document_as_dirty
+        if $xmldoc.root.attributes['version'] < '0.8.4'
+            msg 1, _("File's version prior to 0.8.4, migrating directories and filenames in destination directory if needed")
+            `find '#{source}' -type d -follow`.sort.collect { |v| v.chomp }.each { |dir|
+                old_dest_dir = make_dest_filename_old(dir.sub(/^#{Regexp.quote(source)}/, dest))
+                new_dest_dir = make_dest_filename(dir.sub(/^#{Regexp.quote(source)}/, dest))
+                if old_dest_dir != new_dest_dir
+                    sys("mv '#{old_dest_dir}' '#{new_dest_dir}'")
+                end
+                if xmldir = $xmldoc.elements["//dir[@path='#{utf8(dir)}']"]
+                    xmldir.elements.each { |element|
+                        if %w(image video).include?(element.name) && !element.attributes['deleted']
+                            old_name = old_dest_dir + '/' + make_dest_filename_old(from_utf8(element.attributes['filename'])).sub(/\.[^\.]+$/, '')
+                            new_name = new_dest_dir + '/' + make_dest_filename(from_utf8(element.attributes['filename'])).sub(/\.[^\.]+$/, '')
+                            Dir[old_name + '*'].each { |file|
+                                new_file = file.sub(/^#{Regexp.quote(old_name)}/, new_name)
+                                file != new_file and sys("mv '#{file}' '#{new_file}'")
+                            }
+                        end
+                    }
+                else
+                    msg 1, "could not find <dir> element by path '#{utf8(dir)}' in xml file"
+                end
+            }
+        end
         $xmldoc.root.add_attribute('version', $VERSION)
     end
 
index 991417a..1dc435e 100644 (file)
@@ -59,11 +59,18 @@ module Booh
         return Iconv::iconv($CURRENT_CHARSET, "UTF-8", string).to_s
     end
 
+    def make_dest_filename_old(orig_filename)
+        #- we remove non alphanumeric characters but need to do that
+        #- cleverly to not end up with two similar dest filenames. we won't
+        #- urlencode because urldecode might happen in the browser.
+        return orig_filename.unpack("C*").collect { |v| v.chr =~ /[a-zA-Z\-_0-9\.\/]/ ? v.chr : sprintf("%2X", v) }.to_s
+    end
+
     def make_dest_filename(orig_filename)
         #- we remove non alphanumeric characters but need to do that
         #- cleverly to not end up with two similar dest filenames. we won't
         #- urlencode because urldecode might happen in the browser.
-        return orig_filename.unpack("C*").collect { |v| v.chr =~ /[a-zA-Z\-0-9\.\/]/ ? v.chr : sprintf("_%02X", v) }.to_s
+        return orig_filename.unpack("C*").collect { |v| v.chr =~ /[a-zA-Z\-_0-9\.\/]/ ? v.chr : sprintf("~%02X", v) }.to_s
     end
 
     def msg(verbose_level, msg)