[zopeorg-checkins] CVS: Products/ZopeOrg-NV/Extensions - NZOMigrate.py:1.5

Sidnei da Silva sidnei at x3ng.com.br
Sat Jan 4 12:34:51 EST 2003


Update of /cvs-zopeorg/Products/ZopeOrg-NV/Extensions
In directory cvs.zope.org:/tmp/cvs-serv28080/Extensions

Modified Files:
	NZOMigrate.py 
Log Message:
improved migration of properties

=== Products/ZopeOrg-NV/Extensions/NZOMigrate.py 1.4 => 1.5 ===
 from ZODB.POSException import POSKeyError
 from Acquisition import aq_base
 from Products.CMFCore.utils import getToolByName
+from DateTime import DateTime
 import zLOG
 
 def migrate(source, dest, ignore_path=None, type_map=None):
@@ -37,7 +38,10 @@
                     }
         
     tmut = Transmutator(source, dest, ignore_path, type_map)
-    return tmut.run()
+    tmut.log('Migration script started at %s\n.' % DateTime().ISO())
+    result = tmut.run()
+    tmut.log('Migration script finished at %s\n.' % DateTime().ISO())
+    return result
 
 def normalizeMetaType(meta_type):
     return meta_type.replace(' ', '')
@@ -127,14 +131,23 @@
             try: wf_tool.doActionFor(subob, 'publish')
             except: pass
             massPublish(subob)
+
+def filterList(alist):
+    dict = {}
+    for item in alist:
+        dict[item] = 1
+    alist = dict.keys()
+    alist.sort()
+    return alist
             
 class Transmutator:
 
-    def __init__(self, source, dest, ignore_path, type_map):
+    def __init__(self, source, dest, ignore_path, type_map, level=0):
         self._source = source
         self._dest = dest
         self._ignore_path = ignore_path
         self._type_map = type_map
+        self._level = level
         self._out = StringIO()
 
     def log(self, message, summary='', severity=0, dup=1):
@@ -142,6 +155,9 @@
         if dup:
             zLOG.LOG('NZOMigration: ',severity,summary,message)
 
+    def getResult(self):
+        return self._out.getvalue()
+
     def getMigrationMethod(self, obj):
         if hasattr(obj, 'meta_type'):
             mt = getattr(obj, 'meta_type')
@@ -197,6 +213,7 @@
         title = obj.title or ''
         description = obj.description or ''
         subject = obj.Software_topics or []
+        subject = filterList(subject)
         contact_email = obj.contact_email
         f.setTitle(title)
         f.setDescription(description)
@@ -205,9 +222,6 @@
         return f
 
     def ProductRelease2CMFSoftwareRelease(self, obj, source, dest):
-        if obj.getId() == 'Zope-2.5.x-to-2.5.1-linux2-x86.tgz':
-            import pdb
-            pdb.set_trace()
         dest.invokeFactory(id=obj.getId(), type_name='Software Release')
         f = getattr(dest, obj.getId())
         title = obj.title or ''
@@ -224,7 +238,8 @@
         changes_url = obj.changes_url
         installation_url = obj.installation_url
         license = obj.license
-        data, size = obj._read_data(obj.data)
+        data = str(obj.data)
+        size = len(data)
         content_type = obj._get_content_type(obj.data, data, obj.__name__,
                                             'application/octet-stream')
 
@@ -281,7 +296,8 @@
         f = getattr(dest, obj.getId())
         title = obj.title or ''
         content_type = obj.content_type
-        data, size = f._read_data(obj.data)
+        data = str(obj.data)
+        size = len(data)
         f.update_data(data, content_type, size)
         f.setTitle(title)
         f.setFormat(content_type)
@@ -292,7 +308,8 @@
         f = getattr(dest, obj.getId())
         title = obj.title or ''
         content_type = obj.content_type
-        data, size = f._read_data(obj.data)
+        data = str(obj.data)
+        size = len(data)
         f.update_data(data, content_type, size)
         f.setTitle(title)
         f.setFormat(content_type)
