spine.config.download.download_from_url

spine.config.download.download_from_url(url: str, expected_hash: str | None = None, cache_dir: Path | None = None, max_wait_seconds: int = 3600) str[source]

Download a file from URL and return the cached path.

If the file already exists in cache and passes hash validation (if provided), returns the cached path without re-downloading.

This function is safe for concurrent access from multiple processes. It uses file locking to ensure only one process downloads at a time, while others wait.

Parameters:
  • url (str) – URL to download from

  • expected_hash (str, optional) – Expected SHA256 hash of file for validation

  • cache_dir (Path, optional) – Directory to cache downloaded files (default: from get_cache_dir())

  • max_wait_seconds (int, optional) – Maximum time to wait for lock acquisition in seconds (default: 3600)

Returns:

Absolute path to cached file

Return type:

str

Raises:
  • HTTPError – If download fails with HTTP error

  • URLError – If download fails with URL error

  • ValueError – If hash validation fails

  • TimeoutError – If unable to acquire lock within max_wait_seconds

Examples

>>> path = download_from_url(
...     "https://example.com/model.ckpt",
...     expected_hash="abc123..."
... )
>>> print(f"Model downloaded to: {path}")