##// END OF EJS Templates
py3: drop incorrect fsencode(findexe(...)) in procutil...
Martin von Zweigbergk -
r43152:acf80f9e default
parent child Browse files
Show More
@@ -1,544 +1,541 b''
1 # procutil.py - utility for managing processes and executable environment
1 # procutil.py - utility for managing processes and executable environment
2 #
2 #
3 # Copyright 2005 K. Thananchayan <thananck@yahoo.com>
3 # Copyright 2005 K. Thananchayan <thananck@yahoo.com>
4 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
4 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
5 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
5 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
6 #
6 #
7 # This software may be used and distributed according to the terms of the
7 # This software may be used and distributed according to the terms of the
8 # GNU General Public License version 2 or any later version.
8 # GNU General Public License version 2 or any later version.
9
9
10 from __future__ import absolute_import
10 from __future__ import absolute_import
11
11
12 import contextlib
12 import contextlib
13 import errno
13 import errno
14 import imp
14 import imp
15 import io
15 import io
16 import os
16 import os
17 import signal
17 import signal
18 import subprocess
18 import subprocess
19 import sys
19 import sys
20 import time
20 import time
21
21
22 from ..i18n import _
22 from ..i18n import _
23
23
24 from .. import (
24 from .. import (
25 encoding,
25 encoding,
26 error,
26 error,
27 policy,
27 policy,
28 pycompat,
28 pycompat,
29 )
29 )
30
30
31 osutil = policy.importmod(r'osutil')
31 osutil = policy.importmod(r'osutil')
32
32
33 stderr = pycompat.stderr
33 stderr = pycompat.stderr
34 stdin = pycompat.stdin
34 stdin = pycompat.stdin
35 stdout = pycompat.stdout
35 stdout = pycompat.stdout
36
36
37 def isatty(fp):
37 def isatty(fp):
38 try:
38 try:
39 return fp.isatty()
39 return fp.isatty()
40 except AttributeError:
40 except AttributeError:
41 return False
41 return False
42
42
43 # glibc determines buffering on first write to stdout - if we replace a TTY
43 # glibc determines buffering on first write to stdout - if we replace a TTY
44 # destined stdout with a pipe destined stdout (e.g. pager), we want line
44 # destined stdout with a pipe destined stdout (e.g. pager), we want line
45 # buffering (or unbuffered, on Windows)
45 # buffering (or unbuffered, on Windows)
46 if isatty(stdout):
46 if isatty(stdout):
47 if pycompat.iswindows:
47 if pycompat.iswindows:
48 # Windows doesn't support line buffering
48 # Windows doesn't support line buffering
49 stdout = os.fdopen(stdout.fileno(), r'wb', 0)
49 stdout = os.fdopen(stdout.fileno(), r'wb', 0)
50 else:
50 else:
51 stdout = os.fdopen(stdout.fileno(), r'wb', 1)
51 stdout = os.fdopen(stdout.fileno(), r'wb', 1)
52
52
53 if pycompat.iswindows:
53 if pycompat.iswindows:
54 from .. import windows as platform
54 from .. import windows as platform
55 stdout = platform.winstdout(stdout)
55 stdout = platform.winstdout(stdout)
56 else:
56 else:
57 from .. import posix as platform
57 from .. import posix as platform
58
58
59 findexe = platform.findexe
59 findexe = platform.findexe
60 _gethgcmd = platform.gethgcmd
60 _gethgcmd = platform.gethgcmd
61 getuser = platform.getuser
61 getuser = platform.getuser
62 getpid = os.getpid
62 getpid = os.getpid
63 hidewindow = platform.hidewindow
63 hidewindow = platform.hidewindow
64 quotecommand = platform.quotecommand
64 quotecommand = platform.quotecommand
65 readpipe = platform.readpipe
65 readpipe = platform.readpipe
66 setbinary = platform.setbinary
66 setbinary = platform.setbinary
67 setsignalhandler = platform.setsignalhandler
67 setsignalhandler = platform.setsignalhandler
68 shellquote = platform.shellquote
68 shellquote = platform.shellquote
69 shellsplit = platform.shellsplit
69 shellsplit = platform.shellsplit
70 spawndetached = platform.spawndetached
70 spawndetached = platform.spawndetached
71 sshargs = platform.sshargs
71 sshargs = platform.sshargs
72 testpid = platform.testpid
72 testpid = platform.testpid
73
73
74 try:
74 try:
75 setprocname = osutil.setprocname
75 setprocname = osutil.setprocname
76 except AttributeError:
76 except AttributeError:
77 pass
77 pass
78 try:
78 try:
79 unblocksignal = osutil.unblocksignal
79 unblocksignal = osutil.unblocksignal
80 except AttributeError:
80 except AttributeError:
81 pass
81 pass
82
82
83 closefds = pycompat.isposix
83 closefds = pycompat.isposix
84
84
85 def explainexit(code):
85 def explainexit(code):
86 """return a message describing a subprocess status
86 """return a message describing a subprocess status
87 (codes from kill are negative - not os.system/wait encoding)"""
87 (codes from kill are negative - not os.system/wait encoding)"""
88 if code >= 0:
88 if code >= 0:
89 return _("exited with status %d") % code
89 return _("exited with status %d") % code
90 return _("killed by signal %d") % -code
90 return _("killed by signal %d") % -code
91
91
92 class _pfile(object):
92 class _pfile(object):
93 """File-like wrapper for a stream opened by subprocess.Popen()"""
93 """File-like wrapper for a stream opened by subprocess.Popen()"""
94
94
95 def __init__(self, proc, fp):
95 def __init__(self, proc, fp):
96 self._proc = proc
96 self._proc = proc
97 self._fp = fp
97 self._fp = fp
98
98
99 def close(self):
99 def close(self):
100 # unlike os.popen(), this returns an integer in subprocess coding
100 # unlike os.popen(), this returns an integer in subprocess coding
101 self._fp.close()
101 self._fp.close()
102 return self._proc.wait()
102 return self._proc.wait()
103
103
104 def __iter__(self):
104 def __iter__(self):
105 return iter(self._fp)
105 return iter(self._fp)
106
106
107 def __getattr__(self, attr):
107 def __getattr__(self, attr):
108 return getattr(self._fp, attr)
108 return getattr(self._fp, attr)
109
109
110 def __enter__(self):
110 def __enter__(self):
111 return self
111 return self
112
112
113 def __exit__(self, exc_type, exc_value, exc_tb):
113 def __exit__(self, exc_type, exc_value, exc_tb):
114 self.close()
114 self.close()
115
115
116 def popen(cmd, mode='rb', bufsize=-1):
116 def popen(cmd, mode='rb', bufsize=-1):
117 if mode == 'rb':
117 if mode == 'rb':
118 return _popenreader(cmd, bufsize)
118 return _popenreader(cmd, bufsize)
119 elif mode == 'wb':
119 elif mode == 'wb':
120 return _popenwriter(cmd, bufsize)
120 return _popenwriter(cmd, bufsize)
121 raise error.ProgrammingError('unsupported mode: %r' % mode)
121 raise error.ProgrammingError('unsupported mode: %r' % mode)
122
122
123 def _popenreader(cmd, bufsize):
123 def _popenreader(cmd, bufsize):
124 p = subprocess.Popen(tonativestr(quotecommand(cmd)),
124 p = subprocess.Popen(tonativestr(quotecommand(cmd)),
125 shell=True, bufsize=bufsize,
125 shell=True, bufsize=bufsize,
126 close_fds=closefds,
126 close_fds=closefds,
127 stdout=subprocess.PIPE)
127 stdout=subprocess.PIPE)
128 return _pfile(p, p.stdout)
128 return _pfile(p, p.stdout)
129
129
130 def _popenwriter(cmd, bufsize):
130 def _popenwriter(cmd, bufsize):
131 p = subprocess.Popen(tonativestr(quotecommand(cmd)),
131 p = subprocess.Popen(tonativestr(quotecommand(cmd)),
132 shell=True, bufsize=bufsize,
132 shell=True, bufsize=bufsize,
133 close_fds=closefds,
133 close_fds=closefds,
134 stdin=subprocess.PIPE)
134 stdin=subprocess.PIPE)
135 return _pfile(p, p.stdin)
135 return _pfile(p, p.stdin)
136
136
137 def popen2(cmd, env=None):
137 def popen2(cmd, env=None):
138 # Setting bufsize to -1 lets the system decide the buffer size.
138 # Setting bufsize to -1 lets the system decide the buffer size.
139 # The default for bufsize is 0, meaning unbuffered. This leads to
139 # The default for bufsize is 0, meaning unbuffered. This leads to
140 # poor performance on Mac OS X: http://bugs.python.org/issue4194
140 # poor performance on Mac OS X: http://bugs.python.org/issue4194
141 p = subprocess.Popen(tonativestr(cmd),
141 p = subprocess.Popen(tonativestr(cmd),
142 shell=True, bufsize=-1,
142 shell=True, bufsize=-1,
143 close_fds=closefds,
143 close_fds=closefds,
144 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
144 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
145 env=tonativeenv(env))
145 env=tonativeenv(env))
146 return p.stdin, p.stdout
146 return p.stdin, p.stdout
147
147
148 def popen3(cmd, env=None):
148 def popen3(cmd, env=None):
149 stdin, stdout, stderr, p = popen4(cmd, env)
149 stdin, stdout, stderr, p = popen4(cmd, env)
150 return stdin, stdout, stderr
150 return stdin, stdout, stderr
151
151
152 def popen4(cmd, env=None, bufsize=-1):
152 def popen4(cmd, env=None, bufsize=-1):
153 p = subprocess.Popen(tonativestr(cmd),
153 p = subprocess.Popen(tonativestr(cmd),
154 shell=True, bufsize=bufsize,
154 shell=True, bufsize=bufsize,
155 close_fds=closefds,
155 close_fds=closefds,
156 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
156 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
157 stderr=subprocess.PIPE,
157 stderr=subprocess.PIPE,
158 env=tonativeenv(env))
158 env=tonativeenv(env))
159 return p.stdin, p.stdout, p.stderr, p
159 return p.stdin, p.stdout, p.stderr, p
160
160
161 def pipefilter(s, cmd):
161 def pipefilter(s, cmd):
162 '''filter string S through command CMD, returning its output'''
162 '''filter string S through command CMD, returning its output'''
163 p = subprocess.Popen(tonativestr(cmd),
163 p = subprocess.Popen(tonativestr(cmd),
164 shell=True, close_fds=closefds,
164 shell=True, close_fds=closefds,
165 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
165 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
166 pout, perr = p.communicate(s)
166 pout, perr = p.communicate(s)
167 return pout
167 return pout
168
168
169 def tempfilter(s, cmd):
169 def tempfilter(s, cmd):
170 '''filter string S through a pair of temporary files with CMD.
170 '''filter string S through a pair of temporary files with CMD.
171 CMD is used as a template to create the real command to be run,
171 CMD is used as a template to create the real command to be run,
172 with the strings INFILE and OUTFILE replaced by the real names of
172 with the strings INFILE and OUTFILE replaced by the real names of
173 the temporary files generated.'''
173 the temporary files generated.'''
174 inname, outname = None, None
174 inname, outname = None, None
175 try:
175 try:
176 infd, inname = pycompat.mkstemp(prefix='hg-filter-in-')
176 infd, inname = pycompat.mkstemp(prefix='hg-filter-in-')
177 fp = os.fdopen(infd, r'wb')
177 fp = os.fdopen(infd, r'wb')
178 fp.write(s)
178 fp.write(s)
179 fp.close()
179 fp.close()
180 outfd, outname = pycompat.mkstemp(prefix='hg-filter-out-')
180 outfd, outname = pycompat.mkstemp(prefix='hg-filter-out-')
181 os.close(outfd)
181 os.close(outfd)
182 cmd = cmd.replace('INFILE', inname)
182 cmd = cmd.replace('INFILE', inname)
183 cmd = cmd.replace('OUTFILE', outname)
183 cmd = cmd.replace('OUTFILE', outname)
184 code = system(cmd)
184 code = system(cmd)
185 if pycompat.sysplatform == 'OpenVMS' and code & 1:
185 if pycompat.sysplatform == 'OpenVMS' and code & 1:
186 code = 0
186 code = 0
187 if code:
187 if code:
188 raise error.Abort(_("command '%s' failed: %s") %
188 raise error.Abort(_("command '%s' failed: %s") %
189 (cmd, explainexit(code)))
189 (cmd, explainexit(code)))
190 with open(outname, 'rb') as fp:
190 with open(outname, 'rb') as fp:
191 return fp.read()
191 return fp.read()
192 finally:
192 finally:
193 try:
193 try:
194 if inname:
194 if inname:
195 os.unlink(inname)
195 os.unlink(inname)
196 except OSError:
196 except OSError:
197 pass
197 pass
198 try:
198 try:
199 if outname:
199 if outname:
200 os.unlink(outname)
200 os.unlink(outname)
201 except OSError:
201 except OSError:
202 pass
202 pass
203
203
204 _filtertable = {
204 _filtertable = {
205 'tempfile:': tempfilter,
205 'tempfile:': tempfilter,
206 'pipe:': pipefilter,
206 'pipe:': pipefilter,
207 }
207 }
208
208
209 def filter(s, cmd):
209 def filter(s, cmd):
210 "filter a string through a command that transforms its input to its output"
210 "filter a string through a command that transforms its input to its output"
211 for name, fn in _filtertable.iteritems():
211 for name, fn in _filtertable.iteritems():
212 if cmd.startswith(name):
212 if cmd.startswith(name):
213 return fn(s, cmd[len(name):].lstrip())
213 return fn(s, cmd[len(name):].lstrip())
214 return pipefilter(s, cmd)
214 return pipefilter(s, cmd)
215
215
216 def mainfrozen():
216 def mainfrozen():
217 """return True if we are a frozen executable.
217 """return True if we are a frozen executable.
218
218
219 The code supports py2exe (most common, Windows only) and tools/freeze
219 The code supports py2exe (most common, Windows only) and tools/freeze
220 (portable, not much used).
220 (portable, not much used).
221 """
221 """
222 return (pycompat.safehasattr(sys, "frozen") or # new py2exe
222 return (pycompat.safehasattr(sys, "frozen") or # new py2exe
223 pycompat.safehasattr(sys, "importers") or # old py2exe
223 pycompat.safehasattr(sys, "importers") or # old py2exe
224 imp.is_frozen(r"__main__")) # tools/freeze
224 imp.is_frozen(r"__main__")) # tools/freeze
225
225
226 _hgexecutable = None
226 _hgexecutable = None
227
227
228 def hgexecutable():
228 def hgexecutable():
229 """return location of the 'hg' executable.
229 """return location of the 'hg' executable.
230
230
231 Defaults to $HG or 'hg' in the search path.
231 Defaults to $HG or 'hg' in the search path.
232 """
232 """
233 if _hgexecutable is None:
233 if _hgexecutable is None:
234 hg = encoding.environ.get('HG')
234 hg = encoding.environ.get('HG')
235 mainmod = sys.modules[r'__main__']
235 mainmod = sys.modules[r'__main__']
236 if hg:
236 if hg:
237 _sethgexecutable(hg)
237 _sethgexecutable(hg)
238 elif mainfrozen():
238 elif mainfrozen():
239 if getattr(sys, 'frozen', None) == 'macosx_app':
239 if getattr(sys, 'frozen', None) == 'macosx_app':
240 # Env variable set by py2app
240 # Env variable set by py2app
241 _sethgexecutable(encoding.environ['EXECUTABLEPATH'])
241 _sethgexecutable(encoding.environ['EXECUTABLEPATH'])
242 else:
242 else:
243 _sethgexecutable(pycompat.sysexecutable)
243 _sethgexecutable(pycompat.sysexecutable)
244 elif (not pycompat.iswindows and os.path.basename(
244 elif (not pycompat.iswindows and os.path.basename(
245 pycompat.fsencode(getattr(mainmod, '__file__', ''))) == 'hg'):
245 pycompat.fsencode(getattr(mainmod, '__file__', ''))) == 'hg'):
246 _sethgexecutable(pycompat.fsencode(mainmod.__file__))
246 _sethgexecutable(pycompat.fsencode(mainmod.__file__))
247 else:
247 else:
248 exe = findexe('hg')
248 _sethgexecutable(findexe('hg') or
249 if exe:
249 os.path.basename(pycompat.sysargv[0]))
250 _sethgexecutable(pycompat.fsencode(exe))
251 else:
252 _sethgexecutable(os.path.basename(pycompat.sysargv[0]))
253 return _hgexecutable
250 return _hgexecutable
254
251
255 def _sethgexecutable(path):
252 def _sethgexecutable(path):
256 """set location of the 'hg' executable"""
253 """set location of the 'hg' executable"""
257 global _hgexecutable
254 global _hgexecutable
258 _hgexecutable = path
255 _hgexecutable = path
259
256
260 def _testfileno(f, stdf):
257 def _testfileno(f, stdf):
261 fileno = getattr(f, 'fileno', None)
258 fileno = getattr(f, 'fileno', None)
262 try:
259 try:
263 return fileno and fileno() == stdf.fileno()
260 return fileno and fileno() == stdf.fileno()
264 except io.UnsupportedOperation:
261 except io.UnsupportedOperation:
265 return False # fileno() raised UnsupportedOperation
262 return False # fileno() raised UnsupportedOperation
266
263
267 def isstdin(f):
264 def isstdin(f):
268 return _testfileno(f, sys.__stdin__)
265 return _testfileno(f, sys.__stdin__)
269
266
270 def isstdout(f):
267 def isstdout(f):
271 return _testfileno(f, sys.__stdout__)
268 return _testfileno(f, sys.__stdout__)
272
269
273 def protectstdio(uin, uout):
270 def protectstdio(uin, uout):
274 """Duplicate streams and redirect original if (uin, uout) are stdio
271 """Duplicate streams and redirect original if (uin, uout) are stdio
275
272
276 If uin is stdin, it's redirected to /dev/null. If uout is stdout, it's
273 If uin is stdin, it's redirected to /dev/null. If uout is stdout, it's
277 redirected to stderr so the output is still readable.
274 redirected to stderr so the output is still readable.
278
275
279 Returns (fin, fout) which point to the original (uin, uout) fds, but
276 Returns (fin, fout) which point to the original (uin, uout) fds, but
280 may be copy of (uin, uout). The returned streams can be considered
277 may be copy of (uin, uout). The returned streams can be considered
281 "owned" in that print(), exec(), etc. never reach to them.
278 "owned" in that print(), exec(), etc. never reach to them.
282 """
279 """
283 uout.flush()
280 uout.flush()
284 fin, fout = uin, uout
281 fin, fout = uin, uout
285 if _testfileno(uin, stdin):
282 if _testfileno(uin, stdin):
286 newfd = os.dup(uin.fileno())
283 newfd = os.dup(uin.fileno())
287 nullfd = os.open(os.devnull, os.O_RDONLY)
284 nullfd = os.open(os.devnull, os.O_RDONLY)
288 os.dup2(nullfd, uin.fileno())
285 os.dup2(nullfd, uin.fileno())
289 os.close(nullfd)
286 os.close(nullfd)
290 fin = os.fdopen(newfd, r'rb')
287 fin = os.fdopen(newfd, r'rb')
291 if _testfileno(uout, stdout):
288 if _testfileno(uout, stdout):
292 newfd = os.dup(uout.fileno())
289 newfd = os.dup(uout.fileno())
293 os.dup2(stderr.fileno(), uout.fileno())
290 os.dup2(stderr.fileno(), uout.fileno())
294 fout = os.fdopen(newfd, r'wb')
291 fout = os.fdopen(newfd, r'wb')
295 return fin, fout
292 return fin, fout
296
293
297 def restorestdio(uin, uout, fin, fout):
294 def restorestdio(uin, uout, fin, fout):
298 """Restore (uin, uout) streams from possibly duplicated (fin, fout)"""
295 """Restore (uin, uout) streams from possibly duplicated (fin, fout)"""
299 uout.flush()
296 uout.flush()
300 for f, uif in [(fin, uin), (fout, uout)]:
297 for f, uif in [(fin, uin), (fout, uout)]:
301 if f is not uif:
298 if f is not uif:
302 os.dup2(f.fileno(), uif.fileno())
299 os.dup2(f.fileno(), uif.fileno())
303 f.close()
300 f.close()
304
301
305 def shellenviron(environ=None):
302 def shellenviron(environ=None):
306 """return environ with optional override, useful for shelling out"""
303 """return environ with optional override, useful for shelling out"""
307 def py2shell(val):
304 def py2shell(val):
308 'convert python object into string that is useful to shell'
305 'convert python object into string that is useful to shell'
309 if val is None or val is False:
306 if val is None or val is False:
310 return '0'
307 return '0'
311 if val is True:
308 if val is True:
312 return '1'
309 return '1'
313 return pycompat.bytestr(val)
310 return pycompat.bytestr(val)
314 env = dict(encoding.environ)
311 env = dict(encoding.environ)
315 if environ:
312 if environ:
316 env.update((k, py2shell(v)) for k, v in environ.iteritems())
313 env.update((k, py2shell(v)) for k, v in environ.iteritems())
317 env['HG'] = hgexecutable()
314 env['HG'] = hgexecutable()
318 return env
315 return env
319
316
320 if pycompat.iswindows:
317 if pycompat.iswindows:
321 def shelltonative(cmd, env):
318 def shelltonative(cmd, env):
322 return platform.shelltocmdexe(cmd, shellenviron(env))
319 return platform.shelltocmdexe(cmd, shellenviron(env))
323
320
324 tonativestr = encoding.strfromlocal
321 tonativestr = encoding.strfromlocal
325 else:
322 else:
326 def shelltonative(cmd, env):
323 def shelltonative(cmd, env):
327 return cmd
324 return cmd
328
325
329 tonativestr = pycompat.identity
326 tonativestr = pycompat.identity
330
327
331 def tonativeenv(env):
328 def tonativeenv(env):
332 '''convert the environment from bytes to strings suitable for Popen(), etc.
329 '''convert the environment from bytes to strings suitable for Popen(), etc.
333 '''
330 '''
334 return pycompat.rapply(tonativestr, env)
331 return pycompat.rapply(tonativestr, env)
335
332
336 def system(cmd, environ=None, cwd=None, out=None):
333 def system(cmd, environ=None, cwd=None, out=None):
337 '''enhanced shell command execution.
334 '''enhanced shell command execution.
338 run with environment maybe modified, maybe in different dir.
335 run with environment maybe modified, maybe in different dir.
339
336
340 if out is specified, it is assumed to be a file-like object that has a
337 if out is specified, it is assumed to be a file-like object that has a
341 write() method. stdout and stderr will be redirected to out.'''
338 write() method. stdout and stderr will be redirected to out.'''
342 try:
339 try:
343 stdout.flush()
340 stdout.flush()
344 except Exception:
341 except Exception:
345 pass
342 pass
346 cmd = quotecommand(cmd)
343 cmd = quotecommand(cmd)
347 env = shellenviron(environ)
344 env = shellenviron(environ)
348 if out is None or isstdout(out):
345 if out is None or isstdout(out):
349 rc = subprocess.call(tonativestr(cmd),
346 rc = subprocess.call(tonativestr(cmd),
350 shell=True, close_fds=closefds,
347 shell=True, close_fds=closefds,
351 env=tonativeenv(env),
348 env=tonativeenv(env),
352 cwd=pycompat.rapply(tonativestr, cwd))
349 cwd=pycompat.rapply(tonativestr, cwd))
353 else:
350 else:
354 proc = subprocess.Popen(tonativestr(cmd),
351 proc = subprocess.Popen(tonativestr(cmd),
355 shell=True, close_fds=closefds,
352 shell=True, close_fds=closefds,
356 env=tonativeenv(env),
353 env=tonativeenv(env),
357 cwd=pycompat.rapply(tonativestr, cwd),
354 cwd=pycompat.rapply(tonativestr, cwd),
358 stdout=subprocess.PIPE,
355 stdout=subprocess.PIPE,
359 stderr=subprocess.STDOUT)
356 stderr=subprocess.STDOUT)
360 for line in iter(proc.stdout.readline, ''):
357 for line in iter(proc.stdout.readline, ''):
361 out.write(line)
358 out.write(line)
362 proc.wait()
359 proc.wait()
363 rc = proc.returncode
360 rc = proc.returncode
364 if pycompat.sysplatform == 'OpenVMS' and rc & 1:
361 if pycompat.sysplatform == 'OpenVMS' and rc & 1:
365 rc = 0
362 rc = 0
366 return rc
363 return rc
367
364
368 def gui():
365 def gui():
369 '''Are we running in a GUI?'''
366 '''Are we running in a GUI?'''
370 if pycompat.isdarwin:
367 if pycompat.isdarwin:
371 if 'SSH_CONNECTION' in encoding.environ:
368 if 'SSH_CONNECTION' in encoding.environ:
372 # handle SSH access to a box where the user is logged in
369 # handle SSH access to a box where the user is logged in
373 return False
370 return False
374 elif getattr(osutil, 'isgui', None):
371 elif getattr(osutil, 'isgui', None):
375 # check if a CoreGraphics session is available
372 # check if a CoreGraphics session is available
376 return osutil.isgui()
373 return osutil.isgui()
377 else:
374 else:
378 # pure build; use a safe default
375 # pure build; use a safe default
379 return True
376 return True
380 else:
377 else:
381 return pycompat.iswindows or encoding.environ.get("DISPLAY")
378 return pycompat.iswindows or encoding.environ.get("DISPLAY")
382
379
383 def hgcmd():
380 def hgcmd():
384 """Return the command used to execute current hg
381 """Return the command used to execute current hg
385
382
386 This is different from hgexecutable() because on Windows we want
383 This is different from hgexecutable() because on Windows we want
387 to avoid things opening new shell windows like batch files, so we
384 to avoid things opening new shell windows like batch files, so we
388 get either the python call or current executable.
385 get either the python call or current executable.
389 """
386 """
390 if mainfrozen():
387 if mainfrozen():
391 if getattr(sys, 'frozen', None) == 'macosx_app':
388 if getattr(sys, 'frozen', None) == 'macosx_app':
392 # Env variable set by py2app
389 # Env variable set by py2app
393 return [encoding.environ['EXECUTABLEPATH']]
390 return [encoding.environ['EXECUTABLEPATH']]
394 else:
391 else:
395 return [pycompat.sysexecutable]
392 return [pycompat.sysexecutable]
396 return _gethgcmd()
393 return _gethgcmd()
397
394
398 def rundetached(args, condfn):
395 def rundetached(args, condfn):
399 """Execute the argument list in a detached process.
396 """Execute the argument list in a detached process.
400
397
401 condfn is a callable which is called repeatedly and should return
398 condfn is a callable which is called repeatedly and should return
402 True once the child process is known to have started successfully.
399 True once the child process is known to have started successfully.
403 At this point, the child process PID is returned. If the child
400 At this point, the child process PID is returned. If the child
404 process fails to start or finishes before condfn() evaluates to
401 process fails to start or finishes before condfn() evaluates to
405 True, return -1.
402 True, return -1.
406 """
403 """
407 # Windows case is easier because the child process is either
404 # Windows case is easier because the child process is either
408 # successfully starting and validating the condition or exiting
405 # successfully starting and validating the condition or exiting
409 # on failure. We just poll on its PID. On Unix, if the child
406 # on failure. We just poll on its PID. On Unix, if the child
410 # process fails to start, it will be left in a zombie state until
407 # process fails to start, it will be left in a zombie state until
411 # the parent wait on it, which we cannot do since we expect a long
408 # the parent wait on it, which we cannot do since we expect a long
412 # running process on success. Instead we listen for SIGCHLD telling
409 # running process on success. Instead we listen for SIGCHLD telling
413 # us our child process terminated.
410 # us our child process terminated.
414 terminated = set()
411 terminated = set()
415 def handler(signum, frame):
412 def handler(signum, frame):
416 terminated.add(os.wait())
413 terminated.add(os.wait())
417 prevhandler = None
414 prevhandler = None
418 SIGCHLD = getattr(signal, 'SIGCHLD', None)
415 SIGCHLD = getattr(signal, 'SIGCHLD', None)
419 if SIGCHLD is not None:
416 if SIGCHLD is not None:
420 prevhandler = signal.signal(SIGCHLD, handler)
417 prevhandler = signal.signal(SIGCHLD, handler)
421 try:
418 try:
422 pid = spawndetached(args)
419 pid = spawndetached(args)
423 while not condfn():
420 while not condfn():
424 if ((pid in terminated or not testpid(pid))
421 if ((pid in terminated or not testpid(pid))
425 and not condfn()):
422 and not condfn()):
426 return -1
423 return -1
427 time.sleep(0.1)
424 time.sleep(0.1)
428 return pid
425 return pid
429 finally:
426 finally:
430 if prevhandler is not None:
427 if prevhandler is not None:
431 signal.signal(signal.SIGCHLD, prevhandler)
428 signal.signal(signal.SIGCHLD, prevhandler)
432
429
433 @contextlib.contextmanager
430 @contextlib.contextmanager
434 def uninterruptible(warn):
431 def uninterruptible(warn):
435 """Inhibit SIGINT handling on a region of code.
432 """Inhibit SIGINT handling on a region of code.
436
433
437 Note that if this is called in a non-main thread, it turns into a no-op.
434 Note that if this is called in a non-main thread, it turns into a no-op.
438
435
439 Args:
436 Args:
440 warn: A callable which takes no arguments, and returns True if the
437 warn: A callable which takes no arguments, and returns True if the
441 previous signal handling should be restored.
438 previous signal handling should be restored.
442 """
439 """
443
440
444 oldsiginthandler = [signal.getsignal(signal.SIGINT)]
441 oldsiginthandler = [signal.getsignal(signal.SIGINT)]
445 shouldbail = []
442 shouldbail = []
446
443
447 def disabledsiginthandler(*args):
444 def disabledsiginthandler(*args):
448 if warn():
445 if warn():
449 signal.signal(signal.SIGINT, oldsiginthandler[0])
446 signal.signal(signal.SIGINT, oldsiginthandler[0])
450 del oldsiginthandler[0]
447 del oldsiginthandler[0]
451 shouldbail.append(True)
448 shouldbail.append(True)
452
449
453 try:
450 try:
454 try:
451 try:
455 signal.signal(signal.SIGINT, disabledsiginthandler)
452 signal.signal(signal.SIGINT, disabledsiginthandler)
456 except ValueError:
453 except ValueError:
457 # wrong thread, oh well, we tried
454 # wrong thread, oh well, we tried
458 del oldsiginthandler[0]
455 del oldsiginthandler[0]
459 yield
456 yield
460 finally:
457 finally:
461 if oldsiginthandler:
458 if oldsiginthandler:
462 signal.signal(signal.SIGINT, oldsiginthandler[0])
459 signal.signal(signal.SIGINT, oldsiginthandler[0])
463 if shouldbail:
460 if shouldbail:
464 raise KeyboardInterrupt
461 raise KeyboardInterrupt
465
462
466 if pycompat.iswindows:
463 if pycompat.iswindows:
467 # no fork on Windows, but we can create a detached process
464 # no fork on Windows, but we can create a detached process
468 # https://msdn.microsoft.com/en-us/library/windows/desktop/ms684863.aspx
465 # https://msdn.microsoft.com/en-us/library/windows/desktop/ms684863.aspx
469 # No stdlib constant exists for this value
466 # No stdlib constant exists for this value
470 DETACHED_PROCESS = 0x00000008
467 DETACHED_PROCESS = 0x00000008
471 # Following creation flags might create a console GUI window.
468 # Following creation flags might create a console GUI window.
472 # Using subprocess.CREATE_NEW_CONSOLE might helps.
469 # Using subprocess.CREATE_NEW_CONSOLE might helps.
473 # See https://phab.mercurial-scm.org/D1701 for discussion
470 # See https://phab.mercurial-scm.org/D1701 for discussion
474 _creationflags = DETACHED_PROCESS | subprocess.CREATE_NEW_PROCESS_GROUP
471 _creationflags = DETACHED_PROCESS | subprocess.CREATE_NEW_PROCESS_GROUP
475
472
476 def runbgcommand(
473 def runbgcommand(
477 script, env, shell=False, stdout=None, stderr=None, ensurestart=True):
474 script, env, shell=False, stdout=None, stderr=None, ensurestart=True):
478 '''Spawn a command without waiting for it to finish.'''
475 '''Spawn a command without waiting for it to finish.'''
479 # we can't use close_fds *and* redirect stdin. I'm not sure that we
476 # we can't use close_fds *and* redirect stdin. I'm not sure that we
480 # need to because the detached process has no console connection.
477 # need to because the detached process has no console connection.
481 subprocess.Popen(
478 subprocess.Popen(
482 tonativestr(script),
479 tonativestr(script),
483 shell=shell, env=tonativeenv(env), close_fds=True,
480 shell=shell, env=tonativeenv(env), close_fds=True,
484 creationflags=_creationflags, stdout=stdout,
481 creationflags=_creationflags, stdout=stdout,
485 stderr=stderr)
482 stderr=stderr)
486 else:
483 else:
487 def runbgcommand(
484 def runbgcommand(
488 cmd, env, shell=False, stdout=None, stderr=None, ensurestart=True):
485 cmd, env, shell=False, stdout=None, stderr=None, ensurestart=True):
489 '''Spawn a command without waiting for it to finish.'''
486 '''Spawn a command without waiting for it to finish.'''
490 # double-fork to completely detach from the parent process
487 # double-fork to completely detach from the parent process
491 # based on http://code.activestate.com/recipes/278731
488 # based on http://code.activestate.com/recipes/278731
492 pid = os.fork()
489 pid = os.fork()
493 if pid:
490 if pid:
494 if not ensurestart:
491 if not ensurestart:
495 return
492 return
496 # Parent process
493 # Parent process
497 (_pid, status) = os.waitpid(pid, 0)
494 (_pid, status) = os.waitpid(pid, 0)
498 if os.WIFEXITED(status):
495 if os.WIFEXITED(status):
499 returncode = os.WEXITSTATUS(status)
496 returncode = os.WEXITSTATUS(status)
500 else:
497 else:
501 returncode = -os.WTERMSIG(status)
498 returncode = -os.WTERMSIG(status)
502 if returncode != 0:
499 if returncode != 0:
503 # The child process's return code is 0 on success, an errno
500 # The child process's return code is 0 on success, an errno
504 # value on failure, or 255 if we don't have a valid errno
501 # value on failure, or 255 if we don't have a valid errno
505 # value.
502 # value.
506 #
503 #
507 # (It would be slightly nicer to return the full exception info
504 # (It would be slightly nicer to return the full exception info
508 # over a pipe as the subprocess module does. For now it
505 # over a pipe as the subprocess module does. For now it
509 # doesn't seem worth adding that complexity here, though.)
506 # doesn't seem worth adding that complexity here, though.)
510 if returncode == 255:
507 if returncode == 255:
511 returncode = errno.EINVAL
508 returncode = errno.EINVAL
512 raise OSError(returncode, 'error running %r: %s' %
509 raise OSError(returncode, 'error running %r: %s' %
513 (cmd, os.strerror(returncode)))
510 (cmd, os.strerror(returncode)))
514 return
511 return
515
512
516 returncode = 255
513 returncode = 255
517 try:
514 try:
518 # Start a new session
515 # Start a new session
519 os.setsid()
516 os.setsid()
520
517
521 stdin = open(os.devnull, 'r')
518 stdin = open(os.devnull, 'r')
522 if stdout is None:
519 if stdout is None:
523 stdout = open(os.devnull, 'w')
520 stdout = open(os.devnull, 'w')
524 if stderr is None:
521 if stderr is None:
525 stderr = open(os.devnull, 'w')
522 stderr = open(os.devnull, 'w')
526
523
527 # connect stdin to devnull to make sure the subprocess can't
524 # connect stdin to devnull to make sure the subprocess can't
528 # muck up that stream for mercurial.
525 # muck up that stream for mercurial.
529 subprocess.Popen(
526 subprocess.Popen(
530 cmd, shell=shell, env=env, close_fds=True,
527 cmd, shell=shell, env=env, close_fds=True,
531 stdin=stdin, stdout=stdout, stderr=stderr)
528 stdin=stdin, stdout=stdout, stderr=stderr)
532 returncode = 0
529 returncode = 0
533 except EnvironmentError as ex:
530 except EnvironmentError as ex:
534 returncode = (ex.errno & 0xff)
531 returncode = (ex.errno & 0xff)
535 if returncode == 0:
532 if returncode == 0:
536 # This shouldn't happen, but just in case make sure the
533 # This shouldn't happen, but just in case make sure the
537 # return code is never 0 here.
534 # return code is never 0 here.
538 returncode = 255
535 returncode = 255
539 except Exception:
536 except Exception:
540 returncode = 255
537 returncode = 255
541 finally:
538 finally:
542 # mission accomplished, this child needs to exit and not
539 # mission accomplished, this child needs to exit and not
543 # continue the hg process here.
540 # continue the hg process here.
544 os._exit(returncode)
541 os._exit(returncode)
General Comments 0
You need to be logged in to leave comments. Login now