port part of settings to database (#54)

This commit is contained in:
2022-03-31 01:48:06 +03:00
committed by GitHub
parent fb02e676af
commit 28cc38aaa5
117 changed files with 2768 additions and 1044 deletions

View File

@ -17,13 +17,12 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import json
import logging
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple
from typing import Dict, List, Optional, Tuple
from ahriman.core.configuration import Configuration
from ahriman.core.database.sqlite import SQLite
from ahriman.core.exceptions import UnknownPackage
from ahriman.core.repository import Repository
from ahriman.models.build_status import BuildStatus, BuildStatusEnum
@ -34,33 +33,29 @@ class Watcher:
"""
package status watcher
:ivar architecture: repository architecture
:ivar database: database instance
:ivar known: list of known packages. For the most cases `packages` should be used instead
:ivar logger: class logger
:ivar repository: repository object
:ivar status: daemon status
"""
def __init__(self, architecture: str, configuration: Configuration) -> None:
def __init__(self, architecture: str, configuration: Configuration, database: SQLite) -> None:
"""
default constructor
:param architecture: repository architecture
:param configuration: configuration instance
:param database: database instance
"""
self.logger = logging.getLogger("http")
self.architecture = architecture
self.repository = Repository(architecture, configuration, no_report=True, unsafe=False)
self.database = database
self.repository = Repository(architecture, configuration, database, no_report=True, unsafe=False)
self.known: Dict[str, Tuple[Package, BuildStatus]] = {}
self.status = BuildStatus()
@property
def cache_path(self) -> Path:
"""
:return: path to dump with json cache
"""
return self.repository.paths.root / "status_cache.json"
@property
def packages(self) -> List[Tuple[Package, BuildStatus]]:
"""
@ -68,48 +63,6 @@ class Watcher:
"""
return list(self.known.values())
def _cache_load(self) -> None:
"""
update current state from cache
"""
def parse_single(properties: Dict[str, Any]) -> None:
package = Package.from_json(properties["package"])
status = BuildStatus.from_json(properties["status"])
if package.base in self.known:
self.known[package.base] = (package, status)
if not self.cache_path.is_file():
return
with self.cache_path.open() as cache:
try:
dump = json.load(cache)
except Exception:
self.logger.exception("cannot parse json from file")
dump = {}
for item in dump.get("packages", []):
try:
parse_single(item)
except Exception:
self.logger.exception("cannot parse item %s to package", item)
def _cache_save(self) -> None:
"""
dump current cache to filesystem
"""
dump = {
"packages": [
{
"package": package.view(),
"status": status.view()
} for package, status in self.packages
]
}
try:
with self.cache_path.open("w") as cache:
json.dump(dump, cache)
except Exception:
self.logger.exception("cannot dump cache")
def get(self, base: str) -> Tuple[Package, BuildStatus]:
"""
get current package base build status
@ -131,31 +84,34 @@ class Watcher:
else:
status = BuildStatus()
self.known[package.base] = (package, status)
self._cache_load()
def remove(self, base: str) -> None:
for package, status in self.database.packages_get():
if package.base in self.known:
self.known[package.base] = (package, status)
def remove(self, package_base: str) -> None:
"""
remove package base from known list if any
:param base: package base
:param package_base: package base
"""
self.known.pop(base, None)
self._cache_save()
self.known.pop(package_base, None)
self.database.package_remove(package_base)
def update(self, base: str, status: BuildStatusEnum, package: Optional[Package]) -> None:
def update(self, package_base: str, status: BuildStatusEnum, package: Optional[Package]) -> None:
"""
update package status and description
:param base: package base to update
:param package_base: package base to update
:param status: new build status
:param package: optional new package description. In case if not set current properties will be used
"""
if package is None:
try:
package, _ = self.known[base]
package, _ = self.known[package_base]
except KeyError:
raise UnknownPackage(base)
raise UnknownPackage(package_base)
full_status = BuildStatus(status)
self.known[base] = (package, full_status)
self._cache_save()
self.known[package_base] = (package, full_status)
self.database.package_update(package, full_status)
def update_self(self, status: BuildStatusEnum) -> None:
"""