##// END OF EJS Templates
util: add a stream compression API to compression engines...
Gregory Szorc -
r30356:c86109ef default
parent child Browse files
Show More
@@ -1,3074 +1,3112 b''
1 1 # util.py - Mercurial utility functions and platform specific implementations
2 2 #
3 3 # Copyright 2005 K. Thananchayan <thananck@yahoo.com>
4 4 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
5 5 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
6 6 #
7 7 # This software may be used and distributed according to the terms of the
8 8 # GNU General Public License version 2 or any later version.
9 9
10 10 """Mercurial utility functions and platform specific implementations.
11 11
12 12 This contains helper routines that are independent of the SCM core and
13 13 hide platform-specific details from the core.
14 14 """
15 15
16 16 from __future__ import absolute_import
17 17
18 18 import bz2
19 19 import calendar
20 20 import collections
21 21 import datetime
22 22 import errno
23 23 import gc
24 24 import hashlib
25 25 import imp
26 26 import os
27 27 import re as remod
28 28 import shutil
29 29 import signal
30 30 import socket
31 31 import string
32 32 import subprocess
33 33 import sys
34 34 import tempfile
35 35 import textwrap
36 36 import time
37 37 import traceback
38 38 import zlib
39 39
40 40 from . import (
41 41 encoding,
42 42 error,
43 43 i18n,
44 44 osutil,
45 45 parsers,
46 46 pycompat,
47 47 )
48 48
49 49 for attr in (
50 50 'empty',
51 51 'httplib',
52 52 'httpserver',
53 53 'pickle',
54 54 'queue',
55 55 'urlerr',
56 56 'urlparse',
57 57 # we do import urlreq, but we do it outside the loop
58 58 #'urlreq',
59 59 'stringio',
60 60 'socketserver',
61 61 'xmlrpclib',
62 62 ):
63 63 a = pycompat.sysstr(attr)
64 64 globals()[a] = getattr(pycompat, a)
65 65
66 66 # This line is to make pyflakes happy:
67 67 urlreq = pycompat.urlreq
68 68
69 69 if os.name == 'nt':
70 70 from . import windows as platform
71 71 else:
72 72 from . import posix as platform
73 73
74 74 _ = i18n._
75 75
76 76 bindunixsocket = platform.bindunixsocket
77 77 cachestat = platform.cachestat
78 78 checkexec = platform.checkexec
79 79 checklink = platform.checklink
80 80 copymode = platform.copymode
81 81 executablepath = platform.executablepath
82 82 expandglobs = platform.expandglobs
83 83 explainexit = platform.explainexit
84 84 findexe = platform.findexe
85 85 gethgcmd = platform.gethgcmd
86 86 getuser = platform.getuser
87 87 getpid = os.getpid
88 88 groupmembers = platform.groupmembers
89 89 groupname = platform.groupname
90 90 hidewindow = platform.hidewindow
91 91 isexec = platform.isexec
92 92 isowner = platform.isowner
93 93 localpath = platform.localpath
94 94 lookupreg = platform.lookupreg
95 95 makedir = platform.makedir
96 96 nlinks = platform.nlinks
97 97 normpath = platform.normpath
98 98 normcase = platform.normcase
99 99 normcasespec = platform.normcasespec
100 100 normcasefallback = platform.normcasefallback
101 101 openhardlinks = platform.openhardlinks
102 102 oslink = platform.oslink
103 103 parsepatchoutput = platform.parsepatchoutput
104 104 pconvert = platform.pconvert
105 105 poll = platform.poll
106 106 popen = platform.popen
107 107 posixfile = platform.posixfile
108 108 quotecommand = platform.quotecommand
109 109 readpipe = platform.readpipe
110 110 rename = platform.rename
111 111 removedirs = platform.removedirs
112 112 samedevice = platform.samedevice
113 113 samefile = platform.samefile
114 114 samestat = platform.samestat
115 115 setbinary = platform.setbinary
116 116 setflags = platform.setflags
117 117 setsignalhandler = platform.setsignalhandler
118 118 shellquote = platform.shellquote
119 119 spawndetached = platform.spawndetached
120 120 split = platform.split
121 121 sshargs = platform.sshargs
122 122 statfiles = getattr(osutil, 'statfiles', platform.statfiles)
123 123 statisexec = platform.statisexec
124 124 statislink = platform.statislink
125 125 testpid = platform.testpid
126 126 umask = platform.umask
127 127 unlink = platform.unlink
128 128 unlinkpath = platform.unlinkpath
129 129 username = platform.username
130 130
131 131 # Python compatibility
132 132
133 133 _notset = object()
134 134
135 135 # disable Python's problematic floating point timestamps (issue4836)
136 136 # (Python hypocritically says you shouldn't change this behavior in
137 137 # libraries, and sure enough Mercurial is not a library.)
138 138 os.stat_float_times(False)
139 139
140 140 def safehasattr(thing, attr):
141 141 return getattr(thing, attr, _notset) is not _notset
142 142
143 143 DIGESTS = {
144 144 'md5': hashlib.md5,
145 145 'sha1': hashlib.sha1,
146 146 'sha512': hashlib.sha512,
147 147 }
148 148 # List of digest types from strongest to weakest
149 149 DIGESTS_BY_STRENGTH = ['sha512', 'sha1', 'md5']
150 150
151 151 for k in DIGESTS_BY_STRENGTH:
152 152 assert k in DIGESTS
153 153
154 154 class digester(object):
155 155 """helper to compute digests.
156 156
157 157 This helper can be used to compute one or more digests given their name.
158 158
159 159 >>> d = digester(['md5', 'sha1'])
160 160 >>> d.update('foo')
161 161 >>> [k for k in sorted(d)]
162 162 ['md5', 'sha1']
163 163 >>> d['md5']
164 164 'acbd18db4cc2f85cedef654fccc4a4d8'
165 165 >>> d['sha1']
166 166 '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33'
167 167 >>> digester.preferred(['md5', 'sha1'])
168 168 'sha1'
169 169 """
170 170
171 171 def __init__(self, digests, s=''):
172 172 self._hashes = {}
173 173 for k in digests:
174 174 if k not in DIGESTS:
175 175 raise Abort(_('unknown digest type: %s') % k)
176 176 self._hashes[k] = DIGESTS[k]()
177 177 if s:
178 178 self.update(s)
179 179
180 180 def update(self, data):
181 181 for h in self._hashes.values():
182 182 h.update(data)
183 183
184 184 def __getitem__(self, key):
185 185 if key not in DIGESTS:
186 186 raise Abort(_('unknown digest type: %s') % k)
187 187 return self._hashes[key].hexdigest()
188 188
189 189 def __iter__(self):
190 190 return iter(self._hashes)
191 191
192 192 @staticmethod
193 193 def preferred(supported):
194 194 """returns the strongest digest type in both supported and DIGESTS."""
195 195
196 196 for k in DIGESTS_BY_STRENGTH:
197 197 if k in supported:
198 198 return k
199 199 return None
200 200
201 201 class digestchecker(object):
202 202 """file handle wrapper that additionally checks content against a given
203 203 size and digests.
204 204
205 205 d = digestchecker(fh, size, {'md5': '...'})
206 206
207 207 When multiple digests are given, all of them are validated.
208 208 """
209 209
210 210 def __init__(self, fh, size, digests):
211 211 self._fh = fh
212 212 self._size = size
213 213 self._got = 0
214 214 self._digests = dict(digests)
215 215 self._digester = digester(self._digests.keys())
216 216
217 217 def read(self, length=-1):
218 218 content = self._fh.read(length)
219 219 self._digester.update(content)
220 220 self._got += len(content)
221 221 return content
222 222
223 223 def validate(self):
224 224 if self._size != self._got:
225 225 raise Abort(_('size mismatch: expected %d, got %d') %
226 226 (self._size, self._got))
227 227 for k, v in self._digests.items():
228 228 if v != self._digester[k]:
229 229 # i18n: first parameter is a digest name
230 230 raise Abort(_('%s mismatch: expected %s, got %s') %
231 231 (k, v, self._digester[k]))
232 232
233 233 try:
234 234 buffer = buffer
235 235 except NameError:
236 236 if not pycompat.ispy3:
237 237 def buffer(sliceable, offset=0):
238 238 return sliceable[offset:]
239 239 else:
240 240 def buffer(sliceable, offset=0):
241 241 return memoryview(sliceable)[offset:]
242 242
243 243 closefds = os.name == 'posix'
244 244
245 245 _chunksize = 4096
246 246
247 247 class bufferedinputpipe(object):
248 248 """a manually buffered input pipe
249 249
250 250 Python will not let us use buffered IO and lazy reading with 'polling' at
251 251 the same time. We cannot probe the buffer state and select will not detect
252 252 that data are ready to read if they are already buffered.
253 253
254 254 This class let us work around that by implementing its own buffering
255 255 (allowing efficient readline) while offering a way to know if the buffer is
256 256 empty from the output (allowing collaboration of the buffer with polling).
257 257
258 258 This class lives in the 'util' module because it makes use of the 'os'
259 259 module from the python stdlib.
260 260 """
261 261
262 262 def __init__(self, input):
263 263 self._input = input
264 264 self._buffer = []
265 265 self._eof = False
266 266 self._lenbuf = 0
267 267
268 268 @property
269 269 def hasbuffer(self):
270 270 """True is any data is currently buffered
271 271
272 272 This will be used externally a pre-step for polling IO. If there is
273 273 already data then no polling should be set in place."""
274 274 return bool(self._buffer)
275 275
276 276 @property
277 277 def closed(self):
278 278 return self._input.closed
279 279
280 280 def fileno(self):
281 281 return self._input.fileno()
282 282
283 283 def close(self):
284 284 return self._input.close()
285 285
286 286 def read(self, size):
287 287 while (not self._eof) and (self._lenbuf < size):
288 288 self._fillbuffer()
289 289 return self._frombuffer(size)
290 290
291 291 def readline(self, *args, **kwargs):
292 292 if 1 < len(self._buffer):
293 293 # this should not happen because both read and readline end with a
294 294 # _frombuffer call that collapse it.
295 295 self._buffer = [''.join(self._buffer)]
296 296 self._lenbuf = len(self._buffer[0])
297 297 lfi = -1
298 298 if self._buffer:
299 299 lfi = self._buffer[-1].find('\n')
300 300 while (not self._eof) and lfi < 0:
301 301 self._fillbuffer()
302 302 if self._buffer:
303 303 lfi = self._buffer[-1].find('\n')
304 304 size = lfi + 1
305 305 if lfi < 0: # end of file
306 306 size = self._lenbuf
307 307 elif 1 < len(self._buffer):
308 308 # we need to take previous chunks into account
309 309 size += self._lenbuf - len(self._buffer[-1])
310 310 return self._frombuffer(size)
311 311
312 312 def _frombuffer(self, size):
313 313 """return at most 'size' data from the buffer
314 314
315 315 The data are removed from the buffer."""
316 316 if size == 0 or not self._buffer:
317 317 return ''
318 318 buf = self._buffer[0]
319 319 if 1 < len(self._buffer):
320 320 buf = ''.join(self._buffer)
321 321
322 322 data = buf[:size]
323 323 buf = buf[len(data):]
324 324 if buf:
325 325 self._buffer = [buf]
326 326 self._lenbuf = len(buf)
327 327 else:
328 328 self._buffer = []
329 329 self._lenbuf = 0
330 330 return data
331 331
332 332 def _fillbuffer(self):
333 333 """read data to the buffer"""
334 334 data = os.read(self._input.fileno(), _chunksize)
335 335 if not data:
336 336 self._eof = True
337 337 else:
338 338 self._lenbuf += len(data)
339 339 self._buffer.append(data)
340 340
341 341 def popen2(cmd, env=None, newlines=False):
342 342 # Setting bufsize to -1 lets the system decide the buffer size.
343 343 # The default for bufsize is 0, meaning unbuffered. This leads to
344 344 # poor performance on Mac OS X: http://bugs.python.org/issue4194
345 345 p = subprocess.Popen(cmd, shell=True, bufsize=-1,
346 346 close_fds=closefds,
347 347 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
348 348 universal_newlines=newlines,
349 349 env=env)
350 350 return p.stdin, p.stdout
351 351
352 352 def popen3(cmd, env=None, newlines=False):
353 353 stdin, stdout, stderr, p = popen4(cmd, env, newlines)
354 354 return stdin, stdout, stderr
355 355
356 356 def popen4(cmd, env=None, newlines=False, bufsize=-1):
357 357 p = subprocess.Popen(cmd, shell=True, bufsize=bufsize,
358 358 close_fds=closefds,
359 359 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
360 360 stderr=subprocess.PIPE,
361 361 universal_newlines=newlines,
362 362 env=env)
363 363 return p.stdin, p.stdout, p.stderr, p
364 364
365 365 def version():
366 366 """Return version information if available."""
367 367 try:
368 368 from . import __version__
369 369 return __version__.version
370 370 except ImportError:
371 371 return 'unknown'
372 372
373 373 def versiontuple(v=None, n=4):
374 374 """Parses a Mercurial version string into an N-tuple.
375 375
376 376 The version string to be parsed is specified with the ``v`` argument.
377 377 If it isn't defined, the current Mercurial version string will be parsed.
378 378
379 379 ``n`` can be 2, 3, or 4. Here is how some version strings map to
380 380 returned values:
381 381
382 382 >>> v = '3.6.1+190-df9b73d2d444'
383 383 >>> versiontuple(v, 2)
384 384 (3, 6)
385 385 >>> versiontuple(v, 3)
386 386 (3, 6, 1)
387 387 >>> versiontuple(v, 4)
388 388 (3, 6, 1, '190-df9b73d2d444')
389 389
390 390 >>> versiontuple('3.6.1+190-df9b73d2d444+20151118')
391 391 (3, 6, 1, '190-df9b73d2d444+20151118')
392 392
393 393 >>> v = '3.6'
394 394 >>> versiontuple(v, 2)
395 395 (3, 6)
396 396 >>> versiontuple(v, 3)
397 397 (3, 6, None)
398 398 >>> versiontuple(v, 4)
399 399 (3, 6, None, None)
400 400
401 401 >>> v = '3.9-rc'
402 402 >>> versiontuple(v, 2)
403 403 (3, 9)
404 404 >>> versiontuple(v, 3)
405 405 (3, 9, None)
406 406 >>> versiontuple(v, 4)
407 407 (3, 9, None, 'rc')
408 408
409 409 >>> v = '3.9-rc+2-02a8fea4289b'
410 410 >>> versiontuple(v, 2)
411 411 (3, 9)
412 412 >>> versiontuple(v, 3)
413 413 (3, 9, None)
414 414 >>> versiontuple(v, 4)
415 415 (3, 9, None, 'rc+2-02a8fea4289b')
416 416 """
417 417 if not v:
418 418 v = version()
419 419 parts = remod.split('[\+-]', v, 1)
420 420 if len(parts) == 1:
421 421 vparts, extra = parts[0], None
422 422 else:
423 423 vparts, extra = parts
424 424
425 425 vints = []
426 426 for i in vparts.split('.'):
427 427 try:
428 428 vints.append(int(i))
429 429 except ValueError:
430 430 break
431 431 # (3, 6) -> (3, 6, None)
432 432 while len(vints) < 3:
433 433 vints.append(None)
434 434
435 435 if n == 2:
436 436 return (vints[0], vints[1])
437 437 if n == 3:
438 438 return (vints[0], vints[1], vints[2])
439 439 if n == 4:
440 440 return (vints[0], vints[1], vints[2], extra)
441 441
442 442 # used by parsedate
443 443 defaultdateformats = (
444 444 '%Y-%m-%dT%H:%M:%S', # the 'real' ISO8601
445 445 '%Y-%m-%dT%H:%M', # without seconds
446 446 '%Y-%m-%dT%H%M%S', # another awful but legal variant without :
447 447 '%Y-%m-%dT%H%M', # without seconds
448 448 '%Y-%m-%d %H:%M:%S', # our common legal variant
449 449 '%Y-%m-%d %H:%M', # without seconds
450 450 '%Y-%m-%d %H%M%S', # without :
451 451 '%Y-%m-%d %H%M', # without seconds
452 452 '%Y-%m-%d %I:%M:%S%p',
453 453 '%Y-%m-%d %H:%M',
454 454 '%Y-%m-%d %I:%M%p',
455 455 '%Y-%m-%d',
456 456 '%m-%d',
457 457 '%m/%d',
458 458 '%m/%d/%y',
459 459 '%m/%d/%Y',
460 460 '%a %b %d %H:%M:%S %Y',
461 461 '%a %b %d %I:%M:%S%p %Y',
462 462 '%a, %d %b %Y %H:%M:%S', # GNU coreutils "/bin/date --rfc-2822"
463 463 '%b %d %H:%M:%S %Y',
464 464 '%b %d %I:%M:%S%p %Y',
465 465 '%b %d %H:%M:%S',
466 466 '%b %d %I:%M:%S%p',
467 467 '%b %d %H:%M',
468 468 '%b %d %I:%M%p',
469 469 '%b %d %Y',
470 470 '%b %d',
471 471 '%H:%M:%S',
472 472 '%I:%M:%S%p',
473 473 '%H:%M',
474 474 '%I:%M%p',
475 475 )
476 476
477 477 extendeddateformats = defaultdateformats + (
478 478 "%Y",
479 479 "%Y-%m",
480 480 "%b",
481 481 "%b %Y",
482 482 )
483 483
484 484 def cachefunc(func):
485 485 '''cache the result of function calls'''
486 486 # XXX doesn't handle keywords args
487 487 if func.__code__.co_argcount == 0:
488 488 cache = []
489 489 def f():
490 490 if len(cache) == 0:
491 491 cache.append(func())
492 492 return cache[0]
493 493 return f
494 494 cache = {}
495 495 if func.__code__.co_argcount == 1:
496 496 # we gain a small amount of time because
497 497 # we don't need to pack/unpack the list
498 498 def f(arg):
499 499 if arg not in cache:
500 500 cache[arg] = func(arg)
501 501 return cache[arg]
502 502 else:
503 503 def f(*args):
504 504 if args not in cache:
505 505 cache[args] = func(*args)
506 506 return cache[args]
507 507
508 508 return f
509 509
510 510 class sortdict(dict):
511 511 '''a simple sorted dictionary'''
512 512 def __init__(self, data=None):
513 513 self._list = []
514 514 if data:
515 515 self.update(data)
516 516 def copy(self):
517 517 return sortdict(self)
518 518 def __setitem__(self, key, val):
519 519 if key in self:
520 520 self._list.remove(key)
521 521 self._list.append(key)
522 522 dict.__setitem__(self, key, val)
523 523 def __iter__(self):
524 524 return self._list.__iter__()
525 525 def update(self, src):
526 526 if isinstance(src, dict):
527 527 src = src.iteritems()
528 528 for k, v in src:
529 529 self[k] = v
530 530 def clear(self):
531 531 dict.clear(self)
532 532 self._list = []
533 533 def items(self):
534 534 return [(k, self[k]) for k in self._list]
535 535 def __delitem__(self, key):
536 536 dict.__delitem__(self, key)
537 537 self._list.remove(key)
538 538 def pop(self, key, *args, **kwargs):
539 539 dict.pop(self, key, *args, **kwargs)
540 540 try:
541 541 self._list.remove(key)
542 542 except ValueError:
543 543 pass
544 544 def keys(self):
545 545 return self._list
546 546 def iterkeys(self):
547 547 return self._list.__iter__()
548 548 def iteritems(self):
549 549 for k in self._list:
550 550 yield k, self[k]
551 551 def insert(self, index, key, val):
552 552 self._list.insert(index, key)
553 553 dict.__setitem__(self, key, val)
554 554 def __repr__(self):
555 555 if not self:
556 556 return '%s()' % self.__class__.__name__
557 557 return '%s(%r)' % (self.__class__.__name__, self.items())
558 558
559 559 class _lrucachenode(object):
560 560 """A node in a doubly linked list.
561 561
562 562 Holds a reference to nodes on either side as well as a key-value
563 563 pair for the dictionary entry.
564 564 """
565 565 __slots__ = (u'next', u'prev', u'key', u'value')
566 566
567 567 def __init__(self):
568 568 self.next = None
569 569 self.prev = None
570 570
571 571 self.key = _notset
572 572 self.value = None
573 573
574 574 def markempty(self):
575 575 """Mark the node as emptied."""
576 576 self.key = _notset
577 577
578 578 class lrucachedict(object):
579 579 """Dict that caches most recent accesses and sets.
580 580
581 581 The dict consists of an actual backing dict - indexed by original
582 582 key - and a doubly linked circular list defining the order of entries in
583 583 the cache.
584 584
585 585 The head node is the newest entry in the cache. If the cache is full,
586 586 we recycle head.prev and make it the new head. Cache accesses result in
587 587 the node being moved to before the existing head and being marked as the
588 588 new head node.
589 589 """
590 590 def __init__(self, max):
591 591 self._cache = {}
592 592
593 593 self._head = head = _lrucachenode()
594 594 head.prev = head
595 595 head.next = head
596 596 self._size = 1
597 597 self._capacity = max
598 598
599 599 def __len__(self):
600 600 return len(self._cache)
601 601
602 602 def __contains__(self, k):
603 603 return k in self._cache
604 604
605 605 def __iter__(self):
606 606 # We don't have to iterate in cache order, but why not.
607 607 n = self._head
608 608 for i in range(len(self._cache)):
609 609 yield n.key
610 610 n = n.next
611 611
612 612 def __getitem__(self, k):
613 613 node = self._cache[k]
614 614 self._movetohead(node)
615 615 return node.value
616 616
617 617 def __setitem__(self, k, v):
618 618 node = self._cache.get(k)
619 619 # Replace existing value and mark as newest.
620 620 if node is not None:
621 621 node.value = v
622 622 self._movetohead(node)
623 623 return
624 624
625 625 if self._size < self._capacity:
626 626 node = self._addcapacity()
627 627 else:
628 628 # Grab the last/oldest item.
629 629 node = self._head.prev
630 630
631 631 # At capacity. Kill the old entry.
632 632 if node.key is not _notset:
633 633 del self._cache[node.key]
634 634
635 635 node.key = k
636 636 node.value = v
637 637 self._cache[k] = node
638 638 # And mark it as newest entry. No need to adjust order since it
639 639 # is already self._head.prev.
640 640 self._head = node
641 641
642 642 def __delitem__(self, k):
643 643 node = self._cache.pop(k)
644 644 node.markempty()
645 645
646 646 # Temporarily mark as newest item before re-adjusting head to make
647 647 # this node the oldest item.
648 648 self._movetohead(node)
649 649 self._head = node.next
650 650
651 651 # Additional dict methods.
652 652
653 653 def get(self, k, default=None):
654 654 try:
655 655 return self._cache[k].value
656 656 except KeyError:
657 657 return default
658 658
659 659 def clear(self):
660 660 n = self._head
661 661 while n.key is not _notset:
662 662 n.markempty()
663 663 n = n.next
664 664
665 665 self._cache.clear()
666 666
667 667 def copy(self):
668 668 result = lrucachedict(self._capacity)
669 669 n = self._head.prev
670 670 # Iterate in oldest-to-newest order, so the copy has the right ordering
671 671 for i in range(len(self._cache)):
672 672 result[n.key] = n.value
673 673 n = n.prev
674 674 return result
675 675
676 676 def _movetohead(self, node):
677 677 """Mark a node as the newest, making it the new head.
678 678
679 679 When a node is accessed, it becomes the freshest entry in the LRU
680 680 list, which is denoted by self._head.
681 681
682 682 Visually, let's make ``N`` the new head node (* denotes head):
683 683
684 684 previous/oldest <-> head <-> next/next newest
685 685
686 686 ----<->--- A* ---<->-----
687 687 | |
688 688 E <-> D <-> N <-> C <-> B
689 689
690 690 To:
691 691
692 692 ----<->--- N* ---<->-----
693 693 | |
694 694 E <-> D <-> C <-> B <-> A
695 695
696 696 This requires the following moves:
697 697
698 698 C.next = D (node.prev.next = node.next)
699 699 D.prev = C (node.next.prev = node.prev)
700 700 E.next = N (head.prev.next = node)
701 701 N.prev = E (node.prev = head.prev)
702 702 N.next = A (node.next = head)
703 703 A.prev = N (head.prev = node)
704 704 """
705 705 head = self._head
706 706 # C.next = D
707 707 node.prev.next = node.next
708 708 # D.prev = C
709 709 node.next.prev = node.prev
710 710 # N.prev = E
711 711 node.prev = head.prev
712 712 # N.next = A
713 713 # It is tempting to do just "head" here, however if node is
714 714 # adjacent to head, this will do bad things.
715 715 node.next = head.prev.next
716 716 # E.next = N
717 717 node.next.prev = node
718 718 # A.prev = N
719 719 node.prev.next = node
720 720
721 721 self._head = node
722 722
723 723 def _addcapacity(self):
724 724 """Add a node to the circular linked list.
725 725
726 726 The new node is inserted before the head node.
727 727 """
728 728 head = self._head
729 729 node = _lrucachenode()
730 730 head.prev.next = node
731 731 node.prev = head.prev
732 732 node.next = head
733 733 head.prev = node
734 734 self._size += 1
735 735 return node
736 736
737 737 def lrucachefunc(func):
738 738 '''cache most recent results of function calls'''
739 739 cache = {}
740 740 order = collections.deque()
741 741 if func.__code__.co_argcount == 1:
742 742 def f(arg):
743 743 if arg not in cache:
744 744 if len(cache) > 20:
745 745 del cache[order.popleft()]
746 746 cache[arg] = func(arg)
747 747 else:
748 748 order.remove(arg)
749 749 order.append(arg)
750 750 return cache[arg]
751 751 else:
752 752 def f(*args):
753 753 if args not in cache:
754 754 if len(cache) > 20:
755 755 del cache[order.popleft()]
756 756 cache[args] = func(*args)
757 757 else:
758 758 order.remove(args)
759 759 order.append(args)
760 760 return cache[args]
761 761
762 762 return f
763 763
764 764 class propertycache(object):
765 765 def __init__(self, func):
766 766 self.func = func
767 767 self.name = func.__name__
768 768 def __get__(self, obj, type=None):
769 769 result = self.func(obj)
770 770 self.cachevalue(obj, result)
771 771 return result
772 772
773 773 def cachevalue(self, obj, value):
774 774 # __dict__ assignment required to bypass __setattr__ (eg: repoview)
775 775 obj.__dict__[self.name] = value
776 776
777 777 def pipefilter(s, cmd):
778 778 '''filter string S through command CMD, returning its output'''
779 779 p = subprocess.Popen(cmd, shell=True, close_fds=closefds,
780 780 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
781 781 pout, perr = p.communicate(s)
782 782 return pout
783 783
784 784 def tempfilter(s, cmd):
785 785 '''filter string S through a pair of temporary files with CMD.
786 786 CMD is used as a template to create the real command to be run,
787 787 with the strings INFILE and OUTFILE replaced by the real names of
788 788 the temporary files generated.'''
789 789 inname, outname = None, None
790 790 try:
791 791 infd, inname = tempfile.mkstemp(prefix='hg-filter-in-')
792 792 fp = os.fdopen(infd, 'wb')
793 793 fp.write(s)
794 794 fp.close()
795 795 outfd, outname = tempfile.mkstemp(prefix='hg-filter-out-')
796 796 os.close(outfd)
797 797 cmd = cmd.replace('INFILE', inname)
798 798 cmd = cmd.replace('OUTFILE', outname)
799 799 code = os.system(cmd)
800 800 if sys.platform == 'OpenVMS' and code & 1:
801 801 code = 0
802 802 if code:
803 803 raise Abort(_("command '%s' failed: %s") %
804 804 (cmd, explainexit(code)))
805 805 return readfile(outname)
806 806 finally:
807 807 try:
808 808 if inname:
809 809 os.unlink(inname)
810 810 except OSError:
811 811 pass
812 812 try:
813 813 if outname:
814 814 os.unlink(outname)
815 815 except OSError:
816 816 pass
817 817
818 818 filtertable = {
819 819 'tempfile:': tempfilter,
820 820 'pipe:': pipefilter,
821 821 }
822 822
823 823 def filter(s, cmd):
824 824 "filter a string through a command that transforms its input to its output"
825 825 for name, fn in filtertable.iteritems():
826 826 if cmd.startswith(name):
827 827 return fn(s, cmd[len(name):].lstrip())
828 828 return pipefilter(s, cmd)
829 829
830 830 def binary(s):
831 831 """return true if a string is binary data"""
832 832 return bool(s and '\0' in s)
833 833
834 834 def increasingchunks(source, min=1024, max=65536):
835 835 '''return no less than min bytes per chunk while data remains,
836 836 doubling min after each chunk until it reaches max'''
837 837 def log2(x):
838 838 if not x:
839 839 return 0
840 840 i = 0
841 841 while x:
842 842 x >>= 1
843 843 i += 1
844 844 return i - 1
845 845
846 846 buf = []
847 847 blen = 0
848 848 for chunk in source:
849 849 buf.append(chunk)
850 850 blen += len(chunk)
851 851 if blen >= min:
852 852 if min < max:
853 853 min = min << 1
854 854 nmin = 1 << log2(blen)
855 855 if nmin > min:
856 856 min = nmin
857 857 if min > max:
858 858 min = max
859 859 yield ''.join(buf)
860 860 blen = 0
861 861 buf = []
862 862 if buf:
863 863 yield ''.join(buf)
864 864
865 865 Abort = error.Abort
866 866
867 867 def always(fn):
868 868 return True
869 869
870 870 def never(fn):
871 871 return False
872 872
873 873 def nogc(func):
874 874 """disable garbage collector
875 875
876 876 Python's garbage collector triggers a GC each time a certain number of
877 877 container objects (the number being defined by gc.get_threshold()) are
878 878 allocated even when marked not to be tracked by the collector. Tracking has
879 879 no effect on when GCs are triggered, only on what objects the GC looks
880 880 into. As a workaround, disable GC while building complex (huge)
881 881 containers.
882 882
883 883 This garbage collector issue have been fixed in 2.7.
884 884 """
885 885 if sys.version_info >= (2, 7):
886 886 return func
887 887 def wrapper(*args, **kwargs):
888 888 gcenabled = gc.isenabled()
889 889 gc.disable()
890 890 try:
891 891 return func(*args, **kwargs)
892 892 finally:
893 893 if gcenabled:
894 894 gc.enable()
895 895 return wrapper
896 896
897 897 def pathto(root, n1, n2):
898 898 '''return the relative path from one place to another.
899 899 root should use os.sep to separate directories
900 900 n1 should use os.sep to separate directories
901 901 n2 should use "/" to separate directories
902 902 returns an os.sep-separated path.
903 903
904 904 If n1 is a relative path, it's assumed it's
905 905 relative to root.
906 906 n2 should always be relative to root.
907 907 '''
908 908 if not n1:
909 909 return localpath(n2)
910 910 if os.path.isabs(n1):
911 911 if os.path.splitdrive(root)[0] != os.path.splitdrive(n1)[0]:
912 912 return os.path.join(root, localpath(n2))
913 913 n2 = '/'.join((pconvert(root), n2))
914 914 a, b = splitpath(n1), n2.split('/')
915 915 a.reverse()
916 916 b.reverse()
917 917 while a and b and a[-1] == b[-1]:
918 918 a.pop()
919 919 b.pop()
920 920 b.reverse()
921 921 return os.sep.join((['..'] * len(a)) + b) or '.'
922 922
923 923 def mainfrozen():
924 924 """return True if we are a frozen executable.
925 925
926 926 The code supports py2exe (most common, Windows only) and tools/freeze
927 927 (portable, not much used).
928 928 """
929 929 return (safehasattr(sys, "frozen") or # new py2exe
930 930 safehasattr(sys, "importers") or # old py2exe
931 931 imp.is_frozen(u"__main__")) # tools/freeze
932 932
933 933 # the location of data files matching the source code
934 934 if mainfrozen() and getattr(sys, 'frozen', None) != 'macosx_app':
935 935 # executable version (py2exe) doesn't support __file__
936 936 datapath = os.path.dirname(sys.executable)
937 937 else:
938 938 datapath = os.path.dirname(__file__)
939 939
940 940 if not isinstance(datapath, bytes):
941 941 datapath = pycompat.fsencode(datapath)
942 942
943 943 i18n.setdatapath(datapath)
944 944
945 945 _hgexecutable = None
946 946
947 947 def hgexecutable():
948 948 """return location of the 'hg' executable.
949 949
950 950 Defaults to $HG or 'hg' in the search path.
951 951 """
952 952 if _hgexecutable is None:
953 953 hg = os.environ.get('HG')
954 954 mainmod = sys.modules['__main__']
955 955 if hg:
956 956 _sethgexecutable(hg)
957 957 elif mainfrozen():
958 958 if getattr(sys, 'frozen', None) == 'macosx_app':
959 959 # Env variable set by py2app
960 960 _sethgexecutable(os.environ['EXECUTABLEPATH'])
961 961 else:
962 962 _sethgexecutable(sys.executable)
963 963 elif os.path.basename(getattr(mainmod, '__file__', '')) == 'hg':
964 964 _sethgexecutable(mainmod.__file__)
965 965 else:
966 966 exe = findexe('hg') or os.path.basename(sys.argv[0])
967 967 _sethgexecutable(exe)
968 968 return _hgexecutable
969 969
970 970 def _sethgexecutable(path):
971 971 """set location of the 'hg' executable"""
972 972 global _hgexecutable
973 973 _hgexecutable = path
974 974
975 975 def _isstdout(f):
976 976 fileno = getattr(f, 'fileno', None)
977 977 return fileno and fileno() == sys.__stdout__.fileno()
978 978
979 979 def system(cmd, environ=None, cwd=None, onerr=None, errprefix=None, out=None):
980 980 '''enhanced shell command execution.
981 981 run with environment maybe modified, maybe in different dir.
982 982
983 983 if command fails and onerr is None, return status, else raise onerr
984 984 object as exception.
985 985
986 986 if out is specified, it is assumed to be a file-like object that has a
987 987 write() method. stdout and stderr will be redirected to out.'''
988 988 if environ is None:
989 989 environ = {}
990 990 try:
991 991 sys.stdout.flush()
992 992 except Exception:
993 993 pass
994 994 def py2shell(val):
995 995 'convert python object into string that is useful to shell'
996 996 if val is None or val is False:
997 997 return '0'
998 998 if val is True:
999 999 return '1'
1000 1000 return str(val)
1001 1001 origcmd = cmd
1002 1002 cmd = quotecommand(cmd)
1003 1003 if sys.platform == 'plan9' and (sys.version_info[0] == 2
1004 1004 and sys.version_info[1] < 7):
1005 1005 # subprocess kludge to work around issues in half-baked Python
1006 1006 # ports, notably bichued/python:
1007 1007 if not cwd is None:
1008 1008 os.chdir(cwd)
1009 1009 rc = os.system(cmd)
1010 1010 else:
1011 1011 env = dict(os.environ)
1012 1012 env.update((k, py2shell(v)) for k, v in environ.iteritems())
1013 1013 env['HG'] = hgexecutable()
1014 1014 if out is None or _isstdout(out):
1015 1015 rc = subprocess.call(cmd, shell=True, close_fds=closefds,
1016 1016 env=env, cwd=cwd)
1017 1017 else:
1018 1018 proc = subprocess.Popen(cmd, shell=True, close_fds=closefds,
1019 1019 env=env, cwd=cwd, stdout=subprocess.PIPE,
1020 1020 stderr=subprocess.STDOUT)
1021 1021 for line in iter(proc.stdout.readline, ''):
1022 1022 out.write(line)
1023 1023 proc.wait()
1024 1024 rc = proc.returncode
1025 1025 if sys.platform == 'OpenVMS' and rc & 1:
1026 1026 rc = 0
1027 1027 if rc and onerr:
1028 1028 errmsg = '%s %s' % (os.path.basename(origcmd.split(None, 1)[0]),
1029 1029 explainexit(rc)[0])
1030 1030 if errprefix:
1031 1031 errmsg = '%s: %s' % (errprefix, errmsg)
1032 1032 raise onerr(errmsg)
1033 1033 return rc
1034 1034
1035 1035 def checksignature(func):
1036 1036 '''wrap a function with code to check for calling errors'''
1037 1037 def check(*args, **kwargs):
1038 1038 try:
1039 1039 return func(*args, **kwargs)
1040 1040 except TypeError:
1041 1041 if len(traceback.extract_tb(sys.exc_info()[2])) == 1:
1042 1042 raise error.SignatureError
1043 1043 raise
1044 1044
1045 1045 return check
1046 1046
1047 1047 def copyfile(src, dest, hardlink=False, copystat=False, checkambig=False):
1048 1048 '''copy a file, preserving mode and optionally other stat info like
1049 1049 atime/mtime
1050 1050
1051 1051 checkambig argument is used with filestat, and is useful only if
1052 1052 destination file is guarded by any lock (e.g. repo.lock or
1053 1053 repo.wlock).
1054 1054
1055 1055 copystat and checkambig should be exclusive.
1056 1056 '''
1057 1057 assert not (copystat and checkambig)
1058 1058 oldstat = None
1059 1059 if os.path.lexists(dest):
1060 1060 if checkambig:
1061 1061 oldstat = checkambig and filestat(dest)
1062 1062 unlink(dest)
1063 1063 # hardlinks are problematic on CIFS, quietly ignore this flag
1064 1064 # until we find a way to work around it cleanly (issue4546)
1065 1065 if False and hardlink:
1066 1066 try:
1067 1067 oslink(src, dest)
1068 1068 return
1069 1069 except (IOError, OSError):
1070 1070 pass # fall back to normal copy
1071 1071 if os.path.islink(src):
1072 1072 os.symlink(os.readlink(src), dest)
1073 1073 # copytime is ignored for symlinks, but in general copytime isn't needed
1074 1074 # for them anyway
1075 1075 else:
1076 1076 try:
1077 1077 shutil.copyfile(src, dest)
1078 1078 if copystat:
1079 1079 # copystat also copies mode
1080 1080 shutil.copystat(src, dest)
1081 1081 else:
1082 1082 shutil.copymode(src, dest)
1083 1083 if oldstat and oldstat.stat:
1084 1084 newstat = filestat(dest)
1085 1085 if newstat.isambig(oldstat):
1086 1086 # stat of copied file is ambiguous to original one
1087 1087 advanced = (oldstat.stat.st_mtime + 1) & 0x7fffffff
1088 1088 os.utime(dest, (advanced, advanced))
1089 1089 except shutil.Error as inst:
1090 1090 raise Abort(str(inst))
1091 1091
1092 1092 def copyfiles(src, dst, hardlink=None, progress=lambda t, pos: None):
1093 1093 """Copy a directory tree using hardlinks if possible."""
1094 1094 num = 0
1095 1095
1096 1096 if hardlink is None:
1097 1097 hardlink = (os.stat(src).st_dev ==
1098 1098 os.stat(os.path.dirname(dst)).st_dev)
1099 1099 if hardlink:
1100 1100 topic = _('linking')
1101 1101 else:
1102 1102 topic = _('copying')
1103 1103
1104 1104 if os.path.isdir(src):
1105 1105 os.mkdir(dst)
1106 1106 for name, kind in osutil.listdir(src):
1107 1107 srcname = os.path.join(src, name)
1108 1108 dstname = os.path.join(dst, name)
1109 1109 def nprog(t, pos):
1110 1110 if pos is not None:
1111 1111 return progress(t, pos + num)
1112 1112 hardlink, n = copyfiles(srcname, dstname, hardlink, progress=nprog)
1113 1113 num += n
1114 1114 else:
1115 1115 if hardlink:
1116 1116 try:
1117 1117 oslink(src, dst)
1118 1118 except (IOError, OSError):
1119 1119 hardlink = False
1120 1120 shutil.copy(src, dst)
1121 1121 else:
1122 1122 shutil.copy(src, dst)
1123 1123 num += 1
1124 1124 progress(topic, num)
1125 1125 progress(topic, None)
1126 1126
1127 1127 return hardlink, num
1128 1128
1129 1129 _winreservednames = '''con prn aux nul
1130 1130 com1 com2 com3 com4 com5 com6 com7 com8 com9
1131 1131 lpt1 lpt2 lpt3 lpt4 lpt5 lpt6 lpt7 lpt8 lpt9'''.split()
1132 1132 _winreservedchars = ':*?"<>|'
1133 1133 def checkwinfilename(path):
1134 1134 r'''Check that the base-relative path is a valid filename on Windows.
1135 1135 Returns None if the path is ok, or a UI string describing the problem.
1136 1136
1137 1137 >>> checkwinfilename("just/a/normal/path")
1138 1138 >>> checkwinfilename("foo/bar/con.xml")
1139 1139 "filename contains 'con', which is reserved on Windows"
1140 1140 >>> checkwinfilename("foo/con.xml/bar")
1141 1141 "filename contains 'con', which is reserved on Windows"
1142 1142 >>> checkwinfilename("foo/bar/xml.con")
1143 1143 >>> checkwinfilename("foo/bar/AUX/bla.txt")
1144 1144 "filename contains 'AUX', which is reserved on Windows"
1145 1145 >>> checkwinfilename("foo/bar/bla:.txt")
1146 1146 "filename contains ':', which is reserved on Windows"
1147 1147 >>> checkwinfilename("foo/bar/b\07la.txt")
1148 1148 "filename contains '\\x07', which is invalid on Windows"
1149 1149 >>> checkwinfilename("foo/bar/bla ")
1150 1150 "filename ends with ' ', which is not allowed on Windows"
1151 1151 >>> checkwinfilename("../bar")
1152 1152 >>> checkwinfilename("foo\\")
1153 1153 "filename ends with '\\', which is invalid on Windows"
1154 1154 >>> checkwinfilename("foo\\/bar")
1155 1155 "directory name ends with '\\', which is invalid on Windows"
1156 1156 '''
1157 1157 if path.endswith('\\'):
1158 1158 return _("filename ends with '\\', which is invalid on Windows")
1159 1159 if '\\/' in path:
1160 1160 return _("directory name ends with '\\', which is invalid on Windows")
1161 1161 for n in path.replace('\\', '/').split('/'):
1162 1162 if not n:
1163 1163 continue
1164 1164 for c in n:
1165 1165 if c in _winreservedchars:
1166 1166 return _("filename contains '%s', which is reserved "
1167 1167 "on Windows") % c
1168 1168 if ord(c) <= 31:
1169 1169 return _("filename contains %r, which is invalid "
1170 1170 "on Windows") % c
1171 1171 base = n.split('.')[0]
1172 1172 if base and base.lower() in _winreservednames:
1173 1173 return _("filename contains '%s', which is reserved "
1174 1174 "on Windows") % base
1175 1175 t = n[-1]
1176 1176 if t in '. ' and n not in '..':
1177 1177 return _("filename ends with '%s', which is not allowed "
1178 1178 "on Windows") % t
1179 1179
1180 1180 if os.name == 'nt':
1181 1181 checkosfilename = checkwinfilename
1182 1182 else:
1183 1183 checkosfilename = platform.checkosfilename
1184 1184
1185 1185 def makelock(info, pathname):
1186 1186 try:
1187 1187 return os.symlink(info, pathname)
1188 1188 except OSError as why:
1189 1189 if why.errno == errno.EEXIST:
1190 1190 raise
1191 1191 except AttributeError: # no symlink in os
1192 1192 pass
1193 1193
1194 1194 ld = os.open(pathname, os.O_CREAT | os.O_WRONLY | os.O_EXCL)
1195 1195 os.write(ld, info)
1196 1196 os.close(ld)
1197 1197
1198 1198 def readlock(pathname):
1199 1199 try:
1200 1200 return os.readlink(pathname)
1201 1201 except OSError as why:
1202 1202 if why.errno not in (errno.EINVAL, errno.ENOSYS):
1203 1203 raise
1204 1204 except AttributeError: # no symlink in os
1205 1205 pass
1206 1206 fp = posixfile(pathname)
1207 1207 r = fp.read()
1208 1208 fp.close()
1209 1209 return r
1210 1210
1211 1211 def fstat(fp):
1212 1212 '''stat file object that may not have fileno method.'''
1213 1213 try:
1214 1214 return os.fstat(fp.fileno())
1215 1215 except AttributeError:
1216 1216 return os.stat(fp.name)
1217 1217
1218 1218 # File system features
1219 1219
1220 1220 def fscasesensitive(path):
1221 1221 """
1222 1222 Return true if the given path is on a case-sensitive filesystem
1223 1223
1224 1224 Requires a path (like /foo/.hg) ending with a foldable final
1225 1225 directory component.
1226 1226 """
1227 1227 s1 = os.lstat(path)
1228 1228 d, b = os.path.split(path)
1229 1229 b2 = b.upper()
1230 1230 if b == b2:
1231 1231 b2 = b.lower()
1232 1232 if b == b2:
1233 1233 return True # no evidence against case sensitivity
1234 1234 p2 = os.path.join(d, b2)
1235 1235 try:
1236 1236 s2 = os.lstat(p2)
1237 1237 if s2 == s1:
1238 1238 return False
1239 1239 return True
1240 1240 except OSError:
1241 1241 return True
1242 1242
1243 1243 try:
1244 1244 import re2
1245 1245 _re2 = None
1246 1246 except ImportError:
1247 1247 _re2 = False
1248 1248
1249 1249 class _re(object):
1250 1250 def _checkre2(self):
1251 1251 global _re2
1252 1252 try:
1253 1253 # check if match works, see issue3964
1254 1254 _re2 = bool(re2.match(r'\[([^\[]+)\]', '[ui]'))
1255 1255 except ImportError:
1256 1256 _re2 = False
1257 1257
1258 1258 def compile(self, pat, flags=0):
1259 1259 '''Compile a regular expression, using re2 if possible
1260 1260
1261 1261 For best performance, use only re2-compatible regexp features. The
1262 1262 only flags from the re module that are re2-compatible are
1263 1263 IGNORECASE and MULTILINE.'''
1264 1264 if _re2 is None:
1265 1265 self._checkre2()
1266 1266 if _re2 and (flags & ~(remod.IGNORECASE | remod.MULTILINE)) == 0:
1267 1267 if flags & remod.IGNORECASE:
1268 1268 pat = '(?i)' + pat
1269 1269 if flags & remod.MULTILINE:
1270 1270 pat = '(?m)' + pat
1271 1271 try:
1272 1272 return re2.compile(pat)
1273 1273 except re2.error:
1274 1274 pass
1275 1275 return remod.compile(pat, flags)
1276 1276
1277 1277 @propertycache
1278 1278 def escape(self):
1279 1279 '''Return the version of escape corresponding to self.compile.
1280 1280
1281 1281 This is imperfect because whether re2 or re is used for a particular
1282 1282 function depends on the flags, etc, but it's the best we can do.
1283 1283 '''
1284 1284 global _re2
1285 1285 if _re2 is None:
1286 1286 self._checkre2()
1287 1287 if _re2:
1288 1288 return re2.escape
1289 1289 else:
1290 1290 return remod.escape
1291 1291
1292 1292 re = _re()
1293 1293
1294 1294 _fspathcache = {}
1295 1295 def fspath(name, root):
1296 1296 '''Get name in the case stored in the filesystem
1297 1297
1298 1298 The name should be relative to root, and be normcase-ed for efficiency.
1299 1299
1300 1300 Note that this function is unnecessary, and should not be
1301 1301 called, for case-sensitive filesystems (simply because it's expensive).
1302 1302
1303 1303 The root should be normcase-ed, too.
1304 1304 '''
1305 1305 def _makefspathcacheentry(dir):
1306 1306 return dict((normcase(n), n) for n in os.listdir(dir))
1307 1307
1308 1308 seps = os.sep
1309 1309 if os.altsep:
1310 1310 seps = seps + os.altsep
1311 1311 # Protect backslashes. This gets silly very quickly.
1312 1312 seps.replace('\\','\\\\')
1313 1313 pattern = remod.compile(r'([^%s]+)|([%s]+)' % (seps, seps))
1314 1314 dir = os.path.normpath(root)
1315 1315 result = []
1316 1316 for part, sep in pattern.findall(name):
1317 1317 if sep:
1318 1318 result.append(sep)
1319 1319 continue
1320 1320
1321 1321 if dir not in _fspathcache:
1322 1322 _fspathcache[dir] = _makefspathcacheentry(dir)
1323 1323 contents = _fspathcache[dir]
1324 1324
1325 1325 found = contents.get(part)
1326 1326 if not found:
1327 1327 # retry "once per directory" per "dirstate.walk" which
1328 1328 # may take place for each patches of "hg qpush", for example
1329 1329 _fspathcache[dir] = contents = _makefspathcacheentry(dir)
1330 1330 found = contents.get(part)
1331 1331
1332 1332 result.append(found or part)
1333 1333 dir = os.path.join(dir, part)
1334 1334
1335 1335 return ''.join(result)
1336 1336
1337 1337 def checknlink(testfile):
1338 1338 '''check whether hardlink count reporting works properly'''
1339 1339
1340 1340 # testfile may be open, so we need a separate file for checking to
1341 1341 # work around issue2543 (or testfile may get lost on Samba shares)
1342 1342 f1 = testfile + ".hgtmp1"
1343 1343 if os.path.lexists(f1):
1344 1344 return False
1345 1345 try:
1346 1346 posixfile(f1, 'w').close()
1347 1347 except IOError:
1348 1348 try:
1349 1349 os.unlink(f1)
1350 1350 except OSError:
1351 1351 pass
1352 1352 return False
1353 1353
1354 1354 f2 = testfile + ".hgtmp2"
1355 1355 fd = None
1356 1356 try:
1357 1357 oslink(f1, f2)
1358 1358 # nlinks() may behave differently for files on Windows shares if
1359 1359 # the file is open.
1360 1360 fd = posixfile(f2)
1361 1361 return nlinks(f2) > 1
1362 1362 except OSError:
1363 1363 return False
1364 1364 finally:
1365 1365 if fd is not None:
1366 1366 fd.close()
1367 1367 for f in (f1, f2):
1368 1368 try:
1369 1369 os.unlink(f)
1370 1370 except OSError:
1371 1371 pass
1372 1372
1373 1373 def endswithsep(path):
1374 1374 '''Check path ends with os.sep or os.altsep.'''
1375 1375 return path.endswith(os.sep) or os.altsep and path.endswith(os.altsep)
1376 1376
1377 1377 def splitpath(path):
1378 1378 '''Split path by os.sep.
1379 1379 Note that this function does not use os.altsep because this is
1380 1380 an alternative of simple "xxx.split(os.sep)".
1381 1381 It is recommended to use os.path.normpath() before using this
1382 1382 function if need.'''
1383 1383 return path.split(os.sep)
1384 1384
1385 1385 def gui():
1386 1386 '''Are we running in a GUI?'''
1387 1387 if sys.platform == 'darwin':
1388 1388 if 'SSH_CONNECTION' in os.environ:
1389 1389 # handle SSH access to a box where the user is logged in
1390 1390 return False
1391 1391 elif getattr(osutil, 'isgui', None):
1392 1392 # check if a CoreGraphics session is available
1393 1393 return osutil.isgui()
1394 1394 else:
1395 1395 # pure build; use a safe default
1396 1396 return True
1397 1397 else:
1398 1398 return os.name == "nt" or os.environ.get("DISPLAY")
1399 1399
1400 1400 def mktempcopy(name, emptyok=False, createmode=None):
1401 1401 """Create a temporary file with the same contents from name
1402 1402
1403 1403 The permission bits are copied from the original file.
1404 1404
1405 1405 If the temporary file is going to be truncated immediately, you
1406 1406 can use emptyok=True as an optimization.
1407 1407
1408 1408 Returns the name of the temporary file.
1409 1409 """
1410 1410 d, fn = os.path.split(name)
1411 1411 fd, temp = tempfile.mkstemp(prefix='.%s-' % fn, dir=d)
1412 1412 os.close(fd)
1413 1413 # Temporary files are created with mode 0600, which is usually not
1414 1414 # what we want. If the original file already exists, just copy
1415 1415 # its mode. Otherwise, manually obey umask.
1416 1416 copymode(name, temp, createmode)
1417 1417 if emptyok:
1418 1418 return temp
1419 1419 try:
1420 1420 try:
1421 1421 ifp = posixfile(name, "rb")
1422 1422 except IOError as inst:
1423 1423 if inst.errno == errno.ENOENT:
1424 1424 return temp
1425 1425 if not getattr(inst, 'filename', None):
1426 1426 inst.filename = name
1427 1427 raise
1428 1428 ofp = posixfile(temp, "wb")
1429 1429 for chunk in filechunkiter(ifp):
1430 1430 ofp.write(chunk)
1431 1431 ifp.close()
1432 1432 ofp.close()
1433 1433 except: # re-raises
1434 1434 try: os.unlink(temp)
1435 1435 except OSError: pass
1436 1436 raise
1437 1437 return temp
1438 1438
1439 1439 class filestat(object):
1440 1440 """help to exactly detect change of a file
1441 1441
1442 1442 'stat' attribute is result of 'os.stat()' if specified 'path'
1443 1443 exists. Otherwise, it is None. This can avoid preparative
1444 1444 'exists()' examination on client side of this class.
1445 1445 """
1446 1446 def __init__(self, path):
1447 1447 try:
1448 1448 self.stat = os.stat(path)
1449 1449 except OSError as err:
1450 1450 if err.errno != errno.ENOENT:
1451 1451 raise
1452 1452 self.stat = None
1453 1453
1454 1454 __hash__ = object.__hash__
1455 1455
1456 1456 def __eq__(self, old):
1457 1457 try:
1458 1458 # if ambiguity between stat of new and old file is
1459 1459 # avoided, comparison of size, ctime and mtime is enough
1460 1460 # to exactly detect change of a file regardless of platform
1461 1461 return (self.stat.st_size == old.stat.st_size and
1462 1462 self.stat.st_ctime == old.stat.st_ctime and
1463 1463 self.stat.st_mtime == old.stat.st_mtime)
1464 1464 except AttributeError:
1465 1465 return False
1466 1466
1467 1467 def isambig(self, old):
1468 1468 """Examine whether new (= self) stat is ambiguous against old one
1469 1469
1470 1470 "S[N]" below means stat of a file at N-th change:
1471 1471
1472 1472 - S[n-1].ctime < S[n].ctime: can detect change of a file
1473 1473 - S[n-1].ctime == S[n].ctime
1474 1474 - S[n-1].ctime < S[n].mtime: means natural advancing (*1)
1475 1475 - S[n-1].ctime == S[n].mtime: is ambiguous (*2)
1476 1476 - S[n-1].ctime > S[n].mtime: never occurs naturally (don't care)
1477 1477 - S[n-1].ctime > S[n].ctime: never occurs naturally (don't care)
1478 1478
1479 1479 Case (*2) above means that a file was changed twice or more at
1480 1480 same time in sec (= S[n-1].ctime), and comparison of timestamp
1481 1481 is ambiguous.
1482 1482
1483 1483 Base idea to avoid such ambiguity is "advance mtime 1 sec, if
1484 1484 timestamp is ambiguous".
1485 1485
1486 1486 But advancing mtime only in case (*2) doesn't work as
1487 1487 expected, because naturally advanced S[n].mtime in case (*1)
1488 1488 might be equal to manually advanced S[n-1 or earlier].mtime.
1489 1489
1490 1490 Therefore, all "S[n-1].ctime == S[n].ctime" cases should be
1491 1491 treated as ambiguous regardless of mtime, to avoid overlooking
1492 1492 by confliction between such mtime.
1493 1493
1494 1494 Advancing mtime "if isambig(oldstat)" ensures "S[n-1].mtime !=
1495 1495 S[n].mtime", even if size of a file isn't changed.
1496 1496 """
1497 1497 try:
1498 1498 return (self.stat.st_ctime == old.stat.st_ctime)
1499 1499 except AttributeError:
1500 1500 return False
1501 1501
1502 1502 def __ne__(self, other):
1503 1503 return not self == other
1504 1504
1505 1505 class atomictempfile(object):
1506 1506 '''writable file object that atomically updates a file
1507 1507
1508 1508 All writes will go to a temporary copy of the original file. Call
1509 1509 close() when you are done writing, and atomictempfile will rename
1510 1510 the temporary copy to the original name, making the changes
1511 1511 visible. If the object is destroyed without being closed, all your
1512 1512 writes are discarded.
1513 1513
1514 1514 checkambig argument of constructor is used with filestat, and is
1515 1515 useful only if target file is guarded by any lock (e.g. repo.lock
1516 1516 or repo.wlock).
1517 1517 '''
1518 1518 def __init__(self, name, mode='w+b', createmode=None, checkambig=False):
1519 1519 self.__name = name # permanent name
1520 1520 self._tempname = mktempcopy(name, emptyok=('w' in mode),
1521 1521 createmode=createmode)
1522 1522 self._fp = posixfile(self._tempname, mode)
1523 1523 self._checkambig = checkambig
1524 1524
1525 1525 # delegated methods
1526 1526 self.read = self._fp.read
1527 1527 self.write = self._fp.write
1528 1528 self.seek = self._fp.seek
1529 1529 self.tell = self._fp.tell
1530 1530 self.fileno = self._fp.fileno
1531 1531
1532 1532 def close(self):
1533 1533 if not self._fp.closed:
1534 1534 self._fp.close()
1535 1535 filename = localpath(self.__name)
1536 1536 oldstat = self._checkambig and filestat(filename)
1537 1537 if oldstat and oldstat.stat:
1538 1538 rename(self._tempname, filename)
1539 1539 newstat = filestat(filename)
1540 1540 if newstat.isambig(oldstat):
1541 1541 # stat of changed file is ambiguous to original one
1542 1542 advanced = (oldstat.stat.st_mtime + 1) & 0x7fffffff
1543 1543 os.utime(filename, (advanced, advanced))
1544 1544 else:
1545 1545 rename(self._tempname, filename)
1546 1546
1547 1547 def discard(self):
1548 1548 if not self._fp.closed:
1549 1549 try:
1550 1550 os.unlink(self._tempname)
1551 1551 except OSError:
1552 1552 pass
1553 1553 self._fp.close()
1554 1554
1555 1555 def __del__(self):
1556 1556 if safehasattr(self, '_fp'): # constructor actually did something
1557 1557 self.discard()
1558 1558
1559 1559 def __enter__(self):
1560 1560 return self
1561 1561
1562 1562 def __exit__(self, exctype, excvalue, traceback):
1563 1563 if exctype is not None:
1564 1564 self.discard()
1565 1565 else:
1566 1566 self.close()
1567 1567
1568 1568 def makedirs(name, mode=None, notindexed=False):
1569 1569 """recursive directory creation with parent mode inheritance
1570 1570
1571 1571 Newly created directories are marked as "not to be indexed by
1572 1572 the content indexing service", if ``notindexed`` is specified
1573 1573 for "write" mode access.
1574 1574 """
1575 1575 try:
1576 1576 makedir(name, notindexed)
1577 1577 except OSError as err:
1578 1578 if err.errno == errno.EEXIST:
1579 1579 return
1580 1580 if err.errno != errno.ENOENT or not name:
1581 1581 raise
1582 1582 parent = os.path.dirname(os.path.abspath(name))
1583 1583 if parent == name:
1584 1584 raise
1585 1585 makedirs(parent, mode, notindexed)
1586 1586 try:
1587 1587 makedir(name, notindexed)
1588 1588 except OSError as err:
1589 1589 # Catch EEXIST to handle races
1590 1590 if err.errno == errno.EEXIST:
1591 1591 return
1592 1592 raise
1593 1593 if mode is not None:
1594 1594 os.chmod(name, mode)
1595 1595
1596 1596 def readfile(path):
1597 1597 with open(path, 'rb') as fp:
1598 1598 return fp.read()
1599 1599
1600 1600 def writefile(path, text):
1601 1601 with open(path, 'wb') as fp:
1602 1602 fp.write(text)
1603 1603
1604 1604 def appendfile(path, text):
1605 1605 with open(path, 'ab') as fp:
1606 1606 fp.write(text)
1607 1607
1608 1608 class chunkbuffer(object):
1609 1609 """Allow arbitrary sized chunks of data to be efficiently read from an
1610 1610 iterator over chunks of arbitrary size."""
1611 1611
1612 1612 def __init__(self, in_iter):
1613 1613 """in_iter is the iterator that's iterating over the input chunks.
1614 1614 targetsize is how big a buffer to try to maintain."""
1615 1615 def splitbig(chunks):
1616 1616 for chunk in chunks:
1617 1617 if len(chunk) > 2**20:
1618 1618 pos = 0
1619 1619 while pos < len(chunk):
1620 1620 end = pos + 2 ** 18
1621 1621 yield chunk[pos:end]
1622 1622 pos = end
1623 1623 else:
1624 1624 yield chunk
1625 1625 self.iter = splitbig(in_iter)
1626 1626 self._queue = collections.deque()
1627 1627 self._chunkoffset = 0
1628 1628
1629 1629 def read(self, l=None):
1630 1630 """Read L bytes of data from the iterator of chunks of data.
1631 1631 Returns less than L bytes if the iterator runs dry.
1632 1632
1633 1633 If size parameter is omitted, read everything"""
1634 1634 if l is None:
1635 1635 return ''.join(self.iter)
1636 1636
1637 1637 left = l
1638 1638 buf = []
1639 1639 queue = self._queue
1640 1640 while left > 0:
1641 1641 # refill the queue
1642 1642 if not queue:
1643 1643 target = 2**18
1644 1644 for chunk in self.iter:
1645 1645 queue.append(chunk)
1646 1646 target -= len(chunk)
1647 1647 if target <= 0:
1648 1648 break
1649 1649 if not queue:
1650 1650 break
1651 1651
1652 1652 # The easy way to do this would be to queue.popleft(), modify the
1653 1653 # chunk (if necessary), then queue.appendleft(). However, for cases
1654 1654 # where we read partial chunk content, this incurs 2 dequeue
1655 1655 # mutations and creates a new str for the remaining chunk in the
1656 1656 # queue. Our code below avoids this overhead.
1657 1657
1658 1658 chunk = queue[0]
1659 1659 chunkl = len(chunk)
1660 1660 offset = self._chunkoffset
1661 1661
1662 1662 # Use full chunk.
1663 1663 if offset == 0 and left >= chunkl:
1664 1664 left -= chunkl
1665 1665 queue.popleft()
1666 1666 buf.append(chunk)
1667 1667 # self._chunkoffset remains at 0.
1668 1668 continue
1669 1669
1670 1670 chunkremaining = chunkl - offset
1671 1671
1672 1672 # Use all of unconsumed part of chunk.
1673 1673 if left >= chunkremaining:
1674 1674 left -= chunkremaining
1675 1675 queue.popleft()
1676 1676 # offset == 0 is enabled by block above, so this won't merely
1677 1677 # copy via ``chunk[0:]``.
1678 1678 buf.append(chunk[offset:])
1679 1679 self._chunkoffset = 0
1680 1680
1681 1681 # Partial chunk needed.
1682 1682 else:
1683 1683 buf.append(chunk[offset:offset + left])
1684 1684 self._chunkoffset += left
1685 1685 left -= chunkremaining
1686 1686
1687 1687 return ''.join(buf)
1688 1688
1689 1689 def filechunkiter(f, size=131072, limit=None):
1690 1690 """Create a generator that produces the data in the file size
1691 1691 (default 131072) bytes at a time, up to optional limit (default is
1692 1692 to read all data). Chunks may be less than size bytes if the
1693 1693 chunk is the last chunk in the file, or the file is a socket or
1694 1694 some other type of file that sometimes reads less data than is
1695 1695 requested."""
1696 1696 assert size >= 0
1697 1697 assert limit is None or limit >= 0
1698 1698 while True:
1699 1699 if limit is None:
1700 1700 nbytes = size
1701 1701 else:
1702 1702 nbytes = min(limit, size)
1703 1703 s = nbytes and f.read(nbytes)
1704 1704 if not s:
1705 1705 break
1706 1706 if limit:
1707 1707 limit -= len(s)
1708 1708 yield s
1709 1709
1710 1710 def makedate(timestamp=None):
1711 1711 '''Return a unix timestamp (or the current time) as a (unixtime,
1712 1712 offset) tuple based off the local timezone.'''
1713 1713 if timestamp is None:
1714 1714 timestamp = time.time()
1715 1715 if timestamp < 0:
1716 1716 hint = _("check your clock")
1717 1717 raise Abort(_("negative timestamp: %d") % timestamp, hint=hint)
1718 1718 delta = (datetime.datetime.utcfromtimestamp(timestamp) -
1719 1719 datetime.datetime.fromtimestamp(timestamp))
1720 1720 tz = delta.days * 86400 + delta.seconds
1721 1721 return timestamp, tz
1722 1722
1723 1723 def datestr(date=None, format='%a %b %d %H:%M:%S %Y %1%2'):
1724 1724 """represent a (unixtime, offset) tuple as a localized time.
1725 1725 unixtime is seconds since the epoch, and offset is the time zone's
1726 1726 number of seconds away from UTC.
1727 1727
1728 1728 >>> datestr((0, 0))
1729 1729 'Thu Jan 01 00:00:00 1970 +0000'
1730 1730 >>> datestr((42, 0))
1731 1731 'Thu Jan 01 00:00:42 1970 +0000'
1732 1732 >>> datestr((-42, 0))
1733 1733 'Wed Dec 31 23:59:18 1969 +0000'
1734 1734 >>> datestr((0x7fffffff, 0))
1735 1735 'Tue Jan 19 03:14:07 2038 +0000'
1736 1736 >>> datestr((-0x80000000, 0))
1737 1737 'Fri Dec 13 20:45:52 1901 +0000'
1738 1738 """
1739 1739 t, tz = date or makedate()
1740 1740 if "%1" in format or "%2" in format or "%z" in format:
1741 1741 sign = (tz > 0) and "-" or "+"
1742 1742 minutes = abs(tz) // 60
1743 1743 q, r = divmod(minutes, 60)
1744 1744 format = format.replace("%z", "%1%2")
1745 1745 format = format.replace("%1", "%c%02d" % (sign, q))
1746 1746 format = format.replace("%2", "%02d" % r)
1747 1747 d = t - tz
1748 1748 if d > 0x7fffffff:
1749 1749 d = 0x7fffffff
1750 1750 elif d < -0x80000000:
1751 1751 d = -0x80000000
1752 1752 # Never use time.gmtime() and datetime.datetime.fromtimestamp()
1753 1753 # because they use the gmtime() system call which is buggy on Windows
1754 1754 # for negative values.
1755 1755 t = datetime.datetime(1970, 1, 1) + datetime.timedelta(seconds=d)
1756 1756 s = t.strftime(format)
1757 1757 return s
1758 1758
1759 1759 def shortdate(date=None):
1760 1760 """turn (timestamp, tzoff) tuple into iso 8631 date."""
1761 1761 return datestr(date, format='%Y-%m-%d')
1762 1762
1763 1763 def parsetimezone(s):
1764 1764 """find a trailing timezone, if any, in string, and return a
1765 1765 (offset, remainder) pair"""
1766 1766
1767 1767 if s.endswith("GMT") or s.endswith("UTC"):
1768 1768 return 0, s[:-3].rstrip()
1769 1769
1770 1770 # Unix-style timezones [+-]hhmm
1771 1771 if len(s) >= 5 and s[-5] in "+-" and s[-4:].isdigit():
1772 1772 sign = (s[-5] == "+") and 1 or -1
1773 1773 hours = int(s[-4:-2])
1774 1774 minutes = int(s[-2:])
1775 1775 return -sign * (hours * 60 + minutes) * 60, s[:-5].rstrip()
1776 1776
1777 1777 # ISO8601 trailing Z
1778 1778 if s.endswith("Z") and s[-2:-1].isdigit():
1779 1779 return 0, s[:-1]
1780 1780
1781 1781 # ISO8601-style [+-]hh:mm
1782 1782 if (len(s) >= 6 and s[-6] in "+-" and s[-3] == ":" and
1783 1783 s[-5:-3].isdigit() and s[-2:].isdigit()):
1784 1784 sign = (s[-6] == "+") and 1 or -1
1785 1785 hours = int(s[-5:-3])
1786 1786 minutes = int(s[-2:])
1787 1787 return -sign * (hours * 60 + minutes) * 60, s[:-6]
1788 1788
1789 1789 return None, s
1790 1790
1791 1791 def strdate(string, format, defaults=[]):
1792 1792 """parse a localized time string and return a (unixtime, offset) tuple.
1793 1793 if the string cannot be parsed, ValueError is raised."""
1794 1794 # NOTE: unixtime = localunixtime + offset
1795 1795 offset, date = parsetimezone(string)
1796 1796
1797 1797 # add missing elements from defaults
1798 1798 usenow = False # default to using biased defaults
1799 1799 for part in ("S", "M", "HI", "d", "mb", "yY"): # decreasing specificity
1800 1800 found = [True for p in part if ("%"+p) in format]
1801 1801 if not found:
1802 1802 date += "@" + defaults[part][usenow]
1803 1803 format += "@%" + part[0]
1804 1804 else:
1805 1805 # We've found a specific time element, less specific time
1806 1806 # elements are relative to today
1807 1807 usenow = True
1808 1808
1809 1809 timetuple = time.strptime(date, format)
1810 1810 localunixtime = int(calendar.timegm(timetuple))
1811 1811 if offset is None:
1812 1812 # local timezone
1813 1813 unixtime = int(time.mktime(timetuple))
1814 1814 offset = unixtime - localunixtime
1815 1815 else:
1816 1816 unixtime = localunixtime + offset
1817 1817 return unixtime, offset
1818 1818
1819 1819 def parsedate(date, formats=None, bias=None):
1820 1820 """parse a localized date/time and return a (unixtime, offset) tuple.
1821 1821
1822 1822 The date may be a "unixtime offset" string or in one of the specified
1823 1823 formats. If the date already is a (unixtime, offset) tuple, it is returned.
1824 1824
1825 1825 >>> parsedate(' today ') == parsedate(\
1826 1826 datetime.date.today().strftime('%b %d'))
1827 1827 True
1828 1828 >>> parsedate( 'yesterday ') == parsedate((datetime.date.today() -\
1829 1829 datetime.timedelta(days=1)\
1830 1830 ).strftime('%b %d'))
1831 1831 True
1832 1832 >>> now, tz = makedate()
1833 1833 >>> strnow, strtz = parsedate('now')
1834 1834 >>> (strnow - now) < 1
1835 1835 True
1836 1836 >>> tz == strtz
1837 1837 True
1838 1838 """
1839 1839 if bias is None:
1840 1840 bias = {}
1841 1841 if not date:
1842 1842 return 0, 0
1843 1843 if isinstance(date, tuple) and len(date) == 2:
1844 1844 return date
1845 1845 if not formats:
1846 1846 formats = defaultdateformats
1847 1847 date = date.strip()
1848 1848
1849 1849 if date == 'now' or date == _('now'):
1850 1850 return makedate()
1851 1851 if date == 'today' or date == _('today'):
1852 1852 date = datetime.date.today().strftime('%b %d')
1853 1853 elif date == 'yesterday' or date == _('yesterday'):
1854 1854 date = (datetime.date.today() -
1855 1855 datetime.timedelta(days=1)).strftime('%b %d')
1856 1856
1857 1857 try:
1858 1858 when, offset = map(int, date.split(' '))
1859 1859 except ValueError:
1860 1860 # fill out defaults
1861 1861 now = makedate()
1862 1862 defaults = {}
1863 1863 for part in ("d", "mb", "yY", "HI", "M", "S"):
1864 1864 # this piece is for rounding the specific end of unknowns
1865 1865 b = bias.get(part)
1866 1866 if b is None:
1867 1867 if part[0] in "HMS":
1868 1868 b = "00"
1869 1869 else:
1870 1870 b = "0"
1871 1871
1872 1872 # this piece is for matching the generic end to today's date
1873 1873 n = datestr(now, "%" + part[0])
1874 1874
1875 1875 defaults[part] = (b, n)
1876 1876
1877 1877 for format in formats:
1878 1878 try:
1879 1879 when, offset = strdate(date, format, defaults)
1880 1880 except (ValueError, OverflowError):
1881 1881 pass
1882 1882 else:
1883 1883 break
1884 1884 else:
1885 1885 raise Abort(_('invalid date: %r') % date)
1886 1886 # validate explicit (probably user-specified) date and
1887 1887 # time zone offset. values must fit in signed 32 bits for
1888 1888 # current 32-bit linux runtimes. timezones go from UTC-12
1889 1889 # to UTC+14
1890 1890 if when < -0x80000000 or when > 0x7fffffff:
1891 1891 raise Abort(_('date exceeds 32 bits: %d') % when)
1892 1892 if offset < -50400 or offset > 43200:
1893 1893 raise Abort(_('impossible time zone offset: %d') % offset)
1894 1894 return when, offset
1895 1895
1896 1896 def matchdate(date):
1897 1897 """Return a function that matches a given date match specifier
1898 1898
1899 1899 Formats include:
1900 1900
1901 1901 '{date}' match a given date to the accuracy provided
1902 1902
1903 1903 '<{date}' on or before a given date
1904 1904
1905 1905 '>{date}' on or after a given date
1906 1906
1907 1907 >>> p1 = parsedate("10:29:59")
1908 1908 >>> p2 = parsedate("10:30:00")
1909 1909 >>> p3 = parsedate("10:30:59")
1910 1910 >>> p4 = parsedate("10:31:00")
1911 1911 >>> p5 = parsedate("Sep 15 10:30:00 1999")
1912 1912 >>> f = matchdate("10:30")
1913 1913 >>> f(p1[0])
1914 1914 False
1915 1915 >>> f(p2[0])
1916 1916 True
1917 1917 >>> f(p3[0])
1918 1918 True
1919 1919 >>> f(p4[0])
1920 1920 False
1921 1921 >>> f(p5[0])
1922 1922 False
1923 1923 """
1924 1924
1925 1925 def lower(date):
1926 1926 d = {'mb': "1", 'd': "1"}
1927 1927 return parsedate(date, extendeddateformats, d)[0]
1928 1928
1929 1929 def upper(date):
1930 1930 d = {'mb': "12", 'HI': "23", 'M': "59", 'S': "59"}
1931 1931 for days in ("31", "30", "29"):
1932 1932 try:
1933 1933 d["d"] = days
1934 1934 return parsedate(date, extendeddateformats, d)[0]
1935 1935 except Abort:
1936 1936 pass
1937 1937 d["d"] = "28"
1938 1938 return parsedate(date, extendeddateformats, d)[0]
1939 1939
1940 1940 date = date.strip()
1941 1941
1942 1942 if not date:
1943 1943 raise Abort(_("dates cannot consist entirely of whitespace"))
1944 1944 elif date[0] == "<":
1945 1945 if not date[1:]:
1946 1946 raise Abort(_("invalid day spec, use '<DATE'"))
1947 1947 when = upper(date[1:])
1948 1948 return lambda x: x <= when
1949 1949 elif date[0] == ">":
1950 1950 if not date[1:]:
1951 1951 raise Abort(_("invalid day spec, use '>DATE'"))
1952 1952 when = lower(date[1:])
1953 1953 return lambda x: x >= when
1954 1954 elif date[0] == "-":
1955 1955 try:
1956 1956 days = int(date[1:])
1957 1957 except ValueError:
1958 1958 raise Abort(_("invalid day spec: %s") % date[1:])
1959 1959 if days < 0:
1960 1960 raise Abort(_("%s must be nonnegative (see 'hg help dates')")
1961 1961 % date[1:])
1962 1962 when = makedate()[0] - days * 3600 * 24
1963 1963 return lambda x: x >= when
1964 1964 elif " to " in date:
1965 1965 a, b = date.split(" to ")
1966 1966 start, stop = lower(a), upper(b)
1967 1967 return lambda x: x >= start and x <= stop
1968 1968 else:
1969 1969 start, stop = lower(date), upper(date)
1970 1970 return lambda x: x >= start and x <= stop
1971 1971
1972 1972 def stringmatcher(pattern):
1973 1973 """
1974 1974 accepts a string, possibly starting with 're:' or 'literal:' prefix.
1975 1975 returns the matcher name, pattern, and matcher function.
1976 1976 missing or unknown prefixes are treated as literal matches.
1977 1977
1978 1978 helper for tests:
1979 1979 >>> def test(pattern, *tests):
1980 1980 ... kind, pattern, matcher = stringmatcher(pattern)
1981 1981 ... return (kind, pattern, [bool(matcher(t)) for t in tests])
1982 1982
1983 1983 exact matching (no prefix):
1984 1984 >>> test('abcdefg', 'abc', 'def', 'abcdefg')
1985 1985 ('literal', 'abcdefg', [False, False, True])
1986 1986
1987 1987 regex matching ('re:' prefix)
1988 1988 >>> test('re:a.+b', 'nomatch', 'fooadef', 'fooadefbar')
1989 1989 ('re', 'a.+b', [False, False, True])
1990 1990
1991 1991 force exact matches ('literal:' prefix)
1992 1992 >>> test('literal:re:foobar', 'foobar', 're:foobar')
1993 1993 ('literal', 're:foobar', [False, True])
1994 1994
1995 1995 unknown prefixes are ignored and treated as literals
1996 1996 >>> test('foo:bar', 'foo', 'bar', 'foo:bar')
1997 1997 ('literal', 'foo:bar', [False, False, True])
1998 1998 """
1999 1999 if pattern.startswith('re:'):
2000 2000 pattern = pattern[3:]
2001 2001 try:
2002 2002 regex = remod.compile(pattern)
2003 2003 except remod.error as e:
2004 2004 raise error.ParseError(_('invalid regular expression: %s')
2005 2005 % e)
2006 2006 return 're', pattern, regex.search
2007 2007 elif pattern.startswith('literal:'):
2008 2008 pattern = pattern[8:]
2009 2009 return 'literal', pattern, pattern.__eq__
2010 2010
2011 2011 def shortuser(user):
2012 2012 """Return a short representation of a user name or email address."""
2013 2013 f = user.find('@')
2014 2014 if f >= 0:
2015 2015 user = user[:f]
2016 2016 f = user.find('<')
2017 2017 if f >= 0:
2018 2018 user = user[f + 1:]
2019 2019 f = user.find(' ')
2020 2020 if f >= 0:
2021 2021 user = user[:f]
2022 2022 f = user.find('.')
2023 2023 if f >= 0:
2024 2024 user = user[:f]
2025 2025 return user
2026 2026
2027 2027 def emailuser(user):
2028 2028 """Return the user portion of an email address."""
2029 2029 f = user.find('@')
2030 2030 if f >= 0:
2031 2031 user = user[:f]
2032 2032 f = user.find('<')
2033 2033 if f >= 0:
2034 2034 user = user[f + 1:]
2035 2035 return user
2036 2036
2037 2037 def email(author):
2038 2038 '''get email of author.'''
2039 2039 r = author.find('>')
2040 2040 if r == -1:
2041 2041 r = None
2042 2042 return author[author.find('<') + 1:r]
2043 2043
2044 2044 def ellipsis(text, maxlength=400):
2045 2045 """Trim string to at most maxlength (default: 400) columns in display."""
2046 2046 return encoding.trim(text, maxlength, ellipsis='...')
2047 2047
2048 2048 def unitcountfn(*unittable):
2049 2049 '''return a function that renders a readable count of some quantity'''
2050 2050
2051 2051 def go(count):
2052 2052 for multiplier, divisor, format in unittable:
2053 2053 if count >= divisor * multiplier:
2054 2054 return format % (count / float(divisor))
2055 2055 return unittable[-1][2] % count
2056 2056
2057 2057 return go
2058 2058
2059 2059 bytecount = unitcountfn(
2060 2060 (100, 1 << 30, _('%.0f GB')),
2061 2061 (10, 1 << 30, _('%.1f GB')),
2062 2062 (1, 1 << 30, _('%.2f GB')),
2063 2063 (100, 1 << 20, _('%.0f MB')),
2064 2064 (10, 1 << 20, _('%.1f MB')),
2065 2065 (1, 1 << 20, _('%.2f MB')),
2066 2066 (100, 1 << 10, _('%.0f KB')),
2067 2067 (10, 1 << 10, _('%.1f KB')),
2068 2068 (1, 1 << 10, _('%.2f KB')),
2069 2069 (1, 1, _('%.0f bytes')),
2070 2070 )
2071 2071
2072 2072 def uirepr(s):
2073 2073 # Avoid double backslash in Windows path repr()
2074 2074 return repr(s).replace('\\\\', '\\')
2075 2075
2076 2076 # delay import of textwrap
2077 2077 def MBTextWrapper(**kwargs):
2078 2078 class tw(textwrap.TextWrapper):
2079 2079 """
2080 2080 Extend TextWrapper for width-awareness.
2081 2081
2082 2082 Neither number of 'bytes' in any encoding nor 'characters' is
2083 2083 appropriate to calculate terminal columns for specified string.
2084 2084
2085 2085 Original TextWrapper implementation uses built-in 'len()' directly,
2086 2086 so overriding is needed to use width information of each characters.
2087 2087
2088 2088 In addition, characters classified into 'ambiguous' width are
2089 2089 treated as wide in East Asian area, but as narrow in other.
2090 2090
2091 2091 This requires use decision to determine width of such characters.
2092 2092 """
2093 2093 def _cutdown(self, ucstr, space_left):
2094 2094 l = 0
2095 2095 colwidth = encoding.ucolwidth
2096 2096 for i in xrange(len(ucstr)):
2097 2097 l += colwidth(ucstr[i])
2098 2098 if space_left < l:
2099 2099 return (ucstr[:i], ucstr[i:])
2100 2100 return ucstr, ''
2101 2101
2102 2102 # overriding of base class
2103 2103 def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width):
2104 2104 space_left = max(width - cur_len, 1)
2105 2105
2106 2106 if self.break_long_words:
2107 2107 cut, res = self._cutdown(reversed_chunks[-1], space_left)
2108 2108 cur_line.append(cut)
2109 2109 reversed_chunks[-1] = res
2110 2110 elif not cur_line:
2111 2111 cur_line.append(reversed_chunks.pop())
2112 2112
2113 2113 # this overriding code is imported from TextWrapper of Python 2.6
2114 2114 # to calculate columns of string by 'encoding.ucolwidth()'
2115 2115 def _wrap_chunks(self, chunks):
2116 2116 colwidth = encoding.ucolwidth
2117 2117
2118 2118 lines = []
2119 2119 if self.width <= 0:
2120 2120 raise ValueError("invalid width %r (must be > 0)" % self.width)
2121 2121
2122 2122 # Arrange in reverse order so items can be efficiently popped
2123 2123 # from a stack of chucks.
2124 2124 chunks.reverse()
2125 2125
2126 2126 while chunks:
2127 2127
2128 2128 # Start the list of chunks that will make up the current line.
2129 2129 # cur_len is just the length of all the chunks in cur_line.
2130 2130 cur_line = []
2131 2131 cur_len = 0
2132 2132
2133 2133 # Figure out which static string will prefix this line.
2134 2134 if lines:
2135 2135 indent = self.subsequent_indent
2136 2136 else:
2137 2137 indent = self.initial_indent
2138 2138
2139 2139 # Maximum width for this line.
2140 2140 width = self.width - len(indent)
2141 2141
2142 2142 # First chunk on line is whitespace -- drop it, unless this
2143 2143 # is the very beginning of the text (i.e. no lines started yet).
2144 2144 if self.drop_whitespace and chunks[-1].strip() == '' and lines:
2145 2145 del chunks[-1]
2146 2146
2147 2147 while chunks:
2148 2148 l = colwidth(chunks[-1])
2149 2149
2150 2150 # Can at least squeeze this chunk onto the current line.
2151 2151 if cur_len + l <= width:
2152 2152 cur_line.append(chunks.pop())
2153 2153 cur_len += l
2154 2154
2155 2155 # Nope, this line is full.
2156 2156 else:
2157 2157 break
2158 2158
2159 2159 # The current line is full, and the next chunk is too big to
2160 2160 # fit on *any* line (not just this one).
2161 2161 if chunks and colwidth(chunks[-1]) > width:
2162 2162 self._handle_long_word(chunks, cur_line, cur_len, width)
2163 2163
2164 2164 # If the last chunk on this line is all whitespace, drop it.
2165 2165 if (self.drop_whitespace and
2166 2166 cur_line and cur_line[-1].strip() == ''):
2167 2167 del cur_line[-1]
2168 2168
2169 2169 # Convert current line back to a string and store it in list
2170 2170 # of all lines (return value).
2171 2171 if cur_line:
2172 2172 lines.append(indent + ''.join(cur_line))
2173 2173
2174 2174 return lines
2175 2175
2176 2176 global MBTextWrapper
2177 2177 MBTextWrapper = tw
2178 2178 return tw(**kwargs)
2179 2179
2180 2180 def wrap(line, width, initindent='', hangindent=''):
2181 2181 maxindent = max(len(hangindent), len(initindent))
2182 2182 if width <= maxindent:
2183 2183 # adjust for weird terminal size
2184 2184 width = max(78, maxindent + 1)
2185 2185 line = line.decode(encoding.encoding, encoding.encodingmode)
2186 2186 initindent = initindent.decode(encoding.encoding, encoding.encodingmode)
2187 2187 hangindent = hangindent.decode(encoding.encoding, encoding.encodingmode)
2188 2188 wrapper = MBTextWrapper(width=width,
2189 2189 initial_indent=initindent,
2190 2190 subsequent_indent=hangindent)
2191 2191 return wrapper.fill(line).encode(encoding.encoding)
2192 2192
2193 2193 def iterlines(iterator):
2194 2194 for chunk in iterator:
2195 2195 for line in chunk.splitlines():
2196 2196 yield line
2197 2197
2198 2198 def expandpath(path):
2199 2199 return os.path.expanduser(os.path.expandvars(path))
2200 2200
2201 2201 def hgcmd():
2202 2202 """Return the command used to execute current hg
2203 2203
2204 2204 This is different from hgexecutable() because on Windows we want
2205 2205 to avoid things opening new shell windows like batch files, so we
2206 2206 get either the python call or current executable.
2207 2207 """
2208 2208 if mainfrozen():
2209 2209 if getattr(sys, 'frozen', None) == 'macosx_app':
2210 2210 # Env variable set by py2app
2211 2211 return [os.environ['EXECUTABLEPATH']]
2212 2212 else:
2213 2213 return [sys.executable]
2214 2214 return gethgcmd()
2215 2215
2216 2216 def rundetached(args, condfn):
2217 2217 """Execute the argument list in a detached process.
2218 2218
2219 2219 condfn is a callable which is called repeatedly and should return
2220 2220 True once the child process is known to have started successfully.
2221 2221 At this point, the child process PID is returned. If the child
2222 2222 process fails to start or finishes before condfn() evaluates to
2223 2223 True, return -1.
2224 2224 """
2225 2225 # Windows case is easier because the child process is either
2226 2226 # successfully starting and validating the condition or exiting
2227 2227 # on failure. We just poll on its PID. On Unix, if the child
2228 2228 # process fails to start, it will be left in a zombie state until
2229 2229 # the parent wait on it, which we cannot do since we expect a long
2230 2230 # running process on success. Instead we listen for SIGCHLD telling
2231 2231 # us our child process terminated.
2232 2232 terminated = set()
2233 2233 def handler(signum, frame):
2234 2234 terminated.add(os.wait())
2235 2235 prevhandler = None
2236 2236 SIGCHLD = getattr(signal, 'SIGCHLD', None)
2237 2237 if SIGCHLD is not None:
2238 2238 prevhandler = signal.signal(SIGCHLD, handler)
2239 2239 try:
2240 2240 pid = spawndetached(args)
2241 2241 while not condfn():
2242 2242 if ((pid in terminated or not testpid(pid))
2243 2243 and not condfn()):
2244 2244 return -1
2245 2245 time.sleep(0.1)
2246 2246 return pid
2247 2247 finally:
2248 2248 if prevhandler is not None:
2249 2249 signal.signal(signal.SIGCHLD, prevhandler)
2250 2250
2251 2251 def interpolate(prefix, mapping, s, fn=None, escape_prefix=False):
2252 2252 """Return the result of interpolating items in the mapping into string s.
2253 2253
2254 2254 prefix is a single character string, or a two character string with
2255 2255 a backslash as the first character if the prefix needs to be escaped in
2256 2256 a regular expression.
2257 2257
2258 2258 fn is an optional function that will be applied to the replacement text
2259 2259 just before replacement.
2260 2260
2261 2261 escape_prefix is an optional flag that allows using doubled prefix for
2262 2262 its escaping.
2263 2263 """
2264 2264 fn = fn or (lambda s: s)
2265 2265 patterns = '|'.join(mapping.keys())
2266 2266 if escape_prefix:
2267 2267 patterns += '|' + prefix
2268 2268 if len(prefix) > 1:
2269 2269 prefix_char = prefix[1:]
2270 2270 else:
2271 2271 prefix_char = prefix
2272 2272 mapping[prefix_char] = prefix_char
2273 2273 r = remod.compile(r'%s(%s)' % (prefix, patterns))
2274 2274 return r.sub(lambda x: fn(mapping[x.group()[1:]]), s)
2275 2275
2276 2276 def getport(port):
2277 2277 """Return the port for a given network service.
2278 2278
2279 2279 If port is an integer, it's returned as is. If it's a string, it's
2280 2280 looked up using socket.getservbyname(). If there's no matching
2281 2281 service, error.Abort is raised.
2282 2282 """
2283 2283 try:
2284 2284 return int(port)
2285 2285 except ValueError:
2286 2286 pass
2287 2287
2288 2288 try:
2289 2289 return socket.getservbyname(port)
2290 2290 except socket.error:
2291 2291 raise Abort(_("no port number associated with service '%s'") % port)
2292 2292
2293 2293 _booleans = {'1': True, 'yes': True, 'true': True, 'on': True, 'always': True,
2294 2294 '0': False, 'no': False, 'false': False, 'off': False,
2295 2295 'never': False}
2296 2296
2297 2297 def parsebool(s):
2298 2298 """Parse s into a boolean.
2299 2299
2300 2300 If s is not a valid boolean, returns None.
2301 2301 """
2302 2302 return _booleans.get(s.lower(), None)
2303 2303
2304 2304 _hextochr = dict((a + b, chr(int(a + b, 16)))
2305 2305 for a in string.hexdigits for b in string.hexdigits)
2306 2306
2307 2307 class url(object):
2308 2308 r"""Reliable URL parser.
2309 2309
2310 2310 This parses URLs and provides attributes for the following
2311 2311 components:
2312 2312
2313 2313 <scheme>://<user>:<passwd>@<host>:<port>/<path>?<query>#<fragment>
2314 2314
2315 2315 Missing components are set to None. The only exception is
2316 2316 fragment, which is set to '' if present but empty.
2317 2317
2318 2318 If parsefragment is False, fragment is included in query. If
2319 2319 parsequery is False, query is included in path. If both are
2320 2320 False, both fragment and query are included in path.
2321 2321
2322 2322 See http://www.ietf.org/rfc/rfc2396.txt for more information.
2323 2323
2324 2324 Note that for backward compatibility reasons, bundle URLs do not
2325 2325 take host names. That means 'bundle://../' has a path of '../'.
2326 2326
2327 2327 Examples:
2328 2328
2329 2329 >>> url('http://www.ietf.org/rfc/rfc2396.txt')
2330 2330 <url scheme: 'http', host: 'www.ietf.org', path: 'rfc/rfc2396.txt'>
2331 2331 >>> url('ssh://[::1]:2200//home/joe/repo')
2332 2332 <url scheme: 'ssh', host: '[::1]', port: '2200', path: '/home/joe/repo'>
2333 2333 >>> url('file:///home/joe/repo')
2334 2334 <url scheme: 'file', path: '/home/joe/repo'>
2335 2335 >>> url('file:///c:/temp/foo/')
2336 2336 <url scheme: 'file', path: 'c:/temp/foo/'>
2337 2337 >>> url('bundle:foo')
2338 2338 <url scheme: 'bundle', path: 'foo'>
2339 2339 >>> url('bundle://../foo')
2340 2340 <url scheme: 'bundle', path: '../foo'>
2341 2341 >>> url(r'c:\foo\bar')
2342 2342 <url path: 'c:\\foo\\bar'>
2343 2343 >>> url(r'\\blah\blah\blah')
2344 2344 <url path: '\\\\blah\\blah\\blah'>
2345 2345 >>> url(r'\\blah\blah\blah#baz')
2346 2346 <url path: '\\\\blah\\blah\\blah', fragment: 'baz'>
2347 2347 >>> url(r'file:///C:\users\me')
2348 2348 <url scheme: 'file', path: 'C:\\users\\me'>
2349 2349
2350 2350 Authentication credentials:
2351 2351
2352 2352 >>> url('ssh://joe:xyz@x/repo')
2353 2353 <url scheme: 'ssh', user: 'joe', passwd: 'xyz', host: 'x', path: 'repo'>
2354 2354 >>> url('ssh://joe@x/repo')
2355 2355 <url scheme: 'ssh', user: 'joe', host: 'x', path: 'repo'>
2356 2356
2357 2357 Query strings and fragments:
2358 2358
2359 2359 >>> url('http://host/a?b#c')
2360 2360 <url scheme: 'http', host: 'host', path: 'a', query: 'b', fragment: 'c'>
2361 2361 >>> url('http://host/a?b#c', parsequery=False, parsefragment=False)
2362 2362 <url scheme: 'http', host: 'host', path: 'a?b#c'>
2363 2363
2364 2364 Empty path:
2365 2365
2366 2366 >>> url('')
2367 2367 <url path: ''>
2368 2368 >>> url('#a')
2369 2369 <url path: '', fragment: 'a'>
2370 2370 >>> url('http://host/')
2371 2371 <url scheme: 'http', host: 'host', path: ''>
2372 2372 >>> url('http://host/#a')
2373 2373 <url scheme: 'http', host: 'host', path: '', fragment: 'a'>
2374 2374
2375 2375 Only scheme:
2376 2376
2377 2377 >>> url('http:')
2378 2378 <url scheme: 'http'>
2379 2379 """
2380 2380
2381 2381 _safechars = "!~*'()+"
2382 2382 _safepchars = "/!~*'()+:\\"
2383 2383 _matchscheme = remod.compile('^[a-zA-Z0-9+.\\-]+:').match
2384 2384
2385 2385 def __init__(self, path, parsequery=True, parsefragment=True):
2386 2386 # We slowly chomp away at path until we have only the path left
2387 2387 self.scheme = self.user = self.passwd = self.host = None
2388 2388 self.port = self.path = self.query = self.fragment = None
2389 2389 self._localpath = True
2390 2390 self._hostport = ''
2391 2391 self._origpath = path
2392 2392
2393 2393 if parsefragment and '#' in path:
2394 2394 path, self.fragment = path.split('#', 1)
2395 2395
2396 2396 # special case for Windows drive letters and UNC paths
2397 2397 if hasdriveletter(path) or path.startswith('\\\\'):
2398 2398 self.path = path
2399 2399 return
2400 2400
2401 2401 # For compatibility reasons, we can't handle bundle paths as
2402 2402 # normal URLS
2403 2403 if path.startswith('bundle:'):
2404 2404 self.scheme = 'bundle'
2405 2405 path = path[7:]
2406 2406 if path.startswith('//'):
2407 2407 path = path[2:]
2408 2408 self.path = path
2409 2409 return
2410 2410
2411 2411 if self._matchscheme(path):
2412 2412 parts = path.split(':', 1)
2413 2413 if parts[0]:
2414 2414 self.scheme, path = parts
2415 2415 self._localpath = False
2416 2416
2417 2417 if not path:
2418 2418 path = None
2419 2419 if self._localpath:
2420 2420 self.path = ''
2421 2421 return
2422 2422 else:
2423 2423 if self._localpath:
2424 2424 self.path = path
2425 2425 return
2426 2426
2427 2427 if parsequery and '?' in path:
2428 2428 path, self.query = path.split('?', 1)
2429 2429 if not path:
2430 2430 path = None
2431 2431 if not self.query:
2432 2432 self.query = None
2433 2433
2434 2434 # // is required to specify a host/authority
2435 2435 if path and path.startswith('//'):
2436 2436 parts = path[2:].split('/', 1)
2437 2437 if len(parts) > 1:
2438 2438 self.host, path = parts
2439 2439 else:
2440 2440 self.host = parts[0]
2441 2441 path = None
2442 2442 if not self.host:
2443 2443 self.host = None
2444 2444 # path of file:///d is /d
2445 2445 # path of file:///d:/ is d:/, not /d:/
2446 2446 if path and not hasdriveletter(path):
2447 2447 path = '/' + path
2448 2448
2449 2449 if self.host and '@' in self.host:
2450 2450 self.user, self.host = self.host.rsplit('@', 1)
2451 2451 if ':' in self.user:
2452 2452 self.user, self.passwd = self.user.split(':', 1)
2453 2453 if not self.host:
2454 2454 self.host = None
2455 2455
2456 2456 # Don't split on colons in IPv6 addresses without ports
2457 2457 if (self.host and ':' in self.host and
2458 2458 not (self.host.startswith('[') and self.host.endswith(']'))):
2459 2459 self._hostport = self.host
2460 2460 self.host, self.port = self.host.rsplit(':', 1)
2461 2461 if not self.host:
2462 2462 self.host = None
2463 2463
2464 2464 if (self.host and self.scheme == 'file' and
2465 2465 self.host not in ('localhost', '127.0.0.1', '[::1]')):
2466 2466 raise Abort(_('file:// URLs can only refer to localhost'))
2467 2467
2468 2468 self.path = path
2469 2469
2470 2470 # leave the query string escaped
2471 2471 for a in ('user', 'passwd', 'host', 'port',
2472 2472 'path', 'fragment'):
2473 2473 v = getattr(self, a)
2474 2474 if v is not None:
2475 2475 setattr(self, a, pycompat.urlunquote(v))
2476 2476
2477 2477 def __repr__(self):
2478 2478 attrs = []
2479 2479 for a in ('scheme', 'user', 'passwd', 'host', 'port', 'path',
2480 2480 'query', 'fragment'):
2481 2481 v = getattr(self, a)
2482 2482 if v is not None:
2483 2483 attrs.append('%s: %r' % (a, v))
2484 2484 return '<url %s>' % ', '.join(attrs)
2485 2485
2486 2486 def __str__(self):
2487 2487 r"""Join the URL's components back into a URL string.
2488 2488
2489 2489 Examples:
2490 2490
2491 2491 >>> str(url('http://user:pw@host:80/c:/bob?fo:oo#ba:ar'))
2492 2492 'http://user:pw@host:80/c:/bob?fo:oo#ba:ar'
2493 2493 >>> str(url('http://user:pw@host:80/?foo=bar&baz=42'))
2494 2494 'http://user:pw@host:80/?foo=bar&baz=42'
2495 2495 >>> str(url('http://user:pw@host:80/?foo=bar%3dbaz'))
2496 2496 'http://user:pw@host:80/?foo=bar%3dbaz'
2497 2497 >>> str(url('ssh://user:pw@[::1]:2200//home/joe#'))
2498 2498 'ssh://user:pw@[::1]:2200//home/joe#'
2499 2499 >>> str(url('http://localhost:80//'))
2500 2500 'http://localhost:80//'
2501 2501 >>> str(url('http://localhost:80/'))
2502 2502 'http://localhost:80/'
2503 2503 >>> str(url('http://localhost:80'))
2504 2504 'http://localhost:80/'
2505 2505 >>> str(url('bundle:foo'))
2506 2506 'bundle:foo'
2507 2507 >>> str(url('bundle://../foo'))
2508 2508 'bundle:../foo'
2509 2509 >>> str(url('path'))
2510 2510 'path'
2511 2511 >>> str(url('file:///tmp/foo/bar'))
2512 2512 'file:///tmp/foo/bar'
2513 2513 >>> str(url('file:///c:/tmp/foo/bar'))
2514 2514 'file:///c:/tmp/foo/bar'
2515 2515 >>> print url(r'bundle:foo\bar')
2516 2516 bundle:foo\bar
2517 2517 >>> print url(r'file:///D:\data\hg')
2518 2518 file:///D:\data\hg
2519 2519 """
2520 2520 if self._localpath:
2521 2521 s = self.path
2522 2522 if self.scheme == 'bundle':
2523 2523 s = 'bundle:' + s
2524 2524 if self.fragment:
2525 2525 s += '#' + self.fragment
2526 2526 return s
2527 2527
2528 2528 s = self.scheme + ':'
2529 2529 if self.user or self.passwd or self.host:
2530 2530 s += '//'
2531 2531 elif self.scheme and (not self.path or self.path.startswith('/')
2532 2532 or hasdriveletter(self.path)):
2533 2533 s += '//'
2534 2534 if hasdriveletter(self.path):
2535 2535 s += '/'
2536 2536 if self.user:
2537 2537 s += urlreq.quote(self.user, safe=self._safechars)
2538 2538 if self.passwd:
2539 2539 s += ':' + urlreq.quote(self.passwd, safe=self._safechars)
2540 2540 if self.user or self.passwd:
2541 2541 s += '@'
2542 2542 if self.host:
2543 2543 if not (self.host.startswith('[') and self.host.endswith(']')):
2544 2544 s += urlreq.quote(self.host)
2545 2545 else:
2546 2546 s += self.host
2547 2547 if self.port:
2548 2548 s += ':' + urlreq.quote(self.port)
2549 2549 if self.host:
2550 2550 s += '/'
2551 2551 if self.path:
2552 2552 # TODO: similar to the query string, we should not unescape the
2553 2553 # path when we store it, the path might contain '%2f' = '/',
2554 2554 # which we should *not* escape.
2555 2555 s += urlreq.quote(self.path, safe=self._safepchars)
2556 2556 if self.query:
2557 2557 # we store the query in escaped form.
2558 2558 s += '?' + self.query
2559 2559 if self.fragment is not None:
2560 2560 s += '#' + urlreq.quote(self.fragment, safe=self._safepchars)
2561 2561 return s
2562 2562
2563 2563 def authinfo(self):
2564 2564 user, passwd = self.user, self.passwd
2565 2565 try:
2566 2566 self.user, self.passwd = None, None
2567 2567 s = str(self)
2568 2568 finally:
2569 2569 self.user, self.passwd = user, passwd
2570 2570 if not self.user:
2571 2571 return (s, None)
2572 2572 # authinfo[1] is passed to urllib2 password manager, and its
2573 2573 # URIs must not contain credentials. The host is passed in the
2574 2574 # URIs list because Python < 2.4.3 uses only that to search for
2575 2575 # a password.
2576 2576 return (s, (None, (s, self.host),
2577 2577 self.user, self.passwd or ''))
2578 2578
2579 2579 def isabs(self):
2580 2580 if self.scheme and self.scheme != 'file':
2581 2581 return True # remote URL
2582 2582 if hasdriveletter(self.path):
2583 2583 return True # absolute for our purposes - can't be joined()
2584 2584 if self.path.startswith(r'\\'):
2585 2585 return True # Windows UNC path
2586 2586 if self.path.startswith('/'):
2587 2587 return True # POSIX-style
2588 2588 return False
2589 2589
2590 2590 def localpath(self):
2591 2591 if self.scheme == 'file' or self.scheme == 'bundle':
2592 2592 path = self.path or '/'
2593 2593 # For Windows, we need to promote hosts containing drive
2594 2594 # letters to paths with drive letters.
2595 2595 if hasdriveletter(self._hostport):
2596 2596 path = self._hostport + '/' + self.path
2597 2597 elif (self.host is not None and self.path
2598 2598 and not hasdriveletter(path)):
2599 2599 path = '/' + path
2600 2600 return path
2601 2601 return self._origpath
2602 2602
2603 2603 def islocal(self):
2604 2604 '''whether localpath will return something that posixfile can open'''
2605 2605 return (not self.scheme or self.scheme == 'file'
2606 2606 or self.scheme == 'bundle')
2607 2607
2608 2608 def hasscheme(path):
2609 2609 return bool(url(path).scheme)
2610 2610
2611 2611 def hasdriveletter(path):
2612 2612 return path and path[1:2] == ':' and path[0:1].isalpha()
2613 2613
2614 2614 def urllocalpath(path):
2615 2615 return url(path, parsequery=False, parsefragment=False).localpath()
2616 2616
2617 2617 def hidepassword(u):
2618 2618 '''hide user credential in a url string'''
2619 2619 u = url(u)
2620 2620 if u.passwd:
2621 2621 u.passwd = '***'
2622 2622 return str(u)
2623 2623
2624 2624 def removeauth(u):
2625 2625 '''remove all authentication information from a url string'''
2626 2626 u = url(u)
2627 2627 u.user = u.passwd = None
2628 2628 return str(u)
2629 2629
2630 2630 def isatty(fp):
2631 2631 try:
2632 2632 return fp.isatty()
2633 2633 except AttributeError:
2634 2634 return False
2635 2635
2636 2636 timecount = unitcountfn(
2637 2637 (1, 1e3, _('%.0f s')),
2638 2638 (100, 1, _('%.1f s')),
2639 2639 (10, 1, _('%.2f s')),
2640 2640 (1, 1, _('%.3f s')),
2641 2641 (100, 0.001, _('%.1f ms')),
2642 2642 (10, 0.001, _('%.2f ms')),
2643 2643 (1, 0.001, _('%.3f ms')),
2644 2644 (100, 0.000001, _('%.1f us')),
2645 2645 (10, 0.000001, _('%.2f us')),
2646 2646 (1, 0.000001, _('%.3f us')),
2647 2647 (100, 0.000000001, _('%.1f ns')),
2648 2648 (10, 0.000000001, _('%.2f ns')),
2649 2649 (1, 0.000000001, _('%.3f ns')),
2650 2650 )
2651 2651
2652 2652 _timenesting = [0]
2653 2653
2654 2654 def timed(func):
2655 2655 '''Report the execution time of a function call to stderr.
2656 2656
2657 2657 During development, use as a decorator when you need to measure
2658 2658 the cost of a function, e.g. as follows:
2659 2659
2660 2660 @util.timed
2661 2661 def foo(a, b, c):
2662 2662 pass
2663 2663 '''
2664 2664
2665 2665 def wrapper(*args, **kwargs):
2666 2666 start = time.time()
2667 2667 indent = 2
2668 2668 _timenesting[0] += indent
2669 2669 try:
2670 2670 return func(*args, **kwargs)
2671 2671 finally:
2672 2672 elapsed = time.time() - start
2673 2673 _timenesting[0] -= indent
2674 2674 sys.stderr.write('%s%s: %s\n' %
2675 2675 (' ' * _timenesting[0], func.__name__,
2676 2676 timecount(elapsed)))
2677 2677 return wrapper
2678 2678
2679 2679 _sizeunits = (('m', 2**20), ('k', 2**10), ('g', 2**30),
2680 2680 ('kb', 2**10), ('mb', 2**20), ('gb', 2**30), ('b', 1))
2681 2681
2682 2682 def sizetoint(s):
2683 2683 '''Convert a space specifier to a byte count.
2684 2684
2685 2685 >>> sizetoint('30')
2686 2686 30
2687 2687 >>> sizetoint('2.2kb')
2688 2688 2252
2689 2689 >>> sizetoint('6M')
2690 2690 6291456
2691 2691 '''
2692 2692 t = s.strip().lower()
2693 2693 try:
2694 2694 for k, u in _sizeunits:
2695 2695 if t.endswith(k):
2696 2696 return int(float(t[:-len(k)]) * u)
2697 2697 return int(t)
2698 2698 except ValueError:
2699 2699 raise error.ParseError(_("couldn't parse size: %s") % s)
2700 2700
2701 2701 class hooks(object):
2702 2702 '''A collection of hook functions that can be used to extend a
2703 2703 function's behavior. Hooks are called in lexicographic order,
2704 2704 based on the names of their sources.'''
2705 2705
2706 2706 def __init__(self):
2707 2707 self._hooks = []
2708 2708
2709 2709 def add(self, source, hook):
2710 2710 self._hooks.append((source, hook))
2711 2711
2712 2712 def __call__(self, *args):
2713 2713 self._hooks.sort(key=lambda x: x[0])
2714 2714 results = []
2715 2715 for source, hook in self._hooks:
2716 2716 results.append(hook(*args))
2717 2717 return results
2718 2718
2719 2719 def getstackframes(skip=0, line=' %-*s in %s\n', fileline='%s:%s'):
2720 2720 '''Yields lines for a nicely formatted stacktrace.
2721 2721 Skips the 'skip' last entries.
2722 2722 Each file+linenumber is formatted according to fileline.
2723 2723 Each line is formatted according to line.
2724 2724 If line is None, it yields:
2725 2725 length of longest filepath+line number,
2726 2726 filepath+linenumber,
2727 2727 function
2728 2728
2729 2729 Not be used in production code but very convenient while developing.
2730 2730 '''
2731 2731 entries = [(fileline % (fn, ln), func)
2732 2732 for fn, ln, func, _text in traceback.extract_stack()[:-skip - 1]]
2733 2733 if entries:
2734 2734 fnmax = max(len(entry[0]) for entry in entries)
2735 2735 for fnln, func in entries:
2736 2736 if line is None:
2737 2737 yield (fnmax, fnln, func)
2738 2738 else:
2739 2739 yield line % (fnmax, fnln, func)
2740 2740
2741 2741 def debugstacktrace(msg='stacktrace', skip=0, f=sys.stderr, otherf=sys.stdout):
2742 2742 '''Writes a message to f (stderr) with a nicely formatted stacktrace.
2743 2743 Skips the 'skip' last entries. By default it will flush stdout first.
2744 2744 It can be used everywhere and intentionally does not require an ui object.
2745 2745 Not be used in production code but very convenient while developing.
2746 2746 '''
2747 2747 if otherf:
2748 2748 otherf.flush()
2749 2749 f.write('%s at:\n' % msg)
2750 2750 for line in getstackframes(skip + 1):
2751 2751 f.write(line)
2752 2752 f.flush()
2753 2753
2754 2754 class dirs(object):
2755 2755 '''a multiset of directory names from a dirstate or manifest'''
2756 2756
2757 2757 def __init__(self, map, skip=None):
2758 2758 self._dirs = {}
2759 2759 addpath = self.addpath
2760 2760 if safehasattr(map, 'iteritems') and skip is not None:
2761 2761 for f, s in map.iteritems():
2762 2762 if s[0] != skip:
2763 2763 addpath(f)
2764 2764 else:
2765 2765 for f in map:
2766 2766 addpath(f)
2767 2767
2768 2768 def addpath(self, path):
2769 2769 dirs = self._dirs
2770 2770 for base in finddirs(path):
2771 2771 if base in dirs:
2772 2772 dirs[base] += 1
2773 2773 return
2774 2774 dirs[base] = 1
2775 2775
2776 2776 def delpath(self, path):
2777 2777 dirs = self._dirs
2778 2778 for base in finddirs(path):
2779 2779 if dirs[base] > 1:
2780 2780 dirs[base] -= 1
2781 2781 return
2782 2782 del dirs[base]
2783 2783
2784 2784 def __iter__(self):
2785 2785 return self._dirs.iterkeys()
2786 2786
2787 2787 def __contains__(self, d):
2788 2788 return d in self._dirs
2789 2789
2790 2790 if safehasattr(parsers, 'dirs'):
2791 2791 dirs = parsers.dirs
2792 2792
2793 2793 def finddirs(path):
2794 2794 pos = path.rfind('/')
2795 2795 while pos != -1:
2796 2796 yield path[:pos]
2797 2797 pos = path.rfind('/', 0, pos)
2798 2798
2799 2799 class ctxmanager(object):
2800 2800 '''A context manager for use in 'with' blocks to allow multiple
2801 2801 contexts to be entered at once. This is both safer and more
2802 2802 flexible than contextlib.nested.
2803 2803
2804 2804 Once Mercurial supports Python 2.7+, this will become mostly
2805 2805 unnecessary.
2806 2806 '''
2807 2807
2808 2808 def __init__(self, *args):
2809 2809 '''Accepts a list of no-argument functions that return context
2810 2810 managers. These will be invoked at __call__ time.'''
2811 2811 self._pending = args
2812 2812 self._atexit = []
2813 2813
2814 2814 def __enter__(self):
2815 2815 return self
2816 2816
2817 2817 def enter(self):
2818 2818 '''Create and enter context managers in the order in which they were
2819 2819 passed to the constructor.'''
2820 2820 values = []
2821 2821 for func in self._pending:
2822 2822 obj = func()
2823 2823 values.append(obj.__enter__())
2824 2824 self._atexit.append(obj.__exit__)
2825 2825 del self._pending
2826 2826 return values
2827 2827
2828 2828 def atexit(self, func, *args, **kwargs):
2829 2829 '''Add a function to call when this context manager exits. The
2830 2830 ordering of multiple atexit calls is unspecified, save that
2831 2831 they will happen before any __exit__ functions.'''
2832 2832 def wrapper(exc_type, exc_val, exc_tb):
2833 2833 func(*args, **kwargs)
2834 2834 self._atexit.append(wrapper)
2835 2835 return func
2836 2836
2837 2837 def __exit__(self, exc_type, exc_val, exc_tb):
2838 2838 '''Context managers are exited in the reverse order from which
2839 2839 they were created.'''
2840 2840 received = exc_type is not None
2841 2841 suppressed = False
2842 2842 pending = None
2843 2843 self._atexit.reverse()
2844 2844 for exitfunc in self._atexit:
2845 2845 try:
2846 2846 if exitfunc(exc_type, exc_val, exc_tb):
2847 2847 suppressed = True
2848 2848 exc_type = None
2849 2849 exc_val = None
2850 2850 exc_tb = None
2851 2851 except BaseException:
2852 2852 pending = sys.exc_info()
2853 2853 exc_type, exc_val, exc_tb = pending = sys.exc_info()
2854 2854 del self._atexit
2855 2855 if pending:
2856 2856 raise exc_val
2857 2857 return received and suppressed
2858 2858
2859 2859 # compression code
2860 2860
2861 2861 class compressormanager(object):
2862 2862 """Holds registrations of various compression engines.
2863 2863
2864 2864 This class essentially abstracts the differences between compression
2865 2865 engines to allow new compression formats to be added easily, possibly from
2866 2866 extensions.
2867 2867
2868 2868 Compressors are registered against the global instance by calling its
2869 2869 ``register()`` method.
2870 2870 """
2871 2871 def __init__(self):
2872 2872 self._engines = {}
2873 2873 # Bundle spec human name to engine name.
2874 2874 self._bundlenames = {}
2875 2875 # Internal bundle identifier to engine name.
2876 2876 self._bundletypes = {}
2877 2877
2878 2878 def __getitem__(self, key):
2879 2879 return self._engines[key]
2880 2880
2881 2881 def __contains__(self, key):
2882 2882 return key in self._engines
2883 2883
2884 2884 def __iter__(self):
2885 2885 return iter(self._engines.keys())
2886 2886
2887 2887 def register(self, engine):
2888 2888 """Register a compression engine with the manager.
2889 2889
2890 2890 The argument must be a ``compressionengine`` instance.
2891 2891 """
2892 2892 if not isinstance(engine, compressionengine):
2893 2893 raise ValueError(_('argument must be a compressionengine'))
2894 2894
2895 2895 name = engine.name()
2896 2896
2897 2897 if name in self._engines:
2898 2898 raise error.Abort(_('compression engine %s already registered') %
2899 2899 name)
2900 2900
2901 2901 bundleinfo = engine.bundletype()
2902 2902 if bundleinfo:
2903 2903 bundlename, bundletype = bundleinfo
2904 2904
2905 2905 if bundlename in self._bundlenames:
2906 2906 raise error.Abort(_('bundle name %s already registered') %
2907 2907 bundlename)
2908 2908 if bundletype in self._bundletypes:
2909 2909 raise error.Abort(_('bundle type %s already registered by %s') %
2910 2910 (bundletype, self._bundletypes[bundletype]))
2911 2911
2912 2912 # No external facing name declared.
2913 2913 if bundlename:
2914 2914 self._bundlenames[bundlename] = name
2915 2915
2916 2916 self._bundletypes[bundletype] = name
2917 2917
2918 2918 self._engines[name] = engine
2919 2919
2920 2920 @property
2921 2921 def supportedbundlenames(self):
2922 2922 return set(self._bundlenames.keys())
2923 2923
2924 2924 @property
2925 2925 def supportedbundletypes(self):
2926 2926 return set(self._bundletypes.keys())
2927 2927
2928 2928 def forbundlename(self, bundlename):
2929 2929 """Obtain a compression engine registered to a bundle name.
2930 2930
2931 2931 Will raise KeyError if the bundle type isn't registered.
2932 2932 """
2933 2933 return self._engines[self._bundlenames[bundlename]]
2934 2934
2935 2935 def forbundletype(self, bundletype):
2936 2936 """Obtain a compression engine registered to a bundle type.
2937 2937
2938 2938 Will raise KeyError if the bundle type isn't registered.
2939 2939 """
2940 2940 return self._engines[self._bundletypes[bundletype]]
2941 2941
2942 2942 compengines = compressormanager()
2943 2943
2944 2944 class compressionengine(object):
2945 2945 """Base class for compression engines.
2946 2946
2947 2947 Compression engines must implement the interface defined by this class.
2948 2948 """
2949 2949 def name(self):
2950 2950 """Returns the name of the compression engine.
2951 2951
2952 2952 This is the key the engine is registered under.
2953 2953
2954 2954 This method must be implemented.
2955 2955 """
2956 2956 raise NotImplementedError()
2957 2957
2958 2958 def bundletype(self):
2959 2959 """Describes bundle identifiers for this engine.
2960 2960
2961 2961 If this compression engine isn't supported for bundles, returns None.
2962 2962
2963 2963 If this engine can be used for bundles, returns a 2-tuple of strings of
2964 2964 the user-facing "bundle spec" compression name and an internal
2965 2965 identifier used to denote the compression format within bundles. To
2966 2966 exclude the name from external usage, set the first element to ``None``.
2967 2967
2968 2968 If bundle compression is supported, the class must also implement
2969 ``compressorobj`` and `decompressorreader``.
2969 ``compressstream``, ``compressorobj`` and `decompressorreader``.
2970 2970 """
2971 2971 return None
2972 2972
2973 def compressstream(self, it, opts=None):
2974 """Compress an iterator of chunks.
2975
2976 The method receives an iterator (ideally a generator) of chunks of
2977 bytes to be compressed. It returns an iterator (ideally a generator)
2978 of bytes of chunks representing the compressed output.
2979
2980 Optionally accepts an argument defining how to perform compression.
2981 Each engine treats this argument differently.
2982 """
2983 raise NotImplementedError()
2984
2973 2985 def compressorobj(self):
2974 2986 """(Temporary) Obtain an object used for compression.
2975 2987
2976 2988 The returned object has ``compress(data)`` and ``flush()`` methods.
2977 2989 These are used to incrementally feed data chunks into a compressor.
2978 2990 """
2979 2991 raise NotImplementedError()
2980 2992
2981 2993 def decompressorreader(self, fh):
2982 2994 """Perform decompression on a file object.
2983 2995
2984 2996 Argument is an object with a ``read(size)`` method that returns
2985 2997 compressed data. Return value is an object with a ``read(size)`` that
2986 2998 returns uncompressed data.
2987 2999 """
2988 3000 raise NotImplementedError()
2989 3001
2990 3002 class _zlibengine(compressionengine):
2991 3003 def name(self):
2992 3004 return 'zlib'
2993 3005
2994 3006 def bundletype(self):
2995 3007 return 'gzip', 'GZ'
2996 3008
2997 3009 def compressorobj(self):
2998 3010 return zlib.compressobj()
2999 3011
3012 def compressstream(self, it, opts=None):
3013 opts = opts or {}
3014
3015 z = zlib.compressobj(opts.get('level', -1))
3016 for chunk in it:
3017 data = z.compress(chunk)
3018 # Not all calls to compress emit data. It is cheaper to inspect
3019 # here than to feed empty chunks through generator.
3020 if data:
3021 yield data
3022
3023 yield z.flush()
3024
3000 3025 def decompressorreader(self, fh):
3001 3026 def gen():
3002 3027 d = zlib.decompressobj()
3003 3028 for chunk in filechunkiter(fh):
3004 3029 yield d.decompress(chunk)
3005 3030
3006 3031 return chunkbuffer(gen())
3007 3032
3008 3033 compengines.register(_zlibengine())
3009 3034
3010 3035 class _bz2engine(compressionengine):
3011 3036 def name(self):
3012 3037 return 'bz2'
3013 3038
3014 3039 def bundletype(self):
3015 3040 return 'bzip2', 'BZ'
3016 3041
3017 3042 def compressorobj(self):
3018 3043 return bz2.BZ2Compressor()
3019 3044
3045 def compressstream(self, it, opts=None):
3046 opts = opts or {}
3047 z = bz2.BZ2Compressor(opts.get('level', 9))
3048 for chunk in it:
3049 data = z.compress(chunk)
3050 if data:
3051 yield data
3052
3053 yield z.flush()
3054
3020 3055 def decompressorreader(self, fh):
3021 3056 def gen():
3022 3057 d = bz2.BZ2Decompressor()
3023 3058 for chunk in filechunkiter(fh):
3024 3059 yield d.decompress(chunk)
3025 3060
3026 3061 return chunkbuffer(gen())
3027 3062
3028 3063 compengines.register(_bz2engine())
3029 3064
3030 3065 class _truncatedbz2engine(compressionengine):
3031 3066 def name(self):
3032 3067 return 'bz2truncated'
3033 3068
3034 3069 def bundletype(self):
3035 3070 return None, '_truncatedBZ'
3036 3071
3037 3072 # We don't implement compressorobj because it is hackily handled elsewhere.
3038 3073
3039 3074 def decompressorreader(self, fh):
3040 3075 def gen():
3041 3076 # The input stream doesn't have the 'BZ' header. So add it back.
3042 3077 d = bz2.BZ2Decompressor()
3043 3078 d.decompress('BZ')
3044 3079 for chunk in filechunkiter(fh):
3045 3080 yield d.decompress(chunk)
3046 3081
3047 3082 return chunkbuffer(gen())
3048 3083
3049 3084 compengines.register(_truncatedbz2engine())
3050 3085
3051 3086 class nocompress(object):
3052 3087 def compress(self, x):
3053 3088 return x
3054 3089
3055 3090 def flush(self):
3056 3091 return ''
3057 3092
3058 3093 class _noopengine(compressionengine):
3059 3094 def name(self):
3060 3095 return 'none'
3061 3096
3062 3097 def bundletype(self):
3063 3098 return 'none', 'UN'
3064 3099
3065 3100 def compressorobj(self):
3066 3101 return nocompress()
3067 3102
3103 def compressstream(self, it, opts=None):
3104 return it
3105
3068 3106 def decompressorreader(self, fh):
3069 3107 return fh
3070 3108
3071 3109 compengines.register(_noopengine())
3072 3110
3073 3111 # convenient shortcut
3074 3112 dst = debugstacktrace
General Comments 0
You need to be logged in to leave comments. Login now