monitor: tweak Context() to auto recalculate id, rename methods

This commit tweaks Context a bit so that any write will automatically
reset the `_id`. This ensures that we do not forget to reset `_id`
when the code changes.

It also tweaks the naming a bit, before there was a "setter" for
origin and functions to set "pipeline" and "stage". They are all
functions now with a "set_" prefix for symetry mostly.
This commit is contained in:
Michael Vogt 2023-11-22 09:15:14 +01:00 committed by Ondřej Budai
parent ac16590838
commit 1fa0472a8c
2 changed files with 23 additions and 14 deletions

View file

@ -32,7 +32,7 @@ def omitempty(d: dict):
class Context:
"""Context for a single log line. Automatically calculates hash/id when read."""
"""Context for a single log entry. Automatically calculates hash/id when read."""
def __init__(self,
origin: Optional[str] = None,
@ -46,13 +46,17 @@ class Context:
self._id = None
self._id_history: Set[str] = set()
def __setattr__(self, name, value):
super().__setattr__(name, value)
# reset "_id" on any write so that the hash is automatically recalculated
if name != "_id":
super().__setattr__("_id", None)
@property
def origin(self):
return self._origin
@origin.setter
def origin(self, origin: str):
self._id = None
def set_origin(self, origin: str):
self._origin = origin
@property
@ -63,8 +67,7 @@ class Context:
def pipeline_id(self):
return self._pipeline_id
def pipeline(self, pipeline: osbuild.Pipeline):
self._id = None
def set_pipeline(self, pipeline: osbuild.Pipeline):
self._pipeline_name = pipeline.name
self._pipeline_id = pipeline.id
@ -76,8 +79,7 @@ class Context:
def stage_id(self):
return self._stage_id
def stage(self, stage: osbuild.Stage):
self._id = None
def set_stage(self, stage: osbuild.Stage):
self._stage_name = stage.name
self._stage_id = stage.id
@ -303,7 +305,7 @@ class JSONSeqMonitor(BaseMonitor):
pass
def begin(self, pipeline: osbuild.Pipeline):
self._context.pipeline(pipeline)
self._context.set_pipeline(pipeline)
self._progress.sub_progress(Progress("stages", len(pipeline.stages)))
self._progress.incr()
@ -314,17 +316,17 @@ class JSONSeqMonitor(BaseMonitor):
self._module(assembler)
def _module(self, module):
self._context.stage(module)
self._context.set_stage(module)
self._progress.incr(depth=1)
def log(self, message, origin: Optional[str] = None):
oo = self._context.origin
if origin is not None:
self._context.origin = origin
self._context.set_origin(origin)
entry = log_entry(message=message, context=self._context, progress=self._progress)
self._jsonseq(entry)
# restore old origin
self._context.origin = oo
self._context.set_origin(oo)
def _jsonseq(self, entry):
# follow rfc7464 (application/json-seq)

View file

@ -143,7 +143,7 @@ def test_context():
assert ctx_dict["id"] == "e6305b7e8ccbc39ec88415ea955b89149faf6f51fb6c89831658068bc6850411"
assert len(ctx_dict) == 1
ctx.origin = "org.osbuild.test-2"
ctx.set_origin("org.osbuild.test-2")
ctx_dict = ctx.as_dict()
# should be a full dict again
assert "origin" in ctx_dict
@ -151,7 +151,7 @@ def test_context():
assert ctx_dict["pipeline"]["name"] == "test-pipeline"
assert ctx_dict["pipeline"]["stage"]["name"] == "org.osbuild.noop"
ctx.origin = "org.osbuild.test"
ctx.set_origin("org.osbuild.test")
ctx_dict = ctx.as_dict()
# should only have id again (old context ID)
assert ctx_dict["id"] == "e6305b7e8ccbc39ec88415ea955b89149faf6f51fb6c89831658068bc6850411"
@ -260,3 +260,10 @@ def test_log_line_with_entries():
assert isinstance(entry["context"], dict)
assert isinstance(entry["progress"], dict)
assert entry["timestamp"] > 0
def test_context_id():
ctx = Context()
assert ctx.id == "00d202e4fc9d917def414d1c9f284b137287144087ec275f2d146d9d47b3c8bb"
ctx._origin = "foo"
assert ctx.id != "00d202e4fc9d917def414d1c9f284b137287144087ec275f2d146d9d47b3c8bb"