comparison mercurial/util.py @ 2054:e18beba54a7e

fix exception handling on windows. move win32 code into util_win32.py.
author Vadim Gelfer <vadim.gelfer@gmail.com>
date Sat, 08 Apr 2006 14:12:43 -0700
parents e49d0fa38176
children 547ede0123a2
comparison
equal deleted inserted replaced
2053:0f17f54333b8 2054:e18beba54a7e
513 self.close() 513 self.close()
514 raise IOError(errno.EPIPE, 'Broken pipe') 514 raise IOError(errno.EPIPE, 'Broken pipe')
515 515
516 sys.stdout = winstdout(sys.stdout) 516 sys.stdout = winstdout(sys.stdout)
517 517
518 def system_rcpath():
519 return [r'c:\mercurial\mercurial.ini']
520
518 def os_rcpath(): 521 def os_rcpath():
519 '''return default os-specific hgrc search path''' 522 '''return default os-specific hgrc search path'''
520 try: 523 return system_rcpath() + [os.path.join(os.path.expanduser('~'),
521 import win32api, win32process 524 'mercurial.ini')]
522 proc = win32api.GetCurrentProcess()
523 filename = win32process.GetModuleFileNameEx(proc, 0)
524 systemrc = os.path.join(os.path.dirname(filename), 'mercurial.ini')
525 except ImportError:
526 systemrc = r'c:\mercurial\mercurial.ini'
527
528 return [systemrc,
529 os.path.join(os.path.expanduser('~'), 'mercurial.ini')]
530 525
531 def parse_patch_output(output_line): 526 def parse_patch_output(output_line):
532 """parses the output produced by patch and returns the file name""" 527 """parses the output produced by patch and returns the file name"""
533 pf = output_line[14:] 528 pf = output_line[14:]
534 if pf[0] == '`': 529 if pf[0] == '`':
535 pf = pf[1:-1] # Remove the quotes 530 pf = pf[1:-1] # Remove the quotes
536 return pf 531 return pf
537 532
538 try: # Mark Hammond's win32all package allows better functionality on Windows 533 def testpid(pid):
539 import win32api, win32con, win32file, pywintypes 534 '''return False if pid dead, True if running or not known'''
540 535 return True
541 # create hard links using win32file module
542 def os_link(src, dst): # NB will only succeed on NTFS
543 win32file.CreateHardLink(dst, src)
544
545 def nlinks(pathname):
546 """Return number of hardlinks for the given file."""
547 try:
548 fh = win32file.CreateFile(pathname,
549 win32file.GENERIC_READ, win32file.FILE_SHARE_READ,
550 None, win32file.OPEN_EXISTING, 0, None)
551 res = win32file.GetFileInformationByHandle(fh)
552 fh.Close()
553 return res[7]
554 except:
555 return os.stat(pathname).st_nlink
556
557 def testpid(pid):
558 '''return True if pid is still running or unable to
559 determine, False otherwise'''
560 try:
561 import win32process, winerror
562 handle = win32api.OpenProcess(
563 win32con.PROCESS_QUERY_INFORMATION, False, pid)
564 if handle:
565 status = win32process.GetExitCodeProcess(handle)
566 return status == win32con.STILL_ACTIVE
567 except pywintypes.error, details:
568 return details[0] != winerror.ERROR_INVALID_PARAMETER
569 return True
570
571 except ImportError:
572 def testpid(pid):
573 '''return False if pid dead, True if running or not known'''
574 return True
575 536
576 def is_exec(f, last): 537 def is_exec(f, last):
577 return last 538 return last
578 539
579 def set_exec(f, mode): 540 def set_exec(f, mode):
594 makelock = _makelock_file 555 makelock = _makelock_file
595 readlock = _readlock_file 556 readlock = _readlock_file
596 557
597 def explain_exit(code): 558 def explain_exit(code):
598 return _("exited with status %d") % code, code 559 return _("exited with status %d") % code, code
560
561 try:
562 # override functions with win32 versions if possible
563 from util_win32 import *
564 except ImportError:
565 pass
599 566
600 else: 567 else:
601 nulldev = '/dev/null' 568 nulldev = '/dev/null'
602 569
603 def rcfiles(path): 570 def rcfiles(path):