Refactor: Allow modifiable download dest path

This adds the parameter `archive_dest` to `run_installer`, which will
control where `helper.downloadBinaryFile` will download files, and sets
it to "." by default.

For convenience/readability, this will also turn any reference to the
downloaded file into a `pathlib.Path` object. This makes it easier to
ensure that the file ends up in the right location.
This commit is contained in:
David Dalcino
2021-11-24 13:29:20 -08:00
parent 60018b3bf6
commit 3af9c97ba0
4 changed files with 18 additions and 12 deletions

View File

@@ -79,7 +79,7 @@ def getUrl(url: str, timeout) -> str:
return result
def downloadBinaryFile(url: str, out: str, hash_algo: str, exp: bytes, timeout):
def downloadBinaryFile(url: str, out: Path, hash_algo: str, exp: bytes, timeout):
logger = getLogger("aqt.helper")
filename = Path(url).name
with requests.Session() as session:
@@ -114,7 +114,7 @@ def downloadBinaryFile(url: str, out: str, hash_algo: str, exp: bytes, timeout):
raise ArchiveChecksumError(
f"Downloaded file {filename} is corrupted! Detect checksum error.\n"
f"Expect {exp.hex()}: {url}\n"
f"Actual {hash.digest().hex()}: {out}"
f"Actual {hash.digest().hex()}: {out.name}"
)

View File

@@ -33,6 +33,8 @@ import sys
import time
from logging import getLogger
from logging.handlers import QueueHandler
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import Any, Callable, List, Optional
import aqt
@@ -838,14 +840,16 @@ class Cli:
return function(fallback_url)
def run_installer(archives: List[QtPackage], base_dir: str, sevenzip: Optional[str], keep: bool):
def run_installer(
archives: List[QtPackage], base_dir: str, sevenzip: Optional[str], keep: bool, archive_dest: Path = Path(".")
):
queue = multiprocessing.Manager().Queue(-1)
listener = MyQueueListener(queue)
listener.start()
#
tasks = []
for arc in archives:
tasks.append((arc, base_dir, sevenzip, queue, keep))
tasks.append((arc, base_dir, sevenzip, queue, archive_dest, keep))
ctx = multiprocessing.get_context("spawn")
pool = ctx.Pool(Settings.concurrency, init_worker_sh)
@@ -896,6 +900,7 @@ def installer(
base_dir: str,
command: Optional[str],
queue: multiprocessing.Queue,
archive_dest: Path,
keep: bool = False,
response_timeout: Optional[int] = None,
):
@@ -906,7 +911,7 @@ def installer(
name = qt_archive.name
url = qt_archive.archive_url
hashurl = qt_archive.hashurl
archive = qt_archive.archive
archive: Path = archive_dest / qt_archive.archive
start_time = time.perf_counter()
# set defaults
Settings.load_settings()
@@ -943,10 +948,10 @@ def installer(
"-bd",
"-y",
"-o{}".format(base_dir),
archive,
str(archive),
]
else:
command_args = [command, "x", "-aoa", "-bd", "-y", archive]
command_args = [command, "x", "-aoa", "-bd", "-y", str(archive)]
try:
proc = subprocess.run(command_args, stdout=subprocess.PIPE, check=True)
logger.debug(proc.stdout)
@@ -955,7 +960,7 @@ def installer(
raise ArchiveExtractionError(msg) from cpe
if not keep:
os.unlink(archive)
logger.info("Finished installation of {} in {:.8f}".format(archive, time.perf_counter() - start_time))
logger.info("Finished installation of {} in {:.8f}".format(archive.name, time.perf_counter() - start_time))
qh.flush()
qh.close()
logger.removeHandler(qh)

View File

@@ -127,10 +127,10 @@ def test_helper_downloadBinary_wrong_checksum(tmp_path, monkeypatch):
expected_err = (
f"Downloaded file test.xml is corrupted! Detect checksum error."
f"\nExpect {wrong_hash.hex()}: {url}"
f"\nActual {actual_hash.hex()}: {out}"
f"\nActual {actual_hash.hex()}: {out.name}"
)
with pytest.raises(ArchiveChecksumError) as e:
helper.downloadBinaryFile(url, str(out), "md5", wrong_hash, 60)
helper.downloadBinaryFile(url, out, "md5", wrong_hash, 60)
assert e.type == ArchiveChecksumError
assert format(e.value) == expected_err

View File

@@ -146,11 +146,11 @@ def make_mock_geturl_download_archive(
def locate_archive() -> MockArchive:
for arc in archives:
if out == arc.filename_7z:
if Path(out).name == arc.filename_7z:
return arc
assert False, "Requested an archive that was not mocked"
locate_archive().write_compressed_archive(Path("./"))
locate_archive().write_compressed_archive(Path(out).parent)
return mock_getUrl, mock_download_archive
@@ -731,6 +731,7 @@ def test_install_installer_archive_extraction_err(monkeypatch):
base_dir=temp_dir,
command="some_nonexistent_7z_extractor",
queue=MockMultiprocessingManager.Queue(),
archive_dest=Path(temp_dir),
)
assert err.type == ArchiveExtractionError
err_msg = format(err.value).rstrip()