[Zope3-checkins] CVS: Zope3/lib/python/Zope/App/FSSync - Syncer.py:1.1.2.2

Deb dhazarika@zeomega.com
Wed, 16 Oct 2002 12:00:25 -0400


Update of /cvs-repository/Zope3/lib/python/Zope/App/FSSync
In directory cvs.zope.org:/tmp/cvs-serv24106/lib/python/Zope/App/FSSync

Modified Files:
      Tag: FileSystemSync-branch
	Syncer.py 
Log Message:
Syncer modified to work with registry


=== Zope3/lib/python/Zope/App/FSSync/Syncer.py 1.1.2.1 => 1.1.2.2 ===
--- Zope3/lib/python/Zope/App/FSSync/Syncer.py:1.1.2.1	Thu Oct 10 00:43:25 2002
+++ Zope3/lib/python/Zope/App/FSSync/Syncer.py	Wed Oct 16 12:00:24 2002
@@ -19,8 +19,8 @@
 """
 __metaclass__ = type
 
-import os
-from Zope.ComponentArchitecture import getAdapter, queryAdapter
+import os, string
+from Zope.ComponentArchitecture import queryAdapter, getService
 from Zope.XMLPickle.XMLPickle import dumps, loads
 from IObjectEntry import IObjectEntry
 from IObjectDirectory import IObjectDirectory
@@ -29,8 +29,10 @@
 from Zope.App.OFS.Container.IContainer import IContainer
 from Zope.Configuration.name import resolve
 from Default import Default
+from Zope.App.Traversing import getPhysicalPathString
+from FSRegistry import getSynchronizer
 
-def toFS(ob, name, location):
+def toFS(ob, name, location, mode=None):
     """Check an object out to the file system
 
     ob -- The object to be checked out
