-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Actually add
failed_builds.py
and add time of first failure to the …
…"database" Signed-off-by: magic_rb <[email protected]>
- Loading branch information
Showing
2 changed files
with
82 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import dbm | ||
from datetime import datetime | ||
from pathlib import Path | ||
from typing import TYPE_CHECKING, Any | ||
|
||
from pydantic import BaseModel | ||
|
||
if TYPE_CHECKING: | ||
database: None | dbm._Database = None | ||
else: | ||
database: Any = None | ||
|
||
|
||
class FailedBuildsError(Exception): | ||
pass | ||
|
||
|
||
class FailedBuild(BaseModel): | ||
derivation: str | ||
time: datetime | ||
|
||
|
||
DB_NOT_INIT_MSG = "Database not initialized" | ||
|
||
|
||
def initialize_database(db_path: Path) -> None: | ||
global database # noqa: PLW0603 | ||
|
||
if not database: | ||
database = dbm.open(str(db_path), "c") | ||
|
||
|
||
def add_build(derivation: str, time: datetime) -> None: | ||
global database # noqa: PLW0602 | ||
|
||
if database is not None: | ||
database[derivation] = FailedBuild( | ||
derivation=derivation, time=time | ||
).model_dump_json() | ||
else: | ||
raise FailedBuildsError(DB_NOT_INIT_MSG) | ||
|
||
|
||
def check_build(derivation: str) -> FailedBuild | None: | ||
global database # noqa: PLW0602 | ||
|
||
if database is not None: | ||
if derivation in database: | ||
# TODO create dummy if deser fails? | ||
return FailedBuild.model_validate_json(database[derivation]) | ||
return None | ||
raise FailedBuildsError(DB_NOT_INIT_MSG) | ||
|
||
|
||
def remove_build(derivation: str) -> None: | ||
global database # noqa: PLW0602 | ||
|
||
if database is not None: | ||
del database[derivation] | ||
else: | ||
raise FailedBuildsError(DB_NOT_INIT_MSG) |