[zopeorg-checkins] CVS: Products/ZopeOrg-NV/Extensions - change_modules.py:1.1 NZOMigrate.py:1.17

Sidnei da Silva sidnei at x3ng.com.br
Sat Mar 15 13:51:23 EST 2003


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

Modified Files:
	NZOMigrate.py 
Added Files:
	change_modules.py 
Log Message:
Adding Shane's change_modules ExternalMethod. Also refactored the migration to be more verbose about exceptions.

=== Added File Products/ZopeOrg-NV/Extensions/change_modules.py ===
_trans = {
    ('OFS.ObjectManager', 'Folder'):
    ('CMFCore.PortalFolder', 'PortalFolder'),
    }


def changeClassModules(self, root_object=None):
    get_transaction().commit(1)  # Make sure the current state is stored
    jar = self._p_jar
    if root_object is None:
        root_object = jar.root()
    root_oid = root_object._p_oid
    stats = [-1, -1]
    
    translation_func = lambda class_spec: _trans.get(
        class_spec, class_spec)
    
    jar.onCommitAction('_changeClassDuringCommit',
                       translation_func, root_oid, stats)
    get_transaction().commit(1)
    return ('Done. Visited %d objects and changed %d. '
            'See the event log for more details.' % tuple(stats))

if 1:
    from cStringIO import StringIO
    from cPickle import Pickler, Unpickler
    from ZODB.ExportImport import Ghost, persistent_id
    from zLOG import LOG, INFO, WARNING


    def _changeClassDuringCommit(self, transaction, translation_func,
                                 root_oid, stats=None):
        '''Recursively changes the class of persistent objects.

        Invoked as a method of Connection by the transaction manager
        mid subtransaction commit.
        '''
        to_visit = [root_oid]
        seen = {root_oid:1}
        storage = self._storage

        trigger = [0]
        change_count = 0


        def persistent_load(oid,
                            # Bound to the function:
                            to_visit=to_visit, seen=seen, Ghost=Ghost,
                            trigger=trigger,
                            translation_func=translation_func,
                            TupleType=type(())):
            "Adds to the list of oids to visit."
            if isinstance(oid, TupleType):
                simple_oid, class_spec = oid
                new_class_spec = translation_func(class_spec)
                if new_class_spec != class_spec:
                    # Change this reference.
                    trigger[0] = 1
                    oid = (simple_oid, new_class_spec)
            else:
                simple_oid = oid
            if not seen.has_key(simple_oid):
                seen[simple_oid] = 1
                to_visit.append(simple_oid)
            g = Ghost()
            g.oid = oid
            return g


        def find_global(module, name,
                        # Bound to the function:
                        translation_func=translation_func, trigger=trigger,
                        _silly=('__doc__',), _globals={}):
            class_spec = (module, name)
            new_class_spec = translation_func(class_spec)
            if new_class_spec != class_spec:
                # Change this reference.
                trigger[0] = 1
            module, name = new_class_spec
            m = __import__(module, _globals, _globals, _silly)
            return getattr(m, name)


        version = self._version

        while to_visit:
            oid = to_visit.pop()
            p, serial = storage.load(oid, version)

            convert_ok = 1
            trigger[0] = 0
            pfile = StringIO(p)
            unpickler = Unpickler(pfile)
            unpickler.persistent_load = persistent_load
            unpickler.find_global = find_global
            class_spec, args = unpickler.load()
            try:
                # Get persistent_load() and find_global() called.
                state = unpickler.load()
            except (ImportError, AttributeError):
                # There are problems with this object.
                LOG('changeClasses', WARNING, 'Could not load state for OID '
                    '%s, class %s' % (repr(oid), repr(class_spec)))
                convert_ok = 0

            if convert_ok:
                # Figure out the new class.
                new_class_spec = translation_func(class_spec)
                if new_class_spec != class_spec:
                    # Change this reference.
                    trigger[0] = 1

                if trigger[0]:
                    # Store the object with changes to classes.
                    newp = StringIO()
                    pickler = Pickler(newp, 1)
                    pickler.persistent_id = persistent_id
                    pickler.dump((new_class_spec, args))
                    pickler.dump(state)
                    data = newp.getvalue()
                    self.invalidate(oid)
                    self._invalidating.append(oid)
                    if self._cache.has_key(oid):
                        # Ghostify.
                        ob = self._cache[oid]
                        if not ob._p_changed:
                            ob._p_changed = None
                            del self._cache[oid]
                    storage.store(oid, serial, data, version, transaction)
                    change_count = change_count + 1
                    if class_spec != new_class_spec:
                        LOG('changeClass', INFO,
                            'Changed OID %s from class %s to class %s' % (
                            repr(oid), repr(class_spec), repr(new_class_spec)))
                    else:
                        LOG('changeClass', INFO,
                            'Rewrote OID %s with new references' % repr(oid))

        if stats is not None:
            stats[:] = [len(seen), change_count]

    from ZODB.Connection import Connection
    Connection._changeClassDuringCommit = _changeClassDuringCommit




