|
24 | 24 | import re
|
25 | 25 | import time
|
26 | 26 | import argparse
|
| 27 | +import subprocess |
27 | 28 |
|
28 | 29 | # Initialize start_time globally
|
29 | 30 | start_time = -1
|
@@ -146,6 +147,65 @@ def verify_files(filename, destination, rename_to):
|
146 | 147 |
|
147 | 148 | return True
|
148 | 149 |
|
| 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 |
149 | 209 |
|
150 | 210 | def unpack(filename, destination, force_extract): # noqa: C901
|
151 | 211 | dirname = ""
|
@@ -196,11 +256,11 @@ def unpack(filename, destination, force_extract): # noqa: C901
|
196 | 256 | rename_to = "esp32-arduino-libs"
|
197 | 257 |
|
198 | 258 | 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...") |
204 | 264 | else:
|
205 | 265 | print(" Forcing extraction")
|
206 | 266 |
|
@@ -379,21 +439,21 @@ def identify_platform():
|
379 | 439 | if __name__ == "__main__":
|
380 | 440 | parser = argparse.ArgumentParser(description="Download and extract tools")
|
381 | 441 |
|
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") |
383 | 443 |
|
384 | 444 | 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" |
386 | 446 | )
|
387 | 447 |
|
388 | 448 | 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" |
390 | 450 | )
|
391 | 451 |
|
392 | 452 | 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" |
394 | 454 | )
|
395 | 455 |
|
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) |
397 | 457 |
|
398 | 458 | args = parser.parse_args()
|
399 | 459 |
|
|
0 commit comments