Debian Codegraph @

tests/conftest.py

raw
1 import json
2 import logging
3 import os.path
4 import shutil
5 import subprocess
6 from pathlib import Path
7
8 import pytest
9 import server
10
11 from conda_package_streaming.transmute import transmute_tar_bz2
12
13 log = logging.getLogger(__name__)
14
15
16 LIMIT_TEST_PACKAGES = 16
17
18
19 def find_packages_dirs() -> Path:
20 """
21 Ask conda for package directories.
22 """
23 conda_info = json.loads(
24 subprocess.run(
25 [os.environ["CONDA_EXE"], "info", "--json"],
26 stdout=subprocess.PIPE,
27 check=True,
28 ).stdout
29 )
30
31 # XXX can run individual environment's conda (base conda is more likely to
32 # have useful cached packages)
33 pkgs_dirs = conda_info["pkgs_dirs"] + [os.path.expanduser("~/miniconda3/pkgs")]
34
35 log.debug("search %s", pkgs_dirs)
36
37 first_pkg_dir = next(path for path in pkgs_dirs if os.path.exists(path))
38
39 return Path(first_pkg_dir)
40
41
42 @pytest.fixture(scope="session")
43 def pkgs_dir(tmp_path_factory):
44 """
45 Dedicated test package directory.
46 """
47 return tmp_path_factory.mktemp("pkgs")
48
49
50 @pytest.fixture(scope="session")
51 def package_server(pkgs_dir, conda_paths):
52 thread = server.get_server_thread(pkgs_dir)
53 thread.start()
54 return thread
55
56
57 @pytest.fixture(scope="session")
58 def conda_paths(pkgs_dir: Path):
59 found_packages = find_packages_dirs()
60 conda_paths = []
61 for path in found_packages.iterdir():
62 if path.name.endswith((".tar.bz2", ".conda")):
63 conda_paths.append(path)
64
65 return add_tar_bz2s(conda_paths, pkgs_dir)
66
67
68 def add_tar_bz2s(paths: list[Path], pkgs_dir: Path):
69 """
70 If there aren't enough .tar.bz2's available, create some from available
71 .conda's. Return paths.
72 """
73 conda_paths: list[Path] = []
74 tarbz2_paths: list[Path] = []
75 output_paths: list[Path] = []
76
77 assert isinstance(pkgs_dir, Path)
78
79 for path in paths:
80 if path.name.endswith(".tar.bz2"):
81 tarbz2_paths.append(path)
82 elif path.name.endswith(".conda"):
83 conda_paths.append(path)
84
85 tarbz2_path: Path = pkgs_dir
86
87 medium_conda_paths = []
88 for path in conda_paths:
89 if 1 << 20 < path.stat().st_size < 1 << 22:
90 medium_conda_paths.append(path)
91 medium_conda_paths = medium_conda_paths[:LIMIT_TEST_PACKAGES]
92
93 # this ignores existing .tar.bz2 for simplicity (.tar.bz2 is missing in CI)
94 for conda in set(medium_conda_paths + conda_paths[:10]):
95 shutil.copy(conda, tarbz2_path)
96 transmute_tar_bz2(str(conda), tarbz2_path)
97
98 output_paths.extend(tarbz2_path.glob("*.tar.bz2"))
99 output_paths.extend(tarbz2_path.glob("*.conda"))
100
101 return sorted(output_paths) # sort interleaves .tar.bz2 and .conda