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