Mercurial > hg > mercurial-crew-with-dirclash
comparison mercurial/util.py @ 1680:c21b54f7f7b8
Merge with crew
author | Benoit Boissinot <benoit.boissinot@ens-lyon.org> |
---|---|
date | Wed, 01 Feb 2006 19:18:15 +0100 |
parents | ae61937c61c5 |
children | 7596611ab3d5 b0f6af327fd4 e4abeafd6eb1 |
comparison
equal
deleted
inserted
replaced
1679:675ca845c2f8 | 1680:c21b54f7f7b8 |
---|---|
11 """ | 11 """ |
12 | 12 |
13 import os, errno | 13 import os, errno |
14 from i18n import gettext as _ | 14 from i18n import gettext as _ |
15 from demandload import * | 15 from demandload import * |
16 demandload(globals(), "re cStringIO shutil popen2 sys tempfile threading time") | 16 demandload(globals(), "cStringIO errno popen2 re shutil sys tempfile") |
17 demandload(globals(), "threading time") | |
17 | 18 |
18 def pipefilter(s, cmd): | 19 def pipefilter(s, cmd): |
19 '''filter string S through command CMD, returning its output''' | 20 '''filter string S through command CMD, returning its output''' |
20 (pout, pin) = popen2.popen2(cmd, -1, 'b') | 21 (pout, pin) = popen2.popen2(cmd, -1, 'b') |
21 def writer(): | 22 def writer(): |
188 elif name == root: | 189 elif name == root: |
189 return '' | 190 return '' |
190 else: | 191 else: |
191 raise Abort('%s not under root' % myname) | 192 raise Abort('%s not under root' % myname) |
192 | 193 |
193 def matcher(canonroot, cwd='', names=['.'], inc=[], exc=[], head=''): | 194 def matcher(canonroot, cwd='', names=['.'], inc=[], exc=[], head='', src=None): |
194 return _matcher(canonroot, cwd, names, inc, exc, head, 'glob') | 195 return _matcher(canonroot, cwd, names, inc, exc, head, 'glob', src) |
195 | 196 |
196 def cmdmatcher(canonroot, cwd='', names=['.'], inc=[], exc=[], head=''): | 197 def cmdmatcher(canonroot, cwd='', names=['.'], inc=[], exc=[], head='', src=None): |
197 if os.name == 'nt': | 198 if os.name == 'nt': |
198 dflt_pat = 'glob' | 199 dflt_pat = 'glob' |
199 else: | 200 else: |
200 dflt_pat = 'relpath' | 201 dflt_pat = 'relpath' |
201 return _matcher(canonroot, cwd, names, inc, exc, head, dflt_pat) | 202 return _matcher(canonroot, cwd, names, inc, exc, head, dflt_pat, src) |
202 | 203 |
203 def _matcher(canonroot, cwd, names, inc, exc, head, dflt_pat): | 204 def _matcher(canonroot, cwd, names, inc, exc, head, dflt_pat, src): |
204 """build a function to match a set of file patterns | 205 """build a function to match a set of file patterns |
205 | 206 |
206 arguments: | 207 arguments: |
207 canonroot - the canonical root of the tree you're matching against | 208 canonroot - the canonical root of the tree you're matching against |
208 cwd - the current working directory, if relevant | 209 cwd - the current working directory, if relevant |
259 for k, p in pats: | 260 for k, p in pats: |
260 try: | 261 try: |
261 pat = '(?:%s)' % regex(k, p, tail) | 262 pat = '(?:%s)' % regex(k, p, tail) |
262 matches.append(re.compile(pat).match) | 263 matches.append(re.compile(pat).match) |
263 except re.error: | 264 except re.error: |
264 raise Abort("invalid pattern: %s:%s" % (k, p)) | 265 if src: raise Abort("%s: invalid pattern (%s): %s" % (src, k, p)) |
266 else: raise Abort("invalid pattern (%s): %s" % (k, p)) | |
265 | 267 |
266 def buildfn(text): | 268 def buildfn(text): |
267 for m in matches: | 269 for m in matches: |
268 r = m(text) | 270 r = m(text) |
269 if r: | 271 if r: |
355 if hardlink: | 357 if hardlink: |
356 try: | 358 try: |
357 os_link(src, dst) | 359 os_link(src, dst) |
358 except: | 360 except: |
359 hardlink = False | 361 hardlink = False |
360 shutil.copy2(src, dst) | 362 shutil.copy(src, dst) |
361 else: | 363 else: |
362 shutil.copy2(src, dst) | 364 shutil.copy(src, dst) |
363 | 365 |
364 def opener(base): | 366 def opener(base): |
365 """ | 367 """ |
366 return a function that opens files relative to base | 368 return a function that opens files relative to base |
367 | 369 |
440 | 442 |
441 # Platform specific variants | 443 # Platform specific variants |
442 if os.name == 'nt': | 444 if os.name == 'nt': |
443 demandload(globals(), "msvcrt") | 445 demandload(globals(), "msvcrt") |
444 nulldev = 'NUL:' | 446 nulldev = 'NUL:' |
445 | 447 |
448 class winstdout: | |
449 '''stdout on windows misbehaves if sent through a pipe''' | |
450 | |
451 def __init__(self, fp): | |
452 self.fp = fp | |
453 | |
454 def __getattr__(self, key): | |
455 return getattr(self.fp, key) | |
456 | |
457 def close(self): | |
458 try: | |
459 self.fp.close() | |
460 except: pass | |
461 | |
462 def write(self, s): | |
463 try: | |
464 return self.fp.write(s) | |
465 except IOError, inst: | |
466 if inst.errno != 0: raise | |
467 self.close() | |
468 raise IOError(errno.EPIPE, 'Broken pipe') | |
469 | |
470 sys.stdout = winstdout(sys.stdout) | |
471 | |
446 try: | 472 try: |
447 import win32api, win32process | 473 import win32api, win32process |
448 filename = win32process.GetModuleFileNameEx(win32api.GetCurrentProcess(), 0) | 474 filename = win32process.GetModuleFileNameEx(win32api.GetCurrentProcess(), 0) |
449 systemrc = os.path.join(os.path.dirname(filename), 'mercurial.ini') | 475 systemrc = os.path.join(os.path.dirname(filename), 'mercurial.ini') |
450 | 476 |
451 except ImportError: | 477 except ImportError: |
452 systemrc = r'c:\mercurial\mercurial.ini' | 478 systemrc = r'c:\mercurial\mercurial.ini' |
453 pass | 479 pass |
454 | 480 |
455 rcpath = (systemrc, | 481 rcpath = (systemrc, |
516 try: | 542 try: |
517 rcs.extend([os.path.join(rcdir, f) for f in os.listdir(rcdir) | 543 rcs.extend([os.path.join(rcdir, f) for f in os.listdir(rcdir) |
518 if f.endswith(".rc")]) | 544 if f.endswith(".rc")]) |
519 except OSError, inst: pass | 545 except OSError, inst: pass |
520 return rcs | 546 return rcs |
521 rcpath = rcfiles(os.path.dirname(sys.argv[0]) + '/../etc/mercurial') | 547 rcpath = [] |
548 if len(sys.argv) > 0: | |
549 rcpath.extend(rcfiles(os.path.dirname(sys.argv[0]) + '/../etc/mercurial')) | |
522 rcpath.extend(rcfiles('/etc/mercurial')) | 550 rcpath.extend(rcfiles('/etc/mercurial')) |
523 rcpath.append(os.path.expanduser('~/.hgrc')) | 551 rcpath.append(os.path.expanduser('~/.hgrc')) |
524 rcpath = [os.path.normpath(f) for f in rcpath] | 552 rcpath = [os.path.normpath(f) for f in rcpath] |
525 | 553 |
526 def parse_patch_output(output_line): | 554 def parse_patch_output(output_line): |
527 """parses the output produced by patch and returns the file name""" | 555 """parses the output produced by patch and returns the file name""" |
528 return output_line[14:] | 556 pf = output_line[14:] |
557 if pf.startswith("'") and pf.endswith("'") and pf.find(" ") >= 0: | |
558 pf = pf[1:-1] # Remove the quotes | |
559 return pf | |
529 | 560 |
530 def is_exec(f, last): | 561 def is_exec(f, last): |
531 """check whether a file is executable""" | 562 """check whether a file is executable""" |
532 return (os.stat(f).st_mode & 0100 != 0) | 563 return (os.stat(f).st_mode & 0100 != 0) |
533 | 564 |