Mercurial > hg-stable
annotate mercurial/utils/procutil.py @ 37127:6715e8035b4f
procutil: introduce context-manager interface for protect/restorestdio
The code looks slightly cleaner since it was pretty easy to pass arguments
in wrong order.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Sun, 25 Mar 2018 11:58:05 +0900 |
parents | 0216232f21ab |
children | ac71cbad5da3 |
rev | line source |
---|---|
37121
5be286db5fb5
procutil: move process/executable management functions to new module
Yuya Nishihara <yuya@tcha.org>
parents:
37120
diff
changeset
|
1 # procutil.py - utility for managing processes and executable environment |
8226
8b2cd04a6e97
put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
2 # |
8b2cd04a6e97
put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
3 # Copyright 2005 K. Thananchayan <thananck@yahoo.com> |
8b2cd04a6e97
put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
4 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> |
8b2cd04a6e97
put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
5 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com> |
8b2cd04a6e97
put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
6 # |
8b2cd04a6e97
put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
7 # This software may be used and distributed according to the terms of the |
10263 | 8 # GNU General Public License version 2 or any later version. |
1082 | 9 |
37121
5be286db5fb5
procutil: move process/executable management functions to new module
Yuya Nishihara <yuya@tcha.org>
parents:
37120
diff
changeset
|
10 from __future__ import absolute_import |
27358
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
11 |
37127
6715e8035b4f
procutil: introduce context-manager interface for protect/restorestdio
Yuya Nishihara <yuya@tcha.org>
parents:
37126
diff
changeset
|
12 import contextlib |
27358
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
13 import imp |
36462
1ca4e86c7265
util: handle fileno() on Python 3 throwing io.UnsupportedOperation
Augie Fackler <augie@google.com>
parents:
36452
diff
changeset
|
14 import io |
27358
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
15 import os |
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
16 import signal |
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
17 import subprocess |
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
18 import sys |
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
19 import tempfile |
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
20 import time |
3769 | 21 |
37121
5be286db5fb5
procutil: move process/executable management functions to new module
Yuya Nishihara <yuya@tcha.org>
parents:
37120
diff
changeset
|
22 from ..i18n import _ |
5be286db5fb5
procutil: move process/executable management functions to new module
Yuya Nishihara <yuya@tcha.org>
parents:
37120
diff
changeset
|
23 |
5be286db5fb5
procutil: move process/executable management functions to new module
Yuya Nishihara <yuya@tcha.org>
parents:
37120
diff
changeset
|
24 from .. import ( |
27358
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
25 encoding, |
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
26 error, |
32406
a9c71d578a1c
osutil: switch to policy importer
Yuya Nishihara <yuya@tcha.org>
parents:
32346
diff
changeset
|
27 policy, |
28818
6041fb8f2da8
pycompat: add empty and queue to handle py3 divergence
timeless <timeless@mozdev.org>
parents:
28497
diff
changeset
|
28 pycompat, |
37086
f99d64e8a4e4
stringutil: move generic string helpers to new module
Yuya Nishihara <yuya@tcha.org>
parents:
37085
diff
changeset
|
29 ) |
3769 | 30 |
32406
a9c71d578a1c
osutil: switch to policy importer
Yuya Nishihara <yuya@tcha.org>
parents:
32346
diff
changeset
|
31 osutil = policy.importmod(r'osutil') |
32245
4462a981e8df
base85: proxy through util module
Yuya Nishihara <yuya@tcha.org>
parents:
32203
diff
changeset
|
32 |
30481
277f4fe6d01a
py3: provide bytes stdin/out/err through util module
Yuya Nishihara <yuya@tcha.org>
parents:
30480
diff
changeset
|
33 stderr = pycompat.stderr |
277f4fe6d01a
py3: provide bytes stdin/out/err through util module
Yuya Nishihara <yuya@tcha.org>
parents:
30480
diff
changeset
|
34 stdin = pycompat.stdin |
277f4fe6d01a
py3: provide bytes stdin/out/err through util module
Yuya Nishihara <yuya@tcha.org>
parents:
30480
diff
changeset
|
35 stdout = pycompat.stdout |
32131
377c74ef008d
win32mbcs: avoid unintentional failure at colorization
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
31952
diff
changeset
|
36 |
30908
3a4c0905f357
util: always force line buffered stdout when stdout is a tty (BC)
Simon Farnsworth <simonfar@fb.com>
parents:
30854
diff
changeset
|
37 def isatty(fp): |
3a4c0905f357
util: always force line buffered stdout when stdout is a tty (BC)
Simon Farnsworth <simonfar@fb.com>
parents:
30854
diff
changeset
|
38 try: |
3a4c0905f357
util: always force line buffered stdout when stdout is a tty (BC)
Simon Farnsworth <simonfar@fb.com>
parents:
30854
diff
changeset
|
39 return fp.isatty() |
3a4c0905f357
util: always force line buffered stdout when stdout is a tty (BC)
Simon Farnsworth <simonfar@fb.com>
parents:
30854
diff
changeset
|
40 except AttributeError: |
3a4c0905f357
util: always force line buffered stdout when stdout is a tty (BC)
Simon Farnsworth <simonfar@fb.com>
parents:
30854
diff
changeset
|
41 return False |
3a4c0905f357
util: always force line buffered stdout when stdout is a tty (BC)
Simon Farnsworth <simonfar@fb.com>
parents:
30854
diff
changeset
|
42 |
3a4c0905f357
util: always force line buffered stdout when stdout is a tty (BC)
Simon Farnsworth <simonfar@fb.com>
parents:
30854
diff
changeset
|
43 # glibc determines buffering on first write to stdout - if we replace a TTY |
3a4c0905f357
util: always force line buffered stdout when stdout is a tty (BC)
Simon Farnsworth <simonfar@fb.com>
parents:
30854
diff
changeset
|
44 # destined stdout with a pipe destined stdout (e.g. pager), we want line |
3a4c0905f357
util: always force line buffered stdout when stdout is a tty (BC)
Simon Farnsworth <simonfar@fb.com>
parents:
30854
diff
changeset
|
45 # buffering |
3a4c0905f357
util: always force line buffered stdout when stdout is a tty (BC)
Simon Farnsworth <simonfar@fb.com>
parents:
30854
diff
changeset
|
46 if isatty(stdout): |
36843
5bc7ff103081
py3: use r'' instead of sysstr('') to get around code transformer
Yuya Nishihara <yuya@tcha.org>
parents:
36840
diff
changeset
|
47 stdout = os.fdopen(stdout.fileno(), r'wb', 1) |
30908
3a4c0905f357
util: always force line buffered stdout when stdout is a tty (BC)
Simon Farnsworth <simonfar@fb.com>
parents:
30854
diff
changeset
|
48 |
34645 | 49 if pycompat.iswindows: |
37121
5be286db5fb5
procutil: move process/executable management functions to new module
Yuya Nishihara <yuya@tcha.org>
parents:
37120
diff
changeset
|
50 from .. import windows as platform |
30908
3a4c0905f357
util: always force line buffered stdout when stdout is a tty (BC)
Simon Farnsworth <simonfar@fb.com>
parents:
30854
diff
changeset
|
51 stdout = platform.winstdout(stdout) |
14912
ec46a7da9f2c
util: move windows and posix wildcard imports to begin of file
Adrian Buehlmann <adrian@cadifra.com>
parents:
14911
diff
changeset
|
52 else: |
37121
5be286db5fb5
procutil: move process/executable management functions to new module
Yuya Nishihara <yuya@tcha.org>
parents:
37120
diff
changeset
|
53 from .. import posix as platform |
14926
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
54 |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
55 explainexit = platform.explainexit |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
56 findexe = platform.findexe |
37118
49d6ba67c93f
util: mark platform-specific gethgcmd() as private
Yuya Nishihara <yuya@tcha.org>
parents:
37102
diff
changeset
|
57 _gethgcmd = platform.gethgcmd |
14926
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
58 getuser = platform.getuser |
28027
14033c5dd261
util: enable getpid to be replaced
timeless <timeless@mozdev.org>
parents:
27785
diff
changeset
|
59 getpid = os.getpid |
14926
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
60 hidewindow = platform.hidewindow |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
61 popen = platform.popen |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
62 quotecommand = platform.quotecommand |
22245
234e4c24b980
platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
21914
diff
changeset
|
63 readpipe = platform.readpipe |
14926
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
64 setbinary = platform.setbinary |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
65 setsignalhandler = platform.setsignalhandler |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
66 shellquote = platform.shellquote |
36445
0cb09c322647
util: factor out shellsplit() function
Yuya Nishihara <yuya@tcha.org>
parents:
36395
diff
changeset
|
67 shellsplit = platform.shellsplit |
14926
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
68 spawndetached = platform.spawndetached |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
69 sshargs = platform.sshargs |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
70 testpid = platform.testpid |
14912
ec46a7da9f2c
util: move windows and posix wildcard imports to begin of file
Adrian Buehlmann <adrian@cadifra.com>
parents:
14911
diff
changeset
|
71 |
32248
d74b0cff94a9
osutil: proxy through util (and platform) modules (API)
Yuya Nishihara <yuya@tcha.org>
parents:
32245
diff
changeset
|
72 try: |
d74b0cff94a9
osutil: proxy through util (and platform) modules (API)
Yuya Nishihara <yuya@tcha.org>
parents:
32245
diff
changeset
|
73 setprocname = osutil.setprocname |
d74b0cff94a9
osutil: proxy through util (and platform) modules (API)
Yuya Nishihara <yuya@tcha.org>
parents:
32245
diff
changeset
|
74 except AttributeError: |
d74b0cff94a9
osutil: proxy through util (and platform) modules (API)
Yuya Nishihara <yuya@tcha.org>
parents:
32245
diff
changeset
|
75 pass |
35464
8652ab4046e4
osutil: add a function to unblock signals
Jun Wu <quark@fb.com>
parents:
35166
diff
changeset
|
76 try: |
8652ab4046e4
osutil: add a function to unblock signals
Jun Wu <quark@fb.com>
parents:
35166
diff
changeset
|
77 unblocksignal = osutil.unblocksignal |
8652ab4046e4
osutil: add a function to unblock signals
Jun Wu <quark@fb.com>
parents:
35166
diff
changeset
|
78 except AttributeError: |
8652ab4046e4
osutil: add a function to unblock signals
Jun Wu <quark@fb.com>
parents:
35166
diff
changeset
|
79 pass |
32248
d74b0cff94a9
osutil: proxy through util (and platform) modules (API)
Yuya Nishihara <yuya@tcha.org>
parents:
32245
diff
changeset
|
80 |
34646 | 81 closefds = pycompat.isposix |
10197
29e3c4a7699b
subrepo: normalize svn output line-endings
Patrick Mezard <pmezard@gmail.com>
parents:
9996
diff
changeset
|
82 |
10199
c2e2a5e6c36b
subrepo: force en_US.UTF-8 locale when calling svn
Patrick Mezard <pmezard@gmail.com>
parents:
10197
diff
changeset
|
83 def popen2(cmd, env=None, newlines=False): |
9089
8ec39725d966
util: remove unused bufsize argument
Martin Geisler <mg@lazybytes.net>
parents:
9084
diff
changeset
|
84 # Setting bufsize to -1 lets the system decide the buffer size. |
8ec39725d966
util: remove unused bufsize argument
Martin Geisler <mg@lazybytes.net>
parents:
9084
diff
changeset
|
85 # The default for bufsize is 0, meaning unbuffered. This leads to |
8ec39725d966
util: remove unused bufsize argument
Martin Geisler <mg@lazybytes.net>
parents:
9084
diff
changeset
|
86 # poor performance on Mac OS X: http://bugs.python.org/issue4194 |
8ec39725d966
util: remove unused bufsize argument
Martin Geisler <mg@lazybytes.net>
parents:
9084
diff
changeset
|
87 p = subprocess.Popen(cmd, shell=True, bufsize=-1, |
9083
ec171737aaf1
Backed out changeset fce065538bcf: it caused a 5x performance regression on OS X
Bryan O'Sullivan <bos@serpentine.com>
parents:
8340
diff
changeset
|
88 close_fds=closefds, |
10197
29e3c4a7699b
subrepo: normalize svn output line-endings
Patrick Mezard <pmezard@gmail.com>
parents:
9996
diff
changeset
|
89 stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
10199
c2e2a5e6c36b
subrepo: force en_US.UTF-8 locale when calling svn
Patrick Mezard <pmezard@gmail.com>
parents:
10197
diff
changeset
|
90 universal_newlines=newlines, |
c2e2a5e6c36b
subrepo: force en_US.UTF-8 locale when calling svn
Patrick Mezard <pmezard@gmail.com>
parents:
10197
diff
changeset
|
91 env=env) |
8280
0b02d98d44d0
util: always use subprocess
Martin Geisler <mg@lazybytes.net>
parents:
8257
diff
changeset
|
92 return p.stdin, p.stdout |
10197
29e3c4a7699b
subrepo: normalize svn output line-endings
Patrick Mezard <pmezard@gmail.com>
parents:
9996
diff
changeset
|
93 |
10199
c2e2a5e6c36b
subrepo: force en_US.UTF-8 locale when calling svn
Patrick Mezard <pmezard@gmail.com>
parents:
10197
diff
changeset
|
94 def popen3(cmd, env=None, newlines=False): |
18759
9baf4330d88f
sshpeer: store subprocess so it cleans up correctly
Durham Goode <durham@fb.com>
parents:
18736
diff
changeset
|
95 stdin, stdout, stderr, p = popen4(cmd, env, newlines) |
9baf4330d88f
sshpeer: store subprocess so it cleans up correctly
Durham Goode <durham@fb.com>
parents:
18736
diff
changeset
|
96 return stdin, stdout, stderr |
9baf4330d88f
sshpeer: store subprocess so it cleans up correctly
Durham Goode <durham@fb.com>
parents:
18736
diff
changeset
|
97 |
25245
504ef9c49f4a
util: allow to specify buffer size in popen4
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25211
diff
changeset
|
98 def popen4(cmd, env=None, newlines=False, bufsize=-1): |
504ef9c49f4a
util: allow to specify buffer size in popen4
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25211
diff
changeset
|
99 p = subprocess.Popen(cmd, shell=True, bufsize=bufsize, |
9083
ec171737aaf1
Backed out changeset fce065538bcf: it caused a 5x performance regression on OS X
Bryan O'Sullivan <bos@serpentine.com>
parents:
8340
diff
changeset
|
100 close_fds=closefds, |
8280
0b02d98d44d0
util: always use subprocess
Martin Geisler <mg@lazybytes.net>
parents:
8257
diff
changeset
|
101 stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
10197
29e3c4a7699b
subrepo: normalize svn output line-endings
Patrick Mezard <pmezard@gmail.com>
parents:
9996
diff
changeset
|
102 stderr=subprocess.PIPE, |
10199
c2e2a5e6c36b
subrepo: force en_US.UTF-8 locale when calling svn
Patrick Mezard <pmezard@gmail.com>
parents:
10197
diff
changeset
|
103 universal_newlines=newlines, |
c2e2a5e6c36b
subrepo: force en_US.UTF-8 locale when calling svn
Patrick Mezard <pmezard@gmail.com>
parents:
10197
diff
changeset
|
104 env=env) |
18759
9baf4330d88f
sshpeer: store subprocess so it cleans up correctly
Durham Goode <durham@fb.com>
parents:
18736
diff
changeset
|
105 return p.stdin, p.stdout, p.stderr, p |
7106
4674706b5b95
python2.6: use subprocess if available
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6884
diff
changeset
|
106 |
1293
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
107 def pipefilter(s, cmd): |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
108 '''filter string S through command CMD, returning its output''' |
8302
d2ad8c066676
util: simplify pipefilter and avoid subprocess race
Martin Geisler <mg@lazybytes.net>
parents:
8299
diff
changeset
|
109 p = subprocess.Popen(cmd, shell=True, close_fds=closefds, |
d2ad8c066676
util: simplify pipefilter and avoid subprocess race
Martin Geisler <mg@lazybytes.net>
parents:
8299
diff
changeset
|
110 stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
d2ad8c066676
util: simplify pipefilter and avoid subprocess race
Martin Geisler <mg@lazybytes.net>
parents:
8299
diff
changeset
|
111 pout, perr = p.communicate(s) |
d2ad8c066676
util: simplify pipefilter and avoid subprocess race
Martin Geisler <mg@lazybytes.net>
parents:
8299
diff
changeset
|
112 return pout |
419
28511fc21073
[PATCH] file seperator handling for the other 'OS'
mpm@selenic.com
parents:
diff
changeset
|
113 |
1293
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
114 def tempfilter(s, cmd): |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
115 '''filter string S through a pair of temporary files with CMD. |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
116 CMD is used as a template to create the real command to be run, |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
117 with the strings INFILE and OUTFILE replaced by the real names of |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
118 the temporary files generated.''' |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
119 inname, outname = None, None |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
120 try: |
2165
d821918e3bee
Use better names (hg-{usage}-{random}.{suffix}) for temporary files.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2153
diff
changeset
|
121 infd, inname = tempfile.mkstemp(prefix='hg-filter-in-') |
36843
5bc7ff103081
py3: use r'' instead of sysstr('') to get around code transformer
Yuya Nishihara <yuya@tcha.org>
parents:
36840
diff
changeset
|
122 fp = os.fdopen(infd, r'wb') |
1293
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
123 fp.write(s) |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
124 fp.close() |
2165
d821918e3bee
Use better names (hg-{usage}-{random}.{suffix}) for temporary files.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2153
diff
changeset
|
125 outfd, outname = tempfile.mkstemp(prefix='hg-filter-out-') |
1293
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
126 os.close(outfd) |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
127 cmd = cmd.replace('INFILE', inname) |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
128 cmd = cmd.replace('OUTFILE', outname) |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
129 code = os.system(cmd) |
30647
e995f00a9e9a
py3: replace sys.platform with pycompat.sysplatform (part 2 of 2)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30644
diff
changeset
|
130 if pycompat.sysplatform == 'OpenVMS' and code & 1: |
4720
72fb6f10fac1
OpenVMS patches
Jean-Francois PIERONNE <jf.pieronne@laposte.net>
parents:
4708
diff
changeset
|
131 code = 0 |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
132 if code: |
37099
895f209b593b
util: use error.Abort instead of local alias
Yuya Nishihara <yuya@tcha.org>
parents:
37098
diff
changeset
|
133 raise error.Abort(_("command '%s' failed: %s") % |
895f209b593b
util: use error.Abort instead of local alias
Yuya Nishihara <yuya@tcha.org>
parents:
37098
diff
changeset
|
134 (cmd, explainexit(code))) |
37120
e7b517809ebc
util: stop using readfile() in tempfilter()
Yuya Nishihara <yuya@tcha.org>
parents:
37119
diff
changeset
|
135 with open(outname, 'rb') as fp: |
e7b517809ebc
util: stop using readfile() in tempfilter()
Yuya Nishihara <yuya@tcha.org>
parents:
37119
diff
changeset
|
136 return fp.read() |
1293
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
137 finally: |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
138 try: |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
139 if inname: |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
140 os.unlink(inname) |
14004
97ed99d1f419
eliminate various naked except clauses
Idan Kamara <idankk86@gmail.com>
parents:
13985
diff
changeset
|
141 except OSError: |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
142 pass |
1293
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
143 try: |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
144 if outname: |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
145 os.unlink(outname) |
14004
97ed99d1f419
eliminate various naked except clauses
Idan Kamara <idankk86@gmail.com>
parents:
13985
diff
changeset
|
146 except OSError: |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
147 pass |
1293
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
148 |
37119
7ccc9b8aca4c
util: mark filtertable as private constant
Yuya Nishihara <yuya@tcha.org>
parents:
37118
diff
changeset
|
149 _filtertable = { |
1293
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
150 'tempfile:': tempfilter, |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
151 'pipe:': pipefilter, |
37119
7ccc9b8aca4c
util: mark filtertable as private constant
Yuya Nishihara <yuya@tcha.org>
parents:
37118
diff
changeset
|
152 } |
1293
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
153 |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
154 def filter(s, cmd): |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
155 "filter a string through a command that transforms its input to its output" |
37119
7ccc9b8aca4c
util: mark filtertable as private constant
Yuya Nishihara <yuya@tcha.org>
parents:
37118
diff
changeset
|
156 for name, fn in _filtertable.iteritems(): |
1293
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
157 if cmd.startswith(name): |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
158 return fn(s, cmd[len(name):].lstrip()) |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
159 return pipefilter(s, cmd) |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
160 |
14228
116de1da2154
rename util.main_is_frozen to mainfrozen
Adrian Buehlmann <adrian@cadifra.com>
parents:
14167
diff
changeset
|
161 def mainfrozen(): |
6499
479847ccabe0
Added hgexecutable support for py2exe/frozen scripts
"Paul Moore <p.f.moore@gmail.com>"
parents:
5659
diff
changeset
|
162 """return True if we are a frozen executable. |
479847ccabe0
Added hgexecutable support for py2exe/frozen scripts
"Paul Moore <p.f.moore@gmail.com>"
parents:
5659
diff
changeset
|
163 |
479847ccabe0
Added hgexecutable support for py2exe/frozen scripts
"Paul Moore <p.f.moore@gmail.com>"
parents:
5659
diff
changeset
|
164 The code supports py2exe (most common, Windows only) and tools/freeze |
479847ccabe0
Added hgexecutable support for py2exe/frozen scripts
"Paul Moore <p.f.moore@gmail.com>"
parents:
5659
diff
changeset
|
165 (portable, not much used). |
479847ccabe0
Added hgexecutable support for py2exe/frozen scripts
"Paul Moore <p.f.moore@gmail.com>"
parents:
5659
diff
changeset
|
166 """ |
37121
5be286db5fb5
procutil: move process/executable management functions to new module
Yuya Nishihara <yuya@tcha.org>
parents:
37120
diff
changeset
|
167 return (pycompat.safehasattr(sys, "frozen") or # new py2exe |
5be286db5fb5
procutil: move process/executable management functions to new module
Yuya Nishihara <yuya@tcha.org>
parents:
37120
diff
changeset
|
168 pycompat.safehasattr(sys, "importers") or # old py2exe |
30039
ff7697b436ab
py3: use unicode in is_frozen()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30038
diff
changeset
|
169 imp.is_frozen(u"__main__")) # tools/freeze |
6499
479847ccabe0
Added hgexecutable support for py2exe/frozen scripts
"Paul Moore <p.f.moore@gmail.com>"
parents:
5659
diff
changeset
|
170 |
22632
db15bb2d6323
util: move _hgexecutable a few lines, closer to where it is used
Mads Kiilerich <madski@unity3d.com>
parents:
22245
diff
changeset
|
171 _hgexecutable = None |
db15bb2d6323
util: move _hgexecutable a few lines, closer to where it is used
Mads Kiilerich <madski@unity3d.com>
parents:
22245
diff
changeset
|
172 |
5062
3d35c8cb5eb4
Simplify/correct finding the hg executable (fixes issue644)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4832
diff
changeset
|
173 def hgexecutable(): |
3d35c8cb5eb4
Simplify/correct finding the hg executable (fixes issue644)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4832
diff
changeset
|
174 """return location of the 'hg' executable. |
3d35c8cb5eb4
Simplify/correct finding the hg executable (fixes issue644)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4832
diff
changeset
|
175 |
3d35c8cb5eb4
Simplify/correct finding the hg executable (fixes issue644)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4832
diff
changeset
|
176 Defaults to $HG or 'hg' in the search path. |
3d35c8cb5eb4
Simplify/correct finding the hg executable (fixes issue644)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4832
diff
changeset
|
177 """ |
3d35c8cb5eb4
Simplify/correct finding the hg executable (fixes issue644)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4832
diff
changeset
|
178 if _hgexecutable is None: |
30642
344e68882cd3
py3: replace os.environ with encoding.environ (part 4 of 5)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30630
diff
changeset
|
179 hg = encoding.environ.get('HG') |
36843
5bc7ff103081
py3: use r'' instead of sysstr('') to get around code transformer
Yuya Nishihara <yuya@tcha.org>
parents:
36840
diff
changeset
|
180 mainmod = sys.modules[r'__main__'] |
6500 | 181 if hg: |
14229
85fd8402cbc4
rename util.set_hgexecutable to _sethgexecutable
Adrian Buehlmann <adrian@cadifra.com>
parents:
14228
diff
changeset
|
182 _sethgexecutable(hg) |
14228
116de1da2154
rename util.main_is_frozen to mainfrozen
Adrian Buehlmann <adrian@cadifra.com>
parents:
14167
diff
changeset
|
183 elif mainfrozen(): |
27765
f1fb93eebb1d
util: adjust hgexecutable() to handle frozen Mercurial on OS X
Matt Harbison <matt_harbison@yahoo.com>
parents:
27764
diff
changeset
|
184 if getattr(sys, 'frozen', None) == 'macosx_app': |
f1fb93eebb1d
util: adjust hgexecutable() to handle frozen Mercurial on OS X
Matt Harbison <matt_harbison@yahoo.com>
parents:
27764
diff
changeset
|
185 # Env variable set by py2app |
30642
344e68882cd3
py3: replace os.environ with encoding.environ (part 4 of 5)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30630
diff
changeset
|
186 _sethgexecutable(encoding.environ['EXECUTABLEPATH']) |
27765
f1fb93eebb1d
util: adjust hgexecutable() to handle frozen Mercurial on OS X
Matt Harbison <matt_harbison@yahoo.com>
parents:
27764
diff
changeset
|
187 else: |
30672
10b17ed9b591
py3: replace sys.executable with pycompat.sysexecutable
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30647
diff
changeset
|
188 _sethgexecutable(pycompat.sysexecutable) |
31091
2912b06905dc
py3: use pycompat.fsencode() to convert __file__ to bytes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31008
diff
changeset
|
189 elif (os.path.basename( |
2912b06905dc
py3: use pycompat.fsencode() to convert __file__ to bytes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31008
diff
changeset
|
190 pycompat.fsencode(getattr(mainmod, '__file__', ''))) == 'hg'): |
2912b06905dc
py3: use pycompat.fsencode() to convert __file__ to bytes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31008
diff
changeset
|
191 _sethgexecutable(pycompat.fsencode(mainmod.__file__)) |
6499
479847ccabe0
Added hgexecutable support for py2exe/frozen scripts
"Paul Moore <p.f.moore@gmail.com>"
parents:
5659
diff
changeset
|
192 else: |
14271
4030630fb59c
rename util.find_exe to findexe
Adrian Buehlmann <adrian@cadifra.com>
parents:
14262
diff
changeset
|
193 exe = findexe('hg') or os.path.basename(sys.argv[0]) |
14229
85fd8402cbc4
rename util.set_hgexecutable to _sethgexecutable
Adrian Buehlmann <adrian@cadifra.com>
parents:
14228
diff
changeset
|
194 _sethgexecutable(exe) |
5062
3d35c8cb5eb4
Simplify/correct finding the hg executable (fixes issue644)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4832
diff
changeset
|
195 return _hgexecutable |
4686
849f011dbf79
Remember path to 'hg' executable and pass to external tools and hooks as $HG.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4673
diff
changeset
|
196 |
14229
85fd8402cbc4
rename util.set_hgexecutable to _sethgexecutable
Adrian Buehlmann <adrian@cadifra.com>
parents:
14228
diff
changeset
|
197 def _sethgexecutable(path): |
5062
3d35c8cb5eb4
Simplify/correct finding the hg executable (fixes issue644)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4832
diff
changeset
|
198 """set location of the 'hg' executable""" |
4686
849f011dbf79
Remember path to 'hg' executable and pass to external tools and hooks as $HG.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4673
diff
changeset
|
199 global _hgexecutable |
5062
3d35c8cb5eb4
Simplify/correct finding the hg executable (fixes issue644)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4832
diff
changeset
|
200 _hgexecutable = path |
4686
849f011dbf79
Remember path to 'hg' executable and pass to external tools and hooks as $HG.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4673
diff
changeset
|
201 |
36801
eca1051e6c22
util: add public isstdin/isstdout() functions
Yuya Nishihara <yuya@tcha.org>
parents:
36799
diff
changeset
|
202 def _testfileno(f, stdf): |
26450
1138e1d05207
util.system: compare fileno to see if it needs stdout redirection
Yuya Nishihara <yuya@tcha.org>
parents:
26392
diff
changeset
|
203 fileno = getattr(f, 'fileno', None) |
36462
1ca4e86c7265
util: handle fileno() on Python 3 throwing io.UnsupportedOperation
Augie Fackler <augie@google.com>
parents:
36452
diff
changeset
|
204 try: |
36801
eca1051e6c22
util: add public isstdin/isstdout() functions
Yuya Nishihara <yuya@tcha.org>
parents:
36799
diff
changeset
|
205 return fileno and fileno() == stdf.fileno() |
36462
1ca4e86c7265
util: handle fileno() on Python 3 throwing io.UnsupportedOperation
Augie Fackler <augie@google.com>
parents:
36452
diff
changeset
|
206 except io.UnsupportedOperation: |
1ca4e86c7265
util: handle fileno() on Python 3 throwing io.UnsupportedOperation
Augie Fackler <augie@google.com>
parents:
36452
diff
changeset
|
207 return False # fileno() raised UnsupportedOperation |
26450
1138e1d05207
util.system: compare fileno to see if it needs stdout redirection
Yuya Nishihara <yuya@tcha.org>
parents:
26392
diff
changeset
|
208 |
36801
eca1051e6c22
util: add public isstdin/isstdout() functions
Yuya Nishihara <yuya@tcha.org>
parents:
36799
diff
changeset
|
209 def isstdin(f): |
eca1051e6c22
util: add public isstdin/isstdout() functions
Yuya Nishihara <yuya@tcha.org>
parents:
36799
diff
changeset
|
210 return _testfileno(f, sys.__stdin__) |
eca1051e6c22
util: add public isstdin/isstdout() functions
Yuya Nishihara <yuya@tcha.org>
parents:
36799
diff
changeset
|
211 |
eca1051e6c22
util: add public isstdin/isstdout() functions
Yuya Nishihara <yuya@tcha.org>
parents:
36799
diff
changeset
|
212 def isstdout(f): |
eca1051e6c22
util: add public isstdin/isstdout() functions
Yuya Nishihara <yuya@tcha.org>
parents:
36799
diff
changeset
|
213 return _testfileno(f, sys.__stdout__) |
eca1051e6c22
util: add public isstdin/isstdout() functions
Yuya Nishihara <yuya@tcha.org>
parents:
36799
diff
changeset
|
214 |
37126
0216232f21ab
procutil: move protectio/restoreio from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
37121
diff
changeset
|
215 def protectstdio(uin, uout): |
0216232f21ab
procutil: move protectio/restoreio from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
37121
diff
changeset
|
216 """Duplicate streams and redirect original to null if (uin, uout) are |
0216232f21ab
procutil: move protectio/restoreio from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
37121
diff
changeset
|
217 stdio |
0216232f21ab
procutil: move protectio/restoreio from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
37121
diff
changeset
|
218 |
0216232f21ab
procutil: move protectio/restoreio from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
37121
diff
changeset
|
219 Returns (fin, fout) which point to the original (uin, uout) fds, but |
0216232f21ab
procutil: move protectio/restoreio from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
37121
diff
changeset
|
220 may be copy of (uin, uout). The returned streams can be considered |
0216232f21ab
procutil: move protectio/restoreio from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
37121
diff
changeset
|
221 "owned" in that print(), exec(), etc. never reach to them. |
0216232f21ab
procutil: move protectio/restoreio from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
37121
diff
changeset
|
222 """ |
0216232f21ab
procutil: move protectio/restoreio from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
37121
diff
changeset
|
223 uout.flush() |
0216232f21ab
procutil: move protectio/restoreio from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
37121
diff
changeset
|
224 newfiles = [] |
0216232f21ab
procutil: move protectio/restoreio from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
37121
diff
changeset
|
225 nullfd = os.open(os.devnull, os.O_RDWR) |
0216232f21ab
procutil: move protectio/restoreio from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
37121
diff
changeset
|
226 for f, sysf, mode in [(uin, stdin, r'rb'), |
0216232f21ab
procutil: move protectio/restoreio from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
37121
diff
changeset
|
227 (uout, stdout, r'wb')]: |
0216232f21ab
procutil: move protectio/restoreio from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
37121
diff
changeset
|
228 if f is sysf: |
0216232f21ab
procutil: move protectio/restoreio from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
37121
diff
changeset
|
229 newfd = os.dup(f.fileno()) |
0216232f21ab
procutil: move protectio/restoreio from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
37121
diff
changeset
|
230 os.dup2(nullfd, f.fileno()) |
0216232f21ab
procutil: move protectio/restoreio from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
37121
diff
changeset
|
231 f = os.fdopen(newfd, mode) |
0216232f21ab
procutil: move protectio/restoreio from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
37121
diff
changeset
|
232 newfiles.append(f) |
0216232f21ab
procutil: move protectio/restoreio from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
37121
diff
changeset
|
233 os.close(nullfd) |
0216232f21ab
procutil: move protectio/restoreio from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
37121
diff
changeset
|
234 return tuple(newfiles) |
0216232f21ab
procutil: move protectio/restoreio from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
37121
diff
changeset
|
235 |
0216232f21ab
procutil: move protectio/restoreio from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
37121
diff
changeset
|
236 def restorestdio(uin, uout, fin, fout): |
0216232f21ab
procutil: move protectio/restoreio from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
37121
diff
changeset
|
237 """Restore (uin, uout) streams from possibly duplicated (fin, fout)""" |
0216232f21ab
procutil: move protectio/restoreio from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
37121
diff
changeset
|
238 uout.flush() |
0216232f21ab
procutil: move protectio/restoreio from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
37121
diff
changeset
|
239 for f, uif in [(fin, uin), (fout, uout)]: |
0216232f21ab
procutil: move protectio/restoreio from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
37121
diff
changeset
|
240 if f is not uif: |
0216232f21ab
procutil: move protectio/restoreio from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
37121
diff
changeset
|
241 os.dup2(f.fileno(), uif.fileno()) |
0216232f21ab
procutil: move protectio/restoreio from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
37121
diff
changeset
|
242 f.close() |
0216232f21ab
procutil: move protectio/restoreio from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
37121
diff
changeset
|
243 |
37127
6715e8035b4f
procutil: introduce context-manager interface for protect/restorestdio
Yuya Nishihara <yuya@tcha.org>
parents:
37126
diff
changeset
|
244 @contextlib.contextmanager |
6715e8035b4f
procutil: introduce context-manager interface for protect/restorestdio
Yuya Nishihara <yuya@tcha.org>
parents:
37126
diff
changeset
|
245 def protectedstdio(uin, uout): |
6715e8035b4f
procutil: introduce context-manager interface for protect/restorestdio
Yuya Nishihara <yuya@tcha.org>
parents:
37126
diff
changeset
|
246 """Run code block with protected standard streams""" |
6715e8035b4f
procutil: introduce context-manager interface for protect/restorestdio
Yuya Nishihara <yuya@tcha.org>
parents:
37126
diff
changeset
|
247 fin, fout = protectstdio(uin, uout) |
6715e8035b4f
procutil: introduce context-manager interface for protect/restorestdio
Yuya Nishihara <yuya@tcha.org>
parents:
37126
diff
changeset
|
248 try: |
6715e8035b4f
procutil: introduce context-manager interface for protect/restorestdio
Yuya Nishihara <yuya@tcha.org>
parents:
37126
diff
changeset
|
249 yield fin, fout |
6715e8035b4f
procutil: introduce context-manager interface for protect/restorestdio
Yuya Nishihara <yuya@tcha.org>
parents:
37126
diff
changeset
|
250 finally: |
6715e8035b4f
procutil: introduce context-manager interface for protect/restorestdio
Yuya Nishihara <yuya@tcha.org>
parents:
37126
diff
changeset
|
251 restorestdio(uin, uout, fin, fout) |
6715e8035b4f
procutil: introduce context-manager interface for protect/restorestdio
Yuya Nishihara <yuya@tcha.org>
parents:
37126
diff
changeset
|
252 |
30736
d9e5b0aeeb90
util: extract the logic calculating environment variables
Jun Wu <quark@fb.com>
parents:
30672
diff
changeset
|
253 def shellenviron(environ=None): |
d9e5b0aeeb90
util: extract the logic calculating environment variables
Jun Wu <quark@fb.com>
parents:
30672
diff
changeset
|
254 """return environ with optional override, useful for shelling out""" |
d9e5b0aeeb90
util: extract the logic calculating environment variables
Jun Wu <quark@fb.com>
parents:
30672
diff
changeset
|
255 def py2shell(val): |
d9e5b0aeeb90
util: extract the logic calculating environment variables
Jun Wu <quark@fb.com>
parents:
30672
diff
changeset
|
256 'convert python object into string that is useful to shell' |
d9e5b0aeeb90
util: extract the logic calculating environment variables
Jun Wu <quark@fb.com>
parents:
30672
diff
changeset
|
257 if val is None or val is False: |
d9e5b0aeeb90
util: extract the logic calculating environment variables
Jun Wu <quark@fb.com>
parents:
30672
diff
changeset
|
258 return '0' |
d9e5b0aeeb90
util: extract the logic calculating environment variables
Jun Wu <quark@fb.com>
parents:
30672
diff
changeset
|
259 if val is True: |
d9e5b0aeeb90
util: extract the logic calculating environment variables
Jun Wu <quark@fb.com>
parents:
30672
diff
changeset
|
260 return '1' |
36448
d26b0bedfaa4
util: use pycompat.bytestr() instead of str()
Augie Fackler <augie@google.com>
parents:
36445
diff
changeset
|
261 return pycompat.bytestr(val) |
30736
d9e5b0aeeb90
util: extract the logic calculating environment variables
Jun Wu <quark@fb.com>
parents:
30672
diff
changeset
|
262 env = dict(encoding.environ) |
d9e5b0aeeb90
util: extract the logic calculating environment variables
Jun Wu <quark@fb.com>
parents:
30672
diff
changeset
|
263 if environ: |
d9e5b0aeeb90
util: extract the logic calculating environment variables
Jun Wu <quark@fb.com>
parents:
30672
diff
changeset
|
264 env.update((k, py2shell(v)) for k, v in environ.iteritems()) |
d9e5b0aeeb90
util: extract the logic calculating environment variables
Jun Wu <quark@fb.com>
parents:
30672
diff
changeset
|
265 env['HG'] = hgexecutable() |
d9e5b0aeeb90
util: extract the logic calculating environment variables
Jun Wu <quark@fb.com>
parents:
30672
diff
changeset
|
266 return env |
d9e5b0aeeb90
util: extract the logic calculating environment variables
Jun Wu <quark@fb.com>
parents:
30672
diff
changeset
|
267 |
31125
3f8f53190d6a
chg: deduplicate error handling of ui.system()
Yuya Nishihara <yuya@tcha.org>
parents:
31091
diff
changeset
|
268 def system(cmd, environ=None, cwd=None, out=None): |
1882
c0320567931f
merge util.esystem and util.system.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1880
diff
changeset
|
269 '''enhanced shell command execution. |
c0320567931f
merge util.esystem and util.system.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1880
diff
changeset
|
270 run with environment maybe modified, maybe in different dir. |
508 | 271 |
11469
c37f35d7f2f5
http: deliver hook output to client
Maxim Khitrov <mkhitrov@gmail.com>
parents:
11297
diff
changeset
|
272 if out is specified, it is assumed to be a file-like object that has a |
c37f35d7f2f5
http: deliver hook output to client
Maxim Khitrov <mkhitrov@gmail.com>
parents:
11297
diff
changeset
|
273 write() method. stdout and stderr will be redirected to out.''' |
13439
d724a69309e0
util: flush stdout before calling external processes
Mads Kiilerich <mads@kiilerich.com>
parents:
13400
diff
changeset
|
274 try: |
30482
39d13b8c101d
py3: bulk replace sys.stdin/out/err by util's
Yuya Nishihara <yuya@tcha.org>
parents:
30481
diff
changeset
|
275 stdout.flush() |
13439
d724a69309e0
util: flush stdout before calling external processes
Mads Kiilerich <mads@kiilerich.com>
parents:
13400
diff
changeset
|
276 except Exception: |
d724a69309e0
util: flush stdout before calling external processes
Mads Kiilerich <mads@kiilerich.com>
parents:
13400
diff
changeset
|
277 pass |
13188
6c9345f9edca
util: concentrate quoting knowledge to windows.py quotecommand()
Steve Borho <steve@borho.org>
parents:
13128
diff
changeset
|
278 cmd = quotecommand(cmd) |
32904
19b0fd4b5570
plan9: drop py26 hacks
Matt Harbison <matt_harbison@yahoo.com>
parents:
32835
diff
changeset
|
279 env = shellenviron(environ) |
36801
eca1051e6c22
util: add public isstdin/isstdout() functions
Yuya Nishihara <yuya@tcha.org>
parents:
36799
diff
changeset
|
280 if out is None or isstdout(out): |
32904
19b0fd4b5570
plan9: drop py26 hacks
Matt Harbison <matt_harbison@yahoo.com>
parents:
32835
diff
changeset
|
281 rc = subprocess.call(cmd, shell=True, close_fds=closefds, |
19b0fd4b5570
plan9: drop py26 hacks
Matt Harbison <matt_harbison@yahoo.com>
parents:
32835
diff
changeset
|
282 env=env, cwd=cwd) |
11469
c37f35d7f2f5
http: deliver hook output to client
Maxim Khitrov <mkhitrov@gmail.com>
parents:
11297
diff
changeset
|
283 else: |
32904
19b0fd4b5570
plan9: drop py26 hacks
Matt Harbison <matt_harbison@yahoo.com>
parents:
32835
diff
changeset
|
284 proc = subprocess.Popen(cmd, shell=True, close_fds=closefds, |
19b0fd4b5570
plan9: drop py26 hacks
Matt Harbison <matt_harbison@yahoo.com>
parents:
32835
diff
changeset
|
285 env=env, cwd=cwd, stdout=subprocess.PIPE, |
19b0fd4b5570
plan9: drop py26 hacks
Matt Harbison <matt_harbison@yahoo.com>
parents:
32835
diff
changeset
|
286 stderr=subprocess.STDOUT) |
19b0fd4b5570
plan9: drop py26 hacks
Matt Harbison <matt_harbison@yahoo.com>
parents:
32835
diff
changeset
|
287 for line in iter(proc.stdout.readline, ''): |
19b0fd4b5570
plan9: drop py26 hacks
Matt Harbison <matt_harbison@yahoo.com>
parents:
32835
diff
changeset
|
288 out.write(line) |
19b0fd4b5570
plan9: drop py26 hacks
Matt Harbison <matt_harbison@yahoo.com>
parents:
32835
diff
changeset
|
289 proc.wait() |
19b0fd4b5570
plan9: drop py26 hacks
Matt Harbison <matt_harbison@yahoo.com>
parents:
32835
diff
changeset
|
290 rc = proc.returncode |
19b0fd4b5570
plan9: drop py26 hacks
Matt Harbison <matt_harbison@yahoo.com>
parents:
32835
diff
changeset
|
291 if pycompat.sysplatform == 'OpenVMS' and rc & 1: |
19b0fd4b5570
plan9: drop py26 hacks
Matt Harbison <matt_harbison@yahoo.com>
parents:
32835
diff
changeset
|
292 rc = 0 |
9517
4368f582c806
util.system: Use subprocess instead of os.system
Mads Kiilerich <mads@kiilerich.com>
parents:
9508
diff
changeset
|
293 return rc |
1880
05c7d75be925
fix broken environment save/restore when a hook runs.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1877
diff
changeset
|
294 |
6007
090b1a665901
filemerge: add config item for GUI tools
Matt Mackall <mpm@selenic.com>
parents:
6006
diff
changeset
|
295 def gui(): |
090b1a665901
filemerge: add config item for GUI tools
Matt Mackall <mpm@selenic.com>
parents:
6006
diff
changeset
|
296 '''Are we running in a GUI?''' |
34647 | 297 if pycompat.isdarwin: |
30642
344e68882cd3
py3: replace os.environ with encoding.environ (part 4 of 5)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30630
diff
changeset
|
298 if 'SSH_CONNECTION' in encoding.environ: |
13734
16118b4859a1
util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13439
diff
changeset
|
299 # handle SSH access to a box where the user is logged in |
16118b4859a1
util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13439
diff
changeset
|
300 return False |
16118b4859a1
util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13439
diff
changeset
|
301 elif getattr(osutil, 'isgui', None): |
16118b4859a1
util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13439
diff
changeset
|
302 # check if a CoreGraphics session is available |
16118b4859a1
util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13439
diff
changeset
|
303 return osutil.isgui() |
16118b4859a1
util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13439
diff
changeset
|
304 else: |
16118b4859a1
util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13439
diff
changeset
|
305 # pure build; use a safe default |
16118b4859a1
util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13439
diff
changeset
|
306 return True |
16118b4859a1
util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13439
diff
changeset
|
307 else: |
34645 | 308 return pycompat.iswindows or encoding.environ.get("DISPLAY") |
6007
090b1a665901
filemerge: add config item for GUI tools
Matt Mackall <mpm@selenic.com>
parents:
6006
diff
changeset
|
309 |
10239
8e4be44a676f
Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents:
10199
diff
changeset
|
310 def hgcmd(): |
8e4be44a676f
Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents:
10199
diff
changeset
|
311 """Return the command used to execute current hg |
8e4be44a676f
Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents:
10199
diff
changeset
|
312 |
8e4be44a676f
Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents:
10199
diff
changeset
|
313 This is different from hgexecutable() because on Windows we want |
8e4be44a676f
Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents:
10199
diff
changeset
|
314 to avoid things opening new shell windows like batch files, so we |
8e4be44a676f
Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents:
10199
diff
changeset
|
315 get either the python call or current executable. |
8e4be44a676f
Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents:
10199
diff
changeset
|
316 """ |
14228
116de1da2154
rename util.main_is_frozen to mainfrozen
Adrian Buehlmann <adrian@cadifra.com>
parents:
14167
diff
changeset
|
317 if mainfrozen(): |
27766
198f78a52a2f
util: adjust hgcmd() to handle frozen Mercurial on OS X
Matt Harbison <matt_harbison@yahoo.com>
parents:
27765
diff
changeset
|
318 if getattr(sys, 'frozen', None) == 'macosx_app': |
198f78a52a2f
util: adjust hgcmd() to handle frozen Mercurial on OS X
Matt Harbison <matt_harbison@yahoo.com>
parents:
27765
diff
changeset
|
319 # Env variable set by py2app |
30642
344e68882cd3
py3: replace os.environ with encoding.environ (part 4 of 5)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30630
diff
changeset
|
320 return [encoding.environ['EXECUTABLEPATH']] |
27766
198f78a52a2f
util: adjust hgcmd() to handle frozen Mercurial on OS X
Matt Harbison <matt_harbison@yahoo.com>
parents:
27765
diff
changeset
|
321 else: |
30672
10b17ed9b591
py3: replace sys.executable with pycompat.sysexecutable
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30647
diff
changeset
|
322 return [pycompat.sysexecutable] |
37118
49d6ba67c93f
util: mark platform-specific gethgcmd() as private
Yuya Nishihara <yuya@tcha.org>
parents:
37102
diff
changeset
|
323 return _gethgcmd() |
10344
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
324 |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
325 def rundetached(args, condfn): |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
326 """Execute the argument list in a detached process. |
10422
600142e7a028
util: fix trailing whitespace found by check-code
Augie Fackler <durin42@gmail.com>
parents:
10344
diff
changeset
|
327 |
10344
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
328 condfn is a callable which is called repeatedly and should return |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
329 True once the child process is known to have started successfully. |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
330 At this point, the child process PID is returned. If the child |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
331 process fails to start or finishes before condfn() evaluates to |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
332 True, return -1. |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
333 """ |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
334 # Windows case is easier because the child process is either |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
335 # successfully starting and validating the condition or exiting |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
336 # on failure. We just poll on its PID. On Unix, if the child |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
337 # process fails to start, it will be left in a zombie state until |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
338 # the parent wait on it, which we cannot do since we expect a long |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
339 # running process on success. Instead we listen for SIGCHLD telling |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
340 # us our child process terminated. |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
341 terminated = set() |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
342 def handler(signum, frame): |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
343 terminated.add(os.wait()) |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
344 prevhandler = None |
14968
b7dbe957585c
util: use safehasattr or getattr instead of hasattr
Augie Fackler <durin42@gmail.com>
parents:
14942
diff
changeset
|
345 SIGCHLD = getattr(signal, 'SIGCHLD', None) |
b7dbe957585c
util: use safehasattr or getattr instead of hasattr
Augie Fackler <durin42@gmail.com>
parents:
14942
diff
changeset
|
346 if SIGCHLD is not None: |
b7dbe957585c
util: use safehasattr or getattr instead of hasattr
Augie Fackler <durin42@gmail.com>
parents:
14942
diff
changeset
|
347 prevhandler = signal.signal(SIGCHLD, handler) |
10344
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
348 try: |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
349 pid = spawndetached(args) |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
350 while not condfn(): |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
351 if ((pid in terminated or not testpid(pid)) |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
352 and not condfn()): |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
353 return -1 |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
354 time.sleep(0.1) |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
355 return pid |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
356 finally: |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
357 if prevhandler is not None: |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
358 signal.signal(signal.SIGCHLD, prevhandler) |