Skip to content

Commit 46807ba

Browse files
authored
fix(cache-downloader): Skip re-downloading already cached toolkit versions (#84)
1 parent 9745351 commit 46807ba

2 files changed

Lines changed: 54 additions & 2 deletions

File tree

dockerfiles/cache-downloader/cache-downloader.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ def download_dependency(
1919
version_path = os.path.join(BASE_PATH, release_name, f"python{python_version}")
2020
done_file = os.path.join(version_path, f"{python_version}-done")
2121

22+
if Path(done_file).is_file():
23+
print(
24+
f"{datetime.datetime.now()}: {release_name} python{python_version} already cached, skipping download"
25+
)
26+
return
27+
2228
# Create the version directory if it doesn't exist
2329
os.makedirs(version_path, exist_ok=True)
2430

@@ -57,7 +63,7 @@ def download_dependency(
5763
f"{datetime.datetime.now()}: Done downloading {release_name} {s3_path} and extracting to {version_path}"
5864
)
5965
# Create the "done" file
60-
open(done_file, "a").close()
66+
Path(done_file).touch()
6167

6268

6369
def submit_downloading(

tests/unit/test_cache_downloader.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import importlib.util
22
import os
33
import shutil
4-
from unittest.mock import patch
4+
from unittest.mock import MagicMock, patch
55

66
import pytest
77

@@ -21,6 +21,7 @@
2121
_spec.loader.exec_module(_mod)
2222

2323
cleanup_old_versions = _mod.cleanup_old_versions
24+
download_dependency = _mod.download_dependency
2425

2526

2627
class TestCleanupOldVersions:
@@ -169,3 +170,48 @@ def _fail_on_v1(path, *args, **kwargs):
169170
assert (tmp_path / "v1").exists()
170171
assert (tmp_path / "v2").exists()
171172
assert (tmp_path / "v3").exists()
173+
174+
175+
class TestDownloadDependency:
176+
def test_skips_download_when_done_file_exists(self, tmp_path):
177+
"""Already-cached version should not trigger a download."""
178+
release = "v1.0.0"
179+
py_ver = "3.11"
180+
version_path = tmp_path / release / f"python{py_ver}"
181+
version_path.mkdir(parents=True)
182+
(version_path / f"{py_ver}-done").write_text("ok")
183+
184+
with (
185+
patch.object(_mod, "BASE_PATH", str(tmp_path)),
186+
patch.object(_mod.subprocess, "Popen") as mock_popen,
187+
):
188+
download_dependency(release, py_ver, "fake-bucket")
189+
190+
mock_popen.assert_not_called()
191+
192+
def test_downloads_when_done_file_missing(self, tmp_path):
193+
"""Missing done file should trigger the download."""
194+
release = "v1.0.0"
195+
py_ver = "3.11"
196+
version_path = tmp_path / release / f"python{py_ver}"
197+
version_path.mkdir(parents=True)
198+
199+
mock_aws = MagicMock()
200+
mock_aws.stdout = MagicMock()
201+
mock_aws.stderr = MagicMock(read=MagicMock(return_value=b""))
202+
mock_aws.wait.return_value = 0
203+
204+
mock_tar = MagicMock()
205+
mock_tar.communicate.return_value = (b"", b"")
206+
mock_tar.returncode = 0
207+
208+
with (
209+
patch.object(_mod, "BASE_PATH", str(tmp_path)),
210+
patch.object(
211+
_mod.subprocess, "Popen", side_effect=[mock_aws, mock_tar]
212+
) as mock_popen,
213+
):
214+
download_dependency(release, py_ver, "fake-bucket")
215+
216+
assert mock_popen.call_count == 2
217+
assert (version_path / f"{py_ver}-done").exists()

0 commit comments

Comments
 (0)