intel/genxml: split the processing code up

Instead of crewating output or validating in the process code, just
process, then let main handle the rest

Reviewed-by: Eric Engestrom <eric@engestrom.ch>
Acked-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com>
Acked-by: Jordan Justen <jordan.l.justen@intel.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/18955>
This commit is contained in:
Dylan Baker
2022-10-03 10:31:58 -07:00
parent 8c8a1966ab
commit 0ac3b6f671

View File

@@ -125,12 +125,9 @@ def process_attribs(elem: et.Element) -> None:
process_attribs(e)
def process(filename: pathlib.Path, validate: bool) -> None:
xml = et.parse(filename)
genxml: et.Element = xml.getroot()
original = copy.deepcopy(genxml) if validate else genxml
enums = sorted(genxml.findall('enum'), key=get_name)
def process(xml: et.ElementTree) -> None:
genxml = xml.getroot()
enums = sorted(xml.findall('enum'), key=get_name)
enum_dict: typing.Dict[str, et.Element] = {}
for e in enums:
e[:] = sorted(e, key=get_value)
@@ -166,17 +163,6 @@ def process(filename: pathlib.Path, validate: bool) -> None:
for n in new_elems:
process_attribs(n)
genxml[:] = new_elems
if validate:
for old, new in zip(original, genxml):
assert node_validator(old, new), f'{filename} is invalid, run gen_sort_tags.py and commit that'
else:
tmp = filename.with_suffix(f'{filename.suffix}.tmp')
tree = et.ElementTree(genxml)
et.indent(tree, space=' ')
tree.write(tmp, encoding="utf-8", xml_declaration=True)
filename.unlink()
tmp.rename(filename)
if __name__ == '__main__':
@@ -188,9 +174,23 @@ if __name__ == '__main__':
parser.add_argument('--quiet', action='store_true')
args: Args = parser.parse_args()
for f in args.files:
for filename in args.files:
if not args.quiet:
print('Processing {}... '.format(f), end='', flush=True)
process(f, args.validate)
print('Processing {}... '.format(filename), end='', flush=True)
xml = et.parse(filename)
original = copy.deepcopy(xml) if args.validate else xml
process(xml)
if args.validate:
for old, new in zip(original.getroot(), xml.getroot()):
assert node_validator(old, new), f'{filename} is invalid, run gen_sort_tags.py and commit that'
else:
tmp = filename.with_suffix(f'{filename.suffix}.tmp')
et.indent(xml, space=' ')
xml.write(tmp, encoding="utf-8", xml_declaration=True)
filename.unlink()
tmp.rename(filename)
if not args.quiet:
print('done.')