[Zope3-checkins] CVS: Zope3/src/zope/app/browser/content - fromFS.pt:1.1 configure.zcml:1.21 fssync.py:1.3

Guido van Rossum guido@python.org
Thu, 8 May 2003 15:52:11 -0400


Update of /cvs-repository/Zope3/src/zope/app/browser/content
In directory cvs.zope.org:/tmp/cvs-serv29812

Modified Files:
	configure.zcml fssync.py 
Added Files:
	fromFS.pt 
Log Message:
Add a fromFS.html view which implements the commit operation.


=== Added File Zope3/src/zope/app/browser/content/fromFS.pt ===
<html metal:use-macro="views/standard_macros/page">
<body>
<div metal:fill-slot="body">

<h1>Commit Action</h1>

<div tal:define="status view/update"
     tal:condition="status">
Commit results:
<pre tal:content="status">
Status from update method goes here.
</pre>
</div>

<p>Upload a zipfile in the following form</p>

<form method="POST" action="@@fromFS.html" enctype="multipart/form-data">

  <input type="file" name="zipfile" size="40" />
  <input type="submit">

</form>

</div>
</body>
</html>


=== Zope3/src/zope/app/browser/content/configure.zcml 1.20 => 1.21 ===
--- Zope3/src/zope/app/browser/content/configure.zcml:1.20	Wed May  7 21:08:42 2003
+++ Zope3/src/zope/app/browser/content/configure.zcml	Thu May  8 15:52:10 2003
@@ -424,7 +424,7 @@
       description="An object storing XML text." 
       />
 
-  <!-- toFS.zip view, for new fssync tools -->
+  <!-- toFS.zip and fromFS.html views, for new fssync tools -->
 
   <browser:page
       for="zope.interface.Interface"
@@ -432,6 +432,14 @@
       permission="zope.ManageServices"
       class="zope.app.browser.content.fssync.ZipFile"
       attribute="show" 
+      />
+
+  <browser:page
+      for="zope.interface.Interface"
+      name="fromFS.html"
+      permission="zope.ManageServices"
+      class="zope.app.browser.content.fssync.Commit"
+      template="fromFS.pt"
       />
 
 </zopeConfigure>


=== Zope3/src/zope/app/browser/content/fssync.py 1.2 => 1.3 ===
--- Zope3/src/zope/app/browser/content/fssync.py:1.2	Thu May  8 11:32:21 2003
+++ Zope3/src/zope/app/browser/content/fssync.py	Thu May  8 15:52:10 2003
@@ -12,7 +12,7 @@
 # 
 ##############################################################################
 
-"""Code for the toFS.zip view.
+"""Code for the toFS.zip view and its inverse, fromFS.form.
 
 $Id$
 """
@@ -22,8 +22,10 @@
 import tempfile
 
 from zope.publisher.browser import BrowserView
-from zope.app.fssync.syncer import toFS
-from zope.app.traversing import objectName
+from zope.app.fssync.syncer import toFS, fromFS
+from zope.app.fssync.compare import checkUptodate
+from zope.app.traversing import objectName, getParent
+from zope.app.interfaces.exceptions import UserError
 
 class ZipFile(BrowserView):
 
@@ -44,7 +46,7 @@
         f = open(zipfilename, "rb")
         data = f.read()
         f.close()
-        os.unlink(zipfilename)
+        os.remove(zipfilename)
         response = self.request.response
         response.setHeader("Content-Type", "application/zip")
         # XXX This can return a lot of data; should figure out how to
@@ -103,5 +105,55 @@
     finally:
         shutil.rmtree(dirname)
 
-# XXX Still to do: the reverse operation, fromFS.  This should
-# probably be an HTML form with the zipfile as an uploaded file
+# And here is the inverse operation, fromFS.html (an HTML form).
+
+class Commit(BrowserView):
+
+    """View for committing changes.
+
+    For now, this is an HTML form where you can upload a zipfile.
+    """
+
+    def update(self):
+        zipfile = self.request.get("zipfile")
+        if zipfile is None:
+            return # Not updating -- must be presenting a blank form
+        zipfiledata = zipfile.read()
+        # 00) Allocate temporary names
+        topdir = tempfile.mktemp()
+        zipfilename = os.path.join(topdir, zipfile.filename)
+        working = os.path.join(topdir, "working")
+        current = os.path.join(topdir, "current")
+        try:
+            # 0) Create the top directory
+            os.mkdir(topdir)
+            # 1) Write the zipfile data to disk
+            f = open(zipfilename, "wb")
+            f.write(zipfiledata)
+            f.close()
+            # 2) Unzip it into a working directory
+            os.mkdir(working)
+            os.system("cd %s; unzip -q %s" % (working, zipfilename))
+            # 3) Save the current state of the object to disk
+            os.mkdir(current)
+            toFS(self.context, objectName(self.context) or "root", current,
+                 writeOriginals=False)
+            # 4) Check that the working originals are up-to-date
+            errors = checkUptodate(working, current)
+            if errors:
+                # Make the messages nicer by editing out topdir
+                errors = [x.replace(os.path.join(topdir, ""), "")
+                          for x in errors]
+                errors.insert(0, "Uptodate check failed:")
+                raise UserError(*errors)
+            # 5) Now call fromFS()
+            name = objectName(self.context)
+            container = getParent(self.context)
+            fromFS(container, name, working)
+            # 6) Return success message
+            return "Changes committed successfully."
+        finally:
+            try:
+                shutil.rmtree(topdir)
+            except os.error:
+                pass