@@ -307,6 +324,7 @@
         subject = (type(obj.subject) in [type(''), type([])]) and \
                   obj.subject or []
         subject.extend(obj.NewsItem_topics)
+        subject = filterList(subject)
         news.setTitle(title)
         news.edit(text_format=format, text=text)
         news.setSubject(subject)
@@ -321,6 +339,7 @@
         subject = (type(obj.subject) in [type(''), type([])]) and \
                   obj.subject or []
         subject.extend(obj.Link_topics)
+        subject = filterList(subject)
         remote_url = obj.remote_url
         zope_powered = obj.zope_powered or 0
         link.setTitle(title)
@@ -338,9 +357,9 @@
                       obj.description or ''
         text = obj.content
         format = hasattr(obj, 'format') and obj.format or 'structured-text'
-        import pdb; pdb.set_trace()
         subject = (type(obj.subject) in [type(''), type([])]) and \
                   obj.subject or []
+        subject = filterList(subject)
         doc.setTitle(title)
         doc.setDescription(description)
         doc.edit(text_format=format, text=text)
@@ -354,14 +373,13 @@
         dest.invokeFactory(id=obj.getId(), type_name='Tip')
         tip = getattr(dest, obj.getId())
         title = obj.tip or ''
-        description = obj.details or ''
-        text = obj.content
-        format = obj.format
+        text = obj.details or ''
+        format = 'html'
         subject = (type(obj.subject) in [type(''), type([])]) and \
                   obj.subject or []
         subject.extend(obj.tip_topics)
+        subject = filterList(subject)
         tip.setTitle(title)
-        tip.setDescription(description)
         tip.edit(text_format=format, text=text)
         tip.setSubject(subject)
         return tip
@@ -375,6 +393,7 @@
         subject = (type(obj.subject) in [type(''), type([])]) and \
                   obj.subject or []
         subject.extend(obj.HowTo_topics)
+        subject = filterList(subject)
         howto.setTitle(title)
         howto.edit(text_format=format, text=text)
         howto.setSubject(subject)
@@ -463,12 +482,26 @@
         return new
 
     def copyProperties(self, orig, new):
+        if not hasattr(aq_base(orig), 'propertyIds'):
+            return new
         for prop in orig.propertyIds():
-            if not new.hasProperty(prop):
-                new._setProperty(prop, orig.getProperty(prop), \
-                                 orig.getPropertyType(prop))
-            else: new._setPropValue(prop, orig.getProperty(prop))
-        return new
+            if not new.hasProperty(prop) and not hasattr(aq_base(new), prop):
+                type = orig.getPropertyType(prop)
+                if type in ('selection', 'multiple selection'):
+                    for p in orig._properties:
+                        if p['id'] == prop:
+                            value = p['select_variable']
+                        if not hasattr(new, value):
+                            setattr(new, value, [])
+                else: value = orig.getProperty(prop)
+                try: new._setProperty(prop, value, type)
+                except Exception, msg:
+                    self.log('Could not set property ' + \
+                             '%s of type %s to %s.\n %s\n' % \
+                             (prop, type, value, msg))
+            else:
+                new._setPropValue(prop, orig.getProperty(prop))
+            return new
         
     def run(self):
         source = self._source
@@ -513,13 +546,15 @@
                 if hasattr(obj, 'objectIds') and new_obj is not None:
                     ignore = self.WikiMethodsCleanup(obj, ignore)
                     ignore = self.DefaultDTMLCleanup(obj, ignore)
-                    get_transaction().commit(1)
                     self.log(Transmutator(obj, new_obj, \
-                                          ignore, self._type_map).run(), dup=0)
+                                          ignore, self._type_map, \
+                                          self._level + 1).run(), dup=0)
+                    if self._level <= 2:
+                        get_transaction().commit()
+                    else:
+                        get_transaction().commit(1)
 
-            get_transaction().commit()
-            
-        return 'Ok.\n' or self._out.getvalue()
+        return 'Ok.\n'
 
 
 if __name__ == '__main__':





More information about the zopeorg-checkins mailing list