kumquat-buildroot/package/micropython/collect_micropython_lib.py
Abilio Marques 8f403f0137 package/micropython-lib: merge with, and install as part of micropython
Until now, micropython-lib was a package that installed v1.9.3, which is
more than 6 years old. This was acceptable since micropython never made
any other official release of the library until v1.20.

Meanwhile, the libraries underwent a reorganization, and they are now
available in a directory structure that cannot be copied directly into
the target. This might explain why v1.9.3 is still present in the
current day buildroot (which comes with micropython v1.22).

As part of the changes made by the micropython project, the libraries
are now released together with the interpreter. They are cloned as a
submodule into the lib/micropython-lib directory, and are present in the
release tarball.

This commit introduces an auxiliary script to collect those libraries
and reorder them into a structure that can then be copied into
/usr/lib/micropython. The script utilizes a module from the tools
directory of the micropython repo.

The helper script is kept as simple as possible, and makes use of
existing micropython tools (used to process manifests) to discover the
list of packages available in micropython-lib. The hope is that by
relying on them, any future changes in directory structure will be
covered by the official "manifestfile.py" tool.

It is to be noted that, even though the manifestfile.py script/module is
part of the micropython package, it is actually written for CPython, and
is not expected to even work when using micropython as an interpreter.
This we do not need to introduce host-micropython to use that tool, and
microython already depends on host-python3 for other parts of the build.

With this commit, micropython-lib is installed (optionally) as part
of micropython, and thus a separate package is no longer needed. The
original config variable name was retained as it fits with the
micropython package "namespace", and thus this is backward compatible
and no legacy handling is needed.

This commit also ensures that the libraries in micropython-lib will
be updated together with newer versions of micropython in the future.

Signed-off-by: Abilio Marques <abiliojr@gmail.com>
[yann.morin.1998@free.fr:
  - use if-block in Config.in
  - simplify PYTHONPATH
  - fix check-package
  - reword and reorder parts of the commit log
]
Signed-off-by: Yann E. MORIN <yann.morin.1998@free.fr>
2024-01-29 21:38:53 +01:00

80 lines
2.3 KiB
Python
Executable File

#!/usr/bin/env python3
"""
Sometime after v1.9.3, micropython-lib's directory structure was cleaned up and
enhanced with manifest files.
Though cleaner, it means it cannot be directly copied into the target.
This script is during build process to perform that conversion.
It makes use of manifestfile.py, which is normally located in micropython's
tool directory.
It also depends on the micropython-lib that is cloned in lib/micropython-lib
during build.
"""
import argparse
import manifestfile
import os
import shutil
def get_library_name_type(manifest_path: str) -> tuple[str, bool]:
split = manifest_path.split("/")
return (split[-2], "unix-ffi" in split) # -1: "manifest.py", -2: library name
def get_all_libraries(mpy_lib_dir: str):
# reuse manifestfile module capabilities to scan the micropython-lib directory
collected_list = manifestfile.ManifestFile(
manifestfile.MODE_FREEZE, {"MPY_LIB_DIR": mpy_lib_dir}
)
collected_list.freeze(mpy_lib_dir)
for file in collected_list.files():
if file.target_path.endswith("manifest.py"):
yield get_library_name_type(file.full_path)
def copy_file(src: str, target: str, destination: str):
s = target.split("/")
s.pop()
d = f"{destination}/{'/'.join(s)}"
os.makedirs(d, exist_ok=True)
shutil.copy(src, f"{destination}/{target}")
def copy_libraries(manifest, destination: str):
for f in manifest.files():
copy_file(f.full_path, f.target_path, destination)
def process_cmdline_args():
parser = argparse.ArgumentParser(
description="Prepare micropython-lib to be installed"
)
parser.add_argument("micropython", help="Path to micropython source directory")
parser.add_argument("destination", help="Destination directory")
args = parser.parse_args()
return os.path.abspath(args.micropython), os.path.abspath(args.destination)
def main():
micropython_dir, destination_dir = process_cmdline_args()
mpy_lib_dir = f"{micropython_dir}/lib/micropython-lib"
manifest = manifestfile.ManifestFile(
manifestfile.MODE_FREEZE, {"MPY_LIB_DIR": mpy_lib_dir}
)
for library, is_ffi in get_all_libraries(mpy_lib_dir):
manifest.require(library, unix_ffi=is_ffi)
copy_libraries(manifest, destination_dir)
main()