Files
RustPython/scripts/update_lib/cmd_copy_lib.py
T
youknowdot 073136a5ee Simplify library updates with explicit generic source blocks (#9214)
* Use explicit source blocks instead of module-specific updater adapters

Preserve reviewed local fragments through the existing target-bound migration metadata. Remove dedicated module rules, flags and test scaffolding while retaining generic marker and doctest handling.

Assisted-by: OpenAI Codex: model version unavailable

* Generalize selected assignment migration beyond test support

Use generic assignment/export flags and an explicit Lib Python target with existing source/destination hash checks. Retain the existing dependency-checked operation without module-specific aliases.

Assisted-by: OpenAI Codex: model version unavailable
2026-10-19 21:41:17 +09:00

128 lines
3.6 KiB
Python

#!/usr/bin/env python
"""
Copy library files from CPython.
Usage:
# Single file
python scripts/update_lib copy-lib cpython/Lib/dataclasses.py
# Directory
python scripts/update_lib copy-lib cpython/Lib/json
"""
import argparse
import pathlib
import shutil
import sys
def _ignore_bytecode_caches(directory: str, names: list[str]) -> set[str]:
return {
name
for name in names
if name == "__pycache__" and (pathlib.Path(directory) / name).is_dir()
}
def _copy_single(
src_path: pathlib.Path,
lib_path: pathlib.Path,
verbose: bool = True,
) -> None:
"""Copy a single file or directory."""
# Remove existing file/directory
if lib_path.exists():
if lib_path.is_dir():
if verbose:
print(f"Removing directory: {lib_path}")
shutil.rmtree(lib_path)
else:
if verbose:
print(f"Removing file: {lib_path}")
lib_path.unlink()
# Copy
if src_path.is_dir():
if verbose:
print(f"Copying directory: {src_path} -> {lib_path}")
lib_path.parent.mkdir(parents=True, exist_ok=True)
shutil.copytree(src_path, lib_path, ignore=_ignore_bytecode_caches)
else:
if verbose:
print(f"Copying file: {src_path} -> {lib_path}")
lib_path.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(src_path, lib_path)
def copy_lib(
src_path: pathlib.Path,
verbose: bool = True,
) -> None:
"""
Copy library file or directory from CPython.
Also copies additional files if defined in DEPENDENCIES table.
Args:
src_path: Source path (e.g., cpython/Lib/dataclasses.py or cpython/Lib/json)
verbose: Print progress messages
"""
from update_lib.deps import get_lib_paths
from update_lib.file_utils import parse_lib_path
# Extract module name and cpython prefix from path
path_str = str(src_path).replace("\\", "/")
if "/Lib/" not in path_str:
raise ValueError(f"Path must contain '/Lib/' (got: {src_path})")
if not src_path.exists():
raise FileNotFoundError(f"Source path does not exist: {src_path}")
# Data files name an exact asset, not the containing top-level module.
# Expanding Lib/test/data.txt as "test" would replace the entire test tree.
if src_path.is_file() and src_path.suffix != ".py":
_copy_single(src_path, parse_lib_path(src_path), verbose)
return
cpython_prefix, after_lib = path_str.split("/Lib/", 1)
# Get module name (first component, without .py)
name = after_lib.split("/")[0]
if name.endswith(".py"):
name = name[:-3]
# Get all paths to copy from DEPENDENCIES table
all_src_paths = get_lib_paths(name, cpython_prefix)
# Copy each file
for src in all_src_paths:
if src.exists():
lib_path = parse_lib_path(src)
_copy_single(src, lib_path, verbose)
def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument(
"path",
type=pathlib.Path,
help="Source path containing /Lib/ (e.g., cpython/Lib/dataclasses.py)",
)
args = parser.parse_args(argv)
try:
copy_lib(args.path)
return 0
except ValueError as e:
print(f"Error: {e}", file=sys.stderr)
return 1
except FileNotFoundError as e:
print(f"Error: {e}", file=sys.stderr)
return 1
if __name__ == "__main__":
sys.exit(main())