Show More
@@ -1,813 +1,813 b'' | |||
|
1 | 1 | # procutil.py - utility for managing processes and executable environment |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2005 K. Thananchayan <thananck@yahoo.com> |
|
4 | 4 | # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> |
|
5 | 5 | # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com> |
|
6 | 6 | # |
|
7 | 7 | # This software may be used and distributed according to the terms of the |
|
8 | 8 | # GNU General Public License version 2 or any later version. |
|
9 | 9 | |
|
10 | 10 | from __future__ import absolute_import |
|
11 | 11 | |
|
12 | 12 | import contextlib |
|
13 | 13 | import errno |
|
14 | 14 | import io |
|
15 | 15 | import os |
|
16 | 16 | import signal |
|
17 | 17 | import subprocess |
|
18 | 18 | import sys |
|
19 | 19 | import threading |
|
20 | 20 | import time |
|
21 | 21 | |
|
22 | 22 | from ..i18n import _ |
|
23 | 23 | from ..pycompat import ( |
|
24 | 24 | getattr, |
|
25 | 25 | open, |
|
26 | 26 | ) |
|
27 | 27 | |
|
28 | 28 | from .. import ( |
|
29 | 29 | encoding, |
|
30 | 30 | error, |
|
31 | 31 | policy, |
|
32 | 32 | pycompat, |
|
33 | 33 | ) |
|
34 | 34 | |
|
35 | 35 | # Import like this to keep import-checker happy |
|
36 | 36 | from ..utils import resourceutil |
|
37 | 37 | |
|
38 | 38 | osutil = policy.importmod('osutil') |
|
39 | 39 | |
|
40 | 40 | if pycompat.iswindows: |
|
41 | 41 | from .. import windows as platform |
|
42 | 42 | else: |
|
43 | 43 | from .. import posix as platform |
|
44 | 44 | |
|
45 | 45 | |
|
46 | 46 | def isatty(fp): |
|
47 | 47 | try: |
|
48 | 48 | return fp.isatty() |
|
49 | 49 | except AttributeError: |
|
50 | 50 | return False |
|
51 | 51 | |
|
52 | 52 | |
|
53 | 53 | class BadFile(io.RawIOBase): |
|
54 | 54 | """Dummy file object to simulate closed stdio behavior""" |
|
55 | 55 | |
|
56 | 56 | def readinto(self, b): |
|
57 | 57 | raise IOError(errno.EBADF, 'Bad file descriptor') |
|
58 | 58 | |
|
59 | 59 | def write(self, b): |
|
60 | 60 | raise IOError(errno.EBADF, 'Bad file descriptor') |
|
61 | 61 | |
|
62 | 62 | |
|
63 | 63 | class LineBufferedWrapper(object): |
|
64 | 64 | def __init__(self, orig): |
|
65 | 65 | self.orig = orig |
|
66 | 66 | |
|
67 | 67 | def __getattr__(self, attr): |
|
68 | 68 | return getattr(self.orig, attr) |
|
69 | 69 | |
|
70 | 70 | def write(self, s): |
|
71 | 71 | orig = self.orig |
|
72 | 72 | res = orig.write(s) |
|
73 | 73 | if s.endswith(b'\n'): |
|
74 | 74 | orig.flush() |
|
75 | 75 | return res |
|
76 | 76 | |
|
77 | 77 | |
|
78 | 78 | io.BufferedIOBase.register(LineBufferedWrapper) |
|
79 | 79 | |
|
80 | 80 | |
|
81 | 81 | def make_line_buffered(stream): |
|
82 | 82 | if pycompat.ispy3 and not isinstance(stream, io.BufferedIOBase): |
|
83 | 83 | # On Python 3, buffered streams can be expected to subclass |
|
84 | 84 | # BufferedIOBase. This is definitively the case for the streams |
|
85 | 85 | # initialized by the interpreter. For unbuffered streams, we don't need |
|
86 | 86 | # to emulate line buffering. |
|
87 | 87 | return stream |
|
88 | 88 | if isinstance(stream, LineBufferedWrapper): |
|
89 | 89 | return stream |
|
90 | 90 | return LineBufferedWrapper(stream) |
|
91 | 91 | |
|
92 | 92 | |
|
93 | 93 | def unwrap_line_buffered(stream): |
|
94 | 94 | if isinstance(stream, LineBufferedWrapper): |
|
95 | 95 | assert not isinstance(stream.orig, LineBufferedWrapper) |
|
96 | 96 | return stream.orig |
|
97 | 97 | return stream |
|
98 | 98 | |
|
99 | 99 | |
|
100 | 100 | class WriteAllWrapper(object): |
|
101 | 101 | def __init__(self, orig): |
|
102 | 102 | self.orig = orig |
|
103 | 103 | |
|
104 | 104 | def __getattr__(self, attr): |
|
105 | 105 | return getattr(self.orig, attr) |
|
106 | 106 | |
|
107 | 107 | def write(self, s): |
|
108 | 108 | write1 = self.orig.write |
|
109 | 109 | m = memoryview(s) |
|
110 | 110 | total_to_write = len(s) |
|
111 | 111 | total_written = 0 |
|
112 | 112 | while total_written < total_to_write: |
|
113 | 113 | total_written += write1(m[total_written:]) |
|
114 | 114 | return total_written |
|
115 | 115 | |
|
116 | 116 | |
|
117 | 117 | io.IOBase.register(WriteAllWrapper) |
|
118 | 118 | |
|
119 | 119 | |
|
120 | 120 | def _make_write_all(stream): |
|
121 | 121 | assert pycompat.ispy3 |
|
122 | 122 | if isinstance(stream, WriteAllWrapper): |
|
123 | 123 | return stream |
|
124 | 124 | if isinstance(stream, io.BufferedIOBase): |
|
125 | 125 | # The io.BufferedIOBase.write() contract guarantees that all data is |
|
126 | 126 | # written. |
|
127 | 127 | return stream |
|
128 | 128 | # In general, the write() method of streams is free to write only part of |
|
129 | 129 | # the data. |
|
130 | 130 | return WriteAllWrapper(stream) |
|
131 | 131 | |
|
132 | 132 | |
|
133 | 133 | if pycompat.ispy3: |
|
134 | 134 | # Python 3 implements its own I/O streams. Unlike stdio of C library, |
|
135 | 135 | # sys.stdin/stdout/stderr may be None if underlying fd is closed. |
|
136 | 136 | |
|
137 | 137 | # TODO: .buffer might not exist if std streams were replaced; we'll need |
|
138 | 138 | # a silly wrapper to make a bytes stream backed by a unicode one. |
|
139 | 139 | |
|
140 | 140 | if sys.stdin is None: |
|
141 | 141 | stdin = BadFile() |
|
142 | 142 | else: |
|
143 | 143 | stdin = sys.stdin.buffer |
|
144 | 144 | if sys.stdout is None: |
|
145 | 145 | stdout = BadFile() |
|
146 | 146 | else: |
|
147 | 147 | stdout = _make_write_all(sys.stdout.buffer) |
|
148 | 148 | if sys.stderr is None: |
|
149 | 149 | stderr = BadFile() |
|
150 | 150 | else: |
|
151 | 151 | stderr = _make_write_all(sys.stderr.buffer) |
|
152 | 152 | |
|
153 | 153 | if pycompat.iswindows: |
|
154 | 154 | # Work around Windows bugs. |
|
155 | stdout = platform.winstdout(stdout) | |
|
156 | stderr = platform.winstdout(stderr) | |
|
155 | stdout = platform.winstdout(stdout) # pytype: disable=module-attr | |
|
156 | stderr = platform.winstdout(stderr) # pytype: disable=module-attr | |
|
157 | 157 | if isatty(stdout): |
|
158 | 158 | # The standard library doesn't offer line-buffered binary streams. |
|
159 | 159 | stdout = make_line_buffered(stdout) |
|
160 | 160 | else: |
|
161 | 161 | # Python 2 uses the I/O streams provided by the C library. |
|
162 | 162 | stdin = sys.stdin |
|
163 | 163 | stdout = sys.stdout |
|
164 | 164 | stderr = sys.stderr |
|
165 | 165 | if pycompat.iswindows: |
|
166 | 166 | # Work around Windows bugs. |
|
167 | stdout = platform.winstdout(stdout) | |
|
168 | stderr = platform.winstdout(stderr) | |
|
167 | stdout = platform.winstdout(stdout) # pytype: disable=module-attr | |
|
168 | stderr = platform.winstdout(stderr) # pytype: disable=module-attr | |
|
169 | 169 | if isatty(stdout): |
|
170 | 170 | if pycompat.iswindows: |
|
171 | 171 | # The Windows C runtime library doesn't support line buffering. |
|
172 | 172 | stdout = make_line_buffered(stdout) |
|
173 | 173 | else: |
|
174 | 174 | # glibc determines buffering on first write to stdout - if we |
|
175 | 175 | # replace a TTY destined stdout with a pipe destined stdout (e.g. |
|
176 | 176 | # pager), we want line buffering. |
|
177 | 177 | stdout = os.fdopen(stdout.fileno(), 'wb', 1) |
|
178 | 178 | |
|
179 | 179 | |
|
180 | 180 | findexe = platform.findexe |
|
181 | 181 | _gethgcmd = platform.gethgcmd |
|
182 | 182 | getuser = platform.getuser |
|
183 | 183 | getpid = os.getpid |
|
184 | 184 | hidewindow = platform.hidewindow |
|
185 | 185 | readpipe = platform.readpipe |
|
186 | 186 | setbinary = platform.setbinary |
|
187 | 187 | setsignalhandler = platform.setsignalhandler |
|
188 | 188 | shellquote = platform.shellquote |
|
189 | 189 | shellsplit = platform.shellsplit |
|
190 | 190 | spawndetached = platform.spawndetached |
|
191 | 191 | sshargs = platform.sshargs |
|
192 | 192 | testpid = platform.testpid |
|
193 | 193 | |
|
194 | 194 | try: |
|
195 | 195 | setprocname = osutil.setprocname |
|
196 | 196 | except AttributeError: |
|
197 | 197 | pass |
|
198 | 198 | try: |
|
199 | 199 | unblocksignal = osutil.unblocksignal |
|
200 | 200 | except AttributeError: |
|
201 | 201 | pass |
|
202 | 202 | |
|
203 | 203 | closefds = pycompat.isposix |
|
204 | 204 | |
|
205 | 205 | |
|
206 | 206 | def explainexit(code): |
|
207 | 207 | """return a message describing a subprocess status |
|
208 | 208 | (codes from kill are negative - not os.system/wait encoding)""" |
|
209 | 209 | if code >= 0: |
|
210 | 210 | return _(b"exited with status %d") % code |
|
211 | 211 | return _(b"killed by signal %d") % -code |
|
212 | 212 | |
|
213 | 213 | |
|
214 | 214 | class _pfile(object): |
|
215 | 215 | """File-like wrapper for a stream opened by subprocess.Popen()""" |
|
216 | 216 | |
|
217 | 217 | def __init__(self, proc, fp): |
|
218 | 218 | self._proc = proc |
|
219 | 219 | self._fp = fp |
|
220 | 220 | |
|
221 | 221 | def close(self): |
|
222 | 222 | # unlike os.popen(), this returns an integer in subprocess coding |
|
223 | 223 | self._fp.close() |
|
224 | 224 | return self._proc.wait() |
|
225 | 225 | |
|
226 | 226 | def __iter__(self): |
|
227 | 227 | return iter(self._fp) |
|
228 | 228 | |
|
229 | 229 | def __getattr__(self, attr): |
|
230 | 230 | return getattr(self._fp, attr) |
|
231 | 231 | |
|
232 | 232 | def __enter__(self): |
|
233 | 233 | return self |
|
234 | 234 | |
|
235 | 235 | def __exit__(self, exc_type, exc_value, exc_tb): |
|
236 | 236 | self.close() |
|
237 | 237 | |
|
238 | 238 | |
|
239 | 239 | def popen(cmd, mode=b'rb', bufsize=-1): |
|
240 | 240 | if mode == b'rb': |
|
241 | 241 | return _popenreader(cmd, bufsize) |
|
242 | 242 | elif mode == b'wb': |
|
243 | 243 | return _popenwriter(cmd, bufsize) |
|
244 | 244 | raise error.ProgrammingError(b'unsupported mode: %r' % mode) |
|
245 | 245 | |
|
246 | 246 | |
|
247 | 247 | def _popenreader(cmd, bufsize): |
|
248 | 248 | p = subprocess.Popen( |
|
249 | 249 | tonativestr(cmd), |
|
250 | 250 | shell=True, |
|
251 | 251 | bufsize=bufsize, |
|
252 | 252 | close_fds=closefds, |
|
253 | 253 | stdout=subprocess.PIPE, |
|
254 | 254 | ) |
|
255 | 255 | return _pfile(p, p.stdout) |
|
256 | 256 | |
|
257 | 257 | |
|
258 | 258 | def _popenwriter(cmd, bufsize): |
|
259 | 259 | p = subprocess.Popen( |
|
260 | 260 | tonativestr(cmd), |
|
261 | 261 | shell=True, |
|
262 | 262 | bufsize=bufsize, |
|
263 | 263 | close_fds=closefds, |
|
264 | 264 | stdin=subprocess.PIPE, |
|
265 | 265 | ) |
|
266 | 266 | return _pfile(p, p.stdin) |
|
267 | 267 | |
|
268 | 268 | |
|
269 | 269 | def popen2(cmd, env=None): |
|
270 | 270 | # Setting bufsize to -1 lets the system decide the buffer size. |
|
271 | 271 | # The default for bufsize is 0, meaning unbuffered. This leads to |
|
272 | 272 | # poor performance on Mac OS X: http://bugs.python.org/issue4194 |
|
273 | 273 | p = subprocess.Popen( |
|
274 | 274 | tonativestr(cmd), |
|
275 | 275 | shell=True, |
|
276 | 276 | bufsize=-1, |
|
277 | 277 | close_fds=closefds, |
|
278 | 278 | stdin=subprocess.PIPE, |
|
279 | 279 | stdout=subprocess.PIPE, |
|
280 | 280 | env=tonativeenv(env), |
|
281 | 281 | ) |
|
282 | 282 | return p.stdin, p.stdout |
|
283 | 283 | |
|
284 | 284 | |
|
285 | 285 | def popen3(cmd, env=None): |
|
286 | 286 | stdin, stdout, stderr, p = popen4(cmd, env) |
|
287 | 287 | return stdin, stdout, stderr |
|
288 | 288 | |
|
289 | 289 | |
|
290 | 290 | def popen4(cmd, env=None, bufsize=-1): |
|
291 | 291 | p = subprocess.Popen( |
|
292 | 292 | tonativestr(cmd), |
|
293 | 293 | shell=True, |
|
294 | 294 | bufsize=bufsize, |
|
295 | 295 | close_fds=closefds, |
|
296 | 296 | stdin=subprocess.PIPE, |
|
297 | 297 | stdout=subprocess.PIPE, |
|
298 | 298 | stderr=subprocess.PIPE, |
|
299 | 299 | env=tonativeenv(env), |
|
300 | 300 | ) |
|
301 | 301 | return p.stdin, p.stdout, p.stderr, p |
|
302 | 302 | |
|
303 | 303 | |
|
304 | 304 | def pipefilter(s, cmd): |
|
305 | 305 | '''filter string S through command CMD, returning its output''' |
|
306 | 306 | p = subprocess.Popen( |
|
307 | 307 | tonativestr(cmd), |
|
308 | 308 | shell=True, |
|
309 | 309 | close_fds=closefds, |
|
310 | 310 | stdin=subprocess.PIPE, |
|
311 | 311 | stdout=subprocess.PIPE, |
|
312 | 312 | ) |
|
313 | 313 | pout, perr = p.communicate(s) |
|
314 | 314 | return pout |
|
315 | 315 | |
|
316 | 316 | |
|
317 | 317 | def tempfilter(s, cmd): |
|
318 | 318 | """filter string S through a pair of temporary files with CMD. |
|
319 | 319 | CMD is used as a template to create the real command to be run, |
|
320 | 320 | with the strings INFILE and OUTFILE replaced by the real names of |
|
321 | 321 | the temporary files generated.""" |
|
322 | 322 | inname, outname = None, None |
|
323 | 323 | try: |
|
324 | 324 | infd, inname = pycompat.mkstemp(prefix=b'hg-filter-in-') |
|
325 | 325 | fp = os.fdopen(infd, 'wb') |
|
326 | 326 | fp.write(s) |
|
327 | 327 | fp.close() |
|
328 | 328 | outfd, outname = pycompat.mkstemp(prefix=b'hg-filter-out-') |
|
329 | 329 | os.close(outfd) |
|
330 | 330 | cmd = cmd.replace(b'INFILE', inname) |
|
331 | 331 | cmd = cmd.replace(b'OUTFILE', outname) |
|
332 | 332 | code = system(cmd) |
|
333 | 333 | if pycompat.sysplatform == b'OpenVMS' and code & 1: |
|
334 | 334 | code = 0 |
|
335 | 335 | if code: |
|
336 | 336 | raise error.Abort( |
|
337 | 337 | _(b"command '%s' failed: %s") % (cmd, explainexit(code)) |
|
338 | 338 | ) |
|
339 | 339 | with open(outname, b'rb') as fp: |
|
340 | 340 | return fp.read() |
|
341 | 341 | finally: |
|
342 | 342 | try: |
|
343 | 343 | if inname: |
|
344 | 344 | os.unlink(inname) |
|
345 | 345 | except OSError: |
|
346 | 346 | pass |
|
347 | 347 | try: |
|
348 | 348 | if outname: |
|
349 | 349 | os.unlink(outname) |
|
350 | 350 | except OSError: |
|
351 | 351 | pass |
|
352 | 352 | |
|
353 | 353 | |
|
354 | 354 | _filtertable = { |
|
355 | 355 | b'tempfile:': tempfilter, |
|
356 | 356 | b'pipe:': pipefilter, |
|
357 | 357 | } |
|
358 | 358 | |
|
359 | 359 | |
|
360 | 360 | def filter(s, cmd): |
|
361 | 361 | """filter a string through a command that transforms its input to its |
|
362 | 362 | output""" |
|
363 | 363 | for name, fn in pycompat.iteritems(_filtertable): |
|
364 | 364 | if cmd.startswith(name): |
|
365 | 365 | return fn(s, cmd[len(name) :].lstrip()) |
|
366 | 366 | return pipefilter(s, cmd) |
|
367 | 367 | |
|
368 | 368 | |
|
369 | 369 | _hgexecutable = None |
|
370 | 370 | |
|
371 | 371 | |
|
372 | 372 | def hgexecutable(): |
|
373 | 373 | """return location of the 'hg' executable. |
|
374 | 374 | |
|
375 | 375 | Defaults to $HG or 'hg' in the search path. |
|
376 | 376 | """ |
|
377 | 377 | if _hgexecutable is None: |
|
378 | 378 | hg = encoding.environ.get(b'HG') |
|
379 | 379 | mainmod = sys.modules['__main__'] |
|
380 | 380 | if hg: |
|
381 | 381 | _sethgexecutable(hg) |
|
382 | 382 | elif resourceutil.mainfrozen(): |
|
383 | 383 | if getattr(sys, 'frozen', None) == 'macosx_app': |
|
384 | 384 | # Env variable set by py2app |
|
385 | 385 | _sethgexecutable(encoding.environ[b'EXECUTABLEPATH']) |
|
386 | 386 | else: |
|
387 | 387 | _sethgexecutable(pycompat.sysexecutable) |
|
388 | 388 | elif ( |
|
389 | 389 | not pycompat.iswindows |
|
390 | 390 | and os.path.basename(getattr(mainmod, '__file__', '')) == 'hg' |
|
391 | 391 | ): |
|
392 | 392 | _sethgexecutable(pycompat.fsencode(mainmod.__file__)) |
|
393 | 393 | else: |
|
394 | 394 | _sethgexecutable( |
|
395 | 395 | findexe(b'hg') or os.path.basename(pycompat.sysargv[0]) |
|
396 | 396 | ) |
|
397 | 397 | return _hgexecutable |
|
398 | 398 | |
|
399 | 399 | |
|
400 | 400 | def _sethgexecutable(path): |
|
401 | 401 | """set location of the 'hg' executable""" |
|
402 | 402 | global _hgexecutable |
|
403 | 403 | _hgexecutable = path |
|
404 | 404 | |
|
405 | 405 | |
|
406 | 406 | def _testfileno(f, stdf): |
|
407 | 407 | fileno = getattr(f, 'fileno', None) |
|
408 | 408 | try: |
|
409 | 409 | return fileno and fileno() == stdf.fileno() |
|
410 | 410 | except io.UnsupportedOperation: |
|
411 | 411 | return False # fileno() raised UnsupportedOperation |
|
412 | 412 | |
|
413 | 413 | |
|
414 | 414 | def isstdin(f): |
|
415 | 415 | return _testfileno(f, sys.__stdin__) |
|
416 | 416 | |
|
417 | 417 | |
|
418 | 418 | def isstdout(f): |
|
419 | 419 | return _testfileno(f, sys.__stdout__) |
|
420 | 420 | |
|
421 | 421 | |
|
422 | 422 | def protectstdio(uin, uout): |
|
423 | 423 | """Duplicate streams and redirect original if (uin, uout) are stdio |
|
424 | 424 | |
|
425 | 425 | If uin is stdin, it's redirected to /dev/null. If uout is stdout, it's |
|
426 | 426 | redirected to stderr so the output is still readable. |
|
427 | 427 | |
|
428 | 428 | Returns (fin, fout) which point to the original (uin, uout) fds, but |
|
429 | 429 | may be copy of (uin, uout). The returned streams can be considered |
|
430 | 430 | "owned" in that print(), exec(), etc. never reach to them. |
|
431 | 431 | """ |
|
432 | 432 | uout.flush() |
|
433 | 433 | fin, fout = uin, uout |
|
434 | 434 | if _testfileno(uin, stdin): |
|
435 | 435 | newfd = os.dup(uin.fileno()) |
|
436 | 436 | nullfd = os.open(os.devnull, os.O_RDONLY) |
|
437 | 437 | os.dup2(nullfd, uin.fileno()) |
|
438 | 438 | os.close(nullfd) |
|
439 | 439 | fin = os.fdopen(newfd, 'rb') |
|
440 | 440 | if _testfileno(uout, stdout): |
|
441 | 441 | newfd = os.dup(uout.fileno()) |
|
442 | 442 | os.dup2(stderr.fileno(), uout.fileno()) |
|
443 | 443 | fout = os.fdopen(newfd, 'wb') |
|
444 | 444 | return fin, fout |
|
445 | 445 | |
|
446 | 446 | |
|
447 | 447 | def restorestdio(uin, uout, fin, fout): |
|
448 | 448 | """Restore (uin, uout) streams from possibly duplicated (fin, fout)""" |
|
449 | 449 | uout.flush() |
|
450 | 450 | for f, uif in [(fin, uin), (fout, uout)]: |
|
451 | 451 | if f is not uif: |
|
452 | 452 | os.dup2(f.fileno(), uif.fileno()) |
|
453 | 453 | f.close() |
|
454 | 454 | |
|
455 | 455 | |
|
456 | 456 | def shellenviron(environ=None): |
|
457 | 457 | """return environ with optional override, useful for shelling out""" |
|
458 | 458 | |
|
459 | 459 | def py2shell(val): |
|
460 | 460 | """convert python object into string that is useful to shell""" |
|
461 | 461 | if val is None or val is False: |
|
462 | 462 | return b'0' |
|
463 | 463 | if val is True: |
|
464 | 464 | return b'1' |
|
465 | 465 | return pycompat.bytestr(val) |
|
466 | 466 | |
|
467 | 467 | env = dict(encoding.environ) |
|
468 | 468 | if environ: |
|
469 | 469 | env.update((k, py2shell(v)) for k, v in pycompat.iteritems(environ)) |
|
470 | 470 | env[b'HG'] = hgexecutable() |
|
471 | 471 | return env |
|
472 | 472 | |
|
473 | 473 | |
|
474 | 474 | if pycompat.iswindows: |
|
475 | 475 | |
|
476 | 476 | def shelltonative(cmd, env): |
|
477 | 477 | return platform.shelltocmdexe( # pytype: disable=module-attr |
|
478 | 478 | cmd, shellenviron(env) |
|
479 | 479 | ) |
|
480 | 480 | |
|
481 | 481 | tonativestr = encoding.strfromlocal |
|
482 | 482 | else: |
|
483 | 483 | |
|
484 | 484 | def shelltonative(cmd, env): |
|
485 | 485 | return cmd |
|
486 | 486 | |
|
487 | 487 | tonativestr = pycompat.identity |
|
488 | 488 | |
|
489 | 489 | |
|
490 | 490 | def tonativeenv(env): |
|
491 | 491 | """convert the environment from bytes to strings suitable for Popen(), etc.""" |
|
492 | 492 | return pycompat.rapply(tonativestr, env) |
|
493 | 493 | |
|
494 | 494 | |
|
495 | 495 | def system(cmd, environ=None, cwd=None, out=None): |
|
496 | 496 | """enhanced shell command execution. |
|
497 | 497 | run with environment maybe modified, maybe in different dir. |
|
498 | 498 | |
|
499 | 499 | if out is specified, it is assumed to be a file-like object that has a |
|
500 | 500 | write() method. stdout and stderr will be redirected to out.""" |
|
501 | 501 | try: |
|
502 | 502 | stdout.flush() |
|
503 | 503 | except Exception: |
|
504 | 504 | pass |
|
505 | 505 | env = shellenviron(environ) |
|
506 | 506 | if out is None or isstdout(out): |
|
507 | 507 | rc = subprocess.call( |
|
508 | 508 | tonativestr(cmd), |
|
509 | 509 | shell=True, |
|
510 | 510 | close_fds=closefds, |
|
511 | 511 | env=tonativeenv(env), |
|
512 | 512 | cwd=pycompat.rapply(tonativestr, cwd), |
|
513 | 513 | ) |
|
514 | 514 | else: |
|
515 | 515 | proc = subprocess.Popen( |
|
516 | 516 | tonativestr(cmd), |
|
517 | 517 | shell=True, |
|
518 | 518 | close_fds=closefds, |
|
519 | 519 | env=tonativeenv(env), |
|
520 | 520 | cwd=pycompat.rapply(tonativestr, cwd), |
|
521 | 521 | stdout=subprocess.PIPE, |
|
522 | 522 | stderr=subprocess.STDOUT, |
|
523 | 523 | ) |
|
524 | 524 | for line in iter(proc.stdout.readline, b''): |
|
525 | 525 | out.write(line) |
|
526 | 526 | proc.wait() |
|
527 | 527 | rc = proc.returncode |
|
528 | 528 | if pycompat.sysplatform == b'OpenVMS' and rc & 1: |
|
529 | 529 | rc = 0 |
|
530 | 530 | return rc |
|
531 | 531 | |
|
532 | 532 | |
|
533 | 533 | _is_gui = None |
|
534 | 534 | |
|
535 | 535 | |
|
536 | 536 | def _gui(): |
|
537 | 537 | '''Are we running in a GUI?''' |
|
538 | 538 | if pycompat.isdarwin: |
|
539 | 539 | if b'SSH_CONNECTION' in encoding.environ: |
|
540 | 540 | # handle SSH access to a box where the user is logged in |
|
541 | 541 | return False |
|
542 | 542 | elif getattr(osutil, 'isgui', None): |
|
543 | 543 | # check if a CoreGraphics session is available |
|
544 | 544 | return osutil.isgui() |
|
545 | 545 | else: |
|
546 | 546 | # pure build; use a safe default |
|
547 | 547 | return True |
|
548 | 548 | else: |
|
549 | 549 | return ( |
|
550 | 550 | pycompat.iswindows |
|
551 | 551 | or encoding.environ.get(b"DISPLAY") |
|
552 | 552 | or encoding.environ.get(b"WAYLAND_DISPLAY") |
|
553 | 553 | ) |
|
554 | 554 | |
|
555 | 555 | |
|
556 | 556 | def gui(): |
|
557 | 557 | global _is_gui |
|
558 | 558 | if _is_gui is None: |
|
559 | 559 | _is_gui = _gui() |
|
560 | 560 | return _is_gui |
|
561 | 561 | |
|
562 | 562 | |
|
563 | 563 | def hgcmd(): |
|
564 | 564 | """Return the command used to execute current hg |
|
565 | 565 | |
|
566 | 566 | This is different from hgexecutable() because on Windows we want |
|
567 | 567 | to avoid things opening new shell windows like batch files, so we |
|
568 | 568 | get either the python call or current executable. |
|
569 | 569 | """ |
|
570 | 570 | if resourceutil.mainfrozen(): |
|
571 | 571 | if getattr(sys, 'frozen', None) == 'macosx_app': |
|
572 | 572 | # Env variable set by py2app |
|
573 | 573 | return [encoding.environ[b'EXECUTABLEPATH']] |
|
574 | 574 | else: |
|
575 | 575 | return [pycompat.sysexecutable] |
|
576 | 576 | return _gethgcmd() |
|
577 | 577 | |
|
578 | 578 | |
|
579 | 579 | def rundetached(args, condfn): |
|
580 | 580 | """Execute the argument list in a detached process. |
|
581 | 581 | |
|
582 | 582 | condfn is a callable which is called repeatedly and should return |
|
583 | 583 | True once the child process is known to have started successfully. |
|
584 | 584 | At this point, the child process PID is returned. If the child |
|
585 | 585 | process fails to start or finishes before condfn() evaluates to |
|
586 | 586 | True, return -1. |
|
587 | 587 | """ |
|
588 | 588 | # Windows case is easier because the child process is either |
|
589 | 589 | # successfully starting and validating the condition or exiting |
|
590 | 590 | # on failure. We just poll on its PID. On Unix, if the child |
|
591 | 591 | # process fails to start, it will be left in a zombie state until |
|
592 | 592 | # the parent wait on it, which we cannot do since we expect a long |
|
593 | 593 | # running process on success. Instead we listen for SIGCHLD telling |
|
594 | 594 | # us our child process terminated. |
|
595 | 595 | terminated = set() |
|
596 | 596 | |
|
597 | 597 | def handler(signum, frame): |
|
598 | 598 | terminated.add(os.wait()) |
|
599 | 599 | |
|
600 | 600 | prevhandler = None |
|
601 | 601 | SIGCHLD = getattr(signal, 'SIGCHLD', None) |
|
602 | 602 | if SIGCHLD is not None: |
|
603 | 603 | prevhandler = signal.signal(SIGCHLD, handler) |
|
604 | 604 | try: |
|
605 | 605 | pid = spawndetached(args) |
|
606 | 606 | while not condfn(): |
|
607 | 607 | if (pid in terminated or not testpid(pid)) and not condfn(): |
|
608 | 608 | return -1 |
|
609 | 609 | time.sleep(0.1) |
|
610 | 610 | return pid |
|
611 | 611 | finally: |
|
612 | 612 | if prevhandler is not None: |
|
613 | 613 | signal.signal(signal.SIGCHLD, prevhandler) |
|
614 | 614 | |
|
615 | 615 | |
|
616 | 616 | @contextlib.contextmanager |
|
617 | 617 | def uninterruptible(warn): |
|
618 | 618 | """Inhibit SIGINT handling on a region of code. |
|
619 | 619 | |
|
620 | 620 | Note that if this is called in a non-main thread, it turns into a no-op. |
|
621 | 621 | |
|
622 | 622 | Args: |
|
623 | 623 | warn: A callable which takes no arguments, and returns True if the |
|
624 | 624 | previous signal handling should be restored. |
|
625 | 625 | """ |
|
626 | 626 | |
|
627 | 627 | oldsiginthandler = [signal.getsignal(signal.SIGINT)] |
|
628 | 628 | shouldbail = [] |
|
629 | 629 | |
|
630 | 630 | def disabledsiginthandler(*args): |
|
631 | 631 | if warn(): |
|
632 | 632 | signal.signal(signal.SIGINT, oldsiginthandler[0]) |
|
633 | 633 | del oldsiginthandler[0] |
|
634 | 634 | shouldbail.append(True) |
|
635 | 635 | |
|
636 | 636 | try: |
|
637 | 637 | try: |
|
638 | 638 | signal.signal(signal.SIGINT, disabledsiginthandler) |
|
639 | 639 | except ValueError: |
|
640 | 640 | # wrong thread, oh well, we tried |
|
641 | 641 | del oldsiginthandler[0] |
|
642 | 642 | yield |
|
643 | 643 | finally: |
|
644 | 644 | if oldsiginthandler: |
|
645 | 645 | signal.signal(signal.SIGINT, oldsiginthandler[0]) |
|
646 | 646 | if shouldbail: |
|
647 | 647 | raise KeyboardInterrupt |
|
648 | 648 | |
|
649 | 649 | |
|
650 | 650 | if pycompat.iswindows: |
|
651 | 651 | # no fork on Windows, but we can create a detached process |
|
652 | 652 | # https://msdn.microsoft.com/en-us/library/windows/desktop/ms684863.aspx |
|
653 | 653 | # No stdlib constant exists for this value |
|
654 | 654 | DETACHED_PROCESS = 0x00000008 |
|
655 | 655 | # Following creation flags might create a console GUI window. |
|
656 | 656 | # Using subprocess.CREATE_NEW_CONSOLE might helps. |
|
657 | 657 | # See https://phab.mercurial-scm.org/D1701 for discussion |
|
658 | 658 | _creationflags = ( |
|
659 | 659 | DETACHED_PROCESS |
|
660 | 660 | | subprocess.CREATE_NEW_PROCESS_GROUP # pytype: disable=module-attr |
|
661 | 661 | ) |
|
662 | 662 | |
|
663 | 663 | def runbgcommand( |
|
664 | 664 | script, |
|
665 | 665 | env, |
|
666 | 666 | shell=False, |
|
667 | 667 | stdout=None, |
|
668 | 668 | stderr=None, |
|
669 | 669 | ensurestart=True, |
|
670 | 670 | record_wait=None, |
|
671 | 671 | stdin_bytes=None, |
|
672 | 672 | ): |
|
673 | 673 | '''Spawn a command without waiting for it to finish.''' |
|
674 | 674 | # we can't use close_fds *and* redirect stdin. I'm not sure that we |
|
675 | 675 | # need to because the detached process has no console connection. |
|
676 | 676 | |
|
677 | 677 | try: |
|
678 | 678 | stdin = None |
|
679 | 679 | if stdin_bytes is not None: |
|
680 | 680 | stdin = pycompat.unnamedtempfile() |
|
681 | 681 | stdin.write(stdin_bytes) |
|
682 | 682 | stdin.flush() |
|
683 | 683 | stdin.seek(0) |
|
684 | 684 | |
|
685 | 685 | p = subprocess.Popen( |
|
686 | 686 | pycompat.rapply(tonativestr, script), |
|
687 | 687 | shell=shell, |
|
688 | 688 | env=tonativeenv(env), |
|
689 | 689 | close_fds=True, |
|
690 | 690 | creationflags=_creationflags, |
|
691 | 691 | stdin=stdin, |
|
692 | 692 | stdout=stdout, |
|
693 | 693 | stderr=stderr, |
|
694 | 694 | ) |
|
695 | 695 | if record_wait is not None: |
|
696 | 696 | record_wait(p.wait) |
|
697 | 697 | finally: |
|
698 | 698 | if stdin is not None: |
|
699 | 699 | stdin.close() |
|
700 | 700 | |
|
701 | 701 | |
|
702 | 702 | else: |
|
703 | 703 | |
|
704 | 704 | def runbgcommand( |
|
705 | 705 | cmd, |
|
706 | 706 | env, |
|
707 | 707 | shell=False, |
|
708 | 708 | stdout=None, |
|
709 | 709 | stderr=None, |
|
710 | 710 | ensurestart=True, |
|
711 | 711 | record_wait=None, |
|
712 | 712 | stdin_bytes=None, |
|
713 | 713 | ): |
|
714 | 714 | """Spawn a command without waiting for it to finish. |
|
715 | 715 | |
|
716 | 716 | |
|
717 | 717 | When `record_wait` is not None, the spawned process will not be fully |
|
718 | 718 | detached and the `record_wait` argument will be called with a the |
|
719 | 719 | `Subprocess.wait` function for the spawned process. This is mostly |
|
720 | 720 | useful for developers that need to make sure the spawned process |
|
721 | 721 | finished before a certain point. (eg: writing test)""" |
|
722 | 722 | if pycompat.isdarwin: |
|
723 | 723 | # avoid crash in CoreFoundation in case another thread |
|
724 | 724 | # calls gui() while we're calling fork(). |
|
725 | 725 | gui() |
|
726 | 726 | |
|
727 | 727 | # double-fork to completely detach from the parent process |
|
728 | 728 | # based on http://code.activestate.com/recipes/278731 |
|
729 | 729 | if record_wait is None: |
|
730 | 730 | pid = os.fork() |
|
731 | 731 | if pid: |
|
732 | 732 | if not ensurestart: |
|
733 | 733 | # Even though we're not waiting on the child process, |
|
734 | 734 | # we still must call waitpid() on it at some point so |
|
735 | 735 | # it's not a zombie/defunct. This is especially relevant for |
|
736 | 736 | # chg since the parent process won't die anytime soon. |
|
737 | 737 | # We use a thread to make the overhead tiny. |
|
738 | 738 | def _do_wait(): |
|
739 | 739 | os.waitpid(pid, 0) |
|
740 | 740 | |
|
741 | 741 | t = threading.Thread(target=_do_wait) |
|
742 | 742 | t.daemon = True |
|
743 | 743 | t.start() |
|
744 | 744 | return |
|
745 | 745 | # Parent process |
|
746 | 746 | (_pid, status) = os.waitpid(pid, 0) |
|
747 | 747 | if os.WIFEXITED(status): |
|
748 | 748 | returncode = os.WEXITSTATUS(status) |
|
749 | 749 | else: |
|
750 | 750 | returncode = -(os.WTERMSIG(status)) |
|
751 | 751 | if returncode != 0: |
|
752 | 752 | # The child process's return code is 0 on success, an errno |
|
753 | 753 | # value on failure, or 255 if we don't have a valid errno |
|
754 | 754 | # value. |
|
755 | 755 | # |
|
756 | 756 | # (It would be slightly nicer to return the full exception info |
|
757 | 757 | # over a pipe as the subprocess module does. For now it |
|
758 | 758 | # doesn't seem worth adding that complexity here, though.) |
|
759 | 759 | if returncode == 255: |
|
760 | 760 | returncode = errno.EINVAL |
|
761 | 761 | raise OSError( |
|
762 | 762 | returncode, |
|
763 | 763 | b'error running %r: %s' |
|
764 | 764 | % (cmd, os.strerror(returncode)), |
|
765 | 765 | ) |
|
766 | 766 | return |
|
767 | 767 | |
|
768 | 768 | returncode = 255 |
|
769 | 769 | try: |
|
770 | 770 | if record_wait is None: |
|
771 | 771 | # Start a new session |
|
772 | 772 | os.setsid() |
|
773 | 773 | # connect stdin to devnull to make sure the subprocess can't |
|
774 | 774 | # muck up that stream for mercurial. |
|
775 | 775 | if stdin_bytes is None: |
|
776 | 776 | stdin = open(os.devnull, b'r') |
|
777 | 777 | else: |
|
778 | 778 | stdin = pycompat.unnamedtempfile() |
|
779 | 779 | stdin.write(stdin_bytes) |
|
780 | 780 | stdin.flush() |
|
781 | 781 | stdin.seek(0) |
|
782 | 782 | |
|
783 | 783 | if stdout is None: |
|
784 | 784 | stdout = open(os.devnull, b'w') |
|
785 | 785 | if stderr is None: |
|
786 | 786 | stderr = open(os.devnull, b'w') |
|
787 | 787 | |
|
788 | 788 | p = subprocess.Popen( |
|
789 | 789 | cmd, |
|
790 | 790 | shell=shell, |
|
791 | 791 | env=env, |
|
792 | 792 | close_fds=True, |
|
793 | 793 | stdin=stdin, |
|
794 | 794 | stdout=stdout, |
|
795 | 795 | stderr=stderr, |
|
796 | 796 | ) |
|
797 | 797 | if record_wait is not None: |
|
798 | 798 | record_wait(p.wait) |
|
799 | 799 | returncode = 0 |
|
800 | 800 | except EnvironmentError as ex: |
|
801 | 801 | returncode = ex.errno & 0xFF |
|
802 | 802 | if returncode == 0: |
|
803 | 803 | # This shouldn't happen, but just in case make sure the |
|
804 | 804 | # return code is never 0 here. |
|
805 | 805 | returncode = 255 |
|
806 | 806 | except Exception: |
|
807 | 807 | returncode = 255 |
|
808 | 808 | finally: |
|
809 | 809 | # mission accomplished, this child needs to exit and not |
|
810 | 810 | # continue the hg process here. |
|
811 | 811 | stdin.close() |
|
812 | 812 | if record_wait is None: |
|
813 | 813 | os._exit(returncode) |
@@ -1,756 +1,766 b'' | |||
|
1 | 1 | # win32.py - utility functions that use win32 API |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2005-2009 Matt Mackall <mpm@selenic.com> and others |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms of the |
|
6 | 6 | # GNU General Public License version 2 or any later version. |
|
7 | 7 | |
|
8 | 8 | from __future__ import absolute_import |
|
9 | 9 | |
|
10 | 10 | import ctypes |
|
11 | 11 | import ctypes.wintypes as wintypes |
|
12 | 12 | import errno |
|
13 | 13 | import msvcrt |
|
14 | 14 | import os |
|
15 | 15 | import random |
|
16 | 16 | import subprocess |
|
17 | 17 | |
|
18 | 18 | from . import ( |
|
19 | 19 | encoding, |
|
20 | 20 | pycompat, |
|
21 | 21 | ) |
|
22 | 22 | |
|
23 | # pytype: disable=module-attr | |
|
23 | 24 | _kernel32 = ctypes.windll.kernel32 |
|
24 | 25 | _advapi32 = ctypes.windll.advapi32 |
|
25 | 26 | _user32 = ctypes.windll.user32 |
|
26 | 27 | _crypt32 = ctypes.windll.crypt32 |
|
28 | # pytype: enable=module-attr | |
|
27 | 29 | |
|
28 | 30 | _BOOL = ctypes.c_long |
|
29 | 31 | _WORD = ctypes.c_ushort |
|
30 | 32 | _DWORD = ctypes.c_ulong |
|
31 | 33 | _UINT = ctypes.c_uint |
|
32 | 34 | _LONG = ctypes.c_long |
|
33 | 35 | _LPCSTR = _LPSTR = ctypes.c_char_p |
|
34 | 36 | _HANDLE = ctypes.c_void_p |
|
35 | 37 | _HWND = _HANDLE |
|
36 | 38 | _PCCERT_CONTEXT = ctypes.c_void_p |
|
37 | 39 | _MAX_PATH = wintypes.MAX_PATH |
|
38 | 40 | |
|
39 | 41 | _INVALID_HANDLE_VALUE = _HANDLE(-1).value |
|
40 | 42 | |
|
41 | 43 | # GetLastError |
|
42 | 44 | _ERROR_SUCCESS = 0 |
|
43 | 45 | _ERROR_NO_MORE_FILES = 18 |
|
44 | 46 | _ERROR_INVALID_PARAMETER = 87 |
|
45 | 47 | _ERROR_BROKEN_PIPE = 109 |
|
46 | 48 | _ERROR_INSUFFICIENT_BUFFER = 122 |
|
47 | 49 | _ERROR_NO_DATA = 232 |
|
48 | 50 | |
|
49 | 51 | # WPARAM is defined as UINT_PTR (unsigned type) |
|
50 | 52 | # LPARAM is defined as LONG_PTR (signed type) |
|
51 | 53 | if ctypes.sizeof(ctypes.c_long) == ctypes.sizeof(ctypes.c_void_p): |
|
52 | 54 | _WPARAM = ctypes.c_ulong |
|
53 | 55 | _LPARAM = ctypes.c_long |
|
54 | 56 | elif ctypes.sizeof(ctypes.c_longlong) == ctypes.sizeof(ctypes.c_void_p): |
|
55 | 57 | _WPARAM = ctypes.c_ulonglong |
|
56 | 58 | _LPARAM = ctypes.c_longlong |
|
57 | 59 | |
|
58 | 60 | |
|
59 | 61 | class _FILETIME(ctypes.Structure): |
|
60 | 62 | _fields_ = [('dwLowDateTime', _DWORD), ('dwHighDateTime', _DWORD)] |
|
61 | 63 | |
|
62 | 64 | |
|
63 | 65 | class _BY_HANDLE_FILE_INFORMATION(ctypes.Structure): |
|
64 | 66 | _fields_ = [ |
|
65 | 67 | ('dwFileAttributes', _DWORD), |
|
66 | 68 | ('ftCreationTime', _FILETIME), |
|
67 | 69 | ('ftLastAccessTime', _FILETIME), |
|
68 | 70 | ('ftLastWriteTime', _FILETIME), |
|
69 | 71 | ('dwVolumeSerialNumber', _DWORD), |
|
70 | 72 | ('nFileSizeHigh', _DWORD), |
|
71 | 73 | ('nFileSizeLow', _DWORD), |
|
72 | 74 | ('nNumberOfLinks', _DWORD), |
|
73 | 75 | ('nFileIndexHigh', _DWORD), |
|
74 | 76 | ('nFileIndexLow', _DWORD), |
|
75 | 77 | ] |
|
76 | 78 | |
|
77 | 79 | |
|
78 | 80 | # CreateFile |
|
79 | 81 | _FILE_SHARE_READ = 0x00000001 |
|
80 | 82 | _FILE_SHARE_WRITE = 0x00000002 |
|
81 | 83 | _FILE_SHARE_DELETE = 0x00000004 |
|
82 | 84 | |
|
83 | 85 | _OPEN_EXISTING = 3 |
|
84 | 86 | |
|
85 | 87 | _FILE_FLAG_BACKUP_SEMANTICS = 0x02000000 |
|
86 | 88 | |
|
87 | 89 | # SetFileAttributes |
|
88 | 90 | _FILE_ATTRIBUTE_NORMAL = 0x80 |
|
89 | 91 | _FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 0x2000 |
|
90 | 92 | |
|
91 | 93 | # Process Security and Access Rights |
|
92 | 94 | _PROCESS_QUERY_INFORMATION = 0x0400 |
|
93 | 95 | |
|
94 | 96 | # GetExitCodeProcess |
|
95 | 97 | _STILL_ACTIVE = 259 |
|
96 | 98 | |
|
97 | 99 | |
|
98 | 100 | class _STARTUPINFO(ctypes.Structure): |
|
99 | 101 | _fields_ = [ |
|
100 | 102 | ('cb', _DWORD), |
|
101 | 103 | ('lpReserved', _LPSTR), |
|
102 | 104 | ('lpDesktop', _LPSTR), |
|
103 | 105 | ('lpTitle', _LPSTR), |
|
104 | 106 | ('dwX', _DWORD), |
|
105 | 107 | ('dwY', _DWORD), |
|
106 | 108 | ('dwXSize', _DWORD), |
|
107 | 109 | ('dwYSize', _DWORD), |
|
108 | 110 | ('dwXCountChars', _DWORD), |
|
109 | 111 | ('dwYCountChars', _DWORD), |
|
110 | 112 | ('dwFillAttribute', _DWORD), |
|
111 | 113 | ('dwFlags', _DWORD), |
|
112 | 114 | ('wShowWindow', _WORD), |
|
113 | 115 | ('cbReserved2', _WORD), |
|
114 | 116 | ('lpReserved2', ctypes.c_char_p), |
|
115 | 117 | ('hStdInput', _HANDLE), |
|
116 | 118 | ('hStdOutput', _HANDLE), |
|
117 | 119 | ('hStdError', _HANDLE), |
|
118 | 120 | ] |
|
119 | 121 | |
|
120 | 122 | |
|
121 | 123 | class _PROCESS_INFORMATION(ctypes.Structure): |
|
122 | 124 | _fields_ = [ |
|
123 | 125 | ('hProcess', _HANDLE), |
|
124 | 126 | ('hThread', _HANDLE), |
|
125 | 127 | ('dwProcessId', _DWORD), |
|
126 | 128 | ('dwThreadId', _DWORD), |
|
127 | 129 | ] |
|
128 | 130 | |
|
129 | 131 | |
|
130 | 132 | _CREATE_NO_WINDOW = 0x08000000 |
|
131 | 133 | _SW_HIDE = 0 |
|
132 | 134 | |
|
133 | 135 | |
|
134 | 136 | class _COORD(ctypes.Structure): |
|
135 | 137 | _fields_ = [('X', ctypes.c_short), ('Y', ctypes.c_short)] |
|
136 | 138 | |
|
137 | 139 | |
|
138 | 140 | class _SMALL_RECT(ctypes.Structure): |
|
139 | 141 | _fields_ = [ |
|
140 | 142 | ('Left', ctypes.c_short), |
|
141 | 143 | ('Top', ctypes.c_short), |
|
142 | 144 | ('Right', ctypes.c_short), |
|
143 | 145 | ('Bottom', ctypes.c_short), |
|
144 | 146 | ] |
|
145 | 147 | |
|
146 | 148 | |
|
147 | 149 | class _CONSOLE_SCREEN_BUFFER_INFO(ctypes.Structure): |
|
148 | 150 | _fields_ = [ |
|
149 | 151 | ('dwSize', _COORD), |
|
150 | 152 | ('dwCursorPosition', _COORD), |
|
151 | 153 | ('wAttributes', _WORD), |
|
152 | 154 | ('srWindow', _SMALL_RECT), |
|
153 | 155 | ('dwMaximumWindowSize', _COORD), |
|
154 | 156 | ] |
|
155 | 157 | |
|
156 | 158 | |
|
157 | 159 | _STD_OUTPUT_HANDLE = _DWORD(-11).value |
|
158 | 160 | _STD_ERROR_HANDLE = _DWORD(-12).value |
|
159 | 161 | |
|
160 | 162 | # CERT_TRUST_STATUS dwErrorStatus |
|
161 | 163 | CERT_TRUST_IS_PARTIAL_CHAIN = 0x10000 |
|
162 | 164 | |
|
163 | 165 | # CertCreateCertificateContext encodings |
|
164 | 166 | X509_ASN_ENCODING = 0x00000001 |
|
165 | 167 | PKCS_7_ASN_ENCODING = 0x00010000 |
|
166 | 168 | |
|
167 | 169 | # These structs are only complete enough to achieve what we need. |
|
168 | 170 | class CERT_CHAIN_CONTEXT(ctypes.Structure): |
|
169 | 171 | _fields_ = ( |
|
170 | 172 | ("cbSize", _DWORD), |
|
171 | 173 | # CERT_TRUST_STATUS struct |
|
172 | 174 | ("dwErrorStatus", _DWORD), |
|
173 | 175 | ("dwInfoStatus", _DWORD), |
|
174 | 176 | ("cChain", _DWORD), |
|
175 | 177 | ("rgpChain", ctypes.c_void_p), |
|
176 | 178 | ("cLowerQualityChainContext", _DWORD), |
|
177 | 179 | ("rgpLowerQualityChainContext", ctypes.c_void_p), |
|
178 | 180 | ("fHasRevocationFreshnessTime", _BOOL), |
|
179 | 181 | ("dwRevocationFreshnessTime", _DWORD), |
|
180 | 182 | ) |
|
181 | 183 | |
|
182 | 184 | |
|
183 | 185 | class CERT_USAGE_MATCH(ctypes.Structure): |
|
184 | 186 | _fields_ = ( |
|
185 | 187 | ("dwType", _DWORD), |
|
186 | 188 | # CERT_ENHKEY_USAGE struct |
|
187 | 189 | ("cUsageIdentifier", _DWORD), |
|
188 | 190 | ("rgpszUsageIdentifier", ctypes.c_void_p), # LPSTR * |
|
189 | 191 | ) |
|
190 | 192 | |
|
191 | 193 | |
|
192 | 194 | class CERT_CHAIN_PARA(ctypes.Structure): |
|
193 | 195 | _fields_ = ( |
|
194 | 196 | ("cbSize", _DWORD), |
|
195 | 197 | ("RequestedUsage", CERT_USAGE_MATCH), |
|
196 | 198 | ("RequestedIssuancePolicy", CERT_USAGE_MATCH), |
|
197 | 199 | ("dwUrlRetrievalTimeout", _DWORD), |
|
198 | 200 | ("fCheckRevocationFreshnessTime", _BOOL), |
|
199 | 201 | ("dwRevocationFreshnessTime", _DWORD), |
|
200 | 202 | ("pftCacheResync", ctypes.c_void_p), # LPFILETIME |
|
201 | 203 | ("pStrongSignPara", ctypes.c_void_p), # PCCERT_STRONG_SIGN_PARA |
|
202 | 204 | ("dwStrongSignFlags", _DWORD), |
|
203 | 205 | ) |
|
204 | 206 | |
|
205 | 207 | |
|
206 | 208 | # types of parameters of C functions used (required by pypy) |
|
207 | 209 | |
|
208 | 210 | _crypt32.CertCreateCertificateContext.argtypes = [ |
|
209 | 211 | _DWORD, # cert encoding |
|
210 | 212 | ctypes.c_char_p, # cert |
|
211 | 213 | _DWORD, |
|
212 | 214 | ] # cert size |
|
213 | 215 | _crypt32.CertCreateCertificateContext.restype = _PCCERT_CONTEXT |
|
214 | 216 | |
|
215 | 217 | _crypt32.CertGetCertificateChain.argtypes = [ |
|
216 | 218 | ctypes.c_void_p, # HCERTCHAINENGINE |
|
217 | 219 | _PCCERT_CONTEXT, |
|
218 | 220 | ctypes.c_void_p, # LPFILETIME |
|
219 | 221 | ctypes.c_void_p, # HCERTSTORE |
|
220 | 222 | ctypes.c_void_p, # PCERT_CHAIN_PARA |
|
221 | 223 | _DWORD, |
|
222 | 224 | ctypes.c_void_p, # LPVOID |
|
223 | 225 | ctypes.c_void_p, # PCCERT_CHAIN_CONTEXT * |
|
224 | 226 | ] |
|
225 | 227 | _crypt32.CertGetCertificateChain.restype = _BOOL |
|
226 | 228 | |
|
227 | 229 | _crypt32.CertFreeCertificateContext.argtypes = [_PCCERT_CONTEXT] |
|
228 | 230 | _crypt32.CertFreeCertificateContext.restype = _BOOL |
|
229 | 231 | |
|
230 | 232 | _kernel32.CreateFileA.argtypes = [ |
|
231 | 233 | _LPCSTR, |
|
232 | 234 | _DWORD, |
|
233 | 235 | _DWORD, |
|
234 | 236 | ctypes.c_void_p, |
|
235 | 237 | _DWORD, |
|
236 | 238 | _DWORD, |
|
237 | 239 | _HANDLE, |
|
238 | 240 | ] |
|
239 | 241 | _kernel32.CreateFileA.restype = _HANDLE |
|
240 | 242 | |
|
241 | 243 | _kernel32.GetFileInformationByHandle.argtypes = [_HANDLE, ctypes.c_void_p] |
|
242 | 244 | _kernel32.GetFileInformationByHandle.restype = _BOOL |
|
243 | 245 | |
|
244 | 246 | _kernel32.CloseHandle.argtypes = [_HANDLE] |
|
245 | 247 | _kernel32.CloseHandle.restype = _BOOL |
|
246 | 248 | |
|
247 | 249 | try: |
|
248 | 250 | _kernel32.CreateHardLinkA.argtypes = [_LPCSTR, _LPCSTR, ctypes.c_void_p] |
|
249 | 251 | _kernel32.CreateHardLinkA.restype = _BOOL |
|
250 | 252 | except AttributeError: |
|
251 | 253 | pass |
|
252 | 254 | |
|
253 | 255 | _kernel32.SetFileAttributesA.argtypes = [_LPCSTR, _DWORD] |
|
254 | 256 | _kernel32.SetFileAttributesA.restype = _BOOL |
|
255 | 257 | |
|
256 | 258 | _DRIVE_UNKNOWN = 0 |
|
257 | 259 | _DRIVE_NO_ROOT_DIR = 1 |
|
258 | 260 | _DRIVE_REMOVABLE = 2 |
|
259 | 261 | _DRIVE_FIXED = 3 |
|
260 | 262 | _DRIVE_REMOTE = 4 |
|
261 | 263 | _DRIVE_CDROM = 5 |
|
262 | 264 | _DRIVE_RAMDISK = 6 |
|
263 | 265 | |
|
264 | 266 | _kernel32.GetDriveTypeA.argtypes = [_LPCSTR] |
|
265 | 267 | _kernel32.GetDriveTypeA.restype = _UINT |
|
266 | 268 | |
|
267 | 269 | _kernel32.GetVolumeInformationA.argtypes = [ |
|
268 | 270 | _LPCSTR, |
|
269 | 271 | ctypes.c_void_p, |
|
270 | 272 | _DWORD, |
|
271 | 273 | ctypes.c_void_p, |
|
272 | 274 | ctypes.c_void_p, |
|
273 | 275 | ctypes.c_void_p, |
|
274 | 276 | ctypes.c_void_p, |
|
275 | 277 | _DWORD, |
|
276 | 278 | ] |
|
277 | 279 | _kernel32.GetVolumeInformationA.restype = _BOOL |
|
278 | 280 | |
|
279 | 281 | _kernel32.GetVolumePathNameA.argtypes = [_LPCSTR, ctypes.c_void_p, _DWORD] |
|
280 | 282 | _kernel32.GetVolumePathNameA.restype = _BOOL |
|
281 | 283 | |
|
282 | 284 | _kernel32.OpenProcess.argtypes = [_DWORD, _BOOL, _DWORD] |
|
283 | 285 | _kernel32.OpenProcess.restype = _HANDLE |
|
284 | 286 | |
|
285 | 287 | _kernel32.GetExitCodeProcess.argtypes = [_HANDLE, ctypes.c_void_p] |
|
286 | 288 | _kernel32.GetExitCodeProcess.restype = _BOOL |
|
287 | 289 | |
|
288 | 290 | _kernel32.GetLastError.argtypes = [] |
|
289 | 291 | _kernel32.GetLastError.restype = _DWORD |
|
290 | 292 | |
|
291 | 293 | _kernel32.GetModuleFileNameA.argtypes = [_HANDLE, ctypes.c_void_p, _DWORD] |
|
292 | 294 | _kernel32.GetModuleFileNameA.restype = _DWORD |
|
293 | 295 | |
|
294 | 296 | _kernel32.CreateProcessA.argtypes = [ |
|
295 | 297 | _LPCSTR, |
|
296 | 298 | _LPCSTR, |
|
297 | 299 | ctypes.c_void_p, |
|
298 | 300 | ctypes.c_void_p, |
|
299 | 301 | _BOOL, |
|
300 | 302 | _DWORD, |
|
301 | 303 | ctypes.c_void_p, |
|
302 | 304 | _LPCSTR, |
|
303 | 305 | ctypes.c_void_p, |
|
304 | 306 | ctypes.c_void_p, |
|
305 | 307 | ] |
|
306 | 308 | _kernel32.CreateProcessA.restype = _BOOL |
|
307 | 309 | |
|
308 | 310 | _kernel32.ExitProcess.argtypes = [_UINT] |
|
309 | 311 | _kernel32.ExitProcess.restype = None |
|
310 | 312 | |
|
311 | 313 | _kernel32.GetCurrentProcessId.argtypes = [] |
|
312 | 314 | _kernel32.GetCurrentProcessId.restype = _DWORD |
|
313 | 315 | |
|
316 | # pytype: disable=module-attr | |
|
314 | 317 | _SIGNAL_HANDLER = ctypes.WINFUNCTYPE(_BOOL, _DWORD) |
|
318 | # pytype: enable=module-attr | |
|
315 | 319 | _kernel32.SetConsoleCtrlHandler.argtypes = [_SIGNAL_HANDLER, _BOOL] |
|
316 | 320 | _kernel32.SetConsoleCtrlHandler.restype = _BOOL |
|
317 | 321 | |
|
318 | 322 | _kernel32.SetConsoleMode.argtypes = [_HANDLE, _DWORD] |
|
319 | 323 | _kernel32.SetConsoleMode.restype = _BOOL |
|
320 | 324 | |
|
321 | 325 | _kernel32.GetConsoleMode.argtypes = [_HANDLE, ctypes.c_void_p] |
|
322 | 326 | _kernel32.GetConsoleMode.restype = _BOOL |
|
323 | 327 | |
|
324 | 328 | _kernel32.GetStdHandle.argtypes = [_DWORD] |
|
325 | 329 | _kernel32.GetStdHandle.restype = _HANDLE |
|
326 | 330 | |
|
327 | 331 | _kernel32.GetConsoleScreenBufferInfo.argtypes = [_HANDLE, ctypes.c_void_p] |
|
328 | 332 | _kernel32.GetConsoleScreenBufferInfo.restype = _BOOL |
|
329 | 333 | |
|
330 | 334 | _advapi32.GetUserNameA.argtypes = [ctypes.c_void_p, ctypes.c_void_p] |
|
331 | 335 | _advapi32.GetUserNameA.restype = _BOOL |
|
332 | 336 | |
|
333 | 337 | _user32.GetWindowThreadProcessId.argtypes = [_HANDLE, ctypes.c_void_p] |
|
334 | 338 | _user32.GetWindowThreadProcessId.restype = _DWORD |
|
335 | 339 | |
|
336 | 340 | _user32.ShowWindow.argtypes = [_HANDLE, ctypes.c_int] |
|
337 | 341 | _user32.ShowWindow.restype = _BOOL |
|
338 | 342 | |
|
343 | # pytype: disable=module-attr | |
|
339 | 344 | _WNDENUMPROC = ctypes.WINFUNCTYPE(_BOOL, _HWND, _LPARAM) |
|
345 | # pytype: enable=module-attr | |
|
340 | 346 | _user32.EnumWindows.argtypes = [_WNDENUMPROC, _LPARAM] |
|
341 | 347 | _user32.EnumWindows.restype = _BOOL |
|
342 | 348 | |
|
343 | 349 | _kernel32.PeekNamedPipe.argtypes = [ |
|
344 | 350 | _HANDLE, |
|
345 | 351 | ctypes.c_void_p, |
|
346 | 352 | _DWORD, |
|
347 | 353 | ctypes.c_void_p, |
|
348 | 354 | ctypes.c_void_p, |
|
349 | 355 | ctypes.c_void_p, |
|
350 | 356 | ] |
|
351 | 357 | _kernel32.PeekNamedPipe.restype = _BOOL |
|
352 | 358 | |
|
353 | 359 | |
|
354 | 360 | def _raiseoserror(name): |
|
355 | 361 | # Force the code to a signed int to avoid an 'int too large' error. |
|
356 | 362 | # See https://bugs.python.org/issue28474 |
|
357 | 363 | code = _kernel32.GetLastError() |
|
358 | 364 | if code > 0x7FFFFFFF: |
|
359 | 365 | code -= 2 ** 32 |
|
360 | err = ctypes.WinError(code=code) | |
|
366 | err = ctypes.WinError(code=code) # pytype: disable=module-attr | |
|
361 | 367 | raise OSError( |
|
362 | 368 | err.errno, '%s: %s' % (encoding.strfromlocal(name), err.strerror) |
|
363 | 369 | ) |
|
364 | 370 | |
|
365 | 371 | |
|
366 | 372 | def _getfileinfo(name): |
|
367 | 373 | fh = _kernel32.CreateFileA( |
|
368 | 374 | name, |
|
369 | 375 | 0, |
|
370 | 376 | _FILE_SHARE_READ | _FILE_SHARE_WRITE | _FILE_SHARE_DELETE, |
|
371 | 377 | None, |
|
372 | 378 | _OPEN_EXISTING, |
|
373 | 379 | _FILE_FLAG_BACKUP_SEMANTICS, |
|
374 | 380 | None, |
|
375 | 381 | ) |
|
376 | 382 | if fh == _INVALID_HANDLE_VALUE: |
|
377 | 383 | _raiseoserror(name) |
|
378 | 384 | try: |
|
379 | 385 | fi = _BY_HANDLE_FILE_INFORMATION() |
|
380 | 386 | if not _kernel32.GetFileInformationByHandle(fh, ctypes.byref(fi)): |
|
381 | 387 | _raiseoserror(name) |
|
382 | 388 | return fi |
|
383 | 389 | finally: |
|
384 | 390 | _kernel32.CloseHandle(fh) |
|
385 | 391 | |
|
386 | 392 | |
|
387 | 393 | def checkcertificatechain(cert, build=True): |
|
388 | 394 | """Tests the given certificate to see if there is a complete chain to a |
|
389 | 395 | trusted root certificate. As a side effect, missing certificates are |
|
390 | 396 | downloaded and installed unless ``build=False``. True is returned if a |
|
391 | 397 | chain to a trusted root exists (even if built on the fly), otherwise |
|
392 | 398 | False. NB: A chain to a trusted root does NOT imply that the certificate |
|
393 | 399 | is valid. |
|
394 | 400 | """ |
|
395 | 401 | |
|
396 | 402 | chainctxptr = ctypes.POINTER(CERT_CHAIN_CONTEXT) |
|
397 | 403 | |
|
398 | 404 | pchainctx = chainctxptr() |
|
399 | 405 | chainpara = CERT_CHAIN_PARA( |
|
400 | 406 | cbSize=ctypes.sizeof(CERT_CHAIN_PARA), RequestedUsage=CERT_USAGE_MATCH() |
|
401 | 407 | ) |
|
402 | 408 | |
|
403 | 409 | certctx = _crypt32.CertCreateCertificateContext( |
|
404 | 410 | X509_ASN_ENCODING, cert, len(cert) |
|
405 | 411 | ) |
|
406 | 412 | if certctx is None: |
|
407 | 413 | _raiseoserror(b'CertCreateCertificateContext') |
|
408 | 414 | |
|
409 | 415 | flags = 0 |
|
410 | 416 | |
|
411 | 417 | if not build: |
|
412 | 418 | flags |= 0x100 # CERT_CHAIN_DISABLE_AUTH_ROOT_AUTO_UPDATE |
|
413 | 419 | |
|
414 | 420 | try: |
|
415 | 421 | # Building the certificate chain will update root certs as necessary. |
|
416 | 422 | if not _crypt32.CertGetCertificateChain( |
|
417 | 423 | None, # hChainEngine |
|
418 | 424 | certctx, # pCertContext |
|
419 | 425 | None, # pTime |
|
420 | 426 | None, # hAdditionalStore |
|
421 | 427 | ctypes.byref(chainpara), |
|
422 | 428 | flags, |
|
423 | 429 | None, # pvReserved |
|
424 | 430 | ctypes.byref(pchainctx), |
|
425 | 431 | ): |
|
426 | 432 | _raiseoserror(b'CertGetCertificateChain') |
|
427 | 433 | |
|
428 | 434 | chainctx = pchainctx.contents |
|
429 | 435 | |
|
430 | 436 | return chainctx.dwErrorStatus & CERT_TRUST_IS_PARTIAL_CHAIN == 0 |
|
431 | 437 | finally: |
|
432 | 438 | if pchainctx: |
|
433 | 439 | _crypt32.CertFreeCertificateChain(pchainctx) |
|
434 | 440 | _crypt32.CertFreeCertificateContext(certctx) |
|
435 | 441 | |
|
436 | 442 | |
|
437 | 443 | def oslink(src, dst): |
|
438 | 444 | try: |
|
439 | 445 | if not _kernel32.CreateHardLinkA(dst, src, None): |
|
440 | 446 | _raiseoserror(src) |
|
441 | 447 | except AttributeError: # Wine doesn't support this function |
|
442 | 448 | _raiseoserror(src) |
|
443 | 449 | |
|
444 | 450 | |
|
445 | 451 | def nlinks(name): |
|
446 | 452 | '''return number of hardlinks for the given file''' |
|
447 | 453 | return _getfileinfo(name).nNumberOfLinks |
|
448 | 454 | |
|
449 | 455 | |
|
450 | 456 | def samefile(path1, path2): |
|
451 | 457 | '''Returns whether path1 and path2 refer to the same file or directory.''' |
|
452 | 458 | res1 = _getfileinfo(path1) |
|
453 | 459 | res2 = _getfileinfo(path2) |
|
454 | 460 | return ( |
|
455 | 461 | res1.dwVolumeSerialNumber == res2.dwVolumeSerialNumber |
|
456 | 462 | and res1.nFileIndexHigh == res2.nFileIndexHigh |
|
457 | 463 | and res1.nFileIndexLow == res2.nFileIndexLow |
|
458 | 464 | ) |
|
459 | 465 | |
|
460 | 466 | |
|
461 | 467 | def samedevice(path1, path2): |
|
462 | 468 | '''Returns whether path1 and path2 are on the same device.''' |
|
463 | 469 | res1 = _getfileinfo(path1) |
|
464 | 470 | res2 = _getfileinfo(path2) |
|
465 | 471 | return res1.dwVolumeSerialNumber == res2.dwVolumeSerialNumber |
|
466 | 472 | |
|
467 | 473 | |
|
468 | 474 | def peekpipe(pipe): |
|
469 | handle = msvcrt.get_osfhandle(pipe.fileno()) | |
|
475 | handle = msvcrt.get_osfhandle(pipe.fileno()) # pytype: disable=module-attr | |
|
470 | 476 | avail = _DWORD() |
|
471 | 477 | |
|
472 | 478 | if not _kernel32.PeekNamedPipe( |
|
473 | 479 | handle, None, 0, None, ctypes.byref(avail), None |
|
474 | 480 | ): |
|
475 | 481 | err = _kernel32.GetLastError() |
|
476 | 482 | if err == _ERROR_BROKEN_PIPE: |
|
477 | 483 | return 0 |
|
478 | raise ctypes.WinError(err) | |
|
484 | raise ctypes.WinError(err) # pytype: disable=module-attr | |
|
479 | 485 | |
|
480 | 486 | return avail.value |
|
481 | 487 | |
|
482 | 488 | |
|
483 | 489 | def lasterrorwaspipeerror(err): |
|
484 | 490 | if err.errno != errno.EINVAL: |
|
485 | 491 | return False |
|
486 | 492 | err = _kernel32.GetLastError() |
|
487 | 493 | return err == _ERROR_BROKEN_PIPE or err == _ERROR_NO_DATA |
|
488 | 494 | |
|
489 | 495 | |
|
490 | 496 | def testpid(pid): |
|
491 | 497 | """return True if pid is still running or unable to |
|
492 | 498 | determine, False otherwise""" |
|
493 | 499 | h = _kernel32.OpenProcess(_PROCESS_QUERY_INFORMATION, False, pid) |
|
494 | 500 | if h: |
|
495 | 501 | try: |
|
496 | 502 | status = _DWORD() |
|
497 | 503 | if _kernel32.GetExitCodeProcess(h, ctypes.byref(status)): |
|
498 | 504 | return status.value == _STILL_ACTIVE |
|
499 | 505 | finally: |
|
500 | 506 | _kernel32.CloseHandle(h) |
|
501 | 507 | return _kernel32.GetLastError() != _ERROR_INVALID_PARAMETER |
|
502 | 508 | |
|
503 | 509 | |
|
504 | 510 | def executablepath(): |
|
505 | 511 | '''return full path of hg.exe''' |
|
506 | 512 | size = 600 |
|
507 | 513 | buf = ctypes.create_string_buffer(size + 1) |
|
508 | 514 | len = _kernel32.GetModuleFileNameA(None, ctypes.byref(buf), size) |
|
515 | # pytype: disable=module-attr | |
|
509 | 516 | if len == 0: |
|
510 | 517 | raise ctypes.WinError() # Note: WinError is a function |
|
511 | 518 | elif len == size: |
|
512 | 519 | raise ctypes.WinError(_ERROR_INSUFFICIENT_BUFFER) |
|
520 | # pytype: enable=module-attr | |
|
513 | 521 | return buf.value |
|
514 | 522 | |
|
515 | 523 | |
|
516 | 524 | def getvolumename(path): |
|
517 | 525 | """Get the mount point of the filesystem from a directory or file |
|
518 | 526 | (best-effort) |
|
519 | 527 | |
|
520 | 528 | Returns None if we are unsure. Raises OSError on ENOENT, EPERM, etc. |
|
521 | 529 | """ |
|
522 | 530 | # realpath() calls GetFullPathName() |
|
523 | 531 | realpath = os.path.realpath(path) |
|
524 | 532 | |
|
525 | 533 | # allocate at least MAX_PATH long since GetVolumePathName('c:\\', buf, 4) |
|
526 | 534 | # somehow fails on Windows XP |
|
527 | 535 | size = max(len(realpath), _MAX_PATH) + 1 |
|
528 | 536 | buf = ctypes.create_string_buffer(size) |
|
529 | 537 | |
|
530 | 538 | if not _kernel32.GetVolumePathNameA(realpath, ctypes.byref(buf), size): |
|
531 |
|
|
|
539 | # Note: WinError is a function | |
|
540 | raise ctypes.WinError() # pytype: disable=module-attr | |
|
532 | 541 | |
|
533 | 542 | return buf.value |
|
534 | 543 | |
|
535 | 544 | |
|
536 | 545 | def getfstype(path): |
|
537 | 546 | """Get the filesystem type name from a directory or file (best-effort) |
|
538 | 547 | |
|
539 | 548 | Returns None if we are unsure. Raises OSError on ENOENT, EPERM, etc. |
|
540 | 549 | """ |
|
541 | 550 | volume = getvolumename(path) |
|
542 | 551 | |
|
543 | 552 | t = _kernel32.GetDriveTypeA(volume) |
|
544 | 553 | |
|
545 | 554 | if t == _DRIVE_REMOTE: |
|
546 | 555 | return b'cifs' |
|
547 | 556 | elif t not in ( |
|
548 | 557 | _DRIVE_REMOVABLE, |
|
549 | 558 | _DRIVE_FIXED, |
|
550 | 559 | _DRIVE_CDROM, |
|
551 | 560 | _DRIVE_RAMDISK, |
|
552 | 561 | ): |
|
553 | 562 | return None |
|
554 | 563 | |
|
555 | 564 | size = _MAX_PATH + 1 |
|
556 | 565 | name = ctypes.create_string_buffer(size) |
|
557 | 566 | |
|
558 | 567 | if not _kernel32.GetVolumeInformationA( |
|
559 | 568 | volume, None, 0, None, None, None, ctypes.byref(name), size |
|
560 | 569 | ): |
|
561 |
|
|
|
570 | # Note: WinError is a function | |
|
571 | raise ctypes.WinError() # pytype: disable=module-attr | |
|
562 | 572 | |
|
563 | 573 | return name.value |
|
564 | 574 | |
|
565 | 575 | |
|
566 | 576 | def getuser(): |
|
567 | 577 | '''return name of current user''' |
|
568 | 578 | size = _DWORD(300) |
|
569 | 579 | buf = ctypes.create_string_buffer(size.value + 1) |
|
570 | 580 | if not _advapi32.GetUserNameA(ctypes.byref(buf), ctypes.byref(size)): |
|
571 | raise ctypes.WinError() | |
|
581 | raise ctypes.WinError() # pytype: disable=module-attr | |
|
572 | 582 | return buf.value |
|
573 | 583 | |
|
574 | 584 | |
|
575 | 585 | _signalhandler = [] |
|
576 | 586 | |
|
577 | 587 | |
|
578 | 588 | def setsignalhandler(): |
|
579 | 589 | """Register a termination handler for console events including |
|
580 | 590 | CTRL+C. python signal handlers do not work well with socket |
|
581 | 591 | operations. |
|
582 | 592 | """ |
|
583 | 593 | |
|
584 | 594 | def handler(event): |
|
585 | 595 | _kernel32.ExitProcess(1) |
|
586 | 596 | |
|
587 | 597 | if _signalhandler: |
|
588 | 598 | return # already registered |
|
589 | 599 | h = _SIGNAL_HANDLER(handler) |
|
590 | 600 | _signalhandler.append(h) # needed to prevent garbage collection |
|
591 | 601 | if not _kernel32.SetConsoleCtrlHandler(h, True): |
|
592 | raise ctypes.WinError() | |
|
602 | raise ctypes.WinError() # pytype: disable=module-attr | |
|
593 | 603 | |
|
594 | 604 | |
|
595 | 605 | def hidewindow(): |
|
596 | 606 | def callback(hwnd, pid): |
|
597 | 607 | wpid = _DWORD() |
|
598 | 608 | _user32.GetWindowThreadProcessId(hwnd, ctypes.byref(wpid)) |
|
599 | 609 | if pid == wpid.value: |
|
600 | 610 | _user32.ShowWindow(hwnd, _SW_HIDE) |
|
601 | 611 | return False # stop enumerating windows |
|
602 | 612 | return True |
|
603 | 613 | |
|
604 | 614 | pid = _kernel32.GetCurrentProcessId() |
|
605 | 615 | _user32.EnumWindows(_WNDENUMPROC(callback), pid) |
|
606 | 616 | |
|
607 | 617 | |
|
608 | 618 | def termsize(): |
|
609 | 619 | # cmd.exe does not handle CR like a unix console, the CR is |
|
610 | 620 | # counted in the line length. On 80 columns consoles, if 80 |
|
611 | 621 | # characters are written, the following CR won't apply on the |
|
612 | 622 | # current line but on the new one. Keep room for it. |
|
613 | 623 | width = 80 - 1 |
|
614 | 624 | height = 25 |
|
615 | 625 | # Query stderr to avoid problems with redirections |
|
616 | 626 | screenbuf = _kernel32.GetStdHandle( |
|
617 | 627 | _STD_ERROR_HANDLE |
|
618 | 628 | ) # don't close the handle returned |
|
619 | 629 | if screenbuf is None or screenbuf == _INVALID_HANDLE_VALUE: |
|
620 | 630 | return width, height |
|
621 | 631 | csbi = _CONSOLE_SCREEN_BUFFER_INFO() |
|
622 | 632 | if not _kernel32.GetConsoleScreenBufferInfo(screenbuf, ctypes.byref(csbi)): |
|
623 | 633 | return width, height |
|
624 | 634 | width = csbi.srWindow.Right - csbi.srWindow.Left # don't '+ 1' |
|
625 | 635 | height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1 |
|
626 | 636 | return width, height |
|
627 | 637 | |
|
628 | 638 | |
|
629 | 639 | def enablevtmode(): |
|
630 | 640 | """Enable virtual terminal mode for the associated console. Return True if |
|
631 | 641 | enabled, else False.""" |
|
632 | 642 | |
|
633 | 643 | ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x4 |
|
634 | 644 | |
|
635 | 645 | handle = _kernel32.GetStdHandle( |
|
636 | 646 | _STD_OUTPUT_HANDLE |
|
637 | 647 | ) # don't close the handle |
|
638 | 648 | if handle == _INVALID_HANDLE_VALUE: |
|
639 | 649 | return False |
|
640 | 650 | |
|
641 | 651 | mode = _DWORD(0) |
|
642 | 652 | |
|
643 | 653 | if not _kernel32.GetConsoleMode(handle, ctypes.byref(mode)): |
|
644 | 654 | return False |
|
645 | 655 | |
|
646 | 656 | if (mode.value & ENABLE_VIRTUAL_TERMINAL_PROCESSING) == 0: |
|
647 | 657 | mode.value |= ENABLE_VIRTUAL_TERMINAL_PROCESSING |
|
648 | 658 | |
|
649 | 659 | if not _kernel32.SetConsoleMode(handle, mode): |
|
650 | 660 | return False |
|
651 | 661 | |
|
652 | 662 | return True |
|
653 | 663 | |
|
654 | 664 | |
|
655 | 665 | def spawndetached(args): |
|
656 | 666 | # No standard library function really spawns a fully detached |
|
657 | 667 | # process under win32 because they allocate pipes or other objects |
|
658 | 668 | # to handle standard streams communications. Passing these objects |
|
659 | 669 | # to the child process requires handle inheritance to be enabled |
|
660 | 670 | # which makes really detached processes impossible. |
|
661 | 671 | si = _STARTUPINFO() |
|
662 | 672 | si.cb = ctypes.sizeof(_STARTUPINFO) |
|
663 | 673 | |
|
664 | 674 | pi = _PROCESS_INFORMATION() |
|
665 | 675 | |
|
666 | 676 | env = b'' |
|
667 | 677 | for k in encoding.environ: |
|
668 | 678 | env += b"%s=%s\0" % (k, encoding.environ[k]) |
|
669 | 679 | if not env: |
|
670 | 680 | env = b'\0' |
|
671 | 681 | env += b'\0' |
|
672 | 682 | |
|
673 | 683 | args = subprocess.list2cmdline(pycompat.rapply(encoding.strfromlocal, args)) |
|
674 | 684 | |
|
675 | 685 | # TODO: CreateProcessW on py3? |
|
676 | 686 | res = _kernel32.CreateProcessA( |
|
677 | 687 | None, |
|
678 | 688 | encoding.strtolocal(args), |
|
679 | 689 | None, |
|
680 | 690 | None, |
|
681 | 691 | False, |
|
682 | 692 | _CREATE_NO_WINDOW, |
|
683 | 693 | env, |
|
684 | 694 | encoding.getcwd(), |
|
685 | 695 | ctypes.byref(si), |
|
686 | 696 | ctypes.byref(pi), |
|
687 | 697 | ) |
|
688 | 698 | if not res: |
|
689 | raise ctypes.WinError() | |
|
699 | raise ctypes.WinError() # pytype: disable=module-attr | |
|
690 | 700 | |
|
691 | 701 | _kernel32.CloseHandle(pi.hProcess) |
|
692 | 702 | _kernel32.CloseHandle(pi.hThread) |
|
693 | 703 | |
|
694 | 704 | return pi.dwProcessId |
|
695 | 705 | |
|
696 | 706 | |
|
697 | 707 | def unlink(f): |
|
698 | 708 | '''try to implement POSIX' unlink semantics on Windows''' |
|
699 | 709 | |
|
700 | 710 | if os.path.isdir(f): |
|
701 | 711 | # use EPERM because it is POSIX prescribed value, even though |
|
702 | 712 | # unlink(2) on directories returns EISDIR on Linux |
|
703 | 713 | raise IOError( |
|
704 | 714 | errno.EPERM, |
|
705 | 715 | r"Unlinking directory not permitted: '%s'" |
|
706 | 716 | % encoding.strfromlocal(f), |
|
707 | 717 | ) |
|
708 | 718 | |
|
709 | 719 | # POSIX allows to unlink and rename open files. Windows has serious |
|
710 | 720 | # problems with doing that: |
|
711 | 721 | # - Calling os.unlink (or os.rename) on a file f fails if f or any |
|
712 | 722 | # hardlinked copy of f has been opened with Python's open(). There is no |
|
713 | 723 | # way such a file can be deleted or renamed on Windows (other than |
|
714 | 724 | # scheduling the delete or rename for the next reboot). |
|
715 | 725 | # - Calling os.unlink on a file that has been opened with Mercurial's |
|
716 | 726 | # posixfile (or comparable methods) will delay the actual deletion of |
|
717 | 727 | # the file for as long as the file is held open. The filename is blocked |
|
718 | 728 | # during that time and cannot be used for recreating a new file under |
|
719 | 729 | # that same name ("zombie file"). Directories containing such zombie files |
|
720 | 730 | # cannot be removed or moved. |
|
721 | 731 | # A file that has been opened with posixfile can be renamed, so we rename |
|
722 | 732 | # f to a random temporary name before calling os.unlink on it. This allows |
|
723 | 733 | # callers to recreate f immediately while having other readers do their |
|
724 | 734 | # implicit zombie filename blocking on a temporary name. |
|
725 | 735 | |
|
726 | 736 | for tries in pycompat.xrange(10): |
|
727 | 737 | temp = b'%s-%08x' % (f, random.randint(0, 0xFFFFFFFF)) |
|
728 | 738 | try: |
|
729 | 739 | os.rename(f, temp) # raises OSError EEXIST if temp exists |
|
730 | 740 | break |
|
731 | 741 | except OSError as e: |
|
732 | 742 | if e.errno != errno.EEXIST: |
|
733 | 743 | raise |
|
734 | 744 | else: |
|
735 | 745 | raise IOError(errno.EEXIST, "No usable temporary filename found") |
|
736 | 746 | |
|
737 | 747 | try: |
|
738 | 748 | os.unlink(temp) |
|
739 | 749 | except OSError: |
|
740 | 750 | # The unlink might have failed because the READONLY attribute may heave |
|
741 | 751 | # been set on the original file. Rename works fine with READONLY set, |
|
742 | 752 | # but not os.unlink. Reset all attributes and try again. |
|
743 | 753 | _kernel32.SetFileAttributesA(temp, _FILE_ATTRIBUTE_NORMAL) |
|
744 | 754 | try: |
|
745 | 755 | os.unlink(temp) |
|
746 | 756 | except OSError: |
|
747 | 757 | # The unlink might have failed due to some very rude AV-Scanners. |
|
748 | 758 | # Leaking a tempfile is the lesser evil than aborting here and |
|
749 | 759 | # leaving some potentially serious inconsistencies. |
|
750 | 760 | pass |
|
751 | 761 | |
|
752 | 762 | |
|
753 | 763 | def makedir(path, notindexed): |
|
754 | 764 | os.mkdir(path) |
|
755 | 765 | if notindexed: |
|
756 | 766 | _kernel32.SetFileAttributesA(path, _FILE_ATTRIBUTE_NOT_CONTENT_INDEXED) |
General Comments 0
You need to be logged in to leave comments.
Login now