##// END OF EJS Templates
fix exception handling on windows....
Vadim Gelfer -
r2054:e18beba5 default
parent child Browse files
Show More
@@ -0,0 +1,171 b''
1 # util_win32.py - utility functions that use win32 API
2 #
3 # Copyright 2005 Matt Mackall <mpm@selenic.com>
4 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
5 #
6 # This software may be used and distributed according to the terms of
7 # the GNU General Public License, incorporated herein by reference.
8
9 # Mark Hammond's win32all package allows better functionality on
10 # Windows. this module overrides definitions in util.py. if not
11 # available, import of this module will fail, and generic code will be
12 # used.
13
14 import win32api
15
16 from demandload import *
17 from i18n import gettext as _
18 demandload(globals(), 'errno os pywintypes win32con win32file win32process')
19 demandload(globals(), 'winerror')
20
21 class WinError(OSError):
22 winerror_map = {
23 winerror.ERROR_ACCESS_DENIED: errno.EACCES,
24 winerror.ERROR_ACCOUNT_DISABLED: errno.EACCES,
25 winerror.ERROR_ACCOUNT_RESTRICTION: errno.EACCES,
26 winerror.ERROR_ALREADY_ASSIGNED: errno.EBUSY,
27 winerror.ERROR_ALREADY_EXISTS: errno.EEXIST,
28 winerror.ERROR_ARITHMETIC_OVERFLOW: errno.ERANGE,
29 winerror.ERROR_BAD_COMMAND: errno.EIO,
30 winerror.ERROR_BAD_DEVICE: errno.ENODEV,
31 winerror.ERROR_BAD_DRIVER_LEVEL: errno.ENXIO,
32 winerror.ERROR_BAD_EXE_FORMAT: errno.ENOEXEC,
33 winerror.ERROR_BAD_FORMAT: errno.ENOEXEC,
34 winerror.ERROR_BAD_LENGTH: errno.EINVAL,
35 winerror.ERROR_BAD_PATHNAME: errno.ENOENT,
36 winerror.ERROR_BAD_PIPE: errno.EPIPE,
37 winerror.ERROR_BAD_UNIT: errno.ENODEV,
38 winerror.ERROR_BAD_USERNAME: errno.EINVAL,
39 winerror.ERROR_BROKEN_PIPE: errno.EPIPE,
40 winerror.ERROR_BUFFER_OVERFLOW: errno.ENAMETOOLONG,
41 winerror.ERROR_BUSY: errno.EBUSY,
42 winerror.ERROR_BUSY_DRIVE: errno.EBUSY,
43 winerror.ERROR_CALL_NOT_IMPLEMENTED: errno.ENOSYS,
44 winerror.ERROR_CANNOT_MAKE: errno.EACCES,
45 winerror.ERROR_CANTOPEN: errno.EIO,
46 winerror.ERROR_CANTREAD: errno.EIO,
47 winerror.ERROR_CANTWRITE: errno.EIO,
48 winerror.ERROR_CRC: errno.EIO,
49 winerror.ERROR_CURRENT_DIRECTORY: errno.EACCES,
50 winerror.ERROR_DEVICE_IN_USE: errno.EBUSY,
51 winerror.ERROR_DEV_NOT_EXIST: errno.ENODEV,
52 winerror.ERROR_DIRECTORY: errno.EINVAL,
53 winerror.ERROR_DIR_NOT_EMPTY: errno.ENOTEMPTY,
54 winerror.ERROR_DISK_CHANGE: errno.EIO,
55 winerror.ERROR_DISK_FULL: errno.ENOSPC,
56 winerror.ERROR_DRIVE_LOCKED: errno.EBUSY,
57 winerror.ERROR_ENVVAR_NOT_FOUND: errno.EINVAL,
58 winerror.ERROR_EXE_MARKED_INVALID: errno.ENOEXEC,
59 winerror.ERROR_FILENAME_EXCED_RANGE: errno.ENAMETOOLONG,
60 winerror.ERROR_FILE_EXISTS: errno.EEXIST,
61 winerror.ERROR_FILE_INVALID: errno.ENODEV,
62 winerror.ERROR_FILE_NOT_FOUND: errno.ENOENT,
63 winerror.ERROR_GEN_FAILURE: errno.EIO,
64 winerror.ERROR_HANDLE_DISK_FULL: errno.ENOSPC,
65 winerror.ERROR_INSUFFICIENT_BUFFER: errno.ENOMEM,
66 winerror.ERROR_INVALID_ACCESS: errno.EACCES,
67 winerror.ERROR_INVALID_ADDRESS: errno.EFAULT,
68 winerror.ERROR_INVALID_BLOCK: errno.EFAULT,
69 winerror.ERROR_INVALID_DATA: errno.EINVAL,
70 winerror.ERROR_INVALID_DRIVE: errno.ENODEV,
71 winerror.ERROR_INVALID_EXE_SIGNATURE: errno.ENOEXEC,
72 winerror.ERROR_INVALID_FLAGS: errno.EINVAL,
73 winerror.ERROR_INVALID_FUNCTION: errno.ENOSYS,
74 winerror.ERROR_INVALID_HANDLE: errno.EBADF,
75 winerror.ERROR_INVALID_LOGON_HOURS: errno.EACCES,
76 winerror.ERROR_INVALID_NAME: errno.EINVAL,
77 winerror.ERROR_INVALID_OWNER: errno.EINVAL,
78 winerror.ERROR_INVALID_PARAMETER: errno.EINVAL,
79 winerror.ERROR_INVALID_PASSWORD: errno.EPERM,
80 winerror.ERROR_INVALID_PRIMARY_GROUP: errno.EINVAL,
81 winerror.ERROR_INVALID_SIGNAL_NUMBER: errno.EINVAL,
82 winerror.ERROR_INVALID_TARGET_HANDLE: errno.EIO,
83 winerror.ERROR_INVALID_WORKSTATION: errno.EACCES,
84 winerror.ERROR_IO_DEVICE: errno.EIO,
85 winerror.ERROR_IO_INCOMPLETE: errno.EINTR,
86 winerror.ERROR_LOCKED: errno.EBUSY,
87 winerror.ERROR_LOCK_VIOLATION: errno.EACCES,
88 winerror.ERROR_LOGON_FAILURE: errno.EACCES,
89 winerror.ERROR_MAPPED_ALIGNMENT: errno.EINVAL,
90 winerror.ERROR_META_EXPANSION_TOO_LONG: errno.E2BIG,
91 winerror.ERROR_MORE_DATA: errno.EPIPE,
92 winerror.ERROR_NEGATIVE_SEEK: errno.ESPIPE,
93 winerror.ERROR_NOACCESS: errno.EFAULT,
94 winerror.ERROR_NONE_MAPPED: errno.EINVAL,
95 winerror.ERROR_NOT_ENOUGH_MEMORY: errno.ENOMEM,
96 winerror.ERROR_NOT_READY: errno.EAGAIN,
97 winerror.ERROR_NOT_SAME_DEVICE: errno.EXDEV,
98 winerror.ERROR_NO_DATA: errno.EPIPE,
99 winerror.ERROR_NO_MORE_SEARCH_HANDLES: errno.EIO,
100 winerror.ERROR_NO_PROC_SLOTS: errno.EAGAIN,
101 winerror.ERROR_NO_SUCH_PRIVILEGE: errno.EACCES,
102 winerror.ERROR_OPEN_FAILED: errno.EIO,
103 winerror.ERROR_OPEN_FILES: errno.EBUSY,
104 winerror.ERROR_OPERATION_ABORTED: errno.EINTR,
105 winerror.ERROR_OUTOFMEMORY: errno.ENOMEM,
106 winerror.ERROR_PASSWORD_EXPIRED: errno.EACCES,
107 winerror.ERROR_PATH_BUSY: errno.EBUSY,
108 winerror.ERROR_PATH_NOT_FOUND: errno.ENOTDIR,
109 winerror.ERROR_PIPE_BUSY: errno.EBUSY,
110 winerror.ERROR_PIPE_CONNECTED: errno.EPIPE,
111 winerror.ERROR_PIPE_LISTENING: errno.EPIPE,
112 winerror.ERROR_PIPE_NOT_CONNECTED: errno.EPIPE,
113 winerror.ERROR_PRIVILEGE_NOT_HELD: errno.EACCES,
114 winerror.ERROR_READ_FAULT: errno.EIO,
115 winerror.ERROR_SEEK: errno.EIO,
116 winerror.ERROR_SEEK_ON_DEVICE: errno.ESPIPE,
117 winerror.ERROR_SHARING_BUFFER_EXCEEDED: errno.ENFILE,
118 winerror.ERROR_SHARING_VIOLATION: errno.EACCES,
119 winerror.ERROR_STACK_OVERFLOW: errno.ENOMEM,
120 winerror.ERROR_SWAPERROR: errno.ENOENT,
121 winerror.ERROR_TOO_MANY_MODULES: errno.EMFILE,
122 winerror.ERROR_TOO_MANY_OPEN_FILES: errno.EMFILE,
123 winerror.ERROR_UNRECOGNIZED_MEDIA: errno.ENXIO,
124 winerror.ERROR_UNRECOGNIZED_VOLUME: errno.ENODEV,
125 winerror.ERROR_WAIT_NO_CHILDREN: errno.ECHILD,
126 winerror.ERROR_WRITE_FAULT: errno.EIO,
127 winerror.ERROR_WRITE_PROTECT: errno.EROFS,
128 }
129
130 def __init__(self, err):
131 self.win_errno, self.win_function, self.win_strerror = err
132 OSError.__init__(self, self.winerror_map.get(self.win_errno, 0),
133 self.win_strerror)
134
135 def os_link(src, dst):
136 # NB will only succeed on NTFS
137 try:
138 win32file.CreateHardLink(dst, src)
139 except pywintypes.error, details:
140 raise WinError(details)
141
142 def nlinks(pathname):
143 """Return number of hardlinks for the given file."""
144 try:
145 fh = win32file.CreateFile(pathname,
146 win32file.GENERIC_READ, win32file.FILE_SHARE_READ,
147 None, win32file.OPEN_EXISTING, 0, None)
148 res = win32file.GetFileInformationByHandle(fh)
149 fh.Close()
150 return res[7]
151 except pywintypes.error:
152 return os.stat(pathname).st_nlink
153
154 def testpid(pid):
155 '''return True if pid is still running or unable to
156 determine, False otherwise'''
157 try:
158 handle = win32api.OpenProcess(
159 win32con.PROCESS_QUERY_INFORMATION, False, pid)
160 if handle:
161 status = win32process.GetExitCodeProcess(handle)
162 return status == win32con.STILL_ACTIVE
163 except pywintypes.error, details:
164 return details[0] != winerror.ERROR_INVALID_PARAMETER
165 return True
166
167 def system_rcpath():
168 '''return default os-specific hgrc search path'''
169 proc = win32api.GetCurrentProcess()
170 filename = win32process.GetModuleFileNameEx(proc, 0)
171 return [os.path.join(os.path.dirname(filename), 'mercurial.ini')]
@@ -515,18 +515,13 b" if os.name == 'nt':"
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"""
@@ -535,43 +530,9 b" if os.name == 'nt':"
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
@@ -597,6 +558,12 b" if os.name == 'nt':"
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
599
560
561 try:
562 # override functions with win32 versions if possible
563 from util_win32 import *
564 except ImportError:
565 pass
566
600 else:
567 else:
601 nulldev = '/dev/null'
568 nulldev = '/dev/null'
602
569
General Comments 0
You need to be logged in to leave comments. Login now