xref: /llvm-project/libc/utils/hdrgen/yaml_functions_sorted.py (revision 9abcca5e25296aea49288ad63901f9e0a332dad4)
1*9abcca5eSRoland McGrath# ex: python3 sort_yaml_functions.py
2*9abcca5eSRoland McGrath# ex: must be within yaml directory
3*9abcca5eSRoland McGrathimport yaml
4*9abcca5eSRoland McGrathimport os
5*9abcca5eSRoland McGrath
6*9abcca5eSRoland McGrath
7*9abcca5eSRoland McGrathdef sort_yaml_functions(yaml_file):
8*9abcca5eSRoland McGrath    with open(yaml_file, "r") as f:
9*9abcca5eSRoland McGrath        yaml_data = yaml.safe_load(f)
10*9abcca5eSRoland McGrath
11*9abcca5eSRoland McGrath    if "functions" in yaml_data:
12*9abcca5eSRoland McGrath        yaml_data["functions"].sort(key=lambda x: x["name"])
13*9abcca5eSRoland McGrath
14*9abcca5eSRoland McGrath    class IndentYamlListDumper(yaml.Dumper):
15*9abcca5eSRoland McGrath        def increase_indent(self, flow=False, indentless=False):
16*9abcca5eSRoland McGrath            return super(IndentYamlListDumper, self).increase_indent(flow, False)
17*9abcca5eSRoland McGrath
18*9abcca5eSRoland McGrath    with open(yaml_file, "w") as f:
19*9abcca5eSRoland McGrath        yaml.dump(
20*9abcca5eSRoland McGrath            yaml_data,
21*9abcca5eSRoland McGrath            f,
22*9abcca5eSRoland McGrath            Dumper=IndentYamlListDumper,
23*9abcca5eSRoland McGrath            default_flow_style=False,
24*9abcca5eSRoland McGrath            sort_keys=False,
25*9abcca5eSRoland McGrath        )
26*9abcca5eSRoland McGrath
27*9abcca5eSRoland McGrath
28*9abcca5eSRoland McGrathdef main():
29*9abcca5eSRoland McGrath    current_directory = os.getcwd()
30*9abcca5eSRoland McGrath    yaml_files = [
31*9abcca5eSRoland McGrath        file for file in os.listdir(current_directory) if file.endswith(".yaml")
32*9abcca5eSRoland McGrath    ]
33*9abcca5eSRoland McGrath
34*9abcca5eSRoland McGrath    for yaml_file in yaml_files:
35*9abcca5eSRoland McGrath        sort_yaml_functions(yaml_file)
36*9abcca5eSRoland McGrath
37*9abcca5eSRoland McGrath
38*9abcca5eSRoland McGrathif __name__ == "__main__":
39*9abcca5eSRoland McGrath    main()
40