-
Notifications
You must be signed in to change notification settings - Fork 22
/
mtda-config
executable file
·437 lines (382 loc) · 14.7 KB
/
mtda-config
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#!/usr/bin/env python3
# ---------------------------------------------------------------------------
# Config generator for MTDA
# ---------------------------------------------------------------------------
#
# This software is a part of MTDA.
# Copyright (C) 2024 Siemens Digital Industries Software
#
# ---------------------------------------------------------------------------
# SPDX-License-Identifier: MIT
# ---------------------------------------------------------------------------
import argparse
import logging
import mtda
import os
import re
import sys
import urwid
from kconfiglib import Kconfig, MENU, COMMENT
class Config:
def __init__(self, kconfig, oldconfig=None):
self.kconfig = Kconfig(filename=kconfig)
self.top_node = self.kconfig.top_node
self.config_list = self.get_all_config(self.top_node, [])
self.menu_list = self.get_all_menu(self.top_node, [])
self.oldconfig = oldconfig
def __save_as_config(self, config):
with open("./.config", "w") as config_file:
for key in config:
config_file.write(f"{key}={config[key]}\n")
def __save_as_ini(self, config, output_file=None):
if not output_file:
output_file = os.path.join(os.getcwd(), ".config.ini")
with open(output_file, "w") as config_file:
current_module = None
for key in sorted(config):
"""
Skip saving options without values
"""
if config[key] == "":
continue
"""
Some config names might have "_".
So parse all words without "_" and then \\w+ which is
[a-zA-Z0-9_]+
"""
template = re.search(r"CONFIG_([a-zA-Z0-9]+)_(\w+)", key)
module_name = template.group(1).lower()
config_name = template.group(2).lower()
# Write the module_name
if module_name != current_module:
config_file.write(f"\n[{module_name}]\n")
current_module = module_name
config_file.write(f" {config_name} = {config[key]}\n")
def save_config(self, nodes=[], output_file=None):
config_entries = {}
for node in nodes:
config = f"CONFIG_{node.item.name}"
config_entries[config] = node.item.str_value
logging.debug("save_config: %s=%s", config, config_entries[config])
self.__save_as_ini(config_entries, output_file)
def load_config(self, skip_list=[]):
config_entries = {}
module_name = None
with open(self.oldconfig) as config_file:
for line in config_file:
# Skip empty lines
if line.strip() == "":
continue
# Skip comments
# TODO: handle lines starting with spaces
if line.startswith("#"):
continue
if line.startswith("["):
module_name = re.search(r"\[(\w+)\]", line).group(1)
continue
if module_name not in skip_list:
# Handle invalid cases later
(config, value) = line.split("=")
config = f"CONFIG_{module_name}_{config.strip()}".upper()
config_entries[config] = value.strip()
return config_entries
"""
Merge the config with old config
"""
def merge_config(self, oldconfig_entries):
for key in sorted(oldconfig_entries):
config_name = key.replace("CONFIG_", "", 1)
logging.debug("merge_config: %s", config_name)
node = self.find_node_by_name(config_name)
if node:
self.set_value(node, oldconfig_entries[key])
def find_node_by_label(self, label):
for node in self.config_list + self.menu_list:
if node.prompt[0] == label:
return node
return None
def find_node_by_name(self, name):
for node in self.config_list:
if node.item.name == name:
return node
return None
def get_name(self, node):
return node.name
def get_help(self, node):
return node.help
def set_value(self, node, value):
node.item.set_value(value)
def get_value(self, node):
return node.item.str_value
def get_all_config(self, node, config_list):
while node:
if node.item != MENU and node.item != COMMENT:
config_list.append(node)
if node.list:
self.get_all_config(node.list, config_list)
node = node.next
return config_list
def get_all_menu(self, node, menu_list):
while node:
if node.item == MENU:
menu_list.append(node)
if node.list:
self.get_all_menu(node.list, menu_list)
node = node.next
return menu_list
class ConfigApp:
def __init__(self, kconfig, oldconfig=None, output_file=None):
self.kconfig_file = os.path.abspath(kconfig)
self.output_file = output_file
self.config = Config(self.kconfig_file, oldconfig)
self.mainscreen = None
self.submenu = None
self.loop = None
self.palette = [
("banner", "white", "dark red", "bold"),
("body", "white", "dark blue", "standout"),
("footer", "white", "dark red", "standout"),
("normal", "white", "dark blue", "standout"),
("focus", "black", "white", "bold"),
("editbox", "light gray", "dark blue"),
("editfocus", "white", "dark blue", "bold"),
("editcaption", "black", "light gray", "standout"),
("submenu", "black", "light gray", "standout"),
("button", "white", "dark red", "standout"),
]
def __confirm_exit(self):
# Question
confirm_text = urwid.Text("Save config ?\n", align="center")
# Yes/No Buttons
yes_button = urwid.Button("Yes", on_press=self.__save_and_quit)
yes_button = urwid.AttrMap(yes_button, "button")
no_button = urwid.Button("No", on_press=self.__save_and_quit)
no_button = urwid.AttrMap(no_button, "button")
buttons_gridflow = urwid.GridFlow([yes_button, no_button],
10, 3, 1, "center")
option_pile = urwid.Pile([confirm_text, buttons_gridflow])
option_linebox = urwid.LineBox(urwid.Filler(option_pile))
option_linebox = urwid.AttrMap(option_linebox, "submenu")
optionscreen = urwid.Overlay(
option_linebox,
self.mainscreen,
"center",
("relative", 25),
"middle",
("relative", 25),
)
self.loop.widget = optionscreen
def __handle_keypress(self, key):
if key == "esc":
if self.loop.widget == self.mainscreen:
self.__confirm_exit()
elif self.loop.widget == self.submenu:
self.loop.widget = self.mainscreen
else:
self.loop.widget = self.submenu
if key in ("Q", "q"):
self.__confirm_exit()
def __show_submenu(self, button):
node = self.config.find_node_by_label(button.label)
if node is None:
return
listbox_contents = []
subnode = node.list
while subnode:
if subnode.item != MENU and subnode.item != COMMENT:
logging.debug(
"visibility of %s=%s",
subnode.prompt[0],
subnode.item.visibility
)
if subnode.item.visibility:
listbox_contents.append(
self.__create_menu_entry(subnode, self.__show_popup)
)
subnode = subnode.next
listbox_walker = urwid.SimpleFocusListWalker(listbox_contents)
body_listbox = urwid.ListBox(listbox_walker)
body_listbox = urwid.AttrMap(body_listbox, "body")
option_linebox = urwid.LineBox(body_listbox)
option_linebox = urwid.AttrMap(option_linebox, "submenu")
self.submenu = urwid.Overlay(
option_linebox,
self.mainscreen,
"center",
("relative", 70),
"middle",
("relative", 70),
)
self.loop.widget = self.submenu
def __show_popup(self, button):
node = self.config.find_node_by_label(button.label)
if node is None:
return
if self.loop.widget == self.submenu:
info = self.config.get_help(node)
if info is None:
info = "N/A"
config_value = self.config.get_value(node)
config_value = config_value or ""
value_edit = urwid.Edit(
caption=("editcaption", "Value : "), edit_text=config_value
)
value_edit_attr = urwid.AttrMap(value_edit, "editbox", "editfocus")
help_text = urwid.Text(info)
help_linebox = urwid.LineBox(help_text)
save_button = urwid.Button(
"save",
on_press=self.__save_config_value,
user_data=(value_edit, node)
)
save_button = urwid.AttrMap(save_button, "button")
cancel_button = urwid.Button("cancel",
on_press=self.__save_config_value)
cancel_button = urwid.AttrMap(cancel_button, "button")
buttons_gridflow = urwid.GridFlow(
[save_button, cancel_button], 10, 3, 1, "center"
)
option_pile = urwid.Pile(
[
("weight", 10, value_edit_attr),
("weight", 85, help_linebox),
("weight", 5, buttons_gridflow),
]
)
option_linebox = urwid.LineBox(urwid.Filler(option_pile))
option_linebox = urwid.AttrMap(option_linebox, "submenu")
option_widget = urwid.Overlay(
option_linebox,
self.submenu,
"center",
("relative", 30),
"middle",
("relative", 30),
)
self.loop.widget = option_widget
else:
self.loop.widget = self.submenu
def __create_menu_entry(self, entry, callback=None):
entry = urwid.Button(entry.prompt[0])
if callback is not None:
urwid.connect_signal(entry, "click", callback)
return urwid.AttrMap(entry, "normal", "focus")
def __save_config_value(self, button, user_data=None):
if button.label == "save":
new_value = user_data[0].get_edit_text()
self.config.set_value(user_data[1], new_value)
self.loop.widget = self.submenu
def __save_and_quit(self, button):
if button.label == "Yes":
self.save()
else:
raise urwid.ExitMainLoop()
def save(self):
save_nodes = []
for node in self.config.config_list:
if node.parent and node.parent.prompt[0] == "General Settings":
continue
save_nodes.append(node)
self.config.save_config(save_nodes, output_file=self.output_file)
raise urwid.ExitMainLoop()
def start(self):
# Update based on configs loaded
# We don't support scripts section yet
if self.config.oldconfig:
oldconfig_entries = self.config.load_config(skip_list=["scripts"])
self.config.merge_config(oldconfig_entries)
logging.debug(self.config.config_list)
logging.debug(self.config.menu_list)
# Header of the main display
header_text = urwid.Text("MTDA Config Generator", align="center")
header_text = urwid.AttrMap(header_text, "banner")
# Body of the main display
listbox_contents = []
for menu in self.config.menu_list:
# Hack. Drop Main menu entry
if menu.prompt[0] == "Main menu":
continue
listbox_contents.append(
self.__create_menu_entry(menu, self.__show_submenu)
)
listbox_walker = urwid.SimpleFocusListWalker(listbox_contents)
body_listbox = urwid.ListBox(listbox_walker)
body_listbox = urwid.AttrMap(body_listbox, "body")
# Footer of the main display
footer_left_text = urwid.Text(
"Q/q - quit | Esc - previous screen | Arrow keys - navigation"
)
footer_right_text = urwid.Text(
f"Kconfig: {self.kconfig_file}", align="right"
)
footer_column = urwid.Columns([footer_left_text, footer_right_text])
footer_column = urwid.AttrMap(footer_column, "footer")
# Compose a frame with all header body and footer
self.mainscreen = urwid.Frame(
body_listbox, header=header_text, footer=footer_column
)
self.loop = urwid.MainLoop(
self.mainscreen,
self.palette,
unhandled_input=self.__handle_keypress
)
self.loop.run()
def start_config():
parser = argparse.ArgumentParser(description="MTDA config generator")
parser.add_argument(
"-v",
"--version",
action="version",
version=f"mtda config generator {mtda.__version__}",
)
parser.add_argument(
"-d",
"--debug",
action="store_true",
help="debug mtda config",
)
parser.add_argument(
"-o",
"--output",
dest="output_file",
help="Output file location",
metavar="OUTPUT FILE",
)
parser.add_argument(
"-k",
"--kconfig-file",
dest="kconfig_file",
help="path to kconfig file",
metavar="KCONFIG",
)
parser.add_argument(
"config",
nargs="?",
default=None,
help="path to existing mtda config file",
)
args = parser.parse_args()
kconfig_file = args.kconfig_file
if kconfig_file is None:
kconfig_file = "/var/lib/mtda/Kconfig"
if args.config:
if not os.path.exists(args.config):
print(f"Cannot access config file [ {args.config} ]\n")
parser.print_help()
sys.exit(1)
if not os.path.exists(kconfig_file):
print(f"Cannot access Kconfig file [ {kconfig_file} ]\n")
parser.print_help()
sys.exit(1)
if args.debug:
logging.basicConfig(
filename="mtda-config.debug", encoding="utf-8", level=logging.DEBUG
)
app = ConfigApp(
kconfig=kconfig_file,
oldconfig=args.config,
output_file=args.output_file,
)
app.start()
if __name__ == "__main__":
start_config()