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