]> git.openstreetmap.org Git - osqa.git/blobdiff - forum_modules/exporter/exporter.py
#OSQA-574, a bit of backwards compatibility, importing the with_statements from the...
[osqa.git] / forum_modules / exporter / exporter.py
index 725a310e4dd04820c8709135e1f94a783b4f20b3..49f89da34941e75f7e80f90e1064801846b29446 100644 (file)
@@ -1,4 +1,6 @@
-import os, tarfile, datetime, logging, re, ConfigParser, shutil
+from __future__ import with_statement
+
+import os, tarfile, datetime, logging, re, ConfigParser, shutil, zipfile
 
 from django.core.cache import cache
 from django.utils.translation import ugettext as _
 
 from django.core.cache import cache
 from django.utils.translation import ugettext as _
@@ -16,7 +18,6 @@ CACHE_KEY = "%s_exporter_state" % APP_URL
 EXPORT_STEPS = []
 
 TMP_FOLDER = os.path.join(os.path.dirname(__file__), 'tmp')
 EXPORT_STEPS = []
 
 TMP_FOLDER = os.path.join(os.path.dirname(__file__), 'tmp')
-LAST_BACKUP = os.path.join(TMP_FOLDER, 'backup.tar.gz')
 
 DATE_AND_AUTHOR_INF_SECTION = 'DateAndAuthor'
 OPTIONS_INF_SECTION = 'Options'
 
 DATE_AND_AUTHOR_INF_SECTION = 'DateAndAuthor'
 OPTIONS_INF_SECTION = 'Options'
