Download and cache an artifact from the given URL. Returns the absolute path to the cached zip file.
(self, url: str)
| 383 | return str(sanitize_url_for_path(url)) |
| 384 | |
| 385 | def download_artifact(self, url: str) -> str: |
| 386 | """ |
| 387 | Download and cache an artifact from the given URL. |
| 388 | Returns the absolute path to the cached zip file. |
| 389 | """ |
| 390 | cache_key = self._get_cache_key(url) |
| 391 | # Each artifact gets its own directory |
| 392 | artifact_dir = self.cache_dir / cache_key |
| 393 | artifact_dir.mkdir(parents=True, exist_ok=True) |
| 394 | cached_path = artifact_dir / "artifact.zip" |
| 395 | |
| 396 | # Use write locking for concurrent safety |
| 397 | from ci.util.file_lock_rw import custom_lock |
| 398 | |
| 399 | lock_path = artifact_dir / "artifact.lock" |
| 400 | with custom_lock(lock_path, timeout=60.0, operation="artifact_download"): |
| 401 | if cached_path.exists(): |
| 402 | # Check if processing was completed successfully |
| 403 | status_file = _get_status_file(artifact_dir, cache_key) |
| 404 | if not _is_processing_complete(status_file): |
| 405 | logger.warning( |
| 406 | f"Cache incomplete (no completion status), re-downloading: {url}" |
| 407 | ) |
| 408 | # Remove incomplete zip file |
| 409 | cached_path.unlink() |
| 410 | # Also remove any partial extraction |
| 411 | extracted_dir = artifact_dir / "extracted" |
| 412 | if extracted_dir.exists(): |
| 413 | shutil.rmtree(extracted_dir) |
| 414 | else: |
| 415 | print(f"Using cached artifact: {cached_path}") |
| 416 | # Cache hit - return the cached path (write lock will be released) |
| 417 | return str(cached_path) |
| 418 | |
| 419 | # Check URL scheme to determine action |
| 420 | parsed_url = urllib.parse.urlparse(url) |
| 421 | |
| 422 | if parsed_url.scheme in ("http", "https"): |
| 423 | # Download to temporary file first (atomic operation) |
| 424 | file_size = _get_remote_file_size(url) |
| 425 | size_str = _format_file_size(file_size) |
| 426 | print(f"Downloading: {url} ({size_str})") |
| 427 | temp_file_handle = tempfile.NamedTemporaryFile( |
| 428 | delete=False, suffix=".zip" |
| 429 | ) |
| 430 | temp_path = Path(temp_file_handle.name) |
| 431 | temp_file_handle.close() # Close immediately to avoid Windows file locking issues |
| 432 | |
| 433 | # Create a thread-local cancel event |
| 434 | thread_cancel_event = threading.Event() |
| 435 | |
| 436 | # Download directly (we're already in a thread from the main pool) |
| 437 | download_result = _download_with_progress( |
| 438 | url, temp_path, thread_cancel_event |
| 439 | ) |
| 440 | elif parsed_url.scheme == "file": |
| 441 | # File URL - copy from local file |
| 442 | print(f"Copying from local file: {url}") |
no test coverage detected