##// END OF EJS Templates
Make hg status work for repositories in root directory on windows (issue 228)
Manpreet Singh -
r2278:3711e23a default
parent child Browse files
Show More
@@ -1,892 +1,892 b''
1 1 """
2 2 util.py - Mercurial utility functions and platform specfic implementations
3 3
4 4 Copyright 2005 K. Thananchayan <thananck@yahoo.com>
5 5
6 6 This software may be used and distributed according to the terms
7 7 of the GNU General Public License, incorporated herein by reference.
8 8
9 9 This contains helper routines that are independent of the SCM core and hide
10 10 platform-specific details from the core.
11 11 """
12 12
13 13 import os, errno
14 14 from i18n import gettext as _
15 15 from demandload import *
16 16 demandload(globals(), "cStringIO errno popen2 re shutil sys tempfile")
17 17 demandload(globals(), "threading time")
18 18
19 19 class SignalInterrupt(Exception):
20 20 """Exception raised on SIGTERM and SIGHUP."""
21 21
22 22 def pipefilter(s, cmd):
23 23 '''filter string S through command CMD, returning its output'''
24 24 (pout, pin) = popen2.popen2(cmd, -1, 'b')
25 25 def writer():
26 26 try:
27 27 pin.write(s)
28 28 pin.close()
29 29 except IOError, inst:
30 30 if inst.errno != errno.EPIPE:
31 31 raise
32 32
33 33 # we should use select instead on UNIX, but this will work on most
34 34 # systems, including Windows
35 35 w = threading.Thread(target=writer)
36 36 w.start()
37 37 f = pout.read()
38 38 pout.close()
39 39 w.join()
40 40 return f
41 41
42 42 def tempfilter(s, cmd):
43 43 '''filter string S through a pair of temporary files with CMD.
44 44 CMD is used as a template to create the real command to be run,
45 45 with the strings INFILE and OUTFILE replaced by the real names of
46 46 the temporary files generated.'''
47 47 inname, outname = None, None
48 48 try:
49 49 infd, inname = tempfile.mkstemp(prefix='hg-filter-in-')
50 50 fp = os.fdopen(infd, 'wb')
51 51 fp.write(s)
52 52 fp.close()
53 53 outfd, outname = tempfile.mkstemp(prefix='hg-filter-out-')
54 54 os.close(outfd)
55 55 cmd = cmd.replace('INFILE', inname)
56 56 cmd = cmd.replace('OUTFILE', outname)
57 57 code = os.system(cmd)
58 58 if code: raise Abort(_("command '%s' failed: %s") %
59 59 (cmd, explain_exit(code)))
60 60 return open(outname, 'rb').read()
61 61 finally:
62 62 try:
63 63 if inname: os.unlink(inname)
64 64 except: pass
65 65 try:
66 66 if outname: os.unlink(outname)
67 67 except: pass
68 68
69 69 filtertable = {
70 70 'tempfile:': tempfilter,
71 71 'pipe:': pipefilter,
72 72 }
73 73
74 74 def filter(s, cmd):
75 75 "filter a string through a command that transforms its input to its output"
76 76 for name, fn in filtertable.iteritems():
77 77 if cmd.startswith(name):
78 78 return fn(s, cmd[len(name):].lstrip())
79 79 return pipefilter(s, cmd)
80 80
81 81 def find_in_path(name, path, default=None):
82 82 '''find name in search path. path can be string (will be split
83 83 with os.pathsep), or iterable thing that returns strings. if name
84 84 found, return path to name. else return default.'''
85 85 if isinstance(path, str):
86 86 path = path.split(os.pathsep)
87 87 for p in path:
88 88 p_name = os.path.join(p, name)
89 89 if os.path.exists(p_name):
90 90 return p_name
91 91 return default
92 92
93 93 def patch(strip, patchname, ui):
94 94 """apply the patch <patchname> to the working directory.
95 95 a list of patched files is returned"""
96 96 patcher = find_in_path('gpatch', os.environ.get('PATH', ''), 'patch')
97 97 fp = os.popen('"%s" -p%d < "%s"' % (patcher, strip, patchname))
98 98 files = {}
99 99 for line in fp:
100 100 line = line.rstrip()
101 101 ui.status("%s\n" % line)
102 102 if line.startswith('patching file '):
103 103 pf = parse_patch_output(line)
104 104 files.setdefault(pf, 1)
105 105 code = fp.close()
106 106 if code:
107 107 raise Abort(_("patch command failed: %s") % explain_exit(code)[0])
108 108 return files.keys()
109 109
110 110 def binary(s):
111 111 """return true if a string is binary data using diff's heuristic"""
112 112 if s and '\0' in s[:4096]:
113 113 return True
114 114 return False
115 115
116 116 def unique(g):
117 117 """return the uniq elements of iterable g"""
118 118 seen = {}
119 119 for f in g:
120 120 if f not in seen:
121 121 seen[f] = 1
122 122 yield f
123 123
124 124 class Abort(Exception):
125 125 """Raised if a command needs to print an error and exit."""
126 126
127 127 def always(fn): return True
128 128 def never(fn): return False
129 129
130 130 def patkind(name, dflt_pat='glob'):
131 131 """Split a string into an optional pattern kind prefix and the
132 132 actual pattern."""
133 133 for prefix in 're', 'glob', 'path', 'relglob', 'relpath', 'relre':
134 134 if name.startswith(prefix + ':'): return name.split(':', 1)
135 135 return dflt_pat, name
136 136
137 137 def globre(pat, head='^', tail='$'):
138 138 "convert a glob pattern into a regexp"
139 139 i, n = 0, len(pat)
140 140 res = ''
141 141 group = False
142 142 def peek(): return i < n and pat[i]
143 143 while i < n:
144 144 c = pat[i]
145 145 i = i+1
146 146 if c == '*':
147 147 if peek() == '*':
148 148 i += 1
149 149 res += '.*'
150 150 else:
151 151 res += '[^/]*'
152 152 elif c == '?':
153 153 res += '.'
154 154 elif c == '[':
155 155 j = i
156 156 if j < n and pat[j] in '!]':
157 157 j += 1
158 158 while j < n and pat[j] != ']':
159 159 j += 1
160 160 if j >= n:
161 161 res += '\\['
162 162 else:
163 163 stuff = pat[i:j].replace('\\','\\\\')
164 164 i = j + 1
165 165 if stuff[0] == '!':
166 166 stuff = '^' + stuff[1:]
167 167 elif stuff[0] == '^':
168 168 stuff = '\\' + stuff
169 169 res = '%s[%s]' % (res, stuff)
170 170 elif c == '{':
171 171 group = True
172 172 res += '(?:'
173 173 elif c == '}' and group:
174 174 res += ')'
175 175 group = False
176 176 elif c == ',' and group:
177 177 res += '|'
178 178 elif c == '\\':
179 179 p = peek()
180 180 if p:
181 181 i += 1
182 182 res += re.escape(p)
183 183 else:
184 184 res += re.escape(c)
185 185 else:
186 186 res += re.escape(c)
187 187 return head + res + tail
188 188
189 189 _globchars = {'[': 1, '{': 1, '*': 1, '?': 1}
190 190
191 191 def pathto(n1, n2):
192 192 '''return the relative path from one place to another.
193 193 this returns a path in the form used by the local filesystem, not hg.'''
194 194 if not n1: return localpath(n2)
195 195 a, b = n1.split('/'), n2.split('/')
196 196 a.reverse()
197 197 b.reverse()
198 198 while a and b and a[-1] == b[-1]:
199 199 a.pop()
200 200 b.pop()
201 201 b.reverse()
202 202 return os.sep.join((['..'] * len(a)) + b)
203 203
204 204 def canonpath(root, cwd, myname):
205 205 """return the canonical path of myname, given cwd and root"""
206 206 if root == os.sep:
207 207 rootsep = os.sep
208 208 elif root.endswith(os.sep):
209 209 rootsep = root
210 210 else:
211 211 rootsep = root + os.sep
212 212 name = myname
213 213 if not os.path.isabs(name):
214 214 name = os.path.join(root, cwd, name)
215 215 name = os.path.normpath(name)
216 if name.startswith(rootsep):
216 if name != rootsep and name.startswith(rootsep):
217 217 name = name[len(rootsep):]
218 218 audit_path(name)
219 219 return pconvert(name)
220 220 elif name == root:
221 221 return ''
222 222 else:
223 223 # Determine whether `name' is in the hierarchy at or beneath `root',
224 224 # by iterating name=dirname(name) until that causes no change (can't
225 225 # check name == '/', because that doesn't work on windows). For each
226 226 # `name', compare dev/inode numbers. If they match, the list `rel'
227 227 # holds the reversed list of components making up the relative file
228 228 # name we want.
229 229 root_st = os.stat(root)
230 230 rel = []
231 231 while True:
232 232 try:
233 233 name_st = os.stat(name)
234 234 except OSError:
235 235 break
236 236 if samestat(name_st, root_st):
237 237 rel.reverse()
238 238 name = os.path.join(*rel)
239 239 audit_path(name)
240 240 return pconvert(name)
241 241 dirname, basename = os.path.split(name)
242 242 rel.append(basename)
243 243 if dirname == name:
244 244 break
245 245 name = dirname
246 246
247 247 raise Abort('%s not under root' % myname)
248 248
249 249 def matcher(canonroot, cwd='', names=['.'], inc=[], exc=[], head='', src=None):
250 250 return _matcher(canonroot, cwd, names, inc, exc, head, 'glob', src)
251 251
252 252 def cmdmatcher(canonroot, cwd='', names=['.'], inc=[], exc=[], head='', src=None):
253 253 if os.name == 'nt':
254 254 dflt_pat = 'glob'
255 255 else:
256 256 dflt_pat = 'relpath'
257 257 return _matcher(canonroot, cwd, names, inc, exc, head, dflt_pat, src)
258 258
259 259 def _matcher(canonroot, cwd, names, inc, exc, head, dflt_pat, src):
260 260 """build a function to match a set of file patterns
261 261
262 262 arguments:
263 263 canonroot - the canonical root of the tree you're matching against
264 264 cwd - the current working directory, if relevant
265 265 names - patterns to find
266 266 inc - patterns to include
267 267 exc - patterns to exclude
268 268 head - a regex to prepend to patterns to control whether a match is rooted
269 269
270 270 a pattern is one of:
271 271 'glob:<rooted glob>'
272 272 're:<rooted regexp>'
273 273 'path:<rooted path>'
274 274 'relglob:<relative glob>'
275 275 'relpath:<relative path>'
276 276 'relre:<relative regexp>'
277 277 '<rooted path or regexp>'
278 278
279 279 returns:
280 280 a 3-tuple containing
281 281 - list of explicit non-pattern names passed in
282 282 - a bool match(filename) function
283 283 - a bool indicating if any patterns were passed in
284 284
285 285 todo:
286 286 make head regex a rooted bool
287 287 """
288 288
289 289 def contains_glob(name):
290 290 for c in name:
291 291 if c in _globchars: return True
292 292 return False
293 293
294 294 def regex(kind, name, tail):
295 295 '''convert a pattern into a regular expression'''
296 296 if kind == 're':
297 297 return name
298 298 elif kind == 'path':
299 299 return '^' + re.escape(name) + '(?:/|$)'
300 300 elif kind == 'relglob':
301 301 return head + globre(name, '(?:|.*/)', tail)
302 302 elif kind == 'relpath':
303 303 return head + re.escape(name) + tail
304 304 elif kind == 'relre':
305 305 if name.startswith('^'):
306 306 return name
307 307 return '.*' + name
308 308 return head + globre(name, '', tail)
309 309
310 310 def matchfn(pats, tail):
311 311 """build a matching function from a set of patterns"""
312 312 if not pats:
313 313 return
314 314 matches = []
315 315 for k, p in pats:
316 316 try:
317 317 pat = '(?:%s)' % regex(k, p, tail)
318 318 matches.append(re.compile(pat).match)
319 319 except re.error:
320 320 if src: raise Abort("%s: invalid pattern (%s): %s" % (src, k, p))
321 321 else: raise Abort("invalid pattern (%s): %s" % (k, p))
322 322
323 323 def buildfn(text):
324 324 for m in matches:
325 325 r = m(text)
326 326 if r:
327 327 return r
328 328
329 329 return buildfn
330 330
331 331 def globprefix(pat):
332 332 '''return the non-glob prefix of a path, e.g. foo/* -> foo'''
333 333 root = []
334 334 for p in pat.split(os.sep):
335 335 if contains_glob(p): break
336 336 root.append(p)
337 337 return '/'.join(root)
338 338
339 339 pats = []
340 340 files = []
341 341 roots = []
342 342 for kind, name in [patkind(p, dflt_pat) for p in names]:
343 343 if kind in ('glob', 'relpath'):
344 344 name = canonpath(canonroot, cwd, name)
345 345 if name == '':
346 346 kind, name = 'glob', '**'
347 347 if kind in ('glob', 'path', 're'):
348 348 pats.append((kind, name))
349 349 if kind == 'glob':
350 350 root = globprefix(name)
351 351 if root: roots.append(root)
352 352 elif kind == 'relpath':
353 353 files.append((kind, name))
354 354 roots.append(name)
355 355
356 356 patmatch = matchfn(pats, '$') or always
357 357 filematch = matchfn(files, '(?:/|$)') or always
358 358 incmatch = always
359 359 if inc:
360 360 incmatch = matchfn(map(patkind, inc), '(?:/|$)')
361 361 excmatch = lambda fn: False
362 362 if exc:
363 363 excmatch = matchfn(map(patkind, exc), '(?:/|$)')
364 364
365 365 return (roots,
366 366 lambda fn: (incmatch(fn) and not excmatch(fn) and
367 367 (fn.endswith('/') or
368 368 (not pats and not files) or
369 369 (pats and patmatch(fn)) or
370 370 (files and filematch(fn)))),
371 371 (inc or exc or (pats and pats != [('glob', '**')])) and True)
372 372
373 373 def system(cmd, environ={}, cwd=None, onerr=None, errprefix=None):
374 374 '''enhanced shell command execution.
375 375 run with environment maybe modified, maybe in different dir.
376 376
377 377 if command fails and onerr is None, return status. if ui object,
378 378 print error message and return status, else raise onerr object as
379 379 exception.'''
380 380 oldenv = {}
381 381 for k in environ:
382 382 oldenv[k] = os.environ.get(k)
383 383 if cwd is not None:
384 384 oldcwd = os.getcwd()
385 385 try:
386 386 for k, v in environ.iteritems():
387 387 os.environ[k] = str(v)
388 388 if cwd is not None and oldcwd != cwd:
389 389 os.chdir(cwd)
390 390 rc = os.system(cmd)
391 391 if rc and onerr:
392 392 errmsg = '%s %s' % (os.path.basename(cmd.split(None, 1)[0]),
393 393 explain_exit(rc)[0])
394 394 if errprefix:
395 395 errmsg = '%s: %s' % (errprefix, errmsg)
396 396 try:
397 397 onerr.warn(errmsg + '\n')
398 398 except AttributeError:
399 399 raise onerr(errmsg)
400 400 return rc
401 401 finally:
402 402 for k, v in oldenv.iteritems():
403 403 if v is None:
404 404 del os.environ[k]
405 405 else:
406 406 os.environ[k] = v
407 407 if cwd is not None and oldcwd != cwd:
408 408 os.chdir(oldcwd)
409 409
410 410 def rename(src, dst):
411 411 """forcibly rename a file"""
412 412 try:
413 413 os.rename(src, dst)
414 414 except OSError, err:
415 415 # on windows, rename to existing file is not allowed, so we
416 416 # must delete destination first. but if file is open, unlink
417 417 # schedules it for delete but does not delete it. rename
418 418 # happens immediately even for open files, so we create
419 419 # temporary file, delete it, rename destination to that name,
420 420 # then delete that. then rename is safe to do.
421 421 fd, temp = tempfile.mkstemp(dir=os.path.dirname(dst) or '.')
422 422 os.close(fd)
423 423 os.unlink(temp)
424 424 os.rename(dst, temp)
425 425 os.unlink(temp)
426 426 os.rename(src, dst)
427 427
428 428 def unlink(f):
429 429 """unlink and remove the directory if it is empty"""
430 430 os.unlink(f)
431 431 # try removing directories that might now be empty
432 432 try:
433 433 os.removedirs(os.path.dirname(f))
434 434 except OSError:
435 435 pass
436 436
437 437 def copyfiles(src, dst, hardlink=None):
438 438 """Copy a directory tree using hardlinks if possible"""
439 439
440 440 if hardlink is None:
441 441 hardlink = (os.stat(src).st_dev ==
442 442 os.stat(os.path.dirname(dst)).st_dev)
443 443
444 444 if os.path.isdir(src):
445 445 os.mkdir(dst)
446 446 for name in os.listdir(src):
447 447 srcname = os.path.join(src, name)
448 448 dstname = os.path.join(dst, name)
449 449 copyfiles(srcname, dstname, hardlink)
450 450 else:
451 451 if hardlink:
452 452 try:
453 453 os_link(src, dst)
454 454 except (IOError, OSError):
455 455 hardlink = False
456 456 shutil.copy(src, dst)
457 457 else:
458 458 shutil.copy(src, dst)
459 459
460 460 def audit_path(path):
461 461 """Abort if path contains dangerous components"""
462 462 parts = os.path.normcase(path).split(os.sep)
463 463 if (os.path.splitdrive(path)[0] or parts[0] in ('.hg', '')
464 464 or os.pardir in parts):
465 465 raise Abort(_("path contains illegal component: %s\n") % path)
466 466
467 467 def _makelock_file(info, pathname):
468 468 ld = os.open(pathname, os.O_CREAT | os.O_WRONLY | os.O_EXCL)
469 469 os.write(ld, info)
470 470 os.close(ld)
471 471
472 472 def _readlock_file(pathname):
473 473 return posixfile(pathname).read()
474 474
475 475 def nlinks(pathname):
476 476 """Return number of hardlinks for the given file."""
477 477 return os.stat(pathname).st_nlink
478 478
479 479 if hasattr(os, 'link'):
480 480 os_link = os.link
481 481 else:
482 482 def os_link(src, dst):
483 483 raise OSError(0, _("Hardlinks not supported"))
484 484
485 485 def fstat(fp):
486 486 '''stat file object that may not have fileno method.'''
487 487 try:
488 488 return os.fstat(fp.fileno())
489 489 except AttributeError:
490 490 return os.stat(fp.name)
491 491
492 492 posixfile = file
493 493
494 494 def is_win_9x():
495 495 '''return true if run on windows 95, 98 or me.'''
496 496 try:
497 497 return sys.getwindowsversion()[3] == 1
498 498 except AttributeError:
499 499 return os.name == 'nt' and 'command' in os.environ.get('comspec', '')
500 500
501 501 # Platform specific variants
502 502 if os.name == 'nt':
503 503 demandload(globals(), "msvcrt")
504 504 nulldev = 'NUL:'
505 505
506 506 class winstdout:
507 507 '''stdout on windows misbehaves if sent through a pipe'''
508 508
509 509 def __init__(self, fp):
510 510 self.fp = fp
511 511
512 512 def __getattr__(self, key):
513 513 return getattr(self.fp, key)
514 514
515 515 def close(self):
516 516 try:
517 517 self.fp.close()
518 518 except: pass
519 519
520 520 def write(self, s):
521 521 try:
522 522 return self.fp.write(s)
523 523 except IOError, inst:
524 524 if inst.errno != 0: raise
525 525 self.close()
526 526 raise IOError(errno.EPIPE, 'Broken pipe')
527 527
528 528 sys.stdout = winstdout(sys.stdout)
529 529
530 530 def system_rcpath():
531 531 try:
532 532 return system_rcpath_win32()
533 533 except:
534 534 return [r'c:\mercurial\mercurial.ini']
535 535
536 536 def os_rcpath():
537 537 '''return default os-specific hgrc search path'''
538 538 return system_rcpath() + [os.path.join(os.path.expanduser('~'),
539 539 'mercurial.ini')]
540 540
541 541 def parse_patch_output(output_line):
542 542 """parses the output produced by patch and returns the file name"""
543 543 pf = output_line[14:]
544 544 if pf[0] == '`':
545 545 pf = pf[1:-1] # Remove the quotes
546 546 return pf
547 547
548 548 def testpid(pid):
549 549 '''return False if pid dead, True if running or not known'''
550 550 return True
551 551
552 552 def is_exec(f, last):
553 553 return last
554 554
555 555 def set_exec(f, mode):
556 556 pass
557 557
558 558 def set_binary(fd):
559 559 msvcrt.setmode(fd.fileno(), os.O_BINARY)
560 560
561 561 def pconvert(path):
562 562 return path.replace("\\", "/")
563 563
564 564 def localpath(path):
565 565 return path.replace('/', '\\')
566 566
567 567 def normpath(path):
568 568 return pconvert(os.path.normpath(path))
569 569
570 570 makelock = _makelock_file
571 571 readlock = _readlock_file
572 572
573 573 def samestat(s1, s2):
574 574 return False
575 575
576 576 def explain_exit(code):
577 577 return _("exited with status %d") % code, code
578 578
579 579 try:
580 580 # override functions with win32 versions if possible
581 581 from util_win32 import *
582 582 if not is_win_9x():
583 583 posixfile = posixfile_nt
584 584 except ImportError:
585 585 pass
586 586
587 587 else:
588 588 nulldev = '/dev/null'
589 589
590 590 def rcfiles(path):
591 591 rcs = [os.path.join(path, 'hgrc')]
592 592 rcdir = os.path.join(path, 'hgrc.d')
593 593 try:
594 594 rcs.extend([os.path.join(rcdir, f) for f in os.listdir(rcdir)
595 595 if f.endswith(".rc")])
596 596 except OSError, inst: pass
597 597 return rcs
598 598
599 599 def os_rcpath():
600 600 '''return default os-specific hgrc search path'''
601 601 path = []
602 602 # old mod_python does not set sys.argv
603 603 if len(getattr(sys, 'argv', [])) > 0:
604 604 path.extend(rcfiles(os.path.dirname(sys.argv[0]) +
605 605 '/../etc/mercurial'))
606 606 path.extend(rcfiles('/etc/mercurial'))
607 607 path.append(os.path.expanduser('~/.hgrc'))
608 608 path = [os.path.normpath(f) for f in path]
609 609 return path
610 610
611 611 def parse_patch_output(output_line):
612 612 """parses the output produced by patch and returns the file name"""
613 613 pf = output_line[14:]
614 614 if pf.startswith("'") and pf.endswith("'") and pf.find(" ") >= 0:
615 615 pf = pf[1:-1] # Remove the quotes
616 616 return pf
617 617
618 618 def is_exec(f, last):
619 619 """check whether a file is executable"""
620 620 return (os.stat(f).st_mode & 0100 != 0)
621 621
622 622 def set_exec(f, mode):
623 623 s = os.stat(f).st_mode
624 624 if (s & 0100 != 0) == mode:
625 625 return
626 626 if mode:
627 627 # Turn on +x for every +r bit when making a file executable
628 628 # and obey umask.
629 629 umask = os.umask(0)
630 630 os.umask(umask)
631 631 os.chmod(f, s | (s & 0444) >> 2 & ~umask)
632 632 else:
633 633 os.chmod(f, s & 0666)
634 634
635 635 def set_binary(fd):
636 636 pass
637 637
638 638 def pconvert(path):
639 639 return path
640 640
641 641 def localpath(path):
642 642 return path
643 643
644 644 normpath = os.path.normpath
645 645 samestat = os.path.samestat
646 646
647 647 def makelock(info, pathname):
648 648 try:
649 649 os.symlink(info, pathname)
650 650 except OSError, why:
651 651 if why.errno == errno.EEXIST:
652 652 raise
653 653 else:
654 654 _makelock_file(info, pathname)
655 655
656 656 def readlock(pathname):
657 657 try:
658 658 return os.readlink(pathname)
659 659 except OSError, why:
660 660 if why.errno == errno.EINVAL:
661 661 return _readlock_file(pathname)
662 662 else:
663 663 raise
664 664
665 665 def testpid(pid):
666 666 '''return False if pid dead, True if running or not sure'''
667 667 try:
668 668 os.kill(pid, 0)
669 669 return True
670 670 except OSError, inst:
671 671 return inst.errno != errno.ESRCH
672 672
673 673 def explain_exit(code):
674 674 """return a 2-tuple (desc, code) describing a process's status"""
675 675 if os.WIFEXITED(code):
676 676 val = os.WEXITSTATUS(code)
677 677 return _("exited with status %d") % val, val
678 678 elif os.WIFSIGNALED(code):
679 679 val = os.WTERMSIG(code)
680 680 return _("killed by signal %d") % val, val
681 681 elif os.WIFSTOPPED(code):
682 682 val = os.WSTOPSIG(code)
683 683 return _("stopped by signal %d") % val, val
684 684 raise ValueError(_("invalid exit code"))
685 685
686 686 def opener(base, audit=True):
687 687 """
688 688 return a function that opens files relative to base
689 689
690 690 this function is used to hide the details of COW semantics and
691 691 remote file access from higher level code.
692 692 """
693 693 p = base
694 694 audit_p = audit
695 695
696 696 def mktempcopy(name):
697 697 d, fn = os.path.split(name)
698 698 fd, temp = tempfile.mkstemp(prefix='.%s-' % fn, dir=d)
699 699 os.close(fd)
700 700 ofp = posixfile(temp, "wb")
701 701 try:
702 702 try:
703 703 ifp = posixfile(name, "rb")
704 704 except IOError, inst:
705 705 if not getattr(inst, 'filename', None):
706 706 inst.filename = name
707 707 raise
708 708 for chunk in filechunkiter(ifp):
709 709 ofp.write(chunk)
710 710 ifp.close()
711 711 ofp.close()
712 712 except:
713 713 try: os.unlink(temp)
714 714 except: pass
715 715 raise
716 716 st = os.lstat(name)
717 717 os.chmod(temp, st.st_mode)
718 718 return temp
719 719
720 720 class atomictempfile(posixfile):
721 721 """the file will only be copied when rename is called"""
722 722 def __init__(self, name, mode):
723 723 self.__name = name
724 724 self.temp = mktempcopy(name)
725 725 posixfile.__init__(self, self.temp, mode)
726 726 def rename(self):
727 727 if not self.closed:
728 728 posixfile.close(self)
729 729 rename(self.temp, self.__name)
730 730 def __del__(self):
731 731 if not self.closed:
732 732 try:
733 733 os.unlink(self.temp)
734 734 except: pass
735 735 posixfile.close(self)
736 736
737 737 class atomicfile(atomictempfile):
738 738 """the file will only be copied on close"""
739 739 def __init__(self, name, mode):
740 740 atomictempfile.__init__(self, name, mode)
741 741 def close(self):
742 742 self.rename()
743 743 def __del__(self):
744 744 self.rename()
745 745
746 746 def o(path, mode="r", text=False, atomic=False, atomictemp=False):
747 747 if audit_p:
748 748 audit_path(path)
749 749 f = os.path.join(p, path)
750 750
751 751 if not text:
752 752 mode += "b" # for that other OS
753 753
754 754 if mode[0] != "r":
755 755 try:
756 756 nlink = nlinks(f)
757 757 except OSError:
758 758 d = os.path.dirname(f)
759 759 if not os.path.isdir(d):
760 760 os.makedirs(d)
761 761 else:
762 762 if atomic:
763 763 return atomicfile(f, mode)
764 764 elif atomictemp:
765 765 return atomictempfile(f, mode)
766 766 if nlink > 1:
767 767 rename(mktempcopy(f), f)
768 768 return posixfile(f, mode)
769 769
770 770 return o
771 771
772 772 class chunkbuffer(object):
773 773 """Allow arbitrary sized chunks of data to be efficiently read from an
774 774 iterator over chunks of arbitrary size."""
775 775
776 776 def __init__(self, in_iter, targetsize = 2**16):
777 777 """in_iter is the iterator that's iterating over the input chunks.
778 778 targetsize is how big a buffer to try to maintain."""
779 779 self.in_iter = iter(in_iter)
780 780 self.buf = ''
781 781 self.targetsize = int(targetsize)
782 782 if self.targetsize <= 0:
783 783 raise ValueError(_("targetsize must be greater than 0, was %d") %
784 784 targetsize)
785 785 self.iterempty = False
786 786
787 787 def fillbuf(self):
788 788 """Ignore target size; read every chunk from iterator until empty."""
789 789 if not self.iterempty:
790 790 collector = cStringIO.StringIO()
791 791 collector.write(self.buf)
792 792 for ch in self.in_iter:
793 793 collector.write(ch)
794 794 self.buf = collector.getvalue()
795 795 self.iterempty = True
796 796
797 797 def read(self, l):
798 798 """Read L bytes of data from the iterator of chunks of data.
799 799 Returns less than L bytes if the iterator runs dry."""
800 800 if l > len(self.buf) and not self.iterempty:
801 801 # Clamp to a multiple of self.targetsize
802 802 targetsize = self.targetsize * ((l // self.targetsize) + 1)
803 803 collector = cStringIO.StringIO()
804 804 collector.write(self.buf)
805 805 collected = len(self.buf)
806 806 for chunk in self.in_iter:
807 807 collector.write(chunk)
808 808 collected += len(chunk)
809 809 if collected >= targetsize:
810 810 break
811 811 if collected < targetsize:
812 812 self.iterempty = True
813 813 self.buf = collector.getvalue()
814 814 s, self.buf = self.buf[:l], buffer(self.buf, l)
815 815 return s
816 816
817 817 def filechunkiter(f, size = 65536):
818 818 """Create a generator that produces all the data in the file size
819 819 (default 65536) bytes at a time. Chunks may be less than size
820 820 bytes if the chunk is the last chunk in the file, or the file is a
821 821 socket or some other type of file that sometimes reads less data
822 822 than is requested."""
823 823 s = f.read(size)
824 824 while len(s) > 0:
825 825 yield s
826 826 s = f.read(size)
827 827
828 828 def makedate():
829 829 lt = time.localtime()
830 830 if lt[8] == 1 and time.daylight:
831 831 tz = time.altzone
832 832 else:
833 833 tz = time.timezone
834 834 return time.mktime(lt), tz
835 835
836 836 def datestr(date=None, format='%a %b %d %H:%M:%S %Y', timezone=True):
837 837 """represent a (unixtime, offset) tuple as a localized time.
838 838 unixtime is seconds since the epoch, and offset is the time zone's
839 839 number of seconds away from UTC. if timezone is false, do not
840 840 append time zone to string."""
841 841 t, tz = date or makedate()
842 842 s = time.strftime(format, time.gmtime(float(t) - tz))
843 843 if timezone:
844 844 s += " %+03d%02d" % (-tz / 3600, ((-tz % 3600) / 60))
845 845 return s
846 846
847 847 def shortuser(user):
848 848 """Return a short representation of a user name or email address."""
849 849 f = user.find('@')
850 850 if f >= 0:
851 851 user = user[:f]
852 852 f = user.find('<')
853 853 if f >= 0:
854 854 user = user[f+1:]
855 855 return user
856 856
857 857 def walkrepos(path):
858 858 '''yield every hg repository under path, recursively.'''
859 859 def errhandler(err):
860 860 if err.filename == path:
861 861 raise err
862 862
863 863 for root, dirs, files in os.walk(path, onerror=errhandler):
864 864 for d in dirs:
865 865 if d == '.hg':
866 866 yield root
867 867 dirs[:] = []
868 868 break
869 869
870 870 _rcpath = None
871 871
872 872 def rcpath():
873 873 '''return hgrc search path. if env var HGRCPATH is set, use it.
874 874 for each item in path, if directory, use files ending in .rc,
875 875 else use item.
876 876 make HGRCPATH empty to only look in .hg/hgrc of current repo.
877 877 if no HGRCPATH, use default os-specific path.'''
878 878 global _rcpath
879 879 if _rcpath is None:
880 880 if 'HGRCPATH' in os.environ:
881 881 _rcpath = []
882 882 for p in os.environ['HGRCPATH'].split(os.pathsep):
883 883 if not p: continue
884 884 if os.path.isdir(p):
885 885 for f in os.listdir(p):
886 886 if f.endswith('.rc'):
887 887 _rcpath.append(os.path.join(p, f))
888 888 else:
889 889 _rcpath.append(p)
890 890 else:
891 891 _rcpath = os_rcpath()
892 892 return _rcpath
General Comments 0
You need to be logged in to leave comments. Login now