|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 5 | +import dataclasses |
5 | 6 | import logging |
6 | 7 | import pathlib |
7 | 8 | import typing as t |
|
14 | 15 | logger = logging.getLogger(__name__) |
15 | 16 |
|
16 | 17 |
|
| 18 | +@dataclasses.dataclass |
| 19 | +class SyncError: |
| 20 | + """An error encountered during a sync step. |
| 21 | +
|
| 22 | + Examples |
| 23 | + -------- |
| 24 | + >>> error = SyncError(step="fetch", message="remote not found") |
| 25 | + >>> error.step |
| 26 | + 'fetch' |
| 27 | + >>> error.message |
| 28 | + 'remote not found' |
| 29 | + >>> error.exception is None |
| 30 | + True |
| 31 | + """ |
| 32 | + |
| 33 | + step: str |
| 34 | + message: str |
| 35 | + exception: Exception | None = None |
| 36 | + |
| 37 | + |
| 38 | +@dataclasses.dataclass |
| 39 | +class SyncResult: |
| 40 | + """Result of a repository synchronization. |
| 41 | +
|
| 42 | + Examples |
| 43 | + -------- |
| 44 | + >>> result = SyncResult() |
| 45 | + >>> result.ok |
| 46 | + True |
| 47 | + >>> bool(result) |
| 48 | + True |
| 49 | +
|
| 50 | + >>> result = SyncResult() |
| 51 | + >>> result.add_error(step="fetch", message="remote not found") |
| 52 | + >>> result.ok |
| 53 | + False |
| 54 | + >>> bool(result) |
| 55 | + False |
| 56 | + >>> result.errors[0].step |
| 57 | + 'fetch' |
| 58 | + """ |
| 59 | + |
| 60 | + ok: bool = True |
| 61 | + errors: list[SyncError] = dataclasses.field(default_factory=list) |
| 62 | + |
| 63 | + def __bool__(self) -> bool: |
| 64 | + """Return True if the sync succeeded without errors. |
| 65 | +
|
| 66 | + Returns |
| 67 | + ------- |
| 68 | + bool |
| 69 | + True if no errors were recorded, False otherwise. |
| 70 | + """ |
| 71 | + return self.ok |
| 72 | + |
| 73 | + def add_error( |
| 74 | + self, |
| 75 | + step: str, |
| 76 | + message: str, |
| 77 | + exception: Exception | None = None, |
| 78 | + ) -> None: |
| 79 | + """Record an error and mark the result as failed. |
| 80 | +
|
| 81 | + Parameters |
| 82 | + ---------- |
| 83 | + step : str |
| 84 | + Name of the sync step that failed (e.g. ``"fetch"``, ``"checkout"``). |
| 85 | + message : str |
| 86 | + Human-readable description of the error. |
| 87 | + exception : Exception or None, optional |
| 88 | + The underlying exception, if available. |
| 89 | + """ |
| 90 | + self.ok = False |
| 91 | + self.errors.append(SyncError(step=step, message=message, exception=exception)) |
| 92 | + |
| 93 | + |
17 | 94 | class VCSLocation(t.NamedTuple): |
18 | 95 | """Generic VCS Location (URL and optional revision).""" |
19 | 96 |
|
@@ -200,8 +277,14 @@ def ensure_dir(self, *args: t.Any, **kwargs: t.Any) -> bool: |
200 | 277 |
|
201 | 278 | return True |
202 | 279 |
|
203 | | - def update_repo(self, *args: t.Any, **kwargs: t.Any) -> None: |
204 | | - """Pull latest changes to here from remote repository.""" |
| 280 | + def update_repo(self, *args: t.Any, **kwargs: t.Any) -> SyncResult: |
| 281 | + """Pull latest changes to here from remote repository. |
| 282 | +
|
| 283 | + Returns |
| 284 | + ------- |
| 285 | + SyncResult |
| 286 | + Result of the sync operation, with any errors recorded. |
| 287 | + """ |
205 | 288 | raise NotImplementedError |
206 | 289 |
|
207 | 290 | def obtain(self, *args: t.Any, **kwargs: t.Any) -> None: |
|
0 commit comments