Skip to content

Commit 318fa70

Browse files
authored
Merge pull request #50 from sommersoft/new_path_walk
Allow Multiple Package Folder Prefixes; Improve File Gathering
2 parents ffcbdc5 + b803b26 commit 318fa70

File tree

2 files changed

+48
-61
lines changed

2 files changed

+48
-61
lines changed

circuitpython_build_tools/build.py

Lines changed: 46 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# The MIT License (MIT)
44
#
55
# Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
6+
# 2018, 2019 Michael Schroeder
67
#
78
# Permission is hereby granted, free of charge, to any person obtaining a copy
89
# of this software and associated documentation files (the "Software"), to deal
@@ -24,13 +25,15 @@
2425

2526
import os
2627
import os.path
28+
import pathlib
2729
import semver
2830
import shutil
2931
import sys
3032
import subprocess
3133
import tempfile
3234

3335
IGNORE_PY = ["setup.py", "conf.py", "__init__.py"]
36+
GLOB_PATTERNS = ["*.py", "font5x8.bin"]
3437

3538
def version_string(path=None, *, valid_semver=False):
3639
version = None
@@ -106,71 +109,46 @@ def library(library_path, output_directory, package_folder_prefix,
106109
package_files = []
107110
example_files = []
108111
total_size = 512
109-
for filename in os.listdir(library_path):
110-
full_path = os.path.join(library_path, filename)
111-
if os.path.isdir(full_path):
112-
path_walk = [names for names in os.walk(full_path)]
113-
#print("- '{}' walk: {}".format(filename, path_walk))
114-
115-
# iterate through path_walk, appending each file to
116-
# 'walked_files' while retaining subdirectory structure
117-
walked_files = []
118-
for path in path_walk:
119-
rel_path = ""
120-
if filename.startswith("examples"):
121-
path_tail_idx = path[0].rfind("examples/")
122-
if path_tail_idx != -1:
123-
rel_path = "{}/".format(path[0][path_tail_idx + 9:])
124112

113+
lib_path = pathlib.Path(library_path)
114+
parent_idx = len(lib_path.parts)
115+
glob_search = []
116+
for pattern in GLOB_PATTERNS:
117+
glob_search.extend(list(lib_path.rglob(pattern)))
118+
119+
for file in glob_search:
120+
if file.parts[parent_idx] == "examples":
121+
example_files.append(file)
122+
else:
123+
if not example_bundle:
124+
is_package = False
125+
for prefix in package_folder_prefix:
126+
if file.parts[parent_idx].startswith(prefix):
127+
is_package = True
128+
129+
if is_package:
130+
package_files.append(file)
125131
else:
126-
path_tail_idx = (path[0].rfind("{}/".format(filename))
127-
+ (len(filename) +1))
128-
path_tail = path[0][path_tail_idx:]
129-
130-
# if this entry is the package top dir, keep it
131-
# empty so we don't double append the dir name
132-
if filename not in path_tail:
133-
rel_path = "{}/".format(path_tail)
134-
135-
for path_files in path[2]:
136-
walked_files.append("{}{}".format(rel_path, path_files))
137-
#print(" - expanded file walk: {}".format(walked_files))
138-
139-
files = filter(
140-
lambda x: x.endswith(".py") or x.startswith("font5x8.bin"),
141-
walked_files
142-
)
143-
files = map(lambda x: os.path.join(filename, x), files)
144-
145-
if filename.startswith("examples"):
146-
example_files.extend(files)
147-
#print("- example files: {}".format(example_files))
148-
else:
149-
if (not example_bundle and
150-
not filename.startswith(package_folder_prefix)):
151-
print("skipped path: {}".format(full_path))
132+
if file.name in IGNORE_PY:
133+
#print("Ignoring:", file.resolve())
152134
continue
153-
if not example_bundle:
154-
package_files.extend(files)
155-
#print("- package files: {} | {}".format(filename, package_files))
156-
157-
if (filename.endswith(".py") and
158-
filename not in IGNORE_PY and
159-
not example_bundle):
160-
py_files.append(filename)
135+
if file.parent == lib_path:
136+
py_files.append(file)
161137

162138
if len(py_files) > 1:
163-
raise ValueError("Multiple top level py files not allowed. Please put them in a package "
164-
"or combine them into a single file.")
139+
raise ValueError("Multiple top level py files not allowed. Please put "
140+
"them in a package or combine them into a single file.")
165141

166142
for fn in example_files:
167-
base_dir = os.path.join(output_directory.replace("/lib", "/"), os.path.dirname(fn))
143+
base_dir = os.path.join(output_directory.replace("/lib", "/"),
144+
fn.relative_to(library_path).parent)
168145
if not os.path.isdir(base_dir):
169146
os.makedirs(base_dir)
170147
total_size += 512
171148

172149
for fn in package_files:
173-
base_dir = os.path.join(output_directory, os.path.dirname(fn))
150+
base_dir = os.path.join(output_directory,
151+
fn.relative_to(library_path).parent)
174152
if not os.path.isdir(base_dir):
175153
os.makedirs(base_dir)
176154
total_size += 512
@@ -188,15 +166,17 @@ def library(library_path, output_directory, package_folder_prefix,
188166

189167
for filename in py_files:
190168
full_path = os.path.join(library_path, filename)
191-
output_file = os.path.join(output_directory,
192-
filename.replace(".py", new_extension))
169+
output_file = os.path.join(
170+
output_directory,
171+
filename.relative_to(library_path).with_suffix(new_extension)
172+
)
193173
with tempfile.NamedTemporaryFile() as temp_file:
194174
_munge_to_temp(full_path, temp_file, library_version)
195175

196176
if mpy_cross:
197177
mpy_success = subprocess.call([mpy_cross,
198178
"-o", output_file,
199-
"-s", filename,
179+
"-s", str(filename),
200180
temp_file.name])
201181
if mpy_success != 0:
202182
raise RuntimeError("mpy-cross failed on", full_path)
@@ -208,21 +188,26 @@ def library(library_path, output_directory, package_folder_prefix,
208188
with tempfile.NamedTemporaryFile() as temp_file:
209189
_munge_to_temp(full_path, temp_file, library_version)
210190
if not mpy_cross or os.stat(full_path).st_size == 0:
211-
output_file = os.path.join(output_directory, filename)
191+
output_file = os.path.join(output_directory,
192+
filename.relative_to(library_path))
212193
shutil.copyfile(temp_file.name, output_file)
213194
else:
214-
output_file = os.path.join(output_directory,
215-
filename.replace(".py", new_extension))
195+
output_file = os.path.join(
196+
output_directory,
197+
filename.relative_to(library_path).with_suffix(new_extension)
198+
)
199+
216200
mpy_success = subprocess.call([mpy_cross,
217201
"-o", output_file,
218-
"-s", filename,
202+
"-s", str(filename),
219203
temp_file.name])
220204
if mpy_success != 0:
221205
raise RuntimeError("mpy-cross failed on", full_path)
222206

223207
for filename in example_files:
224208
full_path = os.path.join(library_path, filename)
225-
output_file = os.path.join(output_directory.replace("/lib", "/"), filename)
209+
output_file = os.path.join(output_directory.replace("/lib", "/"),
210+
filename.relative_to(library_path))
226211
with tempfile.NamedTemporaryFile() as temp_file:
227212
_munge_to_temp(full_path, temp_file, library_version)
228213
shutil.copyfile(temp_file.name, output_file)

circuitpython_build_tools/scripts/build_bundles.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ def _find_libraries(current_path, depth):
139139
def build_bundles(filename_prefix, output_directory, library_location, library_depth, package_folder_prefix):
140140
os.makedirs(output_directory, exist_ok=True)
141141

142+
package_folder_prefix = package_folder_prefix.split(", ")
143+
142144
bundle_version = build.version_string()
143145

144146
libs = _find_libraries(os.path.abspath(library_location), library_depth)

0 commit comments

Comments
 (0)