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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
|
from __future__ import annotations
import sys
import logging
from typing import Callable, Any, List, Iterator
from .menu import Menu
from ..general import SysCommand, secret
from ..hardware import has_uefi
from ..storage import storage
from ..output import log
from ..profiles import is_desktop_profile
from ..disk import encrypted_partitions
from ..locale_helpers import set_keyboard_language
from ..user_interaction import get_password, ask_for_a_timezone
from ..user_interaction import ask_ntp
from ..user_interaction import ask_for_swap
from ..user_interaction import ask_for_bootloader
from ..user_interaction import ask_hostname
from ..user_interaction import ask_for_audio_selection
from ..user_interaction import ask_additional_packages_to_install
from ..user_interaction import ask_to_configure_network
from ..user_interaction import ask_for_superuser_account
from ..user_interaction import ask_for_additional_users
from ..user_interaction import select_language
from ..user_interaction import select_mirror_regions
from ..user_interaction import select_locale_lang
from ..user_interaction import select_locale_enc
from ..user_interaction import select_disk_layout
from ..user_interaction import select_kernel
from ..user_interaction import select_encrypted_partitions
from ..user_interaction import select_harddrives
from ..user_interaction import select_profile
from ..user_interaction import select_archinstall_language
from ..user_interaction import select_additional_repositories
from ..translation import Translation
class Selector:
def __init__(
self,
description :str,
func :Callable = None,
display_func :Callable = None,
default :Any = None,
enabled :bool = False,
dependencies :List = [],
dependencies_not :List = [],
exec_func :Callable = None,
preview_func :Callable = None,
mandatory :bool = False
):
"""
Create a new menu selection entry
:param description: Text that will be displayed as the menu entry
:type description: str
:param func: Function that is called when the menu entry is selected
:type func: Callable
:param display_func: After specifying a setting for a menu item it is displayed
on the right side of the item as is; with this function one can modify the entry
to be displayed; e.g. when specifying a password one can display **** instead
:type display_func: Callable
:param default: Default value for this menu entry
:type default: Any
:param enabled: Specify if this menu entry should be displayed
:type enabled: bool
:param dependencies: Specify dependencies for this menu entry; if the dependencies
are not set yet, then this item is not displayed; e.g. disk_layout depends on selectiong
harddrive(s) first
:type dependencies: list
:param dependencies_not: These are the exclusive options; the menu item will only be
displayed if non of the entries in the list have been specified
:type dependencies_not: list
:param exec_func: A function with the name and the result of the selection as input parameter and which returns boolean.
Can be used for any action deemed necessary after selection. If it returns True, exits the menu loop, if False,
menu returns to the selection screen. If not specified it is assumed the return is False
:type exec_func: Callable
:param preview_func: A callable which invokws a preview screen (not implemented)
:type preview_func: Callable
:param mandatory: A boolean which determines that the field is mandatory, i.e. menu can not be exited if it is not set
:type mandatory: bool
"""
self._description = description
self.func = func
self._display_func = display_func
self._current_selection = default
self.enabled = enabled
self._dependencies = dependencies
self._dependencies_not = dependencies_not
self.exec_func = exec_func
self.preview_func = preview_func
self.mandatory = mandatory
@property
def dependencies(self) -> dict:
return self._dependencies
@property
def dependencies_not(self) -> dict:
return self._dependencies_not
@property
def current_selection(self):
return self._current_selection
def set_enabled(self, status :bool = True):
self.enabled = status
def update_description(self, description :str):
self._description = description
def menu_text(self) -> str:
current = ''
if self._display_func:
current = self._display_func(self._current_selection)
else:
if self._current_selection is not None:
current = str(self._current_selection)
if current:
padding = 35 - len(str(self._description))
current = ' ' * padding + f'SET: {current}'
return f'{self._description} {current}'
@property
def text(self):
return self.menu_text()
def set_current_selection(self, current :str):
self._current_selection = current
def has_selection(self) -> bool:
if self._current_selection is None:
return False
return True
def get_selection(self) -> Any:
return self._current_selection
def is_empty(self) -> bool:
if self._current_selection is None:
return True
elif isinstance(self._current_selection, (str, list, dict)) and len(self._current_selection) == 0:
return True
return False
def is_enabled(self) -> bool:
return self.enabled
def is_mandatory(self) -> bool:
return self.mandatory
def set_mandatory(self, status :bool = True):
self.mandatory = status
if status and not self.is_enabled():
self.set_enabled(True)
class GeneralMenu:
def __init__(self, data_store :dict = None, auto_cursor=False):
"""
Create a new selection menu.
:param data_store: Area (Dict) where the resulting data will be held. At least an entry for each option. Default area is self._data_store (not preset in the call, due to circular references
:type data_store: Dict
:param auto_cursor: Boolean which determines if the cursor stays on the first item (false) or steps each invocation of a selection entry (true)
:type auto_cursor: bool
"""
self._translation = Translation.load_nationalization()
self.is_context_mgr = False
self._data_store = data_store if data_store is not None else {}
self.auto_cursor = auto_cursor
self._menu_options = {}
self._setup_selection_menu_options()
def __enter__(self, *args :Any, **kwargs :Any) -> GeneralMenu:
self.is_context_mgr = True
return self
def __exit__(self, *args :Any, **kwargs :Any) -> None:
# TODO: https://stackoverflow.com/questions/28157929/how-to-safely-handle-an-exception-inside-a-context-manager
# TODO: skip processing when it comes from a planified exit
if len(args) >= 2 and args[1]:
log(args[1], level=logging.ERROR, fg='red')
print(" Please submit this issue (and file) to https://github.com/archlinux/archinstall/issues")
raise args[1]
for key in self._menu_options:
sel = self._menu_options[key]
if key and key not in self._data_store:
self._data_store[key] = sel._current_selection
self.exit_callback()
def _setup_selection_menu_options(self):
""" Define the menu options.
Menu options can be defined here in a subclass or done per progam calling self.set_option()
"""
return
def pre_callback(self, selector_name):
""" will be called before each action in the menu """
return
def post_callback(self, selector_name :str, value :Any):
""" will be called after each action in the menu """
return True
def exit_callback(self):
""" will be called at the end of the processing of the menu """
return
def synch(self, selector_name :str, omit_if_set :bool = False,omit_if_disabled :bool = False):
""" loads menu options with data_store value """
arg = self._data_store.get(selector_name, None)
# don't display the menu option if it was defined already
if arg is not None and omit_if_set:
return
if not self.option(selector_name).is_enabled() and omit_if_disabled:
return
if arg is not None:
self._menu_options[selector_name].set_current_selection(arg)
def enable(self, selector_name :str, omit_if_set :bool = False , mandatory :bool = False):
""" activates menu options """
if self._menu_options.get(selector_name, None):
self._menu_options[selector_name].set_enabled(True)
if mandatory:
self._menu_options[selector_name].set_mandatory(True)
self.synch(selector_name,omit_if_set)
else:
print(f'No selector found: {selector_name}')
sys.exit(1)
def run(self):
""" Calls the Menu framework"""
# we synch all the options just in case
for item in self.list_options():
self.synch(item)
cursor_pos = None
while True:
# Before continuing, set the preferred keyboard layout/language in the current terminal.
# This will just help the user with the next following questions.
self._set_kb_language()
enabled_menus = self._menus_to_enable()
menu_text = [m.text for m in enabled_menus.values()]
selection = Menu('Set/Modify the below options', menu_text, sort=False, cursor_index=cursor_pos).run()
if selection and self.auto_cursor:
cursor_pos = menu_text.index(selection) + 1 # before the strip otherwise fails
if cursor_pos >= len(menu_text):
cursor_pos = len(menu_text) - 1
selection = selection.strip()
if selection:
# if this calls returns false, we exit the menu. We allow for an callback for special processing on realeasing control
if not self._process_selection(selection):
break
if not self.is_context_mgr:
self.__exit__()
def _process_selection(self, selection :str) -> bool:
""" determines and executes the selection y
Can / Should be extended to handle specific selection issues
Returns true if the menu shall continue, False if it has ended
"""
# find the selected option in our option list
option = [[k, v] for k, v in self._menu_options.items() if v.text.strip() == selection]
if len(option) != 1:
raise ValueError(f'Selection not found: {selection}')
selector_name = option[0][0]
selector = option[0][1]
return self.exec_option(selector_name,selector)
def exec_option(self,selector_name :str, p_selector :Selector = None) -> bool:
""" processes the exection of a given menu entry
- pre process callback
- selection function
- post process callback
- exec action
returns True if the loop has to continue, false if the loop can be closed
"""
if not p_selector:
selector = self.option(selector_name)
else:
selector = p_selector
self.pre_callback(selector_name)
result = None
if selector.func:
# TODO insert code to allow selector functions with preset value
result = selector.func()
self._menu_options[selector_name].set_current_selection(result)
self._data_store[selector_name] = result
exec_ret_val = selector.exec_func(selector_name,result) if selector.exec_func else False
self.post_callback(selector_name,result)
if exec_ret_val and self._check_mandatory_status():
return False
return True
""" old behaviour
# we allow for a callback after we get the result
self.post_callback(selector_name,result)
# we have a callback, by option, to determine if we can exit the menu. Only if ALL mandatory fields are written
if selector.exec_func:
if selector.exec_func(result) and self._check_mandatory_status():
return False
"""
return True
def _set_kb_language(self):
""" general for ArchInstall"""
# Before continuing, set the preferred keyboard layout/language in the current terminal.
# This will just help the user with the next following questions.
if self._data_store.get('keyboard-layout', None) and len(self._data_store['keyboard-layout']):
set_keyboard_language(self._data_store['keyboard-layout'])
def _verify_selection_enabled(self, selection_name :str) -> bool:
""" general """
if selection := self._menu_options.get(selection_name, None):
if not selection.enabled:
return False
if len(selection.dependencies) > 0:
for d in selection.dependencies:
if not self._verify_selection_enabled(d) or self._menu_options.get(d).is_empty():
return False
if len(selection.dependencies_not) > 0:
for d in selection.dependencies_not:
if not self._menu_options.get(d).is_empty():
return False
return True
raise ValueError(f'No selection found: {selection_name}')
def _menus_to_enable(self) -> dict:
""" general """
enabled_menus = {}
for name, selection in self._menu_options.items():
if self._verify_selection_enabled(name):
enabled_menus[name] = selection
return enabled_menus
def option(self,name :str) -> Selector:
# TODO check inexistent name
return self._menu_options[name]
def list_options(self) -> Iterator:
""" Iterator to retrieve the enabled menu option names
"""
for item in self._menu_options:
yield item
def list_enabled_options(self) -> Iterator:
""" Iterator to retrieve the enabled menu options at a given time.
The results are dynamic (if between calls to the iterator some elements -still not retrieved- are (de)activated
"""
for item in self._menu_options:
if item in self._menus_to_enable():
yield item
def set_option(self, name :str, selector :Selector):
self._menu_options[name] = selector
self.synch(name)
def _check_mandatory_status(self) -> bool:
for field in self._menu_options:
option = self._menu_options[field]
if option.is_mandatory() and not option.has_selection():
return False
return True
def set_mandatory(self, field :str, status :bool):
self.option(field).set_mandatory(status)
def mandatory_overview(self) -> [int, int]:
mandatory_fields = 0
mandatory_waiting = 0
for field in self._menu_options:
option = self._menu_options[field]
if option.is_mandatory():
mandatory_fields += 1
if not option.has_selection():
mandatory_waiting += 1
return mandatory_fields, mandatory_waiting
def _select_archinstall_language(self, default_lang):
language = select_archinstall_language(default_lang)
self._translation.activate(language)
return language
class GlobalMenu(GeneralMenu):
def __init__(self,data_store):
super().__init__(data_store=data_store, auto_cursor=True)
def _setup_selection_menu_options(self):
self._menu_options['archinstall-language'] = \
Selector(
_('Select Archinstall language'),
lambda: self._select_archinstall_language('English'),
default='English',
enabled=True)
self._menu_options['keyboard-layout'] = \
Selector(_('Select keyboard layout'), lambda: select_language('us'), default='us')
self._menu_options['mirror-region'] = \
Selector(
_('Select mirror region'),
lambda: select_mirror_regions(),
display_func=lambda x: list(x.keys()) if x else '[]',
default={})
self._menu_options['sys-language'] = \
Selector(_('Select locale language'), lambda: select_locale_lang('en_US'), default='en_US')
self._menu_options['sys-encoding'] = \
Selector(_('Select locale encoding'), lambda: select_locale_enc('utf-8'), default='utf-8')
self._menu_options['harddrives'] = \
Selector(
_('Select harddrives'),
lambda: self._select_harddrives())
self._menu_options['disk_layouts'] = \
Selector(
_('Select disk layout'),
lambda: select_disk_layout(
storage['arguments'].get('harddrives', []),
storage['arguments'].get('advanced', False)
),
dependencies=['harddrives'])
self._menu_options['!encryption-password'] = \
Selector(
_('Set encryption password'),
lambda: self._select_encrypted_password(),
display_func=lambda x: secret(x) if x else 'None',
dependencies=['harddrives'])
self._menu_options['swap'] = \
Selector(
_('Use swap'),
lambda: ask_for_swap(),
default=True)
self._menu_options['bootloader'] = \
Selector(
_('Select bootloader'),
lambda: ask_for_bootloader(storage['arguments'].get('advanced', False)),
default="systemd-bootctl" if has_uefi() else "grub-install")
self._menu_options['hostname'] = \
Selector(
_('Specify hostname'),
lambda: ask_hostname(),
default='archlinux')
self._menu_options['!root-password'] = \
Selector(
_('Set root password'),
lambda: self._set_root_password(),
display_func=lambda x: secret(x) if x else 'None')
self._menu_options['!superusers'] = \
Selector(
_('Specify superuser account'),
lambda: self._create_superuser_account(),
dependencies_not=['!root-password'],
display_func=lambda x: list(x.keys()) if x else '')
self._menu_options['!users'] = \
Selector(
_('Specify user account'),
lambda: self._create_user_account(),
default={},
display_func=lambda x: list(x.keys()) if x else '[]')
self._menu_options['profile'] = \
Selector(
_('Specify profile'),
lambda: self._select_profile(),
display_func=lambda x: x if x else 'None')
self._menu_options['audio'] = \
Selector(
_('Select audio'),
lambda: ask_for_audio_selection(is_desktop_profile(storage['arguments'].get('profile', None))))
self._menu_options['kernels'] = \
Selector(
_('Select kernels'),
lambda: select_kernel(),
default=['linux'])
self._menu_options['packages'] = \
Selector(
_('Additional packages to install'),
lambda: ask_additional_packages_to_install(storage['arguments'].get('packages', None)),
default=[])
self._menu_options['additional-repositories'] = \
Selector(
_('Additional repositories to enable'),
lambda: select_additional_repositories(),
default=[])
self._menu_options['nic'] = \
Selector(
_('Configure network'),
lambda: ask_to_configure_network(),
display_func=lambda x: x if x else _('Not configured, unavailable unless setup manually'),
default={})
self._menu_options['timezone'] = \
Selector(
_('Select timezone'),
lambda: ask_for_a_timezone(),
default='UTC')
self._menu_options['ntp'] = \
Selector(
_('Set automatic time sync (NTP)'),
lambda: self._select_ntp(),
default=True)
self._menu_options['install'] = \
Selector(
self._install_text(),
exec_func=lambda n,v: True if self._missing_configs() == 0 else False,
enabled=True)
self._menu_options['abort'] = Selector(_('Abort'), exec_func=lambda n,v:exit(1), enabled=True)
def _update_install(self,name :str = None ,result :Any = None):
text = self._install_text()
self._menu_options.get('install').update_description(text)
def post_callback(self,name :str = None ,result :Any = None):
self._update_install(name,result)
def exit_callback(self):
if self._data_store.get('harddrives', None) and self._data_store.get('!encryption-password', None):
# If no partitions was marked as encrypted, but a password was supplied and we have some disks to format..
# Then we need to identify which partitions to encrypt. This will default to / (root).
if len(list(encrypted_partitions(storage['arguments'].get('disk_layouts', [])))) == 0:
storage['arguments']['disk_layouts'] = select_encrypted_partitions(
storage['arguments']['disk_layouts'], storage['arguments']['!encryption-password'])
def _install_text(self):
missing = self._missing_configs()
if missing > 0:
return _('Install ({} config(s) missing)').format(missing)
return 'Install'
def _missing_configs(self):
def check(s):
return self._menu_options.get(s).has_selection()
missing = 0
if not check('bootloader'):
missing += 1
if not check('hostname'):
missing += 1
if not check('audio'):
missing += 1
if not check('timezone'):
missing += 1
if not check('!root-password') and not check('!superusers'):
missing += 1
if not check('harddrives'):
missing += 1
if check('harddrives'):
if not self._menu_options.get('harddrives').is_empty() and not check('disk_layouts'):
missing += 1
return missing
def _set_root_password(self):
prompt = str(_('Enter root password (leave blank to disable root): '))
password = get_password(prompt=prompt)
# TODO: Do we really wanna wipe the !superusers and !users if root password is set?
# What if they set a superuser first, but then decides to set a root password?
if password is not None:
self._menu_options.get('!superusers').set_current_selection(None)
storage['arguments']['!users'] = {}
storage['arguments']['!superusers'] = {}
return password
def _select_encrypted_password(self):
if passwd := get_password(prompt=str(_('Enter disk encryption password (leave blank for no encryption): '))):
return passwd
else:
return None
def _select_ntp(self) -> bool:
ntp = ask_ntp()
value = str(ntp).lower()
SysCommand(f'timedatectl set-ntp {value}')
return ntp
def _select_harddrives(self):
old_haddrives = storage['arguments'].get('harddrives', [])
harddrives = select_harddrives()
# in case the harddrives got changed we have to reset the disk layout as well
if old_haddrives != harddrives:
self._menu_options.get('disk_layouts').set_current_selection(None)
storage['arguments']['disk_layouts'] = {}
if not harddrives:
prompt = _(
"You decided to skip harddrive selection\nand will use whatever drive-setup is mounted at {} (experimental)\n"
"WARNING: Archinstall won't check the suitability of this setup\n"
"Do you wish to continue?"
).format(storage['MOUNT_POINT'])
choice = Menu(prompt, ['yes', 'no'], default_option='yes').run()
if choice == 'no':
return self._select_harddrives()
return harddrives
def _select_profile(self):
profile = select_profile()
# Check the potentially selected profiles preparations to get early checks if some additional questions are needed.
if profile and profile.has_prep_function():
namespace = f'{profile.namespace}.py'
with profile.load_instructions(namespace=namespace) as imported:
if not imported._prep_function():
log(' * Profile\'s preparation requirements was not fulfilled.', fg='red')
exit(1)
return profile
def _create_superuser_account(self):
superuser = ask_for_superuser_account(str(_('Create a required super-user with sudo privileges: ')), forced=True)
return superuser
def _create_user_account(self):
users, superusers = ask_for_additional_users(str(_('Enter a username to create an additional user (leave blank to skip): ')))
storage['arguments']['!superusers'] = {**storage['arguments'].get('!superusers', {}), **superusers}
return users
|