ruff/scripts/generate_known_standard_library.py
konstin 550b643e33
Add script for ecosystem wide checks of all rules and fixes (#4326)
* Add script for ecosystem wide checks of all rules and fixes

This adds my personal script for checking an entire checkout of ~2.1k packages for
panics, autofix errors and similar problems. It's not really meant to be used by anybody else but i thought it's better if it lives in the repo than if it doesn't.

For reference, this is the current output of failing autofixes: https://gist.github.com/konstin/c3fada0135af6cacec74f166adf87a00. Trimmed down to the useful information: https://gist.github.com/konstin/c864f4c300c7903a24fdda49635c5da9

* Keep github template intact

* Remove the need for ripgrep

* sort output
2023-05-22 15:23:25 +02:00

91 lines
2 KiB
Python

"""Vendored from scripts/mkstdlibs.py in PyCQA/isort.
Source:
https://github.com/PyCQA/isort/blob/e321a670d0fefdea0e04ed9d8d696434cf49bdec/scripts/mkstdlibs.py
Only the generation of the file has been modified for use in this project.
"""
from pathlib import Path
from sphinx.ext.intersphinx import fetch_inventory
URL = "https://docs.python.org/{}/objects.inv"
PATH = Path("crates") / "ruff_python" / "src" / "sys.rs"
VERSIONS: list[tuple[int, int]] = [
(3, 7),
(3, 8),
(3, 9),
(3, 10),
(3, 11),
]
class FakeConfig:
intersphinx_timeout = None
tls_verify = True
user_agent = ""
class FakeApp:
srcdir = ""
config = FakeConfig()
with PATH.open("w") as f:
f.write(
"""\
//! This file is generated by `scripts/generate_known_standard_library.py`
use once_cell::sync::Lazy;
use rustc_hash::{FxHashMap, FxHashSet};
// See: https://pycqa.github.io/isort/docs/configuration/options.html#known-standard-library
pub static KNOWN_STANDARD_LIBRARY: Lazy<FxHashMap<(u32, u32), FxHashSet<&'static str>>> =
Lazy::new(|| {
FxHashMap::from_iter([
""", # noqa: E501
)
for major, minor in VERSIONS:
version = f"{major}.{minor}"
url = URL.format(version)
invdata = fetch_inventory(FakeApp(), "", url)
modules = {
"_ast",
"posixpath",
"ntpath",
"sre_constants",
"sre_parse",
"sre_compile",
"sre",
}
for module in invdata["py:module"]:
root, *_ = module.split(".")
if root not in ["__future__", "__main__"]:
modules.add(root)
f.write(
f"""\
(
({major}, {minor}),
FxHashSet::from_iter([
""",
)
for module in sorted(modules):
f.write(
f"""\
"{module}",
""",
)
f.write(
"""\
]),
),
""",
)
f.write(
"""\
])
});
""",
)