=== Products/ZopeOrg-NV/Extensions/NZOMigrate.py 1.16 => 1.17 === (820/920 lines abridged)
+import sys
 from StringIO import StringIO
 from ZODB.POSException import POSKeyError
 from Acquisition import aq_base, aq_parent, aq_inner
 from Products.CMFCore.utils import getToolByName
+from Products.CMFCollector.CollectorIssue import CollectorIssue
 from DateTime import DateTime
+from DocumentTemplate.DT_HTML import HTML as DTML
+from zExceptions.ExceptionFormatter import format_exception
 import zLOG
 
-def migrate(source, dest, ignore_path=None, type_map=None):
+def _send_update_notice(self, *args, **kw):
+    pass
+
+def migrate(source, dest, ignore_path=None, type_map=None, exceptions=None):
     dest_path = dest.getPhysicalPath()
     if ignore_path is None: ignore_path = []
     if not dest_path in ignore_path:
@@ -36,16 +43,140 @@
                     "BackTalk Book": "CMF BackTalk Book",
                     "BackTalk Document": "CMF BackTalk Document"
                     }
-        
-    tmut = Transmutator(source, dest, ignore_path, type_map)
-    tmut.log('Migration script started at %s\n.' % DateTime().ISO())
+
+        if exceptions is None:
+            exceptions = {}
+            exceptions['Resources/ZSP/zsp.xml'] = 'ZSP2ZopeOrgZSP'
+            exceptions['Resources/CaseStudies'] = 'CaseStudies2ZopeOrgCaseStudies'
+            
+    tmut = Transmutator(source, dest, ignore_path, type_map, exceptions)
+    tmut.log('Migration script started at %s.\n' % DateTime().ISO())
     result = tmut.run()
-    tmut.log('Migration script finished at %s\n.' % DateTime().ISO())
+    tmut.log('Migration script finished at %s.\n' % DateTime().ISO())
     return result
 
 def normalizeMetaType(meta_type):
     return meta_type.replace(' ', '')
 
+def dumpTrackerIssue(issue):
+    kw = {}
+    kw['id'] = issue.getId()
+    kw['resolution'] = issue.stage
+    traits = {}
+    for key, value in issue.traitVals:
+        traits[key] = value
+    kw['classification'] = traits.get('type', '')

[-=- -=- -=- 820 lines omitted -=- -=- -=-]

                         self.log('\n'.join(res) + '\n')
-                    new_obj = self.fixModificationDate(obj, new_obj)
+                    
+                    
+                    if hasattr(obj, 'objectIds') and new_obj is not None:
+                        ignore = WikiMethodsCleanup(obj, obj_url, ignore)
+                        ignore = DefaultDTMLCleanup(obj, obj_url, ignore)
+                        self.log(Transmutator(obj, new_obj, \
+                                              ignore, self._type_map, \
+                                              self._exceptions, \
+                                              self._count).run(), dup=0)
+
+                        if source_id == 'Members':
+                            res = recursiveOwnerFix(new_obj, oid, 1)
+                            setLocalRoles(new_obj, (oid, ), 'Owner', obj_url)
+
+                    massPublish(new_obj)
+                    updateRoleMappings(new_obj)
+
+                    new_obj = fixModificationDate(obj, new_obj)
 
                     ct = getToolByName(new_obj, 'portal_catalog', None)
                     if ct is not None:
                         try: ct.reindexObject(obj)
                         except: pass
-                    self.log('%s: Success.\n' % obj.absolute_url(relative=1))
-                    
-                    if hasattr(obj, 'objectIds') and new_obj is not None:
-                        ignore = self.WikiMethodsCleanup(obj, ignore)
-                        ignore = self.DefaultDTMLCleanup(obj, ignore)
-                        self.log(Transmutator(obj, new_obj, \
-                                          ignore, self._type_map, \
-                                          self._level + 1).run(), dup=0)
-                        if self._level <= 2:
-                            get_transaction().commit()
-                        else:
-                            get_transaction().commit(1)
 
-        return 'Ok.\n'
+                    if self._count % 100:
+                        get_transaction().commit()
+                    elif self._count % 50:
+                        get_transaction().commit(1)
+
+                self.log('%s: Success.\n' % obj_url)
 
+        return 'Ok.\n'
 
 if __name__ == '__main__':
     print 'ok'





More information about the zopeorg-checkins mailing list