FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
RustPython/scripts/libc_posix.py at main · RustPython/RustPython · GitHub
RustPython
RustPython
Repository navigation
Code
Issues
183
(183)
Pull requests
111
(111)
Discussions
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
RustPython
/
scripts
/
libc_posix.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
248 lines (207 loc) · 6.67 KB
Breadcrumbs
RustPython
/
scripts
/
libc_posix.py
Copy path
File metadata and controls
248 lines (207 loc) · 6.67 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/usr/bin/env python
import
collections
import
dataclasses
import
pathlib
import
re
import
subprocess
import
urllib
.
request
import
tomllib
CPYTHON_VERSION
=
"3.14"
CONSTS_PATTERN
=
re
.
compile
(
r"\b_*[A-Z]+(?:_+[A-Z]+)*_*\b"
)
CARGO_TOML_FILE
=
pathlib
.
Path
(
__file__
).
parents
[
1
]
/
"Cargo.toml"
CARGO_TOML
=
tomllib
.
loads
(
CARGO_TOML_FILE
.
read_text
())
LIBC_DATA
=
CARGO_TOML
[
"workspace"
][
"dependencies"
][
"libc"
]
LIBC_VERSION
=
LIBC_DATA
[
"version"
]
if
isinstance
(
LIBC_DATA
,
dict
)
else
LIBC_DATA
BASE_URL
=
f"https://raw.githubusercontent.com/rust-lang/libc/refs/tags/
{
LIBC_VERSION
}
"
EXCLUDE
=
frozenset
(
{
# Defined at `vm/src/stdlib/os.rs`
"O_APPEND"
,
"O_CREAT"
,
"O_EXCL"
,
"O_RDONLY"
,
"O_RDWR"
,
"O_TRUNC"
,
"O_WRONLY"
,
"SEEK_CUR"
,
"SEEK_END"
,
"SEEK_SET"
,
# Functions, not consts
"WCOREDUMP"
,
"WIFCONTINUED"
,
"WIFSTOPPED"
,
"WIFSIGNALED"
,
"WIFEXITED"
,
"WEXITSTATUS"
,
"WSTOPSIG"
,
"WTERMSIG"
,
# False positive
# "EOF",
}
)
def
rustfmt
(
code
:
str
)
->
str
:
return
subprocess
.
check_output
([
"rustfmt"
,
"--emit=stdout"
],
input
=
code
,
text
=
True
)
@
dataclasses
.
dataclass
(
eq
=
True
,
frozen
=
True
,
slots
=
True
)
class
Cfg
:
inner
:
str
def
__str__
(
self
)
->
str
:
return
self
.
inner
def
__lt__
(
self
,
other
)
->
bool
:
si
,
oi
=
map
(
str
, (
self
.
inner
,
other
.
inner
))
# Smaller length cfgs are smaller, regardless of value.
return
(
len
(
si
),
si
)
<
(
len
(
oi
),
oi
)
@
dataclasses
.
dataclass
(
frozen
=
True
,
kw_only
=
True
,
slots
=
True
)
class
Target
:
cfgs
:
set
[
Cfg
]
sources
:
set
[
str
]
=
dataclasses
.
field
(
default_factory
=
set
)
extras
:
set
[
str
]
=
dataclasses
.
field
(
default_factory
=
set
)
TARGETS
=
(
Target
(
cfgs
=
{
Cfg
(
'target_os = "android"'
)},
sources
=
{
f"
{
BASE_URL
}
/src/unix/linux_like/android/mod.rs"
,
f"
{
BASE_URL
}
/libc-test/semver/android.txt"
,
},
),
Target
(
cfgs
=
{
Cfg
(
'target_os = "dragonfly"'
)},
sources
=
{
f"
{
BASE_URL
}
/src/unix/bsd/freebsdlike/dragonfly/mod.rs"
,
f"
{
BASE_URL
}
/libc-test/semver/dragonfly.txt"
,
},
),
Target
(
cfgs
=
{
Cfg
(
'target_os = "freebsd"'
)},
sources
=
{
f"
{
BASE_URL
}
/src/unix/bsd/freebsdlike/freebsd/mod.rs"
,
f"
{
BASE_URL
}
/libc-test/semver/freebsd.txt"
,
},
),
Target
(
cfgs
=
{
Cfg
(
'target_os = "linux"'
)},
sources
=
{
f"
{
BASE_URL
}
/src/unix/linux_like/mod.rs"
,
f"
{
BASE_URL
}
/src/unix/linux_like/linux_l4re_shared.rs"
,
f"
{
BASE_URL
}
/libc-test/semver/linux.txt"
,
},
),
Target
(
cfgs
=
{
Cfg
(
'target_os = "macos"'
)},
sources
=
{
f"
{
BASE_URL
}
/src/unix/bsd/apple/mod.rs"
,
f"
{
BASE_URL
}
/libc-test/semver/apple.txt"
,
},
extras
=
{
"COPYFILE_DATA as _COPYFILE_DATA"
},
),
Target
(
cfgs
=
{
Cfg
(
'target_os = "netbsd"'
)},
sources
=
{
f"
{
BASE_URL
}
/src/unix/bsd/netbsdlike/netbsd/mod.rs"
,
f"
{
BASE_URL
}
/libc-test/semver/netbsd.txt"
,
},
),
Target
(
cfgs
=
{
Cfg
(
'target_os = "redox"'
)},
sources
=
{
f"
{
BASE_URL
}
/src/unix/redox/mod.rs"
,
f"
{
BASE_URL
}
/libc-test/semver/redox.txt"
,
},
),
Target
(
cfgs
=
{
Cfg
(
"unix"
)},
sources
=
{
f"
{
BASE_URL
}
/libc-test/semver/unix.txt"
}),
)
def
extract_consts
(
contents
:
str
,
*
,
pattern
:
re
.
Pattern
=
CONSTS_PATTERN
,
exclude
:
frozenset
[
str
]
=
EXCLUDE
,
)
->
frozenset
[
str
]:
"""
Extract all words that are comprised from only uppercase letters + underscores.
Parameters
----------
contents : str
Contents to extract the constants from.
pattern : re.Pattern, Optional
RE compiled pattern for extracting the consts.
exclude : frozenset[str], Optional
Items to exclude from the returned value.
Returns
-------
frozenset[str]
All constant names.
"""
result
=
frozenset
(
pattern
.
findall
(
contents
))
return
result
-
exclude
def
consts_from_url
(
url
:
str
,
*
,
pattern
:
re
.
Pattern
=
CONSTS_PATTERN
,
exclude
:
frozenset
[
str
]
=
EXCLUDE
)
->
frozenset
[
str
]:
"""
Extract all consts from the contents found at the given URL.
Parameters
----------
url : str
URL to fetch the contents from.
pattern : re.Pattern, Optional
RE compiled pattern for extracting the consts.
exclude : frozenset[str], Optional
Items to exclude from the returned value.
Returns
-------
frozenset[str]
All constant names at the URL.
"""
try
:
with
urllib
.
request
.
urlopen
(
url
)
as
f
:
contents
=
f
.
read
().
decode
()
except
urllib
.
error
.
HTTPError
as
err
:
err
.
add_note
(
url
)
raise
return
extract_consts
(
contents
,
pattern
=
pattern
,
exclude
=
exclude
)
def
main
():
# Step 1: Get all OS contants that we do want from upstream
wanted_consts
=
consts_from_url
(
f"https://docs.python.org/
{
CPYTHON_VERSION
}
/library/os.html"
,
# TODO: Exclude matches if they have `(` after (those are functions)
pattern
=
re
.
compile
(
r"\bos\.(_*[A-Z]+(?:_+[A-Z]+)*_*)"
),
)
# Step 2: build dict of what consts are available per cfg. `cfg -> {consts}`
available
=
collections
.
defaultdict
(
set
)
for
target
in
TARGETS
:
consts
=
set
()
for
source
in
target
.
sources
:
consts
|=
consts_from_url
(
source
)
for
cfg
in
target
.
cfgs
:
available
[
cfg
]
|=
consts
# Step 3: Keep only the "wanted" consts. Build a groupped mapping of `{cfgs} -> {consts}'
groups
=
collections
.
defaultdict
(
set
)
available_items
=
available
.
items
()
for
wanted_const
in
wanted_consts
:
cfgs
=
frozenset
(
cfg
for
cfg
,
consts
in
available_items
if
wanted_const
in
consts
)
if
not
cfgs
:
# We have no cfgs for a wanted const :/
continue
groups
[
cfgs
].
add
(
wanted_const
)
# Step 4: Build output
output
=
""
for
cfgs
,
consts
in
sorted
(
groups
.
items
(),
key
=
lambda
t
: (
len
(
t
[
0
]),
sorted
(
t
[
0
]))):
target
=
next
((
target
for
target
in
TARGETS
if
target
.
cfgs
==
cfgs
),
None
)
if
target
:
# If we found an exact target. Add its "extras" as-is
consts
|=
target
.
extras
cfgs_inner
=
","
.
join
(
sorted
(
map
(
str
,
cfgs
)))
if
len
(
cfgs
)
>=
2
:
cfgs_rust
=
f"#[cfg(any(
{
cfgs_inner
}
))]"
else
:
cfgs_rust
=
f"#[cfg(
{
cfgs_inner
}
)]"
imports
=
","
.
join
(
consts
)
entry
=
f"""
{
cfgs_rust
}
#[pyattr]
use libc::{{
{
imports
}
}};
"""
.
strip
()
output
+=
f"
{
entry
}
\n
\n
"
print
(
rustfmt
(
output
))
if
__name__
==
"__main__"
:
main
()
Back
|
FazBrowse Home
|
New Git URL