2626
2727import os
2828import os .path
29+ import platform
2930import pathlib
3031import requests
3132import 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+
129133def _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 )
0 commit comments