Mercurial > hg
annotate mercurial/pure/osutil.py @ 50677:b8de54ac5a21
perf: add a perf::stream-generate command
This record the time we take to generate a bundle.
author | Pierre-Yves David <pierre-yves.david@octobus.net> |
---|---|
date | Tue, 06 Jun 2023 04:09:05 +0200 |
parents | c6a3243567b6 |
children | 18c8c18993f0 |
rev | line source |
---|---|
8232
823f25b25dea
pure/osutil: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents:
7704
diff
changeset
|
1 # osutil.py - pure Python version of osutil.c |
823f25b25dea
pure/osutil: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents:
7704
diff
changeset
|
2 # |
46819
d4ba4d51f85f
contributor: change mentions of mpm to olivia
Raphaël Gomès <rgomes@octobus.net>
parents:
46786
diff
changeset
|
3 # Copyright 2009 Olivia Mackall <olivia@selenic.com> and others |
8232
823f25b25dea
pure/osutil: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents:
7704
diff
changeset
|
4 # |
823f25b25dea
pure/osutil: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents:
7704
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. |
8232
823f25b25dea
pure/osutil: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents:
7704
diff
changeset
|
7 |
27338
810337ae1b76
osutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25645
diff
changeset
|
8 |
27474
e517a89c24e1
osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents:
27338
diff
changeset
|
9 import ctypes |
e517a89c24e1
osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents:
27338
diff
changeset
|
10 import ctypes.util |
7704
30d1d313370b
move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
11 import os |
10651
5f091fc1bab7
style: use consistent variable names (*mod) with imports which would shadow
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10282
diff
changeset
|
12 import stat as statmod |
7704
30d1d313370b
move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
13 |
43089
c59eb1560c44
py3: manually import getattr where it is needed
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43077
diff
changeset
|
14 from ..pycompat import getattr |
32367
a9c71d578a1c
osutil: switch to policy importer
Yuya Nishihara <yuya@tcha.org>
parents:
31644
diff
changeset
|
15 from .. import ( |
39644
3b421154d2ca
py3: fix str vs bytes in enough places to run `hg version` on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
38783
diff
changeset
|
16 encoding, |
30304
ba2c04059317
py3: use pycompat.ossep at certain places
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29821
diff
changeset
|
17 pycompat, |
ba2c04059317
py3: use pycompat.ossep at certain places
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29821
diff
changeset
|
18 ) |
ba2c04059317
py3: use pycompat.ossep at certain places
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29821
diff
changeset
|
19 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42524
diff
changeset
|
20 |
7704
30d1d313370b
move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
21 def _mode_to_kind(mode): |
10651
5f091fc1bab7
style: use consistent variable names (*mod) with imports which would shadow
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10282
diff
changeset
|
22 if statmod.S_ISREG(mode): |
5f091fc1bab7
style: use consistent variable names (*mod) with imports which would shadow
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10282
diff
changeset
|
23 return statmod.S_IFREG |
5f091fc1bab7
style: use consistent variable names (*mod) with imports which would shadow
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10282
diff
changeset
|
24 if statmod.S_ISDIR(mode): |
5f091fc1bab7
style: use consistent variable names (*mod) with imports which would shadow
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10282
diff
changeset
|
25 return statmod.S_IFDIR |
5f091fc1bab7
style: use consistent variable names (*mod) with imports which would shadow
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10282
diff
changeset
|
26 if statmod.S_ISLNK(mode): |
5f091fc1bab7
style: use consistent variable names (*mod) with imports which would shadow
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10282
diff
changeset
|
27 return statmod.S_IFLNK |
5f091fc1bab7
style: use consistent variable names (*mod) with imports which would shadow
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10282
diff
changeset
|
28 if statmod.S_ISBLK(mode): |
5f091fc1bab7
style: use consistent variable names (*mod) with imports which would shadow
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10282
diff
changeset
|
29 return statmod.S_IFBLK |
5f091fc1bab7
style: use consistent variable names (*mod) with imports which would shadow
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10282
diff
changeset
|
30 if statmod.S_ISCHR(mode): |
5f091fc1bab7
style: use consistent variable names (*mod) with imports which would shadow
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10282
diff
changeset
|
31 return statmod.S_IFCHR |
5f091fc1bab7
style: use consistent variable names (*mod) with imports which would shadow
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10282
diff
changeset
|
32 if statmod.S_ISFIFO(mode): |
5f091fc1bab7
style: use consistent variable names (*mod) with imports which would shadow
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10282
diff
changeset
|
33 return statmod.S_IFIFO |
5f091fc1bab7
style: use consistent variable names (*mod) with imports which would shadow
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10282
diff
changeset
|
34 if statmod.S_ISSOCK(mode): |
5f091fc1bab7
style: use consistent variable names (*mod) with imports which would shadow
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10282
diff
changeset
|
35 return statmod.S_IFSOCK |
7704
30d1d313370b
move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
36 return mode |
30d1d313370b
move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
37 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42524
diff
changeset
|
38 |
32512
0e8b0b9a7acc
cffi: split modules from pure
Yuya Nishihara <yuya@tcha.org>
parents:
32506
diff
changeset
|
39 def listdir(path, stat=False, skip=None): |
45942
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
45941
diff
changeset
|
40 """listdir(path, stat=False) -> list_of_tuples |
7704
30d1d313370b
move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
41 |
30d1d313370b
move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
42 Return a sorted list containing information about the entries |
30d1d313370b
move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
43 in the directory. |
30d1d313370b
move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
44 |
30d1d313370b
move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
45 If stat is True, each element is a 3-tuple: |
30d1d313370b
move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
46 |
30d1d313370b
move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
47 (name, type, stat object) |
30d1d313370b
move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
48 |
30d1d313370b
move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
49 Otherwise, each element is a 2-tuple: |
30d1d313370b
move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
50 |
30d1d313370b
move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
51 (name, type) |
45942
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
45941
diff
changeset
|
52 """ |
7704
30d1d313370b
move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
53 result = [] |
30d1d313370b
move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
54 prefix = path |
30304
ba2c04059317
py3: use pycompat.ossep at certain places
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29821
diff
changeset
|
55 if not prefix.endswith(pycompat.ossep): |
ba2c04059317
py3: use pycompat.ossep at certain places
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29821
diff
changeset
|
56 prefix += pycompat.ossep |
7704
30d1d313370b
move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
57 names = os.listdir(path) |
30d1d313370b
move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
58 names.sort() |
30d1d313370b
move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
59 for fn in names: |
30d1d313370b
move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
60 st = os.lstat(prefix + fn) |
10651
5f091fc1bab7
style: use consistent variable names (*mod) with imports which would shadow
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10282
diff
changeset
|
61 if fn == skip and statmod.S_ISDIR(st.st_mode): |
7704
30d1d313370b
move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
62 return [] |
30d1d313370b
move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
63 if stat: |
30d1d313370b
move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
64 result.append((fn, _mode_to_kind(st.st_mode), st)) |
30d1d313370b
move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
65 else: |
30d1d313370b
move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
66 result.append((fn, _mode_to_kind(st.st_mode))) |
30d1d313370b
move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
67 return result |
8421
b6d0fa8c7685
posixfile: remove posixfile_nt and fix import bug in windows.py
Sune Foldager <cryo@cyanite.org>
parents:
8232
diff
changeset
|
68 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42524
diff
changeset
|
69 |
34645 | 70 if not pycompat.iswindows: |
14413
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
71 posixfile = open |
27474
e517a89c24e1
osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents:
27338
diff
changeset
|
72 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42524
diff
changeset
|
73 |
14413
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
74 else: |
27338
810337ae1b76
osutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25645
diff
changeset
|
75 import msvcrt |
14413
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
76 |
46786
52528570312e
typing: disable module attribute warnings for properly conditionalized code
Matt Harbison <matt_harbison@yahoo.com>
parents:
45942
diff
changeset
|
77 _kernel32 = ctypes.windll.kernel32 # pytype: disable=module-attr |
14413
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
78 |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
79 _DWORD = ctypes.c_ulong |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
80 _LPCSTR = _LPSTR = ctypes.c_char_p |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
81 _HANDLE = ctypes.c_void_p |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
82 |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
83 _INVALID_HANDLE_VALUE = _HANDLE(-1).value |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
84 |
18959
2f6418d8a4c9
check-code: catch trailing space in comments
Mads Kiilerich <madski@unity3d.com>
parents:
17429
diff
changeset
|
85 # CreateFile |
14413
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
86 _FILE_SHARE_READ = 0x00000001 |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
87 _FILE_SHARE_WRITE = 0x00000002 |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
88 _FILE_SHARE_DELETE = 0x00000004 |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
89 |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
90 _CREATE_ALWAYS = 2 |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
91 _OPEN_EXISTING = 3 |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
92 _OPEN_ALWAYS = 4 |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
93 |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
94 _GENERIC_READ = 0x80000000 |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
95 _GENERIC_WRITE = 0x40000000 |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
96 |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
97 _FILE_ATTRIBUTE_NORMAL = 0x80 |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
98 |
17429
72fa4ef2245f
declare local constants instead of using magic values and comments
Mads Kiilerich <mads@kiilerich.com>
parents:
16686
diff
changeset
|
99 # open_osfhandle flags |
14413
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
100 _O_RDONLY = 0x0000 |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
101 _O_RDWR = 0x0002 |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
102 _O_APPEND = 0x0008 |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
103 |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
104 _O_TEXT = 0x4000 |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
105 _O_BINARY = 0x8000 |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
106 |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
107 # types of parameters of C functions used (required by pypy) |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
108 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42524
diff
changeset
|
109 _kernel32.CreateFileA.argtypes = [ |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42524
diff
changeset
|
110 _LPCSTR, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42524
diff
changeset
|
111 _DWORD, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42524
diff
changeset
|
112 _DWORD, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42524
diff
changeset
|
113 ctypes.c_void_p, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42524
diff
changeset
|
114 _DWORD, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42524
diff
changeset
|
115 _DWORD, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42524
diff
changeset
|
116 _HANDLE, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42524
diff
changeset
|
117 ] |
14413
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
118 _kernel32.CreateFileA.restype = _HANDLE |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
119 |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
120 def _raiseioerror(name): |
46786
52528570312e
typing: disable module attribute warnings for properly conditionalized code
Matt Harbison <matt_harbison@yahoo.com>
parents:
45942
diff
changeset
|
121 err = ctypes.WinError() # pytype: disable=module-attr |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42524
diff
changeset
|
122 raise IOError( |
43506
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43503
diff
changeset
|
123 err.errno, '%s: %s' % (encoding.strfromlocal(name), err.strerror) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42524
diff
changeset
|
124 ) |
14413
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
125 |
48946
642e31cb55f0
py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
126 class posixfile: |
45942
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
45941
diff
changeset
|
127 """a file object aiming for POSIX-like semantics |
14413
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
128 |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
129 CPython's open() returns a file that was opened *without* setting the |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
130 _FILE_SHARE_DELETE flag, which causes rename and unlink to abort. |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
131 This even happens if any hardlinked copy of the file is in open state. |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
132 We set _FILE_SHARE_DELETE here, so files opened with posixfile can be |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
133 renamed and deleted while they are held open. |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
134 Note that if a file opened with posixfile is unlinked, the file |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
135 remains but cannot be opened again or be recreated under the same name, |
45942
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
45941
diff
changeset
|
136 until all reading processes have closed the file.""" |
14413
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
137 |
39644
3b421154d2ca
py3: fix str vs bytes in enough places to run `hg version` on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
38783
diff
changeset
|
138 def __init__(self, name, mode=b'r', bufsize=-1): |
3b421154d2ca
py3: fix str vs bytes in enough places to run `hg version` on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
38783
diff
changeset
|
139 if b'b' in mode: |
14413
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
140 flags = _O_BINARY |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
141 else: |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
142 flags = _O_TEXT |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
143 |
39644
3b421154d2ca
py3: fix str vs bytes in enough places to run `hg version` on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
38783
diff
changeset
|
144 m0 = mode[0:1] |
3b421154d2ca
py3: fix str vs bytes in enough places to run `hg version` on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
38783
diff
changeset
|
145 if m0 == b'r' and b'+' not in mode: |
14413
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
146 flags |= _O_RDONLY |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
147 access = _GENERIC_READ |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
148 else: |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
149 # work around http://support.microsoft.com/kb/899149 and |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
150 # set _O_RDWR for 'w' and 'a', even if mode has no '+' |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
151 flags |= _O_RDWR |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
152 access = _GENERIC_READ | _GENERIC_WRITE |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
153 |
39644
3b421154d2ca
py3: fix str vs bytes in enough places to run `hg version` on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
38783
diff
changeset
|
154 if m0 == b'r': |
14413
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
155 creation = _OPEN_EXISTING |
39644
3b421154d2ca
py3: fix str vs bytes in enough places to run `hg version` on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
38783
diff
changeset
|
156 elif m0 == b'w': |
14413
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
157 creation = _CREATE_ALWAYS |
39644
3b421154d2ca
py3: fix str vs bytes in enough places to run `hg version` on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
38783
diff
changeset
|
158 elif m0 == b'a': |
14413
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
159 creation = _OPEN_ALWAYS |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
160 flags |= _O_APPEND |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
161 else: |
43503
313e3a279828
cleanup: remove pointless r-prefixes on double-quoted strings
Augie Fackler <augie@google.com>
parents:
43089
diff
changeset
|
162 raise ValueError("invalid mode: %s" % pycompat.sysstr(mode)) |
14413
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
163 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42524
diff
changeset
|
164 fh = _kernel32.CreateFileA( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42524
diff
changeset
|
165 name, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42524
diff
changeset
|
166 access, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42524
diff
changeset
|
167 _FILE_SHARE_READ | _FILE_SHARE_WRITE | _FILE_SHARE_DELETE, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42524
diff
changeset
|
168 None, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42524
diff
changeset
|
169 creation, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42524
diff
changeset
|
170 _FILE_ATTRIBUTE_NORMAL, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42524
diff
changeset
|
171 None, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42524
diff
changeset
|
172 ) |
14413
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
173 if fh == _INVALID_HANDLE_VALUE: |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
174 _raiseioerror(name) |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
175 |
46786
52528570312e
typing: disable module attribute warnings for properly conditionalized code
Matt Harbison <matt_harbison@yahoo.com>
parents:
45942
diff
changeset
|
176 fd = msvcrt.open_osfhandle(fh, flags) # pytype: disable=module-attr |
14413
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
177 if fd == -1: |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
178 _kernel32.CloseHandle(fh) |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
179 _raiseioerror(name) |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
180 |
30925
82f1ef8b4477
py3: convert the mode argument of os.fdopen to unicodes (2 of 2)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30642
diff
changeset
|
181 f = os.fdopen(fd, pycompat.sysstr(mode), bufsize) |
14413
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
182 # unfortunately, f.name is '<fdopen>' at this point -- so we store |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
183 # the name on this wrapper. We cannot just assign to f.name, |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
184 # because that attribute is read-only. |
43506
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43503
diff
changeset
|
185 object.__setattr__(self, 'name', name) |
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43503
diff
changeset
|
186 object.__setattr__(self, '_file', f) |
14413
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
187 |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
188 def __iter__(self): |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
189 return self._file |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
190 |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
191 def __getattr__(self, name): |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
192 return getattr(self._file, name) |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
193 |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
194 def __setattr__(self, name, value): |
45942
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
45941
diff
changeset
|
195 """mimics the read-only attributes of Python file objects |
14413
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
196 by raising 'TypeError: readonly attribute' if someone tries: |
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
197 f = posixfile('foo.txt') |
45941
346af7687c6f
osutil: reformat triple-quoted string so black doesn't confuse itself
Augie Fackler <raf@durin42.com>
parents:
43506
diff
changeset
|
198 f.name = 'bla' |
45942
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
45941
diff
changeset
|
199 """ |
14413
5ef18e28df19
pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents:
10651
diff
changeset
|
200 return self._file.__setattr__(name, value) |
27704
051b0dcec98b
osutil: implement __enter__ and __exit__ on posixfile
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27512
diff
changeset
|
201 |
051b0dcec98b
osutil: implement __enter__ and __exit__ on posixfile
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27512
diff
changeset
|
202 def __enter__(self): |
40940
120ecb17242b
windows: ensure pure posixfile fd doesn't escape by entering context manager
Matt Harbison <matt_harbison@yahoo.com>
parents:
39644
diff
changeset
|
203 self._file.__enter__() |
120ecb17242b
windows: ensure pure posixfile fd doesn't escape by entering context manager
Matt Harbison <matt_harbison@yahoo.com>
parents:
39644
diff
changeset
|
204 return self |
27704
051b0dcec98b
osutil: implement __enter__ and __exit__ on posixfile
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27512
diff
changeset
|
205 |
051b0dcec98b
osutil: implement __enter__ and __exit__ on posixfile
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27512
diff
changeset
|
206 def __exit__(self, exc_type, exc_value, exc_tb): |
051b0dcec98b
osutil: implement __enter__ and __exit__ on posixfile
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27512
diff
changeset
|
207 return self._file.__exit__(exc_type, exc_value, exc_tb) |