Skip to content

Commit f83ac85

Browse files
authored
pathlib
1 parent 6311238 commit f83ac85

File tree

1 file changed

+48
-33
lines changed

1 file changed

+48
-33
lines changed

builder/frameworks/arduino.py

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import sys
3030
import shutil
3131
import hashlib
32+
from pathlib import Path
3233
from os.path import join, exists, isabs, splitdrive, commonpath, relpath
3334

3435
from SCons.Script import DefaultEnvironment, SConscript
@@ -58,21 +59,24 @@ def __init__(self, platform, mcu):
5859
self._framework_dir = None
5960
self._framework_lib_dir = None
6061
self._sdk_dir = None
62+
self._project_dir = None
6163

6264
@property
6365
def framework_dir(self):
6466
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():
6769
raise RuntimeError("Arduino framework package not found")
70+
self._framework_dir = framework_path
6871
return self._framework_dir
6972

7073
@property
7174
def framework_lib_dir(self):
7275
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():
7578
raise RuntimeError("Arduino framework libs package not found")
79+
self._framework_lib_dir = lib_path
7680
return self._framework_lib_dir
7781

7882
@property
@@ -82,6 +86,12 @@ def sdk_dir(self):
8286
join(self.framework_lib_dir, self.mcu, "include")
8387
)
8488
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
8595

8696
# Initialization
8797
env = DefaultEnvironment()
@@ -93,7 +103,6 @@ def sdk_dir(self):
93103
# Cached values
94104
mcu = board.get("build.mcu", "esp32")
95105
pioenv = env["PIOENV"]
96-
project_dir = env.subst("$PROJECT_DIR")
97106
path_cache = PathCache(platform, mcu)
98107

99108
# Board configuration
@@ -123,7 +132,7 @@ def sdk_dir(self):
123132

124133
SConscript("_embed_files.py", exports="env")
125134

126-
flag_any_custom_sdkconfig = exists(join(FRAMEWORK_LIB_DIR, "sdkconfig"))
135+
flag_any_custom_sdkconfig = (Path(FRAMEWORK_LIB_DIR) / "sdkconfig").exists()
127136

128137
def has_unicore_flags():
129138
"""Check if any UNICORE flags are present in configuration"""
@@ -189,15 +198,15 @@ def matching_custom_sdkconfig():
189198
if not flag_any_custom_sdkconfig:
190199
return True, cust_sdk_is_present
191200

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():
194203
return False, cust_sdk_is_present
195204

196205
if not flag_custom_sdkconfig:
197206
return False, cust_sdk_is_present
198207

199208
try:
200-
with open(last_sdkconfig_path) as src:
209+
with last_sdkconfig_path.open('r') as src:
201210
line = src.readline()
202211
if line.startswith("# TASMOTA__"):
203212
cust_sdk_is_present = True
@@ -226,13 +235,14 @@ def check_reinstall_frwrk():
226235
def call_compile_libs():
227236
# ESP32-C2 special handling
228237
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)
236246

237247
print(f"*** Compile Arduino IDF libs for {pioenv} ***")
238248
SConscript("espidf.py")
@@ -280,28 +290,34 @@ def get_frameworks_in_current_env():
280290
"""Determines the frameworks of the current environment"""
281291
if "framework" in config.options(current_env_section):
282292
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 []
284294
return []
285295

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+
286306
def reinstall_framework():
287307
"""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()
293309

294310
print("*** Reinstall Arduino framework ***")
295311

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)
300316
]
301317

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)
305321

306322
# Extract URLs and install packages
307323
arduino_specs = [
@@ -326,7 +342,6 @@ def reinstall_framework():
326342
# Framework reinstallation if required
327343
if check_reinstall_frwrk():
328344
reinstall_framework()
329-
330345
if flag_custom_sdkconfig:
331346
call_compile_libs()
332347
flag_custom_sdkconfig = False
@@ -344,7 +359,7 @@ def reinstall_framework():
344359
if IS_WINDOWS:
345360
env.AddBuildMiddleware(shorthen_includes)
346361

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))
350365

0 commit comments

Comments
 (0)