From 1fa0472a8cd79798b49e43f499e064806b8619f8 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Wed, 22 Nov 2023 09:15:14 +0100 Subject: [PATCH] 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. --- osbuild/monitor.py | 26 ++++++++++++++------------ test/mod/test_monitor.py | 11 +++++++++-- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/osbuild/monitor.py b/osbuild/monitor.py index 4c7a2e4e..a41f1c4c 100644 --- a/osbuild/monitor.py +++ b/osbuild/monitor.py @@ -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) diff --git a/test/mod/test_monitor.py b/test/mod/test_monitor.py index c0302b8f..8bc8c3c9 100644 --- a/test/mod/test_monitor.py +++ b/test/mod/test_monitor.py @@ -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"