osbuild: refactor stage information
For all currently supported modules, i.e. stages and assemblers,
convert the STAGE_DESC and STAGE_INFO into a proper doc-string.
Rename the STAGE_OPTS into SCHEMA.
Refactor meta.ModuleInfo loading accordingly.
The script to be used for the conversion is:
--- 8< --- 8< --- 8< --- 8< --- 8< --- 8< --- 8< --- 8< ---
import os
import sys
import osbuild
import osbuild.meta
from osbuild.meta import ModuleInfo
def find_line(lines, start):
for i, l in enumerate(lines):
if l.startswith(start):
return i
return None
def del_block(lines, prefix):
start = find_line(lines, prefix)
end = find_line(lines[start:], '"""')
print(start, end)
del lines[start:start+end+1]
def main():
index = osbuild.meta.Index(os.curdir)
modules = []
for klass in ("Stage", "Assembler"):
mods = index.list_modules_for_class(klass)
modules += [(klass, module) for module in mods]
for m in modules:
print(m)
klass, name = m
info = ModuleInfo.load(os.curdir, klass, name)
module_path = ModuleInfo.module_class_to_directory(klass)
path = os.path.join(os.curdir, module_path, name)
with open(path, "r") as f:
data = list(f.readlines())
i = find_line(data, "STAGE_DESC")
print(i)
del data[i]
del_block(data, "STAGE_INFO")
i = find_line(data, "STAGE_OPTS")
data[i] = 'SCHEMA = """\n'
docstr = '"""\n' + info.desc + "\n" + info.info + '"""\n'
doclst = docstr.split("\n")
doclst = [l + "\n" for l in doclst]
data = [data[0]] + doclst + data[1:]
with open(path, "w") as f:
f.writelines(data)
if __name__ == "__main__":
main()
This commit is contained in:
parent
131d0264a8
commit
2a9cdde5ec
32 changed files with 340 additions and 269 deletions
|
|
@ -277,10 +277,9 @@ class ModuleInfo:
|
|||
self.name = name
|
||||
self.type = klass
|
||||
|
||||
opts = info.get("STAGE_OPTS") or ""
|
||||
self.info = info.get("STAGE_INFO")
|
||||
self.desc = info.get("STAGE_DESC")
|
||||
self.opts = info.get("STAGE_OPTS")
|
||||
opts = info.get("schema") or ""
|
||||
self.info = info.get("info")
|
||||
self.desc = info.get("desc")
|
||||
self.opts = json.loads("{" + opts + "}")
|
||||
|
||||
@property
|
||||
|
|
@ -312,7 +311,7 @@ class ModuleInfo:
|
|||
|
||||
@classmethod
|
||||
def load(cls, root, klass, name) -> Optional["ModuleInfo"]:
|
||||
names = ['STAGE_INFO', 'STAGE_DESC', 'STAGE_OPTS']
|
||||
names = ['SCHEMA']
|
||||
|
||||
def value(a):
|
||||
v = a.value
|
||||
|
|
@ -338,9 +337,18 @@ class ModuleInfo:
|
|||
return None
|
||||
|
||||
tree = ast.parse(data, name)
|
||||
|
||||
docstring = ast.get_docstring(tree)
|
||||
doclist = docstring.split("\n")
|
||||
|
||||
assigns = filter_type(tree.body, ast.Assign)
|
||||
targets = [(t, a) for a in assigns for t in targets(a)]
|
||||
info = {k: value(v) for k, v in targets if k in names}
|
||||
values = {k: value(v) for k, v in targets if k in names}
|
||||
info = {
|
||||
'schema': values.get("SCHEMA"),
|
||||
'desc': doclist[0],
|
||||
'info': "\n".join(doclist[1:])
|
||||
}
|
||||
return cls(klass, name, info)
|
||||
|
||||
@staticmethod
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue