Load lockfile data from disk.
(self)
| 40 | self._data: Optional[dict[str, Any]] = None |
| 41 | |
| 42 | def _load(self) -> dict[str, Any]: |
| 43 | """Load lockfile data from disk.""" |
| 44 | if self._data is not None: |
| 45 | return self._data |
| 46 | |
| 47 | if not self.lockfile_path.exists(): |
| 48 | self._data = { |
| 49 | "version": "1.0", |
| 50 | "created_at": datetime.now().isoformat(), |
| 51 | "packages": {}, |
| 52 | } |
| 53 | return self._data |
| 54 | |
| 55 | try: |
| 56 | with open(self.lockfile_path, "r") as f: |
| 57 | data = json.load(f) |
| 58 | assert isinstance(data, dict), "Lockfile must contain a JSON object" |
| 59 | self._data = cast(dict[str, Any], data) |
| 60 | return self._data |
| 61 | except (json.JSONDecodeError, OSError) as e: |
| 62 | logger.warning(f"Failed to load lockfile {self.lockfile_path}: {e}") |
| 63 | logger.warning("Creating new lockfile") |
| 64 | self._data = { |
| 65 | "version": "1.0", |
| 66 | "created_at": datetime.now().isoformat(), |
| 67 | "packages": {}, |
| 68 | } |
| 69 | return self._data |
| 70 | |
| 71 | def _save(self) -> bool: |
| 72 | """Save lockfile data to disk with locking.""" |
no test coverage detected