Debian Codegraph @

ci/generate_configuration_table.py

raw
1 #!/usr/bin/python3
2
3 """
4 Generate a table of the Graphviz compile configuration for different platforms
5 from files generated by the ./configure command.
6 """
7
8 import os
9 import re
10 import sys
11 from argparse import ArgumentParser
12
13
14 def main():
15 """entry point"""
16
17 parser = ArgumentParser(
18 description="Generate a table of the Graphviz compile "
19 "configuration for different platforms from files "
20 "generated by the ./configure command."
21 )
22 parser.add_argument(
23 "--color",
24 "-c",
25 action="store_const",
26 dest="colors",
27 const="green:red",
28 help="Color output using default coloring. Yes is "
29 "colored green and No is colored red",
30 )
31 parser.add_argument(
32 "--colors",
33 help="Color output using specified COLORS. The format is "
34 "<Yes-color>:<No-color>",
35 )
36 parser.add_argument(
37 "--short", "-s", action="store_true", help="Output only Yes or No"
38 )
39 parser.add_argument("filename", nargs="*", help="Configuration log to read")
40
41 opts = parser.parse_args()
42
43 if opts.colors is None:
44 styles = {
45 "Yes": "",
46 "No": "",
47 }
48 else:
49 yes_color = "green"
50 no_color = "red"
51 if opts.colors != "":
52 colors = opts.colors.split(":")
53 if len(colors) == 2:
54 yes_color, no_color = colors
55 else:
56 sys.stderr.write(
57 f"Error: {opts.colors} color specification is illegal\n"
58 )
59 parser.print_help(file=sys.stderr)
60 sys.exit(1)
61 styles = {
62 "Yes": f' style="color: {yes_color};"',
63 "No": f' style="color: {no_color};"',
64 }
65
66 table: dict[str, dict[str, dict[str, str]]] = {}
67 table_sections: list[str] = []
68 component_names: dict[str, list[str]] = {}
69 platforms: set[str] = set()
70
71 for filename in opts.filename:
72 os_path = os.path.dirname(filename)
73 os_version_id = os.path.basename(os_path)
74 os_id = os.path.basename(os.path.dirname(os_path))
75 platform = f"{os_id.capitalize()} {os_version_id}"
76 platforms.add(platform)
77 with open(filename, "rt", encoding="utf-8") as fp:
78 for line in fp:
79 item = [item.strip() for item in line.split(":")]
80 if len(item) >= 2:
81 if item[1] == "":
82 section_name = item[0]
83 if section_name not in table:
84 table[section_name] = {}
85 table_sections.append(section_name)
86 component_names[section_name] = []
87 else:
88 component_name, component_value, *_ = item
89 short_value = re.sub("(Yes|No).*", "\\1", component_value)
90 if opts.short:
91 component_value = short_value
92 if component_name not in table[section_name]:
93 table[section_name][component_name] = {}
94 component_names[section_name].append(component_name)
95 table[section_name][component_name][platform] = component_value
96
97 colspan = len(platforms) + 1
98 indent = ""
99 print(f"{indent}<!DOCTYPE html>")
100 print(f"{indent}<html>")
101 indent += " "
102 print(f"{indent}<head>")
103 indent += " "
104 print(f'{indent}<meta charset="utf-8">')
105 print(f"{indent}<style>")
106 indent += " "
107 print(
108 f"{indent}table {{text-align: left; white-space: nowrap; position: relative;}}"
109 )
110 print(
111 f"{indent}thead tr th {{padding-right: 1em; padding-bottom: 5px; "
112 "position: sticky; top: 0px; background-color: white;}"
113 )
114 print(f"{indent}td, th {{padding-left: 1.5em;}}")
115 indent = indent[:-2]
116 print(f"{indent}</style>")
117 indent = indent[:-2]
118 print(f"{indent}</head>")
119 print(f"{indent}<body>")
120 indent += " "
121 header = table_sections[0].replace("will be", "was")
122 print(f"{indent}<h1>{header}:</h1>")
123 print(f"{indent}<table>")
124 indent += " "
125 print(f"{indent}<thead>")
126 indent += " "
127 print(f"{indent}<tr>")
128 indent += " "
129 print(f"{indent}<th></th>")
130 for platform in sorted(platforms):
131 print(f"{indent}<th>{platform}</th>")
132 indent = indent[:-2]
133 print(f"{indent} </tr>")
134 print(f"{indent} </thead>")
135 print(f"{indent} <tbody>")
136 indent += " "
137 indent = indent[:-2]
138 for section_name in table_sections[1:]:
139 print(f'{indent}<tr><th colspan="{colspan}">{section_name};</th></tr>')
140 for component_name in component_names[section_name]:
141 print(f"{indent}<tr>")
142 indent += " "
143 print(f"{indent}<td>{component_name}</td>")
144 for platform in sorted(platforms):
145 component_value = table[section_name][component_name][platform]
146 short_value = re.sub("(Yes|No).*", "\\1", component_value)
147 color_style = styles[short_value]
148 print(f"{indent}<td{color_style}>{component_value}</td>")
149 indent = indent[:-2]
150 print(f"{indent}</tr>")
151 print(f'{indent}<tr><th colspan="{colspan}">&nbsp;</th></tr>')
152 indent = indent[:-2]
153 print(f"{indent}</tbody>")
154 indent = indent[:-2]
155 print(f"{indent}</table>")
156 indent = indent[:-2]
157 print(f"{indent}</body>")
158 indent = indent[:-2]
159 print(f"{indent}</html>")
160
161
162 # Python trick to get a main routine
163 if __name__ == "__main__":
164 main()