pipeline, fmt: use build result object internally

Instead of serializing the `BuildResult` to a dict in `build_stages`,
we keep the object and then only serialize it in the corresponding
formatting code. This doubles down on the separation between the
internal data structures and the external representation of them. It
was partially already done in the v2 format which hand-picked which
elements of the BuildResult it would return for each stage.
This commit is contained in:
Christian Kellner 2022-06-13 20:33:03 +02:00 committed by Tom Gundersen
parent d235e4c26a
commit 3ab2ddd481
3 changed files with 32 additions and 17 deletions

View file

@ -9,7 +9,7 @@ to fetch resources.
""" """
from typing import Dict from typing import Dict
from osbuild.meta import Index, ValidationResult from osbuild.meta import Index, ValidationResult
from ..pipeline import Manifest, Pipeline, detect_host_runner from ..pipeline import BuildResult, Manifest, Pipeline, detect_host_runner
VERSION = "1" VERSION = "1"
@ -194,6 +194,16 @@ def load(description: Dict, index: Index) -> Manifest:
def output(manifest: Manifest, res: Dict) -> Dict: def output(manifest: Manifest, res: Dict) -> Dict:
"""Convert a result into the v1 format""" """Convert a result into the v1 format"""
def result_for_stage(result: BuildResult):
return {
"id": result.id,
"type": result.name,
"success": result.success,
"error": result.error,
"output": result.output,
"metadata": result.metadata,
}
def result_for_pipeline(pipeline): def result_for_pipeline(pipeline):
# The pipeline might not have been built one of its # The pipeline might not have been built one of its
# dependencies, i.e. its build pipeline, failed to # dependencies, i.e. its build pipeline, failed to
@ -211,7 +221,9 @@ def output(manifest: Manifest, res: Dict) -> Dict:
stages = current.get("stages") stages = current.get("stages")
if stages: if stages:
retval["stages"] = stages retval["stages"] = [
result_for_stage(r) for r in stages
]
return retval return retval
result = result_for_pipeline(manifest["tree"]) result = result_for_pipeline(manifest["tree"])
@ -226,7 +238,10 @@ def output(manifest: Manifest, res: Dict) -> Dict:
if not current: if not current:
return result return result
result["assembler"] = current["stages"][0] # The assembler pipeline must have exactly one stage
# which is the v1 assembler
stage = current["stages"][0]
result["assembler"] = result_for_stage(stage)
if not result["assembler"]["success"]: if not result["assembler"]["success"]:
result["success"] = False result["success"] = False

View file

@ -395,10 +395,10 @@ def output(manifest: Manifest, res: Dict) -> Dict:
"type": "org.osbuild.error.stage", "type": "org.osbuild.error.stage",
"details": { "details": {
"stage": { "stage": {
"id": failed["id"], "id": failed.id,
"type": failed["name"], "type": failed.name,
"output": failed["output"], "output": failed.output,
"error": failed["error"] "error": failed.error,
} }
} }
} }
@ -415,10 +415,10 @@ def output(manifest: Manifest, res: Dict) -> Dict:
data = {} data = {}
r = res.get(p.id, {}) r = res.get(p.id, {})
for stage in r.get("stages", []): for stage in r.get("stages", []):
md = stage.get("metadata") md = stage.metadata
if not md: if not md:
continue continue
name = stage["name"] name = stage.name
val = data.setdefault(name, {}) val = data.setdefault(name, {})
val.update(md) val.update(md)
@ -433,14 +433,14 @@ def output(manifest: Manifest, res: Dict) -> Dict:
for stage in r.get("stages", []): for stage in r.get("stages", []):
data = { data = {
"id": stage["id"], "id": stage.id,
"type": stage["name"], "type": stage.name,
"output": stage["output"] "output": stage.output,
} }
if not stage["success"]: if not stage.success:
data["success"] = stage["success"] data["success"] = stage.success
if stage["error"]: if stage.error:
data["error"] = stage["error"] data["error"] = stage.error
log.append(data) log.append(data)

View file

@ -333,7 +333,7 @@ class Pipeline:
monitor.result(r) monitor.result(r)
results["stages"].append(r.as_dict()) results["stages"].append(r)
if not r.success: if not r.success:
cleanup(build_tree, tree) cleanup(build_tree, tree)
results["success"] = False results["success"] = False