14
14
15
15
import os
16
16
import requests
17
+ import json
17
18
import subprocess
18
19
import sys
19
20
import shutil
@@ -45,7 +46,7 @@ def configure_default_packages(self, variables, targets):
45
46
board_sdkconfig = variables .get ("board_espidf.custom_sdkconfig" , board_config .get ("espidf.custom_sdkconfig" , "" ))
46
47
frameworks = variables .get ("pioframework" , [])
47
48
48
- def install_tool (TOOL ):
49
+ def install_tool (TOOL , retry_count = 0 ):
49
50
self .packages [TOOL ]["optional" ] = False
50
51
TOOL_PATH = os .path .join (ProjectConfig .get_instance ().get ("platformio" , "packages_dir" ), TOOL )
51
52
TOOL_PACKAGE_PATH = os .path .join (TOOL_PATH , "package.json" )
@@ -81,13 +82,37 @@ def install_tool(TOOL):
81
82
try :
82
83
shutil .rmtree (TOOL_PATH )
83
84
except Exception as e :
84
- print (f"Error while removing the tool folder: { e } " )
85
+ print (f"Error while removing the tool folder: { e } " )
85
86
pm .install (tl_path )
86
87
# tool is already installed, just activate it
87
88
if tl_flag and pio_flag and not json_flag :
88
- self .packages [TOOL ]["version" ] = TOOL_PATH
89
- self .packages [TOOL ]["optional" ] = False
90
-
89
+ with open (TOOL_PACKAGE_PATH , "r" ) as file :
90
+ package_data = json .load (file )
91
+ # check installed tool version against listed in platforms.json
92
+ if "package-version" in self .packages [TOOL ] \
93
+ and "version" in package_data \
94
+ and self .packages [TOOL ]["package-version" ] == package_data ["version" ]:
95
+ self .packages [TOOL ]["version" ] = TOOL_PATH
96
+ self .packages [TOOL ]["optional" ] = False
97
+ elif "package-version" not in self .packages [TOOL ]:
98
+ # No version check needed, just use the installed tool
99
+ self .packages [TOOL ]["version" ] = TOOL_PATH
100
+ self .packages [TOOL ]["optional" ] = False
101
+ elif "version" not in package_data :
102
+ print (f"Warning: Cannot determine installed version for { TOOL } . Reinstalling..." )
103
+ else : # Installed version does not match required version, deinstall existing and install needed
104
+ self .packages .pop (TOOL , None )
105
+ if os .path .exists (TOOL_PATH ) and os .path .isdir (TOOL_PATH ):
106
+ try :
107
+ shutil .rmtree (TOOL_PATH )
108
+ except Exception as e :
109
+ print (f"Error while removing the tool folder: { e } " )
110
+ if retry_count >= 3 : # Limit to 3 retries
111
+ print (f"Failed to install { TOOL } after multiple attempts. Please check your network connection and try again manually." )
112
+ return
113
+ print (f"Wrong version for { TOOL } . Installing needed version..." )
114
+ install_tool (TOOL , retry_count + 1 )
115
+
91
116
return
92
117
93
118
# Installer only needed for setup, deactivate when installed
@@ -135,7 +160,6 @@ def install_tool(TOOL):
135
160
# Set mandatory toolchains
136
161
for toolchain in toolchain_data ["toolchains" ]:
137
162
install_tool (toolchain )
138
-
139
163
# Set ULP toolchain if applicable
140
164
ulp_toolchain = toolchain_data .get ("ulp_toolchain" )
141
165
if ulp_toolchain and os .path .isdir ("ulp" ):
@@ -174,6 +198,13 @@ def install_tool(TOOL):
174
198
if "buildfs" in targets :
175
199
filesystem = variables .get ("board_build.filesystem" , "littlefs" )
176
200
if filesystem == "littlefs" :
201
+ # ensure use of mklittlefs 3.2.0
202
+ piopm_path = os .path .join (ProjectConfig .get_instance ().get ("platformio" , "packages_dir" ), "tool-mklittlefs" , ".piopm" )
203
+ if os .path .exists (piopm_path ):
204
+ with open (piopm_path , "r" ) as file :
205
+ package_data = json .load (file )
206
+ if package_data ['version' ] == "4.0.0" :
207
+ os .remove (piopm_path )
177
208
install_tool ("tool-mklittlefs" )
178
209
elif filesystem == "fatfs" :
179
210
install_tool ("tool-mkfatfs" )
@@ -184,9 +215,25 @@ def install_tool(TOOL):
184
215
filesystem = variables .get ("board_build.filesystem" , "littlefs" )
185
216
if filesystem == "littlefs" :
186
217
# Use Tasmota mklittlefs v4.0.0 to unpack, older version is incompatible
187
- self .packages ["tool-mklittlefs" ]["version" ] = "https://github.com/pioarduino/registry/releases/download/0.0.1/mklittlefs-4.0.0.zip"
188
- self .packages ["tool-mklittlefs" ]["optional" ] = False
189
- install_tool ("tool-mklittlefs" )
218
+ # make sure mklittlefs 3.2.0 is installed
219
+ mklittlefs_dir = os .path .join (ProjectConfig .get_instance ().get ("platformio" , "packages_dir" ), "tool-mklittlefs" )
220
+ if not os .path .exists (mklittlefs_dir ):
221
+ install_tool ("tool-mklittlefs" )
222
+ if os .path .exists (os .path .join (mklittlefs_dir , "tools.json" )):
223
+ install_tool ("tool-mklittlefs" )
224
+ mklittlefs400_dir = os .path .join (ProjectConfig .get_instance ().get ("platformio" , "packages_dir" ), "tool-mklittlefs-4.0.0" )
225
+ if not os .path .exists (mklittlefs400_dir ):
226
+ # install mklittlefs 4.0.0
227
+ install_tool ("tool-mklittlefs-4.0.0" )
228
+ if os .path .exists (os .path .join (mklittlefs400_dir , "tools.json" )):
229
+ install_tool ("tool-mklittlefs-4.0.0" )
230
+ # use mklittlefs 4.0.0 instead of 3.2.0 by copying over
231
+ if os .path .exists (mklittlefs400_dir ):
232
+ shutil .copyfile (
233
+ os .path .join (mklittlefs_dir , "package.json" ),
234
+ os .path .join (mklittlefs400_dir , "package.json" ),
235
+ )
236
+ shutil .copytree (mklittlefs400_dir , mklittlefs_dir , dirs_exist_ok = True )
190
237
191
238
# Currently only Arduino Nano ESP32 uses the dfuutil tool as uploader
192
239
if variables .get ("board" ) == "arduino_nano_esp32" :
0 commit comments