[meld] Adapt SVN pre-1.7 to used NamedTemporaryFile, and handle new file cases



commit 72c166cb64be450ecf0ee9d9ebae6d4a4877a074
Author: Kai Willadsen <kai willadsen gmail com>
Date:   Mon Dec 3 07:05:33 2012 +1000

    Adapt SVN pre-1.7 to used NamedTemporaryFile, and handle new file cases

 meld/vc/svn.py |   24 ++++++++++++++++--------
 1 files changed, 16 insertions(+), 8 deletions(-)
---
diff --git a/meld/vc/svn.py b/meld/vc/svn.py
index d17e4be..49e5ec9 100644
--- a/meld/vc/svn.py
+++ b/meld/vc/svn.py
@@ -73,17 +73,25 @@ class Vc(_vc.CachedVc):
         if commit is not None:
             raise NotImplementedError()
 
+        if not path.startswith(self.root + os.path.sep):
+            raise _vc.InvalidVCPath(self, path, "Path not in repository")
+        path = path[len(self.root) + 1:]
+
         base, fname = os.path.split(path)
         svn_path = os.path.join(base, ".svn", "text-base", fname + ".svn-base")
 
-        # TODO: In Python 2.6+, this could be done with NamedTemporaryFile
-        tmp_handle, tmp_path = tempfile.mkstemp(prefix='meld-tmp', text=True)
-        with open(svn_path, 'r') as vc_file:
-            tmp_file = os.fdopen(tmp_handle, 'w')
-            shutil.copyfileobj(vc_file, tmp_file)
-            tmp_file.close()
+        with tempfile.NamedTemporaryFile(prefix='meld-tmp', delete=False) as f:
+            try:
+                with open(svn_path, 'r') as vc_file:
+                    shutil.copyfileobj(vc_file, f)
+            except IOError as err:
+                if err.errno != errno.ENOENT:
+                    raise
+                # If the repository path doesn't exist, we either have an
+                # invalid path (difficult to check) or a new file. Either way,
+                # we just return an empty file
 
-        return tmp_path
+        return f.name
 
     def _repo_version_support(self, version):
         return version < 12



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]