forked from astral-sh/ruff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_known_standard_library.py
71 lines (57 loc) · 1.7 KB
/
generate_known_standard_library.py
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
from __future__ import annotations
from pathlib import Path
from stdlibs import stdlib_module_names
PATH = Path("crates") / "ruff_python_stdlib" / "src" / "sys" / "known_stdlib.rs"
VERSIONS: list[tuple[int, int]] = [
(3, 7),
(3, 8),
(3, 9),
(3, 10),
(3, 11),
(3, 12),
(3, 13),
]
with PATH.open("w") as f:
f.write(
"""\
//! This file is generated by `scripts/generate_known_standard_library.py`
pub fn is_known_standard_library(minor_version: u8, module: &str) -> bool {
matches!((minor_version, module),
""",
)
modules_by_version = {}
for major_version, minor_version in VERSIONS:
modules = set()
for module in stdlib_module_names(f"{major_version}.{minor_version}"):
if module != "__future__":
modules.add(module)
modules_by_version[minor_version] = modules
# First, add a case for the modules that are in all versions.
ubiquitous_modules = set.intersection(*modules_by_version.values())
f.write("(_, ")
for i, module in enumerate(sorted(ubiquitous_modules)):
if i > 0:
f.write(" | ")
f.write(f'"{module}"')
f.write(")")
f.write("\n")
# Next, add any version-specific modules.
for _major_version, minor_version in VERSIONS:
version_modules = set.difference(
modules_by_version[minor_version],
ubiquitous_modules,
)
f.write(" | ")
f.write(f"({minor_version}, ")
for i, module in enumerate(sorted(version_modules)):
if i > 0:
f.write(" | ")
f.write(f'"{module}"')
f.write(")")
f.write("\n")
f.write(
"""\
)
}
""",
)