diff --git a/tools/mip_lib.py b/tools/mip_lib.py new file mode 100644 index 000000000..dd7129a7f --- /dev/null +++ b/tools/mip_lib.py @@ -0,0 +1,71 @@ +"""MIP install modules from micropython-lib manifests into a local directory. + +Usage: + python3 tools/mip_lib.py [--mpy ] [--lib ] [--target ] ... + +Run from the micropython-lib checkout, for example: + python3 tools/mip_lib.py collections-abc typing + +Defaults: + --mpy: /tmp/micropython + --lib: parent of this script's tools directory + --target: /tmp/micropython-venv/lib + +Use the --mpy, --lib and --target CLI parameters to specify other locations. + +Manifests are executed as Python code, so use only trusted checkouts. Existing +destination files are overwritten, and a failed copy may leave a partial install. +""" + +import argparse +import sys +from pathlib import Path +from shutil import copy2 + +lib_default = Path(__file__).resolve().parent.parent +mpy_default = Path("/tmp/micropython") +target = Path("/tmp/micropython-venv/lib") + +parser = argparse.ArgumentParser(description="MIP from MicroPython-library.") +parser.add_argument("--mpy", type=Path, default=mpy_default, help="path to MicroPython repo") +parser.add_argument("--lib", type=Path, default=lib_default, help="path to MicroPython-lib repo") +parser.add_argument( + "--target", type=Path, default=target, help="path to target installation directory" +) +parser.add_argument("modules", nargs="+", help="module names from micropython-lib") +args = parser.parse_args() + +mpy_root = args.mpy.resolve() +lib_root = (args.lib if args.lib is not None else mpy_root / "lib/micropython-lib").resolve() +target = Path(args.target).resolve() + +if not (mpy_root / "tools" / "manifestfile.py").is_file(): + parser.error(f"MicroPython manifest tool not found in {mpy_root / 'tools'}; check --mpy") +if not lib_root.is_dir(): + parser.error(f"MicroPython-lib directory not found: {lib_root}; check --lib") + +sys.path.insert(0, str(mpy_root / "tools")) +import manifestfile # noqa: E402 + + +manifest = manifestfile.ManifestFile( + manifestfile.MODE_COMPILE, + {"MPY_LIB_DIR": str(lib_root)}, +) +manifest.metadata(description="Local source installation") +try: + for module in args.modules: + manifest.require(module) +except (manifestfile.ManifestFileError, ValueError, OSError) as exc: + sys.exit(f"Error resolving manifests: {exc}") + +for item in manifest.files(): + destination = target / item.target_path + try: + destination.parent.mkdir(parents=True, exist_ok=True) + copy2(item.full_path, destination) + except OSError as exc: + sys.exit( + f"Error copying {item.full_path} to {destination}: {exc}. Install may be partial." + ) + print(destination)