From 457115acc8d5cac9699e133acf24c956346302cb Mon Sep 17 00:00:00 2001
From: Julien Muchembled <jm@nexedi.com>
Date: Mon, 8 Nov 2010 17:18:11 +0000
Subject: [PATCH] ERP5Subversion: fix cleanup of temporary directories

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@40065 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5Subversion/SubversionClient.py    | 44 +++++++++++--------
 product/ERP5Subversion/Tool/SubversionTool.py | 12 ++---
 2 files changed, 30 insertions(+), 26 deletions(-)

diff --git a/product/ERP5Subversion/SubversionClient.py b/product/ERP5Subversion/SubversionClient.py
index d25e458495..0f4c2fdacf 100644
--- a/product/ERP5Subversion/SubversionClient.py
+++ b/product/ERP5Subversion/SubversionClient.py
@@ -36,11 +36,30 @@ import sys
 from Products.ERP5Type.Utils import convertToUpperCase
 from MethodObject import Method
 from Products.ERP5Type.Globals import InitializeClass
+from Products.ERP5Type.TransactionalVariable import getTransactionalVariable
 from AccessControl import ClassSecurityInfo
 from Products.PythonScripts.Utility import allow_class
-from tempfile import mktemp
+from tempfile import mkdtemp
 import shutil
 
+
+class getTransactionalDirectory(str):
+  """Returns a temporary directory that is automatically deleted when
+     transaction ends
+  """
+  def __new__(cls, tv_key):
+    tv = getTransactionalVariable()
+    try:
+      return str(tv[tv_key])
+    except KeyError:
+      path = mkdtemp()
+      tv[tv_key] = str.__new__(cls, path)
+      return path
+
+  def __del__(self):
+    shutil.rmtree(str(self))
+
+
 class SubversionError(Exception):
   """The base exception class for the Subversion interface.
   """
@@ -284,25 +303,14 @@ try:
       status_list.reverse()
       return status_list
     
-    def removeAllInList(self, path_list):
-      """Remove all files and folders in list
-      """
-      for file_path in path_list:
-        shutil.rmtree(file_path)
-      
     def diff(self, path, revision1, revision2):
-      tmp = mktemp()
-      os.makedirs(tmp)
-      if not revision1 or not revision2:
-        diff = self.client.diff(tmp_path=tmp, url_or_path=path, recurse=False)
+      tmp_path = getTransactionalDirectory('SubversionClient.diff:tmp_dir')
+      if revision1 and revision2:
+        return self.client.diff(tmp_path, url_or_path=path, recurse=False,
+          revision1=pysvn.Revision(pysvn.opt_revision_kind.number,revision1),
+          revision2=pysvn.Revision(pysvn.opt_revision_kind.number,revision2))
       else:
-        diff = self.client.diff(tmp_path=tmp, url_or_path=path, \
-        recurse=False, revision1 = pysvn.Revision(pysvn.opt_revision_kind\
-        .number,revision1), revision2=pysvn.Revision(pysvn\
-        .opt_revision_kind.number,revision2))
-      # clean up temp dir
-      self.activate().removeAllInList([tmp, ])
-      return diff
+        return self.client.diff(tmp_path, url_or_path=path, recurse=False)
     
     def revert(self, path, recurse=False):
       try:
diff --git a/product/ERP5Subversion/Tool/SubversionTool.py b/product/ERP5Subversion/Tool/SubversionTool.py
index deeab5849c..37f77e32be 100644
--- a/product/ERP5Subversion/Tool/SubversionTool.py
+++ b/product/ERP5Subversion/Tool/SubversionTool.py
@@ -40,7 +40,7 @@ import os, re
 from DateTime import DateTime
 from cPickle import dumps, loads
 from App.config import getConfiguration
-from tempfile import gettempdir, mktemp
+from tempfile import gettempdir, mkdtemp
 from Products.CMFCore.utils import getToolByName
 import shutil
 from xml.sax.saxutils import escape
@@ -869,7 +869,7 @@ class SubversionTool(BaseTool):
           # add new file to the tree
           parent.sub_files.append(File(filename, str(status)))
     return something_modified and root
-  
+
   def extractBT(self, business_template):
     """ 
      Extract business template to hard drive
@@ -881,7 +881,7 @@ class SubversionTool(BaseTool):
     business_template.build()
     svn_path = self._getWorkingPath(self.getSubversionPath(business_template) \
     + os.sep)
-    path = mktemp() + os.sep
+    path = mkdtemp()
     try:
       # XXX: Big hack to make export work as expected.
       transaction.commit()
@@ -891,13 +891,9 @@ class SubversionTool(BaseTool):
       # add new files and copy
       self.addNewFiles(svn_path, path)
       self.goToWorkingCopy(business_template)
-    except (pysvn.ClientError, NotFound, AttributeError, \
-    Error), error:
+    finally:
       # Clean up
       shutil.rmtree(path)
-      raise error
-    # Clean up
-    self.activate().removeAllInList([path, ])
     
   def importBT(self, business_template):
     """
-- 
2.30.9