Skip to content

Commit 2d53879

Browse files
committed
solution for #75
1 parent 7da9c1d commit 2d53879

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

circuitpython_build_tools/build.py

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import os
2828
import os.path
29+
import platform
2930
import pathlib
3031
import requests
3132
import semver
@@ -70,14 +71,18 @@ def mpy_cross(mpy_cross_filename, circuitpython_tag, quiet=False):
7071
return
7172

7273
# Try to pull from S3
73-
uname = os.uname()
74+
uname = platform.uname()
7475
s3_url = None
75-
if uname[0] == 'Linux' and uname[4] in ('amd64', 'x86_64'):
76+
if uname[0].title() == 'Linux' and uname[4].lower() in ('amd64', 'x86_64'):
7677
s3_url = f"{S3_MPY_PREFIX}mpy-cross.static-amd64-linux-{circuitpython_tag}"
77-
elif uname[0] == 'Linux' and uname[4] == 'armv7l':
78+
elif uname[0].title() == 'Linux' and uname[4].lower() == 'armv7l':
7879
s3_url = f"{S3_MPY_PREFIX}mpy-cross.static-raspbian-{circuitpython_tag}"
79-
elif uname[0] == 'Darwin' and uname[4] == 'x86_64':
80+
elif uname[0].title() == 'Darwin' and uname[4].lower() == 'x86_64':
8081
s3_url = f"{S3_MPY_PREFIX}mpy-cross-macos-catalina-{circuitpython_tag}"
82+
elif uname[0].title() == "Windows" and uname[4].lower() in ("amd64", "x86_64"):
83+
# prebuilt mpy-cross binary executable does not seem to exist on AWS.
84+
# this URL triggers a "NOT FOUND" prompt below, then attempts to build from src
85+
s3_url = f"{S3_MPY_PREFIX}mpy-cross-windows-{circuitpython_tag}"
8186
elif not quiet:
8287
print(f"Pre-built mpy-cross not available for sysname='{uname[0]}' release='{uname[2]}' machine='{uname[4]}'.")
8388

@@ -121,11 +126,12 @@ def mpy_cross(mpy_cross_filename, circuitpython_tag, quiet=False):
121126
make = subprocess.run("make clean && make", shell=True)
122127
os.chdir(current_dir)
123128

124-
shutil.copy("build_deps/circuitpython/mpy-cross/mpy-cross", mpy_cross_filename)
125-
126129
if make.returncode != 0:
130+
print("Failed to build mpy-cross from source... bailing out")
127131
sys.exit(make.returncode)
128132

133+
shutil.copy("build_deps/circuitpython/mpy-cross/mpy-cross", mpy_cross_filename)
134+
129135
def _munge_to_temp(original_path, temp_file, library_version):
130136
with open(original_path, "rb") as original_file:
131137
for line in original_file:
@@ -171,7 +177,7 @@ def get_package_info(library_path, package_folder_prefix):
171177
package_info["module_name"] = py_files[0].relative_to(library_path).name[:-3]
172178
else:
173179
package_info["module_name"] = None
174-
180+
175181
try:
176182
package_info["version"] = version_string(library_path, valid_semver=True)
177183
except ValueError as e:
@@ -254,7 +260,9 @@ def library(library_path, output_directory, package_folder_prefix,
254260
output_directory,
255261
filename.relative_to(library_path).with_suffix(new_extension)
256262
)
257-
with tempfile.NamedTemporaryFile() as temp_file:
263+
temp_filename = ""
264+
mpy_success = 1
265+
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
258266
_munge_to_temp(full_path, temp_file, library_version)
259267

260268
if mpy_cross:
@@ -264,10 +272,11 @@ def library(library_path, output_directory, package_folder_prefix,
264272
"-s", str(filename.relative_to(library_path)),
265273
temp_file.name
266274
])
267-
if mpy_success != 0:
268-
raise RuntimeError("mpy-cross failed on", full_path)
269-
else:
270-
shutil.copyfile(temp_file.name, output_file)
275+
temp_filename = temp_file.name
276+
if mpy_cross and mpy_success != 0:
277+
raise RuntimeError("mpy-cross failed on", full_path)
278+
shutil.copyfile(temp_filename, output_file)
279+
os.remove(temp_filename)
271280

272281
for filename in package_files:
273282
full_path = os.path.join(library_path, filename)
@@ -276,7 +285,7 @@ def library(library_path, output_directory, package_folder_prefix,
276285
if not mpy_cross or os.stat(full_path).st_size == 0:
277286
output_file = os.path.join(output_directory,
278287
filename.relative_to(library_path))
279-
shutil.copyfile(temp_file.name, output_file)
288+
temp_filename = temp_file.name
280289
else:
281290
output_file = os.path.join(
282291
output_directory,
@@ -291,6 +300,9 @@ def library(library_path, output_directory, package_folder_prefix,
291300
])
292301
if mpy_success != 0:
293302
raise RuntimeError("mpy-cross failed on", full_path)
303+
if not mpy_cross or os.stat(full_path).st_size == 0:
304+
shutil.copyfile(temp_file.name, output_file)
305+
os.remove(temp_filename)
294306

295307
requirements_files = lib_path.glob("requirements.txt*")
296308
requirements_files = [f for f in requirements_files if f.stat().st_size > 0]
@@ -312,6 +324,9 @@ def library(library_path, output_directory, package_folder_prefix,
312324
full_path = os.path.join(library_path, filename)
313325
output_file = os.path.join(output_directory.replace("/lib", "/"),
314326
filename.relative_to(library_path))
315-
with tempfile.NamedTemporaryFile() as temp_file:
327+
temp_filename = ""
328+
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
316329
_munge_to_temp(full_path, temp_file, library_version)
317-
shutil.copyfile(temp_file.name, output_file)
330+
temp_filename = temp_file.name
331+
shutil.copyfile(temp_file.name, output_file)
332+
os.remove(temp_filename)

0 commit comments

Comments
 (0)