##// END OF EJS Templates
procutil: rewrite popen() as a subprocess.Popen wrapper (issue4746) (API)...
Yuya Nishihara -
r37477:90c5ca71 default
parent child Browse files
Show More
@@ -469,9 +469,6 b' def shellsplit(s):'
469 def quotecommand(cmd):
469 def quotecommand(cmd):
470 return cmd
470 return cmd
471
471
472 def popen(command, mode='r'):
473 return os.popen(command, mode)
474
475 def testpid(pid):
472 def testpid(pid):
476 '''return False if pid dead, True if running or not sure'''
473 '''return False if pid dead, True if running or not sure'''
477 if pycompat.sysplatform == 'OpenVMS':
474 if pycompat.sysplatform == 'OpenVMS':
@@ -58,7 +58,6 b' findexe = platform.findexe'
58 getuser = platform.getuser
58 getuser = platform.getuser
59 getpid = os.getpid
59 getpid = os.getpid
60 hidewindow = platform.hidewindow
60 hidewindow = platform.hidewindow
61 popen = platform.popen
62 quotecommand = platform.quotecommand
61 quotecommand = platform.quotecommand
63 readpipe = platform.readpipe
62 readpipe = platform.readpipe
64 setbinary = platform.setbinary
63 setbinary = platform.setbinary
@@ -80,6 +79,49 b' except AttributeError:'
80
79
81 closefds = pycompat.isposix
80 closefds = pycompat.isposix
82
81
82 class _pfile(object):
83 """File-like wrapper for a stream opened by subprocess.Popen()"""
84
85 def __init__(self, proc, fp):
86 self._proc = proc
87 self._fp = fp
88
89 def close(self):
90 # unlike os.popen(), this returns an integer in subprocess coding
91 self._fp.close()
92 return self._proc.wait()
93
94 def __iter__(self):
95 return iter(self._fp)
96
97 def __getattr__(self, attr):
98 return getattr(self._fp, attr)
99
100 def __enter__(self):
101 return self
102
103 def __exit__(self, exc_type, exc_value, exc_tb):
104 self.close()
105
106 def popen(cmd, mode='rb', bufsize=-1):
107 if mode == 'rb':
108 return _popenreader(cmd, bufsize)
109 elif mode == 'wb':
110 return _popenwriter(cmd, bufsize)
111 raise error.ProgrammingError('unsupported mode: %r' % mode)
112
113 def _popenreader(cmd, bufsize):
114 p = subprocess.Popen(quotecommand(cmd), shell=True, bufsize=bufsize,
115 close_fds=closefds,
116 stdout=subprocess.PIPE)
117 return _pfile(p, p.stdout)
118
119 def _popenwriter(cmd, bufsize):
120 p = subprocess.Popen(quotecommand(cmd), shell=True, bufsize=bufsize,
121 close_fds=closefds,
122 stdin=subprocess.PIPE)
123 return _pfile(p, p.stdin)
124
83 def popen2(cmd, env=None, newlines=False):
125 def popen2(cmd, env=None, newlines=False):
84 # Setting bufsize to -1 lets the system decide the buffer size.
126 # Setting bufsize to -1 lets the system decide the buffer size.
85 # The default for bufsize is 0, meaning unbuffered. This leads to
127 # The default for bufsize is 0, meaning unbuffered. This leads to
@@ -311,13 +311,6 b' def quotecommand(cmd):'
311 return '"' + cmd + '"'
311 return '"' + cmd + '"'
312 return cmd
312 return cmd
313
313
314 def popen(command, mode='r'):
315 # Work around "popen spawned process may not write to stdout
316 # under windows"
317 # http://bugs.python.org/issue1366
318 command += " 2> %s" % pycompat.bytestr(os.devnull)
319 return os.popen(quotecommand(command), mode)
320
321 def explainexit(code):
314 def explainexit(code):
322 return _("exited with status %d") % code, code
315 return _("exited with status %d") % code, code
323
316
@@ -89,4 +89,12 b' Clone and apply patch:'
89 # User lines looks like this - but it _is_ just a comment
89 # User lines looks like this - but it _is_ just a comment
90
90
91
91
92
93 Error exit (issue4746)
94
95 $ hg import ../c/p --config ui.patch='sh -c "exit 1"'
96 applying ../c/p
97 abort: patch command failed: exited with status 1
98 [255]
99
92 $ cd ..
100 $ cd ..
General Comments 0
You need to be logged in to leave comments. Login now