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