debian-forge/inputs/org.osbuild.tree
Christian Kellner 99abc1373d inputs: support array of objects references
This extends the possible ways of passing references to inputs. The
current ways possible are:
 1) "plain references", an array of strings:
    ["ref1", "ref2", ...]
 2) "object references", a mapping of keys to objects:
    {"ref1": { <options> }, "ref2": { <options> }, ...}

This patch adds a new way:
  3) "array of object references":
    [{"id": "ref1", "options": { ... }}, {"id": ... }, ]

While osbuild promises to preserves the order for "object references"
not all JSON serialization libraries preserve the order since the
JSON specification does leave this up to the implementation.

The new "array of object references" thus allows for specifying the
references together with reference specific options and this in a
specific order.

Additionally this paves the way for specifying the same input twice,
e.g. in the case of the `org.osbuild.files` input where a pipeline
could then be specified twice with different files. This needs core
rework though, since internally we use dictionaries right now.
2022-04-21 16:39:58 +02:00

99 lines
2.2 KiB
Python
Executable file

#!/usr/bin/python3
"""
Tree inputs
Open the tree produced by the pipeline supplied via the
first and only entry in `references`. The tree is opened
in read only mode. If the id is `null` or the empty
string it returns an empty tree.
"""
import sys
from osbuild import inputs
SCHEMA = """
"additionalProperties": false,
"required": ["type", "origin", "references"],
"properties": {
"type": {
"enum": ["org.osbuild.tree"]
},
"origin": {
"description": "The origin of the input (must be 'org.osbuild.pipeline')",
"type": "string",
"enum": ["org.osbuild.pipeline"]
},
"references": {
"description": "Exactly one pipeline identifier to use as tree input",
"oneOf": [{
"type": "array",
"additionalItems": false,
"minItems": 1,
"maxItems": 1,
"items": [{
"type": "string"
}]
}, {
"type": "object",
"additionalProperties": false,
"patternProperties": {
".*": {
"type": "object",
"additionalProperties": false
}
},
"minProperties": 1,
"maxProperties": 1
}, {
"type": "array",
"additionalItems": false,
"minItems": 1,
"maxItems": 1,
"items": [{
"type": "object",
"additionalProperties": false,
"required": ["id"],
"properties": {
"id": {
"type": "string"
},
"options": {
"type": "object",
"additionalProperties": false
}
}
}]
}]
}
}
"""
class TreeInput(inputs.InputService):
def map(self, store, _origin, refs, target, _options):
# input verification *must* have been done via schema
# verification. It is expected that origin is a pipeline
# and we have exactly one reference, i.e. a pipeline id
pid, _ = refs.popitem()
if pid:
path = store.read_tree_at(pid, target)
if not path:
raise ValueError(f"Unknown pipeline '{pid}'")
reply = {"path": target}
return reply
def main():
service = TreeInput.from_args(sys.argv[1:])
service.main()
if __name__ == '__main__':
main()