Mercurial > public > mercurial-scm > hg
comparison mercurial/util.py @ 7106:4674706b5b95
python2.6: use subprocess if available
author | Dirkjan Ochtman <dirkjan@ochtman.nl> |
---|---|
date | Sun, 05 Oct 2008 21:35:26 +0200 |
parents | 11229144aa01 |
children | 619ebf82cef2 |
comparison
equal
deleted
inserted
replaced
7105:31837416ef4d | 7106:4674706b5b95 |
---|---|
46 _sha1 = hashlib.sha1 | 46 _sha1 = hashlib.sha1 |
47 except ImportError: | 47 except ImportError: |
48 import sha | 48 import sha |
49 _sha1 = sha.sha | 49 _sha1 = sha.sha |
50 return _sha1(s) | 50 return _sha1(s) |
51 | |
52 try: | |
53 import subprocess | |
54 def popen2(cmd, mode='t', bufsize=-1): | |
55 p = subprocess.Popen(cmd, shell=True, bufsize=bufsize, close_fds=True, | |
56 stdin=subprocess.PIPE, stdout=subprocess.PIPE) | |
57 return p.stdin, p.stdout | |
58 def popen3(cmd, mode='t', bufsize=-1): | |
59 p = subprocess.Popen(cmd, shell=True, bufsize=bufsize, close_fds=True, | |
60 stdin=subprocess.PIPE, stdout=subprocess.PIPE, | |
61 stderr=subprocess.PIPE) | |
62 return p.stdin, p.stdout, p.stderr | |
63 def Popen3(cmd, capturestderr=False, bufsize=-1): | |
64 stderr = capturestderr and subprocess.PIPE or None | |
65 p = subprocess.Popen(cmd, shell=True, bufsize=bufsize, close_fds=True, | |
66 stdin=subprocess.PIPE, stdout=subprocess.PIPE, | |
67 stderr=stderr) | |
68 p.fromchild = p.stdout | |
69 p.tochild = p.stdin | |
70 p.childerr = p.stderr | |
71 return p | |
72 except ImportError: | |
73 subprocess = None | |
74 import popen2 as _popen2 | |
75 popen2 = _popen2.popen2 | |
76 Popen3 = _popen2.Popen3 | |
77 | |
51 | 78 |
52 try: | 79 try: |
53 _encoding = os.environ.get("HGENCODING") | 80 _encoding = os.environ.get("HGENCODING") |
54 if sys.platform == 'darwin' and not _encoding: | 81 if sys.platform == 'darwin' and not _encoding: |
55 # On darwin, getpreferredencoding ignores the locale environment and | 82 # On darwin, getpreferredencoding ignores the locale environment and |
181 | 208 |
182 return f | 209 return f |
183 | 210 |
184 def pipefilter(s, cmd): | 211 def pipefilter(s, cmd): |
185 '''filter string S through command CMD, returning its output''' | 212 '''filter string S through command CMD, returning its output''' |
186 (pin, pout) = os.popen2(cmd, 'b') | 213 (pin, pout) = popen2(cmd, 'b') |
187 def writer(): | 214 def writer(): |
188 try: | 215 try: |
189 pin.write(s) | 216 pin.write(s) |
190 pin.close() | 217 pin.close() |
191 except IOError, inst: | 218 except IOError, inst: |