@@ -65,7 +66,7 @@ def Etree_pretty__write(self, file, node, encoding, namespaces,
         if node.text or len(node):
             file.write(">")
             if node.text:
         if node.text or len(node):
             file.write(">")
             if node.text:
-                file.write(_escape_cdata(node.text.replace("\n", (level + 1) * identator + "\n"), encoding))
+                file.write(_escape_cdata(node.text, encoding))
             for n in node:
                 self._write(file, n, encoding, namespaces, level + 1, identator)
             if node.text and len(node.text) < 125:
             for n in node:
                 self._write(file, n, encoding, namespaces, level + 1, identator)
             if node.text and len(node.text) < 125:
@@ -77,7 +78,7 @@ def Etree_pretty__write(self, file, node, encoding, namespaces,
         for k, v in xmlns_items:
             del namespaces[v]
     if node.tail:
         for k, v in xmlns_items:
             del namespaces[v]
     if node.tail:
-        file.write(_escape_cdata(node.tail.replace("\n", level * identator + "\n"), encoding))
+        file.write(_escape_cdata(node.tail.replace("\n", (level * identator )+ "\n"), encoding))
 
 def make_date(date, with_time=True):
     try:
 
 def make_date(date, with_time=True):
     try:
@@ -90,7 +91,10 @@ def ET_Element_add_tag(el, tag_name, content = None, **attrs):
     tag = ET.SubElement(el, tag_name)
 
     if content:
     tag = ET.SubElement(el, tag_name)
 
     if content:
-        tag.text = unicode(content)
+        try:
+            tag.text = unicode(content)
+        except:
+            tag.text = u''
 
     for k, v in attrs.items():
         tag.set(k, unicode(v))
 
     for k, v in attrs.items():
         tag.set(k, unicode(v))
@@ -130,17 +134,36 @@ def write_to_file(root, tmp, filename):
     tree = ET.ElementTree(root)
     tree.write(os.path.join(tmp, filename), encoding='UTF-8')
 
     tree = ET.ElementTree(root)
     tree.write(os.path.join(tmp, filename), encoding='UTF-8')
 
-def create_targz(tmp, files, start_time, options, user, state, set_state):
-    if os.path.exists(LAST_BACKUP):
-        os.remove(LAST_BACKUP)
-        
-    t = tarfile.open(name=LAST_BACKUP, mode = 'w:gz')
+def create_targz(tmp, files, start_time, options, user, state, set_state, file_format):
+    now = datetime.datetime.now()
+    domain = re.match('[\w-]+\.[\w-]+(\.[\w-]+)*', djsettings.APP_URL)
+    if domain:
+        domain = '_'.join(domain.get(0).split('.'))
+    else:
+        domain = 'localhost'
+
+    fname = "%s-%s" % (domain, now.strftime('%Y%m%d%H%M'))
+    if file_format == 'zip':
+        full_fname = "%s.zip" % fname
+    else:
+        full_fname = "%s.tar.gz" % fname
+
+    if file_format == 'zip':
+        t = zipfile.ZipFile(os.path.join(selfsettings.EXPORTER_BACKUP_STORAGE, full_fname), 'w')
+
+        def add_to_file(f, a):
+            t.write(f, a)
+    else:
+        t = tarfile.open(os.path.join(selfsettings.EXPORTER_BACKUP_STORAGE, full_fname), mode = 'w:gz')
+
+        def add_to_file(f, a):
+            t.add(f, a)
 
     state['overall']['status'] = _('Compressing xml files')
     set_state()
 
     for f in files:
 
     state['overall']['status'] = _('Compressing xml files')
     set_state()
 
     for f in files:
-        t.add(os.path.join(tmp, f), arcname="/%s" % f)
+        add_to_file(os.path.join(tmp, f), "/%s" % f)
 
     if options.get('uplodaded_files', False):
         state['overall']['status'] = _('Importing uploaded files')
 
     if options.get('uplodaded_files', False):
         state['overall']['status'] = _('Importing uploaded files')
@@ -155,20 +178,11 @@ def create_targz(tmp, files, start_time, options, user, state, set_state):
     state['overall']['status'] = _('Writing inf file.')
     set_state()
 
     state['overall']['status'] = _('Writing inf file.')
     set_state()
 
-    now = datetime.datetime.now()
-    domain = re.match('[\w-]+\.[\w-]+(\.[\w-]+)*', djsettings.APP_URL)
-    if domain:
-        domain = '_'.join(domain.get(0).split('.'))
-    else:
-        domain = 'localhost'
-
-    fname = "%s-%s" % (domain, now.strftime('%Y%m%d%H%M'))
-
     inf = ConfigParser.SafeConfigParser()
 
     inf.add_section(DATE_AND_AUTHOR_INF_SECTION)
 
     inf = ConfigParser.SafeConfigParser()
 
     inf.add_section(DATE_AND_AUTHOR_INF_SECTION)
 
-    inf.set(DATE_AND_AUTHOR_INF_SECTION, 'file-name', "%s.tar.gz" % fname)
+    inf.set(DATE_AND_AUTHOR_INF_SECTION, 'file-name', full_fname)
     inf.set(DATE_AND_AUTHOR_INF_SECTION, 'author', unicode(user.id))
     inf.set(DATE_AND_AUTHOR_INF_SECTION, 'site', djsettings.APP_URL)
     inf.set(DATE_AND_AUTHOR_INF_SECTION, 'started', start_time.strftime(DATETIME_FORMAT))
     inf.set(DATE_AND_AUTHOR_INF_SECTION, 'author', unicode(user.id))
     inf.set(DATE_AND_AUTHOR_INF_SECTION, 'site', djsettings.APP_URL)
     inf.set(DATE_AND_AUTHOR_INF_SECTION, 'started', start_time.strftime(DATETIME_FORMAT))
@@ -184,17 +198,15 @@ def create_targz(tmp, files, start_time, options, user, state, set_state):
     for id, s in state.items():
         inf.set(META_INF_SECTION, id, str(s['count']))
 
     for id, s in state.items():
         inf.set(META_INF_SECTION, id, str(s['count']))
 
-    with open(os.path.join(tmp, 'backup.inf'), 'wb') as inffile:
+    with open(os.path.join(tmp, '%s.backup.inf' % fname), 'wb') as inffile:
         inf.write(inffile)
 
         inf.write(inffile)
 
-    t.add(os.path.join(tmp, 'backup.inf'), arcname='backup.inf')
+    add_to_file(os.path.join(tmp, '%s.backup.inf' % fname), '/backup.inf')
     state['overall']['status'] = _('Saving backup file')
     set_state()
     t.close()
     state['overall']['status'] = _('Saving backup file')
     set_state()
     t.close()
-    shutil.copyfile(LAST_BACKUP, os.path.join(selfsettings.EXPORTER_BACKUP_STORAGE, "%s.tar.gz" % fname))
-    shutil.copyfile(os.path.join(tmp, 'backup.inf'), os.path.join(selfsettings.EXPORTER_BACKUP_STORAGE, "%s.backup.inf" % fname))
+    return full_fname
 
 
-    
 
 def export_upfiles(tf):
     folder = str(settings.UPFILES_FOLDER)
 
 def export_upfiles(tf):
     folder = str(settings.UPFILES_FOLDER)
@@ -272,10 +284,11 @@ def export(options, user):
         state['overall']['status'] = _('Compressing files')
         set_state()
 
         state['overall']['status'] = _('Compressing files')
         set_state()
 
-        create_targz(tmp, dump_files, start_time, options, user, state, set_state)
+        fname = create_targz(tmp, dump_files, start_time, options, user, state, set_state, options['file_format'])
         full_state['running'] = False
         full_state['errors'] = False
         state['overall']['status'] = _('Done')
         full_state['running'] = False
         full_state['errors'] = False
         state['overall']['status'] = _('Done')
+        state['overall']['fname'] = fname
 
         set_state()
     except Exception, e:
 
         set_state()
     except Exception, e: