13
13
# limitations under the License.
14
14
15
15
import os
16
+ import requests
17
+ import json
16
18
import subprocess
17
19
import sys
18
20
import shutil
@@ -44,7 +46,7 @@ def configure_default_packages(self, variables, targets):
44
46
board_sdkconfig = variables .get ("board_espidf.custom_sdkconfig" , board_config .get ("espidf.custom_sdkconfig" , "" ))
45
47
frameworks = variables .get ("pioframework" , [])
46
48
47
- def install_tool (TOOL ):
49
+ def install_tool (TOOL , retry_count = 0 ):
48
50
self .packages [TOOL ]["optional" ] = False
49
51
TOOL_PATH = os .path .join (ProjectConfig .get_instance ().get ("platformio" , "packages_dir" ), TOOL )
50
52
TOOL_PACKAGE_PATH = os .path .join (TOOL_PATH , "package.json" )
@@ -80,13 +82,37 @@ def install_tool(TOOL):
80
82
try :
81
83
shutil .rmtree (TOOL_PATH )
82
84
except Exception as e :
83
- print (f"Error while removing the tool folder: { e } " )
85
+ print (f"Error while removing the tool folder: { e } " )
84
86
pm .install (tl_path )
85
87
# tool is already installed, just activate it
86
88
if tl_flag and pio_flag and not json_flag :
87
- self .packages [TOOL ]["version" ] = TOOL_PATH
88
- self .packages [TOOL ]["optional" ] = False
89
-
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
+
90
116
return
91
117
92
118
# Installer only needed for setup, deactivate when installed
@@ -130,7 +156,6 @@ def install_tool(TOOL):
130
156
# Set mandatory toolchains
131
157
for toolchain in toolchain_data ["toolchains" ]:
132
158
install_tool (toolchain )
133
-
134
159
# Set ULP toolchain if applicable
135
160
ulp_toolchain = toolchain_data .get ("ulp_toolchain" )
136
161
if ulp_toolchain and os .path .isdir ("ulp" ):
@@ -169,6 +194,13 @@ def install_tool(TOOL):
169
194
if "buildfs" in targets :
170
195
filesystem = variables .get ("board_build.filesystem" , "littlefs" )
171
196
if filesystem == "littlefs" :
197
+ # ensure use of mklittlefs 3.2.0
198
+ piopm_path = os .path .join (ProjectConfig .get_instance ().get ("platformio" , "packages_dir" ), "tool-mklittlefs" , ".piopm" )
199
+ if os .path .exists (piopm_path ):
200
+ with open (piopm_path , "r" ) as file :
201
+ package_data = json .load (file )
202
+ if package_data ['version' ] == "4.0.0" :
203
+ os .remove (piopm_path )
172
204
install_tool ("tool-mklittlefs" )
173
205
elif filesystem == "fatfs" :
174
206
install_tool ("tool-mkfatfs" )
@@ -179,9 +211,25 @@ def install_tool(TOOL):
179
211
filesystem = variables .get ("board_build.filesystem" , "littlefs" )
180
212
if filesystem == "littlefs" :
181
213
# Use Tasmota mklittlefs v4.0.0 to unpack, older version is incompatible
182
- self .packages ["tool-mklittlefs" ]["version" ] = "https://github.com/pioarduino/registry/releases/download/0.0.1/mklittlefs-4.0.0.zip"
183
- self .packages ["tool-mklittlefs" ]["optional" ] = False
184
- install_tool ("tool-mklittlefs" )
214
+ # make sure mklittlefs 3.2.0 is installed
215
+ mklittlefs_dir = os .path .join (ProjectConfig .get_instance ().get ("platformio" , "packages_dir" ), "tool-mklittlefs" )
216
+ if not os .path .exists (mklittlefs_dir ):
217
+ install_tool ("tool-mklittlefs" )
218
+ if os .path .exists (os .path .join (mklittlefs_dir , "tools.json" )):
219
+ install_tool ("tool-mklittlefs" )
220
+ mklittlefs400_dir = os .path .join (ProjectConfig .get_instance ().get ("platformio" , "packages_dir" ), "tool-mklittlefs-4.0.0" )
221
+ if not os .path .exists (mklittlefs400_dir ):
222
+ # install mklittlefs 4.0.0
223
+ install_tool ("tool-mklittlefs-4.0.0" )
224
+ if os .path .exists (os .path .join (mklittlefs400_dir , "tools.json" )):
225
+ install_tool ("tool-mklittlefs-4.0.0" )
226
+ # use mklittlefs 4.0.0 instead of 3.2.0 by copying over
227
+ if os .path .exists (mklittlefs400_dir ):
228
+ shutil .copyfile (
229
+ os .path .join (mklittlefs_dir , "package.json" ),
230
+ os .path .join (mklittlefs400_dir , "package.json" ),
231
+ )
232
+ shutil .copytree (mklittlefs400_dir , mklittlefs_dir , dirs_exist_ok = True )
185
233
186
234
# Currently only Arduino Nano ESP32 uses the dfuutil tool as uploader
187
235
if variables .get ("board" ) == "arduino_nano_esp32" :
0 commit comments