Mercurial > public > mercurial-scm > hg-stable
annotate mercurial/windows.py @ 32248:d74b0cff94a9
osutil: proxy through util (and platform) modules (API)
See the previous commit for why. Marked as API change since osutil.listdir()
seems widely used in third-party extensions.
The win32mbcs extension is updated to wrap both util. and windows. aliases.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Wed, 26 Apr 2017 22:26:28 +0900 |
parents | 87f293edabb6 |
children | bd872f64a8ba |
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 # |
8b2cd04a6e97
put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
3 # Copyright 2005-2009 Matt Mackall <mpm@selenic.com> and others |
8b2cd04a6e97
put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
4 # |
8b2cd04a6e97
put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
5 # This software may be used and distributed according to the terms of the |
10263 | 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 |
27360
6daa795ed32f
windows: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26375
diff
changeset
|
8 from __future__ import absolute_import |
8421
b6d0fa8c7685
posixfile: remove posixfile_nt and fix import bug in windows.py
Sune Foldager <cryo@cyanite.org>
parents:
8364
diff
changeset
|
9 |
27360
6daa795ed32f
windows: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26375
diff
changeset
|
10 import errno |
6daa795ed32f
windows: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26375
diff
changeset
|
11 import msvcrt |
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 |
6daa795ed32f
windows: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26375
diff
changeset
|
15 import sys |
6daa795ed32f
windows: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26375
diff
changeset
|
16 |
6daa795ed32f
windows: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26375
diff
changeset
|
17 from .i18n import _ |
6daa795ed32f
windows: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26375
diff
changeset
|
18 from . import ( |
6daa795ed32f
windows: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26375
diff
changeset
|
19 encoding, |
6daa795ed32f
windows: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26375
diff
changeset
|
20 osutil, |
30617
d623cc6b3742
py3: replace os.pathsep with pycompat.ospathsep
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30483
diff
changeset
|
21 pycompat, |
27436
912255f8f087
windows: correct the import of win32
Matt Harbison <matt_harbison@yahoo.com>
parents:
27360
diff
changeset
|
22 win32, |
27360
6daa795ed32f
windows: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26375
diff
changeset
|
23 ) |
6daa795ed32f
windows: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
26375
diff
changeset
|
24 |
29772
3df9f780c90e
py3: conditionalize _winreg import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29530
diff
changeset
|
25 try: |
3df9f780c90e
py3: conditionalize _winreg import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29530
diff
changeset
|
26 import _winreg as winreg |
3df9f780c90e
py3: conditionalize _winreg import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29530
diff
changeset
|
27 winreg.CloseKey |
3df9f780c90e
py3: conditionalize _winreg import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29530
diff
changeset
|
28 except ImportError: |
3df9f780c90e
py3: conditionalize _winreg import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29530
diff
changeset
|
29 import winreg |
3df9f780c90e
py3: conditionalize _winreg import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29530
diff
changeset
|
30 |
15016
871c77e78f5d
windows: fix pyflakes warning on unused imports
Matt Mackall <mpm@selenic.com>
parents:
15011
diff
changeset
|
31 executablepath = win32.executablepath |
871c77e78f5d
windows: fix pyflakes warning on unused imports
Matt Mackall <mpm@selenic.com>
parents:
15011
diff
changeset
|
32 getuser = win32.getuser |
871c77e78f5d
windows: fix pyflakes warning on unused imports
Matt Mackall <mpm@selenic.com>
parents:
15011
diff
changeset
|
33 hidewindow = win32.hidewindow |
871c77e78f5d
windows: fix pyflakes warning on unused imports
Matt Mackall <mpm@selenic.com>
parents:
15011
diff
changeset
|
34 makedir = win32.makedir |
871c77e78f5d
windows: fix pyflakes warning on unused imports
Matt Mackall <mpm@selenic.com>
parents:
15011
diff
changeset
|
35 nlinks = win32.nlinks |
871c77e78f5d
windows: fix pyflakes warning on unused imports
Matt Mackall <mpm@selenic.com>
parents:
15011
diff
changeset
|
36 oslink = win32.oslink |
871c77e78f5d
windows: fix pyflakes warning on unused imports
Matt Mackall <mpm@selenic.com>
parents:
15011
diff
changeset
|
37 samedevice = win32.samedevice |
871c77e78f5d
windows: fix pyflakes warning on unused imports
Matt Mackall <mpm@selenic.com>
parents:
15011
diff
changeset
|
38 samefile = win32.samefile |
871c77e78f5d
windows: fix pyflakes warning on unused imports
Matt Mackall <mpm@selenic.com>
parents:
15011
diff
changeset
|
39 setsignalhandler = win32.setsignalhandler |
871c77e78f5d
windows: fix pyflakes warning on unused imports
Matt Mackall <mpm@selenic.com>
parents:
15011
diff
changeset
|
40 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
|
41 split = os.path.split |
15016
871c77e78f5d
windows: fix pyflakes warning on unused imports
Matt Mackall <mpm@selenic.com>
parents:
15011
diff
changeset
|
42 testpid = win32.testpid |
871c77e78f5d
windows: fix pyflakes warning on unused imports
Matt Mackall <mpm@selenic.com>
parents:
15011
diff
changeset
|
43 unlink = win32.unlink |
14985
dbf91976f900
windows: eliminate win32 wildcard import
Adrian Buehlmann <adrian@cadifra.com>
parents:
14969
diff
changeset
|
44 |
25658
e93036747902
global: mass rewrite to use modern octal syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25462
diff
changeset
|
45 umask = 0o022 |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
46 |
26375
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
47 class mixedfilemodewrapper(object): |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
48 """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
|
49 |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
50 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
|
51 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
|
52 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
|
53 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
|
54 |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
55 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
|
56 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
|
57 calls when necessary. |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
58 """ |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
59 OPNONE = 0 |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
60 OPREAD = 1 |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
61 OPWRITE = 2 |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
62 |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
63 def __init__(self, fp): |
31649
f80d9ddc40f3
py3: abuse r'' to preserve str-ness of literals passed to __setattr__()
Yuya Nishihara <yuya@tcha.org>
parents:
31545
diff
changeset
|
64 object.__setattr__(self, r'_fp', fp) |
f80d9ddc40f3
py3: abuse r'' to preserve str-ness of literals passed to __setattr__()
Yuya Nishihara <yuya@tcha.org>
parents:
31545
diff
changeset
|
65 object.__setattr__(self, r'_lastop', 0) |
26375
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
66 |
31891
87f293edabb6
windows: add context manager support to mixedfilemodewrapper
Matt Harbison <matt_harbison@yahoo.com>
parents:
31649
diff
changeset
|
67 def __enter__(self): |
87f293edabb6
windows: add context manager support to mixedfilemodewrapper
Matt Harbison <matt_harbison@yahoo.com>
parents:
31649
diff
changeset
|
68 return self._fp.__enter__() |
87f293edabb6
windows: add context manager support to mixedfilemodewrapper
Matt Harbison <matt_harbison@yahoo.com>
parents:
31649
diff
changeset
|
69 |
87f293edabb6
windows: add context manager support to mixedfilemodewrapper
Matt Harbison <matt_harbison@yahoo.com>
parents:
31649
diff
changeset
|
70 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
|
71 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
|
72 |
26375
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
73 def __getattr__(self, name): |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
74 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
|
75 |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
76 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
|
77 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
|
78 |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
79 def _noopseek(self): |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
80 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
|
81 |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
82 def seek(self, *args, **kwargs): |
31649
f80d9ddc40f3
py3: abuse r'' to preserve str-ness of literals passed to __setattr__()
Yuya Nishihara <yuya@tcha.org>
parents:
31545
diff
changeset
|
83 object.__setattr__(self, r'_lastop', self.OPNONE) |
26375
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
84 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
|
85 |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
86 def write(self, d): |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
87 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
|
88 self._noopseek() |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
89 |
31649
f80d9ddc40f3
py3: abuse r'' to preserve str-ness of literals passed to __setattr__()
Yuya Nishihara <yuya@tcha.org>
parents:
31545
diff
changeset
|
90 object.__setattr__(self, r'_lastop', self.OPWRITE) |
26375
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
91 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
|
92 |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
93 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
|
94 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
|
95 self._noopeseek() |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
96 |
31649
f80d9ddc40f3
py3: abuse r'' to preserve str-ness of literals passed to __setattr__()
Yuya Nishihara <yuya@tcha.org>
parents:
31545
diff
changeset
|
97 object.__setattr__(self, r'_lastop', self.OPWRITE) |
26375
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
98 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
|
99 |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
100 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
|
101 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
|
102 self._noopseek() |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
103 |
31649
f80d9ddc40f3
py3: abuse r'' to preserve str-ness of literals passed to __setattr__()
Yuya Nishihara <yuya@tcha.org>
parents:
31545
diff
changeset
|
104 object.__setattr__(self, r'_lastop', self.OPREAD) |
26375
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
105 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
|
106 |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
107 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
|
108 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
|
109 self._noopseek() |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
110 |
31649
f80d9ddc40f3
py3: abuse r'' to preserve str-ness of literals passed to __setattr__()
Yuya Nishihara <yuya@tcha.org>
parents:
31545
diff
changeset
|
111 object.__setattr__(self, r'_lastop', self.OPREAD) |
26375
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
112 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
|
113 |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
114 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
|
115 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
|
116 self._noopseek() |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
117 |
31649
f80d9ddc40f3
py3: abuse r'' to preserve str-ness of literals passed to __setattr__()
Yuya Nishihara <yuya@tcha.org>
parents:
31545
diff
changeset
|
118 object.__setattr__(self, r'_lastop', self.OPREAD) |
26375
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
119 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
|
120 |
8421
b6d0fa8c7685
posixfile: remove posixfile_nt and fix import bug in windows.py
Sune Foldager <cryo@cyanite.org>
parents:
8364
diff
changeset
|
121 def posixfile(name, mode='r', buffering=-1): |
24069
c6666395fdd2
windows: adjust doc string and comments of posixfile()
Adrian Buehlmann <adrian@cadifra.com>
parents:
24051
diff
changeset
|
122 '''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
|
123 try: |
24069
c6666395fdd2
windows: adjust doc string and comments of posixfile()
Adrian Buehlmann <adrian@cadifra.com>
parents:
24051
diff
changeset
|
124 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
|
125 |
7956d17431bc
windows: seek to the end of posixfile when opening in append mode
Matt Harbison <matt_harbison@yahoo.com>
parents:
23682
diff
changeset
|
126 # 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
|
127 # make it consistent with other platforms, which position at EOF. |
7956d17431bc
windows: seek to the end of posixfile when opening in append mode
Matt Harbison <matt_harbison@yahoo.com>
parents:
23682
diff
changeset
|
128 if 'a' in mode: |
25462
021e68d37c5b
windows: use os.SEEK_END
Adrian Buehlmann <adrian@cadifra.com>
parents:
25420
diff
changeset
|
129 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
|
130 |
26375
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
131 if '+' in mode: |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
132 return mixedfilemodewrapper(fp) |
3686fa2b8eee
windows: insert file positioning call between reads and writes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
133 |
24051
7956d17431bc
windows: seek to the end of posixfile when opening in append mode
Matt Harbison <matt_harbison@yahoo.com>
parents:
23682
diff
changeset
|
134 return fp |
25660
328739ea70c3
global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25658
diff
changeset
|
135 except WindowsError as err: |
24069
c6666395fdd2
windows: adjust doc string and comments of posixfile()
Adrian Buehlmann <adrian@cadifra.com>
parents:
24051
diff
changeset
|
136 # convert to a friendlier exception |
9448
bc6b0fef9495
windows: provide filename in IOError exceptions
Steve Borho <steve@borho.org>
parents:
9174
diff
changeset
|
137 raise IOError(err.errno, '%s: %s' % (name, err.strerror)) |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
138 |
32248
d74b0cff94a9
osutil: proxy through util (and platform) modules (API)
Yuya Nishihara <yuya@tcha.org>
parents:
31891
diff
changeset
|
139 # may be wrapped by win32mbcs extension |
d74b0cff94a9
osutil: proxy through util (and platform) modules (API)
Yuya Nishihara <yuya@tcha.org>
parents:
31891
diff
changeset
|
140 listdir = osutil.listdir |
d74b0cff94a9
osutil: proxy through util (and platform) modules (API)
Yuya Nishihara <yuya@tcha.org>
parents:
31891
diff
changeset
|
141 |
8778
c5f36402daad
use new style classes
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8761
diff
changeset
|
142 class winstdout(object): |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
143 '''stdout on windows misbehaves if sent through a pipe''' |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
144 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
145 def __init__(self, fp): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
146 self.fp = fp |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
147 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
148 def __getattr__(self, key): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
149 return getattr(self.fp, key) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
150 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
151 def close(self): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
152 try: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
153 self.fp.close() |
14004
97ed99d1f419
eliminate various naked except clauses
Idan Kamara <idankk86@gmail.com>
parents:
13986
diff
changeset
|
154 except IOError: |
97ed99d1f419
eliminate various naked except clauses
Idan Kamara <idankk86@gmail.com>
parents:
13986
diff
changeset
|
155 pass |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
156 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
157 def write(self, s): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
158 try: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
159 # This is workaround for "Not enough space" error on |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
160 # writing large size of data to console. |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
161 limit = 16000 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
162 l = len(s) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
163 start = 0 |
10394
4612cded5176
fix coding style (reported by pylint)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10282
diff
changeset
|
164 self.softspace = 0 |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
165 while start < l: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
166 end = start + limit |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
167 self.fp.write(s[start:end]) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
168 start = end |
25660
328739ea70c3
global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25658
diff
changeset
|
169 except IOError as inst: |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
170 if inst.errno != 0: |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
171 raise |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
172 self.close() |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
173 raise IOError(errno.EPIPE, 'Broken pipe') |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
174 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
175 def flush(self): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
176 try: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
177 return self.fp.flush() |
25660
328739ea70c3
global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25658
diff
changeset
|
178 except IOError as inst: |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
179 if inst.errno != errno.EINVAL: |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
180 raise |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
181 self.close() |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
182 raise IOError(errno.EPIPE, 'Broken pipe') |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
183 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
184 def _is_win_9x(): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
185 '''return true if run on windows 95, 98 or me.''' |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
186 try: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
187 return sys.getwindowsversion()[3] == 1 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
188 except AttributeError: |
30641
f1c9fafcbf46
py3: replace os.environ with encoding.environ (part 3 of 5)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30620
diff
changeset
|
189 return 'command' in encoding.environ.get('comspec', '') |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
190 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
191 def openhardlinks(): |
13375
f1fa8f481c7c
port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents:
13280
diff
changeset
|
192 return not _is_win_9x() |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
193 |
14231
8abe4db2d162
rename util.parse_patch_output to parsepatchoutput
Adrian Buehlmann <adrian@cadifra.com>
parents:
14064
diff
changeset
|
194 def parsepatchoutput(output_line): |
8761
0289f384e1e5
Generally replace "file name" with "filename" in help and comments.
timeless <timeless@gmail.com>
parents:
8657
diff
changeset
|
195 """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
|
196 pf = output_line[14:] |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
197 if pf[0] == '`': |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
198 pf = pf[1:-1] # Remove the quotes |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
199 return pf |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
200 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
201 def sshargs(sshcmd, host, user, port): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
202 '''Build argument list for ssh or Plink''' |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
203 pflag = 'plink' in sshcmd.lower() and '-P' or '-p' |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
204 args = user and ("%s@%s" % (user, host)) or host |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
205 return port and ("%s %s %s" % (args, pflag, port)) or args |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
206 |
14232
df2399663392
rename util.set_flags to setflags
Adrian Buehlmann <adrian@cadifra.com>
parents:
14231
diff
changeset
|
207 def setflags(f, l, x): |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
208 pass |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
209 |
15011
5e44e4b3a0a3
util: move copymode into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
14985
diff
changeset
|
210 def copymode(src, dst, mode=None): |
5e44e4b3a0a3
util: move copymode into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
14985
diff
changeset
|
211 pass |
5e44e4b3a0a3
util: move copymode into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
14985
diff
changeset
|
212 |
13879
5b0a3f6cbead
util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
13777
diff
changeset
|
213 def checkexec(path): |
5b0a3f6cbead
util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
13777
diff
changeset
|
214 return False |
5b0a3f6cbead
util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
13777
diff
changeset
|
215 |
13890
31eb145b50b6
util: move checklink() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
13879
diff
changeset
|
216 def checklink(path): |
31eb145b50b6
util: move checklink() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
13879
diff
changeset
|
217 return False |
31eb145b50b6
util: move checklink() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
13879
diff
changeset
|
218 |
14233
659f34b833b9
rename util.set_binary to setbinary
Adrian Buehlmann <adrian@cadifra.com>
parents:
14232
diff
changeset
|
219 def setbinary(fd): |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
220 # 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
|
221 # fileno(), usually set to -1. |
14969
a3f97038c1c2
windows: use getattr instead of hasattr
Augie Fackler <durin42@gmail.com>
parents:
14927
diff
changeset
|
222 fno = getattr(fd, 'fileno', None) |
a3f97038c1c2
windows: use getattr instead of hasattr
Augie Fackler <durin42@gmail.com>
parents:
14927
diff
changeset
|
223 if fno is not None and fno() >= 0: |
a3f97038c1c2
windows: use getattr instead of hasattr
Augie Fackler <durin42@gmail.com>
parents:
14927
diff
changeset
|
224 msvcrt.setmode(fno(), os.O_BINARY) |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
225 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
226 def pconvert(path): |
30620
bb77654dc7ae
py3: replace os.sep with pycompat.ossep (part 3 of 4)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30617
diff
changeset
|
227 return path.replace(pycompat.ossep, '/') |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
228 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
229 def localpath(path): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
230 return path.replace('/', '\\') |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
231 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
232 def normpath(path): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
233 return pconvert(os.path.normpath(path)) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
234 |
15671
3c5e818ac679
windows: use upper() instead of lower() or os.path.normcase()
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15637
diff
changeset
|
235 def normcase(path): |
25071
8ce36dba70d1
windows: add comment in normcase()
Adrian Buehlmann <adrian@cadifra.com>
parents:
24908
diff
changeset
|
236 return encoding.upper(path) # NTFS compares via upper() |
15488
6eff984d8e76
dirstate: fix case-folding identity for traditional Unix
Matt Mackall <mpm@selenic.com>
parents:
15016
diff
changeset
|
237 |
24598
22f49c7dd11b
windows: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents:
24148
diff
changeset
|
238 # see posix.py for definitions |
22f49c7dd11b
windows: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents:
24148
diff
changeset
|
239 normcasespec = encoding.normcasespecs.upper |
22f49c7dd11b
windows: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents:
24148
diff
changeset
|
240 normcasefallback = encoding.upperfallback |
22f49c7dd11b
windows: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents:
24148
diff
changeset
|
241 |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
242 def samestat(s1, s2): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
243 return False |
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 # 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
|
246 # - 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
|
247 # 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
|
248 # - 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
|
249 # - 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
|
250 # backslash |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
251 # (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
|
252 # So, to quote a string, we must surround it in double quotes, double |
17505 | 253 # 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
|
254 # 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
|
255 # quote we've appended to the end) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
256 _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
|
257 _needsshellquote = None |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
258 def shellquote(s): |
24908
30b910fea244
windows: add doctest for shellquote()
Matt Harbison <matt_harbison@yahoo.com>
parents:
24885
diff
changeset
|
259 r""" |
30b910fea244
windows: add doctest for shellquote()
Matt Harbison <matt_harbison@yahoo.com>
parents:
24885
diff
changeset
|
260 >>> shellquote(r'C:\Users\xyz') |
30b910fea244
windows: add doctest for shellquote()
Matt Harbison <matt_harbison@yahoo.com>
parents:
24885
diff
changeset
|
261 '"C:\\Users\\xyz"' |
30b910fea244
windows: add doctest for shellquote()
Matt Harbison <matt_harbison@yahoo.com>
parents:
24885
diff
changeset
|
262 >>> shellquote(r'C:\Users\xyz/mixed') |
30b910fea244
windows: add doctest for shellquote()
Matt Harbison <matt_harbison@yahoo.com>
parents:
24885
diff
changeset
|
263 '"C:\\Users\\xyz/mixed"' |
30b910fea244
windows: add doctest for shellquote()
Matt Harbison <matt_harbison@yahoo.com>
parents:
24885
diff
changeset
|
264 >>> # Would be safe not to quote too, since it is all double backslashes |
30b910fea244
windows: add doctest for shellquote()
Matt Harbison <matt_harbison@yahoo.com>
parents:
24885
diff
changeset
|
265 >>> shellquote(r'C:\\Users\\xyz') |
30b910fea244
windows: add doctest for shellquote()
Matt Harbison <matt_harbison@yahoo.com>
parents:
24885
diff
changeset
|
266 '"C:\\\\Users\\\\xyz"' |
30b910fea244
windows: add doctest for shellquote()
Matt Harbison <matt_harbison@yahoo.com>
parents:
24885
diff
changeset
|
267 >>> # But this must be quoted |
30b910fea244
windows: add doctest for shellquote()
Matt Harbison <matt_harbison@yahoo.com>
parents:
24885
diff
changeset
|
268 >>> shellquote(r'C:\\Users\\xyz/abc') |
30b910fea244
windows: add doctest for shellquote()
Matt Harbison <matt_harbison@yahoo.com>
parents:
24885
diff
changeset
|
269 '"C:\\\\Users\\\\xyz/abc"' |
30b910fea244
windows: add doctest for shellquote()
Matt Harbison <matt_harbison@yahoo.com>
parents:
24885
diff
changeset
|
270 """ |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
271 global _quotere |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
272 if _quotere is None: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
273 _quotere = re.compile(r'(\\*)("|\\$)') |
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
|
274 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
|
275 if _needsshellquote is None: |
24885
eea3977e6fca
windows: make shellquote() quote any path containing '\' (issue4629)
Matt Harbison <matt_harbison@yahoo.com>
parents:
24692
diff
changeset
|
276 # ":" 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
|
277 # 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
|
278 # 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
|
279 # 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
|
280 # "\". |
eea3977e6fca
windows: make shellquote() quote any path containing '\' (issue4629)
Matt Harbison <matt_harbison@yahoo.com>
parents:
24692
diff
changeset
|
281 _needsshellquote = re.compile(r'[^a-zA-Z0-9._:/-]').search |
24108
d65ecb814fc0
shellquote: fix missing quotes for empty string
Yuya Nishihara <yuya@tcha.org>
parents:
23682
diff
changeset
|
282 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
|
283 # "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
|
284 return s |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
285 return '"%s"' % _quotere.sub(r'\1\1\\\2', s) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
286 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
287 def quotecommand(cmd): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
288 """Build a command string suitable for os.popen* calls.""" |
13188
6c9345f9edca
util: concentrate quoting knowledge to windows.py quotecommand()
Steve Borho <steve@borho.org>
parents:
12689
diff
changeset
|
289 if sys.version_info < (2, 7, 1): |
6c9345f9edca
util: concentrate quoting knowledge to windows.py quotecommand()
Steve Borho <steve@borho.org>
parents:
12689
diff
changeset
|
290 # Python versions since 2.7.1 do this extra quoting themselves |
6c9345f9edca
util: concentrate quoting knowledge to windows.py quotecommand()
Steve Borho <steve@borho.org>
parents:
12689
diff
changeset
|
291 return '"' + cmd + '"' |
6c9345f9edca
util: concentrate quoting knowledge to windows.py quotecommand()
Steve Borho <steve@borho.org>
parents:
12689
diff
changeset
|
292 return cmd |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
293 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
294 def popen(command, mode='r'): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
295 # Work around "popen spawned process may not write to stdout |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
296 # under windows" |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
297 # http://bugs.python.org/issue1366 |
17391
fc24c10424d2
util: replace util.nulldev with os.devnull
Ross Lagerwall <rosslagerwall@gmail.com>
parents:
17223
diff
changeset
|
298 command += " 2> %s" % os.devnull |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
299 return os.popen(quotecommand(command), mode) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
300 |
14234
600e64004eb5
rename explain_exit to explainexit
Adrian Buehlmann <adrian@cadifra.com>
parents:
14233
diff
changeset
|
301 def explainexit(code): |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
302 return _("exited with status %d") % code, code |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
303 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
304 # 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
|
305 # username and groupname functions above, too. |
8657
3fa92c618624
posix: do not use fstat in isowner
Martin Geisler <mg@lazybytes.net>
parents:
8614
diff
changeset
|
306 def isowner(st): |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
307 return True |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
308 |
14271
4030630fb59c
rename util.find_exe to findexe
Adrian Buehlmann <adrian@cadifra.com>
parents:
14234
diff
changeset
|
309 def findexe(command): |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
310 '''Find executable for command searching like cmd.exe does. |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
311 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
|
312 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
|
313 An extension from PATHEXT is found and added if not present. |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
314 If command isn't found None is returned.''' |
30641
f1c9fafcbf46
py3: replace os.environ with encoding.environ (part 3 of 5)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30620
diff
changeset
|
315 pathext = encoding.environ.get('PATHEXT', '.COM;.EXE;.BAT;.CMD') |
30617
d623cc6b3742
py3: replace os.pathsep with pycompat.ospathsep
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30483
diff
changeset
|
316 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
|
317 if os.path.splitext(command)[1].lower() in pathexts: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
318 pathexts = [''] |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
319 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
320 def findexisting(pathcommand): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
321 'Will append extension (if needed) and return existing file' |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
322 for ext in pathexts: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
323 executable = pathcommand + ext |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
324 if os.path.exists(executable): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
325 return executable |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
326 return None |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
327 |
30620
bb77654dc7ae
py3: replace os.sep with pycompat.ossep (part 3 of 4)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30617
diff
changeset
|
328 if pycompat.ossep in command: |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
329 return findexisting(command) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
330 |
30641
f1c9fafcbf46
py3: replace os.environ with encoding.environ (part 3 of 5)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30620
diff
changeset
|
331 for path in encoding.environ.get('PATH', '').split(pycompat.ospathsep): |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
332 executable = findexisting(os.path.join(path, command)) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
333 if executable is not None: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
334 return executable |
10156
c31ac3f7fd8f
windows: expand environment vars in find_exe
Steve Borho <steve@borho.org>
parents:
9594
diff
changeset
|
335 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
|
336 |
18017
74912fe3d718
dirstate: move file type filtering to its source
Bryan O'Sullivan <bryano@fb.com>
parents:
17560
diff
changeset
|
337 _wantedkinds = set([stat.S_IFREG, stat.S_IFLNK]) |
74912fe3d718
dirstate: move file type filtering to its source
Bryan O'Sullivan <bryano@fb.com>
parents:
17560
diff
changeset
|
338 |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
339 def statfiles(files): |
18017
74912fe3d718
dirstate: move file type filtering to its source
Bryan O'Sullivan <bryano@fb.com>
parents:
17560
diff
changeset
|
340 '''Stat each file in files. Yield each stat, or None if a file |
74912fe3d718
dirstate: move file type filtering to its source
Bryan O'Sullivan <bryano@fb.com>
parents:
17560
diff
changeset
|
341 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
|
342 |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
343 Cluster and cache stat per directory to minimize number of OS stat calls.''' |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
344 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
|
345 getkind = stat.S_IFMT |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
346 for nf in files: |
15637
7f01ad702405
icasefs: use util.normcase() instead of str.lower() or os.path.normpath()
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15488
diff
changeset
|
347 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
|
348 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
|
349 if not dir: |
3d456bf32f18
Use os.path.split() for MBCS with win32mbcs extension.
Shun-ichi GOTO <shunichi.goto@gmail.com>
parents:
8951
diff
changeset
|
350 dir = '.' |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
351 cache = dircache.get(dir, None) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
352 if cache is None: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
353 try: |
15637
7f01ad702405
icasefs: use util.normcase() instead of str.lower() or os.path.normpath()
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
15488
diff
changeset
|
354 dmap = dict([(normcase(n), s) |
32248
d74b0cff94a9
osutil: proxy through util (and platform) modules (API)
Yuya Nishihara <yuya@tcha.org>
parents:
31891
diff
changeset
|
355 for n, k, s in listdir(dir, True) |
18041
f0cfa27c712a
windows: correctly pass a mode to S_IFMT in statfiles
Matt Mackall <mpm@selenic.com>
parents:
18017
diff
changeset
|
356 if getkind(s.st_mode) in _wantedkinds]) |
25660
328739ea70c3
global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25658
diff
changeset
|
357 except OSError as err: |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
358 # Python >= 2.5 returns ENOENT and adds winerror field |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
359 # EINVAL is raised if dir is not a directory. |
25204
0a48380b61fb
windows: drop Python2.4 specific hack for directory not found handling
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25071
diff
changeset
|
360 if err.errno not in (errno.ENOENT, errno.EINVAL, |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
361 errno.ENOTDIR): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
362 raise |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
363 dmap = {} |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
364 cache = dircache.setdefault(dir, dmap) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
365 yield cache.get(base, None) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
366 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
367 def username(uid=None): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
368 """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
|
369 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
370 If uid is None, return the name of the current user.""" |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
371 return None |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
372 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
373 def groupname(gid=None): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
374 """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
|
375 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
376 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
|
377 return None |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
378 |
24692
144883a8d0d4
util: add removedirs as platform depending function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
24653
diff
changeset
|
379 def removedirs(name): |
8364
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
380 """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
|
381 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
|
382 if listdir(name): |
8364
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
383 return |
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
384 os.rmdir(name) |
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
385 head, tail = os.path.split(name) |
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
386 if not tail: |
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
387 head, tail = os.path.split(head) |
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
388 while head and tail: |
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
389 try: |
32248
d74b0cff94a9
osutil: proxy through util (and platform) modules (API)
Yuya Nishihara <yuya@tcha.org>
parents:
31891
diff
changeset
|
390 if listdir(head): |
8364
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
391 return |
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
392 os.rmdir(head) |
14004
97ed99d1f419
eliminate various naked except clauses
Idan Kamara <idankk86@gmail.com>
parents:
13986
diff
changeset
|
393 except (ValueError, OSError): |
8364
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
394 break |
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
395 head, tail = os.path.split(head) |
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
396 |
9549
8b8920209317
util: move rename into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
9449
diff
changeset
|
397 def rename(src, dst): |
8b8920209317
util: move rename into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
9449
diff
changeset
|
398 '''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
|
399 try: |
8b8920209317
util: move rename into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
9449
diff
changeset
|
400 os.rename(src, dst) |
25660
328739ea70c3
global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25658
diff
changeset
|
401 except OSError as e: |
13278
e9a52ed28157
windows.rename: check OSError for EEXIST
Adrian Buehlmann <adrian@cadifra.com>
parents:
13235
diff
changeset
|
402 if e.errno != errno.EEXIST: |
e9a52ed28157
windows.rename: check OSError for EEXIST
Adrian Buehlmann <adrian@cadifra.com>
parents:
13235
diff
changeset
|
403 raise |
13280
6052bbc7aabd
reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents:
13278
diff
changeset
|
404 unlink(dst) |
9549
8b8920209317
util: move rename into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
9449
diff
changeset
|
405 os.rename(src, dst) |
8b8920209317
util: move rename into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
9449
diff
changeset
|
406 |
10239
8e4be44a676f
Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents:
10237
diff
changeset
|
407 def gethgcmd(): |
8e4be44a676f
Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents:
10237
diff
changeset
|
408 return [sys.executable] + sys.argv[:1] |
8e4be44a676f
Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents:
10237
diff
changeset
|
409 |
11138
99eee847beaa
acl: grp module is not available on windows
Patrick Mezard <pmezard@gmail.com>
parents:
11010
diff
changeset
|
410 def groupmembers(name): |
99eee847beaa
acl: grp module is not available on windows
Patrick Mezard <pmezard@gmail.com>
parents:
11010
diff
changeset
|
411 # Don't support groups on Windows for now |
16687
e34106fa0dc3
cleanup: "raise SomeException()" -> "raise SomeException"
Brodie Rao <brodie@sf.io>
parents:
16076
diff
changeset
|
412 raise KeyError |
11138
99eee847beaa
acl: grp module is not available on windows
Patrick Mezard <pmezard@gmail.com>
parents:
11010
diff
changeset
|
413 |
14926
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14892
diff
changeset
|
414 def isexec(f): |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14892
diff
changeset
|
415 return False |
4e7e63fc685a
util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents:
14892
diff
changeset
|
416 |
14927
2aa3e07b2f07
posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents:
14926
diff
changeset
|
417 class cachestat(object): |
2aa3e07b2f07
posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents:
14926
diff
changeset
|
418 def __init__(self, path): |
2aa3e07b2f07
posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents:
14926
diff
changeset
|
419 pass |
2aa3e07b2f07
posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents:
14926
diff
changeset
|
420 |
2aa3e07b2f07
posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents:
14926
diff
changeset
|
421 def cacheable(self): |
2aa3e07b2f07
posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents:
14926
diff
changeset
|
422 return False |
2aa3e07b2f07
posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents:
14926
diff
changeset
|
423 |
16807
80142f385af9
win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
16687
diff
changeset
|
424 def lookupreg(key, valname=None, scope=None): |
80142f385af9
win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
16687
diff
changeset
|
425 ''' Look up a key/value name in the Windows registry. |
80142f385af9
win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
16687
diff
changeset
|
426 |
80142f385af9
win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
16687
diff
changeset
|
427 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
|
428 is used. |
80142f385af9
win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
16687
diff
changeset
|
429 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
|
430 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
|
431 LOCAL_MACHINE). |
80142f385af9
win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
16687
diff
changeset
|
432 ''' |
80142f385af9
win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
16687
diff
changeset
|
433 if scope is None: |
29772
3df9f780c90e
py3: conditionalize _winreg import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29530
diff
changeset
|
434 scope = (winreg.HKEY_CURRENT_USER, winreg.HKEY_LOCAL_MACHINE) |
16807
80142f385af9
win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
16687
diff
changeset
|
435 elif not isinstance(scope, (list, tuple)): |
80142f385af9
win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
16687
diff
changeset
|
436 scope = (scope,) |
80142f385af9
win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
16687
diff
changeset
|
437 for s in scope: |
80142f385af9
win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
16687
diff
changeset
|
438 try: |
29772
3df9f780c90e
py3: conditionalize _winreg import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29530
diff
changeset
|
439 val = winreg.QueryValueEx(winreg.OpenKey(s, key), valname)[0] |
16807
80142f385af9
win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
16687
diff
changeset
|
440 # never let a Unicode string escape into the wild |
31456
067add650129
encoding: factor out unicode variants of from/tolocal()
Yuya Nishihara <yuya@tcha.org>
parents:
30641
diff
changeset
|
441 return encoding.unitolocal(val) |
16807
80142f385af9
win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
16687
diff
changeset
|
442 except EnvironmentError: |
80142f385af9
win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
16687
diff
changeset
|
443 pass |
80142f385af9
win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
16687
diff
changeset
|
444 |
8614
573734e7e6d0
cmdutils: Take over glob expansion duties from util
Matt Mackall <mpm@selenic.com>
parents:
8559
diff
changeset
|
445 expandglobs = True |
18868
cafa447a7d3b
util: add functions to check symlink/exec bits
Bryan O'Sullivan <bryano@fb.com>
parents:
18143
diff
changeset
|
446 |
cafa447a7d3b
util: add functions to check symlink/exec bits
Bryan O'Sullivan <bryano@fb.com>
parents:
18143
diff
changeset
|
447 def statislink(st): |
cafa447a7d3b
util: add functions to check symlink/exec bits
Bryan O'Sullivan <bryano@fb.com>
parents:
18143
diff
changeset
|
448 '''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
|
449 return False |
cafa447a7d3b
util: add functions to check symlink/exec bits
Bryan O'Sullivan <bryano@fb.com>
parents:
18143
diff
changeset
|
450 |
cafa447a7d3b
util: add functions to check symlink/exec bits
Bryan O'Sullivan <bryano@fb.com>
parents:
18143
diff
changeset
|
451 def statisexec(st): |
cafa447a7d3b
util: add functions to check symlink/exec bits
Bryan O'Sullivan <bryano@fb.com>
parents:
18143
diff
changeset
|
452 '''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
|
453 return False |
22245
234e4c24b980
platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
20202
diff
changeset
|
454 |
25420
c2ec81891502
util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25204
diff
changeset
|
455 def poll(fds): |
c2ec81891502
util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25204
diff
changeset
|
456 # see posix.py for description |
c2ec81891502
util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25204
diff
changeset
|
457 raise NotImplementedError() |
c2ec81891502
util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25204
diff
changeset
|
458 |
22245
234e4c24b980
platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
20202
diff
changeset
|
459 def readpipe(pipe): |
234e4c24b980
platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
20202
diff
changeset
|
460 """Read all available data from a pipe.""" |
234e4c24b980
platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
20202
diff
changeset
|
461 chunks = [] |
234e4c24b980
platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
20202
diff
changeset
|
462 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
|
463 size = win32.peekpipe(pipe) |
22245
234e4c24b980
platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
20202
diff
changeset
|
464 if not size: |
234e4c24b980
platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
20202
diff
changeset
|
465 break |
234e4c24b980
platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
20202
diff
changeset
|
466 |
234e4c24b980
platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
20202
diff
changeset
|
467 s = pipe.read(size) |
234e4c24b980
platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
20202
diff
changeset
|
468 if not s: |
234e4c24b980
platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
20202
diff
changeset
|
469 break |
234e4c24b980
platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
20202
diff
changeset
|
470 chunks.append(s) |
234e4c24b980
platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
20202
diff
changeset
|
471 |
234e4c24b980
platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
20202
diff
changeset
|
472 return ''.join(chunks) |
29530
3239e2fdd2e2
chgserver: extract utility to bind unix domain socket to long path
Yuya Nishihara <yuya@tcha.org>
parents:
27436
diff
changeset
|
473 |
3239e2fdd2e2
chgserver: extract utility to bind unix domain socket to long path
Yuya Nishihara <yuya@tcha.org>
parents:
27436
diff
changeset
|
474 def bindunixsocket(sock, path): |
3239e2fdd2e2
chgserver: extract utility to bind unix domain socket to long path
Yuya Nishihara <yuya@tcha.org>
parents:
27436
diff
changeset
|
475 raise NotImplementedError('unsupported platform') |