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