@@ -258,50 +258,49 @@ def library(library_path, output_directory, package_folder_prefix,
258258 output_directory ,
259259 filename .relative_to (library_path ).with_suffix (new_extension )
260260 )
261- temp_filename = ""
262261 with tempfile .NamedTemporaryFile (delete = False ) as temp_file :
263262 _munge_to_temp (full_path , temp_file , library_version )
264-
265- if mpy_cross :
266- mpy_success = subprocess .call ([
267- mpy_cross ,
268- "-o" , output_file ,
269- "-s" , str (filename .relative_to (library_path )),
270- temp_file .name
271- ])
272- if mpy_success != 0 :
273- raise RuntimeError ("mpy-cross failed on" , full_path )
274263 temp_filename = temp_file .name
275- shutil .copyfile (temp_filename , output_file )
264+ # Windows: close the temp file before it can be read or copied by name
265+ if mpy_cross :
266+ mpy_success = subprocess .call ([
267+ mpy_cross ,
268+ "-o" , output_file ,
269+ "-s" , str (filename .relative_to (library_path )),
270+ temp_filename
271+ ])
272+ if mpy_success != 0 :
273+ raise RuntimeError ("mpy-cross failed on" , full_path )
274+ else :
275+ shutil .copyfile (temp_filename , output_file )
276276 os .remove (temp_filename )
277277
278278 for filename in package_files :
279279 full_path = os .path .join (library_path , filename )
280- temp_filename = ""
281280 output_file = ""
282281 with tempfile .NamedTemporaryFile (delete = False ) as temp_file :
283282 _munge_to_temp (full_path , temp_file , library_version )
284- if not mpy_cross or os .stat (full_path ).st_size == 0 :
285- output_file = os .path .join (output_directory ,
286- filename .relative_to (library_path ))
287- temp_filename = temp_file .name
288- else :
289- output_file = os .path .join (
290- output_directory ,
291- filename .relative_to (library_path ).with_suffix (new_extension )
292- )
293-
294- mpy_success = subprocess .call ([
295- mpy_cross ,
296- "-o" , output_file ,
297- "-s" , str (filename .relative_to (library_path )),
298- temp_file .name
299- ])
300- if mpy_success != 0 :
301- raise RuntimeError ("mpy-cross failed on" , full_path )
302- if temp_filename and output_file :
283+ temp_filename = temp_file .name
284+ # Windows: close the temp file before it can be read or copied by name
285+ if not mpy_cross or os .stat (full_path ).st_size == 0 :
286+ output_file = os .path .join (output_directory ,
287+ filename .relative_to (library_path ))
303288 shutil .copyfile (temp_filename , output_file )
304- os .remove (temp_filename )
289+ else :
290+ output_file = os .path .join (
291+ output_directory ,
292+ filename .relative_to (library_path ).with_suffix (new_extension )
293+ )
294+
295+ mpy_success = subprocess .call ([
296+ mpy_cross ,
297+ "-o" , output_file ,
298+ "-s" , str (filename .relative_to (library_path )),
299+ temp_filename
300+ ])
301+ if mpy_success != 0 :
302+ raise RuntimeError ("mpy-cross failed on" , full_path )
303+ os .remove (temp_filename )
305304
306305 requirements_files = lib_path .glob ("requirements.txt*" )
307306 requirements_files = [f for f in requirements_files if f .stat ().st_size > 0 ]
0 commit comments