Mercurial > hg
annotate mercurial/utils/procutil.py @ 43076:2372284d9457
formatting: blacken the codebase
This is using my patch to black
(https://github.com/psf/black/pull/826) so we don't un-wrap collection
literals.
Done with:
hg files 'set:**.py - mercurial/thirdparty/** - "contrib/python-zstandard/**"' | xargs black -S
# skip-blame mass-reformatting only
# no-check-commit reformats foo_bar functions
Differential Revision: https://phab.mercurial-scm.org/D6971
author | Augie Fackler <augie@google.com> |
---|---|
date | Sun, 06 Oct 2019 09:45:02 -0400 |
parents | 2cc453284d5c |
children | 687b865b95ad |
rev | line source |
---|---|
37118
5be286db5fb5
procutil: move process/executable management functions to new module
Yuya Nishihara <yuya@tcha.org>
parents:
37117
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 |
37118
5be286db5fb5
procutil: move process/executable management functions to new module
Yuya Nishihara <yuya@tcha.org>
parents:
37117
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 |
37124
6715e8035b4f
procutil: introduce context-manager interface for protect/restorestdio
Yuya Nishihara <yuya@tcha.org>
parents:
37123
diff
changeset
|
12 import contextlib |
40497
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
13 import errno |
27358
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
14 import imp |
36432
1ca4e86c7265
util: handle fileno() on Python 3 throwing io.UnsupportedOperation
Augie Fackler <augie@google.com>
parents:
36422
diff
changeset
|
15 import io |
27358
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
16 import os |
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
17 import signal |
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
18 import subprocess |
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
19 import sys |
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
20 import time |
3769 | 21 |
37118
5be286db5fb5
procutil: move process/executable management functions to new module
Yuya Nishihara <yuya@tcha.org>
parents:
37117
diff
changeset
|
22 from ..i18n import _ |
5be286db5fb5
procutil: move process/executable management functions to new module
Yuya Nishihara <yuya@tcha.org>
parents:
37117
diff
changeset
|
23 |
5be286db5fb5
procutil: move process/executable management functions to new module
Yuya Nishihara <yuya@tcha.org>
parents:
37117
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, |
32367
a9c71d578a1c
osutil: switch to policy importer
Yuya Nishihara <yuya@tcha.org>
parents:
32306
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, |
37083
f99d64e8a4e4
stringutil: move generic string helpers to new module
Yuya Nishihara <yuya@tcha.org>
parents:
37082
diff
changeset
|
29 ) |
3769 | 30 |
32367
a9c71d578a1c
osutil: switch to policy importer
Yuya Nishihara <yuya@tcha.org>
parents:
32306
diff
changeset
|
31 osutil = policy.importmod(r'osutil') |
32201
4462a981e8df
base85: proxy through util module
Yuya Nishihara <yuya@tcha.org>
parents:
32154
diff
changeset
|
32 |
30472
277f4fe6d01a
py3: provide bytes stdin/out/err through util module
Yuya Nishihara <yuya@tcha.org>
parents:
30471
diff
changeset
|
33 stderr = pycompat.stderr |
277f4fe6d01a
py3: provide bytes stdin/out/err through util module
Yuya Nishihara <yuya@tcha.org>
parents:
30471
diff
changeset
|
34 stdin = pycompat.stdin |
277f4fe6d01a
py3: provide bytes stdin/out/err through util module
Yuya Nishihara <yuya@tcha.org>
parents:
30471
diff
changeset
|
35 stdout = pycompat.stdout |
32572
377c74ef008d
win32mbcs: avoid unintentional failure at colorization
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
31952
diff
changeset
|
36 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
37 |
30876
3a4c0905f357
util: always force line buffered stdout when stdout is a tty (BC)
Simon Farnsworth <simonfar@fb.com>
parents:
30854
diff
changeset
|
38 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
|
39 try: |
3a4c0905f357
util: always force line buffered stdout when stdout is a tty (BC)
Simon Farnsworth <simonfar@fb.com>
parents:
30854
diff
changeset
|
40 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
|
41 except AttributeError: |
3a4c0905f357
util: always force line buffered stdout when stdout is a tty (BC)
Simon Farnsworth <simonfar@fb.com>
parents:
30854
diff
changeset
|
42 return False |
3a4c0905f357
util: always force line buffered stdout when stdout is a tty (BC)
Simon Farnsworth <simonfar@fb.com>
parents:
30854
diff
changeset
|
43 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
44 |
30876
3a4c0905f357
util: always force line buffered stdout when stdout is a tty (BC)
Simon Farnsworth <simonfar@fb.com>
parents:
30854
diff
changeset
|
45 # 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
|
46 # destined stdout with a pipe destined stdout (e.g. pager), we want line |
38455
0b63a6743010
procutil: use unbuffered stdout on Windows
Sune Foldager <cryo@cyanite.org>
parents:
37464
diff
changeset
|
47 # buffering (or unbuffered, on Windows) |
30876
3a4c0905f357
util: always force line buffered stdout when stdout is a tty (BC)
Simon Farnsworth <simonfar@fb.com>
parents:
30854
diff
changeset
|
48 if isatty(stdout): |
38455
0b63a6743010
procutil: use unbuffered stdout on Windows
Sune Foldager <cryo@cyanite.org>
parents:
37464
diff
changeset
|
49 if pycompat.iswindows: |
0b63a6743010
procutil: use unbuffered stdout on Windows
Sune Foldager <cryo@cyanite.org>
parents:
37464
diff
changeset
|
50 # Windows doesn't support line buffering |
0b63a6743010
procutil: use unbuffered stdout on Windows
Sune Foldager <cryo@cyanite.org>
parents:
37464
diff
changeset
|
51 stdout = os.fdopen(stdout.fileno(), r'wb', 0) |
0b63a6743010
procutil: use unbuffered stdout on Windows
Sune Foldager <cryo@cyanite.org>
parents:
37464
diff
changeset
|
52 else: |
0b63a6743010
procutil: use unbuffered stdout on Windows
Sune Foldager <cryo@cyanite.org>
parents:
37464
diff
changeset
|
53 stdout = os.fdopen(stdout.fileno(), r'wb', 1) |
30876
3a4c0905f357
util: always force line buffered stdout when stdout is a tty (BC)
Simon Farnsworth <simonfar@fb.com>
parents:
30854
diff
changeset
|
54 |
34645 | 55 if pycompat.iswindows: |
37118
5be286db5fb5
procutil: move process/executable management functions to new module
Yuya Nishihara <yuya@tcha.org>
parents:
37117
diff
changeset
|
56 from .. import windows as platform |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
57 |
30876
3a4c0905f357
util: always force line buffered stdout when stdout is a tty (BC)
Simon Farnsworth <simonfar@fb.com>
parents:
30854
diff
changeset
|
58 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
|
59 else: |
37118
5be286db5fb5
procutil: move process/executable management functions to new module
Yuya Nishihara <yuya@tcha.org>
parents:
37117
diff
changeset
|
60 from .. import posix as platform |
14926
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
61 |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
62 findexe = platform.findexe |
37115
49d6ba67c93f
util: mark platform-specific gethgcmd() as private
Yuya Nishihara <yuya@tcha.org>
parents:
37099
diff
changeset
|
63 _gethgcmd = platform.gethgcmd |
14926
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
64 getuser = platform.getuser |
28027
14033c5dd261
util: enable getpid to be replaced
timeless <timeless@mozdev.org>
parents:
27785
diff
changeset
|
65 getpid = os.getpid |
14926
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
66 hidewindow = platform.hidewindow |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
67 quotecommand = platform.quotecommand |
22245
234e4c24b980
platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
21914
diff
changeset
|
68 readpipe = platform.readpipe |
14926
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
69 setbinary = platform.setbinary |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
70 setsignalhandler = platform.setsignalhandler |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
71 shellquote = platform.shellquote |
36415
0cb09c322647
util: factor out shellsplit() function
Yuya Nishihara <yuya@tcha.org>
parents:
36362
diff
changeset
|
72 shellsplit = platform.shellsplit |
14926
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
73 spawndetached = platform.spawndetached |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
74 sshargs = platform.sshargs |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14918
diff
changeset
|
75 testpid = platform.testpid |
43019
2cc453284d5c
patchbomb: protect email addresses from shell
Floris Bruynooghe <flub@google.com>
parents:
42883
diff
changeset
|
76 quote = pycompat.shlexquote |
14912
ec46a7da9f2c
util: move windows and posix wildcard imports to begin of file
Adrian Buehlmann <adrian@cadifra.com>
parents:
14911
diff
changeset
|
77 |
32208
d74b0cff94a9
osutil: proxy through util (and platform) modules (API)
Yuya Nishihara <yuya@tcha.org>
parents:
32201
diff
changeset
|
78 try: |
d74b0cff94a9
osutil: proxy through util (and platform) modules (API)
Yuya Nishihara <yuya@tcha.org>
parents:
32201
diff
changeset
|
79 setprocname = osutil.setprocname |
d74b0cff94a9
osutil: proxy through util (and platform) modules (API)
Yuya Nishihara <yuya@tcha.org>
parents:
32201
diff
changeset
|
80 except AttributeError: |
d74b0cff94a9
osutil: proxy through util (and platform) modules (API)
Yuya Nishihara <yuya@tcha.org>
parents:
32201
diff
changeset
|
81 pass |
35460
8652ab4046e4
osutil: add a function to unblock signals
Jun Wu <quark@fb.com>
parents:
35145
diff
changeset
|
82 try: |
8652ab4046e4
osutil: add a function to unblock signals
Jun Wu <quark@fb.com>
parents:
35145
diff
changeset
|
83 unblocksignal = osutil.unblocksignal |
8652ab4046e4
osutil: add a function to unblock signals
Jun Wu <quark@fb.com>
parents:
35145
diff
changeset
|
84 except AttributeError: |
8652ab4046e4
osutil: add a function to unblock signals
Jun Wu <quark@fb.com>
parents:
35145
diff
changeset
|
85 pass |
32208
d74b0cff94a9
osutil: proxy through util (and platform) modules (API)
Yuya Nishihara <yuya@tcha.org>
parents:
32201
diff
changeset
|
86 |
34646 | 87 closefds = pycompat.isposix |
10197
29e3c4a7699b
subrepo: normalize svn output line-endings
Patrick Mezard <pmezard@gmail.com>
parents:
9996
diff
changeset
|
88 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
89 |
37460
a6c6b7beb025
procutil: unify platform.explainexit()
Yuya Nishihara <yuya@tcha.org>
parents:
37459
diff
changeset
|
90 def explainexit(code): |
37463
bbd240f81ac5
procutil: make explainexit() simply return a message (API)
Yuya Nishihara <yuya@tcha.org>
parents:
37462
diff
changeset
|
91 """return a message describing a subprocess status |
37460
a6c6b7beb025
procutil: unify platform.explainexit()
Yuya Nishihara <yuya@tcha.org>
parents:
37459
diff
changeset
|
92 (codes from kill are negative - not os.system/wait encoding)""" |
a6c6b7beb025
procutil: unify platform.explainexit()
Yuya Nishihara <yuya@tcha.org>
parents:
37459
diff
changeset
|
93 if code >= 0: |
37463
bbd240f81ac5
procutil: make explainexit() simply return a message (API)
Yuya Nishihara <yuya@tcha.org>
parents:
37462
diff
changeset
|
94 return _("exited with status %d") % code |
bbd240f81ac5
procutil: make explainexit() simply return a message (API)
Yuya Nishihara <yuya@tcha.org>
parents:
37462
diff
changeset
|
95 return _("killed by signal %d") % -code |
37460
a6c6b7beb025
procutil: unify platform.explainexit()
Yuya Nishihara <yuya@tcha.org>
parents:
37459
diff
changeset
|
96 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
97 |
37459
90c5ca718781
procutil: rewrite popen() as a subprocess.Popen wrapper (issue4746) (API)
Yuya Nishihara <yuya@tcha.org>
parents:
37220
diff
changeset
|
98 class _pfile(object): |
90c5ca718781
procutil: rewrite popen() as a subprocess.Popen wrapper (issue4746) (API)
Yuya Nishihara <yuya@tcha.org>
parents:
37220
diff
changeset
|
99 """File-like wrapper for a stream opened by subprocess.Popen()""" |
90c5ca718781
procutil: rewrite popen() as a subprocess.Popen wrapper (issue4746) (API)
Yuya Nishihara <yuya@tcha.org>
parents:
37220
diff
changeset
|
100 |
90c5ca718781
procutil: rewrite popen() as a subprocess.Popen wrapper (issue4746) (API)
Yuya Nishihara <yuya@tcha.org>
parents:
37220
diff
changeset
|
101 def __init__(self, proc, fp): |
90c5ca718781
procutil: rewrite popen() as a subprocess.Popen wrapper (issue4746) (API)
Yuya Nishihara <yuya@tcha.org>
parents:
37220
diff
changeset
|
102 self._proc = proc |
90c5ca718781
procutil: rewrite popen() as a subprocess.Popen wrapper (issue4746) (API)
Yuya Nishihara <yuya@tcha.org>
parents:
37220
diff
changeset
|
103 self._fp = fp |
90c5ca718781
procutil: rewrite popen() as a subprocess.Popen wrapper (issue4746) (API)
Yuya Nishihara <yuya@tcha.org>
parents:
37220
diff
changeset
|
104 |
90c5ca718781
procutil: rewrite popen() as a subprocess.Popen wrapper (issue4746) (API)
Yuya Nishihara <yuya@tcha.org>
parents:
37220
diff
changeset
|
105 def close(self): |
90c5ca718781
procutil: rewrite popen() as a subprocess.Popen wrapper (issue4746) (API)
Yuya Nishihara <yuya@tcha.org>
parents:
37220
diff
changeset
|
106 # unlike os.popen(), this returns an integer in subprocess coding |
90c5ca718781
procutil: rewrite popen() as a subprocess.Popen wrapper (issue4746) (API)
Yuya Nishihara <yuya@tcha.org>
parents:
37220
diff
changeset
|
107 self._fp.close() |
90c5ca718781
procutil: rewrite popen() as a subprocess.Popen wrapper (issue4746) (API)
Yuya Nishihara <yuya@tcha.org>
parents:
37220
diff
changeset
|
108 return self._proc.wait() |
90c5ca718781
procutil: rewrite popen() as a subprocess.Popen wrapper (issue4746) (API)
Yuya Nishihara <yuya@tcha.org>
parents:
37220
diff
changeset
|
109 |
90c5ca718781
procutil: rewrite popen() as a subprocess.Popen wrapper (issue4746) (API)
Yuya Nishihara <yuya@tcha.org>
parents:
37220
diff
changeset
|
110 def __iter__(self): |
90c5ca718781
procutil: rewrite popen() as a subprocess.Popen wrapper (issue4746) (API)
Yuya Nishihara <yuya@tcha.org>
parents:
37220
diff
changeset
|
111 return iter(self._fp) |
90c5ca718781
procutil: rewrite popen() as a subprocess.Popen wrapper (issue4746) (API)
Yuya Nishihara <yuya@tcha.org>
parents:
37220
diff
changeset
|
112 |
90c5ca718781
procutil: rewrite popen() as a subprocess.Popen wrapper (issue4746) (API)
Yuya Nishihara <yuya@tcha.org>
parents:
37220
diff
changeset
|
113 def __getattr__(self, attr): |
90c5ca718781
procutil: rewrite popen() as a subprocess.Popen wrapper (issue4746) (API)
Yuya Nishihara <yuya@tcha.org>
parents:
37220
diff
changeset
|
114 return getattr(self._fp, attr) |
90c5ca718781
procutil: rewrite popen() as a subprocess.Popen wrapper (issue4746) (API)
Yuya Nishihara <yuya@tcha.org>
parents:
37220
diff
changeset
|
115 |
90c5ca718781
procutil: rewrite popen() as a subprocess.Popen wrapper (issue4746) (API)
Yuya Nishihara <yuya@tcha.org>
parents:
37220
diff
changeset
|
116 def __enter__(self): |
90c5ca718781
procutil: rewrite popen() as a subprocess.Popen wrapper (issue4746) (API)
Yuya Nishihara <yuya@tcha.org>
parents:
37220
diff
changeset
|
117 return self |
90c5ca718781
procutil: rewrite popen() as a subprocess.Popen wrapper (issue4746) (API)
Yuya Nishihara <yuya@tcha.org>
parents:
37220
diff
changeset
|
118 |
90c5ca718781
procutil: rewrite popen() as a subprocess.Popen wrapper (issue4746) (API)
Yuya Nishihara <yuya@tcha.org>
parents:
37220
diff
changeset
|
119 def __exit__(self, exc_type, exc_value, exc_tb): |
90c5ca718781
procutil: rewrite popen() as a subprocess.Popen wrapper (issue4746) (API)
Yuya Nishihara <yuya@tcha.org>
parents:
37220
diff
changeset
|
120 self.close() |
90c5ca718781
procutil: rewrite popen() as a subprocess.Popen wrapper (issue4746) (API)
Yuya Nishihara <yuya@tcha.org>
parents:
37220
diff
changeset
|
121 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
122 |
37459
90c5ca718781
procutil: rewrite popen() as a subprocess.Popen wrapper (issue4746) (API)
Yuya Nishihara <yuya@tcha.org>
parents:
37220
diff
changeset
|
123 def popen(cmd, mode='rb', bufsize=-1): |
90c5ca718781
procutil: rewrite popen() as a subprocess.Popen wrapper (issue4746) (API)
Yuya Nishihara <yuya@tcha.org>
parents:
37220
diff
changeset
|
124 if mode == 'rb': |
90c5ca718781
procutil: rewrite popen() as a subprocess.Popen wrapper (issue4746) (API)
Yuya Nishihara <yuya@tcha.org>
parents:
37220
diff
changeset
|
125 return _popenreader(cmd, bufsize) |
90c5ca718781
procutil: rewrite popen() as a subprocess.Popen wrapper (issue4746) (API)
Yuya Nishihara <yuya@tcha.org>
parents:
37220
diff
changeset
|
126 elif mode == 'wb': |
90c5ca718781
procutil: rewrite popen() as a subprocess.Popen wrapper (issue4746) (API)
Yuya Nishihara <yuya@tcha.org>
parents:
37220
diff
changeset
|
127 return _popenwriter(cmd, bufsize) |
90c5ca718781
procutil: rewrite popen() as a subprocess.Popen wrapper (issue4746) (API)
Yuya Nishihara <yuya@tcha.org>
parents:
37220
diff
changeset
|
128 raise error.ProgrammingError('unsupported mode: %r' % mode) |
90c5ca718781
procutil: rewrite popen() as a subprocess.Popen wrapper (issue4746) (API)
Yuya Nishihara <yuya@tcha.org>
parents:
37220
diff
changeset
|
129 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
130 |
37459
90c5ca718781
procutil: rewrite popen() as a subprocess.Popen wrapper (issue4746) (API)
Yuya Nishihara <yuya@tcha.org>
parents:
37220
diff
changeset
|
131 def _popenreader(cmd, bufsize): |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
132 p = subprocess.Popen( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
133 tonativestr(quotecommand(cmd)), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
134 shell=True, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
135 bufsize=bufsize, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
136 close_fds=closefds, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
137 stdout=subprocess.PIPE, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
138 ) |
37459
90c5ca718781
procutil: rewrite popen() as a subprocess.Popen wrapper (issue4746) (API)
Yuya Nishihara <yuya@tcha.org>
parents:
37220
diff
changeset
|
139 return _pfile(p, p.stdout) |
90c5ca718781
procutil: rewrite popen() as a subprocess.Popen wrapper (issue4746) (API)
Yuya Nishihara <yuya@tcha.org>
parents:
37220
diff
changeset
|
140 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
141 |
37459
90c5ca718781
procutil: rewrite popen() as a subprocess.Popen wrapper (issue4746) (API)
Yuya Nishihara <yuya@tcha.org>
parents:
37220
diff
changeset
|
142 def _popenwriter(cmd, bufsize): |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
143 p = subprocess.Popen( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
144 tonativestr(quotecommand(cmd)), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
145 shell=True, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
146 bufsize=bufsize, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
147 close_fds=closefds, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
148 stdin=subprocess.PIPE, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
149 ) |
37459
90c5ca718781
procutil: rewrite popen() as a subprocess.Popen wrapper (issue4746) (API)
Yuya Nishihara <yuya@tcha.org>
parents:
37220
diff
changeset
|
150 return _pfile(p, p.stdin) |
90c5ca718781
procutil: rewrite popen() as a subprocess.Popen wrapper (issue4746) (API)
Yuya Nishihara <yuya@tcha.org>
parents:
37220
diff
changeset
|
151 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
152 |
37464
632b92899203
procutil: drop unused 'newlines' option from popen*() (API)
Yuya Nishihara <yuya@tcha.org>
parents:
37463
diff
changeset
|
153 def popen2(cmd, env=None): |
9089
8ec39725d966
util: remove unused bufsize argument
Martin Geisler <mg@lazybytes.net>
parents:
9084
diff
changeset
|
154 # 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
|
155 # 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
|
156 # poor performance on Mac OS X: http://bugs.python.org/issue4194 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
157 p = subprocess.Popen( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
158 tonativestr(cmd), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
159 shell=True, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
160 bufsize=-1, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
161 close_fds=closefds, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
162 stdin=subprocess.PIPE, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
163 stdout=subprocess.PIPE, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
164 env=tonativeenv(env), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
165 ) |
8280
0b02d98d44d0
util: always use subprocess
Martin Geisler <mg@lazybytes.net>
parents:
8257
diff
changeset
|
166 return p.stdin, p.stdout |
10197
29e3c4a7699b
subrepo: normalize svn output line-endings
Patrick Mezard <pmezard@gmail.com>
parents:
9996
diff
changeset
|
167 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
168 |
37464
632b92899203
procutil: drop unused 'newlines' option from popen*() (API)
Yuya Nishihara <yuya@tcha.org>
parents:
37463
diff
changeset
|
169 def popen3(cmd, env=None): |
632b92899203
procutil: drop unused 'newlines' option from popen*() (API)
Yuya Nishihara <yuya@tcha.org>
parents:
37463
diff
changeset
|
170 stdin, stdout, stderr, p = popen4(cmd, env) |
18759
9baf4330d88f
sshpeer: store subprocess so it cleans up correctly
Durham Goode <durham@fb.com>
parents:
18736
diff
changeset
|
171 return stdin, stdout, stderr |
9baf4330d88f
sshpeer: store subprocess so it cleans up correctly
Durham Goode <durham@fb.com>
parents:
18736
diff
changeset
|
172 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
173 |
37464
632b92899203
procutil: drop unused 'newlines' option from popen*() (API)
Yuya Nishihara <yuya@tcha.org>
parents:
37463
diff
changeset
|
174 def popen4(cmd, env=None, bufsize=-1): |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
175 p = subprocess.Popen( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
176 tonativestr(cmd), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
177 shell=True, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
178 bufsize=bufsize, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
179 close_fds=closefds, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
180 stdin=subprocess.PIPE, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
181 stdout=subprocess.PIPE, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
182 stderr=subprocess.PIPE, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
183 env=tonativeenv(env), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
184 ) |
18759
9baf4330d88f
sshpeer: store subprocess so it cleans up correctly
Durham Goode <durham@fb.com>
parents:
18736
diff
changeset
|
185 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
|
186 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
187 |
1293
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
188 def pipefilter(s, cmd): |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
189 '''filter string S through command CMD, returning its output''' |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
190 p = subprocess.Popen( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
191 tonativestr(cmd), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
192 shell=True, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
193 close_fds=closefds, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
194 stdin=subprocess.PIPE, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
195 stdout=subprocess.PIPE, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
196 ) |
8302
d2ad8c066676
util: simplify pipefilter and avoid subprocess race
Martin Geisler <mg@lazybytes.net>
parents:
8299
diff
changeset
|
197 pout, perr = p.communicate(s) |
d2ad8c066676
util: simplify pipefilter and avoid subprocess race
Martin Geisler <mg@lazybytes.net>
parents:
8299
diff
changeset
|
198 return pout |
419
28511fc21073
[PATCH] file seperator handling for the other 'OS'
mpm@selenic.com
parents:
diff
changeset
|
199 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
200 |
1293
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
201 def tempfilter(s, cmd): |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
202 '''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
|
203 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
|
204 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
|
205 the temporary files generated.''' |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
206 inname, outname = None, None |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
207 try: |
38164
aac4be30e250
py3: wrap tempfile.mkstemp() to use bytes path
Yuya Nishihara <yuya@tcha.org>
parents:
37464
diff
changeset
|
208 infd, inname = pycompat.mkstemp(prefix='hg-filter-in-') |
36835
5bc7ff103081
py3: use r'' instead of sysstr('') to get around code transformer
Yuya Nishihara <yuya@tcha.org>
parents:
36832
diff
changeset
|
209 fp = os.fdopen(infd, r'wb') |
1293
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
210 fp.write(s) |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
211 fp.close() |
38164
aac4be30e250
py3: wrap tempfile.mkstemp() to use bytes path
Yuya Nishihara <yuya@tcha.org>
parents:
37464
diff
changeset
|
212 outfd, outname = pycompat.mkstemp(prefix='hg-filter-out-') |
1293
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
213 os.close(outfd) |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
214 cmd = cmd.replace('INFILE', inname) |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
215 cmd = cmd.replace('OUTFILE', outname) |
37461
538353b80676
procutil: fix error message of tempfile filter
Yuya Nishihara <yuya@tcha.org>
parents:
37460
diff
changeset
|
216 code = system(cmd) |
30642
e995f00a9e9a
py3: replace sys.platform with pycompat.sysplatform (part 2 of 2)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30639
diff
changeset
|
217 if pycompat.sysplatform == 'OpenVMS' and code & 1: |
4720
72fb6f10fac1
OpenVMS patches
Jean-Francois PIERONNE <jf.pieronne@laposte.net>
parents:
4708
diff
changeset
|
218 code = 0 |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
219 if code: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
220 raise error.Abort( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
221 _("command '%s' failed: %s") % (cmd, explainexit(code)) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
222 ) |
37117
e7b517809ebc
util: stop using readfile() in tempfilter()
Yuya Nishihara <yuya@tcha.org>
parents:
37116
diff
changeset
|
223 with open(outname, 'rb') as fp: |
e7b517809ebc
util: stop using readfile() in tempfilter()
Yuya Nishihara <yuya@tcha.org>
parents:
37116
diff
changeset
|
224 return fp.read() |
1293
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
225 finally: |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
226 try: |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
227 if inname: |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
228 os.unlink(inname) |
14004
97ed99d1f419
eliminate various naked except clauses
Idan Kamara <idankk86@gmail.com>
parents:
13985
diff
changeset
|
229 except OSError: |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
230 pass |
1293
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
231 try: |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
232 if outname: |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
233 os.unlink(outname) |
14004
97ed99d1f419
eliminate various naked except clauses
Idan Kamara <idankk86@gmail.com>
parents:
13985
diff
changeset
|
234 except OSError: |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
235 pass |
1293
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
236 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
237 |
37116
7ccc9b8aca4c
util: mark filtertable as private constant
Yuya Nishihara <yuya@tcha.org>
parents:
37115
diff
changeset
|
238 _filtertable = { |
1293
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
239 'tempfile:': tempfilter, |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
240 'pipe:': pipefilter, |
37116
7ccc9b8aca4c
util: mark filtertable as private constant
Yuya Nishihara <yuya@tcha.org>
parents:
37115
diff
changeset
|
241 } |
1293
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
242 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
243 |
1293
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
244 def filter(s, cmd): |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
245 "filter a string through a command that transforms its input to its output" |
37116
7ccc9b8aca4c
util: mark filtertable as private constant
Yuya Nishihara <yuya@tcha.org>
parents:
37115
diff
changeset
|
246 for name, fn in _filtertable.iteritems(): |
1293
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
247 if cmd.startswith(name): |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
248 return fn(s, cmd[len(name) :].lstrip()) |
1293
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
249 return pipefilter(s, cmd) |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
250 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
251 |
14228
116de1da2154
rename util.main_is_frozen to mainfrozen
Adrian Buehlmann <adrian@cadifra.com>
parents:
14167
diff
changeset
|
252 def mainfrozen(): |
6499
479847ccabe0
Added hgexecutable support for py2exe/frozen scripts
"Paul Moore <p.f.moore@gmail.com>"
parents:
5659
diff
changeset
|
253 """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
|
254 |
479847ccabe0
Added hgexecutable support for py2exe/frozen scripts
"Paul Moore <p.f.moore@gmail.com>"
parents:
5659
diff
changeset
|
255 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
|
256 (portable, not much used). |
479847ccabe0
Added hgexecutable support for py2exe/frozen scripts
"Paul Moore <p.f.moore@gmail.com>"
parents:
5659
diff
changeset
|
257 """ |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
258 return ( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
259 pycompat.safehasattr(sys, "frozen") |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
260 or pycompat.safehasattr(sys, "importers") # new py2exe |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
261 or imp.is_frozen(r"__main__") # old py2exe |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
262 ) # tools/freeze |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
263 |
6499
479847ccabe0
Added hgexecutable support for py2exe/frozen scripts
"Paul Moore <p.f.moore@gmail.com>"
parents:
5659
diff
changeset
|
264 |
22632
db15bb2d6323
util: move _hgexecutable a few lines, closer to where it is used
Mads Kiilerich <madski@unity3d.com>
parents:
22245
diff
changeset
|
265 _hgexecutable = None |
db15bb2d6323
util: move _hgexecutable a few lines, closer to where it is used
Mads Kiilerich <madski@unity3d.com>
parents:
22245
diff
changeset
|
266 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
267 |
5062
3d35c8cb5eb4
Simplify/correct finding the hg executable (fixes issue644)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4832
diff
changeset
|
268 def hgexecutable(): |
3d35c8cb5eb4
Simplify/correct finding the hg executable (fixes issue644)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4832
diff
changeset
|
269 """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
|
270 |
3d35c8cb5eb4
Simplify/correct finding the hg executable (fixes issue644)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4832
diff
changeset
|
271 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
|
272 """ |
3d35c8cb5eb4
Simplify/correct finding the hg executable (fixes issue644)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4832
diff
changeset
|
273 if _hgexecutable is None: |
30637
344e68882cd3
py3: replace os.environ with encoding.environ (part 4 of 5)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30625
diff
changeset
|
274 hg = encoding.environ.get('HG') |
36835
5bc7ff103081
py3: use r'' instead of sysstr('') to get around code transformer
Yuya Nishihara <yuya@tcha.org>
parents:
36832
diff
changeset
|
275 mainmod = sys.modules[r'__main__'] |
6500 | 276 if hg: |
14229
85fd8402cbc4
rename util.set_hgexecutable to _sethgexecutable
Adrian Buehlmann <adrian@cadifra.com>
parents:
14228
diff
changeset
|
277 _sethgexecutable(hg) |
14228
116de1da2154
rename util.main_is_frozen to mainfrozen
Adrian Buehlmann <adrian@cadifra.com>
parents:
14167
diff
changeset
|
278 elif mainfrozen(): |
27765
f1fb93eebb1d
util: adjust hgexecutable() to handle frozen Mercurial on OS X
Matt Harbison <matt_harbison@yahoo.com>
parents:
27764
diff
changeset
|
279 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
|
280 # Env variable set by py2app |
30637
344e68882cd3
py3: replace os.environ with encoding.environ (part 4 of 5)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30625
diff
changeset
|
281 _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
|
282 else: |
30669
10b17ed9b591
py3: replace sys.executable with pycompat.sysexecutable
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30642
diff
changeset
|
283 _sethgexecutable(pycompat.sysexecutable) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
284 elif ( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
285 not pycompat.iswindows |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
286 and os.path.basename( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
287 pycompat.fsencode(getattr(mainmod, '__file__', '')) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
288 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
289 == 'hg' |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
290 ): |
31074
2912b06905dc
py3: use pycompat.fsencode() to convert __file__ to bytes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30988
diff
changeset
|
291 _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
|
292 else: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
293 _sethgexecutable( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
294 findexe('hg') or os.path.basename(pycompat.sysargv[0]) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
295 ) |
5062
3d35c8cb5eb4
Simplify/correct finding the hg executable (fixes issue644)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4832
diff
changeset
|
296 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
|
297 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
298 |
14229
85fd8402cbc4
rename util.set_hgexecutable to _sethgexecutable
Adrian Buehlmann <adrian@cadifra.com>
parents:
14228
diff
changeset
|
299 def _sethgexecutable(path): |
5062
3d35c8cb5eb4
Simplify/correct finding the hg executable (fixes issue644)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4832
diff
changeset
|
300 """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
|
301 global _hgexecutable |
5062
3d35c8cb5eb4
Simplify/correct finding the hg executable (fixes issue644)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4832
diff
changeset
|
302 _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
|
303 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
304 |
36793
eca1051e6c22
util: add public isstdin/isstdout() functions
Yuya Nishihara <yuya@tcha.org>
parents:
36791
diff
changeset
|
305 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
|
306 fileno = getattr(f, 'fileno', None) |
36432
1ca4e86c7265
util: handle fileno() on Python 3 throwing io.UnsupportedOperation
Augie Fackler <augie@google.com>
parents:
36422
diff
changeset
|
307 try: |
36793
eca1051e6c22
util: add public isstdin/isstdout() functions
Yuya Nishihara <yuya@tcha.org>
parents:
36791
diff
changeset
|
308 return fileno and fileno() == stdf.fileno() |
36432
1ca4e86c7265
util: handle fileno() on Python 3 throwing io.UnsupportedOperation
Augie Fackler <augie@google.com>
parents:
36422
diff
changeset
|
309 except io.UnsupportedOperation: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
310 return False # fileno() raised UnsupportedOperation |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
311 |
26450
1138e1d05207
util.system: compare fileno to see if it needs stdout redirection
Yuya Nishihara <yuya@tcha.org>
parents:
26392
diff
changeset
|
312 |
36793
eca1051e6c22
util: add public isstdin/isstdout() functions
Yuya Nishihara <yuya@tcha.org>
parents:
36791
diff
changeset
|
313 def isstdin(f): |
eca1051e6c22
util: add public isstdin/isstdout() functions
Yuya Nishihara <yuya@tcha.org>
parents:
36791
diff
changeset
|
314 return _testfileno(f, sys.__stdin__) |
eca1051e6c22
util: add public isstdin/isstdout() functions
Yuya Nishihara <yuya@tcha.org>
parents:
36791
diff
changeset
|
315 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
316 |
36793
eca1051e6c22
util: add public isstdin/isstdout() functions
Yuya Nishihara <yuya@tcha.org>
parents:
36791
diff
changeset
|
317 def isstdout(f): |
eca1051e6c22
util: add public isstdin/isstdout() functions
Yuya Nishihara <yuya@tcha.org>
parents:
36791
diff
changeset
|
318 return _testfileno(f, sys.__stdout__) |
eca1051e6c22
util: add public isstdin/isstdout() functions
Yuya Nishihara <yuya@tcha.org>
parents:
36791
diff
changeset
|
319 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
320 |
37123
0216232f21ab
procutil: move protectio/restoreio from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
37118
diff
changeset
|
321 def protectstdio(uin, uout): |
37220
7f78de1c93aa
procutil: redirect ui.fout to stderr while stdio is protected
Yuya Nishihara <yuya@tcha.org>
parents:
37219
diff
changeset
|
322 """Duplicate streams and redirect original if (uin, uout) are stdio |
7f78de1c93aa
procutil: redirect ui.fout to stderr while stdio is protected
Yuya Nishihara <yuya@tcha.org>
parents:
37219
diff
changeset
|
323 |
7f78de1c93aa
procutil: redirect ui.fout to stderr while stdio is protected
Yuya Nishihara <yuya@tcha.org>
parents:
37219
diff
changeset
|
324 If uin is stdin, it's redirected to /dev/null. If uout is stdout, it's |
7f78de1c93aa
procutil: redirect ui.fout to stderr while stdio is protected
Yuya Nishihara <yuya@tcha.org>
parents:
37219
diff
changeset
|
325 redirected to stderr so the output is still readable. |
37123
0216232f21ab
procutil: move protectio/restoreio from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
37118
diff
changeset
|
326 |
0216232f21ab
procutil: move protectio/restoreio from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
37118
diff
changeset
|
327 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:
37118
diff
changeset
|
328 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:
37118
diff
changeset
|
329 "owned" in that print(), exec(), etc. never reach to them. |
0216232f21ab
procutil: move protectio/restoreio from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
37118
diff
changeset
|
330 """ |
0216232f21ab
procutil: move protectio/restoreio from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
37118
diff
changeset
|
331 uout.flush() |
37219
ac71cbad5da3
procutil: unroll uin/uout loop in protectstdio()
Yuya Nishihara <yuya@tcha.org>
parents:
37124
diff
changeset
|
332 fin, fout = uin, uout |
39807
e5724be689b3
procutil: compare fd number to see if stdio protection is needed (issue5992)
Yuya Nishihara <yuya@tcha.org>
parents:
38526
diff
changeset
|
333 if _testfileno(uin, stdin): |
37219
ac71cbad5da3
procutil: unroll uin/uout loop in protectstdio()
Yuya Nishihara <yuya@tcha.org>
parents:
37124
diff
changeset
|
334 newfd = os.dup(uin.fileno()) |
37220
7f78de1c93aa
procutil: redirect ui.fout to stderr while stdio is protected
Yuya Nishihara <yuya@tcha.org>
parents:
37219
diff
changeset
|
335 nullfd = os.open(os.devnull, os.O_RDONLY) |
37219
ac71cbad5da3
procutil: unroll uin/uout loop in protectstdio()
Yuya Nishihara <yuya@tcha.org>
parents:
37124
diff
changeset
|
336 os.dup2(nullfd, uin.fileno()) |
37220
7f78de1c93aa
procutil: redirect ui.fout to stderr while stdio is protected
Yuya Nishihara <yuya@tcha.org>
parents:
37219
diff
changeset
|
337 os.close(nullfd) |
37219
ac71cbad5da3
procutil: unroll uin/uout loop in protectstdio()
Yuya Nishihara <yuya@tcha.org>
parents:
37124
diff
changeset
|
338 fin = os.fdopen(newfd, r'rb') |
39807
e5724be689b3
procutil: compare fd number to see if stdio protection is needed (issue5992)
Yuya Nishihara <yuya@tcha.org>
parents:
38526
diff
changeset
|
339 if _testfileno(uout, stdout): |
37219
ac71cbad5da3
procutil: unroll uin/uout loop in protectstdio()
Yuya Nishihara <yuya@tcha.org>
parents:
37124
diff
changeset
|
340 newfd = os.dup(uout.fileno()) |
37220
7f78de1c93aa
procutil: redirect ui.fout to stderr while stdio is protected
Yuya Nishihara <yuya@tcha.org>
parents:
37219
diff
changeset
|
341 os.dup2(stderr.fileno(), uout.fileno()) |
37219
ac71cbad5da3
procutil: unroll uin/uout loop in protectstdio()
Yuya Nishihara <yuya@tcha.org>
parents:
37124
diff
changeset
|
342 fout = os.fdopen(newfd, r'wb') |
ac71cbad5da3
procutil: unroll uin/uout loop in protectstdio()
Yuya Nishihara <yuya@tcha.org>
parents:
37124
diff
changeset
|
343 return fin, fout |
37123
0216232f21ab
procutil: move protectio/restoreio from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
37118
diff
changeset
|
344 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
345 |
37123
0216232f21ab
procutil: move protectio/restoreio from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
37118
diff
changeset
|
346 def restorestdio(uin, uout, fin, fout): |
0216232f21ab
procutil: move protectio/restoreio from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
37118
diff
changeset
|
347 """Restore (uin, uout) streams from possibly duplicated (fin, fout)""" |
0216232f21ab
procutil: move protectio/restoreio from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
37118
diff
changeset
|
348 uout.flush() |
0216232f21ab
procutil: move protectio/restoreio from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
37118
diff
changeset
|
349 for f, uif in [(fin, uin), (fout, uout)]: |
0216232f21ab
procutil: move protectio/restoreio from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
37118
diff
changeset
|
350 if f is not uif: |
0216232f21ab
procutil: move protectio/restoreio from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
37118
diff
changeset
|
351 os.dup2(f.fileno(), uif.fileno()) |
0216232f21ab
procutil: move protectio/restoreio from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
37118
diff
changeset
|
352 f.close() |
0216232f21ab
procutil: move protectio/restoreio from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
37118
diff
changeset
|
353 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
354 |
30736
d9e5b0aeeb90
util: extract the logic calculating environment variables
Jun Wu <quark@fb.com>
parents:
30669
diff
changeset
|
355 def shellenviron(environ=None): |
d9e5b0aeeb90
util: extract the logic calculating environment variables
Jun Wu <quark@fb.com>
parents:
30669
diff
changeset
|
356 """return environ with optional override, useful for shelling out""" |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
357 |
30736
d9e5b0aeeb90
util: extract the logic calculating environment variables
Jun Wu <quark@fb.com>
parents:
30669
diff
changeset
|
358 def py2shell(val): |
d9e5b0aeeb90
util: extract the logic calculating environment variables
Jun Wu <quark@fb.com>
parents:
30669
diff
changeset
|
359 'convert python object into string that is useful to shell' |
d9e5b0aeeb90
util: extract the logic calculating environment variables
Jun Wu <quark@fb.com>
parents:
30669
diff
changeset
|
360 if val is None or val is False: |
d9e5b0aeeb90
util: extract the logic calculating environment variables
Jun Wu <quark@fb.com>
parents:
30669
diff
changeset
|
361 return '0' |
d9e5b0aeeb90
util: extract the logic calculating environment variables
Jun Wu <quark@fb.com>
parents:
30669
diff
changeset
|
362 if val is True: |
d9e5b0aeeb90
util: extract the logic calculating environment variables
Jun Wu <quark@fb.com>
parents:
30669
diff
changeset
|
363 return '1' |
36418
d26b0bedfaa4
util: use pycompat.bytestr() instead of str()
Augie Fackler <augie@google.com>
parents:
36415
diff
changeset
|
364 return pycompat.bytestr(val) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
365 |
30736
d9e5b0aeeb90
util: extract the logic calculating environment variables
Jun Wu <quark@fb.com>
parents:
30669
diff
changeset
|
366 env = dict(encoding.environ) |
d9e5b0aeeb90
util: extract the logic calculating environment variables
Jun Wu <quark@fb.com>
parents:
30669
diff
changeset
|
367 if environ: |
d9e5b0aeeb90
util: extract the logic calculating environment variables
Jun Wu <quark@fb.com>
parents:
30669
diff
changeset
|
368 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:
30669
diff
changeset
|
369 env['HG'] = hgexecutable() |
d9e5b0aeeb90
util: extract the logic calculating environment variables
Jun Wu <quark@fb.com>
parents:
30669
diff
changeset
|
370 return env |
d9e5b0aeeb90
util: extract the logic calculating environment variables
Jun Wu <quark@fb.com>
parents:
30669
diff
changeset
|
371 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
372 |
38491
72286f9e324f
procutil: add a shim for translating shell commands to native commands
Matt Harbison <matt_harbison@yahoo.com>
parents:
38454
diff
changeset
|
373 if pycompat.iswindows: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
374 |
38491
72286f9e324f
procutil: add a shim for translating shell commands to native commands
Matt Harbison <matt_harbison@yahoo.com>
parents:
38454
diff
changeset
|
375 def shelltonative(cmd, env): |
72286f9e324f
procutil: add a shim for translating shell commands to native commands
Matt Harbison <matt_harbison@yahoo.com>
parents:
38454
diff
changeset
|
376 return platform.shelltocmdexe(cmd, shellenviron(env)) |
39662
50f46b771921
py3: partially fix pager spawning on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
38526
diff
changeset
|
377 |
50f46b771921
py3: partially fix pager spawning on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
38526
diff
changeset
|
378 tonativestr = encoding.strfromlocal |
38491
72286f9e324f
procutil: add a shim for translating shell commands to native commands
Matt Harbison <matt_harbison@yahoo.com>
parents:
38454
diff
changeset
|
379 else: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
380 |
38491
72286f9e324f
procutil: add a shim for translating shell commands to native commands
Matt Harbison <matt_harbison@yahoo.com>
parents:
38454
diff
changeset
|
381 def shelltonative(cmd, env): |
72286f9e324f
procutil: add a shim for translating shell commands to native commands
Matt Harbison <matt_harbison@yahoo.com>
parents:
38454
diff
changeset
|
382 return cmd |
72286f9e324f
procutil: add a shim for translating shell commands to native commands
Matt Harbison <matt_harbison@yahoo.com>
parents:
38454
diff
changeset
|
383 |
39662
50f46b771921
py3: partially fix pager spawning on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
38526
diff
changeset
|
384 tonativestr = pycompat.identity |
50f46b771921
py3: partially fix pager spawning on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
38526
diff
changeset
|
385 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
386 |
39662
50f46b771921
py3: partially fix pager spawning on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
38526
diff
changeset
|
387 def tonativeenv(env): |
50f46b771921
py3: partially fix pager spawning on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
38526
diff
changeset
|
388 '''convert the environment from bytes to strings suitable for Popen(), etc. |
50f46b771921
py3: partially fix pager spawning on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
38526
diff
changeset
|
389 ''' |
50f46b771921
py3: partially fix pager spawning on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
38526
diff
changeset
|
390 return pycompat.rapply(tonativestr, env) |
50f46b771921
py3: partially fix pager spawning on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
38526
diff
changeset
|
391 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
392 |
31108
3f8f53190d6a
chg: deduplicate error handling of ui.system()
Yuya Nishihara <yuya@tcha.org>
parents:
31074
diff
changeset
|
393 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
|
394 '''enhanced shell command execution. |
c0320567931f
merge util.esystem and util.system.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1880
diff
changeset
|
395 run with environment maybe modified, maybe in different dir. |
508 | 396 |
11469
c37f35d7f2f5
http: deliver hook output to client
Maxim Khitrov <mkhitrov@gmail.com>
parents:
11297
diff
changeset
|
397 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
|
398 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
|
399 try: |
30473
39d13b8c101d
py3: bulk replace sys.stdin/out/err by util's
Yuya Nishihara <yuya@tcha.org>
parents:
30472
diff
changeset
|
400 stdout.flush() |
13439
d724a69309e0
util: flush stdout before calling external processes
Mads Kiilerich <mads@kiilerich.com>
parents:
13400
diff
changeset
|
401 except Exception: |
d724a69309e0
util: flush stdout before calling external processes
Mads Kiilerich <mads@kiilerich.com>
parents:
13400
diff
changeset
|
402 pass |
13188
6c9345f9edca
util: concentrate quoting knowledge to windows.py quotecommand()
Steve Borho <steve@borho.org>
parents:
13128
diff
changeset
|
403 cmd = quotecommand(cmd) |
32886
19b0fd4b5570
plan9: drop py26 hacks
Matt Harbison <matt_harbison@yahoo.com>
parents:
32816
diff
changeset
|
404 env = shellenviron(environ) |
36793
eca1051e6c22
util: add public isstdin/isstdout() functions
Yuya Nishihara <yuya@tcha.org>
parents:
36791
diff
changeset
|
405 if out is None or isstdout(out): |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
406 rc = subprocess.call( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
407 tonativestr(cmd), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
408 shell=True, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
409 close_fds=closefds, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
410 env=tonativeenv(env), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
411 cwd=pycompat.rapply(tonativestr, cwd), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
412 ) |
11469
c37f35d7f2f5
http: deliver hook output to client
Maxim Khitrov <mkhitrov@gmail.com>
parents:
11297
diff
changeset
|
413 else: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
414 proc = subprocess.Popen( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
415 tonativestr(cmd), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
416 shell=True, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
417 close_fds=closefds, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
418 env=tonativeenv(env), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
419 cwd=pycompat.rapply(tonativestr, cwd), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
420 stdout=subprocess.PIPE, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
421 stderr=subprocess.STDOUT, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
422 ) |
32886
19b0fd4b5570
plan9: drop py26 hacks
Matt Harbison <matt_harbison@yahoo.com>
parents:
32816
diff
changeset
|
423 for line in iter(proc.stdout.readline, ''): |
19b0fd4b5570
plan9: drop py26 hacks
Matt Harbison <matt_harbison@yahoo.com>
parents:
32816
diff
changeset
|
424 out.write(line) |
19b0fd4b5570
plan9: drop py26 hacks
Matt Harbison <matt_harbison@yahoo.com>
parents:
32816
diff
changeset
|
425 proc.wait() |
19b0fd4b5570
plan9: drop py26 hacks
Matt Harbison <matt_harbison@yahoo.com>
parents:
32816
diff
changeset
|
426 rc = proc.returncode |
19b0fd4b5570
plan9: drop py26 hacks
Matt Harbison <matt_harbison@yahoo.com>
parents:
32816
diff
changeset
|
427 if pycompat.sysplatform == 'OpenVMS' and rc & 1: |
19b0fd4b5570
plan9: drop py26 hacks
Matt Harbison <matt_harbison@yahoo.com>
parents:
32816
diff
changeset
|
428 rc = 0 |
9517
4368f582c806
util.system: Use subprocess instead of os.system
Mads Kiilerich <mads@kiilerich.com>
parents:
9508
diff
changeset
|
429 return rc |
1880
05c7d75be925
fix broken environment save/restore when a hook runs.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1877
diff
changeset
|
430 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
431 |
6007
090b1a665901
filemerge: add config item for GUI tools
Matt Mackall <mpm@selenic.com>
parents:
6006
diff
changeset
|
432 def gui(): |
090b1a665901
filemerge: add config item for GUI tools
Matt Mackall <mpm@selenic.com>
parents:
6006
diff
changeset
|
433 '''Are we running in a GUI?''' |
34647 | 434 if pycompat.isdarwin: |
30637
344e68882cd3
py3: replace os.environ with encoding.environ (part 4 of 5)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30625
diff
changeset
|
435 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
|
436 # 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
|
437 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
|
438 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
|
439 # 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
|
440 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
|
441 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
|
442 # 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
|
443 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
|
444 else: |
34645 | 445 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
|
446 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
447 |
10239
8e4be44a676f
Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents:
10199
diff
changeset
|
448 def hgcmd(): |
8e4be44a676f
Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents:
10199
diff
changeset
|
449 """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
|
450 |
8e4be44a676f
Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents:
10199
diff
changeset
|
451 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
|
452 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
|
453 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
|
454 """ |
14228
116de1da2154
rename util.main_is_frozen to mainfrozen
Adrian Buehlmann <adrian@cadifra.com>
parents:
14167
diff
changeset
|
455 if mainfrozen(): |
27766
198f78a52a2f
util: adjust hgcmd() to handle frozen Mercurial on OS X
Matt Harbison <matt_harbison@yahoo.com>
parents:
27765
diff
changeset
|
456 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
|
457 # Env variable set by py2app |
30637
344e68882cd3
py3: replace os.environ with encoding.environ (part 4 of 5)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30625
diff
changeset
|
458 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
|
459 else: |
30669
10b17ed9b591
py3: replace sys.executable with pycompat.sysexecutable
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30642
diff
changeset
|
460 return [pycompat.sysexecutable] |
37115
49d6ba67c93f
util: mark platform-specific gethgcmd() as private
Yuya Nishihara <yuya@tcha.org>
parents:
37099
diff
changeset
|
461 return _gethgcmd() |
10344
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
462 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
463 |
10344
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
464 def rundetached(args, condfn): |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
465 """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
|
466 |
10344
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
467 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
|
468 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
|
469 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
|
470 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
|
471 True, return -1. |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
472 """ |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
473 # 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
|
474 # 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
|
475 # 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
|
476 # 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
|
477 # 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
|
478 # 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
|
479 # us our child process terminated. |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
480 terminated = set() |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
481 |
10344
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
482 def handler(signum, frame): |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
483 terminated.add(os.wait()) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
484 |
10344
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
485 prevhandler = None |
14968
b7dbe957585c
util: use safehasattr or getattr instead of hasattr
Augie Fackler <durin42@gmail.com>
parents:
14942
diff
changeset
|
486 SIGCHLD = getattr(signal, 'SIGCHLD', None) |
b7dbe957585c
util: use safehasattr or getattr instead of hasattr
Augie Fackler <durin42@gmail.com>
parents:
14942
diff
changeset
|
487 if SIGCHLD is not None: |
b7dbe957585c
util: use safehasattr or getattr instead of hasattr
Augie Fackler <durin42@gmail.com>
parents:
14942
diff
changeset
|
488 prevhandler = signal.signal(SIGCHLD, handler) |
10344
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
489 try: |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
490 pid = spawndetached(args) |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
491 while not condfn(): |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
492 if (pid in terminated or not testpid(pid)) and not condfn(): |
10344
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
493 return -1 |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
494 time.sleep(0.1) |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
495 return pid |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
496 finally: |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
497 if prevhandler is not None: |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
498 signal.signal(signal.SIGCHLD, prevhandler) |
38526
313a940d49a3
ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
38525
diff
changeset
|
499 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
500 |
38526
313a940d49a3
ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
38525
diff
changeset
|
501 @contextlib.contextmanager |
41076
8ecb17b7f432
procutil: correct spelling of uninterruptable -> uninterruptible
Kyle Lippincott <spectral@google.com>
parents:
40712
diff
changeset
|
502 def uninterruptible(warn): |
38526
313a940d49a3
ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
38525
diff
changeset
|
503 """Inhibit SIGINT handling on a region of code. |
313a940d49a3
ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
38525
diff
changeset
|
504 |
313a940d49a3
ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
38525
diff
changeset
|
505 Note that if this is called in a non-main thread, it turns into a no-op. |
313a940d49a3
ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
38525
diff
changeset
|
506 |
313a940d49a3
ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
38525
diff
changeset
|
507 Args: |
313a940d49a3
ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
38525
diff
changeset
|
508 warn: A callable which takes no arguments, and returns True if the |
313a940d49a3
ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
38525
diff
changeset
|
509 previous signal handling should be restored. |
313a940d49a3
ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
38525
diff
changeset
|
510 """ |
313a940d49a3
ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
38525
diff
changeset
|
511 |
313a940d49a3
ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
38525
diff
changeset
|
512 oldsiginthandler = [signal.getsignal(signal.SIGINT)] |
313a940d49a3
ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
38525
diff
changeset
|
513 shouldbail = [] |
313a940d49a3
ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
38525
diff
changeset
|
514 |
313a940d49a3
ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
38525
diff
changeset
|
515 def disabledsiginthandler(*args): |
313a940d49a3
ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
38525
diff
changeset
|
516 if warn(): |
313a940d49a3
ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
38525
diff
changeset
|
517 signal.signal(signal.SIGINT, oldsiginthandler[0]) |
313a940d49a3
ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
38525
diff
changeset
|
518 del oldsiginthandler[0] |
313a940d49a3
ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
38525
diff
changeset
|
519 shouldbail.append(True) |
313a940d49a3
ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
38525
diff
changeset
|
520 |
313a940d49a3
ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
38525
diff
changeset
|
521 try: |
313a940d49a3
ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
38525
diff
changeset
|
522 try: |
313a940d49a3
ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
38525
diff
changeset
|
523 signal.signal(signal.SIGINT, disabledsiginthandler) |
313a940d49a3
ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
38525
diff
changeset
|
524 except ValueError: |
313a940d49a3
ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
38525
diff
changeset
|
525 # wrong thread, oh well, we tried |
313a940d49a3
ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
38525
diff
changeset
|
526 del oldsiginthandler[0] |
313a940d49a3
ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
38525
diff
changeset
|
527 yield |
313a940d49a3
ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
38525
diff
changeset
|
528 finally: |
313a940d49a3
ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
38525
diff
changeset
|
529 if oldsiginthandler: |
313a940d49a3
ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
38525
diff
changeset
|
530 signal.signal(signal.SIGINT, oldsiginthandler[0]) |
313a940d49a3
ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
38525
diff
changeset
|
531 if shouldbail: |
313a940d49a3
ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
38525
diff
changeset
|
532 raise KeyboardInterrupt |
40497
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
533 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
534 |
40497
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
535 if pycompat.iswindows: |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
536 # no fork on Windows, but we can create a detached process |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
537 # https://msdn.microsoft.com/en-us/library/windows/desktop/ms684863.aspx |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
538 # No stdlib constant exists for this value |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
539 DETACHED_PROCESS = 0x00000008 |
40536
1d3bed7d2923
procutil: import concerns about creationflags on Windows from D1701
Boris Feld <boris.feld@octobus.net>
parents:
40498
diff
changeset
|
540 # Following creation flags might create a console GUI window. |
1d3bed7d2923
procutil: import concerns about creationflags on Windows from D1701
Boris Feld <boris.feld@octobus.net>
parents:
40498
diff
changeset
|
541 # Using subprocess.CREATE_NEW_CONSOLE might helps. |
1d3bed7d2923
procutil: import concerns about creationflags on Windows from D1701
Boris Feld <boris.feld@octobus.net>
parents:
40498
diff
changeset
|
542 # See https://phab.mercurial-scm.org/D1701 for discussion |
40497
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
543 _creationflags = DETACHED_PROCESS | subprocess.CREATE_NEW_PROCESS_GROUP |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
544 |
42496
ca1014ad3de4
procutil: allow callers of runbgcommand to assume the process starts
Augie Fackler <augie@google.com>
parents:
41832
diff
changeset
|
545 def runbgcommand( |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
546 script, env, shell=False, stdout=None, stderr=None, ensurestart=True |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
547 ): |
40497
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
548 '''Spawn a command without waiting for it to finish.''' |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
549 # we can't use close_fds *and* redirect stdin. I'm not sure that we |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
550 # need to because the detached process has no console connection. |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
551 subprocess.Popen( |
40498
8fab95aa5280
procutil: port over windows encoding fixes from logtoprocess
Augie Fackler <augie@google.com>
parents:
40497
diff
changeset
|
552 tonativestr(script), |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
553 shell=shell, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
554 env=tonativeenv(env), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
555 close_fds=True, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
556 creationflags=_creationflags, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
557 stdout=stdout, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
558 stderr=stderr, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
559 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
560 |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
561 |
40497
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
562 else: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
563 |
42496
ca1014ad3de4
procutil: allow callers of runbgcommand to assume the process starts
Augie Fackler <augie@google.com>
parents:
41832
diff
changeset
|
564 def runbgcommand( |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
565 cmd, env, shell=False, stdout=None, stderr=None, ensurestart=True |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
566 ): |
40497
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
567 '''Spawn a command without waiting for it to finish.''' |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
568 # double-fork to completely detach from the parent process |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
569 # based on http://code.activestate.com/recipes/278731 |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
570 pid = os.fork() |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
571 if pid: |
42496
ca1014ad3de4
procutil: allow callers of runbgcommand to assume the process starts
Augie Fackler <augie@google.com>
parents:
41832
diff
changeset
|
572 if not ensurestart: |
ca1014ad3de4
procutil: allow callers of runbgcommand to assume the process starts
Augie Fackler <augie@google.com>
parents:
41832
diff
changeset
|
573 return |
40497
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
574 # Parent process |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
575 (_pid, status) = os.waitpid(pid, 0) |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
576 if os.WIFEXITED(status): |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
577 returncode = os.WEXITSTATUS(status) |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
578 else: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
579 returncode = -(os.WTERMSIG(status)) |
40497
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
580 if returncode != 0: |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
581 # The child process's return code is 0 on success, an errno |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
582 # value on failure, or 255 if we don't have a valid errno |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
583 # value. |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
584 # |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
585 # (It would be slightly nicer to return the full exception info |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
586 # over a pipe as the subprocess module does. For now it |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
587 # doesn't seem worth adding that complexity here, though.) |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
588 if returncode == 255: |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
589 returncode = errno.EINVAL |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
590 raise OSError( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
591 returncode, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
592 'error running %r: %s' % (cmd, os.strerror(returncode)), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
593 ) |
40497
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
594 return |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
595 |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
596 returncode = 255 |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
597 try: |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
598 # Start a new session |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
599 os.setsid() |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
600 |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
601 stdin = open(os.devnull, 'r') |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
602 if stdout is None: |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
603 stdout = open(os.devnull, 'w') |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
604 if stderr is None: |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
605 stderr = open(os.devnull, 'w') |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
606 |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
607 # connect stdin to devnull to make sure the subprocess can't |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
608 # muck up that stream for mercurial. |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
609 subprocess.Popen( |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
610 cmd, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
611 shell=shell, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
612 env=env, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
613 close_fds=True, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
614 stdin=stdin, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
615 stdout=stdout, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
616 stderr=stderr, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
617 ) |
40497
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
618 returncode = 0 |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
619 except EnvironmentError as ex: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
620 returncode = ex.errno & 0xFF |
40497
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
621 if returncode == 0: |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
622 # This shouldn't happen, but just in case make sure the |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
623 # return code is never 0 here. |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
624 returncode = 255 |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
625 except Exception: |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
626 returncode = 255 |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
627 finally: |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
628 # mission accomplished, this child needs to exit and not |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
629 # continue the hg process here. |
3fbfbc8c9f82
remotefilelog: transplant runbgcommand to procutil
Augie Fackler <augie@google.com>
parents:
39840
diff
changeset
|
630 os._exit(returncode) |