#!/usr/bin/env python3 """ Cache plugin for deb-mock Provides caching functionality for build artifacts and dependencies """ from deb_mock.plugins.base import BasePlugin from deb_mock.plugins.registry import PluginInfo import os import hashlib import shutil from pathlib import Path class CachePlugin(BasePlugin): """Plugin for caching build artifacts and dependencies""" def __init__(self, plugin_manager, config, deb_mock): super().__init__(plugin_manager, config, deb_mock) self.cache_dir = Path("/var/cache/deb-mock") self.artifact_cache = self.cache_dir / "artifacts" self.dependency_cache = self.cache_dir / "dependencies" def pre_build_hook(self, environment, package_info): """Check cache before building""" cache_key = self._generate_cache_key(package_info) cached_artifact = self.artifact_cache / f"{cache_key}.deb" if cached_artifact.exists(): self.logger.info(f"Found cached artifact: {cached_artifact}") return {"cached": True, "artifact": cached_artifact} return {"cached": False} def post_build_hook(self, environment, package_info, build_result): """Cache build artifacts after successful build""" if build_result.get("success"): cache_key = self._generate_cache_key(package_info) artifact_path = build_result.get("artifact_path") if artifact_path and os.path.exists(artifact_path): cached_artifact = self.artifact_cache / f"{cache_key}.deb" shutil.copy2(artifact_path, cached_artifact) self.logger.info(f"Cached artifact: {cached_artifact}") def _generate_cache_key(self, package_info): """Generate a cache key based on package information""" key_data = f"{package_info.get('name', '')}-{package_info.get('version', '')}-{package_info.get('architecture', '')}" return hashlib.md5(key_data.encode()).hexdigest() # Plugin registration PLUGIN_INFO = PluginInfo( name="cache", version="1.0.0", description="Caching plugin for build artifacts and dependencies", author="Deb-Mock Team", plugin_class=CachePlugin )