Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 47 additions & 18 deletions vinca/pinning.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,28 +87,57 @@ def get_pinning_distribution(version: str) -> str:
return _newest_pinning_record(f"{PINNING_PACKAGE}=={version}").url


def pinning_cache_dir() -> Path:
"""Where downloaded conda-forge-pinning versions are kept: ``$VINCA_CACHE_DIR``,
or ``vinca`` in the user's cache directory."""
if cache := os.environ.get("VINCA_CACHE_DIR"):
return Path(cache) / "conda-forge-pinning"
base = os.environ.get("XDG_CACHE_HOME") or Path.home() / ".cache"
return Path(base) / "vinca" / "conda-forge-pinning"


def _extract_pinning(url: str, dest: Path) -> None:
try:
asyncio.run(download_and_extract(rattler.Client(), url, dest))
except (ExtractError, OSError) as exc:
raise PinningError(f"Could not download {url}: {exc}") from exc
if not (dest / "conda_build_config.yaml").is_file():
raise PinningError("conda_build_config.yaml is missing from pinning artifact")


def _read_pinning(path: Path) -> tuple[bytes, dict[str, bytes]]:
migrations = {
migration.stem: migration.read_bytes()
for migration in (path / "share/conda-forge/migrations").glob("*.yaml")
}
return (path / "conda_build_config.yaml").read_bytes(), migrations


def download_pinning_package(
version: str, *, artifact_url: Optional[str] = None
) -> tuple[bytes, dict[str, bytes]]:
"""Download a versioned pinning artifact, or the supplied artifact URL, into its
base config and named migrations."""
url = artifact_url or get_pinning_distribution(version)
with tempfile.TemporaryDirectory() as tmp:
dest = Path(tmp)
try:
asyncio.run(download_and_extract(rattler.Client(), url, dest))
except (ExtractError, OSError) as exc:
raise PinningError(f"Could not download {url}: {exc}") from exc
base = dest / "conda_build_config.yaml"
if not base.is_file():
raise PinningError(
"conda_build_config.yaml is missing from pinning artifact"
)
migrations = {
path.stem: path.read_bytes()
for path in (dest / "share/conda-forge/migrations").glob("*.yaml")
}
return base.read_bytes(), migrations
base config and named migrations.

A version is downloaded once and kept in :func:`pinning_cache_dir` (a version's
contents don't change): rendering every distribution, or rendering again, needs
no download."""
if artifact_url:
with tempfile.TemporaryDirectory() as tmp:
_extract_pinning(artifact_url, Path(tmp))
return _read_pinning(Path(tmp))
cached = pinning_cache_dir() / version
if not (cached / "conda_build_config.yaml").is_file():
cached.parent.mkdir(parents=True, exist_ok=True)
# extracted next to the cache entry, then moved: never a partial entry
with tempfile.TemporaryDirectory(dir=cached.parent) as tmp:
_extract_pinning(get_pinning_distribution(version), Path(tmp) / "pkg")
try:
(Path(tmp) / "pkg").rename(cached)
except OSError:
if not (cached / "conda_build_config.yaml").is_file():
raise # not a concurrent download of the same version
return _read_pinning(cached)


def _pinning_spec_fields(
Expand Down
20 changes: 20 additions & 0 deletions vinca/test_pinning.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,26 @@ def test_download_pinning_package_that_does_not_exist(tmp_path):
download_pinning_package("1", artifact_url=url)


def test_download_pinning_package_downloads_a_version_once(tmp_path, monkeypatch):
url = _pinning_package(
tmp_path / "conda-forge-pinning-1-0.tar.bz2",
{
"conda_build_config.yaml": BASE_CONFIG,
"share/conda-forge/migrations/libboost190.yaml": BOOST_MIGRATION,
},
)
monkeypatch.setenv("VINCA_CACHE_DIR", str(tmp_path / "cache"))
with patch("vinca.pinning.get_pinning_distribution", return_value=url) as lookup:
first = download_pinning_package("1")
second = download_pinning_package("1")

assert first == second == (BASE_CONFIG, {"libboost190": BOOST_MIGRATION})
assert lookup.call_count == 1
assert (
tmp_path / "cache" / "conda-forge-pinning" / "1" / "conda_build_config.yaml"
).is_file()


def test_render_applies_base_migration_then_override(tmp_path):
config_path = tmp_path / "vinca_pinning.yaml"
config_path.write_text(
Expand Down
Loading