changeset 4440:847d257bee5f

twikidraw mig script: use .tdraw for the tar file, get rid of .gif
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Mon, 29 Dec 2008 22:12:07 +0100
parents ec62380ae400
children 39109af9ac90
files MoinMoin/script/migration/1089999.py
diffstat 1 files changed, 21 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/script/migration/1089999.py	Mon Dec 29 02:09:32 2008 +0100
+++ b/MoinMoin/script/migration/1089999.py	Mon Dec 29 22:12:07 2008 +0100
@@ -4,7 +4,7 @@
 
     This script looks at all pages' attachments and checks whether they
     contain TWikiDraw items. If this is the case, the TWikiDraw items are
-    transformed into a single tarfile on the disk.
+    bundled into a single tar file (.tdraw) on the disk.
     (The intention is to later store this as a single Item in the new storage
     layer.)
 
@@ -13,9 +13,9 @@
 
 """
 
-import os
-import tarfile
+import os, errno
 
+from MoinMoin.support import tarfile
 from MoinMoin.action.AttachFile import getAttachDir
 
 
@@ -25,28 +25,27 @@
     for pagename in pagenames:
         attachdir = getAttachDir(script.request, pagename)
         try:
-           filenames = os.listdir(attachdir)
+            drawings = [fn for fn in os.listdir(attachdir) if fn.endswith('.draw')]
         except OSError:
             # silenced. attachment directory does not exist. proceed with next page
             continue
-        for filename in filenames:
-            if filename.endswith('.draw'):
-                filename = filename.split('.')[0]  # XXX only 1 dot in filename allowed atm
-                # Open for uncompressed writing
-                os.chdir(attachdir)  # XXX Do we really need to catch permission errors here?
-                tar = tarfile.open(filename + '.tar', 'w:')  # XXX same question here.
-                tar.add(filename + '.draw')
-                os.remove(filename + '.draw')  # XXX and here
+        for drawing in drawings:
+            basename = os.path.splitext(drawing)[0]
+            tar_filename = os.path.join(attachdir, basename + '.tdraw')
+            tar = tarfile.open(tar_filename, 'w:')
+            for ext in ['.draw', '.map', '.png', '.gif', ]:
+                filename = os.path.join(attachdir, basename + ext)
                 try:
-                    tar.add(filename + '.map')
-                    os.remove(filename + '.map')  # XXX and here
-                except OSError:
-                    # The .map file is optional
-                    pass
-
-                # TODO Decide whether png files shall be included in the
-                # TODO tar file and take appropriate action.
-
-                tar.close()
+                    if ext != '.gif':
+                        # get rid of the gif (TWikiDraw will (re)create
+                        # a .png when someone edits the drawing)
+                        tar.add(filename, basename + ext)
+                    os.remove(filename)
+                except OSError, err:
+                    if err.errno != errno.ENOENT:
+                        # .map and .png are optional, .draw should be there
+                        raise
+            tar.close()
 
     return 1090000
+