Skip to content

Commit fbb43bd

Browse files
authored
Merge pull request #40 from sommersoft/fix_arg_round_2
Attempt 2: Include mpy-cross '__init__.py' In Packages; Restructure Package Bundling
2 parents 63c6be1 + 7f8af94 commit fbb43bd

File tree

3 files changed

+50
-17
lines changed

3 files changed

+50
-17
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ locally like so:
6464
cd circuitpython-build-tools # this will be specific to your storage location
6565
python3 -m venv .env
6666
source .env/bin/activate
67-
python3 -u -m circuitpython_build_tools.scripts.build_mpy_cross circuitpython_build_tools/data/
6867
pip install -e . # '-e' is pip's "development" install feature
6968
circuitpython-build-bundles --filename_prefix <output file prefix> --library_location <library location>
7069
```

circuitpython_build_tools/build.py

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,31 +100,66 @@ def _munge_to_temp(original_path, temp_file, library_version):
100100
temp_file.write(line.encode("utf-8") + b"\r\n")
101101
temp_file.flush()
102102

103-
def library(library_path, output_directory, mpy_cross=None, example_bundle=False):
103+
def library(library_path, output_directory, package_folder_prefix, mpy_cross=None, example_bundle=False):
104104
py_files = []
105105
package_files = []
106106
example_files = []
107107
total_size = 512
108108
for filename in os.listdir(library_path):
109109
full_path = os.path.join(library_path, filename)
110-
if os.path.isdir(full_path) and filename not in ["docs"]:
111-
files = os.listdir(full_path)
112-
files = filter(lambda x: x.endswith(".py") or x.startswith("font5x8.bin"), files)
110+
if os.path.isdir(full_path):
111+
path_walk = [names for names in os.walk(full_path)]
112+
#print("- '{}' walk: {}".format(filename, path_walk))
113+
114+
# iterate through path_walk, appending each file to
115+
# 'walked_files' while retaining subdirectory structure
116+
walked_files = []
117+
for path in path_walk:
118+
if filename.startswith("examples"):
119+
path_tail_idx = path[0].rfind("examples/") + 9
120+
else:
121+
path_tail_idx = path[0].rfind("/") + 1
122+
path_tail = path[0][path_tail_idx:]
123+
rel_path = ""
124+
# if this entry is the package top dir, keep it
125+
# empty so we don't double append the dir name
126+
if filename not in path_tail:
127+
rel_path = "{}/".format(path_tail)
128+
129+
for path_files in path[2]:
130+
walked_files.append("{}{}".format(rel_path, path_files))
131+
#print(" - expanded file walk: {}".format(walked_files))
132+
133+
files = filter(lambda x: x.endswith(".py") or x.startswith("font5x8.bin"), walked_files)
113134
files = map(lambda x: os.path.join(filename, x), files)
135+
114136
if filename.startswith("examples"):
115137
example_files.extend(files)
138+
#print("- example files: {}".format(example_files))
116139
else:
140+
if (not example_bundle and
141+
not filename.startswith(package_folder_prefix)):
142+
#print("skipped path: {}".format(full_path))
143+
continue
117144
if not example_bundle:
118145
package_files.extend(files)
146+
#print("- package files: {} | {}".format(filename, package_files))
147+
119148
if (filename.endswith(".py") and
120149
filename not in IGNORE_PY and
121150
not example_bundle):
122-
py_files.append(filename)
151+
py_files.append(filename)
123152

124153
if len(py_files) > 1:
125154
raise ValueError("Multiple top level py files not allowed. Please put them in a package "
126155
"or combine them into a single file.")
127156

157+
for fn in example_files:
158+
base_dir = os.path.join(output_directory.replace("/lib", "/"), os.path.dirname(fn))
159+
if not os.path.isdir(base_dir):
160+
os.makedirs(base_dir)
161+
total_size += 512
162+
128163
for fn in package_files:
129164
base_dir = os.path.join(output_directory, os.path.dirname(fn))
130165
if not os.path.isdir(base_dir):
@@ -163,9 +198,7 @@ def library(library_path, output_directory, mpy_cross=None, example_bundle=False
163198
full_path = os.path.join(library_path, filename)
164199
with tempfile.NamedTemporaryFile() as temp_file:
165200
_munge_to_temp(full_path, temp_file, library_version)
166-
if (not mpy_cross or
167-
os.stat(full_path).st_size == 0 or
168-
filename.endswith("__init__.py")):
201+
if not mpy_cross or os.stat(full_path).st_size == 0:
169202
output_file = os.path.join(output_directory, filename)
170203
shutil.copyfile(temp_file.name, output_file)
171204
else:

circuitpython_build_tools/scripts/build_bundles.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def add_file(bundle, src_file, zip_name):
4848
return file_sector_size
4949

5050

51-
def build_bundle(libs, bundle_version, output_filename,
51+
def build_bundle(libs, bundle_version, output_filename, package_folder_prefix,
5252
build_tools_version="devel", mpy_cross=None, example_bundle=False):
5353
build_dir = "build-" + os.path.basename(output_filename)
5454
top_folder = os.path.basename(output_filename).replace(".zip", "")
@@ -69,8 +69,8 @@ def build_bundle(libs, bundle_version, output_filename,
6969
success = True
7070
for library_path in libs:
7171
try:
72-
build.library(library_path, build_lib_dir, mpy_cross=mpy_cross,
73-
example_bundle=example_bundle)
72+
build.library(library_path, build_lib_dir, package_folder_prefix,
73+
mpy_cross=mpy_cross, example_bundle=example_bundle)
7474
except ValueError as e:
7575
print("build.library failure:", library_path)
7676
print(e)
@@ -135,7 +135,8 @@ def _find_libraries(current_path, depth):
135135
@click.option('--output_directory', default="bundles", help="Output location for the zip files.")
136136
@click.option('--library_location', required=True, help="Location of libraries to bundle.")
137137
@click.option('--library_depth', default=0, help="Depth of library folders. This is useful when multiple libraries are bundled together but are initially in separate subfolders.")
138-
def build_bundles(filename_prefix, output_directory, library_location, library_depth):
138+
@click.option('--package_folder_prefix', default="adafruit_", help="Prefix string used to determine package folders to bundle.")
139+
def build_bundles(filename_prefix, output_directory, library_location, library_depth, package_folder_prefix):
139140
os.makedirs(output_directory, exist_ok=True)
140141

141142
bundle_version = build.version_string()
@@ -157,7 +158,7 @@ def build_bundles(filename_prefix, output_directory, library_location, library_d
157158
zip_filename = os.path.join(output_directory,
158159
filename_prefix + '-py-{VERSION}.zip'.format(
159160
VERSION=bundle_version))
160-
build_bundle(libs, bundle_version, zip_filename,
161+
build_bundle(libs, bundle_version, zip_filename, package_folder_prefix,
161162
build_tools_version=build_tools_version)
162163

163164
# Build .mpy bundle(s)
@@ -174,12 +175,12 @@ def build_bundles(filename_prefix, output_directory, library_location, library_d
174175
filename_prefix + '-{TAG}-mpy-{VERSION}.zip'.format(
175176
TAG=version["name"],
176177
VERSION=bundle_version))
177-
build_bundle(libs, bundle_version, zip_filename, mpy_cross=mpy_cross,
178-
build_tools_version=build_tools_version)
178+
build_bundle(libs, bundle_version, zip_filename, package_folder_prefix,
179+
mpy_cross=mpy_cross, build_tools_version=build_tools_version)
179180

180181
# Build example bundle
181182
zip_filename = os.path.join(output_directory,
182183
filename_prefix + '-examples-{VERSION}.zip'.format(
183184
VERSION=bundle_version))
184-
build_bundle(libs, bundle_version, zip_filename,
185+
build_bundle(libs, bundle_version, zip_filename, package_folder_prefix,
185186
build_tools_version=build_tools_version, example_bundle=True)

0 commit comments

Comments
 (0)