Acquire lock for a specific cache artifact directory. Provides fine-grained locking per toolchain/package URL. Multiple different packages can download concurrently. Args: cache_dir: Global cache directory url: URL being downloaded/accessed operation: Opera
(
cache_dir: Path, url: str, operation: str = "download", timeout: float = 5.0
)
| 40 | |
| 41 | |
| 42 | def acquire_artifact_lock( |
| 43 | cache_dir: Path, url: str, operation: str = "download", timeout: float = 5.0 |
| 44 | ) -> FileLock: |
| 45 | """ |
| 46 | Acquire lock for a specific cache artifact directory. |
| 47 | |
| 48 | Provides fine-grained locking per toolchain/package URL. |
| 49 | Multiple different packages can download concurrently. |
| 50 | |
| 51 | Args: |
| 52 | cache_dir: Global cache directory |
| 53 | url: URL being downloaded/accessed |
| 54 | operation: Operation name (download/delete/update) for debugging |
| 55 | timeout: Lock timeout in seconds (default: 5.0) |
| 56 | |
| 57 | Returns: |
| 58 | FileLock context manager with stale lock recovery |
| 59 | |
| 60 | Example: |
| 61 | with acquire_artifact_lock(cache_dir, url, operation="download") as lock: |
| 62 | # Download inside lock |
| 63 | download_file(url, dest) |
| 64 | # Write breadcrumb inside lock |
| 65 | write_breadcrumb(path, data) |
| 66 | """ |
| 67 | # Get artifact directory for this URL |
| 68 | cache_key = str(sanitize_url_for_path(url)) |
| 69 | artifact_dir = cache_dir / cache_key |
| 70 | |
| 71 | # Ensure artifact directory exists |
| 72 | artifact_dir.mkdir(parents=True, exist_ok=True) |
| 73 | |
| 74 | # Use download_lock for consistency |
| 75 | lock_file = artifact_dir / ".download.lock" |
| 76 | return FileLock(lock_file, timeout=timeout, operation=operation) |
| 77 | |
| 78 | |
| 79 | def force_unlock_cache(cache_dir: Path) -> int: |