objectstore: integrate metadata object

Integrate the new `Metadata` object as `meta` property on `Object`.
Use it to actually store metadata after a successful stage run.
A new class `PathAdapter` is introduce which is in turned used to
expose the base path of `Object` as `os.PathLike` so it can be
passed as path to `Metadata`. The advantage is that any changes
to the base path in `Object` will automatically be picked up by
`Metadata`; the prominent, and currently only, case where this is
happening in `Object` is `store_tree`.
This commit is contained in:
Christian Kellner 2022-11-25 13:28:45 +01:00
parent fec9dcea97
commit 1205de0abb
3 changed files with 51 additions and 1 deletions

View file

@ -5,7 +5,7 @@ import os
import subprocess
import tempfile
import uuid
from typing import Optional, Set
from typing import Any, Optional, Set
from osbuild.util import jsoncomm, rmrf
from osbuild.util.mnt import mount, umount
@ -18,6 +18,17 @@ __all__ = [
]
class PathAdapter:
"""Expose an object attribute as `os.PathLike`"""
def __init__(self, obj: Any, attr: str) -> None:
self.obj = obj
self.attr = attr
def __fspath__(self):
return getattr(self.obj, self.attr)
class Object:
class Mode(enum.Enum):
READ = 0
@ -111,6 +122,12 @@ class Object:
tree = os.path.join(self._path, "tree")
os.makedirs(tree)
# Expose our base path as `os.PathLike` via `PathAdater`
# so any changes to it, e.g. via `store_tree`, will be
# automatically picked up by `Metadata`.
wrapped = PathAdapter(self, "_path")
self._meta = self.Metadata(wrapped, folder="meta")
@property
def id(self) -> Optional[str]:
return self._id
@ -128,6 +145,10 @@ class Object:
def tree(self) -> str:
return os.path.join(self._path, "tree")
@property
def meta(self) -> Metadata:
return self._meta
def store_tree(self):
"""Store the tree with a fresh name and close it