1+ import shutil
12import sys
23import os
34import subprocess
@@ -39,50 +40,71 @@ def iter_path(src_p, dst_p):
3940 iter_path (src_file , dst_file )
4041 os .rmdir (src_file )
4142 else :
42- read_file (src_file , dst_file )
43+ shutil . copyfile (src_file , dst_file )
4344 os .remove (src_file )
4445
4546 iter_path (src_path , dst_path )
4647
4748
48- def copy_updated_files (port ):
49+ def copy_micropy_updates (port ):
4950
5051 src_path = f'micropy_updates/{ port } '
51- originals_path = f'micropy_updates/originals/{ port } '
52+ org_path = f'micropy_updates/originals/{ port } '
5253
5354 if port in ('raspberry_pi' , 'macOS' ):
5455 port = 'unix'
5556
5657 dst_path = f'lib/micropython/ports/{ port } '
5758
58- for file in os .listdir (src_path ):
59+ def iter_files (s_path , d_path , o_path ):
60+ for file in os .listdir (s_path ):
61+ src_file = os .path .join (s_path , file )
62+ dst_file = os .path .join (d_path , file )
63+ org_file = os .path .join (o_path , file )
5964
60- src_file = os .path .join (dst_path , file )
61- dst_file = os .path .join (originals_path , file )
62-
63- read_file (src_file , dst_file )
65+ if os .path .isdir (src_file ):
66+ if not os .path .exists (org_file ):
67+ os .makedirs (org_file )
6468
65- src_file = os .path .join (src_path , file )
66- dst_file = os .path .join (dst_path , file )
69+ iter_files (src_file , dst_file , org_file )
70+ else :
71+ shutil .copyfile (dst_file , org_file )
72+ shutil .copyfile (src_file , dst_file )
6773
68- read_file ( src_file , dst_file )
74+ iter_files ( src_path , dst_path , org_path )
6975
7076
7177def write_file (file , data ):
7278 with open (file , 'wb' ) as f :
7379 f .write (data .encode ('utf-8' ))
7480
7581
76- def read_file (file , save_file ):
77- with open (file , 'rb' ) as f1 :
78- data = f1 .read ()
82+ def read_file (port , file ):
83+ org_path = f'micropy_updates/originals/{ port } '
84+
85+ filepath , filename = os .path .split (file )
86+
87+ if port in ('raspberry_pi' , 'macOS' ):
88+ port = 'unix'
7989
80- save_path = os .path .split (save_file )[0 ]
81- if not os .path .exists (save_path ):
82- os .makedirs (save_path )
90+ head , tail = os .path .split (filepath )
91+ save_path = []
92+ while tail != port :
93+ save_path .insert (0 , tail )
94+ head , tail = os .path .split (head )
8395
84- with open (save_file , 'wb' ) as f2 :
85- f2 .write (data )
96+ if save_path :
97+ org_path = os .path .join (org_path , * save_path )
98+
99+ if not os .path .exists (org_path ):
100+ os .makedirs (org_path )
101+
102+ org_file = os .path .join (org_path , filename )
103+ if not os .path .exists (org_file ):
104+ shutil .copyfile (file , org_file )
105+
106+ with open (file , 'rb' ) as f :
107+ data = f .read ()
86108
87109 return data .decode ('utf-8' )
88110
@@ -142,13 +164,7 @@ def set_mp_version(port):
142164
143165
144166def update_mphalport (target ):
145- if target == 'esp8266' :
146- mphalport_path = f'lib/micropython/ports/{ target } /esp_mphal.h'
147- elif target == 'pic16bit' :
148- mphalport_path = f'lib/micropython/ports/{ target } /pic16bit_mphal.h'
149- elif target == 'teensy' :
150- mphalport_path = f'lib/micropython/ports/{ target } /teensy_hal.h'
151- elif target == 'macOS' :
167+ if target in ('macOS' , 'raspberry_pi' ):
152168 mphalport_path = f'lib/micropython/ports/unix/mphalport.h'
153169 elif target == 'windows' :
154170 mphalport_path = f'lib/micropython/ports/{ target } /windows_mphal.h'
@@ -158,18 +174,17 @@ def update_mphalport(target):
158174 if not os .path .exists (mphalport_path ):
159175 raise RuntimeError (mphalport_path )
160176
161- with open (mphalport_path , 'rb' ) as f :
162- data = f .read ().decode ('utf-8' )
177+ data = read_file (target , mphalport_path )
163178
164- if '#ifndef _MPHALPORT_H_ ' not in data :
179+ if '__MPHALPORT_H__ ' not in data :
165180 data = (
166- f'#ifndef _MPHALPORT_H_ \n '
167- f'#define _MPHALPORT_H_ \n '
181+ f'#ifndef __MPHALPORT_H__ \n '
182+ f'#define __MPHALPORT_H__ \n '
168183 f'{ data } \n '
169- f'#endif /* _MPHALPORT_H_ */\n '
184+ f'#endif /* __MPHALPORT_H__ */\n '
170185 )
171- with open ( mphalport_path , 'wb' ) as f :
172- f . write ( data . encode ( 'utf-8' ) )
186+
187+ write_file ( mphalport_path , data )
173188
174189
175190def generate_manifest (
@@ -236,7 +251,7 @@ def generate_manifest(
236251 for file in indevs :
237252 if not os .path .exists (file ):
238253 tmp_file = (
239- f'{ script_dir } /api_drivers/common_api_drivers/indev/{ file } .py'
254+ f'{ script_dir } /api_drivers/common_api_drivers/indev/{ file . lower () } .py'
240255 )
241256
242257 if not os .path .exists (tmp_file ):
@@ -255,11 +270,11 @@ def generate_manifest(
255270 file = tmp_file
256271
257272 directory , file_name = os .path .split (file )
258- extension_file = file_name .rsplit ('.' )[0 ] + '_extension.py'
273+ extension_file = file_name .rsplit ('.' , 1 )[0 ] + '_extension.py'
259274 extension = os .path .join (directory , extension_file )
260275
261276 if os .path .exists (extension ):
262- print (extension_file )
277+ print (extension )
263278 entry = f"freeze('{ directory } ', '{ extension_file } ')"
264279 manifest_files .append (entry )
265280
@@ -273,7 +288,7 @@ def generate_manifest(
273288 for file in displays :
274289 if not os .path .exists (file ):
275290 tmp_file = (
276- f'{ script_dir } /api_drivers/common_api_drivers/display/{ file } '
291+ f'{ script_dir } /api_drivers/common_api_drivers/display/{ file . lower () } '
277292 )
278293
279294 if not os .path .exists (tmp_file ):
@@ -284,7 +299,7 @@ def generate_manifest(
284299 if not file_name .endswith ('.py' ):
285300 continue
286301
287- print (file_name )
302+ print (os . path . join ( tmp_file , file_name ) )
288303
289304 entry = f"freeze('{ tmp_file } ', '{ file_name } ')"
290305 manifest_files .append (entry )
0 commit comments