1+ import shutil
12import sys
23import os
34import subprocess
89_windows_env = None
910
1011
12+ def scrub_build_folder ():
13+ for f in os .listdir ('build' ):
14+ f = os .path .join ('build' ,f )
15+ for pattern in ('.h' ,'manifest.py' ,'.board' ):
16+ if f .endswith (pattern ):
17+ os .remove (f )
18+
19+
20+ def revert_files (port ):
21+ if port in ('macOS' ,'raspberry_pi' ):
22+ revert_files ('unix' )
23+
24+ src_path = f'micropy_updates/originals/{ port } '
25+
26+ if port in ('raspberry_pi' ,'macOS' ):
27+ port = 'unix'
28+
29+ dst_path = f'lib/micropython/ports/{ port } '
30+
31+ if not os .path .exists (src_path )or not os .listdir (src_path ):
32+ return
33+
34+ def iter_path (src_p ,dst_p ):
35+ for file in os .listdir (src_p ):
36+ src_file = os .path .join (src_p ,file )
37+ dst_file = os .path .join (dst_p ,file )
38+
39+ if os .path .isdir (src_file ):
40+ iter_path (src_file ,dst_file )
41+ os .rmdir (src_file )
42+ else :
43+ shutil .copyfile (src_file ,dst_file )
44+ os .remove (src_file )
45+
46+ iter_path (src_path ,dst_path )
47+
48+
49+ def copy_micropy_updates (port ):
50+
51+ src_path = f'micropy_updates/{ port } '
52+ org_path = f'micropy_updates/originals/{ port } '
53+
54+ if port in ('raspberry_pi' ,'macOS' ):
55+ port = 'unix'
56+
57+ dst_path = f'lib/micropython/ports/{ port } '
58+
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 )
64+
65+ if os .path .isdir (src_file ):
66+ if not os .path .exists (org_file ):
67+ os .makedirs (org_file )
68+
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 )
73+
74+ iter_files (src_path ,dst_path ,org_path )
75+
76+
77+ def write_file (file ,data ):
78+ with open (file ,'wb' )as f :
79+ f .write (data .encode ('utf-8' ))
80+
81+
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'
89+
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 )
95+
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 ()
108+
109+ return data .decode ('utf-8' )
110+
111+
11112def setup_windows_build ():
12113
13114global _windows_env
@@ -63,13 +164,7 @@ def set_mp_version(port):
63164
64165
65166def update_mphalport (target ):
66- if target == 'esp8266' :
67- mphalport_path = f'lib/micropython/ports/{ target } /esp_mphal.h'
68- elif target == 'pic16bit' :
69- mphalport_path = f'lib/micropython/ports/{ target } /pic16bit_mphal.h'
70- elif target == 'teensy' :
71- mphalport_path = f'lib/micropython/ports/{ target } /teensy_hal.h'
72- elif target == 'macOS' :
167+ if target in ('macOS' ,'raspberry_pi' ):
73168mphalport_path = f'lib/micropython/ports/unix/mphalport.h'
74169elif target == 'windows' :
75170mphalport_path = f'lib/micropython/ports/{ target } /windows_mphal.h'
@@ -79,18 +174,17 @@ def update_mphalport(target):
79174if not os .path .exists (mphalport_path ):
80175raise RuntimeError (mphalport_path )
81176
82- with open (mphalport_path ,'rb' )as f :
83- data = f .read ().decode ('utf-8' )
177+ data = read_file (target ,mphalport_path )
84178
85- if '#ifndef _MPHALPORT_H_ ' not in data :
179+ if '__MPHALPORT_H__ ' not in data :
86180data = (
87- f'#ifndef_MPHALPORT_H_ \n '
88- f'#define_MPHALPORT_H_ \n '
181+ f'#ifndef__MPHALPORT_H__ \n '
182+ f'#define__MPHALPORT_H__ \n '
89183f'{ data } \n '
90- f'#endif /*_MPHALPORT_H_ */\n '
184+ f'#endif /*__MPHALPORT_H__ */\n '
91185 )
92- with open ( mphalport_path , 'wb' ) as f :
93- f . write ( data . encode ( 'utf-8' ) )
186+
187+ write_file ( mphalport_path , data )
94188
95189
96190def generate_manifest (
@@ -157,7 +251,7 @@ def generate_manifest(
157251for file in indevs :
158252if not os .path .exists (file ):
159253tmp_file = (
160- f'{ script_dir } /api_drivers/common_api_drivers/indev/{ file } .py'
254+ f'{ script_dir } /api_drivers/common_api_drivers/indev/{ file . lower () } .py'
161255 )
162256
163257if not os .path .exists (tmp_file ):
@@ -176,11 +270,11 @@ def generate_manifest(
176270file = tmp_file
177271
178272directory ,file_name = os .path .split (file )
179- extension_file = file_name .rsplit ('.' )[0 ]+ '_extension.py'
273+ extension_file = file_name .rsplit ('.' , 1 )[0 ]+ '_extension.py'
180274extension = os .path .join (directory ,extension_file )
181275
182276if os .path .exists (extension ):
183- print (extension_file )
277+ print (extension )
184278entry = f"freeze('{ directory } ', '{ extension_file } ')"
185279manifest_files .append (entry )
186280
@@ -194,7 +288,7 @@ def generate_manifest(
194288for file in displays :
195289if not os .path .exists (file ):
196290tmp_file = (
197- f'{ script_dir } /api_drivers/common_api_drivers/display/{ file } '
291+ f'{ script_dir } /api_drivers/common_api_drivers/display/{ file . lower () } '
198292 )
199293
200294if not os .path .exists (tmp_file ):
@@ -205,7 +299,7 @@ def generate_manifest(
205299if not file_name .endswith ('.py' ):
206300continue
207301
208- print (file_name )
302+ print (os . path . join ( tmp_file , file_name ) )
209303
210304entry = f"freeze('{ tmp_file } ', '{ file_name } ')"
211305manifest_files .append (entry )
@@ -225,13 +319,7 @@ def generate_manifest(
225319
226320def get_lvgl ():
227321cmd_ = [
228- 'git' ,
229- 'submodule' ,
230- 'update' ,
231- '--init' ,
232- '--depth=1' ,
233- '--' ,
234- f'lib/lvgl'
322+ 'git submodule update --init --depth=1 -- lib/lvgl'
235323 ]
236324print ()
237325print ('collecting LVGL' )
@@ -244,13 +332,7 @@ def get_lvgl():
244332def get_micropython ():
245333
246334cmd_ = [
247- 'git' ,
248- 'submodule' ,
249- 'update' ,
250- '--init' ,
251- '--depth=1' ,
252- '--' ,
253- f'lib/micropython'
335+ 'git submodule update --init --depth=1 -- lib/micropython' ,
254336 ]
255337print ()
256338print ('collecting MicroPython 1.23.0' )