Skip to content

Commit e53f262

Browse files
committed
tools/mpremote: For mip install, use hash to skip files that exist.
When using `mip install`, if a file that needs to be downloaded already exists locally, then the hash of that local file will be computed and if it matches the known hash of the remote file it will not be downloaded. Hashes in mip are guaranteed unique, so this change should never leave stale files on the filesystem. This behaviour follows that of the `mip` package in `micropython-lib`. Signed-off-by: Damien George <[email protected]>
1 parent 4117a2d commit e53f262

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

tools/mpremote/mpremote/mip.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ def _ensure_path_exists(transport, path):
3434
prefix += "/"
3535

3636

37+
# Check if the specified path exists and matches the hash.
38+
def _check_exists(transport, path, short_hash):
39+
try:
40+
remote_hash = transport.fs_hashfile(path, "sha256")
41+
except FileNotFoundError:
42+
return False
43+
return remote_hash.hex()[: len(short_hash)] == short_hash
44+
45+
3746
def _rewrite_url(url, branch=None):
3847
if not branch:
3948
branch = "HEAD"
@@ -115,8 +124,11 @@ def _install_json(transport, package_json_url, index, target, version, mpy):
115124
raise CommandError(f"Invalid url for package: {package_json_url}")
116125
for target_path, short_hash in package_json.get("hashes", ()):
117126
fs_target_path = target + "/" + target_path
118-
file_url = f"{index}/file/{short_hash[:2]}/{short_hash}"
119-
_download_file(transport, file_url, fs_target_path)
127+
if _check_exists(transport, fs_target_path, short_hash):
128+
print("Exists:", fs_target_path)
129+
else:
130+
file_url = f"{index}/file/{short_hash[:2]}/{short_hash}"
131+
_download_file(transport, file_url, fs_target_path)
120132
for target_path, url in package_json.get("urls", ()):
121133
fs_target_path = target + "/" + target_path
122134
if base_url and not url.startswith(allowed_mip_url_prefixes):

0 commit comments

Comments
 (0)