Skip to content

Added JSON generator for bundle #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ circuitpython_build_tools/data/
.eggs
version.py
.env/*
.DS_Store
35 changes: 35 additions & 0 deletions circuitpython_build_tools/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
63 changes: 63 additions & 0 deletions circuitpython_build_tools/scripts/build_bundles.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import json
import os
import os.path
import re
import shlex
import shutil
import subprocess
Expand All @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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)