1e8556ba3Sopenharmony_ci#!/usr/bin/python3
2e8556ba3Sopenharmony_ci#
3e8556ba3Sopenharmony_ci# Copyright 2023 The Khronos Group Inc.
4e8556ba3Sopenharmony_ci# SPDX-License-Identifier: Apache-2.0
5e8556ba3Sopenharmony_ci
6e8556ba3Sopenharmony_ciimport argparse
7e8556ba3Sopenharmony_ciimport xml.etree.ElementTree as etree
8e8556ba3Sopenharmony_cifrom reg import stripNonmatchingAPIs
9e8556ba3Sopenharmony_ci
10e8556ba3Sopenharmony_ciif __name__ == '__main__':
11e8556ba3Sopenharmony_ci    parser = argparse.ArgumentParser(prog='stripAPI',
12e8556ba3Sopenharmony_ci                formatter_class=argparse.RawDescriptionHelpFormatter,
13e8556ba3Sopenharmony_ci                description='''\
14e8556ba3Sopenharmony_ciFilters out elements with non-matching explicit 'api' attributes from API XML.
15e8556ba3Sopenharmony_ciTo remove Vulkan SC-only elements from the combined API XML:
16e8556ba3Sopenharmony_ci    python3 scripts/stripAPI.py -input xml/vk.xml -output vulkan-only.xml -keepAPI vulkan
17e8556ba3Sopenharmony_ciTo remove Vulkan-only elements:
18e8556ba3Sopenharmony_ci    python3 scripts/stripAPI.py -input xml/vk.xml -output vulkansc-only.xml -keepAPI vulkansc
19e8556ba3Sopenharmony_ciIf you are parsing the XML yourself but using the xml.etree package, the
20e8556ba3Sopenharmony_ciequivalent runtime code is:
21e8556ba3Sopenharmony_ci   import reg
22e8556ba3Sopenharmony_ci   reg.stripNonmatchingAPIs(tree.getroot(), keepAPI, actuallyDelete=True)
23e8556ba3Sopenharmony_ciwhere 'tree' is an ElementTree created from the XML file using
24e8556ba3Sopenharmony_ci    etree.parse(filename)''')
25e8556ba3Sopenharmony_ci
26e8556ba3Sopenharmony_ci    parser.add_argument('-input', action='store',
27e8556ba3Sopenharmony_ci                        required=True,
28e8556ba3Sopenharmony_ci                        help='Specify input registry XML')
29e8556ba3Sopenharmony_ci    parser.add_argument('-output', action='store',
30e8556ba3Sopenharmony_ci                        required=True,
31e8556ba3Sopenharmony_ci                        help='Specify output registry XML')
32e8556ba3Sopenharmony_ci    parser.add_argument('-keepAPI', action='store',
33e8556ba3Sopenharmony_ci                        default=None,
34e8556ba3Sopenharmony_ci                        help='Specify API name whose \'api\' tags are kept')
35e8556ba3Sopenharmony_ci
36e8556ba3Sopenharmony_ci    args = parser.parse_args()
37e8556ba3Sopenharmony_ci
38e8556ba3Sopenharmony_ci    tree = etree.parse(args.input)
39e8556ba3Sopenharmony_ci    if args.keepAPI is not None:
40e8556ba3Sopenharmony_ci        stripNonmatchingAPIs(tree.getroot(), args.keepAPI, actuallyDelete = True)
41e8556ba3Sopenharmony_ci    tree.write(args.output)
42e8556ba3Sopenharmony_ci
43