annotate mercurial/win32.py @ 14344:e1db8a00188b

win32.py: more explicit definition of _STD_ERROR_HANDLE
author Adrian Buehlmann <adrian@cadifra.com>
date Sun, 15 May 2011 21:27:59 +0200
parents 4d684d8210a1
children bf9a105aed0a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8226
8b2cd04a6e97 put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents: 8225
diff changeset
1 # win32.py - utility functions that use win32 API
8b2cd04a6e97 put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents: 8225
diff changeset
2 #
8b2cd04a6e97 put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents: 8225
diff changeset
3 # Copyright 2005-2009 Matt Mackall <mpm@selenic.com> and others
8b2cd04a6e97 put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents: 8225
diff changeset
4 #
8b2cd04a6e97 put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents: 8225
diff changeset
5 # This software may be used and distributed according to the terms of the
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 10219
diff changeset
6 # GNU General Public License version 2 or any later version.
8227
0a9542703300 turn some comments back into module docstrings
Martin Geisler <mg@lazybytes.net>
parents: 8226
diff changeset
7
13377
4ac565a30e84 win32: move system_rcpath_win32() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13376
diff changeset
8 import encoding
13775
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
9 import ctypes, errno, os, struct, subprocess, random
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
10
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
11 _kernel32 = ctypes.windll.kernel32
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
12
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
13 _BOOL = ctypes.c_long
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
14 _WORD = ctypes.c_ushort
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
15 _DWORD = ctypes.c_ulong
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
16 _LPCSTR = _LPSTR = ctypes.c_char_p
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
17 _HANDLE = ctypes.c_void_p
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
18 _HWND = _HANDLE
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
19
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
20 _INVALID_HANDLE_VALUE = -1
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
21
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
22 # GetLastError
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
23 _ERROR_SUCCESS = 0
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
24 _ERROR_INVALID_PARAMETER = 87
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
25 _ERROR_INSUFFICIENT_BUFFER = 122
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
26
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
27 # WPARAM is defined as UINT_PTR (unsigned type)
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
28 # LPARAM is defined as LONG_PTR (signed type)
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
29 if ctypes.sizeof(ctypes.c_long) == ctypes.sizeof(ctypes.c_void_p):
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
30 _WPARAM = ctypes.c_ulong
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
31 _LPARAM = ctypes.c_long
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
32 elif ctypes.sizeof(ctypes.c_longlong) == ctypes.sizeof(ctypes.c_void_p):
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
33 _WPARAM = ctypes.c_ulonglong
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
34 _LPARAM = ctypes.c_longlong
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
35
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
36 class _FILETIME(ctypes.Structure):
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
37 _fields_ = [('dwLowDateTime', _DWORD),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
38 ('dwHighDateTime', _DWORD)]
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
39
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
40 class _BY_HANDLE_FILE_INFORMATION(ctypes.Structure):
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
41 _fields_ = [('dwFileAttributes', _DWORD),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
42 ('ftCreationTime', _FILETIME),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
43 ('ftLastAccessTime', _FILETIME),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
44 ('ftLastWriteTime', _FILETIME),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
45 ('dwVolumeSerialNumber', _DWORD),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
46 ('nFileSizeHigh', _DWORD),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
47 ('nFileSizeLow', _DWORD),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
48 ('nNumberOfLinks', _DWORD),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
49 ('nFileIndexHigh', _DWORD),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
50 ('nFileIndexLow', _DWORD)]
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
51
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
52 # CreateFile
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
53 _FILE_SHARE_READ = 0x00000001
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
54 _FILE_SHARE_WRITE = 0x00000002
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
55 _FILE_SHARE_DELETE = 0x00000004
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
56
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
57 _OPEN_EXISTING = 3
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
58
13776
a2f0fdb1e488 win32: remove READONLY attribute on unlink
Adrian Buehlmann <adrian@cadifra.com>
parents: 13775
diff changeset
59 # SetFileAttributes
a2f0fdb1e488 win32: remove READONLY attribute on unlink
Adrian Buehlmann <adrian@cadifra.com>
parents: 13775
diff changeset
60 _FILE_ATTRIBUTE_NORMAL = 0x80
13795
43b5fe18ea6c set NOT_CONTENT_INDEXED on .hg dir (issue2694)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13776
diff changeset
61 _FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 0x2000
13776
a2f0fdb1e488 win32: remove READONLY attribute on unlink
Adrian Buehlmann <adrian@cadifra.com>
parents: 13775
diff changeset
62
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
63 # Process Security and Access Rights
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
64 _PROCESS_QUERY_INFORMATION = 0x0400
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
65
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
66 # GetExitCodeProcess
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
67 _STILL_ACTIVE = 259
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
68
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
69 # registry
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
70 _HKEY_CURRENT_USER = 0x80000001L
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
71 _HKEY_LOCAL_MACHINE = 0x80000002L
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
72 _KEY_READ = 0x20019
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
73 _REG_SZ = 1
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
74 _REG_DWORD = 4
8227
0a9542703300 turn some comments back into module docstrings
Martin Geisler <mg@lazybytes.net>
parents: 8226
diff changeset
75
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
76 class _STARTUPINFO(ctypes.Structure):
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
77 _fields_ = [('cb', _DWORD),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
78 ('lpReserved', _LPSTR),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
79 ('lpDesktop', _LPSTR),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
80 ('lpTitle', _LPSTR),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
81 ('dwX', _DWORD),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
82 ('dwY', _DWORD),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
83 ('dwXSize', _DWORD),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
84 ('dwYSize', _DWORD),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
85 ('dwXCountChars', _DWORD),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
86 ('dwYCountChars', _DWORD),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
87 ('dwFillAttribute', _DWORD),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
88 ('dwFlags', _DWORD),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
89 ('wShowWindow', _WORD),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
90 ('cbReserved2', _WORD),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
91 ('lpReserved2', ctypes.c_char_p),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
92 ('hStdInput', _HANDLE),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
93 ('hStdOutput', _HANDLE),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
94 ('hStdError', _HANDLE)]
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
95
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
96 class _PROCESS_INFORMATION(ctypes.Structure):
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
97 _fields_ = [('hProcess', _HANDLE),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
98 ('hThread', _HANDLE),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
99 ('dwProcessId', _DWORD),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
100 ('dwThreadId', _DWORD)]
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
101
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
102 _DETACHED_PROCESS = 0x00000008
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
103 _STARTF_USESHOWWINDOW = 0x00000001
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
104 _SW_HIDE = 0
2054
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
105
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
106 class _COORD(ctypes.Structure):
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
107 _fields_ = [('X', ctypes.c_short),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
108 ('Y', ctypes.c_short)]
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
109
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
110 class _SMALL_RECT(ctypes.Structure):
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
111 _fields_ = [('Left', ctypes.c_short),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
112 ('Top', ctypes.c_short),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
113 ('Right', ctypes.c_short),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
114 ('Bottom', ctypes.c_short)]
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
115
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
116 class _CONSOLE_SCREEN_BUFFER_INFO(ctypes.Structure):
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
117 _fields_ = [('dwSize', _COORD),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
118 ('dwCursorPosition', _COORD),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
119 ('wAttributes', _WORD),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
120 ('srWindow', _SMALL_RECT),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
121 ('dwMaximumWindowSize', _COORD)]
2054
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
122
14344
e1db8a00188b win32.py: more explicit definition of _STD_ERROR_HANDLE
Adrian Buehlmann <adrian@cadifra.com>
parents: 14237
diff changeset
123 _STD_ERROR_HANDLE = _DWORD(-12).value
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
124
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
125 def _raiseoserror(name):
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
126 err = ctypes.WinError()
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
127 raise OSError(err.errno, '%s: %s' % (name, err.strerror))
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
128
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
129 def _getfileinfo(name):
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
130 fh = _kernel32.CreateFileA(name, 0,
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
131 _FILE_SHARE_READ | _FILE_SHARE_WRITE | _FILE_SHARE_DELETE,
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
132 None, _OPEN_EXISTING, 0, None)
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
133 if fh == _INVALID_HANDLE_VALUE:
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
134 _raiseoserror(name)
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
135 try:
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
136 fi = _BY_HANDLE_FILE_INFORMATION()
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
137 if not _kernel32.GetFileInformationByHandle(fh, ctypes.byref(fi)):
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
138 _raiseoserror(name)
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
139 return fi
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
140 finally:
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
141 _kernel32.CloseHandle(fh)
2054
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
142
14235
b9e1b041744f rename util.os_link to oslink
Adrian Buehlmann <adrian@cadifra.com>
parents: 14230
diff changeset
143 def oslink(src, dst):
13976
9ca1ff3d4f8c win32: Wine doesn't know about hardlinks
Matt Mackall <mpm@selenic.com>
parents: 13795
diff changeset
144 try:
9ca1ff3d4f8c win32: Wine doesn't know about hardlinks
Matt Mackall <mpm@selenic.com>
parents: 13795
diff changeset
145 if not _kernel32.CreateHardLinkA(dst, src, None):
9ca1ff3d4f8c win32: Wine doesn't know about hardlinks
Matt Mackall <mpm@selenic.com>
parents: 13795
diff changeset
146 _raiseoserror(src)
9ca1ff3d4f8c win32: Wine doesn't know about hardlinks
Matt Mackall <mpm@selenic.com>
parents: 13795
diff changeset
147 except AttributeError: # Wine doesn't support this function
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
148 _raiseoserror(src)
2054
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
149
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
150 def nlinks(name):
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
151 '''return number of hardlinks for the given file'''
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
152 return _getfileinfo(name).nNumberOfLinks
2054
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
153
10218
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
154 def samefile(fpath1, fpath2):
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
155 '''Returns whether fpath1 and fpath2 refer to the same file. This is only
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
156 guaranteed to work for files, not directories.'''
10218
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
157 res1 = _getfileinfo(fpath1)
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
158 res2 = _getfileinfo(fpath2)
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
159 return (res1.dwVolumeSerialNumber == res2.dwVolumeSerialNumber
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
160 and res1.nFileIndexHigh == res2.nFileIndexHigh
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
161 and res1.nFileIndexLow == res2.nFileIndexLow)
10218
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
162
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
163 def samedevice(fpath1, fpath2):
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
164 '''Returns whether fpath1 and fpath2 are on the same device. This is only
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
165 guaranteed to work for files, not directories.'''
10218
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
166 res1 = _getfileinfo(fpath1)
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
167 res2 = _getfileinfo(fpath2)
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
168 return res1.dwVolumeSerialNumber == res2.dwVolumeSerialNumber
10218
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
169
2054
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
170 def testpid(pid):
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
171 '''return True if pid is still running or unable to
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
172 determine, False otherwise'''
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
173 h = _kernel32.OpenProcess(_PROCESS_QUERY_INFORMATION, False, pid)
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
174 if h:
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
175 try:
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
176 status = _DWORD()
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
177 if _kernel32.GetExitCodeProcess(h, ctypes.byref(status)):
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
178 return status.value == _STILL_ACTIVE
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
179 finally:
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
180 _kernel32.CloseHandle(h)
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
181 return _kernel32.GetLastError() != _ERROR_INVALID_PARAMETER
2054
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
182
14230
d51630301241 rename util.lookup_reg to lookupreg
Adrian Buehlmann <adrian@cadifra.com>
parents: 13976
diff changeset
183 def lookupreg(key, valname=None, scope=None):
6006
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
184 ''' Look up a key/value name in the Windows registry.
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
185
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
186 valname: value name. If unspecified, the default value for the key
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
187 is used.
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
188 scope: optionally specify scope for registry lookup, this can be
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
189 a sequence of scopes to look up in order. Default (CURRENT_USER,
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
190 LOCAL_MACHINE).
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
191 '''
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
192 adv = ctypes.windll.advapi32
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
193 byref = ctypes.byref
6006
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
194 if scope is None:
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
195 scope = (_HKEY_CURRENT_USER, _HKEY_LOCAL_MACHINE)
6006
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
196 elif not isinstance(scope, (list, tuple)):
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
197 scope = (scope,)
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
198 for s in scope:
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
199 kh = _HANDLE()
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
200 res = adv.RegOpenKeyExA(s, key, 0, _KEY_READ, ctypes.byref(kh))
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
201 if res != _ERROR_SUCCESS:
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
202 continue
6880
892806b3fc0f util: disinfect lookup_reg strings (issue1126)
Matt Mackall <mpm@selenic.com>
parents: 6216
diff changeset
203 try:
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
204 size = _DWORD(600)
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
205 type = _DWORD()
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
206 buf = ctypes.create_string_buffer(size.value + 1)
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
207 res = adv.RegQueryValueExA(kh.value, valname, None,
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
208 byref(type), buf, byref(size))
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
209 if res != _ERROR_SUCCESS:
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
210 continue
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
211 if type.value == _REG_SZ:
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
212 # never let a Unicode string escape into the wild
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
213 return encoding.tolocal(buf.value.encode('UTF-8'))
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
214 elif type.value == _REG_DWORD:
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
215 fmt = '<L'
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
216 s = ctypes.string_at(byref(buf), struct.calcsize(fmt))
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
217 return struct.unpack(fmt, s)[0]
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
218 finally:
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
219 adv.RegCloseKey(kh.value)
6006
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
220
14236
e949a008999d rename util.executable_path to executablepath
Adrian Buehlmann <adrian@cadifra.com>
parents: 14235
diff changeset
221 def executablepath():
13376
60b5c6c3fd12 win32: new function executable_path
Adrian Buehlmann <adrian@cadifra.com>
parents: 13375
diff changeset
222 '''return full path of hg.exe'''
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
223 size = 600
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
224 buf = ctypes.create_string_buffer(size + 1)
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
225 len = _kernel32.GetModuleFileNameA(None, ctypes.byref(buf), size)
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
226 if len == 0:
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
227 raise ctypes.WinError()
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
228 elif len == size:
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
229 raise ctypes.WinError(_ERROR_INSUFFICIENT_BUFFER)
13376
60b5c6c3fd12 win32: new function executable_path
Adrian Buehlmann <adrian@cadifra.com>
parents: 13375
diff changeset
230 return buf.value
60b5c6c3fd12 win32: new function executable_path
Adrian Buehlmann <adrian@cadifra.com>
parents: 13375
diff changeset
231
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents: 7778
diff changeset
232 def getuser():
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents: 7778
diff changeset
233 '''return name of current user'''
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
234 adv = ctypes.windll.advapi32
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
235 size = _DWORD(300)
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
236 buf = ctypes.create_string_buffer(size.value + 1)
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
237 if not adv.GetUserNameA(ctypes.byref(buf), ctypes.byref(size)):
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
238 raise ctypes.WinError()
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
239 return buf.value
4672
272c0a09b203 Handle CTRL+C in serve under Windows.
Marcos Chaves <marcos.nospam@gmail.com>
parents: 4407
diff changeset
240
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
241 _SIGNAL_HANDLER = ctypes.WINFUNCTYPE(_BOOL, _DWORD)
14237
4d684d8210a1 rename util.set_signal_handler to setsignalhandler
Adrian Buehlmann <adrian@cadifra.com>
parents: 14236
diff changeset
242 _signalhandler = []
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
243
14237
4d684d8210a1 rename util.set_signal_handler to setsignalhandler
Adrian Buehlmann <adrian@cadifra.com>
parents: 14236
diff changeset
244 def setsignalhandler():
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
245 '''Register a termination handler for console events including
4672
272c0a09b203 Handle CTRL+C in serve under Windows.
Marcos Chaves <marcos.nospam@gmail.com>
parents: 4407
diff changeset
246 CTRL+C. python signal handlers do not work well with socket
272c0a09b203 Handle CTRL+C in serve under Windows.
Marcos Chaves <marcos.nospam@gmail.com>
parents: 4407
diff changeset
247 operations.
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
248 '''
4672
272c0a09b203 Handle CTRL+C in serve under Windows.
Marcos Chaves <marcos.nospam@gmail.com>
parents: 4407
diff changeset
249 def handler(event):
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
250 _kernel32.ExitProcess(1)
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
251
14237
4d684d8210a1 rename util.set_signal_handler to setsignalhandler
Adrian Buehlmann <adrian@cadifra.com>
parents: 14236
diff changeset
252 if _signalhandler:
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
253 return # already registered
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
254 h = _SIGNAL_HANDLER(handler)
14237
4d684d8210a1 rename util.set_signal_handler to setsignalhandler
Adrian Buehlmann <adrian@cadifra.com>
parents: 14236
diff changeset
255 _signalhandler.append(h) # needed to prevent garbage collection
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
256 if not _kernel32.SetConsoleCtrlHandler(h, True):
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
257 raise ctypes.WinError()
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
258
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
259 _WNDENUMPROC = ctypes.WINFUNCTYPE(_BOOL, _HWND, _LPARAM)
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents: 7778
diff changeset
260
10240
3af4b39afe2a cmdutil: hide child window created by win32 spawndetached()
Patrick Mezard <pmezard@gmail.com>
parents: 10219
diff changeset
261 def hidewindow():
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
262 user32 = ctypes.windll.user32
10240
3af4b39afe2a cmdutil: hide child window created by win32 spawndetached()
Patrick Mezard <pmezard@gmail.com>
parents: 10219
diff changeset
263
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
264 def callback(hwnd, pid):
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
265 wpid = _DWORD()
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
266 user32.GetWindowThreadProcessId(hwnd, ctypes.byref(wpid))
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
267 if pid == wpid.value:
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
268 user32.ShowWindow(hwnd, _SW_HIDE)
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
269 return False # stop enumerating windows
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
270 return True
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
271
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
272 pid = _kernel32.GetCurrentProcessId()
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
273 user32.EnumWindows(_WNDENUMPROC(callback), pid)
11012
81631f0cf13b win32: detect console width on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10388
diff changeset
274
12689
c52c629ce19e termwidth: move to ui.ui from util
Augie Fackler <durin42@gmail.com>
parents: 12401
diff changeset
275 def termwidth():
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
276 # cmd.exe does not handle CR like a unix console, the CR is
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
277 # counted in the line length. On 80 columns consoles, if 80
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
278 # characters are written, the following CR won't apply on the
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
279 # current line but on the new one. Keep room for it.
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
280 width = 79
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
281 # Query stderr to avoid problems with redirections
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
282 screenbuf = _kernel32.GetStdHandle(
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
283 _STD_ERROR_HANDLE) # don't close the handle returned
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
284 if screenbuf is None or screenbuf == _INVALID_HANDLE_VALUE:
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
285 return width
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
286 csbi = _CONSOLE_SCREEN_BUFFER_INFO()
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
287 if not _kernel32.GetConsoleScreenBufferInfo(
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
288 screenbuf, ctypes.byref(csbi)):
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
289 return width
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
290 width = csbi.srWindow.Right - csbi.srWindow.Left
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
291 return width
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
292
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
293 def spawndetached(args):
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
294 # No standard library function really spawns a fully detached
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
295 # process under win32 because they allocate pipes or other objects
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
296 # to handle standard streams communications. Passing these objects
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
297 # to the child process requires handle inheritance to be enabled
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
298 # which makes really detached processes impossible.
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
299 si = _STARTUPINFO()
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
300 si.cb = ctypes.sizeof(_STARTUPINFO)
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
301 si.dwFlags = _STARTF_USESHOWWINDOW
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
302 si.wShowWindow = _SW_HIDE
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
303
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
304 pi = _PROCESS_INFORMATION()
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
305
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
306 env = ''
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
307 for k in os.environ:
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
308 env += "%s=%s\0" % (k, os.environ[k])
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
309 if not env:
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
310 env = '\0'
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
311 env += '\0'
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
312
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
313 args = subprocess.list2cmdline(args)
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
314 # Not running the command in shell mode makes python26 hang when
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
315 # writing to hgweb output socket.
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
316 comspec = os.environ.get("COMSPEC", "cmd.exe")
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
317 args = comspec + " /c " + args
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
318
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
319 res = _kernel32.CreateProcessA(
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
320 None, args, None, None, False, _DETACHED_PROCESS,
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
321 env, os.getcwd(), ctypes.byref(si), ctypes.byref(pi))
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
322 if not res:
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
323 raise ctypes.WinError()
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
324
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
325 return pi.dwProcessId
13775
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
326
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
327 def unlink(f):
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
328 '''try to implement POSIX' unlink semantics on Windows'''
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
329
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
330 # POSIX allows to unlink and rename open files. Windows has serious
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
331 # problems with doing that:
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
332 # - Calling os.unlink (or os.rename) on a file f fails if f or any
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
333 # hardlinked copy of f has been opened with Python's open(). There is no
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
334 # way such a file can be deleted or renamed on Windows (other than
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
335 # scheduling the delete or rename for the next reboot).
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
336 # - Calling os.unlink on a file that has been opened with Mercurial's
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
337 # posixfile (or comparable methods) will delay the actual deletion of
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
338 # the file for as long as the file is held open. The filename is blocked
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
339 # during that time and cannot be used for recreating a new file under
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
340 # that same name ("zombie file"). Directories containing such zombie files
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
341 # cannot be removed or moved.
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
342 # A file that has been opened with posixfile can be renamed, so we rename
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
343 # f to a random temporary name before calling os.unlink on it. This allows
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
344 # callers to recreate f immediately while having other readers do their
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
345 # implicit zombie filename blocking on a temporary name.
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
346
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
347 for tries in xrange(10):
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
348 temp = '%s-%08x' % (f, random.randint(0, 0xffffffff))
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
349 try:
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
350 os.rename(f, temp) # raises OSError EEXIST if temp exists
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
351 break
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
352 except OSError, e:
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
353 if e.errno != errno.EEXIST:
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
354 raise
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
355 else:
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
356 raise IOError, (errno.EEXIST, "No usable temporary filename found")
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
357
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
358 try:
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
359 os.unlink(temp)
13776
a2f0fdb1e488 win32: remove READONLY attribute on unlink
Adrian Buehlmann <adrian@cadifra.com>
parents: 13775
diff changeset
360 except OSError:
a2f0fdb1e488 win32: remove READONLY attribute on unlink
Adrian Buehlmann <adrian@cadifra.com>
parents: 13775
diff changeset
361 # The unlink might have failed because the READONLY attribute may heave
a2f0fdb1e488 win32: remove READONLY attribute on unlink
Adrian Buehlmann <adrian@cadifra.com>
parents: 13775
diff changeset
362 # been set on the original file. Rename works fine with READONLY set,
a2f0fdb1e488 win32: remove READONLY attribute on unlink
Adrian Buehlmann <adrian@cadifra.com>
parents: 13775
diff changeset
363 # but not os.unlink. Reset all attributes and try again.
a2f0fdb1e488 win32: remove READONLY attribute on unlink
Adrian Buehlmann <adrian@cadifra.com>
parents: 13775
diff changeset
364 _kernel32.SetFileAttributesA(temp, _FILE_ATTRIBUTE_NORMAL)
a2f0fdb1e488 win32: remove READONLY attribute on unlink
Adrian Buehlmann <adrian@cadifra.com>
parents: 13775
diff changeset
365 try:
a2f0fdb1e488 win32: remove READONLY attribute on unlink
Adrian Buehlmann <adrian@cadifra.com>
parents: 13775
diff changeset
366 os.unlink(temp)
a2f0fdb1e488 win32: remove READONLY attribute on unlink
Adrian Buehlmann <adrian@cadifra.com>
parents: 13775
diff changeset
367 except OSError:
a2f0fdb1e488 win32: remove READONLY attribute on unlink
Adrian Buehlmann <adrian@cadifra.com>
parents: 13775
diff changeset
368 # The unlink might have failed due to some very rude AV-Scanners.
a2f0fdb1e488 win32: remove READONLY attribute on unlink
Adrian Buehlmann <adrian@cadifra.com>
parents: 13775
diff changeset
369 # Leaking a tempfile is the lesser evil than aborting here and
a2f0fdb1e488 win32: remove READONLY attribute on unlink
Adrian Buehlmann <adrian@cadifra.com>
parents: 13775
diff changeset
370 # leaving some potentially serious inconsistencies.
a2f0fdb1e488 win32: remove READONLY attribute on unlink
Adrian Buehlmann <adrian@cadifra.com>
parents: 13775
diff changeset
371 pass
13795
43b5fe18ea6c set NOT_CONTENT_INDEXED on .hg dir (issue2694)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13776
diff changeset
372
43b5fe18ea6c set NOT_CONTENT_INDEXED on .hg dir (issue2694)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13776
diff changeset
373 def makedir(path, notindexed):
43b5fe18ea6c set NOT_CONTENT_INDEXED on .hg dir (issue2694)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13776
diff changeset
374 os.mkdir(path)
43b5fe18ea6c set NOT_CONTENT_INDEXED on .hg dir (issue2694)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13776
diff changeset
375 if notindexed:
43b5fe18ea6c set NOT_CONTENT_INDEXED on .hg dir (issue2694)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13776
diff changeset
376 _kernel32.SetFileAttributesA(path, _FILE_ATTRIBUTE_NOT_CONTENT_INDEXED)