[meld/Python3: 24/54] vc: Break out helper for writing process output to a temp file



commit 15fc6fa62273ee250c2e31a2123e337c5400ad24
Author: Kai Willadsen <kai willadsen gmail com>
Date:   Sun Nov 15 07:41:02 2015 +1000

    vc: Break out helper for writing process output to a temp file

 meld/vc/_vc.py       |   20 ++++++++++++++++++++
 meld/vc/bzr.py       |   15 +--------------
 meld/vc/git.py       |   13 ++-----------
 meld/vc/mercurial.py |   12 ++----------
 meld/vc/svn.py       |   17 ++---------------
 5 files changed, 27 insertions(+), 50 deletions(-)
---
diff --git a/meld/vc/_vc.py b/meld/vc/_vc.py
index 4f98528..5d9912e 100644
--- a/meld/vc/_vc.py
+++ b/meld/vc/_vc.py
@@ -27,7 +27,9 @@ import collections
 import itertools
 import os
 import re
+import shutil
 import subprocess
+import tempfile
 
 from gi.repository import Gio
 
@@ -371,6 +373,24 @@ def popen(cmd, cwd=None):
     return subprocess.Popen(cmd, cwd=cwd, stdout=subprocess.PIPE).stdout
 
 
+def call_temp_output(cmd, cwd):
+    """Call `cmd` in `cwd` and write the output to a temporary file
+
+    This returns the name of the temporary file used. It is the
+    caller's responsibility to delete this file.
+    """
+    process = subprocess.Popen(
+        cmd, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+    vc_file = process.stdout
+
+    # Error handling here involves doing nothing; in most cases, the only
+    # sane response is to return an empty temp file.
+
+    with tempfile.NamedTemporaryFile(prefix='meld-tmp', delete=False) as f:
+        shutil.copyfileobj(vc_file, f)
+    return f.name
+
+
 # Return the return value of a given command
 def call(cmd, cwd=None):
     NULL = open(os.devnull, "wb")
diff --git a/meld/vc/bzr.py b/meld/vc/bzr.py
index 3eb1eb5..4c302e4 100644
--- a/meld/vc/bzr.py
+++ b/meld/vc/bzr.py
@@ -29,9 +29,6 @@ from collections import defaultdict
 import errno
 import os
 import re
-import shutil
-import subprocess
-import tempfile
 
 from . import _vc
 
@@ -230,17 +227,7 @@ class Vc(_vc.Vc):
         if commit:
             args.append("-r%s" % commit)
 
-        process = subprocess.Popen(args,
-                                   cwd=self.root, stdout=subprocess.PIPE,
-                                   stderr=subprocess.PIPE)
-        vc_file = process.stdout
-
-        # Error handling here involves doing nothing; in most cases, the only
-        # sane response is to return an empty temp file.
-
-        with tempfile.NamedTemporaryFile(prefix='meld-tmp', delete=False) as f:
-            shutil.copyfileobj(vc_file, f)
-        return f.name
+        return _vc.call_temp_output(args, cwd=self.root)
 
     def get_path_for_conflict(self, path, conflict):
         if path in self._reverse_rename_cache and not \
diff --git a/meld/vc/git.py b/meld/vc/git.py
index c799741..967f454 100644
--- a/meld/vc/git.py
+++ b/meld/vc/git.py
@@ -255,17 +255,8 @@ class Vc(_vc.Vc):
             path = path.replace("\\", "/")
 
         obj = commit + ":" + path
-        process = subprocess.Popen([self.CMD, "cat-file", "blob", obj],
-                                   cwd=self.root, stdout=subprocess.PIPE,
-                                   stderr=subprocess.PIPE)
-        vc_file = process.stdout
-
-        # Error handling here involves doing nothing; in most cases, the only
-        # sane response is to return an empty temp file.
-
-        with tempfile.NamedTemporaryFile(prefix='meld-tmp', delete=False) as f:
-            shutil.copyfileobj(vc_file, f)
-        return f.name
+        args = [self.CMD, "cat-file", "blob", obj]
+        return _vc.call_temp_output(args, cwd=self.root)
 
     @classmethod
     def valid_repo(cls, path):
diff --git a/meld/vc/mercurial.py b/meld/vc/mercurial.py
index 03dadfd..d49196d 100644
--- a/meld/vc/mercurial.py
+++ b/meld/vc/mercurial.py
@@ -25,9 +25,6 @@
 
 import errno
 import os
-import shutil
-import subprocess
-import tempfile
 
 from . import _vc
 
@@ -80,13 +77,8 @@ class Vc(_vc.Vc):
             raise _vc.InvalidVCPath(self, path, "Path not in repository")
         path = path[len(self.root) + 1:]
 
-        process = subprocess.Popen([self.CMD, "cat", path], cwd=self.root,
-                                   stdout=subprocess.PIPE,
-                                   stderr=subprocess.PIPE)
-
-        with tempfile.NamedTemporaryFile(prefix='meld-tmp', delete=False) as f:
-            shutil.copyfileobj(process.stdout, f)
-        return f.name
+        args = [self.CMD, "cat", path]
+        return _vc.call_temp_output(args, cwd=self.root)
 
     def _update_tree_state_cache(self, path):
         """ Update the state of the file(s) at self._tree_cache['path'] """
diff --git a/meld/vc/svn.py b/meld/vc/svn.py
index 4cecb4f..e73bfe6 100644
--- a/meld/vc/svn.py
+++ b/meld/vc/svn.py
@@ -26,10 +26,7 @@
 import errno
 import glob
 import os
-import shutil
-import tempfile
 import xml.etree.ElementTree as ElementTree
-import subprocess
 
 from meld.conf import _
 from . import _vc
@@ -82,18 +79,8 @@ class Vc(_vc.Vc):
             raise _vc.InvalidVCPath(self, path, "Path not in repository")
         path = path[len(self.root) + 1:]
 
-        process = subprocess.Popen([self.CMD, "cat", "-r", commit, path],
-                                   cwd=self.root, stdout=subprocess.PIPE,
-                                   stderr=subprocess.PIPE)
-        vc_file = process.stdout
-
-        # Error handling here involves doing nothing; in most cases, the only
-        # sane response is to return an empty temp file. The most common error
-        # is "no base revision until committed" from diffing a new file.
-
-        with tempfile.NamedTemporaryFile(prefix='meld-tmp', delete=False) as f:
-            shutil.copyfileobj(vc_file, f)
-        return f.name
+        args = [self.CMD, "cat", "-r", commit, path]
+        return _vc.call_temp_output(args, cwd=self.root)
 
     def get_path_for_conflict(self, path, conflict=None):
         """


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