From 60b5f6620d206f3dfb24d162dbfe14e078914a41 Mon Sep 17 00:00:00 2001 From: Nicola Soranzo Date: Mon, 15 Apr 2024 18:41:16 +0100 Subject: [PATCH] Use iterators --- lib/tool_shed/test/base/populators.py | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/lib/tool_shed/test/base/populators.py b/lib/tool_shed/test/base/populators.py index e1d3364573d7..4b2d6cc70185 100644 --- a/lib/tool_shed/test/base/populators.py +++ b/lib/tool_shed/test/base/populators.py @@ -2,6 +2,7 @@ from pathlib import Path from tempfile import NamedTemporaryFile from typing import ( + Iterator, List, Optional, Union, @@ -56,28 +57,21 @@ TEST_DATA_REPO_FILES = resource_path(__package__, "../test_data") -def repo_files(test_data_path: str) -> List[Path]: +def repo_files(test_data_path: str) -> Iterator[Path]: repos = TEST_DATA_REPO_FILES.joinpath(f"repos/{test_data_path}") - paths = [] - for trav in repos.iterdir(): - with as_file(trav) as path: - paths.append(path) - return sorted(paths) + for child in sorted(_.name for _ in repos.iterdir()): + with as_file(repos.joinpath(child)) as path: + yield path -def repo_tars(test_data_path: str) -> List[Path]: - tar_paths = [] +def repo_tars(test_data_path: str) -> Iterator[Path]: for path in repo_files(test_data_path): - if path.is_dir(): - prefix = f"shedtest_{test_data_path}_{path.name}_" - tf = NamedTemporaryFile(delete=False, prefix=prefix) + assert path.is_dir() + prefix = f"shedtest_{test_data_path}_{path.name}_" + with NamedTemporaryFile(prefix=prefix) as tf: with tarfile.open(tf.name, "w:gz") as tar: tar.add(str(path.absolute()), arcname=test_data_path or path.name) - tar_path = Path(tf.name) - else: - tar_path = path - tar_paths.append(tar_path) - return tar_paths + yield Path(tf.name) class HostsTestToolShed(Protocol):