Skip to content

Commit d4044dc

Browse files
committed
added examples to each bundle; example bundle build. see bundle issue #3.
1 parent 68a736f commit d4044dc

File tree

2 files changed

+62
-32
lines changed

2 files changed

+62
-32
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: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -49,47 +49,54 @@ 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()
73-
print("Generating VERSIONS")
74-
if multiple_libs:
75-
with open(os.path.join(build_lib_dir, "VERSIONS.txt"), "w") as f:
76-
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)
78-
if versions.returncode != 0:
79-
print("Failed to generate versions file. Its likely a library hasn't been "
80-
"released yet.")
81-
success = False
82-
83-
repo = None
84-
for line in versions.stdout.split(b"\n"):
85-
if line.startswith(b"Entering") or not line:
86-
continue
87-
if line.startswith(b"git@"):
88-
repo = b"https://github.com/" + line.split(b":")[1][:-len(".git")]
89-
elif line.startswith(b"https:"):
90-
repo = line.strip()[:-len(".git")]
91-
else:
92-
f.write(repo.decode("utf-8", "strict") + "/releases/tag/" + line.strip().decode("utf-8", "strict") + "\r\n")
79+
if not example_bundle:
80+
print("Generating VERSIONS")
81+
if multiple_libs:
82+
with open(os.path.join(build_lib_dir, "VERSIONS.txt"), "w") as f:
83+
f.write(bundle_version + "\r\n")
84+
versions = subprocess.run('git submodule foreach \"git remote get-url origin && git describe --tags\"', shell=True, stdout=subprocess.PIPE)
85+
if versions.returncode != 0:
86+
print("Failed to generate versions file. Its likely a library hasn't been "
87+
"released yet.")
88+
success = False
89+
90+
repo = None
91+
for line in versions.stdout.split(b"\n"):
92+
if line.startswith(b"Entering") or not line:
93+
continue
94+
if line.startswith(b"git@"):
95+
repo = b"https://github.com/" + line.split(b":")[1][:-len(".git")]
96+
elif line.startswith(b"https:"):
97+
repo = line.strip()[:-len(".git")]
98+
else:
99+
f.write(repo.decode("utf-8", "strict") + "/releases/tag/" + line.strip().decode("utf-8", "strict") + "\r\n")
93100

94101
if not success:
95102
print("WARNING: some failures above")
@@ -103,7 +110,7 @@ def build_bundle(libs, bundle_version, output_filename,
103110
bundle.comment = json.dumps(build_metadata).encode("utf-8")
104111
if multiple_libs:
105112
total_size += add_file(bundle, "README.txt", "lib/README.txt")
106-
for root, dirs, files in os.walk(build_lib_dir):
113+
for root, dirs, files in os.walk(build_dir):
107114
ziproot = root[len(build_dir + "/"):].replace("-", "_")
108115
for filename in files:
109116
total_size += add_file(bundle, os.path.join(root, filename),
@@ -146,11 +153,14 @@ def build_bundles(filename_prefix, output_directory, library_location, library_d
146153
with open(build_tools_fn, "w") as f:
147154
f.write(build_tools_version)
148155

156+
# Build raw source .py bundle
149157
zip_filename = os.path.join(output_directory,
150158
filename_prefix + '-py-{VERSION}.zip'.format(
151159
VERSION=bundle_version))
152160
build_bundle(libs, bundle_version, zip_filename,
153161
build_tools_version=build_tools_version)
162+
163+
# Build .mpy bundle(s)
154164
os.makedirs("build_deps", exist_ok=True)
155165
for version in target_versions.VERSIONS:
156166
# Use prebuilt mpy-cross on Travis, otherwise build our own.
@@ -166,3 +176,10 @@ def build_bundles(filename_prefix, output_directory, library_location, library_d
166176
VERSION=bundle_version))
167177
build_bundle(libs, bundle_version, zip_filename, mpy_cross=mpy_cross,
168178
build_tools_version=build_tools_version)
179+
180+
# Build example bundle
181+
zip_filename = os.path.join(output_directory,
182+
filename_prefix + '-examples-{VERSION}.zip'.format(
183+
VERSION=bundle_version))
184+
build_bundle(libs, bundle_version, zip_filename,
185+
build_tools_version=build_tools_version, example_bundle=True)

0 commit comments

Comments
 (0)