comparison mercurial/win32.py @ 43076:2372284d9457

formatting: blacken the codebase This is using my patch to black (https://github.com/psf/black/pull/826) so we don't un-wrap collection literals. Done with: hg files 'set:**.py - mercurial/thirdparty/** - "contrib/python-zstandard/**"' | xargs black -S # skip-blame mass-reformatting only # no-check-commit reformats foo_bar functions Differential Revision: https://phab.mercurial-scm.org/D6971
author Augie Fackler <augie@google.com>
date Sun, 06 Oct 2019 09:45:02 -0400
parents 576474baa209
children 687b865b95ad
comparison
equal deleted inserted replaced
43075:57875cf423c9 43076:2372284d9457
53 _LPARAM = ctypes.c_long 53 _LPARAM = ctypes.c_long
54 elif ctypes.sizeof(ctypes.c_longlong) == ctypes.sizeof(ctypes.c_void_p): 54 elif ctypes.sizeof(ctypes.c_longlong) == ctypes.sizeof(ctypes.c_void_p):
55 _WPARAM = ctypes.c_ulonglong 55 _WPARAM = ctypes.c_ulonglong
56 _LPARAM = ctypes.c_longlong 56 _LPARAM = ctypes.c_longlong
57 57
58
58 class _FILETIME(ctypes.Structure): 59 class _FILETIME(ctypes.Structure):
59 _fields_ = [(r'dwLowDateTime', _DWORD), 60 _fields_ = [(r'dwLowDateTime', _DWORD), (r'dwHighDateTime', _DWORD)]
60 (r'dwHighDateTime', _DWORD)] 61
61 62
62 class _BY_HANDLE_FILE_INFORMATION(ctypes.Structure): 63 class _BY_HANDLE_FILE_INFORMATION(ctypes.Structure):
63 _fields_ = [(r'dwFileAttributes', _DWORD), 64 _fields_ = [
64 (r'ftCreationTime', _FILETIME), 65 (r'dwFileAttributes', _DWORD),
65 (r'ftLastAccessTime', _FILETIME), 66 (r'ftCreationTime', _FILETIME),
66 (r'ftLastWriteTime', _FILETIME), 67 (r'ftLastAccessTime', _FILETIME),
67 (r'dwVolumeSerialNumber', _DWORD), 68 (r'ftLastWriteTime', _FILETIME),
68 (r'nFileSizeHigh', _DWORD), 69 (r'dwVolumeSerialNumber', _DWORD),
69 (r'nFileSizeLow', _DWORD), 70 (r'nFileSizeHigh', _DWORD),
70 (r'nNumberOfLinks', _DWORD), 71 (r'nFileSizeLow', _DWORD),
71 (r'nFileIndexHigh', _DWORD), 72 (r'nNumberOfLinks', _DWORD),
72 (r'nFileIndexLow', _DWORD)] 73 (r'nFileIndexHigh', _DWORD),
74 (r'nFileIndexLow', _DWORD),
75 ]
76
73 77
74 # CreateFile 78 # CreateFile
75 _FILE_SHARE_READ = 0x00000001 79 _FILE_SHARE_READ = 0x00000001
76 _FILE_SHARE_WRITE = 0x00000002 80 _FILE_SHARE_WRITE = 0x00000002
77 _FILE_SHARE_DELETE = 0x00000004 81 _FILE_SHARE_DELETE = 0x00000004
88 _PROCESS_QUERY_INFORMATION = 0x0400 92 _PROCESS_QUERY_INFORMATION = 0x0400
89 93
90 # GetExitCodeProcess 94 # GetExitCodeProcess
91 _STILL_ACTIVE = 259 95 _STILL_ACTIVE = 259
92 96
97
93 class _STARTUPINFO(ctypes.Structure): 98 class _STARTUPINFO(ctypes.Structure):
94 _fields_ = [(r'cb', _DWORD), 99 _fields_ = [
95 (r'lpReserved', _LPSTR), 100 (r'cb', _DWORD),
96 (r'lpDesktop', _LPSTR), 101 (r'lpReserved', _LPSTR),
97 (r'lpTitle', _LPSTR), 102 (r'lpDesktop', _LPSTR),
98 (r'dwX', _DWORD), 103 (r'lpTitle', _LPSTR),
99 (r'dwY', _DWORD), 104 (r'dwX', _DWORD),
100 (r'dwXSize', _DWORD), 105 (r'dwY', _DWORD),
101 (r'dwYSize', _DWORD), 106 (r'dwXSize', _DWORD),
102 (r'dwXCountChars', _DWORD), 107 (r'dwYSize', _DWORD),
103 (r'dwYCountChars', _DWORD), 108 (r'dwXCountChars', _DWORD),
104 (r'dwFillAttribute', _DWORD), 109 (r'dwYCountChars', _DWORD),
105 (r'dwFlags', _DWORD), 110 (r'dwFillAttribute', _DWORD),
106 (r'wShowWindow', _WORD), 111 (r'dwFlags', _DWORD),
107 (r'cbReserved2', _WORD), 112 (r'wShowWindow', _WORD),
108 (r'lpReserved2', ctypes.c_char_p), 113 (r'cbReserved2', _WORD),
109 (r'hStdInput', _HANDLE), 114 (r'lpReserved2', ctypes.c_char_p),
110 (r'hStdOutput', _HANDLE), 115 (r'hStdInput', _HANDLE),
111 (r'hStdError', _HANDLE)] 116 (r'hStdOutput', _HANDLE),
117 (r'hStdError', _HANDLE),
118 ]
119
112 120
113 class _PROCESS_INFORMATION(ctypes.Structure): 121 class _PROCESS_INFORMATION(ctypes.Structure):
114 _fields_ = [(r'hProcess', _HANDLE), 122 _fields_ = [
115 (r'hThread', _HANDLE), 123 (r'hProcess', _HANDLE),
116 (r'dwProcessId', _DWORD), 124 (r'hThread', _HANDLE),
117 (r'dwThreadId', _DWORD)] 125 (r'dwProcessId', _DWORD),
126 (r'dwThreadId', _DWORD),
127 ]
128
118 129
119 _CREATE_NO_WINDOW = 0x08000000 130 _CREATE_NO_WINDOW = 0x08000000
120 _SW_HIDE = 0 131 _SW_HIDE = 0
121 132
133
122 class _COORD(ctypes.Structure): 134 class _COORD(ctypes.Structure):
123 _fields_ = [(r'X', ctypes.c_short), 135 _fields_ = [(r'X', ctypes.c_short), (r'Y', ctypes.c_short)]
124 (r'Y', ctypes.c_short)] 136
125 137
126 class _SMALL_RECT(ctypes.Structure): 138 class _SMALL_RECT(ctypes.Structure):
127 _fields_ = [(r'Left', ctypes.c_short), 139 _fields_ = [
128 (r'Top', ctypes.c_short), 140 (r'Left', ctypes.c_short),
129 (r'Right', ctypes.c_short), 141 (r'Top', ctypes.c_short),
130 (r'Bottom', ctypes.c_short)] 142 (r'Right', ctypes.c_short),
143 (r'Bottom', ctypes.c_short),
144 ]
145
131 146
132 class _CONSOLE_SCREEN_BUFFER_INFO(ctypes.Structure): 147 class _CONSOLE_SCREEN_BUFFER_INFO(ctypes.Structure):
133 _fields_ = [(r'dwSize', _COORD), 148 _fields_ = [
134 (r'dwCursorPosition', _COORD), 149 (r'dwSize', _COORD),
135 (r'wAttributes', _WORD), 150 (r'dwCursorPosition', _COORD),
136 (r'srWindow', _SMALL_RECT), 151 (r'wAttributes', _WORD),
137 (r'dwMaximumWindowSize', _COORD)] 152 (r'srWindow', _SMALL_RECT),
153 (r'dwMaximumWindowSize', _COORD),
154 ]
155
138 156
139 _STD_OUTPUT_HANDLE = _DWORD(-11).value 157 _STD_OUTPUT_HANDLE = _DWORD(-11).value
140 _STD_ERROR_HANDLE = _DWORD(-12).value 158 _STD_ERROR_HANDLE = _DWORD(-12).value
141 159
142 # CERT_TRUST_STATUS dwErrorStatus 160 # CERT_TRUST_STATUS dwErrorStatus
148 166
149 # These structs are only complete enough to achieve what we need. 167 # These structs are only complete enough to achieve what we need.
150 class CERT_CHAIN_CONTEXT(ctypes.Structure): 168 class CERT_CHAIN_CONTEXT(ctypes.Structure):
151 _fields_ = ( 169 _fields_ = (
152 (r"cbSize", _DWORD), 170 (r"cbSize", _DWORD),
153
154 # CERT_TRUST_STATUS struct 171 # CERT_TRUST_STATUS struct
155 (r"dwErrorStatus", _DWORD), 172 (r"dwErrorStatus", _DWORD),
156 (r"dwInfoStatus", _DWORD), 173 (r"dwInfoStatus", _DWORD),
157
158 (r"cChain", _DWORD), 174 (r"cChain", _DWORD),
159 (r"rgpChain", ctypes.c_void_p), 175 (r"rgpChain", ctypes.c_void_p),
160 (r"cLowerQualityChainContext", _DWORD), 176 (r"cLowerQualityChainContext", _DWORD),
161 (r"rgpLowerQualityChainContext", ctypes.c_void_p), 177 (r"rgpLowerQualityChainContext", ctypes.c_void_p),
162 (r"fHasRevocationFreshnessTime", _BOOL), 178 (r"fHasRevocationFreshnessTime", _BOOL),
163 (r"dwRevocationFreshnessTime", _DWORD), 179 (r"dwRevocationFreshnessTime", _DWORD),
164 ) 180 )
165 181
182
166 class CERT_USAGE_MATCH(ctypes.Structure): 183 class CERT_USAGE_MATCH(ctypes.Structure):
167 _fields_ = ( 184 _fields_ = (
168 (r"dwType", _DWORD), 185 (r"dwType", _DWORD),
169 186 # CERT_ENHKEY_USAGE struct
170 # CERT_ENHKEY_USAGE struct
171 (r"cUsageIdentifier", _DWORD), 187 (r"cUsageIdentifier", _DWORD),
172 (r"rgpszUsageIdentifier", ctypes.c_void_p), # LPSTR * 188 (r"rgpszUsageIdentifier", ctypes.c_void_p), # LPSTR *
173 ) 189 )
190
174 191
175 class CERT_CHAIN_PARA(ctypes.Structure): 192 class CERT_CHAIN_PARA(ctypes.Structure):
176 _fields_ = ( 193 _fields_ = (
177 (r"cbSize", _DWORD), 194 (r"cbSize", _DWORD),
178 (r"RequestedUsage", CERT_USAGE_MATCH), 195 (r"RequestedUsage", CERT_USAGE_MATCH),
179 (r"RequestedIssuancePolicy", CERT_USAGE_MATCH), 196 (r"RequestedIssuancePolicy", CERT_USAGE_MATCH),
180 (r"dwUrlRetrievalTimeout", _DWORD), 197 (r"dwUrlRetrievalTimeout", _DWORD),
181 (r"fCheckRevocationFreshnessTime", _BOOL), 198 (r"fCheckRevocationFreshnessTime", _BOOL),
182 (r"dwRevocationFreshnessTime", _DWORD), 199 (r"dwRevocationFreshnessTime", _DWORD),
183 (r"pftCacheResync", ctypes.c_void_p), # LPFILETIME 200 (r"pftCacheResync", ctypes.c_void_p), # LPFILETIME
184 (r"pStrongSignPara", ctypes.c_void_p), # PCCERT_STRONG_SIGN_PARA 201 (r"pStrongSignPara", ctypes.c_void_p), # PCCERT_STRONG_SIGN_PARA
185 (r"dwStrongSignFlags", _DWORD), 202 (r"dwStrongSignFlags", _DWORD),
186 ) 203 )
187 204
205
188 # types of parameters of C functions used (required by pypy) 206 # types of parameters of C functions used (required by pypy)
189 207
190 _crypt32.CertCreateCertificateContext.argtypes = [_DWORD, # cert encoding 208 _crypt32.CertCreateCertificateContext.argtypes = [
191 ctypes.c_char_p, # cert 209 _DWORD, # cert encoding
192 _DWORD] # cert size 210 ctypes.c_char_p, # cert
211 _DWORD,
212 ] # cert size
193 _crypt32.CertCreateCertificateContext.restype = _PCCERT_CONTEXT 213 _crypt32.CertCreateCertificateContext.restype = _PCCERT_CONTEXT
194 214
195 _crypt32.CertGetCertificateChain.argtypes = [ 215 _crypt32.CertGetCertificateChain.argtypes = [
196 ctypes.c_void_p, # HCERTCHAINENGINE 216 ctypes.c_void_p, # HCERTCHAINENGINE
197 _PCCERT_CONTEXT, 217 _PCCERT_CONTEXT,
198 ctypes.c_void_p, # LPFILETIME 218 ctypes.c_void_p, # LPFILETIME
199 ctypes.c_void_p, # HCERTSTORE 219 ctypes.c_void_p, # HCERTSTORE
200 ctypes.c_void_p, # PCERT_CHAIN_PARA 220 ctypes.c_void_p, # PCERT_CHAIN_PARA
201 _DWORD, 221 _DWORD,
202 ctypes.c_void_p, # LPVOID 222 ctypes.c_void_p, # LPVOID
203 ctypes.c_void_p # PCCERT_CHAIN_CONTEXT * 223 ctypes.c_void_p, # PCCERT_CHAIN_CONTEXT *
204 ] 224 ]
205 _crypt32.CertGetCertificateChain.restype = _BOOL 225 _crypt32.CertGetCertificateChain.restype = _BOOL
206 226
207 _crypt32.CertFreeCertificateContext.argtypes = [_PCCERT_CONTEXT] 227 _crypt32.CertFreeCertificateContext.argtypes = [_PCCERT_CONTEXT]
208 _crypt32.CertFreeCertificateContext.restype = _BOOL 228 _crypt32.CertFreeCertificateContext.restype = _BOOL
209 229
210 _kernel32.CreateFileA.argtypes = [_LPCSTR, _DWORD, _DWORD, ctypes.c_void_p, 230 _kernel32.CreateFileA.argtypes = [
211 _DWORD, _DWORD, _HANDLE] 231 _LPCSTR,
232 _DWORD,
233 _DWORD,
234 ctypes.c_void_p,
235 _DWORD,
236 _DWORD,
237 _HANDLE,
238 ]
212 _kernel32.CreateFileA.restype = _HANDLE 239 _kernel32.CreateFileA.restype = _HANDLE
213 240
214 _kernel32.GetFileInformationByHandle.argtypes = [_HANDLE, ctypes.c_void_p] 241 _kernel32.GetFileInformationByHandle.argtypes = [_HANDLE, ctypes.c_void_p]
215 _kernel32.GetFileInformationByHandle.restype = _BOOL 242 _kernel32.GetFileInformationByHandle.restype = _BOOL
216 243
235 _DRIVE_RAMDISK = 6 262 _DRIVE_RAMDISK = 6
236 263
237 _kernel32.GetDriveTypeA.argtypes = [_LPCSTR] 264 _kernel32.GetDriveTypeA.argtypes = [_LPCSTR]
238 _kernel32.GetDriveTypeA.restype = _UINT 265 _kernel32.GetDriveTypeA.restype = _UINT
239 266
240 _kernel32.GetVolumeInformationA.argtypes = [_LPCSTR, ctypes.c_void_p, _DWORD, 267 _kernel32.GetVolumeInformationA.argtypes = [
241 ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, _DWORD] 268 _LPCSTR,
269 ctypes.c_void_p,
270 _DWORD,
271 ctypes.c_void_p,
272 ctypes.c_void_p,
273 ctypes.c_void_p,
274 ctypes.c_void_p,
275 _DWORD,
276 ]
242 _kernel32.GetVolumeInformationA.restype = _BOOL 277 _kernel32.GetVolumeInformationA.restype = _BOOL
243 278
244 _kernel32.GetVolumePathNameA.argtypes = [_LPCSTR, ctypes.c_void_p, _DWORD] 279 _kernel32.GetVolumePathNameA.argtypes = [_LPCSTR, ctypes.c_void_p, _DWORD]
245 _kernel32.GetVolumePathNameA.restype = _BOOL 280 _kernel32.GetVolumePathNameA.restype = _BOOL
246 281
254 _kernel32.GetLastError.restype = _DWORD 289 _kernel32.GetLastError.restype = _DWORD
255 290
256 _kernel32.GetModuleFileNameA.argtypes = [_HANDLE, ctypes.c_void_p, _DWORD] 291 _kernel32.GetModuleFileNameA.argtypes = [_HANDLE, ctypes.c_void_p, _DWORD]
257 _kernel32.GetModuleFileNameA.restype = _DWORD 292 _kernel32.GetModuleFileNameA.restype = _DWORD
258 293
259 _kernel32.CreateProcessA.argtypes = [_LPCSTR, _LPCSTR, ctypes.c_void_p, 294 _kernel32.CreateProcessA.argtypes = [
260 ctypes.c_void_p, _BOOL, _DWORD, ctypes.c_void_p, _LPCSTR, ctypes.c_void_p, 295 _LPCSTR,
261 ctypes.c_void_p] 296 _LPCSTR,
297 ctypes.c_void_p,
298 ctypes.c_void_p,
299 _BOOL,
300 _DWORD,
301 ctypes.c_void_p,
302 _LPCSTR,
303 ctypes.c_void_p,
304 ctypes.c_void_p,
305 ]
262 _kernel32.CreateProcessA.restype = _BOOL 306 _kernel32.CreateProcessA.restype = _BOOL
263 307
264 _kernel32.ExitProcess.argtypes = [_UINT] 308 _kernel32.ExitProcess.argtypes = [_UINT]
265 _kernel32.ExitProcess.restype = None 309 _kernel32.ExitProcess.restype = None
266 310
294 338
295 _WNDENUMPROC = ctypes.WINFUNCTYPE(_BOOL, _HWND, _LPARAM) 339 _WNDENUMPROC = ctypes.WINFUNCTYPE(_BOOL, _HWND, _LPARAM)
296 _user32.EnumWindows.argtypes = [_WNDENUMPROC, _LPARAM] 340 _user32.EnumWindows.argtypes = [_WNDENUMPROC, _LPARAM]
297 _user32.EnumWindows.restype = _BOOL 341 _user32.EnumWindows.restype = _BOOL
298 342
299 _kernel32.PeekNamedPipe.argtypes = [_HANDLE, ctypes.c_void_p, _DWORD, 343 _kernel32.PeekNamedPipe.argtypes = [
300 ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p] 344 _HANDLE,
345 ctypes.c_void_p,
346 _DWORD,
347 ctypes.c_void_p,
348 ctypes.c_void_p,
349 ctypes.c_void_p,
350 ]
301 _kernel32.PeekNamedPipe.restype = _BOOL 351 _kernel32.PeekNamedPipe.restype = _BOOL
352
302 353
303 def _raiseoserror(name): 354 def _raiseoserror(name):
304 # Force the code to a signed int to avoid an 'int too large' error. 355 # Force the code to a signed int to avoid an 'int too large' error.
305 # See https://bugs.python.org/issue28474 356 # See https://bugs.python.org/issue28474
306 code = _kernel32.GetLastError() 357 code = _kernel32.GetLastError()
307 if code > 0x7fffffff: 358 if code > 0x7FFFFFFF:
308 code -= 2**32 359 code -= 2 ** 32
309 err = ctypes.WinError(code=code) 360 err = ctypes.WinError(code=code)
310 raise OSError(err.errno, r'%s: %s' % (encoding.strfromlocal(name), 361 raise OSError(
311 err.strerror)) 362 err.errno, r'%s: %s' % (encoding.strfromlocal(name), err.strerror)
363 )
364
312 365
313 def _getfileinfo(name): 366 def _getfileinfo(name):
314 fh = _kernel32.CreateFileA(name, 0, 367 fh = _kernel32.CreateFileA(
315 _FILE_SHARE_READ | _FILE_SHARE_WRITE | _FILE_SHARE_DELETE, 368 name,
316 None, _OPEN_EXISTING, _FILE_FLAG_BACKUP_SEMANTICS, None) 369 0,
370 _FILE_SHARE_READ | _FILE_SHARE_WRITE | _FILE_SHARE_DELETE,
371 None,
372 _OPEN_EXISTING,
373 _FILE_FLAG_BACKUP_SEMANTICS,
374 None,
375 )
317 if fh == _INVALID_HANDLE_VALUE: 376 if fh == _INVALID_HANDLE_VALUE:
318 _raiseoserror(name) 377 _raiseoserror(name)
319 try: 378 try:
320 fi = _BY_HANDLE_FILE_INFORMATION() 379 fi = _BY_HANDLE_FILE_INFORMATION()
321 if not _kernel32.GetFileInformationByHandle(fh, ctypes.byref(fi)): 380 if not _kernel32.GetFileInformationByHandle(fh, ctypes.byref(fi)):
322 _raiseoserror(name) 381 _raiseoserror(name)
323 return fi 382 return fi
324 finally: 383 finally:
325 _kernel32.CloseHandle(fh) 384 _kernel32.CloseHandle(fh)
326 385
386
327 def checkcertificatechain(cert, build=True): 387 def checkcertificatechain(cert, build=True):
328 '''Tests the given certificate to see if there is a complete chain to a 388 '''Tests the given certificate to see if there is a complete chain to a
329 trusted root certificate. As a side effect, missing certificates are 389 trusted root certificate. As a side effect, missing certificates are
330 downloaded and installed unless ``build=False``. True is returned if a 390 downloaded and installed unless ``build=False``. True is returned if a
331 chain to a trusted root exists (even if built on the fly), otherwise 391 chain to a trusted root exists (even if built on the fly), otherwise
334 ''' 394 '''
335 395
336 chainctxptr = ctypes.POINTER(CERT_CHAIN_CONTEXT) 396 chainctxptr = ctypes.POINTER(CERT_CHAIN_CONTEXT)
337 397
338 pchainctx = chainctxptr() 398 pchainctx = chainctxptr()
339 chainpara = CERT_CHAIN_PARA(cbSize=ctypes.sizeof(CERT_CHAIN_PARA), 399 chainpara = CERT_CHAIN_PARA(
340 RequestedUsage=CERT_USAGE_MATCH()) 400 cbSize=ctypes.sizeof(CERT_CHAIN_PARA), RequestedUsage=CERT_USAGE_MATCH()
341 401 )
342 certctx = _crypt32.CertCreateCertificateContext(X509_ASN_ENCODING, cert, 402
343 len(cert)) 403 certctx = _crypt32.CertCreateCertificateContext(
404 X509_ASN_ENCODING, cert, len(cert)
405 )
344 if certctx is None: 406 if certctx is None:
345 _raiseoserror('CertCreateCertificateContext') 407 _raiseoserror('CertCreateCertificateContext')
346 408
347 flags = 0 409 flags = 0
348 410
349 if not build: 411 if not build:
350 flags |= 0x100 # CERT_CHAIN_DISABLE_AUTH_ROOT_AUTO_UPDATE 412 flags |= 0x100 # CERT_CHAIN_DISABLE_AUTH_ROOT_AUTO_UPDATE
351 413
352 try: 414 try:
353 # Building the certificate chain will update root certs as necessary. 415 # Building the certificate chain will update root certs as necessary.
354 if not _crypt32.CertGetCertificateChain(None, # hChainEngine 416 if not _crypt32.CertGetCertificateChain(
355 certctx, # pCertContext 417 None, # hChainEngine
356 None, # pTime 418 certctx, # pCertContext
357 None, # hAdditionalStore 419 None, # pTime
358 ctypes.byref(chainpara), 420 None, # hAdditionalStore
359 flags, 421 ctypes.byref(chainpara),
360 None, # pvReserved 422 flags,
361 ctypes.byref(pchainctx)): 423 None, # pvReserved
424 ctypes.byref(pchainctx),
425 ):
362 _raiseoserror('CertGetCertificateChain') 426 _raiseoserror('CertGetCertificateChain')
363 427
364 chainctx = pchainctx.contents 428 chainctx = pchainctx.contents
365 429
366 return chainctx.dwErrorStatus & CERT_TRUST_IS_PARTIAL_CHAIN == 0 430 return chainctx.dwErrorStatus & CERT_TRUST_IS_PARTIAL_CHAIN == 0
367 finally: 431 finally:
368 if pchainctx: 432 if pchainctx:
369 _crypt32.CertFreeCertificateChain(pchainctx) 433 _crypt32.CertFreeCertificateChain(pchainctx)
370 _crypt32.CertFreeCertificateContext(certctx) 434 _crypt32.CertFreeCertificateContext(certctx)
371 435
436
372 def oslink(src, dst): 437 def oslink(src, dst):
373 try: 438 try:
374 if not _kernel32.CreateHardLinkA(dst, src, None): 439 if not _kernel32.CreateHardLinkA(dst, src, None):
375 _raiseoserror(src) 440 _raiseoserror(src)
376 except AttributeError: # Wine doesn't support this function 441 except AttributeError: # Wine doesn't support this function
377 _raiseoserror(src) 442 _raiseoserror(src)
443
378 444
379 def nlinks(name): 445 def nlinks(name):
380 '''return number of hardlinks for the given file''' 446 '''return number of hardlinks for the given file'''
381 return _getfileinfo(name).nNumberOfLinks 447 return _getfileinfo(name).nNumberOfLinks
448
382 449
383 def samefile(path1, path2): 450 def samefile(path1, path2):
384 '''Returns whether path1 and path2 refer to the same file or directory.''' 451 '''Returns whether path1 and path2 refer to the same file or directory.'''
385 res1 = _getfileinfo(path1) 452 res1 = _getfileinfo(path1)
386 res2 = _getfileinfo(path2) 453 res2 = _getfileinfo(path2)
387 return (res1.dwVolumeSerialNumber == res2.dwVolumeSerialNumber 454 return (
455 res1.dwVolumeSerialNumber == res2.dwVolumeSerialNumber
388 and res1.nFileIndexHigh == res2.nFileIndexHigh 456 and res1.nFileIndexHigh == res2.nFileIndexHigh
389 and res1.nFileIndexLow == res2.nFileIndexLow) 457 and res1.nFileIndexLow == res2.nFileIndexLow
458 )
459
390 460
391 def samedevice(path1, path2): 461 def samedevice(path1, path2):
392 '''Returns whether path1 and path2 are on the same device.''' 462 '''Returns whether path1 and path2 are on the same device.'''
393 res1 = _getfileinfo(path1) 463 res1 = _getfileinfo(path1)
394 res2 = _getfileinfo(path2) 464 res2 = _getfileinfo(path2)
395 return res1.dwVolumeSerialNumber == res2.dwVolumeSerialNumber 465 return res1.dwVolumeSerialNumber == res2.dwVolumeSerialNumber
396 466
467
397 def peekpipe(pipe): 468 def peekpipe(pipe):
398 handle = msvcrt.get_osfhandle(pipe.fileno()) 469 handle = msvcrt.get_osfhandle(pipe.fileno())
399 avail = _DWORD() 470 avail = _DWORD()
400 471
401 if not _kernel32.PeekNamedPipe(handle, None, 0, None, ctypes.byref(avail), 472 if not _kernel32.PeekNamedPipe(
402 None): 473 handle, None, 0, None, ctypes.byref(avail), None
474 ):
403 err = _kernel32.GetLastError() 475 err = _kernel32.GetLastError()
404 if err == _ERROR_BROKEN_PIPE: 476 if err == _ERROR_BROKEN_PIPE:
405 return 0 477 return 0
406 raise ctypes.WinError(err) 478 raise ctypes.WinError(err)
407 479
408 return avail.value 480 return avail.value
481
409 482
410 def lasterrorwaspipeerror(err): 483 def lasterrorwaspipeerror(err):
411 if err.errno != errno.EINVAL: 484 if err.errno != errno.EINVAL:
412 return False 485 return False
413 err = _kernel32.GetLastError() 486 err = _kernel32.GetLastError()
414 return err == _ERROR_BROKEN_PIPE or err == _ERROR_NO_DATA 487 return err == _ERROR_BROKEN_PIPE or err == _ERROR_NO_DATA
488
415 489
416 def testpid(pid): 490 def testpid(pid):
417 '''return True if pid is still running or unable to 491 '''return True if pid is still running or unable to
418 determine, False otherwise''' 492 determine, False otherwise'''
419 h = _kernel32.OpenProcess(_PROCESS_QUERY_INFORMATION, False, pid) 493 h = _kernel32.OpenProcess(_PROCESS_QUERY_INFORMATION, False, pid)
424 return status.value == _STILL_ACTIVE 498 return status.value == _STILL_ACTIVE
425 finally: 499 finally:
426 _kernel32.CloseHandle(h) 500 _kernel32.CloseHandle(h)
427 return _kernel32.GetLastError() != _ERROR_INVALID_PARAMETER 501 return _kernel32.GetLastError() != _ERROR_INVALID_PARAMETER
428 502
503
429 def executablepath(): 504 def executablepath():
430 '''return full path of hg.exe''' 505 '''return full path of hg.exe'''
431 size = 600 506 size = 600
432 buf = ctypes.create_string_buffer(size + 1) 507 buf = ctypes.create_string_buffer(size + 1)
433 len = _kernel32.GetModuleFileNameA(None, ctypes.byref(buf), size) 508 len = _kernel32.GetModuleFileNameA(None, ctypes.byref(buf), size)
434 if len == 0: 509 if len == 0:
435 raise ctypes.WinError() # Note: WinError is a function 510 raise ctypes.WinError() # Note: WinError is a function
436 elif len == size: 511 elif len == size:
437 raise ctypes.WinError(_ERROR_INSUFFICIENT_BUFFER) 512 raise ctypes.WinError(_ERROR_INSUFFICIENT_BUFFER)
438 return buf.value 513 return buf.value
514
439 515
440 def getvolumename(path): 516 def getvolumename(path):
441 """Get the mount point of the filesystem from a directory or file 517 """Get the mount point of the filesystem from a directory or file
442 (best-effort) 518 (best-effort)
443 519
450 # somehow fails on Windows XP 526 # somehow fails on Windows XP
451 size = max(len(realpath), _MAX_PATH) + 1 527 size = max(len(realpath), _MAX_PATH) + 1
452 buf = ctypes.create_string_buffer(size) 528 buf = ctypes.create_string_buffer(size)
453 529
454 if not _kernel32.GetVolumePathNameA(realpath, ctypes.byref(buf), size): 530 if not _kernel32.GetVolumePathNameA(realpath, ctypes.byref(buf), size):
455 raise ctypes.WinError() # Note: WinError is a function 531 raise ctypes.WinError() # Note: WinError is a function
456 532
457 return buf.value 533 return buf.value
534
458 535
459 def getfstype(path): 536 def getfstype(path):
460 """Get the filesystem type name from a directory or file (best-effort) 537 """Get the filesystem type name from a directory or file (best-effort)
461 538
462 Returns None if we are unsure. Raises OSError on ENOENT, EPERM, etc. 539 Returns None if we are unsure. Raises OSError on ENOENT, EPERM, etc.
465 542
466 t = _kernel32.GetDriveTypeA(volume) 543 t = _kernel32.GetDriveTypeA(volume)
467 544
468 if t == _DRIVE_REMOTE: 545 if t == _DRIVE_REMOTE:
469 return 'cifs' 546 return 'cifs'
470 elif t not in (_DRIVE_REMOVABLE, _DRIVE_FIXED, _DRIVE_CDROM, 547 elif t not in (
471 _DRIVE_RAMDISK): 548 _DRIVE_REMOVABLE,
549 _DRIVE_FIXED,
550 _DRIVE_CDROM,
551 _DRIVE_RAMDISK,
552 ):
472 return None 553 return None
473 554
474 size = _MAX_PATH + 1 555 size = _MAX_PATH + 1
475 name = ctypes.create_string_buffer(size) 556 name = ctypes.create_string_buffer(size)
476 557
477 if not _kernel32.GetVolumeInformationA(volume, None, 0, None, None, None, 558 if not _kernel32.GetVolumeInformationA(
478 ctypes.byref(name), size): 559 volume, None, 0, None, None, None, ctypes.byref(name), size
479 raise ctypes.WinError() # Note: WinError is a function 560 ):
561 raise ctypes.WinError() # Note: WinError is a function
480 562
481 return name.value 563 return name.value
564
482 565
483 def getuser(): 566 def getuser():
484 '''return name of current user''' 567 '''return name of current user'''
485 size = _DWORD(300) 568 size = _DWORD(300)
486 buf = ctypes.create_string_buffer(size.value + 1) 569 buf = ctypes.create_string_buffer(size.value + 1)
487 if not _advapi32.GetUserNameA(ctypes.byref(buf), ctypes.byref(size)): 570 if not _advapi32.GetUserNameA(ctypes.byref(buf), ctypes.byref(size)):
488 raise ctypes.WinError() 571 raise ctypes.WinError()
489 return buf.value 572 return buf.value
490 573
574
491 _signalhandler = [] 575 _signalhandler = []
576
492 577
493 def setsignalhandler(): 578 def setsignalhandler():
494 '''Register a termination handler for console events including 579 '''Register a termination handler for console events including
495 CTRL+C. python signal handlers do not work well with socket 580 CTRL+C. python signal handlers do not work well with socket
496 operations. 581 operations.
497 ''' 582 '''
583
498 def handler(event): 584 def handler(event):
499 _kernel32.ExitProcess(1) 585 _kernel32.ExitProcess(1)
500 586
501 if _signalhandler: 587 if _signalhandler:
502 return # already registered 588 return # already registered
503 h = _SIGNAL_HANDLER(handler) 589 h = _SIGNAL_HANDLER(handler)
504 _signalhandler.append(h) # needed to prevent garbage collection 590 _signalhandler.append(h) # needed to prevent garbage collection
505 if not _kernel32.SetConsoleCtrlHandler(h, True): 591 if not _kernel32.SetConsoleCtrlHandler(h, True):
506 raise ctypes.WinError() 592 raise ctypes.WinError()
507 593
594
508 def hidewindow(): 595 def hidewindow():
509
510 def callback(hwnd, pid): 596 def callback(hwnd, pid):
511 wpid = _DWORD() 597 wpid = _DWORD()
512 _user32.GetWindowThreadProcessId(hwnd, ctypes.byref(wpid)) 598 _user32.GetWindowThreadProcessId(hwnd, ctypes.byref(wpid))
513 if pid == wpid.value: 599 if pid == wpid.value:
514 _user32.ShowWindow(hwnd, _SW_HIDE) 600 _user32.ShowWindow(hwnd, _SW_HIDE)
515 return False # stop enumerating windows 601 return False # stop enumerating windows
516 return True 602 return True
517 603
518 pid = _kernel32.GetCurrentProcessId() 604 pid = _kernel32.GetCurrentProcessId()
519 _user32.EnumWindows(_WNDENUMPROC(callback), pid) 605 _user32.EnumWindows(_WNDENUMPROC(callback), pid)
606
520 607
521 def termsize(): 608 def termsize():
522 # cmd.exe does not handle CR like a unix console, the CR is 609 # cmd.exe does not handle CR like a unix console, the CR is
523 # counted in the line length. On 80 columns consoles, if 80 610 # counted in the line length. On 80 columns consoles, if 80
524 # characters are written, the following CR won't apply on the 611 # characters are written, the following CR won't apply on the
525 # current line but on the new one. Keep room for it. 612 # current line but on the new one. Keep room for it.
526 width = 80 - 1 613 width = 80 - 1
527 height = 25 614 height = 25
528 # Query stderr to avoid problems with redirections 615 # Query stderr to avoid problems with redirections
529 screenbuf = _kernel32.GetStdHandle( 616 screenbuf = _kernel32.GetStdHandle(
530 _STD_ERROR_HANDLE) # don't close the handle returned 617 _STD_ERROR_HANDLE
618 ) # don't close the handle returned
531 if screenbuf is None or screenbuf == _INVALID_HANDLE_VALUE: 619 if screenbuf is None or screenbuf == _INVALID_HANDLE_VALUE:
532 return width, height 620 return width, height
533 csbi = _CONSOLE_SCREEN_BUFFER_INFO() 621 csbi = _CONSOLE_SCREEN_BUFFER_INFO()
534 if not _kernel32.GetConsoleScreenBufferInfo( 622 if not _kernel32.GetConsoleScreenBufferInfo(screenbuf, ctypes.byref(csbi)):
535 screenbuf, ctypes.byref(csbi)):
536 return width, height 623 return width, height
537 width = csbi.srWindow.Right - csbi.srWindow.Left # don't '+ 1' 624 width = csbi.srWindow.Right - csbi.srWindow.Left # don't '+ 1'
538 height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1 625 height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1
539 return width, height 626 return width, height
540 627
628
541 def enablevtmode(): 629 def enablevtmode():
542 '''Enable virtual terminal mode for the associated console. Return True if 630 '''Enable virtual terminal mode for the associated console. Return True if
543 enabled, else False.''' 631 enabled, else False.'''
544 632
545 ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x4 633 ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x4
546 634
547 handle = _kernel32.GetStdHandle(_STD_OUTPUT_HANDLE) # don't close the handle 635 handle = _kernel32.GetStdHandle(
636 _STD_OUTPUT_HANDLE
637 ) # don't close the handle
548 if handle == _INVALID_HANDLE_VALUE: 638 if handle == _INVALID_HANDLE_VALUE:
549 return False 639 return False
550 640
551 mode = _DWORD(0) 641 mode = _DWORD(0)
552 642
558 648
559 if not _kernel32.SetConsoleMode(handle, mode): 649 if not _kernel32.SetConsoleMode(handle, mode):
560 return False 650 return False
561 651
562 return True 652 return True
653
563 654
564 def spawndetached(args): 655 def spawndetached(args):
565 # No standard library function really spawns a fully detached 656 # No standard library function really spawns a fully detached
566 # process under win32 because they allocate pipes or other objects 657 # process under win32 because they allocate pipes or other objects
567 # to handle standard streams communications. Passing these objects 658 # to handle standard streams communications. Passing these objects
581 672
582 args = subprocess.list2cmdline(pycompat.rapply(encoding.strfromlocal, args)) 673 args = subprocess.list2cmdline(pycompat.rapply(encoding.strfromlocal, args))
583 674
584 # TODO: CreateProcessW on py3? 675 # TODO: CreateProcessW on py3?
585 res = _kernel32.CreateProcessA( 676 res = _kernel32.CreateProcessA(
586 None, encoding.strtolocal(args), None, None, False, _CREATE_NO_WINDOW, 677 None,
587 env, encoding.getcwd(), ctypes.byref(si), ctypes.byref(pi)) 678 encoding.strtolocal(args),
679 None,
680 None,
681 False,
682 _CREATE_NO_WINDOW,
683 env,
684 encoding.getcwd(),
685 ctypes.byref(si),
686 ctypes.byref(pi),
687 )
588 if not res: 688 if not res:
589 raise ctypes.WinError() 689 raise ctypes.WinError()
590 690
591 _kernel32.CloseHandle(pi.hProcess) 691 _kernel32.CloseHandle(pi.hProcess)
592 _kernel32.CloseHandle(pi.hThread) 692 _kernel32.CloseHandle(pi.hThread)
593 693
594 return pi.dwProcessId 694 return pi.dwProcessId
695
595 696
596 def unlink(f): 697 def unlink(f):
597 '''try to implement POSIX' unlink semantics on Windows''' 698 '''try to implement POSIX' unlink semantics on Windows'''
598 699
599 if os.path.isdir(f): 700 if os.path.isdir(f):
600 # use EPERM because it is POSIX prescribed value, even though 701 # use EPERM because it is POSIX prescribed value, even though
601 # unlink(2) on directories returns EISDIR on Linux 702 # unlink(2) on directories returns EISDIR on Linux
602 raise IOError(errno.EPERM, 703 raise IOError(
603 r"Unlinking directory not permitted: '%s'" 704 errno.EPERM,
604 % encoding.strfromlocal(f)) 705 r"Unlinking directory not permitted: '%s'"
706 % encoding.strfromlocal(f),
707 )
605 708
606 # POSIX allows to unlink and rename open files. Windows has serious 709 # POSIX allows to unlink and rename open files. Windows has serious
607 # problems with doing that: 710 # problems with doing that:
608 # - Calling os.unlink (or os.rename) on a file f fails if f or any 711 # - Calling os.unlink (or os.rename) on a file f fails if f or any
609 # hardlinked copy of f has been opened with Python's open(). There is no 712 # hardlinked copy of f has been opened with Python's open(). There is no
619 # f to a random temporary name before calling os.unlink on it. This allows 722 # f to a random temporary name before calling os.unlink on it. This allows
620 # callers to recreate f immediately while having other readers do their 723 # callers to recreate f immediately while having other readers do their
621 # implicit zombie filename blocking on a temporary name. 724 # implicit zombie filename blocking on a temporary name.
622 725
623 for tries in pycompat.xrange(10): 726 for tries in pycompat.xrange(10):
624 temp = '%s-%08x' % (f, random.randint(0, 0xffffffff)) 727 temp = '%s-%08x' % (f, random.randint(0, 0xFFFFFFFF))
625 try: 728 try:
626 os.rename(f, temp) # raises OSError EEXIST if temp exists 729 os.rename(f, temp) # raises OSError EEXIST if temp exists
627 break 730 break
628 except OSError as e: 731 except OSError as e:
629 if e.errno != errno.EEXIST: 732 if e.errno != errno.EEXIST:
644 # The unlink might have failed due to some very rude AV-Scanners. 747 # The unlink might have failed due to some very rude AV-Scanners.
645 # Leaking a tempfile is the lesser evil than aborting here and 748 # Leaking a tempfile is the lesser evil than aborting here and
646 # leaving some potentially serious inconsistencies. 749 # leaving some potentially serious inconsistencies.
647 pass 750 pass
648 751
752
649 def makedir(path, notindexed): 753 def makedir(path, notindexed):
650 os.mkdir(path) 754 os.mkdir(path)
651 if notindexed: 755 if notindexed:
652 _kernel32.SetFileAttributesA(path, _FILE_ATTRIBUTE_NOT_CONTENT_INDEXED) 756 _kernel32.SetFileAttributesA(path, _FILE_ATTRIBUTE_NOT_CONTENT_INDEXED)