Mercurial > public > mercurial-scm > hg-stable
annotate mercurial/windows.py @ 49907:58dff81ffba1
typing: add type hints to the common posix/windows platform functions
These are done in sync because some platforms have empty implementations, and it
isn't obvious what the types should be without examining the other. We want the
types aligned, so @overload definitions that differ aren't generated. The only
differences here are the few methods that unconditionally raise an error are
marked as `NoReturn`, which doesn't seem to bother pytype.
A couple of the posix module functions needed to be updated with a modern
ternary operator, because pytype seems to want to use the type of the second
object in the old `return x and y` style.
author | Matt Harbison <matt_harbison@yahoo.com> |
---|---|
date | Fri, 16 Dec 2022 00:54:39 -0500 |
parents | c43b283a19c3 |
children | 464fe8b8f474 |
rev | line source |
---|---|
8226
8b2cd04a6e97
put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
1 # windows.py - Windows utility function implementations for Mercurial |
8b2cd04a6e97
put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
2 # |
46819
d4ba4d51f85f
contributor: change mentions of mpm to olivia
Rapha?l Gom?s <rgomes@octobus.net>
parents:
45957
diff
changeset
|
3 # Copyright 2005-2009 Olivia Mackall <olivia@selenic.com> and others |
8226
8b2cd04a6e97
put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
4 # |
8b2cd04a6e97
put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
5 # This software may be used and distributed according to the terms of the |
10263 | 6 # GNU General Public License version 2 or any later version. |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
7 |
8421
b6d0fa8c7685
posixfile: remove posixfile_nt and fix import bug in windows.py
Sune Foldager <cryo@cyanite.org>
parents:
8364
diff
changeset
|
8 |
27360
6daa795ed32f
windows: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26375
diff
changeset
|
9 import errno |
43910
1b26cb044097
windows: if username(uid=None) is loaded, just use getpass
Augie Fackler <augie@google.com>
parents:
43807
diff
changeset
|
10 import getpass |
49325
a75b530cfc29
typing: add a missing suppression directive for `msvcrt`
Matt Harbison <matt_harbison@yahoo.com>
parents:
49324
diff
changeset
|
11 import msvcrt # pytype: disable=import-error |
27360
6daa795ed32f
windows: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26375
diff
changeset
|
12 import os |
6daa795ed32f
windows: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26375
diff
changeset
|
13 import re |
6daa795ed32f
windows: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26375
diff
changeset
|
14 import stat |
38487
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
15 import string |
27360
6daa795ed32f
windows: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26375
diff
changeset
|
16 import sys |
49324
6b39c7265935
windows: drop some py2 compatibility code
Matt Harbison <matt_harbison@yahoo.com>
parents:
49323
diff
changeset
|
17 import winreg # pytype: disable=import-error |
27360
6daa795ed32f
windows: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26375
diff
changeset
|
18 |
49888
8147abc05794
pytype: stop excluding mercurial/ui.py
Matt Harbison <matt_harbison@yahoo.com>
parents:
49426
diff
changeset
|
19 from typing import ( |
8147abc05794
pytype: stop excluding mercurial/ui.py
Matt Harbison <matt_harbison@yahoo.com>
parents:
49426
diff
changeset
|
20 BinaryIO, |
49907
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
21 Iterable, |
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
22 Iterator, |
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
23 List, |
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
24 NoReturn, |
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
25 Optional, |
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
26 Sequence, |
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
27 Union, |
49888
8147abc05794
pytype: stop excluding mercurial/ui.py
Matt Harbison <matt_harbison@yahoo.com>
parents:
49426
diff
changeset
|
28 ) |
8147abc05794
pytype: stop excluding mercurial/ui.py
Matt Harbison <matt_harbison@yahoo.com>
parents:
49426
diff
changeset
|
29 |
27360
6daa795ed32f
windows: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26375
diff
changeset
|
30 from .i18n import _ |
43089
c59eb1560c44
py3: manually import getattr where it is needed
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43077
diff
changeset
|
31 from .pycompat import getattr |
27360
6daa795ed32f
windows: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26375
diff
changeset
|
32 from . import ( |
6daa795ed32f
windows: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26375
diff
changeset
|
33 encoding, |
33649
739cc0f9cbb4
ssh: ban any username@host or host that starts with - (SEC)
Augie Fackler <augie@google.com>
parents:
32706
diff
changeset
|
34 error, |
32406
a9c71d578a1c
osutil: switch to policy importer
Yuya Nishihara <yuya@tcha.org>
parents:
32331
diff
changeset
|
35 policy, |
30617
d623cc6b3742
py3: replace os.pathsep with pycompat.ospathsep
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30483
diff
changeset
|
36 pycompat, |
49888
8147abc05794
pytype: stop excluding mercurial/ui.py
Matt Harbison <matt_harbison@yahoo.com>
parents:
49426
diff
changeset
|
37 typelib, |
27436
912255f8f087
windows: correct the import of win32
Matt Harbison <matt_harbison@yahoo.com>
parents:
27360
diff
changeset
|
38 win32, |
27360
6daa795ed32f
windows: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26375
diff
changeset
|
39 ) |
6daa795ed32f
windows: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26375
diff
changeset
|
40 |
29772
3df9f780c90e
py3: conditionalize _winreg import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29530
diff
changeset
|
41 |
43554
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43089
diff
changeset
|
42 osutil = policy.importmod('osutil') |
32406
a9c71d578a1c
osutil: switch to policy importer
Yuya Nishihara <yuya@tcha.org>
parents:
32331
diff
changeset
|
43 |
35517
c4caf530b1c7
util: add a function to show the mount point of the filesystem
Matt Harbison <matt_harbison@yahoo.com>
parents:
35514
diff
changeset
|
44 getfsmountpoint = win32.getvolumename |
35514
2062f7c2ac83
win32: implement util.getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents:
35513
diff
changeset
|
45 getfstype = win32.getfstype |
15016
871c77e78f5d
windows: fix pyflakes warning on unused imports
Matt Mackall <mpm@selenic.com>
parents:
15011
diff
changeset
|
46 getuser = win32.getuser |
871c77e78f5d
windows: fix pyflakes warning on unused imports
Matt Mackall <mpm@selenic.com>
parents:
15011
diff
changeset
|
47 hidewindow = win32.hidewindow |
871c77e78f5d
windows: fix pyflakes warning on unused imports
Matt Mackall <mpm@selenic.com>
parents:
15011
diff
changeset
|
48 makedir = win32.makedir |
871c77e78f5d
windows: fix pyflakes warning on unused imports
Matt Mackall <mpm@selenic.com>
parents:
15011
diff
changeset
|
49 nlinks = win32.nlinks |
871c77e78f5d
windows: fix pyflakes warning on unused imports
Matt Mackall <mpm@selenic.com>
parents:
15011
diff
changeset
|
50 oslink = win32.oslink |
871c77e78f5d
windows: fix pyflakes warning on unused imports
Matt Mackall <mpm@selenic.com>
parents:
15011
diff
changeset
|
51 samedevice = win32.samedevice |
871c77e78f5d
windows: fix pyflakes warning on unused imports
Matt Mackall <mpm@selenic.com>
parents:
15011
diff
changeset
|
52 samefile = win32.samefile |
871c77e78f5d
windows: fix pyflakes warning on unused imports
Matt Mackall <mpm@selenic.com>
parents:
15011
diff
changeset
|
53 setsignalhandler = win32.setsignalhandler |
871c77e78f5d
windows: fix pyflakes warning on unused imports
Matt Mackall <mpm@selenic.com>
parents:
15011
diff
changeset
|
54 spawndetached = win32.spawndetached |
17560
9ee25d7b1aed
util: implement a faster os.path.split for posix systems
Bryan O'Sullivan <bryano@fb.com>
parents:
17537
diff
changeset
|
55 split = os.path.split |
15016
871c77e78f5d
windows: fix pyflakes warning on unused imports
Matt Mackall <mpm@selenic.com>
parents:
15011
diff
changeset
|
56 testpid = win32.testpid |
871c77e78f5d
windows: fix pyflakes warning on unused imports
Matt Mackall <mpm@selenic.com>
parents:
15011
diff
changeset
|
57 unlink = win32.unlink |
14985
dbf91976f900
windows: eliminate win32 wildcard import
Adrian Buehlmann <adrian@cadifra.com>
parents:
14969
diff
changeset
|
58 |
25658
e93036747902
global: mass rewrite to use modern octal syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25462
diff
changeset
|
59 umask = 0o022 |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
60 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
61 |
49037
642e31cb55f0
py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48990
diff
changeset
|
62 class mixedfilemodewrapper: |
26375
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
63 """Wraps a file handle when it is opened in read/write mode. |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
64 |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
65 fopen() and fdopen() on Windows have a specific-to-Windows requirement |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
66 that files opened with mode r+, w+, or a+ make a call to a file positioning |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
67 function when switching between reads and writes. Without this extra call, |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
68 Python will raise a not very intuitive "IOError: [Errno 0] Error." |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
69 |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
70 This class wraps posixfile instances when the file is opened in read/write |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
71 mode and automatically adds checks or inserts appropriate file positioning |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
72 calls when necessary. |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
73 """ |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
74 |
26375
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
75 OPNONE = 0 |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
76 OPREAD = 1 |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
77 OPWRITE = 2 |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
78 |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
79 def __init__(self, fp): |
43554
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43089
diff
changeset
|
80 object.__setattr__(self, '_fp', fp) |
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43089
diff
changeset
|
81 object.__setattr__(self, '_lastop', 0) |
26375
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
82 |
31891
87f293edabb6
windows: add context manager support to mixedfilemodewrapper
Matt Harbison <matt_harbison@yahoo.com>
parents:
31649
diff
changeset
|
83 def __enter__(self): |
40467
9ae4aed27930
windows: ensure mixedfilemodewrapper fd doesn't escape by entering context mgr
Matt Harbison <matt_harbison@yahoo.com>
parents:
40466
diff
changeset
|
84 self._fp.__enter__() |
9ae4aed27930
windows: ensure mixedfilemodewrapper fd doesn't escape by entering context mgr
Matt Harbison <matt_harbison@yahoo.com>
parents:
40466
diff
changeset
|
85 return self |
31891
87f293edabb6
windows: add context manager support to mixedfilemodewrapper
Matt Harbison <matt_harbison@yahoo.com>
parents:
31649
diff
changeset
|
86 |
87f293edabb6
windows: add context manager support to mixedfilemodewrapper
Matt Harbison <matt_harbison@yahoo.com>
parents:
31649
diff
changeset
|
87 def __exit__(self, exc_type, exc_val, exc_tb): |
87f293edabb6
windows: add context manager support to mixedfilemodewrapper
Matt Harbison <matt_harbison@yahoo.com>
parents:
31649
diff
changeset
|
88 self._fp.__exit__(exc_type, exc_val, exc_tb) |
87f293edabb6
windows: add context manager support to mixedfilemodewrapper
Matt Harbison <matt_harbison@yahoo.com>
parents:
31649
diff
changeset
|
89 |
26375
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
90 def __getattr__(self, name): |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
91 return getattr(self._fp, name) |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
92 |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
93 def __setattr__(self, name, value): |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
94 return self._fp.__setattr__(name, value) |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
95 |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
96 def _noopseek(self): |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
97 self._fp.seek(0, os.SEEK_CUR) |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
98 |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
99 def seek(self, *args, **kwargs): |
43554
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43089
diff
changeset
|
100 object.__setattr__(self, '_lastop', self.OPNONE) |
26375
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
101 return self._fp.seek(*args, **kwargs) |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
102 |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
103 def write(self, d): |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
104 if self._lastop == self.OPREAD: |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
105 self._noopseek() |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
106 |
43554
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43089
diff
changeset
|
107 object.__setattr__(self, '_lastop', self.OPWRITE) |
26375
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
108 return self._fp.write(d) |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
109 |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
110 def writelines(self, *args, **kwargs): |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
111 if self._lastop == self.OPREAD: |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
112 self._noopeseek() |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
113 |
43554
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43089
diff
changeset
|
114 object.__setattr__(self, '_lastop', self.OPWRITE) |
26375
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
115 return self._fp.writelines(*args, **kwargs) |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
116 |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
117 def read(self, *args, **kwargs): |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
118 if self._lastop == self.OPWRITE: |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
119 self._noopseek() |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
120 |
43554
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43089
diff
changeset
|
121 object.__setattr__(self, '_lastop', self.OPREAD) |
26375
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
122 return self._fp.read(*args, **kwargs) |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
123 |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
124 def readline(self, *args, **kwargs): |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
125 if self._lastop == self.OPWRITE: |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
126 self._noopseek() |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
127 |
43554
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43089
diff
changeset
|
128 object.__setattr__(self, '_lastop', self.OPREAD) |
26375
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
129 return self._fp.readline(*args, **kwargs) |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
130 |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
131 def readlines(self, *args, **kwargs): |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
132 if self._lastop == self.OPWRITE: |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
133 self._noopseek() |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
134 |
43554
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43089
diff
changeset
|
135 object.__setattr__(self, '_lastop', self.OPREAD) |
26375
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
136 return self._fp.readlines(*args, **kwargs) |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
137 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
138 |
49037
642e31cb55f0
py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48990
diff
changeset
|
139 class fdproxy: |
39825
68ea1f8dcb84
py3: proxy posixfile objects to re-add a useful 'name' attribute on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
39735
diff
changeset
|
140 """Wraps osutil.posixfile() to override the name attribute to reflect the |
68ea1f8dcb84
py3: proxy posixfile objects to re-add a useful 'name' attribute on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
39735
diff
changeset
|
141 underlying file name. |
68ea1f8dcb84
py3: proxy posixfile objects to re-add a useful 'name' attribute on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
39735
diff
changeset
|
142 """ |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
143 |
39825
68ea1f8dcb84
py3: proxy posixfile objects to re-add a useful 'name' attribute on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
39735
diff
changeset
|
144 def __init__(self, name, fp): |
68ea1f8dcb84
py3: proxy posixfile objects to re-add a useful 'name' attribute on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
39735
diff
changeset
|
145 self.name = name |
68ea1f8dcb84
py3: proxy posixfile objects to re-add a useful 'name' attribute on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
39735
diff
changeset
|
146 self._fp = fp |
68ea1f8dcb84
py3: proxy posixfile objects to re-add a useful 'name' attribute on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
39735
diff
changeset
|
147 |
68ea1f8dcb84
py3: proxy posixfile objects to re-add a useful 'name' attribute on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
39735
diff
changeset
|
148 def __enter__(self): |
40466
e11e03f72baf
py3: ensure the proxied Windows fd doesn't escape by entering context manager
Matt Harbison <matt_harbison@yahoo.com>
parents:
40266
diff
changeset
|
149 self._fp.__enter__() |
e11e03f72baf
py3: ensure the proxied Windows fd doesn't escape by entering context manager
Matt Harbison <matt_harbison@yahoo.com>
parents:
40266
diff
changeset
|
150 # Return this wrapper for the context manager so that the name is |
e11e03f72baf
py3: ensure the proxied Windows fd doesn't escape by entering context manager
Matt Harbison <matt_harbison@yahoo.com>
parents:
40266
diff
changeset
|
151 # still available. |
e11e03f72baf
py3: ensure the proxied Windows fd doesn't escape by entering context manager
Matt Harbison <matt_harbison@yahoo.com>
parents:
40266
diff
changeset
|
152 return self |
39825
68ea1f8dcb84
py3: proxy posixfile objects to re-add a useful 'name' attribute on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
39735
diff
changeset
|
153 |
68ea1f8dcb84
py3: proxy posixfile objects to re-add a useful 'name' attribute on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
39735
diff
changeset
|
154 def __exit__(self, exc_type, exc_value, traceback): |
68ea1f8dcb84
py3: proxy posixfile objects to re-add a useful 'name' attribute on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
39735
diff
changeset
|
155 self._fp.__exit__(exc_type, exc_value, traceback) |
68ea1f8dcb84
py3: proxy posixfile objects to re-add a useful 'name' attribute on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
39735
diff
changeset
|
156 |
68ea1f8dcb84
py3: proxy posixfile objects to re-add a useful 'name' attribute on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
39735
diff
changeset
|
157 def __iter__(self): |
68ea1f8dcb84
py3: proxy posixfile objects to re-add a useful 'name' attribute on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
39735
diff
changeset
|
158 return iter(self._fp) |
68ea1f8dcb84
py3: proxy posixfile objects to re-add a useful 'name' attribute on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
39735
diff
changeset
|
159 |
68ea1f8dcb84
py3: proxy posixfile objects to re-add a useful 'name' attribute on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
39735
diff
changeset
|
160 def __getattr__(self, name): |
68ea1f8dcb84
py3: proxy posixfile objects to re-add a useful 'name' attribute on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
39735
diff
changeset
|
161 return getattr(self._fp, name) |
68ea1f8dcb84
py3: proxy posixfile objects to re-add a useful 'name' attribute on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
39735
diff
changeset
|
162 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
163 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
164 def posixfile(name, mode=b'r', buffering=-1): |
24069
c6666395fdd2
windows: adjust doc string and comments of posixfile()
Adrian Buehlmann <adrian@cadifra.com>
parents:
24051
diff
changeset
|
165 '''Open a file with even more POSIX-like semantics''' |
8421
b6d0fa8c7685
posixfile: remove posixfile_nt and fix import bug in windows.py
Sune Foldager <cryo@cyanite.org>
parents:
8364
diff
changeset
|
166 try: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
167 fp = osutil.posixfile(name, mode, buffering) # may raise WindowsError |
24051
7956d17431bc
windows: seek to the end of posixfile when opening in append mode
Matt Harbison <matt_harbison@yahoo.com>
parents:
23682
diff
changeset
|
168 |
39825
68ea1f8dcb84
py3: proxy posixfile objects to re-add a useful 'name' attribute on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
39735
diff
changeset
|
169 # PyFile_FromFd() ignores the name, and seems to report fp.name as the |
68ea1f8dcb84
py3: proxy posixfile objects to re-add a useful 'name' attribute on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
39735
diff
changeset
|
170 # underlying file descriptor. |
48989
a98e32e5fca1
windows: remove conditional for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48966
diff
changeset
|
171 fp = fdproxy(name, fp) |
39825
68ea1f8dcb84
py3: proxy posixfile objects to re-add a useful 'name' attribute on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
39735
diff
changeset
|
172 |
24051
7956d17431bc
windows: seek to the end of posixfile when opening in append mode
Matt Harbison <matt_harbison@yahoo.com>
parents:
23682
diff
changeset
|
173 # The position when opening in append mode is implementation defined, so |
7956d17431bc
windows: seek to the end of posixfile when opening in append mode
Matt Harbison <matt_harbison@yahoo.com>
parents:
23682
diff
changeset
|
174 # make it consistent with other platforms, which position at EOF. |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
175 if b'a' in mode: |
25462
021e68d37c5b
windows: use os.SEEK_END
Adrian Buehlmann <adrian@cadifra.com>
parents:
25420
diff
changeset
|
176 fp.seek(0, os.SEEK_END) |
24051
7956d17431bc
windows: seek to the end of posixfile when opening in append mode
Matt Harbison <matt_harbison@yahoo.com>
parents:
23682
diff
changeset
|
177 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
178 if b'+' in mode: |
26375
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
179 return mixedfilemodewrapper(fp) |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
180 |
24051
7956d17431bc
windows: seek to the end of posixfile when opening in append mode
Matt Harbison <matt_harbison@yahoo.com>
parents:
23682
diff
changeset
|
181 return fp |
48032
94d4a3f78e99
typing: suppress an name-error warning in `mercurial/windows.py`
Matt Harbison <matt_harbison@yahoo.com>
parents:
47858
diff
changeset
|
182 except WindowsError as err: # pytype: disable=name-error |
24069
c6666395fdd2
windows: adjust doc string and comments of posixfile()
Adrian Buehlmann <adrian@cadifra.com>
parents:
24051
diff
changeset
|
183 # convert to a friendlier exception |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
184 raise IOError( |
43554
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43089
diff
changeset
|
185 err.errno, '%s: %s' % (encoding.strfromlocal(name), err.strerror) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
186 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
187 |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
188 |
32248
d74b0cff94a9
osutil: proxy through util (and platform) modules (API)
Yuya Nishihara <yuya@tcha.org>
parents:
31891
diff
changeset
|
189 # may be wrapped by win32mbcs extension |
d74b0cff94a9
osutil: proxy through util (and platform) modules (API)
Yuya Nishihara <yuya@tcha.org>
parents:
31891
diff
changeset
|
190 listdir = osutil.listdir |
d74b0cff94a9
osutil: proxy through util (and platform) modules (API)
Yuya Nishihara <yuya@tcha.org>
parents:
31891
diff
changeset
|
191 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
192 |
49907
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
193 def get_password() -> bytes: |
47038
5b3513177f2b
util: avoid echoing the password to the console on Windows py3 (issue6446)
Matt Harbison <matt_harbison@yahoo.com>
parents:
46819
diff
changeset
|
194 """Prompt for password with echo off, using Windows getch(). |
5b3513177f2b
util: avoid echoing the password to the console on Windows py3 (issue6446)
Matt Harbison <matt_harbison@yahoo.com>
parents:
46819
diff
changeset
|
195 |
5b3513177f2b
util: avoid echoing the password to the console on Windows py3 (issue6446)
Matt Harbison <matt_harbison@yahoo.com>
parents:
46819
diff
changeset
|
196 This shouldn't be called directly- use ``ui.getpass()`` instead, which |
5b3513177f2b
util: avoid echoing the password to the console on Windows py3 (issue6446)
Matt Harbison <matt_harbison@yahoo.com>
parents:
46819
diff
changeset
|
197 checks if the session is interactive first. |
5b3513177f2b
util: avoid echoing the password to the console on Windows py3 (issue6446)
Matt Harbison <matt_harbison@yahoo.com>
parents:
46819
diff
changeset
|
198 """ |
47788
064cd182555f
windows: avoid a bytes vs unicode crash reading passwords on py2
Matt Harbison <matt_harbison@yahoo.com>
parents:
47622
diff
changeset
|
199 pw = u"" |
47038
5b3513177f2b
util: avoid echoing the password to the console on Windows py3 (issue6446)
Matt Harbison <matt_harbison@yahoo.com>
parents:
46819
diff
changeset
|
200 while True: |
47430
f77404040776
typing: disable warnings for a couple of Windows specific attributes
Matt Harbison <matt_harbison@yahoo.com>
parents:
47038
diff
changeset
|
201 c = msvcrt.getwch() # pytype: disable=module-attr |
47788
064cd182555f
windows: avoid a bytes vs unicode crash reading passwords on py2
Matt Harbison <matt_harbison@yahoo.com>
parents:
47622
diff
changeset
|
202 if c == u'\r' or c == u'\n': |
47038
5b3513177f2b
util: avoid echoing the password to the console on Windows py3 (issue6446)
Matt Harbison <matt_harbison@yahoo.com>
parents:
46819
diff
changeset
|
203 break |
47788
064cd182555f
windows: avoid a bytes vs unicode crash reading passwords on py2
Matt Harbison <matt_harbison@yahoo.com>
parents:
47622
diff
changeset
|
204 if c == u'\003': |
47038
5b3513177f2b
util: avoid echoing the password to the console on Windows py3 (issue6446)
Matt Harbison <matt_harbison@yahoo.com>
parents:
46819
diff
changeset
|
205 raise KeyboardInterrupt |
47788
064cd182555f
windows: avoid a bytes vs unicode crash reading passwords on py2
Matt Harbison <matt_harbison@yahoo.com>
parents:
47622
diff
changeset
|
206 if c == u'\b': |
47038
5b3513177f2b
util: avoid echoing the password to the console on Windows py3 (issue6446)
Matt Harbison <matt_harbison@yahoo.com>
parents:
46819
diff
changeset
|
207 pw = pw[:-1] |
5b3513177f2b
util: avoid echoing the password to the console on Windows py3 (issue6446)
Matt Harbison <matt_harbison@yahoo.com>
parents:
46819
diff
changeset
|
208 else: |
5b3513177f2b
util: avoid echoing the password to the console on Windows py3 (issue6446)
Matt Harbison <matt_harbison@yahoo.com>
parents:
46819
diff
changeset
|
209 pw = pw + c |
47788
064cd182555f
windows: avoid a bytes vs unicode crash reading passwords on py2
Matt Harbison <matt_harbison@yahoo.com>
parents:
47622
diff
changeset
|
210 msvcrt.putwch(u'\r') # pytype: disable=module-attr |
064cd182555f
windows: avoid a bytes vs unicode crash reading passwords on py2
Matt Harbison <matt_harbison@yahoo.com>
parents:
47622
diff
changeset
|
211 msvcrt.putwch(u'\n') # pytype: disable=module-attr |
064cd182555f
windows: avoid a bytes vs unicode crash reading passwords on py2
Matt Harbison <matt_harbison@yahoo.com>
parents:
47622
diff
changeset
|
212 return encoding.unitolocal(pw) |
47038
5b3513177f2b
util: avoid echoing the password to the console on Windows py3 (issue6446)
Matt Harbison <matt_harbison@yahoo.com>
parents:
46819
diff
changeset
|
213 |
5b3513177f2b
util: avoid echoing the password to the console on Windows py3 (issue6446)
Matt Harbison <matt_harbison@yahoo.com>
parents:
46819
diff
changeset
|
214 |
49888
8147abc05794
pytype: stop excluding mercurial/ui.py
Matt Harbison <matt_harbison@yahoo.com>
parents:
49426
diff
changeset
|
215 class winstdout(typelib.BinaryIO_Proxy): |
45957
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
45158
diff
changeset
|
216 """Some files on Windows misbehave. |
45145
aea246bc04bd
windows: augment docstring of `winstdout` class
Manuel Jacob <me@manueljacob.de>
parents:
44926
diff
changeset
|
217 |
aea246bc04bd
windows: augment docstring of `winstdout` class
Manuel Jacob <me@manueljacob.de>
parents:
44926
diff
changeset
|
218 When writing to a broken pipe, EINVAL instead of EPIPE may be raised. |
aea246bc04bd
windows: augment docstring of `winstdout` class
Manuel Jacob <me@manueljacob.de>
parents:
44926
diff
changeset
|
219 |
aea246bc04bd
windows: augment docstring of `winstdout` class
Manuel Jacob <me@manueljacob.de>
parents:
44926
diff
changeset
|
220 When writing too many bytes to a console at the same, a "Not enough space" |
aea246bc04bd
windows: augment docstring of `winstdout` class
Manuel Jacob <me@manueljacob.de>
parents:
44926
diff
changeset
|
221 error may happen. Python 3 already works around that. |
45957
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
45158
diff
changeset
|
222 """ |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
223 |
49888
8147abc05794
pytype: stop excluding mercurial/ui.py
Matt Harbison <matt_harbison@yahoo.com>
parents:
49426
diff
changeset
|
224 def __init__(self, fp: BinaryIO): |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
225 self.fp = fp |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
226 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
227 def __getattr__(self, key): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
228 return getattr(self.fp, key) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
229 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
230 def close(self): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
231 try: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
232 self.fp.close() |
14004
97ed99d1f419
eliminate various naked except clauses
Idan Kamara <idankk86@gmail.com>
parents:
13986
diff
changeset
|
233 except IOError: |
97ed99d1f419
eliminate various naked except clauses
Idan Kamara <idankk86@gmail.com>
parents:
13986
diff
changeset
|
234 pass |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
235 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
236 def write(self, s): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
237 try: |
48990
563eb25e079b
windows: remove write throttling support
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48989
diff
changeset
|
238 return self.fp.write(s) |
25660
328739ea70c3
global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25658
diff
changeset
|
239 except IOError as inst: |
37882
3a0f322af192
windows: fix incorrect detection of broken pipe when writing to pager
Sune Foldager <cryo@cyanite.org>
parents:
37460
diff
changeset
|
240 if inst.errno != 0 and not win32.lasterrorwaspipeerror(inst): |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
241 raise |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
242 self.close() |
43554
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43089
diff
changeset
|
243 raise IOError(errno.EPIPE, 'Broken pipe') |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
244 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
245 def flush(self): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
246 try: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
247 return self.fp.flush() |
25660
328739ea70c3
global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25658
diff
changeset
|
248 except IOError as inst: |
37882
3a0f322af192
windows: fix incorrect detection of broken pipe when writing to pager
Sune Foldager <cryo@cyanite.org>
parents:
37460
diff
changeset
|
249 if not win32.lasterrorwaspipeerror(inst): |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
250 raise |
43554
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43089
diff
changeset
|
251 raise IOError(errno.EPIPE, 'Broken pipe') |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
252 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
253 |
49907
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
254 def openhardlinks() -> bool: |
43928
70abcb614a5c
windows: drop detection of Windows 95/98/ME
Matt Harbison <matt_harbison@yahoo.com>
parents:
43910
diff
changeset
|
255 return True |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
256 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
257 |
49907
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
258 def parsepatchoutput(output_line: bytes) -> bytes: |
8761
0289f384e1e5
Generally replace "file name" with "filename" in help and comments.
timeless <timeless@gmail.com>
parents:
8657
diff
changeset
|
259 """parses the output produced by patch and returns the filename""" |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
260 pf = output_line[14:] |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
261 if pf[0] == b'`': |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
262 pf = pf[1:-1] # Remove the quotes |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
263 return pf |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
264 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
265 |
49907
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
266 def sshargs( |
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
267 sshcmd: bytes, host: bytes, user: Optional[bytes], port: Optional[bytes] |
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
268 ) -> bytes: |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
269 '''Build argument list for ssh or Plink''' |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
270 pflag = b'plink' in sshcmd.lower() and b'-P' or b'-p' |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
271 args = user and (b"%s@%s" % (user, host)) or host |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
272 if args.startswith(b'-') or args.startswith(b'/'): |
33649
739cc0f9cbb4
ssh: ban any username@host or host that starts with - (SEC)
Augie Fackler <augie@google.com>
parents:
32706
diff
changeset
|
273 raise error.Abort( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
274 _(b'illegal ssh hostname or username starting with - or /: %s') |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
275 % args |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
276 ) |
33657
8cb9e921ef8c
ssh: quote parameters using shellquote (SEC)
Jun Wu <quark@fb.com>
parents:
33649
diff
changeset
|
277 args = shellquote(args) |
8cb9e921ef8c
ssh: quote parameters using shellquote (SEC)
Jun Wu <quark@fb.com>
parents:
33649
diff
changeset
|
278 if port: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
279 args = b'%s %s %s' % (pflag, shellquote(port), args) |
33657
8cb9e921ef8c
ssh: quote parameters using shellquote (SEC)
Jun Wu <quark@fb.com>
parents:
33649
diff
changeset
|
280 return args |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
281 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
282 |
49907
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
283 def setflags(f: bytes, l: bool, x: bool) -> None: |
15011
5e44e4b3a0a3
util: move copymode into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
14985
diff
changeset
|
284 pass |
5e44e4b3a0a3
util: move copymode into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
14985
diff
changeset
|
285 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
286 |
49907
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
287 def copymode( |
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
288 src: bytes, |
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
289 dst: bytes, |
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
290 mode: Optional[bytes] = None, |
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
291 enforcewritable: bool = False, |
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
292 ) -> None: |
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
293 pass |
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
294 |
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
295 |
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
296 def checkexec(path: bytes) -> bool: |
13879
5b0a3f6cbead
util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
13777
diff
changeset
|
297 return False |
5b0a3f6cbead
util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
13777
diff
changeset
|
298 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
299 |
49907
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
300 def checklink(path: bytes) -> bool: |
13890
31eb145b50b6
util: move checklink() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
13879
diff
changeset
|
301 return False |
31eb145b50b6
util: move checklink() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
13879
diff
changeset
|
302 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
303 |
49907
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
304 def setbinary(fd) -> None: |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
305 # When run without console, pipes may expose invalid |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
306 # fileno(), usually set to -1. |
14969
a3f97038c1c2
windows: use getattr instead of hasattr
Augie Fackler <durin42@gmail.com>
parents:
14927
diff
changeset
|
307 fno = getattr(fd, 'fileno', None) |
a3f97038c1c2
windows: use getattr instead of hasattr
Augie Fackler <durin42@gmail.com>
parents:
14927
diff
changeset
|
308 if fno is not None and fno() >= 0: |
43788
fe73ec69350e
windows: suppress pytype warnings for Windows imports and functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
43554
diff
changeset
|
309 msvcrt.setmode(fno(), os.O_BINARY) # pytype: disable=module-attr |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
310 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
311 |
49907
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
312 def pconvert(path: bytes) -> bytes: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
313 return path.replace(pycompat.ossep, b'/') |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
314 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
315 |
49907
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
316 def localpath(path: bytes) -> bytes: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
317 return path.replace(b'/', b'\\') |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
318 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
319 |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
320 def normpath(path): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
321 return pconvert(os.path.normpath(path)) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
322 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
323 |
49907
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
324 def normcase(path: bytes) -> bytes: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
325 return encoding.upper(path) # NTFS compares via upper() |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
326 |
15488
6eff984d8e76
dirstate: fix case-folding identity for traditional Unix
Matt Mackall <mpm@selenic.com>
parents:
15016
diff
changeset
|
327 |
47622
bb917eea1605
windows: introduce a `util.abspath` to replace os.path.abspath
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47430
diff
changeset
|
328 DRIVE_RE_B = re.compile(b'^[a-z]:') |
bb917eea1605
windows: introduce a `util.abspath` to replace os.path.abspath
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47430
diff
changeset
|
329 DRIVE_RE_S = re.compile('^[a-z]:') |
bb917eea1605
windows: introduce a `util.abspath` to replace os.path.abspath
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47430
diff
changeset
|
330 |
bb917eea1605
windows: introduce a `util.abspath` to replace os.path.abspath
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47430
diff
changeset
|
331 |
bb917eea1605
windows: introduce a `util.abspath` to replace os.path.abspath
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47430
diff
changeset
|
332 def abspath(path): |
bb917eea1605
windows: introduce a `util.abspath` to replace os.path.abspath
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47430
diff
changeset
|
333 abs_path = os.path.abspath(path) # re-exports |
bb917eea1605
windows: introduce a `util.abspath` to replace os.path.abspath
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47430
diff
changeset
|
334 # Python on Windows is inconsistent regarding the capitalization of drive |
bb917eea1605
windows: introduce a `util.abspath` to replace os.path.abspath
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47430
diff
changeset
|
335 # letter and this cause issue with various path comparison along the way. |
bb917eea1605
windows: introduce a `util.abspath` to replace os.path.abspath
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47430
diff
changeset
|
336 # So we normalize the drive later to upper case here. |
bb917eea1605
windows: introduce a `util.abspath` to replace os.path.abspath
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47430
diff
changeset
|
337 # |
bb917eea1605
windows: introduce a `util.abspath` to replace os.path.abspath
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47430
diff
changeset
|
338 # See https://bugs.python.org/issue40368 for and example of this hell. |
bb917eea1605
windows: introduce a `util.abspath` to replace os.path.abspath
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47430
diff
changeset
|
339 if isinstance(abs_path, bytes): |
bb917eea1605
windows: introduce a `util.abspath` to replace os.path.abspath
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47430
diff
changeset
|
340 if DRIVE_RE_B.match(abs_path): |
bb917eea1605
windows: introduce a `util.abspath` to replace os.path.abspath
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47430
diff
changeset
|
341 abs_path = abs_path[0:1].upper() + abs_path[1:] |
bb917eea1605
windows: introduce a `util.abspath` to replace os.path.abspath
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47430
diff
changeset
|
342 elif DRIVE_RE_S.match(abs_path): |
bb917eea1605
windows: introduce a `util.abspath` to replace os.path.abspath
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47430
diff
changeset
|
343 abs_path = abs_path[0:1].upper() + abs_path[1:] |
bb917eea1605
windows: introduce a `util.abspath` to replace os.path.abspath
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47430
diff
changeset
|
344 return abs_path |
bb917eea1605
windows: introduce a `util.abspath` to replace os.path.abspath
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47430
diff
changeset
|
345 |
bb917eea1605
windows: introduce a `util.abspath` to replace os.path.abspath
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47430
diff
changeset
|
346 |
24598
22f49c7dd11b
windows: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents:
24148
diff
changeset
|
347 # see posix.py for definitions |
22f49c7dd11b
windows: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents:
24148
diff
changeset
|
348 normcasespec = encoding.normcasespecs.upper |
22f49c7dd11b
windows: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents:
24148
diff
changeset
|
349 normcasefallback = encoding.upperfallback |
22f49c7dd11b
windows: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents:
24148
diff
changeset
|
350 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
351 |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
352 def samestat(s1, s2): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
353 return False |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
354 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
355 |
38487
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
356 def shelltocmdexe(path, env): |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
357 r"""Convert shell variables in the form $var and ${var} inside ``path`` |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
358 to %var% form. Existing Windows style variables are left unchanged. |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
359 |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
360 The variables are limited to the given environment. Unknown variables are |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
361 left unchanged. |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
362 |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
363 >>> e = {b'var1': b'v1', b'var2': b'v2', b'var3': b'v3'} |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
364 >>> # Only valid values are expanded |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
365 >>> shelltocmdexe(b'cmd $var1 ${var2} %var3% $missing ${missing} %missing%', |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
366 ... e) |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
367 'cmd %var1% %var2% %var3% $missing ${missing} %missing%' |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
368 >>> # Single quote prevents expansion, as does \$ escaping |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
369 >>> shelltocmdexe(b"cmd '$var1 ${var2} %var3%' \$var1 \${var2} \\", e) |
38724
02b5b5c1bba8
windows: replace single quote with double quote when translating to cmd.exe
Matt Harbison <matt_harbison@yahoo.com>
parents:
38627
diff
changeset
|
370 'cmd "$var1 ${var2} %var3%" $var1 ${var2} \\' |
38627
93ed193bc03e
windows: don't consider '$$' to be an escaped '$' when translating to cmd.exe
Matt Harbison <matt_harbison@yahoo.com>
parents:
38602
diff
changeset
|
371 >>> # $$ is not special. %% is not special either, but can be the end and |
93ed193bc03e
windows: don't consider '$$' to be an escaped '$' when translating to cmd.exe
Matt Harbison <matt_harbison@yahoo.com>
parents:
38602
diff
changeset
|
372 >>> # start of consecutive variables |
38487
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
373 >>> shelltocmdexe(b"cmd $$ %% %var1%%var2%", e) |
38627
93ed193bc03e
windows: don't consider '$$' to be an escaped '$' when translating to cmd.exe
Matt Harbison <matt_harbison@yahoo.com>
parents:
38602
diff
changeset
|
374 'cmd $$ %% %var1%%var2%' |
38487
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
375 >>> # No double substitution |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
376 >>> shelltocmdexe(b"$var1 %var1%", {b'var1': b'%var2%', b'var2': b'boom'}) |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
377 '%var1% %var1%' |
38725
c382c19ce9bd
windows: expand '~/' and '~\' to %USERPROFILE% when translating to cmd.exe
Matt Harbison <matt_harbison@yahoo.com>
parents:
38724
diff
changeset
|
378 >>> # Tilde expansion |
c382c19ce9bd
windows: expand '~/' and '~\' to %USERPROFILE% when translating to cmd.exe
Matt Harbison <matt_harbison@yahoo.com>
parents:
38724
diff
changeset
|
379 >>> shelltocmdexe(b"~/dir ~\dir2 ~tmpfile \~/", {}) |
c382c19ce9bd
windows: expand '~/' and '~\' to %USERPROFILE% when translating to cmd.exe
Matt Harbison <matt_harbison@yahoo.com>
parents:
38724
diff
changeset
|
380 '%USERPROFILE%/dir %USERPROFILE%\\dir2 ~tmpfile ~/' |
38487
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
381 """ |
38725
c382c19ce9bd
windows: expand '~/' and '~\' to %USERPROFILE% when translating to cmd.exe
Matt Harbison <matt_harbison@yahoo.com>
parents:
38724
diff
changeset
|
382 if not any(c in path for c in b"$'~"): |
38487
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
383 return path |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
384 |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
385 varchars = pycompat.sysbytes(string.ascii_letters + string.digits) + b'_-' |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
386 |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
387 res = b'' |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
388 index = 0 |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
389 pathlen = len(path) |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
390 while index < pathlen: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
391 c = path[index : index + 1] |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
392 if c == b'\'': # no expansion within single quotes |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
393 path = path[index + 1 :] |
38487
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
394 pathlen = len(path) |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
395 try: |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
396 index = path.index(b'\'') |
38724
02b5b5c1bba8
windows: replace single quote with double quote when translating to cmd.exe
Matt Harbison <matt_harbison@yahoo.com>
parents:
38627
diff
changeset
|
397 res += b'"' + path[:index] + b'"' |
38487
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
398 except ValueError: |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
399 res += c + path |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
400 index = pathlen - 1 |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
401 elif c == b'%': # variable |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
402 path = path[index + 1 :] |
38487
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
403 pathlen = len(path) |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
404 try: |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
405 index = path.index(b'%') |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
406 except ValueError: |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
407 res += b'%' + path |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
408 index = pathlen - 1 |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
409 else: |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
410 var = path[:index] |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
411 res += b'%' + var + b'%' |
38627
93ed193bc03e
windows: don't consider '$$' to be an escaped '$' when translating to cmd.exe
Matt Harbison <matt_harbison@yahoo.com>
parents:
38602
diff
changeset
|
412 elif c == b'$': # variable |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
413 if path[index + 1 : index + 2] == b'{': |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
414 path = path[index + 2 :] |
38487
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
415 pathlen = len(path) |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
416 try: |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
417 index = path.index(b'}') |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
418 var = path[:index] |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
419 |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
420 # See below for why empty variables are handled specially |
39915
6e2c8f7f894e
py3: byteify windows.shelltocmdexe()
Matt Harbison <matt_harbison@yahoo.com>
parents:
39909
diff
changeset
|
421 if env.get(var, b'') != b'': |
38487
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
422 res += b'%' + var + b'%' |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
423 else: |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
424 res += b'${' + var + b'}' |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
425 except ValueError: |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
426 res += b'${' + path |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
427 index = pathlen - 1 |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
428 else: |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
429 var = b'' |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
430 index += 1 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
431 c = path[index : index + 1] |
38487
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
432 while c != b'' and c in varchars: |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
433 var += c |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
434 index += 1 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
435 c = path[index : index + 1] |
38487
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
436 # Some variables (like HG_OLDNODE) may be defined, but have an |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
437 # empty value. Those need to be skipped because when spawning |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
438 # cmd.exe to run the hook, it doesn't replace %VAR% for an empty |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
439 # VAR, and that really confuses things like revset expressions. |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
440 # OTOH, if it's left in Unix format and the hook runs sh.exe, it |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
441 # will substitute to an empty string, and everything is happy. |
39915
6e2c8f7f894e
py3: byteify windows.shelltocmdexe()
Matt Harbison <matt_harbison@yahoo.com>
parents:
39909
diff
changeset
|
442 if env.get(var, b'') != b'': |
38487
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
443 res += b'%' + var + b'%' |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
444 else: |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
445 res += b'$' + var |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
446 |
39915
6e2c8f7f894e
py3: byteify windows.shelltocmdexe()
Matt Harbison <matt_harbison@yahoo.com>
parents:
39909
diff
changeset
|
447 if c != b'': |
38487
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
448 index -= 1 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
449 elif ( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
450 c == b'~' |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
451 and index + 1 < pathlen |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
452 and path[index + 1 : index + 2] in (b'\\', b'/') |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
453 ): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
454 res += b"%USERPROFILE%" |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
455 elif ( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
456 c == b'\\' |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
457 and index + 1 < pathlen |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
458 and path[index + 1 : index + 2] in (b'$', b'~') |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
459 ): |
38725
c382c19ce9bd
windows: expand '~/' and '~\' to %USERPROFILE% when translating to cmd.exe
Matt Harbison <matt_harbison@yahoo.com>
parents:
38724
diff
changeset
|
460 # Skip '\', but only if it is escaping $ or ~ |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
461 res += path[index + 1 : index + 2] |
38487
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
462 index += 1 |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
463 else: |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
464 res += c |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
465 |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
466 index += 1 |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
467 return res |
3efadf2317c7
windows: add a method to convert Unix style command lines to Windows style
Matt Harbison <matt_harbison@yahoo.com>
parents:
37460
diff
changeset
|
468 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
469 |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
470 # A sequence of backslashes is special iff it precedes a double quote: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
471 # - if there's an even number of backslashes, the double quote is not |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
472 # quoted (i.e. it ends the quoted region) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
473 # - if there's an odd number of backslashes, the double quote is quoted |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
474 # - in both cases, every pair of backslashes is unquoted into a single |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
475 # backslash |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
476 # (See http://msdn2.microsoft.com/en-us/library/a1y7w461.aspx ) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
477 # So, to quote a string, we must surround it in double quotes, double |
17505 | 478 # the number of backslashes that precede double quotes and add another |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
479 # backslash before every double quote (being careful with the double |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
480 # quote we've appended to the end) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
481 _quotere = None |
23682
1642eb429536
windows: quote the specified string only when it has to be quoted
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
22245
diff
changeset
|
482 _needsshellquote = None |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
483 |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
484 |
49907
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
485 def shellquote(s: bytes) -> bytes: |
24908
30b910fea244
windows: add doctest for shellquote()
Matt Harbison <matt_harbison@yahoo.com>
parents:
24885
diff
changeset
|
486 r""" |
34146
0fa781320203
doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents:
34040
diff
changeset
|
487 >>> shellquote(br'C:\Users\xyz') |
24908
30b910fea244
windows: add doctest for shellquote()
Matt Harbison <matt_harbison@yahoo.com>
parents:
24885
diff
changeset
|
488 '"C:\\Users\\xyz"' |
34146
0fa781320203
doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents:
34040
diff
changeset
|
489 >>> shellquote(br'C:\Users\xyz/mixed') |
24908
30b910fea244
windows: add doctest for shellquote()
Matt Harbison <matt_harbison@yahoo.com>
parents:
24885
diff
changeset
|
490 '"C:\\Users\\xyz/mixed"' |
30b910fea244
windows: add doctest for shellquote()
Matt Harbison <matt_harbison@yahoo.com>
parents:
24885
diff
changeset
|
491 >>> # Would be safe not to quote too, since it is all double backslashes |
34146
0fa781320203
doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents:
34040
diff
changeset
|
492 >>> shellquote(br'C:\\Users\\xyz') |
24908
30b910fea244
windows: add doctest for shellquote()
Matt Harbison <matt_harbison@yahoo.com>
parents:
24885
diff
changeset
|
493 '"C:\\\\Users\\\\xyz"' |
30b910fea244
windows: add doctest for shellquote()
Matt Harbison <matt_harbison@yahoo.com>
parents:
24885
diff
changeset
|
494 >>> # But this must be quoted |
34146
0fa781320203
doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents:
34040
diff
changeset
|
495 >>> shellquote(br'C:\\Users\\xyz/abc') |
24908
30b910fea244
windows: add doctest for shellquote()
Matt Harbison <matt_harbison@yahoo.com>
parents:
24885
diff
changeset
|
496 '"C:\\\\Users\\\\xyz/abc"' |
30b910fea244
windows: add doctest for shellquote()
Matt Harbison <matt_harbison@yahoo.com>
parents:
24885
diff
changeset
|
497 """ |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
498 global _quotere |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
499 if _quotere is None: |
39660
3b421154d2ca
py3: fix str vs bytes in enough places to run `hg version` on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
39659
diff
changeset
|
500 _quotere = re.compile(br'(\\*)("|\\$)') |
23682
1642eb429536
windows: quote the specified string only when it has to be quoted
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
22245
diff
changeset
|
501 global _needsshellquote |
1642eb429536
windows: quote the specified string only when it has to be quoted
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
22245
diff
changeset
|
502 if _needsshellquote is None: |
24885
eea3977e6fca
windows: make shellquote() quote any path containing '\' (issue4629)
Matt Harbison <matt_harbison@yahoo.com>
parents:
24692
diff
changeset
|
503 # ":" is also treated as "safe character", because it is used as a part |
eea3977e6fca
windows: make shellquote() quote any path containing '\' (issue4629)
Matt Harbison <matt_harbison@yahoo.com>
parents:
24692
diff
changeset
|
504 # of path name on Windows. "\" is also part of a path name, but isn't |
eea3977e6fca
windows: make shellquote() quote any path containing '\' (issue4629)
Matt Harbison <matt_harbison@yahoo.com>
parents:
24692
diff
changeset
|
505 # safe because shlex.split() (kind of) treats it as an escape char and |
eea3977e6fca
windows: make shellquote() quote any path containing '\' (issue4629)
Matt Harbison <matt_harbison@yahoo.com>
parents:
24692
diff
changeset
|
506 # drops it. It will leave the next character, even if it is another |
eea3977e6fca
windows: make shellquote() quote any path containing '\' (issue4629)
Matt Harbison <matt_harbison@yahoo.com>
parents:
24692
diff
changeset
|
507 # "\". |
39660
3b421154d2ca
py3: fix str vs bytes in enough places to run `hg version` on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
39659
diff
changeset
|
508 _needsshellquote = re.compile(br'[^a-zA-Z0-9._:/-]').search |
24108
d65ecb814fc0
shellquote: fix missing quotes for empty string
Yuya Nishihara <yuya@tcha.org>
parents:
23682
diff
changeset
|
509 if s and not _needsshellquote(s) and not _quotere.search(s): |
23682
1642eb429536
windows: quote the specified string only when it has to be quoted
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
22245
diff
changeset
|
510 # "s" shouldn't have to be quoted |
1642eb429536
windows: quote the specified string only when it has to be quoted
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
22245
diff
changeset
|
511 return s |
39660
3b421154d2ca
py3: fix str vs bytes in enough places to run `hg version` on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
39659
diff
changeset
|
512 return b'"%s"' % _quotere.sub(br'\1\1\\\2', s) |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
513 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
514 |
36445
0cb09c322647
util: factor out shellsplit() function
Yuya Nishihara <yuya@tcha.org>
parents:
35517
diff
changeset
|
515 def _unquote(s): |
0cb09c322647
util: factor out shellsplit() function
Yuya Nishihara <yuya@tcha.org>
parents:
35517
diff
changeset
|
516 if s.startswith(b'"') and s.endswith(b'"'): |
0cb09c322647
util: factor out shellsplit() function
Yuya Nishihara <yuya@tcha.org>
parents:
35517
diff
changeset
|
517 return s[1:-1] |
0cb09c322647
util: factor out shellsplit() function
Yuya Nishihara <yuya@tcha.org>
parents:
35517
diff
changeset
|
518 return s |
0cb09c322647
util: factor out shellsplit() function
Yuya Nishihara <yuya@tcha.org>
parents:
35517
diff
changeset
|
519 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
520 |
49907
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
521 def shellsplit(s: bytes) -> List[bytes]: |
36445
0cb09c322647
util: factor out shellsplit() function
Yuya Nishihara <yuya@tcha.org>
parents:
35517
diff
changeset
|
522 """Parse a command string in cmd.exe way (best-effort)""" |
0cb09c322647
util: factor out shellsplit() function
Yuya Nishihara <yuya@tcha.org>
parents:
35517
diff
changeset
|
523 return pycompat.maplist(_unquote, pycompat.shlexsplit(s, posix=False)) |
0cb09c322647
util: factor out shellsplit() function
Yuya Nishihara <yuya@tcha.org>
parents:
35517
diff
changeset
|
524 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
525 |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
526 # if you change this stub into a real check, please try to implement the |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
527 # username and groupname functions above, too. |
49907
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
528 def isowner(st: os.stat_result) -> bool: |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
529 return True |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
530 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
531 |
49907
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
532 def findexe(command: bytes) -> Optional[bytes]: |
45957
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
45158
diff
changeset
|
533 """Find executable for command searching like cmd.exe does. |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
534 If command is a basename then PATH is searched for command. |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
535 PATH isn't searched if command is an absolute or relative path. |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
536 An extension from PATHEXT is found and added if not present. |
45957
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
45158
diff
changeset
|
537 If command isn't found None is returned.""" |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
538 pathext = encoding.environ.get(b'PATHEXT', b'.COM;.EXE;.BAT;.CMD') |
30617
d623cc6b3742
py3: replace os.pathsep with pycompat.ospathsep
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30483
diff
changeset
|
539 pathexts = [ext for ext in pathext.lower().split(pycompat.ospathsep)] |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
540 if os.path.splitext(command)[1].lower() in pathexts: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
541 pathexts = [b''] |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
542 |
49907
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
543 def findexisting(pathcommand: bytes) -> Optional[bytes]: |
43807
be8552f25cab
cleanup: fix docstring formatting
Matt Harbison <matt_harbison@yahoo.com>
parents:
43788
diff
changeset
|
544 """Will append extension (if needed) and return existing file""" |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
545 for ext in pathexts: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
546 executable = pathcommand + ext |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
547 if os.path.exists(executable): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
548 return executable |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
549 return None |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
550 |
30620
bb77654dc7ae
py3: replace os.sep with pycompat.ossep (part 3 of 4)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30617
diff
changeset
|
551 if pycompat.ossep in command: |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
552 return findexisting(command) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
553 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
554 for path in encoding.environ.get(b'PATH', b'').split(pycompat.ospathsep): |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
555 executable = findexisting(os.path.join(path, command)) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
556 if executable is not None: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
557 return executable |
10156
c31ac3f7fd8f
windows: expand environment vars in find_exe
Steve Borho <steve@borho.org>
parents:
9594
diff
changeset
|
558 return findexisting(os.path.expanduser(os.path.expandvars(command))) |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
559 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
560 |
32331
bd872f64a8ba
cleanup: use set literals
Martin von Zweigbergk <martinvonz@google.com>
parents:
32248
diff
changeset
|
561 _wantedkinds = {stat.S_IFREG, stat.S_IFLNK} |
18017
74912fe3d718
dirstate: move file type filtering to its source
Bryan O'Sullivan <bryano@fb.com>
parents:
17560
diff
changeset
|
562 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
563 |
49907
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
564 def statfiles(files: Sequence[bytes]) -> Iterator[Optional[os.stat_result]]: |
45957
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
45158
diff
changeset
|
565 """Stat each file in files. Yield each stat, or None if a file |
18017
74912fe3d718
dirstate: move file type filtering to its source
Bryan O'Sullivan <bryano@fb.com>
parents:
17560
diff
changeset
|
566 does not exist or has a type we don't care about. |
74912fe3d718
dirstate: move file type filtering to its source
Bryan O'Sullivan <bryano@fb.com>
parents:
17560
diff
changeset
|
567 |
45957
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
45158
diff
changeset
|
568 Cluster and cache stat per directory to minimize number of OS stat calls.""" |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
569 dircache = {} # dirname -> filename -> status | None if file does not exist |
18017
74912fe3d718
dirstate: move file type filtering to its source
Bryan O'Sullivan <bryano@fb.com>
parents:
17560
diff
changeset
|
570 getkind = stat.S_IFMT |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
571 for nf in files: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
572 nf = normcase(nf) |
9099
3d456bf32f18
Use os.path.split() for MBCS with win32mbcs extension.
Shun-ichi GOTO <shunichi.goto@gmail.com>
parents:
8951
diff
changeset
|
573 dir, base = os.path.split(nf) |
3d456bf32f18
Use os.path.split() for MBCS with win32mbcs extension.
Shun-ichi GOTO <shunichi.goto@gmail.com>
parents:
8951
diff
changeset
|
574 if not dir: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
575 dir = b'.' |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
576 cache = dircache.get(dir, None) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
577 if cache is None: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
578 try: |
44470
9d2b2df2c2ba
cleanup: run pyupgrade on our source tree to clean up varying things
Augie Fackler <augie@google.com>
parents:
43928
diff
changeset
|
579 dmap = { |
9d2b2df2c2ba
cleanup: run pyupgrade on our source tree to clean up varying things
Augie Fackler <augie@google.com>
parents:
43928
diff
changeset
|
580 normcase(n): s |
9d2b2df2c2ba
cleanup: run pyupgrade on our source tree to clean up varying things
Augie Fackler <augie@google.com>
parents:
43928
diff
changeset
|
581 for n, k, s in listdir(dir, True) |
9d2b2df2c2ba
cleanup: run pyupgrade on our source tree to clean up varying things
Augie Fackler <augie@google.com>
parents:
43928
diff
changeset
|
582 if getkind(s.st_mode) in _wantedkinds |
9d2b2df2c2ba
cleanup: run pyupgrade on our source tree to clean up varying things
Augie Fackler <augie@google.com>
parents:
43928
diff
changeset
|
583 } |
49319
defc369d705e
py3: catch specific OSError subclasses instead of checking errno
Manuel Jacob <me@manueljacob.de>
parents:
49313
diff
changeset
|
584 except (FileNotFoundError, NotADirectoryError): |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
585 dmap = {} |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
586 cache = dircache.setdefault(dir, dmap) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
587 yield cache.get(base, None) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
588 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
589 |
49907
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
590 def username(uid: Optional[int] = None) -> Optional[bytes]: |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
591 """Return the name of the user with the given uid. |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
592 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
593 If uid is None, return the name of the current user.""" |
43910
1b26cb044097
windows: if username(uid=None) is loaded, just use getpass
Augie Fackler <augie@google.com>
parents:
43807
diff
changeset
|
594 if not uid: |
49426
805419729e11
windows: gracefully handle when the username cannot be determined
Matt Harbison <matt_harbison@yahoo.com>
parents:
49325
diff
changeset
|
595 try: |
805419729e11
windows: gracefully handle when the username cannot be determined
Matt Harbison <matt_harbison@yahoo.com>
parents:
49325
diff
changeset
|
596 return pycompat.fsencode(getpass.getuser()) |
805419729e11
windows: gracefully handle when the username cannot be determined
Matt Harbison <matt_harbison@yahoo.com>
parents:
49325
diff
changeset
|
597 except ModuleNotFoundError: |
805419729e11
windows: gracefully handle when the username cannot be determined
Matt Harbison <matt_harbison@yahoo.com>
parents:
49325
diff
changeset
|
598 # getpass.getuser() checks for a few environment variables first, |
805419729e11
windows: gracefully handle when the username cannot be determined
Matt Harbison <matt_harbison@yahoo.com>
parents:
49325
diff
changeset
|
599 # but if those aren't set, imports pwd and calls getpwuid(), none of |
805419729e11
windows: gracefully handle when the username cannot be determined
Matt Harbison <matt_harbison@yahoo.com>
parents:
49325
diff
changeset
|
600 # which exists on Windows. |
805419729e11
windows: gracefully handle when the username cannot be determined
Matt Harbison <matt_harbison@yahoo.com>
parents:
49325
diff
changeset
|
601 pass |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
602 return None |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
603 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
604 |
49907
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
605 def groupname(gid: Optional[int] = None) -> Optional[bytes]: |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
606 """Return the name of the group with the given gid. |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
607 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
608 If gid is None, return the name of the current group.""" |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
609 return None |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
610 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
611 |
39909
5fe0b880200e
py3: convert os.readlink() path to native strings on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
39888
diff
changeset
|
612 def readlink(pathname): |
47858
4162f6b40f2c
windows: degrade to py2 behavior when reading a non-symlink as a symlink
Matt Harbison <matt_harbison@yahoo.com>
parents:
47788
diff
changeset
|
613 path = pycompat.fsdecode(pathname) |
4162f6b40f2c
windows: degrade to py2 behavior when reading a non-symlink as a symlink
Matt Harbison <matt_harbison@yahoo.com>
parents:
47788
diff
changeset
|
614 try: |
4162f6b40f2c
windows: degrade to py2 behavior when reading a non-symlink as a symlink
Matt Harbison <matt_harbison@yahoo.com>
parents:
47788
diff
changeset
|
615 link = os.readlink(path) |
4162f6b40f2c
windows: degrade to py2 behavior when reading a non-symlink as a symlink
Matt Harbison <matt_harbison@yahoo.com>
parents:
47788
diff
changeset
|
616 except ValueError as e: |
4162f6b40f2c
windows: degrade to py2 behavior when reading a non-symlink as a symlink
Matt Harbison <matt_harbison@yahoo.com>
parents:
47788
diff
changeset
|
617 # On py2, os.readlink() raises an AttributeError since it is |
4162f6b40f2c
windows: degrade to py2 behavior when reading a non-symlink as a symlink
Matt Harbison <matt_harbison@yahoo.com>
parents:
47788
diff
changeset
|
618 # unsupported. On py3, reading a non-link raises a ValueError. Simply |
4162f6b40f2c
windows: degrade to py2 behavior when reading a non-symlink as a symlink
Matt Harbison <matt_harbison@yahoo.com>
parents:
47788
diff
changeset
|
619 # treat this as the error the locking code has been expecting up to now |
4162f6b40f2c
windows: degrade to py2 behavior when reading a non-symlink as a symlink
Matt Harbison <matt_harbison@yahoo.com>
parents:
47788
diff
changeset
|
620 # until an effort can be made to enable symlink support on Windows. |
4162f6b40f2c
windows: degrade to py2 behavior when reading a non-symlink as a symlink
Matt Harbison <matt_harbison@yahoo.com>
parents:
47788
diff
changeset
|
621 raise AttributeError(e) |
4162f6b40f2c
windows: degrade to py2 behavior when reading a non-symlink as a symlink
Matt Harbison <matt_harbison@yahoo.com>
parents:
47788
diff
changeset
|
622 return pycompat.fsencode(link) |
39909
5fe0b880200e
py3: convert os.readlink() path to native strings on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
39888
diff
changeset
|
623 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
624 |
24692
144883a8d0d4
util: add removedirs as platform depending function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
24653
diff
changeset
|
625 def removedirs(name): |
8364
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
626 """special version of os.removedirs that does not remove symlinked |
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
627 directories or junction points if they actually contain files""" |
32248
d74b0cff94a9
osutil: proxy through util (and platform) modules (API)
Yuya Nishihara <yuya@tcha.org>
parents:
31891
diff
changeset
|
628 if listdir(name): |
8364
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
629 return |
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
630 os.rmdir(name) |
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
631 head, tail = os.path.split(name) |
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
632 if not tail: |
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
633 head, tail = os.path.split(head) |
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
634 while head and tail: |
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
635 try: |
32248
d74b0cff94a9
osutil: proxy through util (and platform) modules (API)
Yuya Nishihara <yuya@tcha.org>
parents:
31891
diff
changeset
|
636 if listdir(head): |
8364
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
637 return |
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
638 os.rmdir(head) |
14004
97ed99d1f419
eliminate various naked except clauses
Idan Kamara <idankk86@gmail.com>
parents:
13986
diff
changeset
|
639 except (ValueError, OSError): |
8364
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
640 break |
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
641 head, tail = os.path.split(head) |
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
642 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
643 |
9549
8b8920209317
util: move rename into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
9449
diff
changeset
|
644 def rename(src, dst): |
8b8920209317
util: move rename into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
9449
diff
changeset
|
645 '''atomically rename file src to dst, replacing dst if it exists''' |
8b8920209317
util: move rename into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
9449
diff
changeset
|
646 try: |
8b8920209317
util: move rename into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
9449
diff
changeset
|
647 os.rename(src, dst) |
49313
53e9422a9b45
py3: catch FileExistsError instead of checking errno == EEXIST
Manuel Jacob <me@manueljacob.de>
parents:
49065
diff
changeset
|
648 except FileExistsError: |
13280
6052bbc7aabd
reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents:
13278
diff
changeset
|
649 unlink(dst) |
9549
8b8920209317
util: move rename into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
9449
diff
changeset
|
650 os.rename(src, dst) |
8b8920209317
util: move rename into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
9449
diff
changeset
|
651 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
652 |
10239
8e4be44a676f
Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents:
10237
diff
changeset
|
653 def gethgcmd(): |
39735
255d1885c7f8
py3: resolve Unicode issues around `hg serve` on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
39660
diff
changeset
|
654 return [encoding.strtolocal(arg) for arg in [sys.executable] + sys.argv[:1]] |
10239
8e4be44a676f
Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents:
10237
diff
changeset
|
655 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
656 |
49907
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
657 def groupmembers(name: bytes) -> List[bytes]: |
11138
99eee847beaa
acl: grp module is not available on windows
Patrick Mezard <pmezard@gmail.com>
parents:
11010
diff
changeset
|
658 # Don't support groups on Windows for now |
16687
e34106fa0dc3
cleanup: "raise SomeException()" -> "raise SomeException"
Brodie Rao <brodie@sf.io>
parents:
16076
diff
changeset
|
659 raise KeyError |
11138
99eee847beaa
acl: grp module is not available on windows
Patrick Mezard <pmezard@gmail.com>
parents:
11010
diff
changeset
|
660 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
661 |
49907
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
662 def isexec(f: bytes) -> bool: |
14926
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14892
diff
changeset
|
663 return False |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14892
diff
changeset
|
664 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
665 |
49037
642e31cb55f0
py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48990
diff
changeset
|
666 class cachestat: |
14927
2aa3e07b2f07
posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents:
14926
diff
changeset
|
667 def __init__(self, path): |
2aa3e07b2f07
posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents:
14926
diff
changeset
|
668 pass |
2aa3e07b2f07
posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents:
14926
diff
changeset
|
669 |
2aa3e07b2f07
posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents:
14926
diff
changeset
|
670 def cacheable(self): |
2aa3e07b2f07
posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents:
14926
diff
changeset
|
671 return False |
2aa3e07b2f07
posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents:
14926
diff
changeset
|
672 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
673 |
49907
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
674 def lookupreg( |
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
675 key: bytes, |
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
676 valname: Optional[bytes] = None, |
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
677 scope: Optional[Union[int, Iterable[int]]] = None, |
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
678 ) -> Optional[bytes]: |
45957
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
45158
diff
changeset
|
679 """Look up a key/value name in the Windows registry. |
16807
80142f385af9
win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
16687
diff
changeset
|
680 |
80142f385af9
win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
16687
diff
changeset
|
681 valname: value name. If unspecified, the default value for the key |
80142f385af9
win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
16687
diff
changeset
|
682 is used. |
80142f385af9
win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
16687
diff
changeset
|
683 scope: optionally specify scope for registry lookup, this can be |
80142f385af9
win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
16687
diff
changeset
|
684 a sequence of scopes to look up in order. Default (CURRENT_USER, |
80142f385af9
win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
16687
diff
changeset
|
685 LOCAL_MACHINE). |
45957
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
45158
diff
changeset
|
686 """ |
16807
80142f385af9
win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
16687
diff
changeset
|
687 if scope is None: |
49065
d500df2e8034
pytype: disable a few errors about Windows specific module attributes
Matt Harbison <matt_harbison@yahoo.com>
parents:
49037
diff
changeset
|
688 # pytype: disable=module-attr |
29772
3df9f780c90e
py3: conditionalize _winreg import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29530
diff
changeset
|
689 scope = (winreg.HKEY_CURRENT_USER, winreg.HKEY_LOCAL_MACHINE) |
49065
d500df2e8034
pytype: disable a few errors about Windows specific module attributes
Matt Harbison <matt_harbison@yahoo.com>
parents:
49037
diff
changeset
|
690 # pytype: enable=module-attr |
16807
80142f385af9
win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
16687
diff
changeset
|
691 elif not isinstance(scope, (list, tuple)): |
80142f385af9
win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
16687
diff
changeset
|
692 scope = (scope,) |
80142f385af9
win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
16687
diff
changeset
|
693 for s in scope: |
80142f385af9
win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
16687
diff
changeset
|
694 try: |
49065
d500df2e8034
pytype: disable a few errors about Windows specific module attributes
Matt Harbison <matt_harbison@yahoo.com>
parents:
49037
diff
changeset
|
695 # pytype: disable=module-attr |
39659
47ac5d93d708
windows: open registry keys using unicode names
Matt Harbison <matt_harbison@yahoo.com>
parents:
38725
diff
changeset
|
696 with winreg.OpenKey(s, encoding.strfromlocal(key)) as hkey: |
49065
d500df2e8034
pytype: disable a few errors about Windows specific module attributes
Matt Harbison <matt_harbison@yahoo.com>
parents:
49037
diff
changeset
|
697 # pytype: enable=module-attr |
49323
709e5f7eec1f
windows: prevent bytes from being passed to registry APIs
Matt Harbison <matt_harbison@yahoo.com>
parents:
49319
diff
changeset
|
698 name = None |
709e5f7eec1f
windows: prevent bytes from being passed to registry APIs
Matt Harbison <matt_harbison@yahoo.com>
parents:
49319
diff
changeset
|
699 if valname is not None: |
709e5f7eec1f
windows: prevent bytes from being passed to registry APIs
Matt Harbison <matt_harbison@yahoo.com>
parents:
49319
diff
changeset
|
700 name = encoding.strfromlocal(valname) |
49065
d500df2e8034
pytype: disable a few errors about Windows specific module attributes
Matt Harbison <matt_harbison@yahoo.com>
parents:
49037
diff
changeset
|
701 # pytype: disable=module-attr |
40266
ab04ce6f0674
py3: use str to query registry values on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
39915
diff
changeset
|
702 val = winreg.QueryValueEx(hkey, name)[0] |
49065
d500df2e8034
pytype: disable a few errors about Windows specific module attributes
Matt Harbison <matt_harbison@yahoo.com>
parents:
49037
diff
changeset
|
703 # pytype: enable=module-attr |
d500df2e8034
pytype: disable a few errors about Windows specific module attributes
Matt Harbison <matt_harbison@yahoo.com>
parents:
49037
diff
changeset
|
704 |
39659
47ac5d93d708
windows: open registry keys using unicode names
Matt Harbison <matt_harbison@yahoo.com>
parents:
38725
diff
changeset
|
705 # never let a Unicode string escape into the wild |
47ac5d93d708
windows: open registry keys using unicode names
Matt Harbison <matt_harbison@yahoo.com>
parents:
38725
diff
changeset
|
706 return encoding.unitolocal(val) |
16807
80142f385af9
win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
16687
diff
changeset
|
707 except EnvironmentError: |
80142f385af9
win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
16687
diff
changeset
|
708 pass |
80142f385af9
win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
16687
diff
changeset
|
709 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
710 |
8614
573734e7e6d0
cmdutils: Take over glob expansion duties from util
Matt Mackall <mpm@selenic.com>
parents:
8559
diff
changeset
|
711 expandglobs = True |
18868
cafa447a7d3b
util: add functions to check symlink/exec bits
Bryan O'Sullivan <bryano@fb.com>
parents:
18143
diff
changeset
|
712 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
713 |
49907
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
714 def statislink(st: Optional[os.stat_result]) -> bool: |
18868
cafa447a7d3b
util: add functions to check symlink/exec bits
Bryan O'Sullivan <bryano@fb.com>
parents:
18143
diff
changeset
|
715 '''check whether a stat result is a symlink''' |
cafa447a7d3b
util: add functions to check symlink/exec bits
Bryan O'Sullivan <bryano@fb.com>
parents:
18143
diff
changeset
|
716 return False |
cafa447a7d3b
util: add functions to check symlink/exec bits
Bryan O'Sullivan <bryano@fb.com>
parents:
18143
diff
changeset
|
717 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
718 |
49907
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
719 def statisexec(st: Optional[os.stat_result]) -> bool: |
18868
cafa447a7d3b
util: add functions to check symlink/exec bits
Bryan O'Sullivan <bryano@fb.com>
parents:
18143
diff
changeset
|
720 '''check whether a stat result is an executable file''' |
cafa447a7d3b
util: add functions to check symlink/exec bits
Bryan O'Sullivan <bryano@fb.com>
parents:
18143
diff
changeset
|
721 return False |
22245
234e4c24b980
platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
20202
diff
changeset
|
722 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
723 |
25420
c2ec81891502
util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25204
diff
changeset
|
724 def poll(fds): |
c2ec81891502
util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25204
diff
changeset
|
725 # see posix.py for description |
c2ec81891502
util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25204
diff
changeset
|
726 raise NotImplementedError() |
c2ec81891502
util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25204
diff
changeset
|
727 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
728 |
49907
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
729 def readpipe(pipe) -> bytes: |
22245
234e4c24b980
platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
20202
diff
changeset
|
730 """Read all available data from a pipe.""" |
234e4c24b980
platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
20202
diff
changeset
|
731 chunks = [] |
234e4c24b980
platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
20202
diff
changeset
|
732 while True: |
24653
83f6c4733ecc
windows: allow readpipe() to actually read data out of the pipe
Matt Harbison <matt_harbison@yahoo.com>
parents:
24598
diff
changeset
|
733 size = win32.peekpipe(pipe) |
22245
234e4c24b980
platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
20202
diff
changeset
|
734 if not size: |
234e4c24b980
platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
20202
diff
changeset
|
735 break |
234e4c24b980
platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
20202
diff
changeset
|
736 |
234e4c24b980
platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
20202
diff
changeset
|
737 s = pipe.read(size) |
234e4c24b980
platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
20202
diff
changeset
|
738 if not s: |
234e4c24b980
platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
20202
diff
changeset
|
739 break |
234e4c24b980
platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
20202
diff
changeset
|
740 chunks.append(s) |
234e4c24b980
platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
20202
diff
changeset
|
741 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
742 return b''.join(chunks) |
29530
3239e2fdd2e2
chgserver: extract utility to bind unix domain socket to long path
Yuya Nishihara <yuya@tcha.org>
parents:
27436
diff
changeset
|
743 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41289
diff
changeset
|
744 |
49907
58dff81ffba1
typing: add type hints to the common posix/windows platform functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
49895
diff
changeset
|
745 def bindunixsocket(sock, path: bytes) -> NoReturn: |
43554
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43089
diff
changeset
|
746 raise NotImplementedError('unsupported platform') |