Skip to content

Commit 8a326f3

Browse files
authored
Merge pull request #18 from sommersoft/examples
Add Examples To Bundles
2 parents 68a736f + ff34fa3 commit 8a326f3

File tree

2 files changed

+44
-15
lines changed

2 files changed

+44
-15
lines changed

circuitpython_build_tools/build.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,25 @@ def _munge_to_temp(original_path, temp_file, library_version):
9292
temp_file.write(line.encode("utf-8") + b"\r\n")
9393
temp_file.flush()
9494

95-
def library(library_path, output_directory, mpy_cross=None):
95+
def library(library_path, output_directory, mpy_cross=None, example_bundle=False):
9696
py_files = []
9797
package_files = []
98+
example_files = []
9899
total_size = 512
99100
for filename in os.listdir(library_path):
100101
full_path = os.path.join(library_path, filename)
101-
if os.path.isdir(full_path) and filename not in ["examples", "docs"]:
102+
if os.path.isdir(full_path) and filename not in ["docs"]:
102103
files = os.listdir(full_path)
103104
files = filter(lambda x: x.endswith(".py"), files)
104105
files = map(lambda x: os.path.join(filename, x), files)
105-
package_files.extend(files)
106-
if filename.endswith(".py") and filename not in IGNORE_PY:
106+
if filename.startswith("examples"):
107+
example_files.extend(files)
108+
else:
109+
if not example_bundle:
110+
package_files.extend(files)
111+
if (filename.endswith(".py") and
112+
filename not in IGNORE_PY and
113+
not example_bundle):
107114
py_files.append(filename)
108115

109116
if len(py_files) > 1:
@@ -116,7 +123,6 @@ def library(library_path, output_directory, mpy_cross=None):
116123
os.makedirs(base_dir)
117124
total_size += 512
118125

119-
120126
new_extension = ".py"
121127
if mpy_cross:
122128
new_extension = ".mpy"
@@ -163,3 +169,10 @@ def library(library_path, output_directory, mpy_cross=None):
163169
temp_file.name])
164170
if mpy_success != 0:
165171
raise RuntimeError("mpy-cross failed on", full_path)
172+
173+
for filename in example_files:
174+
full_path = os.path.join(library_path, filename)
175+
output_file = os.path.join(output_directory.replace("/lib", "/"), filename)
176+
with tempfile.NamedTemporaryFile() as temp_file:
177+
_munge_to_temp(full_path, temp_file, library_version)
178+
shutil.copyfile(temp_file.name, output_file)

circuitpython_build_tools/scripts/build_bundles.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,37 +49,43 @@ def add_file(bundle, src_file, zip_name):
4949

5050

5151
def build_bundle(libs, bundle_version, output_filename,
52-
build_tools_version="devel", mpy_cross=None):
52+
build_tools_version="devel", mpy_cross=None, example_bundle=False):
5353
build_dir = "build-" + os.path.basename(output_filename)
54-
build_lib_dir = os.path.join(build_dir, "lib")
54+
build_lib_dir = os.path.join(build_dir, build_dir.replace(".zip", ""), "lib")
55+
build_example_dir = os.path.join(build_dir, build_dir.replace(".zip", ""), "examples")
5556
if os.path.isdir(build_dir):
5657
print("Deleting existing build.")
5758
shutil.rmtree(build_dir)
58-
os.makedirs(build_lib_dir)
59+
total_size = 0
60+
if not example_bundle:
61+
os.makedirs(build_lib_dir)
62+
total_size += 512
63+
os.makedirs(build_example_dir)
64+
total_size += 512
5965

6066
multiple_libs = len(libs) > 1
6167

6268
success = True
63-
total_size = 512
6469
for library_path in libs:
6570
try:
66-
build.library(library_path, build_lib_dir, mpy_cross=mpy_cross)
71+
build.library(library_path, build_lib_dir, mpy_cross=mpy_cross,
72+
example_bundle=example_bundle)
6773
except ValueError as e:
68-
print(library_path)
74+
print("build.library failure:", library_path)
6975
print(e)
7076
success = False
7177

7278
print()
7379
print("Generating VERSIONS")
7480
if multiple_libs:
75-
with open(os.path.join(build_lib_dir, "VERSIONS.txt"), "w") as f:
81+
with open(os.path.join(build_dir, build_dir.replace(".zip", ""), "VERSIONS.txt"), "w") as f:
7682
f.write(bundle_version + "\r\n")
77-
versions = subprocess.run('git submodule foreach \"git remote get-url origin && git describe --tags\"', shell=True, stdout=subprocess.PIPE)
83+
versions = subprocess.run('git submodule foreach \"git remote get-url origin && git describe --tags\"', shell=True, stdout=subprocess.PIPE, cwd=os.path.commonpath(libs))
7884
if versions.returncode != 0:
7985
print("Failed to generate versions file. Its likely a library hasn't been "
8086
"released yet.")
8187
success = False
82-
88+
8389
repo = None
8490
for line in versions.stdout.split(b"\n"):
8591
if line.startswith(b"Entering") or not line:
@@ -103,7 +109,7 @@ def build_bundle(libs, bundle_version, output_filename,
103109
bundle.comment = json.dumps(build_metadata).encode("utf-8")
104110
if multiple_libs:
105111
total_size += add_file(bundle, "README.txt", "lib/README.txt")
106-
for root, dirs, files in os.walk(build_lib_dir):
112+
for root, dirs, files in os.walk(build_dir):
107113
ziproot = root[len(build_dir + "/"):].replace("-", "_")
108114
for filename in files:
109115
total_size += add_file(bundle, os.path.join(root, filename),
@@ -146,11 +152,14 @@ def build_bundles(filename_prefix, output_directory, library_location, library_d
146152
with open(build_tools_fn, "w") as f:
147153
f.write(build_tools_version)
148154

155+
# Build raw source .py bundle
149156
zip_filename = os.path.join(output_directory,
150157
filename_prefix + '-py-{VERSION}.zip'.format(
151158
VERSION=bundle_version))
152159
build_bundle(libs, bundle_version, zip_filename,
153160
build_tools_version=build_tools_version)
161+
162+
# Build .mpy bundle(s)
154163
os.makedirs("build_deps", exist_ok=True)
155164
for version in target_versions.VERSIONS:
156165
# Use prebuilt mpy-cross on Travis, otherwise build our own.
@@ -166,3 +175,10 @@ def build_bundles(filename_prefix, output_directory, library_location, library_d
166175
VERSION=bundle_version))
167176
build_bundle(libs, bundle_version, zip_filename, mpy_cross=mpy_cross,
168177
build_tools_version=build_tools_version)
178+
179+
# Build example bundle
180+
zip_filename = os.path.join(output_directory,
181+
filename_prefix + '-examples-{VERSION}.zip'.format(
182+
VERSION=bundle_version))
183+
build_bundle(libs, bundle_version, zip_filename,
184+
build_tools_version=build_tools_version, example_bundle=True)

0 commit comments

Comments
 (0)