Skip to content

Commit de44a70

Browse files
authored
Merge pull request #84 from 2bndy5/make-windows-compatible
Windows compatibility
2 parents 7da9c1d + a55a923 commit de44a70

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

circuitpython_build_tools/build.py

Lines changed: 28 additions & 14 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,16 @@ 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+
s3_url = f"{S3_MPY_PREFIX}mpy-cross.static-x64-windows-{circuitpython_tag}.exe"
8184
elif not quiet:
8285
print(f"Pre-built mpy-cross not available for sysname='{uname[0]}' release='{uname[2]}' machine='{uname[4]}'.")
8386

@@ -121,11 +124,12 @@ def mpy_cross(mpy_cross_filename, circuitpython_tag, quiet=False):
121124
make = subprocess.run("make clean && make", shell=True)
122125
os.chdir(current_dir)
123126

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

131+
shutil.copy("build_deps/circuitpython/mpy-cross/mpy-cross", mpy_cross_filename)
132+
129133
def _munge_to_temp(original_path, temp_file, library_version):
130134
with open(original_path, "rb") as original_file:
131135
for line in original_file:
@@ -171,7 +175,7 @@ def get_package_info(library_path, package_folder_prefix):
171175
package_info["module_name"] = py_files[0].relative_to(library_path).name[:-3]
172176
else:
173177
package_info["module_name"] = None
174-
178+
175179
try:
176180
package_info["version"] = version_string(library_path, valid_semver=True)
177181
except ValueError as e:
@@ -254,7 +258,8 @@ def library(library_path, output_directory, package_folder_prefix,
254258
output_directory,
255259
filename.relative_to(library_path).with_suffix(new_extension)
256260
)
257-
with tempfile.NamedTemporaryFile() as temp_file:
261+
temp_filename = ""
262+
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
258263
_munge_to_temp(full_path, temp_file, library_version)
259264

260265
if mpy_cross:
@@ -266,17 +271,20 @@ def library(library_path, output_directory, package_folder_prefix,
266271
])
267272
if mpy_success != 0:
268273
raise RuntimeError("mpy-cross failed on", full_path)
269-
else:
270-
shutil.copyfile(temp_file.name, output_file)
274+
temp_filename = temp_file.name
275+
shutil.copyfile(temp_filename, output_file)
276+
os.remove(temp_filename)
271277

272278
for filename in package_files:
273279
full_path = os.path.join(library_path, filename)
274-
with tempfile.NamedTemporaryFile() as temp_file:
280+
temp_filename = ""
281+
output_file = ""
282+
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
275283
_munge_to_temp(full_path, temp_file, library_version)
276284
if not mpy_cross or os.stat(full_path).st_size == 0:
277285
output_file = os.path.join(output_directory,
278286
filename.relative_to(library_path))
279-
shutil.copyfile(temp_file.name, output_file)
287+
temp_filename = temp_file.name
280288
else:
281289
output_file = os.path.join(
282290
output_directory,
@@ -291,6 +299,9 @@ def library(library_path, output_directory, package_folder_prefix,
291299
])
292300
if mpy_success != 0:
293301
raise RuntimeError("mpy-cross failed on", full_path)
302+
if temp_filename and output_file:
303+
shutil.copyfile(temp_filename, output_file)
304+
os.remove(temp_filename)
294305

295306
requirements_files = lib_path.glob("requirements.txt*")
296307
requirements_files = [f for f in requirements_files if f.stat().st_size > 0]
@@ -312,6 +323,9 @@ def library(library_path, output_directory, package_folder_prefix,
312323
full_path = os.path.join(library_path, filename)
313324
output_file = os.path.join(output_directory.replace("/lib", "/"),
314325
filename.relative_to(library_path))
315-
with tempfile.NamedTemporaryFile() as temp_file:
326+
temp_filename = ""
327+
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
316328
_munge_to_temp(full_path, temp_file, library_version)
317-
shutil.copyfile(temp_file.name, output_file)
329+
temp_filename = temp_file.name
330+
shutil.copyfile(temp_filename, output_file)
331+
os.remove(temp_filename)

circuitpython_build_tools/scripts/build_bundles.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ def build_bundles(filename_prefix, output_directory, library_location, library_d
257257
mpy_cross = pkg_resources.resource_filename(
258258
target_versions.__name__, "data/mpy-cross-" + version["name"])
259259
else:
260-
mpy_cross = "build_deps/mpy-cross-" + version["name"]
260+
mpy_cross = "build_deps/mpy-cross-" + version["name"] + (".exe" * (os.name == "nt"))
261261
build.mpy_cross(mpy_cross, version["tag"])
262262
zip_filename = os.path.join(output_directory,
263263
filename_prefix + '-{TAG}-mpy-{VERSION}.zip'.format(

0 commit comments

Comments
 (0)