Skip to content

Commit 86730fc

Browse files
committed
Move to using checksum
1 parent 78b687b commit 86730fc

File tree

1 file changed

+12
-41
lines changed

1 file changed

+12
-41
lines changed

tools/get.py

+12-41
Original file line numberDiff line numberDiff line change
@@ -147,52 +147,20 @@ def verify_files(filename, destination, rename_to):
147147

148148
return True
149149

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+)?)'
150+
def is_latest_version(filename, destination, dirname, rename_to, cfile, checksum):
153151
current_version = None
154-
try:
155-
expected_version = re.search(regex, filename).group(1)
156-
except Exception as e:
157-
expected_version = None
152+
expected_version = None
158153

159154
try:
160155
if rename_to.startswith("esp32-arduino-libs"):
161-
# overwrite expected_version with the one from versions.txt
162-
if filename.endswith("tar.gz") or filename.endswith("tar.xz"):
163-
expected_version = cfile.extractfile(dirname + "/versions.txt").read().decode("utf-8")
164-
else:
165-
expected_version = cfile.read(dirname + "/versions.txt").decode("utf-8")
156+
expected_version = cfile.read(dirname + "/versions.txt").decode("utf-8")
166157
with open(os.path.join(destination, rename_to, "versions.txt"), "r") as f:
167158
# cfile is zip
168159
current_version = f.read()
169-
elif rename_to.startswith("mklittlefs"):
170-
# overwrite expected_version with the one from package.json
171-
if filename.endswith("tar.gz") or filename.endswith("tar.xz"):
172-
expected_version = cfile.extractfile(dirname + "/package.json").read().decode("utf-8")
173-
else:
174-
expected_version = cfile.read(dirname + "/package.json").decode("utf-8")
175-
with open(os.path.join(destination, rename_to, "package.json"), "r") as f:
176-
# cfile is tar.gz
177-
current_version = f.read()
178-
elif rename_to.startswith("esptool"):
179-
bin_path = os.path.join(destination, rename_to, "esptool")
180-
result = subprocess.run([bin_path, "--help"], text=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
181-
current_version = re.search(regex, result.stdout).group(1)
182160
else:
183-
if rename_to.startswith("xtensa-esp-elf-gdb"):
184-
bin_path = os.path.join(destination, rename_to, "bin", "xtensa-esp32-elf-gdb")
185-
elif rename_to.startswith("riscv32-esp-elf-gdb"):
186-
bin_path = os.path.join(destination, rename_to, "bin", "riscv32-esp-elf-gdb")
187-
elif rename_to.startswith("openocd"):
188-
bin_path = os.path.join(destination, rename_to, "bin", "openocd")
189-
elif rename_to.startswith("mkspiffs"):
190-
bin_path = os.path.join(destination, rename_to, "mkspiffs")
191-
else:
192-
bin_path = os.path.join(destination, rename_to, "bin", rename_to + "-gcc")
193-
194-
result = subprocess.run([bin_path, "--version"], text=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
195-
current_version = re.search(regex, result.stdout).group(1)
161+
expected_version = checksum
162+
with open(os.path.join(destination, rename_to, ".package_checksum"), "r") as f:
163+
current_version = f.read()
196164

197165
if verbose:
198166
print(f"\nTool: {rename_to}")
@@ -213,7 +181,7 @@ def is_latest_version(filename, destination, dirname, rename_to, cfile):
213181

214182
return False
215183

216-
def unpack(filename, destination, force_extract): # noqa: C901
184+
def unpack(filename, destination, force_extract, checksum): # noqa: C901
217185
dirname = ""
218186
cfile = None # Compressed file
219187
file_is_corrupted = False
@@ -262,7 +230,7 @@ def unpack(filename, destination, force_extract): # noqa: C901
262230
rename_to = "esp32-arduino-libs"
263231

264232
if not force_extract:
265-
if is_latest_version(filename, destination, dirname, rename_to, cfile):
233+
if is_latest_version(filename, destination, dirname, rename_to, cfile, checksum):
266234
if verify_files(filename, destination, rename_to):
267235
print(" Files ok. Skipping Extraction")
268236
return True
@@ -291,6 +259,9 @@ def unpack(filename, destination, force_extract): # noqa: C901
291259
shutil.rmtree(rename_to)
292260
shutil.move(dirname, rename_to)
293261

262+
with open(os.path.join(destination, rename_to, ".package_checksum"), "w") as f:
263+
f.write(checksum)
264+
294265
if verify_files(filename, destination, rename_to):
295266
print(" Files extracted successfully.")
296267
return True
@@ -394,7 +365,7 @@ def get_tool(tool, force_download, force_extract):
394365
print("Checksum mismatch for {0}".format(archive_name))
395366
return False
396367

397-
return unpack(local_path, ".", force_extract)
368+
return unpack(local_path, ".", force_extract, checksum)
398369

399370

400371
def load_tools_list(filename, platform):

0 commit comments

Comments
 (0)