Skip to content

Commit 9d9d4ef

Browse files
authored
Merge pull request #63 from makermelissa/master
Added JSON generator for bundle
2 parents 91a6313 + 85a47fc commit 9d9d4ef

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ circuitpython_build_tools/data/
88
.eggs
99
version.py
1010
.env/*
11+
.DS_Store

circuitpython_build_tools/build.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,41 @@ def _munge_to_temp(original_path, temp_file, library_version):
103103
temp_file.write(line.encode("utf-8") + b"\r\n")
104104
temp_file.flush()
105105

106+
def get_package_info(library_path, package_folder_prefix):
107+
lib_path = pathlib.Path(library_path)
108+
parent_idx = len(lib_path.parts)
109+
py_files = []
110+
package_files = []
111+
package_info = {}
112+
glob_search = []
113+
for pattern in GLOB_PATTERNS:
114+
glob_search.extend(list(lib_path.rglob(pattern)))
115+
116+
for file in glob_search:
117+
if file.parts[parent_idx] != "examples":
118+
package_info["is_package"] = False
119+
if len(file.parts) > parent_idx + 1:
120+
for prefix in package_folder_prefix:
121+
if file.parts[parent_idx].startswith(prefix):
122+
package_info["is_package"] = True
123+
if package_info["is_package"]:
124+
package_files.append(file)
125+
else:
126+
if file.name in IGNORE_PY:
127+
#print("Ignoring:", file.resolve())
128+
continue
129+
if file.parent == lib_path:
130+
py_files.append(file)
131+
132+
if package_files:
133+
package_info["module_name"] = package_files[0].relative_to(library_path).parent.name
134+
elif py_files:
135+
package_info["module_name"] = py_files[0].relative_to(library_path).name[:-3]
136+
else:
137+
package_info["module_name"] = None
138+
139+
return package_info
140+
106141
def library(library_path, output_directory, package_folder_prefix,
107142
mpy_cross=None, example_bundle=False):
108143
py_files = []

circuitpython_build_tools/scripts/build_bundles.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import json
2626
import os
2727
import os.path
28+
import re
2829
import shlex
2930
import shutil
3031
import subprocess
@@ -38,6 +39,13 @@
3839

3940
import pkg_resources
4041

42+
NOT_BUNDLE_LIBRARIES = [
43+
"adafruit-blinka",
44+
"adafruit-blinka-bleio",
45+
"adafruit-blinka-displayio",
46+
"pyserial",
47+
]
48+
4149
def add_file(bundle, src_file, zip_name):
4250
bundle.write(src_file, zip_name)
4351
file_size = os.stat(src_file).st_size
@@ -47,6 +55,55 @@ def add_file(bundle, src_file, zip_name):
4755
print(zip_name, file_size, file_sector_size)
4856
return file_sector_size
4957

58+
def get_module_name(library_path):
59+
"""Figure out the module or package name anbd return it"""
60+
url = subprocess.run('git remote get-url origin', shell=True, stdout=subprocess.PIPE, cwd=library_path)
61+
url = url.stdout.decode("utf-8", errors="ignore").strip().lower()
62+
module_name = url[:-4].split("/")[-1].replace("_", "-")
63+
return module_name
64+
65+
def get_bundle_requirements(directory):
66+
"""
67+
Open the requirements.txt if it exists
68+
Remove anything that shouldn't be a requirement like Adafruit_Blinka
69+
Return the list
70+
"""
71+
72+
libraries = []
73+
path = directory + "/requirements.txt"
74+
if os.path.exists(path):
75+
with open(path, "r") as file:
76+
requirements = file.read()
77+
file.close()
78+
for line in requirements.split("\n"):
79+
line = line.lower().strip()
80+
if line.startswith("#") or line == "":
81+
# skip comments
82+
pass
83+
else:
84+
if any(operators in line for operators in [">", "<", "="]):
85+
# Remove everything after any pip style version specifiers
86+
line = re.split("[<|>|=|]", line)[0]
87+
if line not in libraries and line not in NOT_BUNDLE_LIBRARIES:
88+
libraries.append(line)
89+
return libraries
90+
91+
def build_bundle_json(libs, bundle_version, output_filename, package_folder_prefix):
92+
"""
93+
Generate a JSON file of all the libraries in libs
94+
"""
95+
library_submodules = {}
96+
for library_path in libs:
97+
library = {}
98+
package_info = build.get_package_info(library_path, package_folder_prefix)
99+
module_name = get_module_name(library_path)
100+
library["package"] = package_info["is_package"]
101+
library["path"] = "lib/" + package_info["module_name"]
102+
library["dependencies"] = get_bundle_requirements(library_path)
103+
library_submodules[module_name] = library
104+
out_file = open(output_filename, "w")
105+
json.dump(library_submodules, out_file)
106+
out_file.close()
50107

51108
def build_bundle(libs, bundle_version, output_filename, package_folder_prefix,
52109
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
188245
VERSION=bundle_version))
189246
build_bundle(libs, bundle_version, zip_filename, package_folder_prefix,
190247
build_tools_version=build_tools_version, example_bundle=True)
248+
249+
# Build Bundle JSON
250+
json_filename = os.path.join(output_directory,
251+
filename_prefix + '-{VERSION}.json'.format(
252+
VERSION=bundle_version))
253+
build_bundle_json(libs, bundle_version, json_filename, package_folder_prefix)

0 commit comments

Comments
 (0)