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