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