Skip to content

Commit 4c62391

Browse files
committed
updates handling of original files
1 parent 1e31eb8 commit 4c62391

File tree

7 files changed

+80
-128
lines changed

7 files changed

+80
-128
lines changed

builder/__init__.py

Lines changed: 54 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import shutil
12
import sys
23
import os
34
import 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

7177
def 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

144166
def 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

175190
def 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)

builder/esp32.py

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -843,27 +843,18 @@ def find_esp32_ports(chip):
843843
SDKCONFIG_PATH = f'build/sdkconfig.board'
844844

845845
MPTHREADPORT_PATH = 'lib/micropython/ports/esp32/mpthreadport.c'
846-
MPTHREADPORT_SAVE_PATH = 'micropy_updates/originals/esp32/mpthreadport.c'
847-
848846
MPCONFIGPORT_PATH = 'lib/micropython/ports/esp32/mpconfigport.h'
849-
MPCONFIGPORT_SAVE_PATH = 'micropy_updates/originals/esp32/mpconfigport.h'
850-
851847
PANICHANDLER_PATH = 'lib/micropython/ports/esp32/panichandler.c'
852-
PANICHANDLER_SAVE_PATH = 'micropy_updates/originals/esp32/panichandler.c'
853-
854848
MPHALPORT_PATH = 'lib/micropython/ports/esp32/mphalport.c'
855-
MPHALPORT_SAVE_PATH = 'micropy_updates/originals/esp32/mphalport.c'
856-
857849
MAIN_PATH = 'lib/micropython/ports/esp32/main.c'
858-
MAIN_SAVE_PATH = 'micropy_updates/originals/esp32/main.c'
859850

860851

861852
if not os.path.exists('micropy_updates/originals/esp32'):
862853
os.mkdir('micropy_updates/originals/esp32')
863854

864855

865856
def update_mpthreadport():
866-
data = read_file(MPTHREADPORT_PATH, MPTHREADPORT_SAVE_PATH)
857+
data = read_file('esp32', MPTHREADPORT_PATH)
867858

868859
if '_CORE_ID' not in data:
869860
data = data.replace('MP_TASK_COREID', '_CORE_ID')
@@ -885,7 +876,7 @@ def update_mpthreadport():
885876

886877

887878
def update_panic_handler():
888-
data = read_file(PANICHANDLER_PATH, PANICHANDLER_SAVE_PATH)
879+
data = read_file('esp32', PANICHANDLER_PATH)
889880

890881
if '"MPY version : "' in data:
891882
beg, end = data.split('"MPY version : "', 1)
@@ -903,11 +894,8 @@ def update_mpconfigboard():
903894
'lib/micropython/ports/esp32/boards/'
904895
f'{board}/mpconfigboard.cmake'
905896
)
906-
mpconfigboard_cmake_save_path = (
907-
'micropy_updates/originals/esp32/boards/'
908-
f'{board}/mpconfigboard.cmake'
909-
)
910-
data = read_file(mpconfigboard_cmake_path, mpconfigboard_cmake_save_path)
897+
898+
data = read_file('esp32', mpconfigboard_cmake_path)
911899

