osbuild: allow json data to come from a {stage}-meta.json file

Instead of always parsing the python stage to load meta information
allow the user of a new `{stage}-meta.json` file. This is a first
step towards allowing modules to be written in a different language
than python. It also has some practical advantages:
- slightly faster as it avoids calling python to output the schemas
- easier to write schemas as this can be done in a real json editor
  now
- more extensible in a future where stages maybe binaries with
  shlib dependencies that are only satisfied in the buildroot
  but not on the host
This commit is contained in:
Michael Vogt 2023-11-21 12:51:02 +01:00 committed by Simon de Vlieger
parent 3dd12931e4
commit 9b09ed9eb4
4 changed files with 139 additions and 22 deletions

View file

@ -409,6 +409,42 @@ class ModuleInfo:
@classmethod
def load(cls, root, klass, name) -> Optional["ModuleInfo"]:
base = cls.MODULES.get(klass)
if not base:
raise ValueError(f"Unsupported type: {klass}")
path = os.path.join(root, base, name)
try:
return cls._load_from_json(path, klass, name)
except FileNotFoundError:
# should we print a deprecation warning here?
pass
return cls._load_from_py(path, klass, name)
@classmethod
def _load_from_json(cls, path, klass, name) -> Optional["ModuleInfo"]:
# ideas welcome for a better filename/suffix :)
meta_json_suffix = "-meta.json"
with open(path + meta_json_suffix, encoding="utf-8") as fp:
meta = json.load(fp)
long_description = meta.get("description", "no description provided")
if isinstance(long_description, list):
long_description = "\n".join(long_description)
info = {
"schema": {
"1": meta.get("schema", {}),
"2": meta.get("schema_2", {}),
},
"desc": meta.get("summary", "no summary provided"),
"info": long_description,
"caps": meta.get("capabilities", set()),
}
return cls(klass, name, path, info)
@classmethod
def _load_from_py(cls, path, klass, name) -> Optional["ModuleInfo"]:
names = ["SCHEMA", "SCHEMA_2", "CAPABILITIES"]
def filter_type(lst, target):
@ -417,11 +453,6 @@ class ModuleInfo:
def targets(a):
return [t.id for t in filter_type(a.targets, ast.Name)]
base = cls.MODULES.get(klass)
if not base:
raise ValueError(f"Unsupported type: {klass}")
path = os.path.join(root, base, name)
try:
with open(path, encoding="utf8") as f:
data = f.read()