Skim a python package to only include code used by a certain entry point

# Import/Call Entrypoint
# from mypy.stubgen import generate_stubs
from sysconfig import get_paths
from pathlib import Path
from shutil import copy2
from sys import modules
# DEFINE TARGETS
PACKAGE_NAME = "mypy"
OUTPUT_DIR = Path("./isolated_source")
SITE_PACKAGES = Path(get_paths()["purelib"])
# FILTER AND COPY ONLY TOUCHED LOCAL SOURCE FILES
for name, module in modules.items():
# Look only for modules belonging to your package
if name.startswith(PACKAGE_NAME) and hasattr(module, "__file__"):
src = Path(module.__file__)
if src.suffix == ".pyc":
src = src.with_suffix('.py')
if src.exists():
rel = src.relative_to(SITE_PACKAGES)
dst = OUTPUT_DIR.joinpath(rel)
dst.parent.mkdir(parents=True, exist_ok=True)
copy2(src, dst)
print()
print(f"{rel=}")
print(f"{src=}")
print(f"{dst=}")
print()
参与讨论