29
29
import sys
30
30
import shutil
31
31
import hashlib
32
+ from pathlib import Path
32
33
from os .path import join , exists , isabs , splitdrive , commonpath , relpath
33
34
34
35
from SCons .Script import DefaultEnvironment , SConscript
@@ -58,21 +59,24 @@ def __init__(self, platform, mcu):
58
59
self ._framework_dir = None
59
60
self ._framework_lib_dir = None
60
61
self ._sdk_dir = None
62
+ self ._project_dir = None
61
63
62
64
@property
63
65
def framework_dir (self ):
64
66
if self ._framework_dir is None :
65
- self . _framework_dir = self .platform .get_package_dir ("framework-arduinoespressif32" )
66
- if not self . _framework_dir or not exists ( self . _framework_dir ):
67
+ framework_path = self .platform .get_package_dir ("framework-arduinoespressif32" )
68
+ if not framework_path or not Path ( framework_path ). exists ( ):
67
69
raise RuntimeError ("Arduino framework package not found" )
70
+ self ._framework_dir = framework_path
68
71
return self ._framework_dir
69
72
70
73
@property
71
74
def framework_lib_dir (self ):
72
75
if self ._framework_lib_dir is None :
73
- self . _framework_lib_dir = self .platform .get_package_dir ("framework-arduinoespressif32-libs" )
74
- if not self . _framework_lib_dir or not exists ( self . _framework_lib_dir ):
76
+ lib_path = self .platform .get_package_dir ("framework-arduinoespressif32-libs" )
77
+ if not lib_path or not Path ( lib_path ). exists ( ):
75
78
raise RuntimeError ("Arduino framework libs package not found" )
79
+ self ._framework_lib_dir = lib_path
76
80
return self ._framework_lib_dir
77
81
78
82
@property
@@ -82,6 +86,12 @@ def sdk_dir(self):
82
86
join (self .framework_lib_dir , self .mcu , "include" )
83
87
)
84
88
return self ._sdk_dir
89
+
90
+ @property
91
+ def project_dir (self ):
92
+ if self ._project_dir is None :
93
+ self ._project_dir = Path (env .subst ("$PROJECT_DIR" ))
94
+ return self ._project_dir
85
95
86
96
# Initialization
87
97
env = DefaultEnvironment ()
@@ -93,7 +103,6 @@ def sdk_dir(self):
93
103
# Cached values
94
104
mcu = board .get ("build.mcu" , "esp32" )
95
105
pioenv = env ["PIOENV" ]
96
- project_dir = env .subst ("$PROJECT_DIR" )
97
106
path_cache = PathCache (platform , mcu )
98
107
99
108
# Board configuration
@@ -123,7 +132,7 @@ def sdk_dir(self):
123
132
124
133
SConscript ("_embed_files.py" , exports = "env" )
125
134
126
- flag_any_custom_sdkconfig = exists ( join (FRAMEWORK_LIB_DIR , "sdkconfig" ))
135
+ flag_any_custom_sdkconfig = ( Path (FRAMEWORK_LIB_DIR ) / "sdkconfig" ). exists ( )
127
136
128
137
def has_unicore_flags ():
129
138
"""Check if any UNICORE flags are present in configuration"""
@@ -189,15 +198,15 @@ def matching_custom_sdkconfig():
189
198
if not flag_any_custom_sdkconfig :
190
199
return True , cust_sdk_is_present
191
200
192
- last_sdkconfig_path = join ( project_dir , "sdkconfig.defaults" )
193
- if not exists (last_sdkconfig_path ):
201
+ last_sdkconfig_path = path_cache . project_dir / "sdkconfig.defaults"
202
+ if not last_sdkconfig_path . exists ():
194
203
return False , cust_sdk_is_present
195
204
196
205
if not flag_custom_sdkconfig :
197
206
return False , cust_sdk_is_present
198
207
199
208
try :
200
- with open (last_sdkconfig_path ) as src :
209
+ with last_sdkconfig_path . open ('r' ) as src :
201
210
line = src .readline ()
202
211
if line .startswith ("# TASMOTA__" ):
203
212
cust_sdk_is_present = True
@@ -226,13 +235,14 @@ def check_reinstall_frwrk():
226
235
def call_compile_libs ():
227
236
# ESP32-C2 special handling
228
237
if mcu == "esp32c2" :
229
- arduino_frmwrk_c2_lib_dir = join (FRAMEWORK_LIB_DIR , mcu )
230
- if not exists (arduino_frmwrk_c2_lib_dir ):
231
- arduino_c2_dir = join (
232
- platform .get_package_dir ("framework-arduino-c2-skeleton-lib" ), mcu
233
- )
234
- if exists (arduino_c2_dir ):
235
- shutil .copytree (arduino_c2_dir , arduino_frmwrk_c2_lib_dir , dirs_exist_ok = True )
238
+ arduino_frmwrk_c2_lib_dir = Path (FRAMEWORK_LIB_DIR ) / mcu
239
+ if not arduino_frmwrk_c2_lib_dir .exists ():
240
+ arduino_c2_source = Path (
241
+ platform .get_package_dir ("framework-arduino-c2-skeleton-lib" )
242
+ ) / mcu
243
+
244
+ if arduino_c2_source .exists ():
245
+ shutil .copytree (arduino_c2_source , arduino_frmwrk_c2_lib_dir , dirs_exist_ok = True )
236
246
237
247
print (f"*** Compile Arduino IDF libs for { pioenv } ***" )
238
248
SConscript ("espidf.py" )
@@ -280,28 +290,34 @@ def get_frameworks_in_current_env():
280
290
"""Determines the frameworks of the current environment"""
281
291
if "framework" in config .options (current_env_section ):
282
292
frameworks_str = config .get (current_env_section , "framework" , "" )
283
- return frameworks_str .split ("," ) if isinstance ( frameworks_str , str ) else frameworks_str
293
+ return frameworks_str .split ("," ) if frameworks_str else []
284
294
return []
285
295
296
+ def clean_sdkconfig_files ():
297
+ """Clean up existing sdkconfig files using pathlib"""
298
+ envs = [section .replace ("env:" , "" ) for section in config .sections ()
299
+ if section .startswith ("env:" )]
300
+
301
+ for env_name in envs :
302
+ sdkconfig_file = path_cache .project_dir / f"sdkconfig.{ env_name } "
303
+ if sdkconfig_file .exists ():
304
+ sdkconfig_file .unlink ()
305
+
286
306
def reinstall_framework ():
287
307
"""Reinstall Arduino framework packages"""
288
- envs = [section .replace ("env:" , "" ) for section in config .sections () if section .startswith ("env:" )]
289
- for env_name in envs :
290
- file_path = join (project_dir , f"sdkconfig.{ env_name } " )
291
- if exists (file_path ):
292
- os .remove (file_path )
308
+ clean_sdkconfig_files ()
293
309
294
310
print ("*** Reinstall Arduino framework ***" )
295
311
296
- # Remove framework directories
297
- framework_dirs = [
298
- path_cache .framework_dir ,
299
- path_cache .framework_lib_dir
312
+ # Remove framework directories using pathlib
313
+ framework_paths = [
314
+ Path ( path_cache .framework_dir ) ,
315
+ Path ( path_cache .framework_lib_dir )
300
316
]
301
317
302
- for dir_path in framework_dirs :
303
- if exists (dir_path ):
304
- shutil .rmtree (dir_path )
318
+ for framework_path in framework_paths :
319
+ if framework_path . exists ():
320
+ shutil .rmtree (framework_path )
305
321
306
322
# Extract URLs and install packages
307
323
arduino_specs = [
@@ -326,7 +342,6 @@ def reinstall_framework():
326
342
# Framework reinstallation if required
327
343
if check_reinstall_frwrk ():
328
344
reinstall_framework ()
329
-
330
345
if flag_custom_sdkconfig :
331
346
call_compile_libs ()
332
347
flag_custom_sdkconfig = False
@@ -344,7 +359,7 @@ def reinstall_framework():
344
359
if IS_WINDOWS :
345
360
env .AddBuildMiddleware (shorthen_includes )
346
361
347
- # Arduino SCons build script
348
- build_script_path = join (path_cache .framework_dir , "tools" , "pioarduino-build.py" )
349
- SConscript (build_script_path )
362
+ # Arduino SCons build script using pathlib for cleaner path construction
363
+ build_script_path = Path (path_cache .framework_dir ) / "tools" / "pioarduino-build.py"
364
+ SConscript (str ( build_script_path ) )
350
365
0 commit comments