912900
sdkconfig = (
913901
'set(SDKCONFIG_DEFAULTS ${SDKCONFIG_DEFAULTS} '
@@ -921,7 +909,7 @@ def update_mpconfigboard():
921909

922910

923911
def update_mpconfigport():
924-
data = read_file(MPCONFIGPORT_PATH, MPCONFIGPORT_SAVE_PATH)
912+
data = read_file('esp32', MPCONFIGPORT_PATH)
925913

926914
if 'MP_USB_OTG' in data:
927915
data = data.rsplit('\n\n#define MP_USB_OTG', 1)[0]
@@ -1003,7 +991,7 @@ def update_mpconfigport():
1003991

1004992

1005993
def update_mphalport():
1006-
data = read_file(MPHALPORT_PATH, MPHALPORT_SAVE_PATH)
994+
data = read_file('esp32', MPHALPORT_PATH)
1007995
data = data.replace(
1008996
'#if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG',
1009997
'#if MP_USB_SERIAL_JTAG'
@@ -1017,7 +1005,7 @@ def update_mphalport():
10171005

10181006

10191007
def update_main():
1020-
data = read_file(MAIN_PATH, MAIN_SAVE_PATH)
1008+
data = read_file('esp32', MAIN_PATH)
10211009
data = data.replace(
10221010
'#if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG',
10231011
'#if MP_USB_SERIAL_JTAG'

builder/macOS.py

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,6 @@
1818

1919
unix.REAL_PORT = 'macOS'
2020

21-
unix.INPUT_SAVE_PATH = (
22-
unix.INPUT_SAVE_PATH.replace('/unix/', '/macOS/')
23-
)
24-
unix.MAIN_SAVE_PATH = (
25-
unix.MAIN_SAVE_PATH.replace('/unix/', '/macOS/')
26-
)
27-
unix.UNIX_MPHAL_SAVE_PATH = (
28-
unix.UNIX_MPHAL_SAVE_PATH.replace('/unix/', '/macOS/')
29-
)
30-
unix.MAKEFILE_SAVE_PATH = (
31-
unix.MAKEFILE_SAVE_PATH.replace('/unix/', '/macOS/')
32-
)
33-
unix.MODMACHINE_SAVE_PATH = (
34-
unix.MODMACHINE_SAVE_PATH.replace('/unix/', '/macOS/')
35-
)
36-
unix.MPCONFIGVARIANT_COMMON_SAVE_PATH = (
37-
unix.MPCONFIGVARIANT_COMMON_SAVE_PATH.replace('/unix/', '/macOS/')
38-
)
39-
40-
4121
if not os.path.exists('micropy_updates/originals/macOS'):
4222
os.mkdir('micropy_updates/originals/macOS')
4323

@@ -83,7 +63,8 @@ def compile(*args): # NOQA
8363

8464
src = f'lib/micropython/ports/unix/build-{unix.variant}/SDL/libSDL2-2.0.0.dylib'
8565
dst = f'build/libSDL2-2.0.0.dylib'
86-
shutil.copyfile(src, dst)
66+
if os.path.exists(src):
67+
shutil.copyfile(src, dst)
8768

8869

8970
def mpy_cross():

builder/raspberry_pi.py

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,6 @@
2222

2323
unix.REAL_PORT = 'raspberry_pi'
2424

25-
unix.INPUT_SAVE_PATH = (
26-
unix.INPUT_SAVE_PATH.replace('/unix/', '/raspberry_pi/')
27-
)
28-
unix.MAIN_SAVE_PATH = (
29-
unix.MAIN_SAVE_PATH.replace('/unix/', '/raspberry_pi/')
30-
)
31-
unix.UNIX_MPHAL_SAVE_PATH = (
32-
unix.UNIX_MPHAL_SAVE_PATH.replace('/unix/', '/raspberry_pi/')
33-
)
34-
unix.MAKEFILE_SAVE_PATH = (
35-
unix.MAKEFILE_SAVE_PATH.replace('/unix/', '/raspberry_pi/')
36-
)
37-
unix.MODMACHINE_SAVE_PATH = (
38-
unix.MODMACHINE_SAVE_PATH.replace('/unix/', '/raspberry_pi/')
39-
)
40-
unix.MPCONFIGVARIANT_COMMON_SAVE_PATH = (
41-
unix.MPCONFIGVARIANT_COMMON_SAVE_PATH.replace('/unix/', '/raspberry_pi/')
42-
)
43-
4425

4526
def parse_args(extra_args, lv_cflags, board):
4627
return _parse_args(extra_args, lv_cflags, board)
@@ -99,8 +80,7 @@ def has_neon():
9980
if not line.startswith('Features'):
10081
continue
10182

102-
features = line.split(':')[-1].strip().split(' ')
103-
res = 'neon' in features
83+
res = ' neon ' in line
10484

10585
if res:
10686
break

0 commit comments

Comments
 (0)