From 85a47fc9c15a8297b77157689bab213895d10198 Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Tue, 23 Mar 2021 11:53:31 -0700 Subject: [PATCH] Added JSON generator for bundle --- .gitignore | 1 + circuitpython_build_tools/build.py | 35 +++++++++++ .../scripts/build_bundles.py | 63 +++++++++++++++++++ 3 files changed, 99 insertions(+) diff --git a/.gitignore b/.gitignore index 2096062..10deb2a 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ circuitpython_build_tools/data/ .eggs version.py .env/* +.DS_Store diff --git a/circuitpython_build_tools/build.py b/circuitpython_build_tools/build.py index 41a7ef1..4379786 100644 --- a/circuitpython_build_tools/build.py +++ b/circuitpython_build_tools/build.py @@ -103,6 +103,41 @@ def _munge_to_temp(original_path, temp_file, library_version): temp_file.write(line.encode("utf-8") + b"\r\n") temp_file.flush() +def get_package_info(library_path, package_folder_prefix): + lib_path = pathlib.Path(library_path) + parent_idx = len(lib_path.parts) + py_files = [] + package_files = [] + package_info = {} + glob_search = [] + for pattern in GLOB_PATTERNS: + glob_search.extend(list(lib_path.rglob(pattern))) + + for file in glob_search: + if file.parts[parent_idx] != "examples": + package_info["is_package"] = False + if len(file.parts) > parent_idx + 1: + for prefix in package_folder_prefix: + if file.parts[parent_idx].startswith(prefix): + package_info["is_package"] = True + if package_info["is_package"]: + package_files.append(file) + else: + if file.name in IGNORE_PY: + #print("Ignoring:", file.resolve()) + continue + if file.parent == lib_path: + py_files.append(file) + + if package_files: + package_info["module_name"] = package_files[0].relative_to(library_path).parent.name + elif py_files: + package_info["module_name"] = py_files[0].relative_to(library_path).name[:-3] + else: + package_info["module_name"] = None + + return package_info + def library(library_path, output_directory, package_folder_prefix, mpy_cross=None, example_bundle=False): py_files = [] diff --git a/circuitpython_build_tools/scripts/build_bundles.py b/circuitpython_build_tools/scripts/build_bundles.py index be3ba8e..3368ab6 100755 --- a/circuitpython_build_tools/scripts/build_bundles.py +++ b/circuitpython_build_tools/scripts/build_bundles.py @@ -25,6 +25,7 @@ import json import os import os.path +import re import shlex import shutil import subprocess @@ -38,6 +39,13 @@ import pkg_resources +NOT_BUNDLE_LIBRARIES = [ + "adafruit-blinka", + "adafruit-blinka-bleio", + "adafruit-blinka-displayio", + "pyserial", +] + def add_file(bundle, src_file, zip_name): bundle.write(src_file, zip_name) file_size = os.stat(src_file).st_size @@ -47,6 +55,55 @@ def add_file(bundle, src_file, zip_name): print(zip_name, file_size, file_sector_size) return file_sector_size +def get_module_name(library_path): + """Figure out the module or package name anbd return it""" + url = subprocess.run('git remote get-url origin', shell=True, stdout=subprocess.PIPE, cwd=library_path) + url = url.stdout.decode("utf-8", errors="ignore").strip().lower() + module_name = url[:-4].split("/")[-1].replace("_", "-") + return module_name + +def get_bundle_requirements(directory): + """ + Open the requirements.txt if it exists + Remove anything that shouldn't be a requirement like Adafruit_Blinka + Return the list + """ + + libraries = [] + path = directory + "/requirements.txt" + if os.path.exists(path): + with open(path, "r") as file: + requirements = file.read() + file.close() + for line in requirements.split("\n"): + line = line.lower().strip() + if line.startswith("#") or line == "": + # skip comments + pass + else: + if any(operators in line for operators in [">", "<", "="]): + # Remove everything after any pip style version specifiers + line = re.split("[<|>|=|]", line)[0] + if line not in libraries and line not in NOT_BUNDLE_LIBRARIES: + libraries.append(line) + return libraries + +def build_bundle_json(libs, bundle_version, output_filename, package_folder_prefix): + """ + Generate a JSON file of all the libraries in libs + """ + library_submodules = {} + for library_path in libs: + library = {} + package_info = build.get_package_info(library_path, package_folder_prefix) + module_name = get_module_name(library_path) + library["package"] = package_info["is_package"] + library["path"] = "lib/" + package_info["module_name"] + library["dependencies"] = get_bundle_requirements(library_path) + library_submodules[module_name] = library + out_file = open(output_filename, "w") + json.dump(library_submodules, out_file) + out_file.close() def build_bundle(libs, bundle_version, output_filename, package_folder_prefix, build_tools_version="devel", mpy_cross=None, example_bundle=False): @@ -188,3 +245,9 @@ def build_bundles(filename_prefix, output_directory, library_location, library_d VERSION=bundle_version)) build_bundle(libs, bundle_version, zip_filename, package_folder_prefix, build_tools_version=build_tools_version, example_bundle=True) + + # Build Bundle JSON + json_filename = os.path.join(output_directory, + filename_prefix + '-{VERSION}.json'.format( + VERSION=bundle_version)) + build_bundle_json(libs, bundle_version, json_filename, package_folder_prefix)