changeset 7106:4674706b5b95

python2.6: use subprocess if available
author Dirkjan Ochtman <dirkjan@ochtman.nl>
date Sun, 05 Oct 2008 21:35:26 +0200
parents 31837416ef4d
children 125c8fedcbe0
files hgext/convert/cvs.py hgext/convert/subversion.py mercurial/patch.py mercurial/sshrepo.py mercurial/util.py
diffstat 5 files changed, 33 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/convert/cvs.py	Fri Oct 17 17:34:25 2008 +0200
+++ b/hgext/convert/cvs.py	Sun Oct 05 21:35:26 2008 +0200
@@ -252,7 +252,7 @@
             # popen2 does not support argument lists under Windows
             cmd = [util.shellquote(arg) for arg in cmd]
             cmd = util.quotecommand(' '.join(cmd))
-            self.writep, self.readp = os.popen2(cmd, 'b')
+            self.writep, self.readp = util.popen2(cmd, 'b')
 
         self.realroot = root
 
--- a/hgext/convert/subversion.py	Fri Oct 17 17:34:25 2008 +0200
+++ b/hgext/convert/subversion.py	Sun Oct 05 21:35:26 2008 +0200
@@ -914,7 +914,7 @@
         arg = encodeargs(args)
         hgexe = util.hgexecutable()
         cmd = '%s debugsvnlog' % util.shellquote(hgexe)
-        stdin, stdout = os.popen2(cmd, 'b')
+        stdin, stdout = util.popen2(cmd, 'b')
         stdin.write(arg)
         stdin.close()
         return logstream(stdout)
--- a/mercurial/patch.py	Fri Oct 17 17:34:25 2008 +0200
+++ b/mercurial/patch.py	Sun Oct 05 21:35:26 2008 +0200
@@ -9,7 +9,7 @@
 from i18n import _
 from node import hex, nullid, short
 import base85, cmdutil, mdiff, util, revlog, diffhelpers, copies
-import cStringIO, email.Parser, os, popen2, re, errno
+import cStringIO, email.Parser, os, re, errno
 import sys, tempfile, zlib
 
 class PatchError(Exception):
@@ -1308,7 +1308,7 @@
         return
     fd, name = tempfile.mkstemp(prefix="hg-patchbomb-", suffix=".txt")
     try:
-        p = popen2.Popen3('diffstat -p1 -w79 2>/dev/null > ' + name)
+        p = util.Popen3('diffstat -p1 -w79 2>/dev/null > ' + name)
         try:
             for line in patchlines:
                 p.tochild.write(line + "\n")
--- a/mercurial/sshrepo.py	Fri Oct 17 17:34:25 2008 +0200
+++ b/mercurial/sshrepo.py	Sun Oct 05 21:35:26 2008 +0200
@@ -61,7 +61,7 @@
 
         cmd = util.quotecommand(cmd)
         ui.note(_('running %s\n') % cmd)
-        self.pipeo, self.pipei, self.pipee = os.popen3(cmd, 'b')
+        self.pipeo, self.pipei, self.pipee = util.popen3(cmd, 'b')
 
         # skip any noise generated by remote shell
         self.do_cmd("hello")
--- a/mercurial/util.py	Fri Oct 17 17:34:25 2008 +0200
+++ b/mercurial/util.py	Sun Oct 05 21:35:26 2008 +0200
@@ -50,6 +50,33 @@
     return _sha1(s)
 
 try:
+    import subprocess
+    def popen2(cmd, mode='t', bufsize=-1):
+        p = subprocess.Popen(cmd, shell=True, bufsize=bufsize, close_fds=True,
+                             stdin=subprocess.PIPE, stdout=subprocess.PIPE)
+        return p.stdin, p.stdout
+    def popen3(cmd, mode='t', bufsize=-1):
+        p = subprocess.Popen(cmd, shell=True, bufsize=bufsize, close_fds=True,
+                             stdin=subprocess.PIPE, stdout=subprocess.PIPE,
+                             stderr=subprocess.PIPE)
+        return p.stdin, p.stdout, p.stderr
+    def Popen3(cmd, capturestderr=False, bufsize=-1):
+        stderr = capturestderr and subprocess.PIPE or None
+        p = subprocess.Popen(cmd, shell=True, bufsize=bufsize, close_fds=True,
+                             stdin=subprocess.PIPE, stdout=subprocess.PIPE,
+                             stderr=stderr)
+        p.fromchild = p.stdout
+        p.tochild = p.stdin
+        p.childerr = p.stderr
+        return p
+except ImportError:
+    subprocess = None
+    import popen2 as _popen2
+    popen2 = _popen2.popen2
+    Popen3 = _popen2.Popen3
+
+
+try:
     _encoding = os.environ.get("HGENCODING")
     if sys.platform == 'darwin' and not _encoding:
         # On darwin, getpreferredencoding ignores the locale environment and
@@ -183,7 +210,7 @@
 
 def pipefilter(s, cmd):
     '''filter string S through command CMD, returning its output'''
-    (pin, pout) = os.popen2(cmd, 'b')
+    (pin, pout) = popen2(cmd, 'b')
     def writer():
         try:
             pin.write(s)