Skip to content

Commit cf50ff4

Browse files
committed
fix(get.py): Add version checking of installed tools
1 parent 4aab817 commit cf50ff4

File tree

1 file changed

+70
-10
lines changed

1 file changed

+70
-10
lines changed

tools/get.py

+70-10
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import re
2525
import time
2626
import argparse
27+
import subprocess
2728

2829
# Initialize start_time globally
2930
start_time = -1
@@ -146,6 +147,65 @@ def verify_files(filename, destination, rename_to):
146147

147148
return True
148149

150+
def is_latest_version(filename, destination, dirname, rename_to, cfile):
151+
# Regex to extract version number from any of the sources below
152+
regex = r'(?<![{,])(?:[ _\-vV])(\d+\.\d+(?:\.\d+)?)'
153+
current_version = None
154+
try:
155+
expected_version = re.search(regex, filename).group(1)
156+
except Exception as e:
157+
expected_version = None
158+
159+
try:
160+
if rename_to.startswith("esp32-arduino-libs"):
161+
# overwrite expected_version with the one from versions.txt
162+
expected_version = cfile.read(os.path.join(dirname, "versions.txt")).decode("utf-8")
163+
with open(os.path.join(destination, rename_to, "versions.txt"), "r") as f:
164+
# cfile is zip
165+
current_version = f.read()
166+
elif rename_to.startswith("mklittlefs"):
167+
# overwrite expected_version with the one from package.json
168+
expected_version = cfile.extractfile(os.path.join(dirname, "package.json")).read().decode("utf-8")
169+
with open(os.path.join(destination, rename_to, "package.json"), "r") as f:
170+
# cfile is tar.gz
171+
current_version = f.read()
172+
elif rename_to.startswith("esptool"):
173+
bin_path = os.path.join(destination, rename_to, "esptool")
174+
result = subprocess.run([bin_path, "--help"], text=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
175+
current_version = re.search(regex, result.stdout).group(1)
176+
else:
177+
if rename_to.startswith("xtensa-esp-elf-gdb"):
178+
bin_path = os.path.join(destination, rename_to, "bin/xtensa-esp32-elf-gdb")
179+
elif rename_to.startswith("riscv32-esp-elf-gdb"):
180+
bin_path = os.path.join(destination, rename_to, "bin/riscv32-esp-elf-gdb")
181+
elif rename_to.startswith("openocd"):
182+
bin_path = os.path.join(destination, rename_to, "bin/openocd")
183+
elif rename_to.startswith("mkspiffs"):
184+
bin_path = os.path.join(destination, rename_to, "mkspiffs")
185+
else:
186+
bin_path = os.path.join(destination, rename_to, "bin/" + rename_to + "-gcc")
187+
188+
result = subprocess.run([bin_path, "--version"], text=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
189+
current_version = re.search(regex, result.stdout).group(1)
190+
191+
if verbose:
192+
print(f"\nTool: {rename_to}")
193+
print(f"Current version: {current_version}")
194+
print(f"Expected version: {expected_version}")
195+
196+
if current_version and current_version == expected_version:
197+
if verbose:
198+
print("Latest version already installed. Skipping extraction")
199+
return True
200+
201+
if verbose:
202+
print("New version detected")
203+
204+
except Exception as e:
205+
if verbose:
206+
print(f"Falied to verify version for {rename_to}: {e}")
207+
208+
return False
149209

150210
def unpack(filename, destination, force_extract): # noqa: C901
151211
dirname = ""
@@ -196,11 +256,11 @@ def unpack(filename, destination, force_extract): # noqa: C901
196256
rename_to = "esp32-arduino-libs"
197257

198258
if not force_extract:
199-
if verify_files(filename, destination, rename_to):
200-
print(" Files ok. Skipping Extraction")
201-
return True
202-
else:
203-
print(" Extracting archive...")
259+
if is_latest_version(filename, destination, dirname, rename_to, cfile):
260+
if verify_files(filename, destination, rename_to):
261+
print(" Files ok. Skipping Extraction")
262+
return True
263+
print(" Extracting archive...")
204264
else:
205265
print(" Forcing extraction")
206266

@@ -379,21 +439,21 @@ def identify_platform():
379439
if __name__ == "__main__":
380440
parser = argparse.ArgumentParser(description="Download and extract tools")
381441

382-
parser.add_argument("-v", "--verbose", type=bool, default=False, required=False, help="Print verbose output")
442+
parser.add_argument("-v", "--verbose", action='store_true', required=False, help="Print verbose output")
383443

384444
parser.add_argument(
385-
"-d", "--force_download", type=bool, default=False, required=False, help="Force download of tools"
445+
"-d", "--force_download", action='store_true', required=False, help="Force download of tools"
386446
)
387447

388448
parser.add_argument(
389-
"-e", "--force_extract", type=bool, default=False, required=False, help="Force extraction of tools"
449+
"-e", "--force_extract", action='store_true', required=False, help="Force extraction of tools"
390450
)
391451

392452
parser.add_argument(
393-
"-f", "--force_all", type=bool, default=False, required=False, help="Force download and extraction of tools"
453+
"-f", "--force_all", action='store_true', required=False, help="Force download and extraction of tools"
394454
)
395455

396-
parser.add_argument("-t", "--test", type=bool, default=False, required=False, help=argparse.SUPPRESS)
456+
parser.add_argument("-t", "--test", action='store_true', required=False, help=argparse.SUPPRESS)
397457

398458
args = parser.parse_args()
399459

0 commit comments

Comments
 (0)