Enhanced cache manager for PlatformIO artifacts.
| 363 | |
| 364 | |
| 365 | class PlatformIOCache: |
| 366 | """Enhanced cache manager for PlatformIO artifacts.""" |
| 367 | |
| 368 | def __init__(self, cache_dir: Path): |
| 369 | """Initialize cache manager with directory structure.""" |
| 370 | self.cache_dir = cache_dir |
| 371 | # Simplified structure: each artifact gets its own directory directly in cache root |
| 372 | # containing both the .zip and extracted/ folder |
| 373 | |
| 374 | # Create directory structure |
| 375 | self.cache_dir.mkdir(parents=True, exist_ok=True) |
| 376 | |
| 377 | # Proactively clean up stale PlatformIO locks before starting builds |
| 378 | # This prevents hangs from previous crashed builds |
| 379 | cleanup_stale_platformio_locks(max_age_minutes=30) |
| 380 | |
| 381 | def _get_cache_key(self, url: str) -> str: |
| 382 | """Generate cache key from URL - sanitized for filesystem use.""" |
| 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"): |
no outgoing calls