Skip to content

Commit 87ef606

Browse files
Issue(medcat):CU-869c570zv stability on windows and macos (#338)
* CU-869c570zv: Add teardown for temporary folder * CU-869c570zv: Use a temporary directory instead of a file for Windows compatibility * CU-869c570zv: Fix issue with zip downloading logic with changed codebase * CU-869c570zv: Add further moclk for finding latest tag * CU-869c570zv: Run stability workflow temporarily * Revert "CU-869c570zv: Run stability workflow temporarily" This reverts commit 7d2147b. --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 3da6805 commit 87ef606

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

medcat-v2/medcat/utils/download_scripts.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import importlib.metadata
99
import tempfile
1010
import zipfile
11+
import os
1112
import sys
1213
from pathlib import Path
1314
import requests
@@ -77,12 +78,13 @@ def _determine_url(overwrite_url: str | None,
7778
return zip_url
7879

7980

80-
def _download_zip(zip_url: str, tmp: tempfile._TemporaryFileWrapper):
81-
with requests.get(zip_url, stream=True, timeout=30) as r:
82-
r.raise_for_status()
83-
for chunk in r.iter_content(chunk_size=8192):
84-
tmp.write(chunk)
85-
tmp.flush()
81+
def _download_zip(zip_url: str, tmp_path: str):
82+
with open(tmp_path, 'wb') as tmp:
83+
with requests.get(zip_url, stream=True, timeout=30) as r:
84+
r.raise_for_status()
85+
for chunk in r.iter_content(chunk_size=8192):
86+
tmp.write(chunk)
87+
tmp.flush()
8688

8789

8890
def _extract_zip(dest: Path, zip_path: Path):
@@ -146,9 +148,10 @@ def fetch_scripts(destination: str | Path = ".",
146148
dest.mkdir(parents=True, exist_ok=True)
147149

148150
zip_url = _determine_url(overwrite_url, overwrite_tag)
149-
with tempfile.NamedTemporaryFile() as tmp:
150-
_download_zip(zip_url, tmp)
151-
_extract_zip(dest, Path(tmp.name))
151+
with tempfile.TemporaryDirectory() as tmp_dir:
152+
zip_path = os.path.join(tmp_dir, 'downloaded_scripts.zip')
153+
_download_zip(zip_url, zip_path)
154+
_extract_zip(dest, Path(zip_path))
152155
_fix_requirements(dest, _get_medcat_version())
153156
logger.info(
154157
"You also need to install the requiements by doing:\n"

medcat-v2/tests/utils/test_download_scripts.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,16 @@ def setUpClass(cls):
1515
with unittest.mock.patch(
1616
"medcat.utils.download_scripts._get_medcat_version"
1717
) as mock_get_version:
18-
mock_get_version.return_value = cls.use_version
19-
cls.scripts_path = download_scripts.fetch_scripts(cls._temp_dir.name)
18+
with unittest.mock.patch(
19+
"medcat.utils.download_scripts._find_latest_scripts_tag"
20+
) as mock_get_tag:
21+
mock_get_version.return_value = cls.use_version
22+
mock_get_tag.return_value = f"medcat/v{cls.use_version}"
23+
cls.scripts_path = download_scripts.fetch_scripts(cls._temp_dir.name)
24+
25+
@classmethod
26+
def tearDownClass(cls):
27+
cls._temp_dir.cleanup()
2028

2129
def test_can_download(self):
2230
self.assertTrue(os.path.exists(self.scripts_path))

0 commit comments

Comments
 (0)