@@ -39,7 +41,7 @@
 
     location -- The directory on the file system where the object will go
     """
-
+    objectPath = ''
     # Look for location admin dir
     admin_dir = os.path.join(location, '@@Zope')
     if not os.path.exists(admin_dir):
@@ -53,12 +55,19 @@
         entries = {}
 
     # Get the object adapter
-    adapter = getAdapter(ob, IObjectEntry)
-
+    syncService = getService(ob, 'FSRegistryService')
+    adapter = syncService.getSynchronizer(ob)
+    
     entries[name] = {'type': adapter.typeIdentifier(),
                      'factory': adapter.factory(),
-                     }
-
+                     }
+    
+    try:
+        objectPath = str(getPhysicalPathString(ob))
+        entries[name]['path'] = objectPath
+    except TypeError:
+        pass
+    
     # Write entries file
     open(entries_path, 'w').write(dumps(entries))
 
@@ -97,8 +106,11 @@
         
 
     # Handle data
-    if IObjectFile.isImplementedBy(adapter):
-        open(path, 'w').write(adapter.getBody())
+    if IObjectFile.isImplementedBy(adapter):
+        if mode is None:
+            open(path, 'w').write(adapter.getBody())
+            if objectPath:
+                print 'U %s' % (objectPath[1:])
         original_path = os.path.join(admin_dir, 'Original')
         if not os.path.exists(original_path):
             os.mkdir(original_path)
@@ -107,18 +119,22 @@
         
 
     else:
-        # Directory
-        if os.path.exists(path):
+        # Directory
+        if objectPath:
+            print 'UPDATING %s' % (objectPath[1:])
+        if os.path.exists(path):
             dir_entries = os.path.join(path, '@@Zope', 'Entries.xml')
             if os.path.exists(dir_entries):
                 open(dir_entries, 'w').write(dumps({}))
-        else:
+        else:
             os.mkdir(path)
             
         
         for cname, cob in  adapter.contents():
             toFS(cob, cname, path)
-        
+
+    
+
 class SynchronizationError(Exception): pass
 
 def _setItem(container, name, ob, old=0):
@@ -134,10 +150,11 @@
         # Not a container, must be a mapping
         container[name] = ob
 
-def fromFS(container, name, location):
+def fromFS(container, name, location, mode=None):
     """Synchromize a file from what's on the file system.
     """
-
+    msg =''
+    objectPath = ''
     # Look for location admin dir
     admin_dir = os.path.join(location, '@@Zope')
     if not os.path.exists(admin_dir):
@@ -153,120 +170,147 @@
     path = os.path.join(location, name)
     if path == name:
         raise ValueError("Invalid absolute path name")
+
+
+    # See if this is an existing object
+    if name in container:
+        # Yup, let's see if we have the same kind of object
+    
+        # Get the object adapter
+        ob = container[name]
+        syncService = getService(ob, 'FSRegistryService')
+        adapter = syncService.getSynchronizer(ob)        
+
+        
+        # Replace the object if the type is different
+        if adapter.typeIdentifier() != entry.get('type'):
+            # We have a different object, replace the one that's there
+
+            if factory:            
+                newOb = resolve(factory)()
+            else:
+                newOb = loads(open(path).read())
+
+            _setItem(container, name, newOb, old=1)
+
+        elif not factory:
+            if entry.get('type') == '__builtin__.str':
+                newOb = open(path).read()
+                _setItem(container, name, newOb, old=1)
+            else:
+                # Special case pickle data
+                oldOb = container[name]
+                newOb = loads(open(path).read())
+                try:
+                    # See if we can and should just copy the state
+                    oldOb._p_oid # Is it persisteny
+                    getstate = newOb.__getstate__
+                except AttributeError:
+                    # Nope, we have to replace.
+                    _setItem(container, name, newOb, old=1)
+                else:
+                    oldOb.__setstate__(getstate())
+                    oldOb._p_changed = 1
+                
+
+    else:
+        # We need to create a new object
+
+        if factory:            
+            newOb = resolve(entry['factory'])()
+        else:
+            newOb = loads(open(path).read())
+
+        _setItem(container, name, newOb)
+        
+
+    # Get the object adapter again
+    ob = container[name] 
+    syncService = getService(ob, 'FSRegistryService')
+    adapter = syncService.getSynchronizer(ob)
+
+
+    
+    # Handle extra
+    extra = adapter.extra()
+    extra_dir = os.path.join(admin_dir, 'Extra', name)
+    extra_entries_path = os.path.join(extra_dir, "@@Zope", "Entries.xml")
+    if extra:
+        if not os.path.exists(extra_entries_path):
+            # The file system has no extras, so delete all of the object's
+            # extras.
+            for key in extra:
+                del extra[key]
+        else:
+            extra_entries = loads(
+                open(extra_entries_path).read())
+            for ename in extra_entries:
+                fromFS(extra, ename, extra_dir, mode)
+    elif os.path.exists(extra_entries_path):
+        extra_entries = loads(
+            open(extra_entries_path).read())
+        if extra_entries:
+            raise SynchronizationError(
+                "File-system extras for object with no extra data")
+    
+
+    # Handle annotations
+    annotations = queryAdapter(ob, IAnnotations)
+    annotation_dir = os.path.join(admin_dir, 'Annotations', name)
+    annotation_entries_path = os.path.join(
+        annotation_dir, "@@Zope", "Entries.xml")
+    if annotations is not None:
+        if not os.path.exists(annotation_entries_path):
+            # The file system has no annotations, so delete all of the object's
+            # annotations.
+            for key in annotations:
+                del annotations[key]
+        else:
+            annotation_entries = loads(
+                open(annotation_entries_path).read())
+            for ename in annotation_entries:
+                fromFS(annotations, ename, annotation_dir, mode)
+    elif os.path.exists(annotation_entries_path):
+        annotation_entries = loads(
+            open(annotation_entries_path).read())
+        if annotation_entries:
+            raise SynchronizationError(
+                "File-system annotations for non annotatable object")
+    
+    # Handle data
+    if IObjectFile.isImplementedBy(adapter):
+        # File
+        if os.path.isdir(path):
+            raise SynchronizationError("Object is file, but data is directory")
+        adapter.setBody(open(path).read())
+        if mode is not None:
+            if string.find(path,'@@Zope')==-1:
+                #copying to original
+                fspath = path
+                f = open(fspath, 'r')
+                data = f.read()
+                f.close()
+                original_path = os.path.join(os.path.dirname(fspath),'@@Zope','Original',os.path.basename(fspath))
+                f = open(original_path, 'w')
+                f.write(data)
+                f.close()
+                entries_path = os.path.join(os.path.dirname(fspath),'@@Zope','Entries.xml')
+                entries = loads(open(entries_path).read())
+                objectpath = entries[os.path.basename(fspath)]['path']
+                msg = "%s  <--  %s" %(objectpath, string.split(objectpath,'/')[-1])
+                print msg
+   
+
+    else:
+        # Directory
+        if not os.path.isdir(path):
+            raise SynchronizationError("Object is directory, but data is file")
+
+        dir_entries_path = os.path.join(path, '@@Zope', 'Entries.xml')
+        dir_entries = loads(open(dir_entries_path).read())
+        for cname in dir_entries:
+            fromFS(ob, cname, path, mode)
+
 
-    # See if this is an existing object
-    if name in container:
-        # Yup, let's see if we have the same kind of object
-    
-        # Get the object adapter
-        adapter = getAdapter(container[name], IObjectEntry)
-        
-        # Replace the object if the type is different
-        if adapter.typeIdentifier() != entry.get('type'):
-            # We have a different object, replace the one that's there
-
-            if factory:            
-                newOb = resolve(factory)()
-            else:
-                newOb = loads(open(path).read())
-
-            _setItem(container, name, newOb, old=1)
-
-        elif not factory:
-            # Special case pickle data
-            oldOb = container[name]
-            newOb = loads(open(path).read())
-            try:
-                # See id we can amd should just copy the state
-                oldOb._p_oid # Is it persisteny
-                getstate = newOb.__getstate__
-            except AttributeError:
-                # Nope, we have to replace.
-                _setItem(container, name, newOb, old=1)
-            else:
-                oldOb.__setstate__(getstate())
-                oldOb._p_changed = 1
-                
-
-    else:
-        # We need to create a new object
-
-        if factory:            
-            newOb = resolve(entry['factory'])()
-        else:
-            newOb = loads(open(path).read())
-
-        _setItem(container, name, newOb)
-        
-
-    # Get the object adapter again
-    ob = container[name]
-    adapter = getAdapter(ob, IObjectEntry)
-    
-
-    # Handle extra
-    extra = adapter.extra()
-    extra_dir = os.path.join(admin_dir, 'Extra', name)
-    extra_entries_path = os.path.join(extra_dir, "@@Zope", "Entries.xml")
-    if extra:
-        if not os.path.exists(extra_entries_path):
-            # The file system has no extras, so delete all of the object's
-            # extras.
-            for key in extra:
-                del extra[key]
-        else:
-            extra_entries = loads(
-                open(extra_entries_path).read())
-            for ename in extra_entries:
-                fromFS(extra, ename, extra_dir)
-    elif os.path.exists(extra_entries_path):
-        extra_entries = loads(
-            open(extra_entries_path).read())
-        if extra_entries:
-            raise SynchronizationError(
-                "File-system extras for object with no extra data")
-    
-
-    # Handle annotations
-    annotations = queryAdapter(ob, IAnnotations)
-    annotation_dir = os.path.join(admin_dir, 'Annotations', name)
-    annotation_entries_path = os.path.join(
-        annotation_dir, "@@Zope", "Entries.xml")
-    if annotations is not None:
-        if not os.path.exists(annotation_entries_path):
-            # The file system has no annotations, so delete all of the object's
-            # annotations.
-            for key in annotations:
-                del annotations[key]
-        else:
-            annotation_entries = loads(
-                open(annotation_entries_path).read())
-            for ename in annotation_entries:
-                fromFS(annotations, ename, annotation_dir)
-    elif os.path.exists(annotation_entries_path):
-        annotation_entries = loads(
-            open(annotation_entries_path).read())
-        if annotation_entries:
-            raise SynchronizationError(
-                "File-system annotations for non annotatable object")
-
-    # Handle data
-    if IObjectFile.isImplementedBy(adapter):
-        # File
-        if os.path.isdir(path):
-            raise SynchronizationError("Object is file, but data is directory")
-        adapter.setBody(open(path).read())
-
-    else:
-        # Directory
-        if not os.path.isdir(path):
-            raise SynchronizationError("Object is directory, but data is file")
-
-        dir_entries_path = os.path.join(path, '@@Zope', 'Entries.xml')
-        dir_entries = loads(open(dir_entries_path).read())
-
-        for cname in dir_entries:
-            fromFS(ob, cname, path)
     
-    
-        
+        
\ No newline at end of file