From 1f2fe467d1956fd3d4550c33069da2f9f0017c72 Mon Sep 17 00:00:00 2001 From: SecondThundeR Date: Thu, 29 Apr 2021 00:59:35 +0300 Subject: Update generic_multi_select Changes: - Add useful checks from `generic_select` - Sorting is now disabled by default (As many lists are already sorted) - Some checks have been changed (This includes unnecessary checks with `len()`, etc.) - Removed x, y from `print_large_list` as they aren't used in code - Added check for string to strip it without getting `AttributeError` - Switched to RequirementError handling as in `generic_select` - Added a log when the default option is selected with unselected options by the user - Added break when adding default option to empty list (See comments for more info) - Added support for selecting option by name --- archinstall/lib/user_interaction.py | 70 +++++++++++++++++++++++++------------ 1 file changed, 48 insertions(+), 22 deletions(-) (limited to 'archinstall/lib/user_interaction.py') diff --git a/archinstall/lib/user_interaction.py b/archinstall/lib/user_interaction.py index 451251cd..8a4b4c49 100644 --- a/archinstall/lib/user_interaction.py +++ b/archinstall/lib/user_interaction.py @@ -99,7 +99,18 @@ def print_large_list(options, padding=5, margin_bottom=0, separator=': '): return column, row -def generic_multi_select(options, text="Select one or more of the options above (leave blank to continue): ", sort=True, default=None, allow_empty=False): +def generic_multi_select(options, text="Select one or more of the options above (leave blank to continue): ", sort=False, default=None, allow_empty=False): + # For now, we check for list, but in future it's better to have support for dictionary + # (At the moment there are no cases of using dictionaries with this function) + if type(options) not in [list]: + log(f" * Generic multi-select doesn't support ({type(options)}) as type of options * ", fg='red') + log(" * If problem persists, please create an issue on https://github.com/archlinux/archinstall/issues * ", fg='yellow') + raise RequirementError("generic_multi_select() requires list as options.") + if not options: + log(f" * Generic multi-select didn't find any options to choose from * ", fg='red') + log(" * If problem persists, please create an issue on https://github.com/archlinux/archinstall/issues * ", fg='yellow') + raise RequirementError('generic_multi_select() requires at least one option to proceed.') + # After passing the checks, function continues to work if sort: options = sorted(options) @@ -108,7 +119,7 @@ def generic_multi_select(options, text="Select one or more of the options above selected_options = [] while True: - if len(selected_options) <= 0 and default and default in options: + if not selected_options and default in options: selected_options.append(default) printed_options = [] @@ -119,32 +130,47 @@ def generic_multi_select(options, text="Select one or more of the options above printed_options.append(f'{option}') section.clear(0, get_terminal_height()-section._cursor_y-1) - x, y = print_large_list(printed_options, margin_bottom=2) + print_large_list(printed_options, margin_bottom=2) section._cursor_y = len(printed_options) section._cursor_x = 0 section.write_line(text) section.input_pos = section._cursor_x selected_option = section.get_keyboard_input(end=None) - - if selected_option is None: - if len(selected_options) <= 0 and default: - selected_options = [default] - - if len(selected_options) or allow_empty is True: - break - else: - log('* Need to select at least one option!', fg='red') - continue - - elif selected_option.isdigit(): - if (selected_option := int(selected_option)) >= len(options): - log('* Option is out of range, please select another one!', fg='red') - continue - selected_option = options[selected_option] - if selected_option in selected_options: - selected_options.remove(selected_option) + # This string check is necessary to correct work with it + # Without this, Python can raise AttributeError because of stripping `None` + # It also allows you to remove empty spaces if the user accidentally entered them. + if isinstance(selected_option, str): + selected_option = selected_option.strip() + try: + if not selected_option: + # Added break when adding default option to empty list + # So that the check doesn't go to the next elif + # Since it still breaks the loop + if not selected_options and default: + selected_options = [default] + log(f'Default option selected: "{default}"', fg='yellow') + break + elif selected_options or allow_empty: + break + else: + raise RequirementError('Please select at least one option to continue!') + elif selected_option.isnumeric(): + if (selected_option := int(selected_option)) >= len(options): + raise RequirementError(f'Selected option "{selected_option}" is out of range!') + selected_option = options[selected_option] + if selected_option in selected_options: + selected_options.remove(selected_option) + else: + selected_options.append(selected_option) + elif selected_option in options: + if selected_option in selected_options: + selected_options.remove(selected_option) + else: + selected_options.append(selected_option) else: - selected_options.append(selected_option) + raise RequirementError(f'Selected option "{selected_option}" does not exist in available options') + except RequirementError as e: + log(f" * {e} * ", fg='red') return selected_options -- cgit v1.2.3-54-g00ecf From 5c8748dbb5f663f579b6a462d317162de163fa84 Mon Sep 17 00:00:00 2001 From: SecondThundeR Date: Thu, 29 Apr 2021 01:13:19 +0300 Subject: Update generic_select Changes: - Moved some functions for options below checks for the correctness of passed options - Removed unnecessary `continue` from `except ...`, since the loop will return to the beginning anyway - Added stripping of `selected_option` straight on input - Changed check `len() == 0` to `not ...` - Returned changing string to number on check === - Removed '!' as they look weird inside such ` * ... * ` log style (Change for generic_multi_select) --- archinstall/lib/user_interaction.py | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) (limited to 'archinstall/lib/user_interaction.py') diff --git a/archinstall/lib/user_interaction.py b/archinstall/lib/user_interaction.py index 8a4b4c49..437ca68f 100644 --- a/archinstall/lib/user_interaction.py +++ b/archinstall/lib/user_interaction.py @@ -153,10 +153,10 @@ def generic_multi_select(options, text="Select one or more of the options above elif selected_options or allow_empty: break else: - raise RequirementError('Please select at least one option to continue!') + raise RequirementError('Please select at least one option to continue') elif selected_option.isnumeric(): if (selected_option := int(selected_option)) >= len(options): - raise RequirementError(f'Selected option "{selected_option}" is out of range!') + raise RequirementError(f'Selected option "{selected_option}" is out of range') selected_option = options[selected_option] if selected_option in selected_options: selected_options.remove(selected_option) @@ -461,20 +461,24 @@ def generic_select(options, input_text="Select one of the above by index or abso this function returns an item from list, a string, or None """ - # Checking if options are different from `list` or `dict` + # Checking if the options are different from `list` or `dict` or if they are empty if type(options) not in [list, dict]: log(f" * Generic select doesn't support ({type(options)}) as type of options * ", fg='red') log(" * If problem persists, please create an issue on https://github.com/archlinux/archinstall/issues * ", fg='yellow') raise RequirementError("generic_select() requires list or dictionary as options.") - # To allow only `list` and `dict`, converting values of options here. - # Therefore, now we can only provide the dictionary itself - if type(options) == dict: options = list(options.values()) - if sort: options = sorted(options) # As we pass only list and dict (converted to list), we can skip converting to list - if len(options) == 0: + if not options: log(f" * Generic select didn't find any options to choose from * ", fg='red') log(" * If problem persists, please create an issue on https://github.com/archlinux/archinstall/issues * ", fg='yellow') raise RequirementError('generic_select() requires at least one option to proceed.') - + # After passing the checks, function continues to work + if type(options) == dict: + # To allow only `list` and `dict`, converting values of options here. + # Therefore, now we can only provide the dictionary itself + options = list(options.values()) + if sort: + # As we pass only list and dict (converted to list), we can skip converting to list + options = sorted(options) + # Added ability to disable the output of options items, # if another function displays something different from this @@ -486,8 +490,8 @@ def generic_select(options, input_text="Select one of the above by index or abso # Now the try...except block handles validation for invalid input from the user while True: try: - selected_option = input(input_text) - if len(selected_option.strip()) == 0: + selected_option = input(input_text).strip() + if not selected_option: # `allow_empty_input` parameter handles return of None on empty input, if necessary # Otherwise raise `RequirementError` if allow_empty_input: @@ -495,8 +499,7 @@ def generic_select(options, input_text="Select one of the above by index or abso raise RequirementError('Please select an option to continue') # Replaced `isdigit` with` isnumeric` to discard all negative numbers elif selected_option.isnumeric(): - selected_option = int(selected_option) - if selected_option >= len(options): + if (selected_option := int(selected_option)) >= len(options): raise RequirementError(f'Selected option "{selected_option}" is out of range') selected_option = options[selected_option] break @@ -506,7 +509,6 @@ def generic_select(options, input_text="Select one of the above by index or abso raise RequirementError(f'Selected option "{selected_option}" does not exist in available options') except RequirementError as err: log(f" * {err} * ", fg='red') - continue return selected_option -- cgit v1.2.3-54-g00ecf From 5f1156f80e4038a52719f4ba01c87eae4f0a8660 Mon Sep 17 00:00:00 2001 From: SecondThundeR Date: Thu, 29 Apr 2021 01:30:35 +0300 Subject: Fix multi select and video card driver selection Changes: - Rephrased input text for kernel selection - Fixed crash with empty video card driver selection - Removed log info for default option --- archinstall/lib/user_interaction.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'archinstall/lib/user_interaction.py') diff --git a/archinstall/lib/user_interaction.py b/archinstall/lib/user_interaction.py index 437ca68f..10d74b63 100644 --- a/archinstall/lib/user_interaction.py +++ b/archinstall/lib/user_interaction.py @@ -118,10 +118,10 @@ def generic_multi_select(options, text="Select one or more of the options above selected_options = [] - while True: - if not selected_options and default in options: - selected_options.append(default) + if not selected_options and default in options: + selected_options.append(default) + while True: printed_options = [] for option in options: if option in selected_options: @@ -148,7 +148,6 @@ def generic_multi_select(options, text="Select one or more of the options above # Since it still breaks the loop if not selected_options and default: selected_options = [default] - log(f'Default option selected: "{default}"', fg='yellow') break elif selected_options or allow_empty: break @@ -687,7 +686,7 @@ def select_driver(options=AVAILABLE_GFX_DRIVERS): elif b'amd' in line.lower(): print(' ** AMD card detected, suggested driver: AMD / ATI **') - initial_option = generic_select(drivers, input_text="Select your graphics card driver: ") + initial_option = generic_select(drivers, input_text="Select your graphics card driver: ", allow_empty_input=False) selected_driver = options[initial_option] if type(selected_driver) == dict: @@ -719,6 +718,6 @@ def select_kernel(options): kernels = sorted(list(options)) if kernels: - return generic_multi_select(kernels, f"Choose which kernel to use (leave blank for default: {DEFAULT_KERNEL}): ", default=DEFAULT_KERNEL) + return generic_multi_select(kernels, f"Choose which kernels to use (Default value for empty selection: {DEFAULT_KERNEL}): ", default=DEFAULT_KERNEL) raise RequirementError("Selecting kernels require a least one kernel to be given as an option.") -- cgit v1.2.3-54-g00ecf From ea14e860c7f730897544cb1bdb43964e25785c74 Mon Sep 17 00:00:00 2001 From: SecondThundeR Date: Thu, 29 Apr 2021 18:47:31 +0300 Subject: Update `user_interaction.py` - Reverted some changes for default options in multi select - Added check for dict and convert from dict to list - Replaced spaces with tabs for certain comment line --- archinstall/lib/user_interaction.py | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) (limited to 'archinstall/lib/user_interaction.py') diff --git a/archinstall/lib/user_interaction.py b/archinstall/lib/user_interaction.py index 10d74b63..f06354d2 100644 --- a/archinstall/lib/user_interaction.py +++ b/archinstall/lib/user_interaction.py @@ -100,17 +100,18 @@ def print_large_list(options, padding=5, margin_bottom=0, separator=': '): def generic_multi_select(options, text="Select one or more of the options above (leave blank to continue): ", sort=False, default=None, allow_empty=False): - # For now, we check for list, but in future it's better to have support for dictionary - # (At the moment there are no cases of using dictionaries with this function) - if type(options) not in [list]: + # Checking if the options are different from `list` or `dict` or if they are empty + if type(options) not in [list, dict]: log(f" * Generic multi-select doesn't support ({type(options)}) as type of options * ", fg='red') log(" * If problem persists, please create an issue on https://github.com/archlinux/archinstall/issues * ", fg='yellow') - raise RequirementError("generic_multi_select() requires list as options.") + raise RequirementError("generic_multi_select() requires list or dictionary as options.") if not options: log(f" * Generic multi-select didn't find any options to choose from * ", fg='red') log(" * If problem persists, please create an issue on https://github.com/archlinux/archinstall/issues * ", fg='yellow') raise RequirementError('generic_multi_select() requires at least one option to proceed.') # After passing the checks, function continues to work + if type(options) == dict: + options = list(options.values()) if sort: options = sorted(options) @@ -118,10 +119,10 @@ def generic_multi_select(options, text="Select one or more of the options above selected_options = [] - if not selected_options and default in options: - selected_options.append(default) - while True: + if not selected_options and default in options: + selected_options.append(default) + printed_options = [] for option in options: if option in selected_options: @@ -136,19 +137,15 @@ def generic_multi_select(options, text="Select one or more of the options above section.write_line(text) section.input_pos = section._cursor_x selected_option = section.get_keyboard_input(end=None) - # This string check is necessary to correct work with it + # This string check is necessary to correct work with it # Without this, Python can raise AttributeError because of stripping `None` # It also allows you to remove empty spaces if the user accidentally entered them. if isinstance(selected_option, str): selected_option = selected_option.strip() try: if not selected_option: - # Added break when adding default option to empty list - # So that the check doesn't go to the next elif - # Since it still breaks the loop if not selected_options and default: selected_options = [default] - break elif selected_options or allow_empty: break else: @@ -718,6 +715,6 @@ def select_kernel(options): kernels = sorted(list(options)) if kernels: - return generic_multi_select(kernels, f"Choose which kernels to use (Default value for empty selection: {DEFAULT_KERNEL}): ", default=DEFAULT_KERNEL) + return generic_multi_select(kernels, f"Choose which kernels to use (leave blank for default: {DEFAULT_KERNEL}): ", default=DEFAULT_KERNEL) raise RequirementError("Selecting kernels require a least one kernel to be given as an option.") -- cgit v1.2.3-54-g00ecf From efadd4a42694d87b3fe86bd7bcedf1da9a9e1773 Mon Sep 17 00:00:00 2001 From: SecondThundeR Date: Fri, 30 Apr 2021 22:31:31 +0300 Subject: Revert disabling default sorting This change reverts a previous change that disabled sorting by default in the multi select function, which would be better disabled manually for pre-sorted lists than manually enabling for unsorted lists. Also, comments of the line check have been slightly changed --- archinstall/lib/user_interaction.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'archinstall/lib/user_interaction.py') diff --git a/archinstall/lib/user_interaction.py b/archinstall/lib/user_interaction.py index f06354d2..81f04db4 100644 --- a/archinstall/lib/user_interaction.py +++ b/archinstall/lib/user_interaction.py @@ -99,7 +99,7 @@ def print_large_list(options, padding=5, margin_bottom=0, separator=': '): return column, row -def generic_multi_select(options, text="Select one or more of the options above (leave blank to continue): ", sort=False, default=None, allow_empty=False): +def generic_multi_select(options, text="Select one or more of the options above (leave blank to continue): ", sort=True, default=None, allow_empty=False): # Checking if the options are different from `list` or `dict` or if they are empty if type(options) not in [list, dict]: log(f" * Generic multi-select doesn't support ({type(options)}) as type of options * ", fg='red') @@ -138,8 +138,8 @@ def generic_multi_select(options, text="Select one or more of the options above section.input_pos = section._cursor_x selected_option = section.get_keyboard_input(end=None) # This string check is necessary to correct work with it - # Without this, Python can raise AttributeError because of stripping `None` - # It also allows you to remove empty spaces if the user accidentally entered them. + # Without this, Python will raise AttributeError because of stripping `None` + # It also allows to remove empty spaces if the user accidentally entered them. if isinstance(selected_option, str): selected_option = selected_option.strip() try: @@ -715,6 +715,6 @@ def select_kernel(options): kernels = sorted(list(options)) if kernels: - return generic_multi_select(kernels, f"Choose which kernels to use (leave blank for default: {DEFAULT_KERNEL}): ", default=DEFAULT_KERNEL) + return generic_multi_select(kernels, f"Choose which kernels to use (leave blank for default: {DEFAULT_KERNEL}): ", default=DEFAULT_KERNEL, sort=False) raise RequirementError("Selecting kernels require a least one kernel to be given as an option.") -- cgit v1.2.3-54-g00ecf From 493814d8bd9a89089c6bedf82f9c85ca7075edfb Mon Sep 17 00:00:00 2001 From: SecondThundeR Date: Thu, 6 May 2021 21:38:20 +0300 Subject: Add default graphics card driver option --- archinstall/lib/user_interaction.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'archinstall/lib/user_interaction.py') diff --git a/archinstall/lib/user_interaction.py b/archinstall/lib/user_interaction.py index 81f04db4..aea708c8 100644 --- a/archinstall/lib/user_interaction.py +++ b/archinstall/lib/user_interaction.py @@ -673,6 +673,7 @@ def select_driver(options=AVAILABLE_GFX_DRIVERS): """ drivers = sorted(list(options)) + default_option = options["All open-source (default)"] if drivers: lspci = sys_command(f'/usr/bin/lspci') @@ -683,7 +684,11 @@ def select_driver(options=AVAILABLE_GFX_DRIVERS): elif b'amd' in line.lower(): print(' ** AMD card detected, suggested driver: AMD / ATI **') - initial_option = generic_select(drivers, input_text="Select your graphics card driver: ", allow_empty_input=False) + initial_option = generic_select(drivers, input_text="Select your graphics card driver: ") + + if not initial_option: + return default_option + selected_driver = options[initial_option] if type(selected_driver) == dict: -- cgit v1.2.3-54-g00ecf From 24a14d1800e990e2e90dd628b8460f85fe3d1495 Mon Sep 17 00:00:00 2001 From: aboven Date: Sat, 8 May 2021 13:18:03 +0200 Subject: fix error when to many options and calculation spaces --- archinstall/lib/user_interaction.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'archinstall/lib/user_interaction.py') diff --git a/archinstall/lib/user_interaction.py b/archinstall/lib/user_interaction.py index be01594e..1240e0be 100644 --- a/archinstall/lib/user_interaction.py +++ b/archinstall/lib/user_interaction.py @@ -83,16 +83,18 @@ def get_password(prompt="Enter a password: "): def print_large_list(options, padding=5, margin_bottom=0, separator=': '): highest_index_number_length = len(str(len(options))) longest_line = highest_index_number_length + len(separator) + get_longest_option(options) + padding + spaces_without_option = longest_line - (len(separator) + highest_index_number_length) max_num_of_columns = get_terminal_width() // longest_line max_options_in_cells = max_num_of_columns * (get_terminal_height()-margin_bottom) if (len(options) > max_options_in_cells): for index, option in enumerate(options): print(f"{index}: {option}") + return 1, index else: for row in range(0, (get_terminal_height()-margin_bottom)): for column in range(row, len(options), (get_terminal_height()-margin_bottom)): - spaces = " "*(longest_line - len(options[column])) + spaces = " "*(spaces_without_option - len(options[column])) print(f"{str(column): >{highest_index_number_length}}{separator}{options[column]}", end = spaces) print() -- cgit v1.2.3-54-g00ecf From 69d675f4aa14b4957d6376d642bec5cf4b96674e Mon Sep 17 00:00:00 2001 From: Dylan Taylor Date: Sat, 15 May 2021 12:29:57 -0400 Subject: Many more manual changes --- archinstall/lib/disk.py | 25 ++++---- archinstall/lib/exceptions.py | 20 ++++++- archinstall/lib/general.py | 21 ++++--- archinstall/lib/hardware.py | 11 ++-- archinstall/lib/installer.py | 42 +++++++------- archinstall/lib/locale_helpers.py | 4 ++ archinstall/lib/luks.py | 20 +++---- archinstall/lib/mirrors.py | 14 +++-- archinstall/lib/networking.py | 22 +++++--- archinstall/lib/output.py | 34 ++++++----- archinstall/lib/packages.py | 17 ++++-- archinstall/lib/profiles.py | 24 +++++--- archinstall/lib/services.py | 4 +- archinstall/lib/storage.py | 2 +- archinstall/lib/systemd.py | 4 +- archinstall/lib/user_interaction.py | 110 ++++++++++++++++++++++-------------- examples/guided.py | 4 +- 17 files changed, 230 insertions(+), 148 deletions(-) (limited to 'archinstall/lib/user_interaction.py') diff --git a/archinstall/lib/disk.py b/archinstall/lib/disk.py index fd08ea63..0a0337ec 100644 --- a/archinstall/lib/disk.py +++ b/archinstall/lib/disk.py @@ -1,12 +1,11 @@ -from typing import Optional -import glob, re, os, json, time, hashlib -import pathlib, traceback, logging +import glob +import pathlib +import re from collections import OrderedDict -from .exceptions import DiskError + from .general import * -from .output import log -from .storage import storage from .hardware import hasUEFI +from .output import log ROOT_DIR_PATTERN = re.compile('^.*?/devices') GPT = 0b00000001 @@ -172,7 +171,7 @@ class Partition(): self.mount(mountpoint) mount_information = get_mount_info(self.path) - + if self.mountpoint != mount_information.get('target', None) and mountpoint: raise DiskError(f"{self} was given a mountpoint but the actual mountpoint differs: {mount_information.get('target', None)}") @@ -250,14 +249,14 @@ class Partition(): def has_content(self): if not get_filesystem_type(self.path): return False - + temporary_mountpoint = '/tmp/'+hashlib.md5(bytes(f"{time.time()}", 'UTF-8')+os.urandom(12)).hexdigest() temporary_path = pathlib.Path(temporary_mountpoint) temporary_path.mkdir(parents=True, exist_ok=True) if (handle := sys_command(f'/usr/bin/mount {self.path} {temporary_mountpoint}')).exit_code != 0: raise DiskError(f'Could not mount and check for content on {self.path} because: {b"".join(handle)}') - + files = len(glob.glob(f"{temporary_mountpoint}/*")) sys_command(f'/usr/bin/umount {temporary_mountpoint}') @@ -385,7 +384,7 @@ class Partition(): sys_command(f'/usr/bin/mount {self.path} {target}') except SysCallError as err: raise err - + self.mountpoint = target return True @@ -446,7 +445,7 @@ class Filesystem(): raise DiskError(f'Problem setting the partition format to GPT:', f'/usr/bin/parted -s {self.blockdevice.device} mklabel msdos') else: raise DiskError(f'Unknown mode selected to format in: {self.mode}') - + # TODO: partition_table_type is hardcoded to GPT at the moment. This has to be changed. elif self.mode == self.blockdevice.partition_table_type: log(f'Kept partition format {self.mode} for {self.blockdevice}', level=logging.DEBUG) @@ -513,7 +512,7 @@ class Filesystem(): def add_partition(self, type, start, end, format=None): log(f'Adding partition to {self.blockdevice}', level=logging.INFO) - + previous_partitions = self.blockdevice.partitions if self.mode == MBR: if len(self.blockdevice.partitions)>3: @@ -632,4 +631,4 @@ def disk_layouts(): return json.loads(b''.join(handle).decode('UTF-8')) except SysCallError as err: log(f"Could not return disk layouts: {err}") - return None \ No newline at end of file + return None diff --git a/archinstall/lib/exceptions.py b/archinstall/lib/exceptions.py index 49913980..6837f582 100644 --- a/archinstall/lib/exceptions.py +++ b/archinstall/lib/exceptions.py @@ -1,23 +1,41 @@ class RequirementError(BaseException): pass + + class DiskError(BaseException): pass + + class UnknownFilesystemFormat(BaseException): pass + + class ProfileError(BaseException): pass + + class SysCallError(BaseException): def __init__(self, message, exit_code): super(SysCallError, self).__init__(message) self.message = message self.exit_code = exit_code + + class ProfileNotFound(BaseException): pass + + class HardwareIncompatibilityError(BaseException): pass + + class PermissionError(BaseException): pass + + class UserError(BaseException): pass + + class ServiceException(BaseException): - pass \ No newline at end of file + pass diff --git a/archinstall/lib/general.py b/archinstall/lib/general.py index 72f8677f..2b27ac4c 100644 --- a/archinstall/lib/general.py +++ b/archinstall/lib/general.py @@ -1,11 +1,18 @@ -import os, json, hashlib, shlex, sys -import time, pty, logging +import hashlib +import json +import logging +import os +import pty +import shlex +import sys +import time from datetime import datetime, date -from subprocess import Popen, STDOUT, PIPE, check_output from select import epoll, EPOLLIN, EPOLLHUP +from typing import Union + from .exceptions import * from .output import log -from typing import Optional, Union + def gen_uid(entropy_length=256): return hashlib.sha512(os.urandom(entropy_length)).hexdigest() @@ -37,16 +44,16 @@ class JSON_Encoder: if isinstance(obj, dict): ## We'll need to iterate not just the value that default() usually gets passed ## But also iterate manually over each key: value pair in order to trap the keys. - + copy = {} for key, val in list(obj.items()): if isinstance(val, dict): val = json.loads(json.dumps(val, cls=JSON)) # This, is a EXTREMELY ugly hack.. - # But it's the only quick way I can think of to + # But it's the only quick way I can think of to # trigger a encoding of sub-dictionaries. else: val = JSON_Encoder._encode(val) - + if type(key) == str and key[0] == '!': copy[JSON_Encoder._encode(key)] = '******' else: diff --git a/archinstall/lib/hardware.py b/archinstall/lib/hardware.py index 009a3a6c..e4f87a0c 100644 --- a/archinstall/lib/hardware.py +++ b/archinstall/lib/hardware.py @@ -1,8 +1,11 @@ -import os, subprocess, json -from .general import sys_command -from .networking import list_interfaces, enrichIfaceTypes +import json +import os +import subprocess from typing import Optional +from .general import sys_command +from .networking import list_interfaces, enrich_iface_types + __packages__ = [ "mesa", "xf86-video-amdgpu", @@ -53,7 +56,7 @@ AVAILABLE_GFX_DRIVERS = { } def hasWifi()->bool: - return 'WIRELESS' in enrichIfaceTypes(list_interfaces().values()).values() + return 'WIRELESS' in enrich_iface_types(list_interfaces().values()).values() def hasAMDCPU()->bool: if subprocess.check_output("lscpu | grep AMD", shell=True).strip().decode(): diff --git a/archinstall/lib/installer.py b/archinstall/lib/installer.py index 68d058f0..ba92d519 100644 --- a/archinstall/lib/installer.py +++ b/archinstall/lib/installer.py @@ -1,15 +1,11 @@ -import os, stat, time, shutil, pathlib -import subprocess, logging -from .exceptions import * from .disk import * -from .general import * -from .user_interaction import * -from .profiles import Profile +from .hardware import * from .mirrors import * -from .systemd import Networkd from .output import log +from .profiles import Profile from .storage import storage -from .hardware import * +from .systemd import Networkd +from .user_interaction import * # Any package that the Installer() is responsible for (optional and the default ones) __packages__ = ["base", "base-devel", "linux-firmware", "linux", "linux-lts", "linux-zen", "linux-hardened"] @@ -47,7 +43,7 @@ class Installer(): 'base' : False, 'bootloader' : False } - + self.base_packages = base_packages.split(' ') if type(base_packages) is str else base_packages for kernel in kernels: self.base_packages.append(kernel) @@ -100,10 +96,10 @@ class Installer(): self.log('Some required steps were not successfully installed/configured before leaving the installer:', fg='red', level=logging.WARNING) for step in missing_steps: self.log(f' - {step}', fg='red', level=logging.WARNING) - + self.log(f"Detailed error logs can be found at: {storage['LOG_PATH']}", level=logging.WARNING) self.log(f"Submit this zip file as an issue to https://github.com/archlinux/archinstall/issues", level=logging.WARNING) - + self.sync_log_to_install_medium() return False @@ -116,7 +112,7 @@ class Installer(): if not os.path.isdir(f"{self.target}/{os.path.dirname(absolute_logfile)}"): os.makedirs(f"{self.target}/{os.path.dirname(absolute_logfile)}") - + shutil.copy2(absolute_logfile, f"{self.target}/{absolute_logfile}") return True @@ -124,7 +120,7 @@ class Installer(): def mount(self, partition, mountpoint, create_mountpoint=True): if create_mountpoint and not os.path.isdir(f'{self.target}{mountpoint}'): os.makedirs(f'{self.target}{mountpoint}') - + partition.mount(f'{self.target}{mountpoint}') def post_install_check(self, *args, **kwargs): @@ -147,7 +143,7 @@ class Installer(): def genfstab(self, flags='-pU'): self.log(f"Updating {self.target}/etc/fstab", level=logging.INFO) - + fstab = sys_command(f'/usr/bin/genfstab {flags} {self.target}').trace_log with open(f"{self.target}/etc/fstab", 'ab') as fstab_fh: fstab_fh.write(fstab) @@ -204,7 +200,7 @@ class Installer(): def arch_chroot(self, cmd, *args, **kwargs): if 'runas' in kwargs: cmd = f"su - {kwargs['runas']} -c \"{cmd}\"" - + return self.run_command(cmd) def drop_to_shell(self): @@ -224,7 +220,7 @@ class Installer(): network["DNS"] = dns conf = Networkd(Match={"Name": nic}, Network=network) - + with open(f"{self.target}/etc/systemd/network/10-{nic}.network", "a") as netconf: netconf.write(str(conf)) @@ -272,7 +268,7 @@ class Installer(): # Otherwise, we can go ahead and enable the services else: self.enable_service('systemd-networkd', 'systemd-resolved') - + return True @@ -281,7 +277,7 @@ class Installer(): return partition elif partition.parent not in partition.path and Partition(partition.parent, None, autodetect_filesystem=True).filesystem == 'crypto_LUKS': return Partition(partition.parent, None, autodetect_filesystem=True) - + return False def mkinitcpio(self, *flags): @@ -298,7 +294,7 @@ class Installer(): ## TODO: Perhaps this should be living in the function which dictates ## the partitioning. Leaving here for now. - + for partition in self.partitions: if partition.filesystem == 'btrfs': @@ -322,7 +318,7 @@ class Installer(): if not(hasUEFI()): self.base_packages.append('grub') - + if not isVM(): vendor = cpuVendor() if vendor == "AuthenticAMD": @@ -331,7 +327,7 @@ class Installer(): self.base_packages.append("intel-ucode") else: self.log("Unknown cpu vendor not installing ucode") - + self.pacstrap(self.base_packages) self.helper_flags['base-strapped'] = True @@ -395,7 +391,7 @@ class Installer(): f"default {self.init_time}", f"timeout 5" ] - + with open(f'{self.target}/boot/loader/loader.conf', 'w') as loader: for line in loader_data: if line[:8] == 'default ': @@ -500,7 +496,7 @@ class Installer(): o = b''.join(sys_command(f"/usr/bin/arch-chroot {self.target} sh -c \"echo '{user}:{password}' | chpasswd\"")) pass - + def user_set_shell(self, user, shell): self.log(f'Setting shell for {user} to {shell}', level=logging.INFO) diff --git a/archinstall/lib/locale_helpers.py b/archinstall/lib/locale_helpers.py index 3c373bc6..daf67e5b 100644 --- a/archinstall/lib/locale_helpers.py +++ b/archinstall/lib/locale_helpers.py @@ -4,6 +4,7 @@ import os from .exceptions import * # from .general import sys_command + def list_keyboard_languages(): locale_dir = '/usr/share/kbd/keymaps/' @@ -16,16 +17,19 @@ def list_keyboard_languages(): if os.path.splitext(file)[1] == '.gz': yield file.strip('.gz').strip('.map') + def verify_keyboard_layout(layout): for language in list_keyboard_languages(): if layout.lower() == language.lower(): return True return False + def search_keyboard_layout(filter): for language in list_keyboard_languages(): if filter.lower() in language.lower(): yield language + def set_keyboard_language(locale): return subprocess.call(['loadkeys', locale]) == 0 diff --git a/archinstall/lib/luks.py b/archinstall/lib/luks.py index 7f8485e6..e6e1c897 100644 --- a/archinstall/lib/luks.py +++ b/archinstall/lib/luks.py @@ -1,13 +1,9 @@ -import os -import shlex -import time import pathlib -import logging -from .exceptions import * -from .general import * + from .disk import Partition +from .general import * from .output import log -from .storage import storage + class luks2(): def __init__(self, partition, mountpoint, password, key_file=None, auto_unmount=False, *args, **kwargs): @@ -22,12 +18,12 @@ class luks2(): self.mapdev = None def __enter__(self): - #if self.partition.allow_formatting: - # self.key_file = self.encrypt(self.partition, *self.args, **self.kwargs) - #else: + # if self.partition.allow_formatting: + # self.key_file = self.encrypt(self.partition, *self.args, **self.kwargs) + # else: if not self.key_file: self.key_file = f"/tmp/{os.path.basename(self.partition.path)}.disk_pw" # TODO: Make disk-pw-file randomly unique? - + if type(self.password) != bytes: self.password = bytes(self.password, 'UTF-8') @@ -112,7 +108,7 @@ class luks2(): if cmd_handle.exit_code != 0: raise DiskError(f'Could not encrypt volume "{partition.path}": {cmd_output}') - + return key_file def unlock(self, partition, mountpoint, key_file): diff --git a/archinstall/lib/mirrors.py b/archinstall/lib/mirrors.py index ae6c6422..dd8fadc4 100644 --- a/archinstall/lib/mirrors.py +++ b/archinstall/lib/mirrors.py @@ -1,9 +1,8 @@ -import urllib.request, logging +import urllib.request -from .exceptions import * from .general import * from .output import log -from .storage import storage + def filter_mirrors_by_region(regions, destination='/etc/pacman.d/mirrorlist', tmp_dir='/root', *args, **kwargs): """ @@ -19,9 +18,10 @@ def filter_mirrors_by_region(regions, destination='/etc/pacman.d/mirrorlist', tm o = b''.join(sys_command((f"/usr/bin/wget 'https://archlinux.org/mirrorlist/?{'&'.join(region_list)}&protocol=https&ip_version=4&ip_version=6&use_mirror_status=on' -O {tmp_dir}/mirrorlist"))) o = b''.join(sys_command((f"/usr/bin/sed -i 's/#Server/Server/' {tmp_dir}/mirrorlist"))) o = b''.join(sys_command((f"/usr/bin/mv {tmp_dir}/mirrorlist {destination}"))) - + return True + def add_custom_mirrors(mirrors:list, *args, **kwargs): """ This will append custom mirror definitions in pacman.conf @@ -37,6 +37,7 @@ def add_custom_mirrors(mirrors:list, *args, **kwargs): return True + def insert_mirrors(mirrors, *args, **kwargs): """ This function will insert a given mirror-list at the top of `/etc/pacman.d/mirrorlist`. @@ -58,6 +59,7 @@ def insert_mirrors(mirrors, *args, **kwargs): return True + def use_mirrors(regions :dict, destination='/etc/pacman.d/mirrorlist'): log(f'A new package mirror-list has been created: {destination}', level=logging.INFO) for region, mirrors in regions.items(): @@ -67,11 +69,13 @@ def use_mirrors(regions :dict, destination='/etc/pacman.d/mirrorlist'): mirrorlist.write(f'Server = {mirror}\n') return True + def re_rank_mirrors(top=10, *positionals, **kwargs): if sys_command((f'/usr/bin/rankmirrors -n {top} /etc/pacman.d/mirrorlist > /etc/pacman.d/mirrorlist')).exit_code == 0: return True return False + def list_mirrors(): url = f"https://archlinux.org/mirrorlist/?protocol=https&ip_version=4&ip_version=6&use_mirror_status=on" regions = {} @@ -97,4 +101,4 @@ def list_mirrors(): url = line.lstrip('#Server = ') regions[region][url] = True - return regions \ No newline at end of file + return regions diff --git a/archinstall/lib/networking.py b/archinstall/lib/networking.py index 2dc8be9b..768cc1cc 100644 --- a/archinstall/lib/networking.py +++ b/archinstall/lib/networking.py @@ -7,22 +7,25 @@ from .exceptions import * from .general import sys_command from .storage import storage -def getHwAddr(ifname): + +def get_hw_addr(ifname): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', bytes(ifname, 'utf-8')[:15])) return ':'.join('%02x' % b for b in info[18:24]) - + + def list_interfaces(skip_loopback=True): interfaces = OrderedDict() for index, iface in socket.if_nameindex(): if skip_loopback and iface == "lo": continue - mac = getHwAddr(iface).replace(':', '-').lower() + mac = get_hw_addr(iface).replace(':', '-').lower() interfaces[mac] = iface return interfaces -def enrichIfaceTypes(interfaces :dict): + +def enrich_iface_types(interfaces :dict): result = {} for iface in interfaces: if os.path.isdir(f"/sys/class/net/{iface}/bridge/"): @@ -39,11 +42,13 @@ def enrichIfaceTypes(interfaces :dict): result[iface] = 'UNKNOWN' return result + def get_interface_from_mac(mac): return list_interfaces().get(mac.lower(), None) -def wirelessScan(interface): - interfaces = enrichIfaceTypes(list_interfaces().values()) + +def wireless_scan(interface): + interfaces = enrich_iface_types(list_interfaces().values()) if interfaces[interface] != 'WIRELESS': raise HardwareIncompatibilityError(f"Interface {interface} is not a wireless interface: {interfaces}") @@ -56,12 +61,13 @@ def wirelessScan(interface): storage['_WIFI'][interface]['scanning'] = True + # TODO: Full WiFi experience might get evolved in the future, pausing for now 2021-01-25 -def getWirelessNetworks(interface): +def get_wireless_networks(interface): # TODO: Make this oneliner pritter to check if the interface is scanning or not. if not '_WIFI' in storage or interface not in storage['_WIFI'] or storage['_WIFI'][interface].get('scanning', False) is False: import time - wirelessScan(interface) + wireless_scan(interface) time.sleep(5) for line in sys_command(f"iwctl station {interface} get-networks"): diff --git a/archinstall/lib/output.py b/archinstall/lib/output.py index d6a197f1..0818aed0 100644 --- a/archinstall/lib/output.py +++ b/archinstall/lib/output.py @@ -5,16 +5,18 @@ import logging from pathlib import Path from .storage import storage + # TODO: use logging's built in levels instead. # Although logging is threaded and I wish to avoid that. # It's more Pythonistic or w/e you want to call it. -class LOG_LEVELS: +class LogLevels: Critical = 0b001 Error = 0b010 Warning = 0b011 Info = 0b101 Debug = 0b111 + class journald(dict): @abc.abstractmethod def log(message, level=logging.DEBUG): @@ -27,19 +29,19 @@ class journald(dict): # to logging levels (and warn about deprecated usage) # There's some code re-usage here but that should be fine. # TODO: Remove these in a few versions: - if level == LOG_LEVELS.Critical: + if level == LogLevels.Critical: log("Deprecated level detected in log message, please use new logging. instead for the following log message:", fg="red", level=logging.ERROR, force=True) level = logging.CRITICAL - elif level == LOG_LEVELS.Error: + elif level == LogLevels.Error: log("Deprecated level detected in log message, please use new logging. instead for the following log message:", fg="red", level=logging.ERROR, force=True) level = logging.ERROR - elif level == LOG_LEVELS.Warning: + elif level == LogLevels.Warning: log("Deprecated level detected in log message, please use new logging. instead for the following log message:", fg="red", level=logging.ERROR, force=True) level = logging.WARNING - elif level == LOG_LEVELS.Info: + elif level == LogLevels.Info: log("Deprecated level detected in log message, please use new logging. instead for the following log message:", fg="red", level=logging.ERROR, force=True) level = logging.INFO - elif level == LOG_LEVELS.Debug: + elif level == LogLevels.Debug: log("Deprecated level detected in log message, please use new logging. instead for the following log message:", fg="red", level=logging.ERROR, force=True) level = logging.DEBUG @@ -49,14 +51,16 @@ class journald(dict): log_ch.setFormatter(log_fmt) log_adapter.addHandler(log_ch) log_adapter.setLevel(logging.DEBUG) - + log_adapter.log(level, message) + # TODO: Replace log() for session based logging. -class SessionLogging(): +class SessionLogging: def __init__(self): pass + # Found first reference here: https://stackoverflow.com/questions/7445658/how-to-detect-if-the-console-does-support-ansi-escape-codes-in-python # And re-used this: https://github.com/django/django/blob/master/django/core/management/color.py#L12 def supports_color(): @@ -70,6 +74,7 @@ def supports_color(): is_a_tty = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty() return supported_platform and is_a_tty + # Heavily influenced by: https://github.com/django/django/blob/ae8338daf34fd746771e0678081999b656177bae/django/utils/termcolors.py#L13 # Color options here: https://askubuntu.com/questions/528928/how-to-do-underline-bold-italic-strikethrough-color-background-and-size-i def stylize_output(text :str, *opts, **kwargs): @@ -94,6 +99,7 @@ def stylize_output(text :str, *opts, **kwargs): text = '%s\x1b[%sm' % (text or '', RESET) return '%s%s' % (('\x1b[%sm' % ';'.join(code_list)), text or '') + def log(*args, **kwargs): string = orig_string = ' '.join([str(x) for x in args]) @@ -132,19 +138,19 @@ def log(*args, **kwargs): # to logging levels (and warn about deprecated usage) # There's some code re-usage here but that should be fine. # TODO: Remove these in a few versions: - if kwargs['level'] == LOG_LEVELS.Critical: + if kwargs['level'] == LogLevels.Critical: log("Deprecated level detected in log message, please use new logging. instead for the following log message:", fg="red", level=logging.ERROR, force=True) kwargs['level'] = logging.CRITICAL - elif kwargs['level'] == LOG_LEVELS.Error: + elif kwargs['level'] == LogLevels.Error: log("Deprecated level detected in log message, please use new logging. instead for the following log message:", fg="red", level=logging.ERROR, force=True) kwargs['level'] = logging.ERROR - elif kwargs['level'] == LOG_LEVELS.Warning: + elif kwargs['level'] == LogLevels.Warning: log("Deprecated level detected in log message, please use new logging. instead for the following log message:", fg="red", level=logging.ERROR, force=True) kwargs['level'] = logging.WARNING - elif kwargs['level'] == LOG_LEVELS.Info: + elif kwargs['level'] == LogLevels.Info: log("Deprecated level detected in log message, please use new logging. instead for the following log message:", fg="red", level=logging.ERROR, force=True) kwargs['level'] = logging.INFO - elif kwargs['level'] == LOG_LEVELS.Debug: + elif kwargs['level'] == LogLevels.Debug: log("Deprecated level detected in log message, please use new logging. instead for the following log message:", fg="red", level=logging.ERROR, force=True) kwargs['level'] = logging.DEBUG @@ -156,7 +162,7 @@ def log(*args, **kwargs): try: journald.log(string, level=kwargs.get('level', logging.INFO)) except ModuleNotFoundError: - pass # Ignore writing to journald + pass # Ignore writing to journald # Finally, print the log unless we skipped it based on level. # We use sys.stdout.write()+flush() instead of print() to try and diff --git a/archinstall/lib/packages.py b/archinstall/lib/packages.py index 4f6b6c61..87c60abb 100644 --- a/archinstall/lib/packages.py +++ b/archinstall/lib/packages.py @@ -1,10 +1,14 @@ -import urllib.request, urllib.parse -import ssl, json +import json +import ssl +import urllib.parse +import urllib.request + from .exceptions import * BASE_URL = 'https://archlinux.org/packages/search/json/?name={package}' BASE_GROUP_URL = 'https://archlinux.org/groups/x86_64/{group}/' + def find_group(name): ssl_context = ssl.create_default_context() ssl_context.check_hostname = False @@ -16,11 +20,12 @@ def find_group(name): return False else: raise err - + # Just to be sure some code didn't slip through the exception if response.code == 200: return True + def find_package(name): """ Finds a specific package via the package database. @@ -33,6 +38,7 @@ def find_package(name): data = response.read().decode('UTF-8') return json.loads(data) + def find_packages(*names): """ This function returns the search results for many packages. @@ -44,6 +50,7 @@ def find_packages(*names): result[package] = find_package(package) return result + def validate_package_list(packages :list): """ Validates a list of given packages. @@ -53,8 +60,8 @@ def validate_package_list(packages :list): for package in packages: if not find_package(package)['results'] and not find_group(package): invalid_packages.append(package) - + if invalid_packages: raise RequirementError(f"Invalid package names: {invalid_packages}") - return True \ No newline at end of file + return True diff --git a/archinstall/lib/profiles.py b/archinstall/lib/profiles.py index 42fd4c24..ebcd3aff 100644 --- a/archinstall/lib/profiles.py +++ b/archinstall/lib/profiles.py @@ -1,13 +1,17 @@ +import hashlib +import importlib.util +import json +import re +import ssl +import sys +import urllib.parse +import urllib.request from typing import Optional -import os, urllib.request, urllib.parse, ssl, json, re -import importlib.util, sys, glob, hashlib, logging -from collections import OrderedDict -from .general import multisplit, sys_command -from .exceptions import * +from .general import multisplit from .networking import * -from .output import log from .storage import storage + def grab_url_data(path): safe_path = path[:path.find(':')+1]+''.join([item if item in ('/', '?', '=', '&') else urllib.parse.quote(item) for item in multisplit(path[path.find(':')+1:], ('/', '?', '=', '&'))]) ssl_context = ssl.create_default_context() @@ -16,6 +20,7 @@ def grab_url_data(path): response = urllib.request.urlopen(safe_path, context=ssl_context) return response.read() + def list_profiles(filter_irrelevant_macs=True, subpath='', filter_top_level_profiles=False): # TODO: Grab from github page as well, not just local static files if filter_irrelevant_macs: @@ -55,7 +60,7 @@ def list_profiles(filter_irrelevant_macs=True, subpath='', filter_top_level_prof except json.decoder.JSONDecodeError as err: print(f'Error: Could not decode "{profiles_url}" result as JSON:', err) return cache - + for profile in profile_list: if os.path.splitext(profile)[1] == '.py': tailored = False @@ -73,7 +78,8 @@ def list_profiles(filter_irrelevant_macs=True, subpath='', filter_top_level_prof return cache -class Script(): + +class Script: def __init__(self, profile, installer=None): # profile: https://hvornum.se/something.py # profile: desktop @@ -154,6 +160,7 @@ class Script(): return sys.modules[self.namespace] + class Profile(Script): def __init__(self, installer, path, args={}): super(Profile, self).__init__(path, installer) @@ -238,6 +245,7 @@ class Profile(Script): return imported.__packages__ return None + class Application(Profile): def __repr__(self, *args, **kwargs): return f'Application({os.path.basename(self.profile)})' diff --git a/archinstall/lib/services.py b/archinstall/lib/services.py index bb6f64f2..46aa7846 100644 --- a/archinstall/lib/services.py +++ b/archinstall/lib/services.py @@ -1,8 +1,6 @@ -import os - -from .exceptions import * from .general import * + def service_state(service_name: str): if os.path.splitext(service_name)[1] != '.service': service_name += '.service' # Just to be safe diff --git a/archinstall/lib/storage.py b/archinstall/lib/storage.py index 43d088bb..d985ca17 100644 --- a/archinstall/lib/storage.py +++ b/archinstall/lib/storage.py @@ -12,7 +12,7 @@ storage = { './profiles', '~/.config/archinstall/profiles', os.path.join(os.path.dirname(os.path.abspath(__file__)), 'profiles'), - #os.path.abspath(f'{os.path.dirname(__file__)}/../examples') + # os.path.abspath(f'{os.path.dirname(__file__)}/../examples') ], 'UPSTREAM_URL' : 'https://raw.githubusercontent.com/archlinux/archinstall/master/profiles', 'PROFILE_DB' : None, # Used in cases when listing profiles is desired, not mandatory for direct profile grabing. diff --git a/archinstall/lib/systemd.py b/archinstall/lib/systemd.py index f2b7c9b3..5607250b 100644 --- a/archinstall/lib/systemd.py +++ b/archinstall/lib/systemd.py @@ -1,4 +1,4 @@ -class Ini(): +class Ini: def __init__(self, *args, **kwargs): """ Limited INI handler for now. @@ -25,11 +25,13 @@ class Ini(): return result + class Systemd(Ini): """ Placeholder class to do systemd specific setups. """ + class Networkd(Systemd): """ Placeholder class to do systemd-network specific setups. diff --git a/archinstall/lib/user_interaction.py b/archinstall/lib/user_interaction.py index c76dc9a5..0aeba3b9 100644 --- a/archinstall/lib/user_interaction.py +++ b/archinstall/lib/user_interaction.py @@ -1,27 +1,40 @@ -import getpass, pathlib, os, shutil, re, time -import sys, time, signal, ipaddress, logging -import termios, tty, select # Used for char by char polling of sys.stdin +import getpass +import ipaddress +import logging +import pathlib +import re +import select # Used for char by char polling of sys.stdin +import shutil +import signal +import sys +import termios +import time +import tty + from .exceptions import * -from .profiles import Profile -from .locale_helpers import list_keyboard_languages, verify_keyboard_layout, search_keyboard_layout -from .output import log -from .storage import storage -from .networking import list_interfaces from .general import sys_command from .hardware import AVAILABLE_GFX_DRIVERS, hasUEFI +from .locale_helpers import list_keyboard_languages, verify_keyboard_layout, search_keyboard_layout +from .networking import list_interfaces +from .output import log +from .profiles import Profile -## TODO: Some inconsistencies between the selection processes. -## Some return the keys from the options, some the values? + +# TODO: Some inconsistencies between the selection processes. +# Some return the keys from the options, some the values? def get_terminal_height(): return shutil.get_terminal_size().lines + def get_terminal_width(): return shutil.get_terminal_size().columns + def get_longest_option(options): return max([len(x) for x in options]) + def check_for_correct_username(username): if re.match(r'^[a-z_][a-z0-9_-]*\$?$', username) and len(username) <= 32: return True @@ -32,6 +45,7 @@ def check_for_correct_username(username): ) return False + def do_countdown(): SIG_TRIGGER = False def kill_handler(sig, frame): @@ -67,6 +81,7 @@ def do_countdown(): signal.signal(signal.SIGINT, original_sigint_handler) return True + def get_password(prompt="Enter a password: "): while (passwd := getpass.getpass(prompt)): passwd_verification = getpass.getpass(prompt='And one more time for verification: ') @@ -80,6 +95,7 @@ def get_password(prompt="Enter a password: "): return passwd return None + def print_large_list(options, padding=5, margin_bottom=0, separator=': '): highest_index_number_length = len(str(len(options))) longest_line = highest_index_number_length + len(separator) + get_longest_option(options) + padding @@ -140,7 +156,7 @@ def generic_multi_select(options, text="Select one or more of the options above section.input_pos = section._cursor_x selected_option = section.get_keyboard_input(end=None) # This string check is necessary to correct work with it - # Without this, Python will raise AttributeError because of stripping `None` + # Without this, Python will raise AttributeError because of stripping `None` # It also allows to remove empty spaces if the user accidentally entered them. if isinstance(selected_option, str): selected_option = selected_option.strip() @@ -173,7 +189,7 @@ def generic_multi_select(options, text="Select one or more of the options above return selected_options -class MiniCurses(): +class MiniCurses: def __init__(self, width, height): self.width = width self.height = height @@ -200,10 +216,10 @@ class MiniCurses(): if x < 0: x = 0 if y < 0: y = 0 - #import time - #sys.stdout.write(f"Clearing from: {x, y}") - #sys.stdout.flush() - #time.sleep(2) + # import time + # sys.stdout.write(f"Clearing from: {x, y}") + # sys.stdout.flush() + # time.sleep(2) sys.stdout.flush() sys.stdout.write('\033[%d;%df' % (y, x)) @@ -259,16 +275,16 @@ class MiniCurses(): poller.register(sys.stdin.fileno(), select.EPOLLIN) - EOF = False - while EOF is False: + eof = False + while eof is False: for fileno, event in poller.poll(0.025): char = sys.stdin.read(1) - #sys.stdout.write(f"{[char]}") - #sys.stdout.flush() + # sys.stdout.write(f"{[char]}") + # sys.stdout.flush() - if (newline := (char in ('\n', '\r'))): - EOF = True + if newline := (char in ('\n', '\r')): + eof = True if not newline or strip_rowbreaks is False: response += char @@ -287,6 +303,7 @@ class MiniCurses(): if response: return response + def ask_for_superuser_account(prompt='Username for required superuser with sudo privileges: ', forced=False): while 1: new_user = input(prompt).strip(' ') @@ -304,6 +321,7 @@ def ask_for_superuser_account(prompt='Username for required superuser with sudo password = get_password(prompt=f'Password for user {new_user}: ') return {new_user: {"!password" : password}} + def ask_for_additional_users(prompt='Any additional users to install (leave blank for no users): '): users = {} superusers = {} @@ -315,7 +333,7 @@ def ask_for_additional_users(prompt='Any additional users to install (leave blan if not check_for_correct_username(new_user): continue password = get_password(prompt=f'Password for user {new_user}: ') - + if input("Should this user be a superuser (sudoer) [y/N]: ").strip(' ').lower() in ('y', 'yes'): superusers[new_user] = {"!password" : password} else: @@ -323,6 +341,7 @@ def ask_for_additional_users(prompt='Any additional users to install (leave blan return users, superusers + def ask_for_a_timezone(): while True: timezone = input('Enter a valid timezone (examples: Europe/Stockholm, US/Eastern) or press enter to use UTC: ').strip().strip('*.') @@ -337,6 +356,7 @@ def ask_for_a_timezone(): fg='red' ) + def ask_for_bootloader() -> str: bootloader = "systemd-bootctl" if hasUEFI()==False: @@ -347,6 +367,7 @@ def ask_for_bootloader() -> str: bootloader="grub-install" return bootloader + def ask_for_audio_selection(): audio = "pulseaudio" # Default for most desktop environments pipewire_choice = input("Would you like to install pipewire instead of pulseaudio as the default audio server? [Y/n] ").lower() @@ -355,6 +376,7 @@ def ask_for_audio_selection(): return audio + def ask_to_configure_network(): # Optionally configure one network interface. #while 1: @@ -422,6 +444,7 @@ def ask_to_configure_network(): return {} + def ask_for_disk_layout(): options = { 'keep-existing' : 'Keep existing partition layout and select which ones to use where', @@ -433,6 +456,7 @@ def ask_for_disk_layout(): allow_empty_input=False, sort=True) return next((key for key, val in options.items() if val == value), None) + def ask_for_main_filesystem_format(): options = { 'btrfs' : 'btrfs', @@ -445,6 +469,7 @@ def ask_for_main_filesystem_format(): allow_empty_input=False) return next((key for key, val in options.items() if val == value), None) + def generic_select(options, input_text="Select one of the above by index or absolute value: ", allow_empty_input=True, options_output=True, sort=False): """ A generic select function that does not output anything @@ -477,7 +502,6 @@ def generic_select(options, input_text="Select one of the above by index or abso # As we pass only list and dict (converted to list), we can skip converting to list options = sorted(options) - # Added ability to disable the output of options items, # if another function displays something different from this if options_output: @@ -510,6 +534,7 @@ def generic_select(options, input_text="Select one of the above by index or abso return selected_option + def select_disk(dict_o_disks): """ Asks the user to select a harddrive from the `dict_o_disks` selection. @@ -525,18 +550,18 @@ def select_disk(dict_o_disks): if len(drives) >= 1: for index, drive in enumerate(drives): print(f"{index}: {drive} ({dict_o_disks[drive]['size'], dict_o_disks[drive].device, dict_o_disks[drive]['label']})") - + log(f"You can skip selecting a drive and partitioning and use whatever drive-setup is mounted at /mnt (experimental)", fg="yellow") - drive = generic_select(drives, 'Select one of the above disks (by name or number) or leave blank to use /mnt: ', - options_output=False) + drive = generic_select(drives, 'Select one of the above disks (by name or number) or leave blank to use /mnt: ', options_output=False) if not drive: return drive - + drive = dict_o_disks[drive] return drive raise DiskError('select_disk() requires a non-empty dictionary of disks to select from.') + def select_profile(options): """ Asks the user to select a profile from the `options` dictionary parameter. @@ -565,6 +590,7 @@ def select_profile(options): else: raise RequirementError("Selecting profiles require a least one profile to be given as an option.") + def select_language(options, show_only_country_codes=True): """ Asks the user to select a language from the `options` dictionary parameter. @@ -579,8 +605,8 @@ def select_language(options, show_only_country_codes=True): :return: The language/dictionary key of the selected language :rtype: str """ - DEFAULT_KEYBOARD_LANGUAGE = 'us' - + default_keyboard_language = 'us' + if show_only_country_codes: languages = sorted([language for language in list(options) if len(language) == 2]) else: @@ -596,7 +622,7 @@ def select_language(options, show_only_country_codes=True): while True: selected_language = input('Select one of the above keyboard languages (by name or full name): ') if not selected_language: - return DEFAULT_KEYBOARD_LANGUAGE + return default_keyboard_language elif selected_language.lower() in ('?', 'help'): while True: filter_string = input("Search for layout containing (example: \"sv-\") or enter 'exit' to exit from search: ") @@ -624,6 +650,7 @@ def select_language(options, show_only_country_codes=True): raise RequirementError("Selecting languages require a least one language to be given as an option.") + def select_mirror_regions(mirrors, show_top_mirrors=True): """ Asks the user to select a mirror or region from the `mirrors` dictionary parameter. @@ -665,6 +692,7 @@ def select_mirror_regions(mirrors, show_top_mirrors=True): raise RequirementError("Selecting mirror region require a least one region to be given as an option.") + def select_driver(options=AVAILABLE_GFX_DRIVERS): """ Some what convoluted function, which's job is simple. @@ -673,10 +701,10 @@ def select_driver(options=AVAILABLE_GFX_DRIVERS): (The template xorg is for beginner users, not advanced, and should there for appeal to the general public first and edge cases later) """ - + drivers = sorted(list(options)) default_option = options["All open-source (default)"] - + if drivers: lspci = sys_command(f'/usr/bin/lspci') for line in lspci.trace_log.split(b'\r\n'): @@ -696,8 +724,7 @@ def select_driver(options=AVAILABLE_GFX_DRIVERS): if type(selected_driver) == dict: driver_options = sorted(list(selected_driver)) - driver_package_group = generic_select(driver_options, f'Which driver-type do you want for {initial_option}: ', - allow_empty_input=False) + driver_package_group = generic_select(driver_options, f'Which driver-type do you want for {initial_option}: ', allow_empty_input=False) driver_package_group = selected_driver[driver_package_group] return driver_package_group @@ -706,6 +733,7 @@ def select_driver(options=AVAILABLE_GFX_DRIVERS): raise RequirementError("Selecting drivers require a least one profile to be given as an option.") + def select_kernel(options): """ Asks the user to select a kernel for system. @@ -716,12 +744,12 @@ def select_kernel(options): :return: The string as a selected kernel :rtype: string """ - - DEFAULT_KERNEL = "linux" - + + default_kernel = "linux" + kernels = sorted(list(options)) - + if kernels: - return generic_multi_select(kernels, f"Choose which kernels to use (leave blank for default: {DEFAULT_KERNEL}): ", default=DEFAULT_KERNEL, sort=False) - + return generic_multi_select(kernels, f"Choose which kernels to use (leave blank for default: {default_kernel}): ", default=default_kernel, sort=False) + raise RequirementError("Selecting kernels require a least one kernel to be given as an option.") diff --git a/examples/guided.py b/examples/guided.py index 95ccca29..a4fb5e3b 100644 --- a/examples/guided.py +++ b/examples/guided.py @@ -10,7 +10,7 @@ if archinstall.arguments.get('help'): exit(0) # For support reasons, we'll log the disk layout pre installation to match against post-installation layout -archinstall.log(f"Disk states before installing: {archinstall.disk_layouts()}", level=archinstall.LOG_LEVELS.Debug) +archinstall.log(f"Disk states before installing: {archinstall.disk_layouts()}", level=archinstall.LogLevels.Debug) def ask_user_questions(): @@ -387,7 +387,7 @@ def perform_installation(mountpoint): pass # For support reasons, we'll log the disk layout post installation (crash or no crash) - archinstall.log(f"Disk states after installing: {archinstall.disk_layouts()}", level=archinstall.LOG_LEVELS.Debug) + archinstall.log(f"Disk states after installing: {archinstall.disk_layouts()}", level=archinstall.LogLevels.Debug) ask_user_questions() perform_installation_steps() -- cgit v1.2.3-54-g00ecf From 6057203e5bc1dc22f95dfef513fdb23950db8326 Mon Sep 17 00:00:00 2001 From: Dylan Taylor Date: Sat, 15 May 2021 12:59:38 -0400 Subject: More formatting fixes --- CONTRIBUTING.md | 80 +++++++++++++++++++++++-------------- archinstall/lib/mirrors.py | 3 +- archinstall/lib/networking.py | 7 ++-- archinstall/lib/output.py | 13 +++--- archinstall/lib/packages.py | 2 +- archinstall/lib/profiles.py | 11 ++--- archinstall/lib/storage.py | 12 +++--- archinstall/lib/user_interaction.py | 80 +++++++++++++++++++------------------ docs/conf.py | 2 - docs/pull_request_template.md | 4 +- examples/guided.py | 4 +- 11 files changed, 121 insertions(+), 97 deletions(-) (limited to 'archinstall/lib/user_interaction.py') diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index a72704bb..cc443fbd 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,57 +1,79 @@ # Contributing to archinstall -Any contributions through pull requests are welcome as this project aims to be a community based project to ease some Arch Linux installation steps. Bear in mind that in the future this repo might be transferred to the official [GitLab repo under Arch Linux](http://gitlab.archlinux.org/archlinux/) *(if GitLab becomes open to the general public)*. +Any contributions through pull requests are welcome as this project aims to be a community based project to ease some +Arch Linux installation steps. Bear in mind that in the future this repo might be transferred to the +official [GitLab repo under Arch Linux](http://gitlab.archlinux.org/archlinux/) *(if GitLab becomes open to the general +public)*. -Therefore guidelines and style changes to the code might come into affect as well as guidelines surrounding bug reporting and discussions. +Therefore guidelines and style changes to the code might come into affect as well as guidelines surrounding bug +reporting and discussions. ## Branches -`master` is currently the default branch, and that's where all future feature work is being done, this means that `master` is a living entity and will most likely never be in a fully stable state. For stable releases, please see the tagged commits. +`master` is currently the default branch, and that's where all future feature work is being done, this means +that `master` is a living entity and will most likely never be in a fully stable state. For stable releases, please see +the tagged commits. -Patch releases will be done against their own branches, branched from stable tagged releases and will be named according to the version it will become on release *(Patches to `v2.1.4` will be done on branch `v2.1.5` for instance)*. +Patch releases will be done against their own branches, branched from stable tagged releases and will be named according +to the version it will become on release *(Patches to `v2.1.4` will be done on branch `v2.1.5` for instance)*. ## Discussions -Currently, questions, bugs and suggestions should be reported through [GitHub issue tracker](https://github.com/archlinux/archinstall/issues).
+Currently, questions, bugs and suggestions should be reported +through [GitHub issue tracker](https://github.com/archlinux/archinstall/issues).
For less formal discussions there are also a [archinstall Discord server](https://discord.gg/cqXU88y). ## Coding convention -Archinstall's goal is to follow [PEP8](https://www.python.org/dev/peps/pep-0008/) as best as it can with some minor exceptions.
+Archinstall's goal is to follow [PEP8](https://www.python.org/dev/peps/pep-0008/) as best as it can with some minor +exceptions.
The exceptions to PEP8 are: - * Archinstall uses [tabs instead of spaces](https://www.python.org/dev/peps/pep-0008/#tabs-or-spaces) simply to make it easier for non-IDE developers to navigate the code *(Tab display-width should be equal to 4 spaces)*. Exception to the rule are comments that need fine-tuned indentation for documentation purposes. - * [Line length](https://www.python.org/dev/peps/pep-0008/#maximum-line-length) should aim for no more than 100 characters, but not strictly enforced. - * [Line breaks before/after binary operator](https://www.python.org/dev/peps/pep-0008/#should-a-line-break-before-or-after-a-binary-operator) is not enforced, as long as the style of line breaks are consistent within the same code block. - * Archinstall should always be saved with **Unix-formatted line endings** and no other platform-specific formats. - * [Blank lines](https://www.python.org/dev/peps/pep-0008/#blank-lines) before/after imports and functions are not followed and discouraged. One space is commonly used in archinstall. - * Multiple [Imports](https://www.python.org/dev/peps/pep-0008/#imports) on the same line is allowed, but more than five imports should be avoided on any given line. This simply saves up some space at the top of the file *(for non-IDE developers)* and will not be enforced. - * [String quotes](https://www.python.org/dev/peps/pep-0008/#string-quotes) follow PEP8, the exception being when creating formatted strings, double-quoted strings are *preferred* but not required on the outer edges *(Example: `f"Welcome {name}"` rather than `f'Welcome {name}'`)*. +* Archinstall uses [tabs instead of spaces](https://www.python.org/dev/peps/pep-0008/#tabs-or-spaces) simply to make it + easier for non-IDE developers to navigate the code *(Tab display-width should be equal to 4 spaces)*. Exception to the + rule are comments that need fine-tuned indentation for documentation purposes. +* [Line length](https://www.python.org/dev/peps/pep-0008/#maximum-line-length) should aim for no more than 100 + characters, but not strictly enforced. +* [Line breaks before/after binary operator](https://www.python.org/dev/peps/pep-0008/#should-a-line-break-before-or-after-a-binary-operator) + is not enforced, as long as the style of line breaks are consistent within the same code block. +* Archinstall should always be saved with **Unix-formatted line endings** and no other platform-specific formats. +* [Blank lines](https://www.python.org/dev/peps/pep-0008/#blank-lines) before/after imports and functions are not + followed and discouraged. One space is commonly used in archinstall. +* Multiple [Imports](https://www.python.org/dev/peps/pep-0008/#imports) on the same line is allowed, but more than five + imports should be avoided on any given line. This simply saves up some space at the top of the file *(for non-IDE + developers)* and will not be enforced. +* [String quotes](https://www.python.org/dev/peps/pep-0008/#string-quotes) follow PEP8, the exception being when + creating formatted strings, double-quoted strings are *preferred* but not required on the outer edges *( + Example: `f"Welcome {name}"` rather than `f'Welcome {name}'`)*. Most of these style guidelines have been put into place after the fact *(in an attempt to clean up the code)*.
There might therefore be older code which does not follow the coding convention and the code is subject to change. ## Submitting Changes -Archinstall uses Github's pull-request workflow and all contributions in terms of code should be done through pull requests.
+Archinstall uses Github's pull-request workflow and all contributions in terms of code should be done through pull +requests.
-Anyone interested in archinstall may review your code. One of the core developers will merge your pull request when they think it is ready. -For every pull request, we aim to promptly either merge it or say why it is not yet ready; if you go a few days without a reply, please feel free to ping the thread by adding a new comment. +Anyone interested in archinstall may review your code. One of the core developers will merge your pull request when they +think it is ready. For every pull request, we aim to promptly either merge it or say why it is not yet ready; if you go +a few days without a reply, please feel free to ping the thread by adding a new comment. -To get your pull request merged sooner, you should explain why you are making the change. For example, you can point to a code sample that is outdated in terms of Arch Linux command lines. -It is also helpful to add links to online documentation or to the implementation of the code you are changing. +To get your pull request merged sooner, you should explain why you are making the change. For example, you can point to +a code sample that is outdated in terms of Arch Linux command lines. It is also helpful to add links to online +documentation or to the implementation of the code you are changing. -Also, do not squash your commits after you have submitted a pull request, as this erases context during review. We will squash commits when the pull request is merged. +Also, do not squash your commits after you have submitted a pull request, as this erases context during review. We will +squash commits when the pull request is merged. At present the current contributors are (alphabetically): - * Anton Hvornum ([@Torxed](https://github.com/Torxed)) - * Borislav Kosharov ([@nikibobi](https://github.com/nikibobi)) - * demostanis ([@demostanis](https://github.com/demostanis)) - * Giancarlo Razzolini (@[grazzolini](https://github.com/grazzolini)) - * j-james ([@j-james](https://github.com/j-james)) - * Jerker Bengtsson ([@jaybent](https://github.com/jaybent)) - * Ninchester ([@ninchester](https://github.com/ninchester)) - * Philipp Schaffrath ([@phisch](https://github.com/phisch)) - * Varun Madiath ([@vamega](https://github.com/vamega)) - * nullrequest ([@advaithm](https://github.com/advaithm)) +* Anton Hvornum ([@Torxed](https://github.com/Torxed)) +* Borislav Kosharov ([@nikibobi](https://github.com/nikibobi)) +* demostanis ([@demostanis](https://github.com/demostanis)) +* Giancarlo Razzolini (@[grazzolini](https://github.com/grazzolini)) +* j-james ([@j-james](https://github.com/j-james)) +* Jerker Bengtsson ([@jaybent](https://github.com/jaybent)) +* Ninchester ([@ninchester](https://github.com/ninchester)) +* Philipp Schaffrath ([@phisch](https://github.com/phisch)) +* Varun Madiath ([@vamega](https://github.com/vamega)) +* nullrequest ([@advaithm](https://github.com/advaithm)) diff --git a/archinstall/lib/mirrors.py b/archinstall/lib/mirrors.py index dd8fadc4..e53b356a 100644 --- a/archinstall/lib/mirrors.py +++ b/archinstall/lib/mirrors.py @@ -60,7 +60,7 @@ def insert_mirrors(mirrors, *args, **kwargs): return True -def use_mirrors(regions :dict, destination='/etc/pacman.d/mirrorlist'): +def use_mirrors(regions: dict, destination='/etc/pacman.d/mirrorlist'): log(f'A new package mirror-list has been created: {destination}', level=logging.INFO) for region, mirrors in regions.items(): with open(destination, 'w') as mirrorlist: @@ -86,7 +86,6 @@ def list_mirrors(): log(f'Could not fetch an active mirror-list: {err}', level=logging.WARNING, fg="yellow") return regions - region = 'Unknown region' for line in response.readlines(): if len(line.strip()) == 0: diff --git a/archinstall/lib/networking.py b/archinstall/lib/networking.py index 768cc1cc..3e5ed4e7 100644 --- a/archinstall/lib/networking.py +++ b/archinstall/lib/networking.py @@ -1,8 +1,9 @@ -import os import fcntl +import os import socket import struct from collections import OrderedDict + from .exceptions import * from .general import sys_command from .storage import storage @@ -10,7 +11,7 @@ from .storage import storage def get_hw_addr(ifname): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', bytes(ifname, 'utf-8')[:15])) + info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', bytes(ifname, 'utf-8')[:15])) return ':'.join('%02x' % b for b in info[18:24]) @@ -25,7 +26,7 @@ def list_interfaces(skip_loopback=True): return interfaces -def enrich_iface_types(interfaces :dict): +def enrich_iface_types(interfaces: dict): result = {} for iface in interfaces: if os.path.isdir(f"/sys/class/net/{iface}/bridge/"): diff --git a/archinstall/lib/output.py b/archinstall/lib/output.py index 0818aed0..cce9e88c 100644 --- a/archinstall/lib/output.py +++ b/archinstall/lib/output.py @@ -1,8 +1,9 @@ import abc +import logging import os import sys -import logging from pathlib import Path + from .storage import storage @@ -21,7 +22,7 @@ class journald(dict): @abc.abstractmethod def log(message, level=logging.DEBUG): try: - import systemd.journal # type: ignore + import systemd.journal # type: ignore except ModuleNotFoundError: return False @@ -77,8 +78,8 @@ def supports_color(): # Heavily influenced by: https://github.com/django/django/blob/ae8338daf34fd746771e0678081999b656177bae/django/utils/termcolors.py#L13 # Color options here: https://askubuntu.com/questions/528928/how-to-do-underline-bold-italic-strikethrough-color-background-and-size-i -def stylize_output(text :str, *opts, **kwargs): - opt_dict = {'bold': '1', 'italic' : '3', 'underscore': '4', 'blink': '5', 'reverse': '7', 'conceal': '8'} +def stylize_output(text: str, *opts, **kwargs): + opt_dict = {'bold': '1', 'italic': '3', 'underscore': '4', 'blink': '5', 'reverse': '7', 'conceal': '8'} color_names = ('black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white') foreground = {color_names[x]: '3%s' % x for x in range(8)} background = {color_names[x]: '4%s' % x for x in range(8)} @@ -120,8 +121,8 @@ def log(*args, **kwargs): log_file.write("") except PermissionError: # Fallback to creating the log file in the current folder - err_string = f"Not enough permission to place log file at {absolute_logfile}, creating it in {Path('./').absolute()/filename} instead." - absolute_logfile = Path('./').absolute()/filename + err_string = f"Not enough permission to place log file at {absolute_logfile}, creating it in {Path('./').absolute() / filename} instead." + absolute_logfile = Path('./').absolute() / filename absolute_logfile.parents[0].mkdir(exist_ok=True) absolute_logfile = str(absolute_logfile) storage['LOG_PATH'] = './' diff --git a/archinstall/lib/packages.py b/archinstall/lib/packages.py index 87c60abb..e16ed99e 100644 --- a/archinstall/lib/packages.py +++ b/archinstall/lib/packages.py @@ -51,7 +51,7 @@ def find_packages(*names): return result -def validate_package_list(packages :list): +def validate_package_list(packages: list): """ Validates a list of given packages. Raises `RequirementError` if one or more packages are not found. diff --git a/archinstall/lib/profiles.py b/archinstall/lib/profiles.py index ebcd3aff..8b5525b4 100644 --- a/archinstall/lib/profiles.py +++ b/archinstall/lib/profiles.py @@ -7,6 +7,7 @@ import sys import urllib.parse import urllib.request from typing import Optional + from .general import multisplit from .networking import * from .storage import storage @@ -16,7 +17,7 @@ def grab_url_data(path): safe_path = path[:path.find(':')+1]+''.join([item if item in ('/', '?', '=', '&') else urllib.parse.quote(item) for item in multisplit(path[path.find(':')+1:], ('/', '?', '=', '&'))]) ssl_context = ssl.create_default_context() ssl_context.check_hostname = False - ssl_context.verify_mode=ssl.CERT_NONE + ssl_context.verify_mode = ssl.CERT_NONE response = urllib.request.urlopen(safe_path, context=ssl_context) return response.read() @@ -29,7 +30,7 @@ def list_profiles(filter_irrelevant_macs=True, subpath='', filter_top_level_prof cache = {} # Grab all local profiles found in PROFILE_PATH for PATH_ITEM in storage['PROFILE_PATH']: - for root, folders, files in os.walk(os.path.abspath(os.path.expanduser(PATH_ITEM+subpath))): + for root, folders, files in os.walk(os.path.abspath(os.path.expanduser(PATH_ITEM + subpath))): for file in files: if file == '__init__.py': continue @@ -51,7 +52,7 @@ def list_profiles(filter_irrelevant_macs=True, subpath='', filter_top_level_prof # Grab profiles from upstream URL if storage['PROFILE_DB']: - profiles_url = os.path.join(storage["UPSTREAM_URL"]+subpath, storage['PROFILE_DB']) + profiles_url = os.path.join(storage["UPSTREAM_URL"] + subpath, storage['PROFILE_DB']) try: profile_list = json.loads(grab_url_data(profiles_url)) except urllib.error.HTTPError as err: @@ -74,7 +75,7 @@ def list_profiles(filter_irrelevant_macs=True, subpath='', filter_top_level_prof if filter_top_level_profiles: for profile in list(cache.keys()): if Profile(None, profile).is_top_level_profile() is False: - del(cache[profile]) + del (cache[profile]) return cache @@ -166,7 +167,7 @@ class Profile(Script): super(Profile, self).__init__(path, installer) def __dump__(self, *args, **kwargs): - return {'path' : self.path} + return {'path': self.path} def __repr__(self, *args, **kwargs): return f'Profile({os.path.basename(self.profile)})' diff --git a/archinstall/lib/storage.py b/archinstall/lib/storage.py index d985ca17..53d5e938 100644 --- a/archinstall/lib/storage.py +++ b/archinstall/lib/storage.py @@ -8,15 +8,15 @@ import os # # And Keeping this in dict ensures that variables are shared across imports. storage = { - 'PROFILE_PATH' : [ + 'PROFILE_PATH': [ './profiles', '~/.config/archinstall/profiles', os.path.join(os.path.dirname(os.path.abspath(__file__)), 'profiles'), # os.path.abspath(f'{os.path.dirname(__file__)}/../examples') ], - 'UPSTREAM_URL' : 'https://raw.githubusercontent.com/archlinux/archinstall/master/profiles', - 'PROFILE_DB' : None, # Used in cases when listing profiles is desired, not mandatory for direct profile grabing. - 'LOG_PATH' : '/var/log/archinstall', - 'LOG_FILE' : 'install.log', - 'MOUNT_POINT' : '/mnt' + 'UPSTREAM_URL': 'https://raw.githubusercontent.com/archlinux/archinstall/master/profiles', + 'PROFILE_DB': None, # Used in cases when listing profiles is desired, not mandatory for direct profile grabing. + 'LOG_PATH': '/var/log/archinstall', + 'LOG_FILE': 'install.log', + 'MOUNT_POINT': '/mnt' } diff --git a/archinstall/lib/user_interaction.py b/archinstall/lib/user_interaction.py index 0aeba3b9..4ca0fed8 100644 --- a/archinstall/lib/user_interaction.py +++ b/archinstall/lib/user_interaction.py @@ -48,6 +48,7 @@ def check_for_correct_username(username): def do_countdown(): SIG_TRIGGER = False + def kill_handler(sig, frame): print() exit(0) @@ -101,17 +102,17 @@ def print_large_list(options, padding=5, margin_bottom=0, separator=': '): longest_line = highest_index_number_length + len(separator) + get_longest_option(options) + padding spaces_without_option = longest_line - (len(separator) + highest_index_number_length) max_num_of_columns = get_terminal_width() // longest_line - max_options_in_cells = max_num_of_columns * (get_terminal_height()-margin_bottom) + max_options_in_cells = max_num_of_columns * (get_terminal_height() - margin_bottom) if (len(options) > max_options_in_cells): for index, option in enumerate(options): print(f"{index}: {option}") return 1, index else: - for row in range(0, (get_terminal_height()-margin_bottom)): - for column in range(row, len(options), (get_terminal_height()-margin_bottom)): - spaces = " "*(spaces_without_option - len(options[column])) - print(f"{str(column): >{highest_index_number_length}}{separator}{options[column]}", end = spaces) + for row in range(0, (get_terminal_height() - margin_bottom)): + for column in range(row, len(options), (get_terminal_height() - margin_bottom)): + spaces = " " * (spaces_without_option - len(options[column])) + print(f"{str(column): >{highest_index_number_length}}{separator}{options[column]}", end=spaces) print() return column, row @@ -148,7 +149,7 @@ def generic_multi_select(options, text="Select one or more of the options above else: printed_options.append(f'{option}') - section.clear(0, get_terminal_height()-section._cursor_y-1) + section.clear(0, get_terminal_height() - section._cursor_y - 1) print_large_list(printed_options, margin_bottom=2) section._cursor_y = len(printed_options) section._cursor_x = 0 @@ -204,7 +205,7 @@ class MiniCurses: sys.stdout.flush() sys.stdout.write("\033[%dG" % 0) sys.stdout.flush() - sys.stdout.write(" " * (get_terminal_width()-1)) + sys.stdout.write(" " * (get_terminal_width() - 1)) sys.stdout.flush() sys.stdout.write("\033[%dG" % 0) sys.stdout.flush() @@ -223,17 +224,17 @@ class MiniCurses: sys.stdout.flush() sys.stdout.write('\033[%d;%df' % (y, x)) - for line in range(get_terminal_height()-y-1, y): - sys.stdout.write(" " * (get_terminal_width()-1)) + for line in range(get_terminal_height() - y - 1, y): + sys.stdout.write(" " * (get_terminal_width() - 1)) sys.stdout.flush() sys.stdout.write('\033[%d;%df' % (y, x)) sys.stdout.flush() def deal_with_control_characters(self, char): mapper = { - '\x7f' : 'BACKSPACE', - '\r' : 'CR', - '\n' : 'NL' + '\x7f': 'BACKSPACE', + '\r': 'CR', + '\n': 'NL' } if (mapped_char := mapper.get(char, None)) == 'BACKSPACE': @@ -319,7 +320,7 @@ def ask_for_superuser_account(prompt='Username for required superuser with sudo continue password = get_password(prompt=f'Password for user {new_user}: ') - return {new_user: {"!password" : password}} + return {new_user: {"!password": password}} def ask_for_additional_users(prompt='Any additional users to install (leave blank for no users): '): @@ -335,9 +336,9 @@ def ask_for_additional_users(prompt='Any additional users to install (leave blan password = get_password(prompt=f'Password for user {new_user}: ') if input("Should this user be a superuser (sudoer) [y/N]: ").strip(' ').lower() in ('y', 'yes'): - superusers[new_user] = {"!password" : password} + superusers[new_user] = {"!password": password} else: - users[new_user] = {"!password" : password} + users[new_user] = {"!password": password} return users, superusers @@ -347,7 +348,7 @@ def ask_for_a_timezone(): timezone = input('Enter a valid timezone (examples: Europe/Stockholm, US/Eastern) or press enter to use UTC: ').strip().strip('*.') if timezone == '': timezone = 'UTC' - if (pathlib.Path("/usr")/"share"/"zoneinfo"/timezone).exists(): + if (pathlib.Path("/usr") / "share" / "zoneinfo" / timezone).exists(): return timezone else: log( @@ -359,17 +360,17 @@ def ask_for_a_timezone(): def ask_for_bootloader() -> str: bootloader = "systemd-bootctl" - if hasUEFI()==False: - bootloader="grub-install" + if hasUEFI() == False: + bootloader = "grub-install" else: bootloader_choice = input("Would you like to use GRUB as a bootloader instead of systemd-boot? [y/N] ").lower() if bootloader_choice == "y": - bootloader="grub-install" + bootloader = "grub-install" return bootloader def ask_for_audio_selection(): - audio = "pulseaudio" # Default for most desktop environments + audio = "pulseaudio" # Default for most desktop environments pipewire_choice = input("Would you like to install pipewire instead of pulseaudio as the default audio server? [Y/n] ").lower() if pipewire_choice in ("y", ""): audio = "pipewire" @@ -379,18 +380,18 @@ def ask_for_audio_selection(): def ask_to_configure_network(): # Optionally configure one network interface. - #while 1: + # while 1: # {MAC: Ifname} interfaces = { - 'ISO-CONFIG' : 'Copy ISO network configuration to installation', - 'NetworkManager':'Use NetworkManager to control and manage your internet connection', + 'ISO-CONFIG': 'Copy ISO network configuration to installation', + 'NetworkManager': 'Use NetworkManager to control and manage your internet connection', **list_interfaces() } nic = generic_select(interfaces, "Select one network interface to configure (leave blank to skip): ") if nic and nic != 'Copy ISO network configuration to installation': if nic == 'Use NetworkManager to control and manage your internet connection': - return {'nic': nic,'NetworkManager':True} + return {'nic': nic, 'NetworkManager': True} # Current workaround: # For selecting modes without entering text within brackets, @@ -401,7 +402,7 @@ def ask_to_configure_network(): print(f"{index}: {mode}") mode = generic_select(['DHCP', 'IP'], f"Select which mode to configure for {nic} or leave blank for DHCP: ", - options_output=False) + options_output=False) if mode == 'IP': while 1: ip = input(f"Enter the IP and subnet for {nic} (example: 192.168.0.5/24): ").strip() @@ -436,7 +437,7 @@ def ask_to_configure_network(): if len(dns_input := input('Enter your DNS servers (space separated, blank for none): ').strip()): dns = dns_input.split(' ') - return {'nic': nic, 'dhcp': False, 'ip': ip, 'gateway' : gateway, 'dns' : dns} + return {'nic': nic, 'dhcp': False, 'ip': ip, 'gateway': gateway, 'dns': dns} else: return {'nic': nic} elif nic: @@ -447,26 +448,26 @@ def ask_to_configure_network(): def ask_for_disk_layout(): options = { - 'keep-existing' : 'Keep existing partition layout and select which ones to use where', - 'format-all' : 'Format entire drive and setup a basic partition scheme', - 'abort' : 'Abort the installation' + 'keep-existing': 'Keep existing partition layout and select which ones to use where', + 'format-all': 'Format entire drive and setup a basic partition scheme', + 'abort': 'Abort the installation' } value = generic_select(options, "Found partitions on the selected drive, (select by number) what you want to do: ", - allow_empty_input=False, sort=True) + allow_empty_input=False, sort=True) return next((key for key, val in options.items() if val == value), None) def ask_for_main_filesystem_format(): options = { - 'btrfs' : 'btrfs', - 'ext4' : 'ext4', - 'xfs' : 'xfs', - 'f2fs' : 'f2fs' + 'btrfs': 'btrfs', + 'ext4': 'ext4', + 'xfs': 'xfs', + 'f2fs': 'f2fs' } value = generic_select(options, "Select which filesystem your main partition should use (by number or name): ", - allow_empty_input=False) + allow_empty_input=False) return next((key for key, val in options.items() if val == value), None) @@ -526,7 +527,7 @@ def generic_select(options, input_text="Select one of the above by index or abso selected_option = options[selected_option] break elif selected_option in options: - break # We gave a correct absolute value + break # We gave a correct absolute value else: raise RequirementError(f'Selected option "{selected_option}" does not exist in available options') except RequirementError as err: @@ -584,7 +585,7 @@ def select_profile(options): print(' -- (Leave blank and hit enter to skip this step and continue) --') selected_profile = generic_select(profiles, 'Enter a pre-programmed profile name if you want to install one: ', - options_output=False) + options_output=False) if selected_profile: return Profile(None, selected_profile) else: @@ -674,8 +675,9 @@ def select_mirror_regions(mirrors, show_top_mirrors=True): print_large_list(regions, margin_bottom=4) print(' -- You can skip this step by leaving the option blank --') - selected_mirror = generic_select(regions, 'Select one of the above regions to download packages from (by number or full name): ', - options_output=False) + selected_mirror = generic_select(regions, + 'Select one of the above regions to download packages from (by number or full name): ', + options_output=False) if not selected_mirror: # Returning back empty options which can be both used to # do "if x:" logic as well as do `x.get('mirror', {}).get('sub', None)` chaining diff --git a/docs/conf.py b/docs/conf.py index 2c699184..9d23f979 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -43,7 +43,6 @@ author = 'Anton Hvornum' # The full version, including alpha/beta/rc tags release = 'v2.1.0' - # -- General configuration --------------------------------------------------- master_doc = 'index' @@ -64,7 +63,6 @@ templates_path = ['_templates'] # This pattern also affects html_static_path and html_extra_path. exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store'] - # -- Options for HTML output ------------------------------------------------- # The theme to use for HTML and HTML Help pages. See the documentation for diff --git a/docs/pull_request_template.md b/docs/pull_request_template.md index 729c1aae..18d01ab2 100644 --- a/docs/pull_request_template.md +++ b/docs/pull_request_template.md @@ -12,5 +12,5 @@ If the PR is larger than ~20 lines, please describe it here unless described in # Testing -Any new feature or stability improvement should be tested if possible. -Please follow the test instructions at the bottom of the README or use the ISO built on each PR. \ No newline at end of file +Any new feature or stability improvement should be tested if possible. Please follow the test instructions at the bottom +of the README or use the ISO built on each PR. diff --git a/examples/guided.py b/examples/guided.py index a4fb5e3b..76741708 100644 --- a/examples/guided.py +++ b/examples/guided.py @@ -1,9 +1,9 @@ -import time import json import logging +import time + import archinstall from archinstall.lib.hardware import hasUEFI -from archinstall.lib.profiles import Profile if archinstall.arguments.get('help'): print("See `man archinstall` for help.") -- cgit v1.2.3-54-g00ecf From a0cbb31d3ef12d5cde2253b4a31757c0409aee40 Mon Sep 17 00:00:00 2001 From: Dylan Taylor Date: Sat, 15 May 2021 14:10:44 -0400 Subject: f-string fixes --- archinstall/lib/disk.py | 14 +++++++------- archinstall/lib/general.py | 2 +- archinstall/lib/hardware.py | 2 +- archinstall/lib/installer.py | 22 +++++++++++----------- archinstall/lib/mirrors.py | 2 +- archinstall/lib/user_interaction.py | 8 ++++---- examples/guided.py | 16 ++++++++-------- profiles/server.py | 4 ++-- 8 files changed, 35 insertions(+), 35 deletions(-) (limited to 'archinstall/lib/user_interaction.py') diff --git a/archinstall/lib/disk.py b/archinstall/lib/disk.py index 62f1785f..5967bf3e 100644 --- a/archinstall/lib/disk.py +++ b/archinstall/lib/disk.py @@ -105,7 +105,7 @@ class BlockDevice(): raise DiskError(f'Can not read partitions off something that isn\'t a block device: {self.path}') if not o[:1] == b'{': - raise DiskError(f'Error getting JSON output from:', f'/usr/bin/lsblk -J {self.path}') + raise DiskError('Error getting JSON output from:', f'/usr/bin/lsblk -J {self.path}') r = json.loads(o.decode('UTF-8')) if len(r['blockdevices']) and 'children' in r['blockdevices'][0]: @@ -130,7 +130,7 @@ class BlockDevice(): @property def uuid(self): - log(f'BlockDevice().uuid is untested!', level=logging.WARNING, fg='yellow') + log('BlockDevice().uuid is untested!', level=logging.WARNING, fg='yellow') """ Returns the disk UUID as returned by lsblk. This is more reliable than relying on /dev/disk/by-partuuid as @@ -441,12 +441,12 @@ class Filesystem(): self.blockdevice.flush_cache() return self else: - raise DiskError(f'Problem setting the partition format to GPT:', f'/usr/bin/parted -s {self.blockdevice.device} mklabel gpt') + raise DiskError('Problem setting the partition format to GPT:', f'/usr/bin/parted -s {self.blockdevice.device} mklabel gpt') elif self.mode == MBR: if sys_command(f'/usr/bin/parted -s {self.blockdevice.device} mklabel msdos').exit_code == 0: return self else: - raise DiskError(f'Problem setting the partition format to GPT:', f'/usr/bin/parted -s {self.blockdevice.device} mklabel msdos') + raise DiskError('Problem setting the partition format to GPT:', f'/usr/bin/parted -s {self.blockdevice.device} mklabel msdos') else: raise DiskError(f'Unknown mode selected to format in: {self.mode}') @@ -465,7 +465,7 @@ class Filesystem(): # TODO: https://stackoverflow.com/questions/28157929/how-to-safely-handle-an-exception-inside-a-context-manager if len(args) >= 2 and args[1]: raise args[1] - b''.join(sys_command(f'sync')) + b''.join(sys_command('sync')) return True def find_partition(self, mountpoint): @@ -565,7 +565,7 @@ def all_disks(*args, **kwargs): kwargs.setdefault("partitions", False) drives = OrderedDict() # for drive in json.loads(sys_command(f'losetup --json', *args, **lkwargs, hide_from_log=True)).decode('UTF_8')['loopdevices']: - for drive in json.loads(b''.join(sys_command(f'lsblk --json -l -n -o path,size,type,mountpoint,label,pkname,model', *args, **kwargs, hide_from_log=True)).decode('UTF_8'))['blockdevices']: + for drive in json.loads(b''.join(sys_command('lsblk --json -l -n -o path,size,type,mountpoint,label,pkname,model', *args, **kwargs, hide_from_log=True)).decode('UTF_8'))['blockdevices']: if not kwargs['partitions'] and drive['type'] == 'part': continue drives[drive['path']] = BlockDevice(drive['path'], drive) @@ -639,7 +639,7 @@ def get_filesystem_type(path): def disk_layouts(): try: - handle = sys_command(f"lsblk -f -o+TYPE,SIZE -J") + handle = sys_command("lsblk -f -o+TYPE,SIZE -J") return json.loads(b''.join(handle).decode('UTF-8')) except SysCallError as err: log(f"Could not return disk layouts: {err}") diff --git a/archinstall/lib/general.py b/archinstall/lib/general.py index f9b9078a..b65e2593 100644 --- a/archinstall/lib/general.py +++ b/archinstall/lib/general.py @@ -281,7 +281,7 @@ class sys_command: if 'debug' in self.kwargs and self.kwargs['debug']: self.log(f"Waiting for last command {self.cmd[0]} to finish.", level=logging.DEBUG) - if bytes(f']$'.lower(), 'UTF-8') in self.trace_log[0 - len(f']$') - 5:].lower(): + if bytes(']$'.lower(), 'UTF-8') in self.trace_log[0 - len(']$') - 5:].lower(): if 'debug' in self.kwargs and self.kwargs['debug']: self.log(f"{self.cmd[0]} has finished.", level=logging.DEBUG) alive = False diff --git a/archinstall/lib/hardware.py b/archinstall/lib/hardware.py index 3203daee..d1723cde 100644 --- a/archinstall/lib/hardware.py +++ b/archinstall/lib/hardware.py @@ -78,7 +78,7 @@ def hasUEFI() -> bool: def graphicsDevices() -> dict: cards = {} - for line in sys_command(f"lspci"): + for line in sys_command("lspci"): if b' VGA ' in line: _, identifier = line.split(b': ', 1) cards[identifier.strip().lower().decode('UTF-8')] = line diff --git a/archinstall/lib/installer.py b/archinstall/lib/installer.py index cacdff69..bac19007 100644 --- a/archinstall/lib/installer.py +++ b/archinstall/lib/installer.py @@ -70,7 +70,7 @@ class Installer(): return self def __exit__(self, *args, **kwargs): - # b''.join(sys_command(f'sync')) # No need to, since the underlying fs() object will call sync. + # b''.join(sys_command('sync')) # No need to, since the underlying fs() object will call sync. # TODO: https://stackoverflow.com/questions/28157929/how-to-safely-handle-an-exception-inside-a-context-manager if len(args) >= 2 and args[1]: @@ -82,7 +82,7 @@ class Installer(): # We avoid printing /mnt/ because that might confuse people if they note it down # and then reboot, and a identical log file will be found in the ISO medium anyway. print(f"[!] A log file has been created here: {os.path.join(storage['LOG_PATH'], storage['LOG_FILE'])}") - print(f" Please submit this issue (and file) to https://github.com/archlinux/archinstall/issues") + print(" Please submit this issue (and file) to https://github.com/archlinux/archinstall/issues") raise args[1] self.genfstab() @@ -98,7 +98,7 @@ class Installer(): self.log(f' - {step}', fg='red', level=logging.WARNING) self.log(f"Detailed error logs can be found at: {storage['LOG_PATH']}", level=logging.WARNING) - self.log(f"Submit this zip file as an issue to https://github.com/archlinux/archinstall/issues", level=logging.WARNING) + self.log("Submit this zip file as an issue to https://github.com/archlinux/archinstall/issues", level=logging.WARNING) self.sync_log_to_install_medium() return False @@ -183,7 +183,7 @@ class Installer(): ) def activate_ntp(self): - self.log(f'Installing and activating NTP.', level=logging.INFO) + self.log('Installing and activating NTP.', level=logging.INFO) if self.pacstrap('ntp'): if self.enable_service('ntpd'): return True @@ -389,7 +389,7 @@ class Installer(): else: loader_data = [ f"default {self.init_time}", - f"timeout 5" + "timeout 5" ] with open(f'{self.target}/boot/loader/loader.conf', 'w') as loader: @@ -407,10 +407,10 @@ class Installer(): # UUID = sys_command('blkid -s PARTUUID -o value {drive}{partition_2}'.format(**args)).decode('UTF-8').strip() # Setup the loader entry with open(f'{self.target}/boot/loader/entries/{self.init_time}.conf', 'w') as entry: - entry.write(f'# Created by: archinstall\n') + entry.write('# Created by: archinstall\n') entry.write(f'# Created on: {self.init_time}\n') - entry.write(f'title Arch Linux\n') - entry.write(f'linux /vmlinuz-linux\n') + entry.write('title Arch Linux\n') + entry.write('linux /vmlinuz-linux\n') if not isVM(): vendor = cpuVendor() if vendor == "AuthenticAMD": @@ -419,7 +419,7 @@ class Installer(): entry.write("initrd /intel-ucode.img\n") else: self.log("unknow cpu vendor, not adding ucode to systemd-boot config") - entry.write(f'initrd /initramfs-linux.img\n') + entry.write('initrd /initramfs-linux.img\n') ## blkid doesn't trigger on loopback devices really well, ## so we'll use the old manual method until we get that sorted out. @@ -506,7 +506,7 @@ class Installer(): if len(language.strip()): with open(f'{self.target}/etc/vconsole.conf', 'w') as vconsole: vconsole.write(f'KEYMAP={language}\n') - vconsole.write(f'FONT=lat9w-16\n') + vconsole.write('FONT=lat9w-16\n') else: - self.log(f'Keyboard language was not changed from default (no language specified).', fg="yellow", level=logging.INFO) + self.log('Keyboard language was not changed from default (no language specified).', fg="yellow", level=logging.INFO) return True diff --git a/archinstall/lib/mirrors.py b/archinstall/lib/mirrors.py index 4b05b96a..e2630710 100644 --- a/archinstall/lib/mirrors.py +++ b/archinstall/lib/mirrors.py @@ -77,7 +77,7 @@ def re_rank_mirrors(top=10, *positionals, **kwargs): def list_mirrors(): - url = f"https://archlinux.org/mirrorlist/?protocol=https&ip_version=4&ip_version=6&use_mirror_status=on" + url = "https://archlinux.org/mirrorlist/?protocol=https&ip_version=4&ip_version=6&use_mirror_status=on" regions = {} try: diff --git a/archinstall/lib/user_interaction.py b/archinstall/lib/user_interaction.py index 4ca0fed8..69689479 100644 --- a/archinstall/lib/user_interaction.py +++ b/archinstall/lib/user_interaction.py @@ -125,7 +125,7 @@ def generic_multi_select(options, text="Select one or more of the options above log(" * If problem persists, please create an issue on https://github.com/archlinux/archinstall/issues * ", fg='yellow') raise RequirementError("generic_multi_select() requires list or dictionary as options.") if not options: - log(f" * Generic multi-select didn't find any options to choose from * ", fg='red') + log(" * Generic multi-select didn't find any options to choose from * ", fg='red') log(" * If problem persists, please create an issue on https://github.com/archlinux/archinstall/issues * ", fg='yellow') raise RequirementError('generic_multi_select() requires at least one option to proceed.') # After passing the checks, function continues to work @@ -491,7 +491,7 @@ def generic_select(options, input_text="Select one of the above by index or abso log(" * If problem persists, please create an issue on https://github.com/archlinux/archinstall/issues * ", fg='yellow') raise RequirementError("generic_select() requires list or dictionary as options.") if not options: - log(f" * Generic select didn't find any options to choose from * ", fg='red') + log(" * Generic select didn't find any options to choose from * ", fg='red') log(" * If problem persists, please create an issue on https://github.com/archlinux/archinstall/issues * ", fg='yellow') raise RequirementError('generic_select() requires at least one option to proceed.') # After passing the checks, function continues to work @@ -552,7 +552,7 @@ def select_disk(dict_o_disks): for index, drive in enumerate(drives): print(f"{index}: {drive} ({dict_o_disks[drive]['size'], dict_o_disks[drive].device, dict_o_disks[drive]['label']})") - log(f"You can skip selecting a drive and partitioning and use whatever drive-setup is mounted at /mnt (experimental)", fg="yellow") + log("You can skip selecting a drive and partitioning and use whatever drive-setup is mounted at /mnt (experimental)", fg="yellow") drive = generic_select(drives, 'Select one of the above disks (by name or number) or leave blank to use /mnt: ', options_output=False) if not drive: return drive @@ -708,7 +708,7 @@ def select_driver(options=AVAILABLE_GFX_DRIVERS): default_option = options["All open-source (default)"] if drivers: - lspci = sys_command(f'/usr/bin/lspci') + lspci = sys_command('/usr/bin/lspci') for line in lspci.trace_log.split(b'\r\n'): if b' vga ' in line.lower(): if b'nvidia' in line.lower(): diff --git a/examples/guided.py b/examples/guided.py index 9b7d3f3a..0ddca5a2 100644 --- a/examples/guided.py +++ b/examples/guided.py @@ -72,13 +72,13 @@ def ask_user_questions(): # We then ask what to do with the partitions. if (option := archinstall.ask_for_disk_layout()) == 'abort': - archinstall.log(f"Safely aborting the installation. No changes to the disk or system has been made.") + archinstall.log("Safely aborting the installation. No changes to the disk or system has been made.") exit(1) elif option == 'keep-existing': archinstall.arguments['harddrive'].keep_partitions = True - archinstall.log(f" ** You will now select which partitions to use by selecting mount points (inside the installation). **") - archinstall.log(f" ** The root would be a simple / and the boot partition /boot (as all paths are relative inside the installation). **") + archinstall.log(" ** You will now select which partitions to use by selecting mount points (inside the installation). **") + archinstall.log(" ** The root would be a simple / and the boot partition /boot (as all paths are relative inside the installation). **") mountpoints_set = [] while True: # Select a partition @@ -108,8 +108,8 @@ def ask_user_questions(): if (autodetected_filesystem := partition.detect_inner_filesystem(old_password)): new_filesystem = autodetected_filesystem else: - archinstall.log(f"Could not auto-detect the filesystem inside the encrypted volume.", fg='red') - archinstall.log(f"A filesystem must be defined for the unlocked encrypted partition.") + archinstall.log("Could not auto-detect the filesystem inside the encrypted volume.", fg='red') + archinstall.log("A filesystem must be defined for the unlocked encrypted partition.") continue break @@ -215,7 +215,7 @@ def ask_user_questions(): if len(archinstall.arguments['packages']): # Verify packages that were given try: - archinstall.log(f"Verifying that additional packages exist (this might take a few seconds)") + archinstall.log("Verifying that additional packages exist (this might take a few seconds)") archinstall.validate_package_list(archinstall.arguments['packages']) break except archinstall.RequirementError as e: @@ -229,7 +229,7 @@ def ask_user_questions(): if not archinstall.arguments.get('nic', None): archinstall.arguments['nic'] = archinstall.ask_to_configure_network() if not archinstall.arguments['nic']: - archinstall.log(f"No network configuration was selected. Network is going to be unavailable until configured manually!", fg="yellow") + archinstall.log("No network configuration was selected. Network is going to be unavailable until configured manually!", fg="yellow") if not archinstall.arguments.get('timezone', None): archinstall.arguments['timezone'] = archinstall.ask_for_a_timezone() @@ -312,7 +312,7 @@ def perform_installation(mountpoint): # Certain services might be running that affects the system during installation. # Currently, only one such service is "reflector.service" which updates /etc/pacman.d/mirrorlist # We need to wait for it before we continue since we opted in to use a custom mirror/region. - installation.log(f'Waiting for automatic mirror selection (reflector) to complete.', level=logging.INFO) + installation.log('Waiting for automatic mirror selection (reflector) to complete.', level=logging.INFO) while archinstall.service_state('reflector') not in ('dead', 'failed'): time.sleep(1) # Set mirrors used by pacstrap (outside of installation) diff --git a/profiles/server.py b/profiles/server.py index afc37776..355be9f5 100644 --- a/profiles/server.py +++ b/profiles/server.py @@ -14,7 +14,7 @@ def _prep_function(*args, **kwargs): Magic function called by the importing installer before continuing any further. """ - selected_servers = archinstall.generic_multi_select(available_servers, f"Choose which servers to install and enable (leave blank for a minimal installation): ") + selected_servers = archinstall.generic_multi_select(available_servers, "Choose which servers to install and enable (leave blank for a minimal installation): ") archinstall.storage['_selected_servers'] = selected_servers return True @@ -24,7 +24,7 @@ if __name__ == 'server': """ This "profile" is a meta-profile. """ - archinstall.log(f'Now installing the selected servers.', level=logging.INFO) + archinstall.log('Now installing the selected servers.', level=logging.INFO) archinstall.log(archinstall.storage['_selected_servers'], level=logging.DEBUG) for server in archinstall.storage['_selected_servers']: archinstall.log(f'Installing {server} ...', level=logging.INFO) -- cgit v1.2.3-54-g00ecf From 126c7ebfca16156d4e1c738a5f562ba99a4ab5f2 Mon Sep 17 00:00:00 2001 From: Dylan Taylor Date: Sat, 15 May 2021 14:46:40 -0400 Subject: More formatting fixes to satisfy PEP 8 --- archinstall/__init__.py | 3 ++- archinstall/lib/disk.py | 28 ++++++++++++++++------------ archinstall/lib/general.py | 6 +++--- archinstall/lib/installer.py | 19 +++++++++++-------- archinstall/lib/luks.py | 3 ++- archinstall/lib/networking.py | 1 + archinstall/lib/profiles.py | 4 ++-- archinstall/lib/storage.py | 2 +- archinstall/lib/user_interaction.py | 27 ++++++++++++--------------- docs/conf.py | 5 +---- examples/guided.py | 8 ++------ profiles/desktop.py | 4 +--- profiles/i3.py | 4 +--- profiles/sway.py | 4 +--- 14 files changed, 56 insertions(+), 62 deletions(-) (limited to 'archinstall/lib/user_interaction.py') diff --git a/archinstall/__init__.py b/archinstall/__init__.py index f1d8341e..58012a21 100644 --- a/archinstall/__init__.py +++ b/archinstall/__init__.py @@ -34,7 +34,8 @@ for arg in sys.argv[1:]: # TODO: Learn the dark arts of argparse... -# (I summon thee dark spawn of cPython) +# (I summon thee dark spawn of cPython) + def run_as_a_module(): """ diff --git a/archinstall/lib/disk.py b/archinstall/lib/disk.py index 65d1599b..85e8a402 100644 --- a/archinstall/lib/disk.py +++ b/archinstall/lib/disk.py @@ -17,7 +17,8 @@ MBR = 0b00000010 # libc = ctypes.CDLL(ctypes.util.find_library('c'), use_errno=True) # libc.mount.argtypes = (ctypes.c_char_p, ctypes.c_char_p, ctypes.c_char_p, ctypes.c_ulong, ctypes.c_char_p) -class BlockDevice(): + +class BlockDevice: def __init__(self, path, info=None): if not info: # If we don't give any information, we need to auto-fill it. @@ -76,7 +77,8 @@ class BlockDevice(): if self.info['type'] == 'loop': for drive in json.loads(b''.join(sys_command(['losetup', '--json'], hide_from_log=True)).decode('UTF_8'))['loopdevices']: - if not drive['name'] == self.path: continue + if not drive['name'] == self.path: + continue return drive['back-file'] elif self.info['type'] == 'disk': @@ -91,8 +93,8 @@ class BlockDevice(): else: log(f"Unknown blockdevice type for {self.path}: {self.info['type']}", level=logging.DEBUG) - # if not stat.S_ISBLK(os.stat(full_path).st_mode): - # raise DiskError(f'Selected disk "{full_path}" is not a block device.') + # if not stat.S_ISBLK(os.stat(full_path).st_mode): + # raise DiskError(f'Selected disk "{full_path}" is not a block device.') @property def partitions(self): @@ -153,7 +155,7 @@ class BlockDevice(): self.part_cache = OrderedDict() -class Partition(): +class Partition: def __init__(self, path: str, block_device: BlockDevice, part_id=None, size=-1, filesystem=None, mountpoint=None, encrypted=False, autodetect_filesystem=True): if not part_id: part_id = os.path.basename(path) @@ -236,7 +238,7 @@ class Partition(): for blockdevice in json.loads(b''.join(sys_command('lsblk -J')).decode('UTF-8'))['blockdevices']: if (parent := self.find_parent_of(blockdevice, os.path.basename(self.path))): return f"/dev/{parent}" - # raise DiskError(f'Could not find appropriate parent for encrypted partition {self}') + # raise DiskError(f'Could not find appropriate parent for encrypted partition {self}') return self.path def detect_inner_filesystem(self, password): @@ -351,9 +353,9 @@ class Partition(): self.filesystem = 'f2fs' elif filesystem == 'crypto_LUKS': - # from .luks import luks2 - # encrypted_partition = luks2(self, None, None) - # encrypted_partition.format(path) + # from .luks import luks2 + # encrypted_partition = luks2(self, None, None) + # encrypted_partition.format(path) self.filesystem = 'crypto_LUKS' else: @@ -378,7 +380,8 @@ class Partition(): if not self.mountpoint: log(f'Mounting {self} to {target}', level=logging.INFO) if not fs: - if not self.filesystem: raise DiskError(f'Need to format (or define) the filesystem on {self} before mounting.') + if not self.filesystem: + raise DiskError(f'Need to format (or define) the filesystem on {self} before mounting.') fs = self.filesystem pathlib.Path(target).mkdir(parents=True, exist_ok=True) @@ -425,7 +428,7 @@ class Partition(): return True -class Filesystem(): +class Filesystem: # TODO: # When instance of a HDD is selected, check all usages and gracefully unmount them # as well as close any crypto handles. @@ -566,7 +569,8 @@ def all_disks(*args, **kwargs): drives = OrderedDict() # for drive in json.loads(sys_command(f'losetup --json', *args, **lkwargs, hide_from_log=True)).decode('UTF_8')['loopdevices']: for drive in json.loads(b''.join(sys_command('lsblk --json -l -n -o path,size,type,mountpoint,label,pkname,model', *args, **kwargs, hide_from_log=True)).decode('UTF_8'))['blockdevices']: - if not kwargs['partitions'] and drive['type'] == 'part': continue + if not kwargs['partitions'] and drive['type'] == 'part': + continue drives[drive['path']] = BlockDevice(drive['path'], drive) return drives diff --git a/archinstall/lib/general.py b/archinstall/lib/general.py index b65e2593..816fa755 100644 --- a/archinstall/lib/general.py +++ b/archinstall/lib/general.py @@ -170,7 +170,7 @@ class sys_command: 'ended': self.ended, 'started_pprint': '{}-{}-{} {}:{}:{}'.format(*time.localtime(self.started)), 'ended_pprint': '{}-{}-{} {}:{}:{}'.format(*time.localtime(self.ended)) if self.ended else None, - 'exit_code': self.exit_code + 'exit_code': self.exit_code, } def peak(self, output: Union[str, bytes]) -> bool: @@ -256,7 +256,7 @@ class sys_command: original = trigger trigger = bytes(original, 'UTF-8') self.kwargs['events'][trigger] = self.kwargs['events'][original] - del (self.kwargs['events'][original]) + del self.kwargs['events'][original] if type(self.kwargs['events'][trigger]) != bytes: self.kwargs['events'][trigger] = bytes(self.kwargs['events'][trigger], 'UTF-8') @@ -269,7 +269,7 @@ class sys_command: last_trigger_pos = trigger_pos os.write(child_fd, self.kwargs['events'][trigger]) - del (self.kwargs['events'][trigger]) + del self.kwargs['events'][trigger] broke = True break diff --git a/archinstall/lib/installer.py b/archinstall/lib/installer.py index bac19007..139d9d33 100644 --- a/archinstall/lib/installer.py +++ b/archinstall/lib/installer.py @@ -9,7 +9,7 @@ from .user_interaction import * __packages__ = ["base", "base-devel", "linux-firmware", "linux", "linux-lts", "linux-zen", "linux-hardened"] -class Installer(): +class Installer: """ `Installer()` is the wrapper for most basic installation steps. It also wraps :py:func:`~archinstall.Installer.pacstrap` among other things. @@ -127,7 +127,8 @@ class Installer(): return [step for step, flag in self.helper_flags.items() if flag is False] def pacstrap(self, *packages, **kwargs): - if type(packages[0]) in (list, tuple): packages = packages[0] + if type(packages[0]) in (list, tuple): + packages = packages[0] self.log(f'Installing packages: {packages}', level=logging.INFO) if (sync_mirrors := sys_command('/usr/bin/pacman -Syy')).exit_code == 0: @@ -158,7 +159,8 @@ class Installer(): fh.write(hostname + '\n') def set_locale(self, locale, encoding='UTF-8', *args, **kwargs): - if not len(locale): return True + if not len(locale): + return True with open(f'{self.target}/etc/locale.gen', 'a') as fh: fh.write(f'{locale}.{encoding} {encoding}\n') @@ -168,8 +170,10 @@ class Installer(): return True if sys_command(f'/usr/bin/arch-chroot {self.target} locale-gen').exit_code == 0 else False def set_timezone(self, zone, *args, **kwargs): - if not zone: return True - if not len(zone): return True # Redundant + if not zone: + return True + if not len(zone): + return True # Redundant if (pathlib.Path("/usr") / "share" / "zoneinfo" / zone).exists(): (pathlib.Path(self.target) / "etc" / "localtime").unlink(missing_ok=True) @@ -263,6 +267,7 @@ class Installer(): if enable_services: # If we haven't installed the base yet (function called pre-maturely) if self.helper_flags.get('base', False) is False: + def post_install_enable_networkd_resolved(*args, **kwargs): self.enable_service('systemd-networkd', 'systemd-resolved') @@ -332,9 +337,7 @@ class Installer(): self.helper_flags['base-strapped'] = True with open(f"{self.target}/etc/fstab", "a") as fstab: - fstab.write( - "\ntmpfs /tmp tmpfs defaults,noatime,mode=1777 0 0\n" - ) # Redundant \n at the start? who knows? + fstab.write("\ntmpfs /tmp tmpfs defaults,noatime,mode=1777 0 0\n") # Redundant \n at the start? who knows? ## TODO: Support locale and timezone # os.remove(f'{self.target}/etc/localtime') diff --git a/archinstall/lib/luks.py b/archinstall/lib/luks.py index e6e1c897..1cb6777f 100644 --- a/archinstall/lib/luks.py +++ b/archinstall/lib/luks.py @@ -5,7 +5,7 @@ from .general import * from .output import log -class luks2(): +class luks2: def __init__(self, partition, mountpoint, password, key_file=None, auto_unmount=False, *args, **kwargs): self.password = password self.partition = partition @@ -120,6 +120,7 @@ class luks2(): :type mountpoint: str """ from .disk import get_filesystem_type + if '/' in mountpoint: os.path.basename(mountpoint) # TODO: Raise exception instead? diff --git a/archinstall/lib/networking.py b/archinstall/lib/networking.py index 3e5ed4e7..e12d9cc3 100644 --- a/archinstall/lib/networking.py +++ b/archinstall/lib/networking.py @@ -68,6 +68,7 @@ def get_wireless_networks(interface): # TODO: Make this oneliner pritter to check if the interface is scanning or not. if not '_WIFI' in storage or interface not in storage['_WIFI'] or storage['_WIFI'][interface].get('scanning', False) is False: import time + wireless_scan(interface) time.sleep(5) diff --git a/archinstall/lib/profiles.py b/archinstall/lib/profiles.py index 2f97231c..894b8fcb 100644 --- a/archinstall/lib/profiles.py +++ b/archinstall/lib/profiles.py @@ -14,7 +14,7 @@ from .storage import storage def grab_url_data(path): - safe_path = path[:path.find(':') + 1] + ''.join([item if item in ('/', '?', '=', '&') else urllib.parse.quote(item) for item in multisplit(path[path.find(':') + 1:], ('/', '?', '=', '&'))]) + safe_path = path[: path.find(':') + 1] + ''.join([item if item in ('/', '?', '=', '&') else urllib.parse.quote(item) for item in multisplit(path[path.find(':') + 1:], ('/', '?', '=', '&'))]) ssl_context = ssl.create_default_context() ssl_context.check_hostname = False ssl_context.verify_mode = ssl.CERT_NONE @@ -75,7 +75,7 @@ def list_profiles(filter_irrelevant_macs=True, subpath='', filter_top_level_prof if filter_top_level_profiles: for profile in list(cache.keys()): if Profile(None, profile).is_top_level_profile() is False: - del (cache[profile]) + del cache[profile] return cache diff --git a/archinstall/lib/storage.py b/archinstall/lib/storage.py index 53d5e938..42214572 100644 --- a/archinstall/lib/storage.py +++ b/archinstall/lib/storage.py @@ -18,5 +18,5 @@ storage = { 'PROFILE_DB': None, # Used in cases when listing profiles is desired, not mandatory for direct profile grabing. 'LOG_PATH': '/var/log/archinstall', 'LOG_FILE': 'install.log', - 'MOUNT_POINT': '/mnt' + 'MOUNT_POINT': '/mnt', } diff --git a/archinstall/lib/user_interaction.py b/archinstall/lib/user_interaction.py index 69689479..f39eea1c 100644 --- a/archinstall/lib/user_interaction.py +++ b/archinstall/lib/user_interaction.py @@ -23,6 +23,7 @@ from .profiles import Profile # TODO: Some inconsistencies between the selection processes. # Some return the keys from the options, some the values? + def get_terminal_height(): return shutil.get_terminal_size().lines @@ -104,7 +105,7 @@ def print_large_list(options, padding=5, margin_bottom=0, separator=': '): max_num_of_columns = get_terminal_width() // longest_line max_options_in_cells = max_num_of_columns * (get_terminal_height() - margin_bottom) - if (len(options) > max_options_in_cells): + if len(options) > max_options_in_cells: for index, option in enumerate(options): print(f"{index}: {option}") return 1, index @@ -214,8 +215,10 @@ class MiniCurses: self._cursor_x += len(text) def clear(self, x, y): - if x < 0: x = 0 - if y < 0: y = 0 + if x < 0: + x = 0 + if y < 0: + y = 0 # import time # sys.stdout.write(f"Clearing from: {x, y}") @@ -401,8 +404,7 @@ def ask_to_configure_network(): for index, mode in enumerate(modes): print(f"{index}: {mode}") - mode = generic_select(['DHCP', 'IP'], f"Select which mode to configure for {nic} or leave blank for DHCP: ", - options_output=False) + mode = generic_select(['DHCP', 'IP'], f"Select which mode to configure for {nic} or leave blank for DHCP: ", options_output=False) if mode == 'IP': while 1: ip = input(f"Enter the IP and subnet for {nic} (example: 192.168.0.5/24): ").strip() @@ -450,11 +452,10 @@ def ask_for_disk_layout(): options = { 'keep-existing': 'Keep existing partition layout and select which ones to use where', 'format-all': 'Format entire drive and setup a basic partition scheme', - 'abort': 'Abort the installation' + 'abort': 'Abort the installation', } - value = generic_select(options, "Found partitions on the selected drive, (select by number) what you want to do: ", - allow_empty_input=False, sort=True) + value = generic_select(options, "Found partitions on the selected drive, (select by number) what you want to do: ", allow_empty_input=False, sort=True) return next((key for key, val in options.items() if val == value), None) @@ -466,8 +467,7 @@ def ask_for_main_filesystem_format(): 'f2fs': 'f2fs' } - value = generic_select(options, "Select which filesystem your main partition should use (by number or name): ", - allow_empty_input=False) + value = generic_select(options, "Select which filesystem your main partition should use (by number or name): ", allow_empty_input=False) return next((key for key, val in options.items() if val == value), None) @@ -584,8 +584,7 @@ def select_profile(options): print(' -- They might make it easier to install things like desktop environments. --') print(' -- (Leave blank and hit enter to skip this step and continue) --') - selected_profile = generic_select(profiles, 'Enter a pre-programmed profile name if you want to install one: ', - options_output=False) + selected_profile = generic_select(profiles, 'Enter a pre-programmed profile name if you want to install one: ', options_output=False) if selected_profile: return Profile(None, selected_profile) else: @@ -675,9 +674,7 @@ def select_mirror_regions(mirrors, show_top_mirrors=True): print_large_list(regions, margin_bottom=4) print(' -- You can skip this step by leaving the option blank --') - selected_mirror = generic_select(regions, - 'Select one of the above regions to download packages from (by number or full name): ', - options_output=False) + selected_mirror = generic_select(regions, 'Select one of the above regions to download packages from (by number or full name): ', options_output=False) if not selected_mirror: # Returning back empty options which can be both used to # do "if x:" logic as well as do `x.get('mirror', {}).get('sub', None)` chaining diff --git a/docs/conf.py b/docs/conf.py index 9d23f979..88a55e02 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -123,8 +123,5 @@ man_pages = [("index", "archinstall", u"archinstall Documentation", [u"Anton Hvo # (source start file, target name, title, author, # dir menu entry, description, category) texinfo_documents = [ - ( - "index", "archinstall", u"archinstall Documentation", - u"Anton Hvornum", "archinstall", "Simple and minimal HTTP server." - ), + ("index", "archinstall", u"archinstall Documentation", u"Anton Hvornum", "archinstall", "Simple and minimal HTTP server."), ] diff --git a/examples/guided.py b/examples/guided.py index 0ddca5a2..f5da9265 100644 --- a/examples/guided.py +++ b/examples/guided.py @@ -84,8 +84,7 @@ def ask_user_questions(): # Select a partition # If we provide keys as options, it's better to convert them to list and sort before passing mountpoints_list = sorted(list(partition_mountpoints.keys())) - partition = archinstall.generic_select(mountpoints_list, - "Select a partition by number that you want to set a mount-point for (leave blank when done): ") + partition = archinstall.generic_select(mountpoints_list, "Select a partition by number that you want to set a mount-point for (leave blank when done): ") if not partition: if set(mountpoints_set) & {'/', '/boot'} == {'/', '/boot'}: break @@ -373,10 +372,7 @@ def perform_installation(mountpoint): if archinstall.arguments['profile'] and archinstall.arguments['profile'].has_post_install(): with archinstall.arguments['profile'].load_instructions(namespace=f"{archinstall.arguments['profile'].namespace}.py") as imported: if not imported._post_install(): - archinstall.log( - ' * Profile\'s post configuration requirements was not fulfilled.', - fg='red' - ) + archinstall.log(' * Profile\'s post configuration requirements was not fulfilled.', fg='red') exit(1) installation.log("For post-installation tips, see https://wiki.archlinux.org/index.php/Installation_guide#Post-installation", fg="yellow") diff --git a/profiles/desktop.py b/profiles/desktop.py index 67514a97..30bb9a6a 100644 --- a/profiles/desktop.py +++ b/profiles/desktop.py @@ -43,9 +43,7 @@ def _prep_function(*args, **kwargs): 'enlightenment', ] - desktop = archinstall.generic_select( - supported_desktops, 'Select your desired desktop environment: ', allow_empty_input=False, sort=True - ) + desktop = archinstall.generic_select(supported_desktops, 'Select your desired desktop environment: ', allow_empty_input=False, sort=True) # Temporarily store the selected desktop profile # in a session-safe location, since this module will get reloaded diff --git a/profiles/i3.py b/profiles/i3.py index 418749c0..c25003f9 100644 --- a/profiles/i3.py +++ b/profiles/i3.py @@ -18,9 +18,7 @@ def _prep_function(*args, **kwargs): """ supported_configurations = ['i3-wm', 'i3-gaps'] - desktop = archinstall.generic_select( - supported_configurations, 'Select your desired configuration: ', allow_empty_input=False, sort=True - ) + desktop = archinstall.generic_select(supported_configurations, 'Select your desired configuration: ', allow_empty_input=False, sort=True) # Temporarily store the selected desktop profile # in a session-safe location, since this module will get reloaded diff --git a/profiles/sway.py b/profiles/sway.py index 686fe868..9afc047d 100644 --- a/profiles/sway.py +++ b/profiles/sway.py @@ -35,9 +35,7 @@ def _prep_function(*args, **kwargs): # or through conventional import sway if __name__ == "sway": if "nvidia" in _gfx_driver_packages: - choice = input( - "The proprietary Nvidia driver is not supported by Sway. It is likely that you will run into issues. Continue anyways? [y/N] " - ) + choice = input("The proprietary Nvidia driver is not supported by Sway. It is likely that you will run into issues. Continue anyways? [y/N] ") if choice.lower() in ("n", ""): raise archinstall.lib.exceptions.HardwareIncompatibilityError("Sway does not support the proprietary nvidia drivers.") -- cgit v1.2.3-54-g00ecf From 4a34b3a9d032e62265f31444339ceba1ef0d7168 Mon Sep 17 00:00:00 2001 From: Dylan Taylor Date: Sat, 15 May 2021 15:08:50 -0400 Subject: Fix E712 --- archinstall/lib/user_interaction.py | 2 +- examples/guided.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'archinstall/lib/user_interaction.py') diff --git a/archinstall/lib/user_interaction.py b/archinstall/lib/user_interaction.py index f39eea1c..6289ef0a 100644 --- a/archinstall/lib/user_interaction.py +++ b/archinstall/lib/user_interaction.py @@ -363,7 +363,7 @@ def ask_for_a_timezone(): def ask_for_bootloader() -> str: bootloader = "systemd-bootctl" - if hasUEFI() == False: + if not hasUEFI(): bootloader = "grub-install" else: bootloader_choice = input("Would you like to use GRUB as a bootloader instead of systemd-boot? [y/N] ").lower() diff --git a/examples/guided.py b/examples/guided.py index f5da9265..a1a49448 100644 --- a/examples/guided.py +++ b/examples/guided.py @@ -321,7 +321,7 @@ def perform_installation(mountpoint): installation.set_hostname(archinstall.arguments['hostname']) if archinstall.arguments['mirror-region'].get("mirrors", None) is not None: installation.set_mirrors(archinstall.arguments['mirror-region']) # Set the mirrors in the installation medium - if archinstall.arguments["bootloader"] == "grub-install" and hasUEFI() == True: + if archinstall.arguments["bootloader"] == "grub-install" and hasUEFI(): installation.add_additional_packages("grub") installation.set_keyboard_language(archinstall.arguments['keyboard-language']) installation.add_bootloader(archinstall.arguments["bootloader"]) -- cgit v1.2.3-54-g00ecf From f7642786c9e169245fae52d8754b9d68f2507cb7 Mon Sep 17 00:00:00 2001 From: Dylan Taylor Date: Sat, 15 May 2021 15:18:46 -0400 Subject: Remove some redundant parenthesis --- archinstall/lib/disk.py | 10 +++++----- archinstall/lib/installer.py | 8 ++++---- archinstall/lib/mirrors.py | 8 ++++---- archinstall/lib/user_interaction.py | 6 +++--- docs/archinstall/general.rst | 2 +- examples/guided.py | 2 +- 6 files changed, 18 insertions(+), 18 deletions(-) (limited to 'archinstall/lib/user_interaction.py') diff --git a/archinstall/lib/disk.py b/archinstall/lib/disk.py index 85e8a402..00a6cae3 100644 --- a/archinstall/lib/disk.py +++ b/archinstall/lib/disk.py @@ -179,11 +179,11 @@ class Partition: if self.mountpoint != mount_information.get('target', None) and mountpoint: raise DiskError(f"{self} was given a mountpoint but the actual mountpoint differs: {mount_information.get('target', None)}") - if (target := mount_information.get('target', None)): + if target := mount_information.get('target', None): self.mountpoint = target if not self.filesystem and autodetect_filesystem: - if (fstype := mount_information.get('fstype', get_filesystem_type(path))): + if fstype := mount_information.get('fstype', get_filesystem_type(path)): self.filesystem = fstype if self.filesystem == 'crypto_LUKS': @@ -236,7 +236,7 @@ class Partition: @property def real_device(self): for blockdevice in json.loads(b''.join(sys_command('lsblk -J')).decode('UTF-8'))['blockdevices']: - if (parent := self.find_parent_of(blockdevice, os.path.basename(self.path))): + if parent := self.find_parent_of(blockdevice, os.path.basename(self.path)): return f"/dev/{parent}" # raise DiskError(f'Could not find appropriate parent for encrypted partition {self}') return self.path @@ -373,7 +373,7 @@ class Partition: return parent elif 'children' in data: for child in data['children']: - if (parent := self.find_parent_of(child, name, parent=data['name'])): + if parent := self.find_parent_of(child, name, parent=data['name']): return parent def mount(self, target, fs=None, options=''): @@ -403,7 +403,7 @@ class Partition: # Without to much research, it seams that low error codes are errors. # And above 8k is indicators such as "/dev/x not mounted.". # So anything in between 0 and 8k are errors (?). - if exit_code > 0 and exit_code < 8000: + if 0 < exit_code < 8000: raise err self.mountpoint = None diff --git a/archinstall/lib/installer.py b/archinstall/lib/installer.py index 139d9d33..cdf69273 100644 --- a/archinstall/lib/installer.py +++ b/archinstall/lib/installer.py @@ -107,7 +107,7 @@ class Installer: # Copy over the install log (if there is one) to the install medium if # at least the base has been strapped in, otherwise we won't have a filesystem/structure to copy to. if self.helper_flags.get('base-strapped', False) is True: - if (filename := storage.get('LOG_FILE', None)): + if filename := storage.get('LOG_FILE', None): absolute_logfile = os.path.join(storage.get('LOG_PATH', './'), filename) if not os.path.isdir(f"{self.target}/{os.path.dirname(absolute_logfile)}"): @@ -231,7 +231,7 @@ class Installer: def copy_ISO_network_config(self, enable_services=False): # Copy (if any) iwd password and config files if os.path.isdir('/var/lib/iwd/'): - if (psk_files := glob.glob('/var/lib/iwd/*.psk')): + if psk_files := glob.glob('/var/lib/iwd/*.psk'): if not os.path.isdir(f"{self.target}/var/lib/iwd"): os.makedirs(f"{self.target}/var/lib/iwd") @@ -257,7 +257,7 @@ class Installer: shutil.copy2(psk, f"{self.target}/var/lib/iwd/{os.path.basename(psk)}") # Copy (if any) systemd-networkd config files - if (netconfigurations := glob.glob('/etc/systemd/network/*')): + if netconfigurations := glob.glob('/etc/systemd/network/*'): if not os.path.isdir(f"{self.target}/etc/systemd/network/"): os.makedirs(f"{self.target}/etc/systemd/network/") @@ -426,7 +426,7 @@ class Installer: ## blkid doesn't trigger on loopback devices really well, ## so we'll use the old manual method until we get that sorted out. - if (real_device := self.detect_encryption(root_partition)): + if real_device := self.detect_encryption(root_partition): # TODO: We need to detect if the encrypted device is a whole disk encryption, # or simply a partition encryption. Right now we assume it's a partition (and we always have) log(f"Identifying root partition by PART-UUID on {real_device}: '{real_device.uuid}'.", level=logging.DEBUG) diff --git a/archinstall/lib/mirrors.py b/archinstall/lib/mirrors.py index e2630710..55ec5a98 100644 --- a/archinstall/lib/mirrors.py +++ b/archinstall/lib/mirrors.py @@ -15,9 +15,9 @@ def filter_mirrors_by_region(regions, destination='/etc/pacman.d/mirrorlist', tm region_list = [] for region in regions.split(','): region_list.append(f'country={region}') - o = b''.join(sys_command((f"/usr/bin/wget 'https://archlinux.org/mirrorlist/?{'&'.join(region_list)}&protocol=https&ip_version=4&ip_version=6&use_mirror_status=on' -O {tmp_dir}/mirrorlist"))) - o = b''.join(sys_command((f"/usr/bin/sed -i 's/#Server/Server/' {tmp_dir}/mirrorlist"))) - o = b''.join(sys_command((f"/usr/bin/mv {tmp_dir}/mirrorlist {destination}"))) + o = b''.join(sys_command(f"/usr/bin/wget 'https://archlinux.org/mirrorlist/?{'&'.join(region_list)}&protocol=https&ip_version=4&ip_version=6&use_mirror_status=on' -O {tmp_dir}/mirrorlist")) + o = b''.join(sys_command(f"/usr/bin/sed -i 's/#Server/Server/' {tmp_dir}/mirrorlist")) + o = b''.join(sys_command(f"/usr/bin/mv {tmp_dir}/mirrorlist {destination}")) return True @@ -71,7 +71,7 @@ def use_mirrors(regions: dict, destination='/etc/pacman.d/mirrorlist'): def re_rank_mirrors(top=10, *positionals, **kwargs): - if sys_command((f'/usr/bin/rankmirrors -n {top} /etc/pacman.d/mirrorlist > /etc/pacman.d/mirrorlist')).exit_code == 0: + if sys_command(f'/usr/bin/rankmirrors -n {top} /etc/pacman.d/mirrorlist > /etc/pacman.d/mirrorlist').exit_code == 0: return True return False diff --git a/archinstall/lib/user_interaction.py b/archinstall/lib/user_interaction.py index 6289ef0a..d490aeec 100644 --- a/archinstall/lib/user_interaction.py +++ b/archinstall/lib/user_interaction.py @@ -85,7 +85,7 @@ def do_countdown(): def get_password(prompt="Enter a password: "): - while (passwd := getpass.getpass(prompt)): + while passwd := getpass.getpass(prompt): passwd_verification = getpass.getpass(prompt='And one more time for verification: ') if passwd != passwd_verification: log(' * Passwords did not match * ', fg='red') @@ -246,7 +246,7 @@ class MiniCurses: return True # Move back to the current known position (BACKSPACE doesn't updated x-pos) sys.stdout.flush() - sys.stdout.write("\033[%dG" % (self._cursor_x)) + sys.stdout.write("\033[%dG" % self._cursor_x) sys.stdout.flush() # Write a blank space @@ -256,7 +256,7 @@ class MiniCurses: # And move back again sys.stdout.flush() - sys.stdout.write("\033[%dG" % (self._cursor_x)) + sys.stdout.write("\033[%dG" % self._cursor_x) sys.stdout.flush() self._cursor_x -= 1 diff --git a/docs/archinstall/general.rst b/docs/archinstall/general.rst index 7319d244..79406ea3 100644 --- a/docs/archinstall/general.rst +++ b/docs/archinstall/general.rst @@ -1,7 +1,7 @@ .. _archinstall.helpers: .. warning:: - All these helper functions are mostly, if not all, related to outside-installation-instructions. Meaning the calls will affect your current running system - and not touch your installed system. + All these helper functions are mostly, if not all, related to outside-installation-instructions. Meaning the calls will affect your current running system - and not touch your installed system. Profile related helpers ======================= diff --git a/examples/guided.py b/examples/guided.py index a1a49448..e9873ef0 100644 --- a/examples/guided.py +++ b/examples/guided.py @@ -104,7 +104,7 @@ def ask_user_questions(): if not old_password: old_password = input(f'Enter the old encryption password for {partition}: ') - if (autodetected_filesystem := partition.detect_inner_filesystem(old_password)): + if autodetected_filesystem := partition.detect_inner_filesystem(old_password): new_filesystem = autodetected_filesystem else: archinstall.log("Could not auto-detect the filesystem inside the encrypted volume.", fg='red') -- cgit v1.2.3-54-g00ecf From 1796bbb91885d8a11837b0d50c4b2093e2314352 Mon Sep 17 00:00:00 2001 From: Dylan Taylor Date: Sat, 15 May 2021 17:50:28 -0400 Subject: Perform refactoring to PEP 8 naming conventions --- archinstall/lib/disk.py | 54 ++++++++++++++-------------- archinstall/lib/general.py | 14 ++++---- archinstall/lib/hardware.py | 30 ++++++++-------- archinstall/lib/installer.py | 70 ++++++++++++++++++------------------- archinstall/lib/luks.py | 16 ++++----- archinstall/lib/mirrors.py | 8 ++--- archinstall/lib/networking.py | 6 ++-- archinstall/lib/output.py | 4 +-- archinstall/lib/services.py | 2 +- archinstall/lib/user_interaction.py | 8 ++--- docs/conf.py | 4 +-- examples/guided.py | 10 +++--- examples/minimal.py | 2 +- 13 files changed, 114 insertions(+), 114 deletions(-) (limited to 'archinstall/lib/user_interaction.py') diff --git a/archinstall/lib/disk.py b/archinstall/lib/disk.py index 410bb481..60c264e1 100644 --- a/archinstall/lib/disk.py +++ b/archinstall/lib/disk.py @@ -5,7 +5,7 @@ from collections import OrderedDict from typing import Optional from .general import * -from .hardware import hasUEFI +from .hardware import has_uefi from .output import log ROOT_DIR_PATTERN = re.compile('^.*?/devices') @@ -77,7 +77,7 @@ class BlockDevice: raise DiskError(f'Could not locate backplane info for "{self.path}"') if self.info['type'] == 'loop': - for drive in json.loads(b''.join(sys_command(['losetup', '--json'], hide_from_log=True)).decode('UTF_8'))['loopdevices']: + for drive in json.loads(b''.join(SysCommand(['losetup', '--json'], hide_from_log=True)).decode('UTF_8'))['loopdevices']: if not drive['name'] == self.path: continue @@ -99,10 +99,10 @@ class BlockDevice: @property def partitions(self): - o = b''.join(sys_command(['partprobe', self.path])) + o = b''.join(SysCommand(['partprobe', self.path])) # o = b''.join(sys_command('/usr/bin/lsblk -o name -J -b {dev}'.format(dev=dev))) - o = b''.join(sys_command(['/usr/bin/lsblk', '-J', self.path])) + o = b''.join(SysCommand(['/usr/bin/lsblk', '-J', self.path])) if b'not a block device' in o: raise DiskError(f'Can not read partitions off something that isn\'t a block device: {self.path}') @@ -116,7 +116,7 @@ class BlockDevice: for part in r['blockdevices'][0]['children']: part_id = part['name'][len(os.path.basename(self.path)):] if part_id not in self.part_cache: - ## TODO: Force over-write even if in cache? + # TODO: Force over-write even if in cache? if part_id not in self.part_cache or self.part_cache[part_id].size != part['size']: self.part_cache[part_id] = Partition(root_path + part_id, self, part_id=part_id, size=part['size']) @@ -139,7 +139,7 @@ class BlockDevice: This is more reliable than relying on /dev/disk/by-partuuid as it doesn't seam to be able to detect md raid partitions. """ - lsblk = b''.join(sys_command(f'lsblk -J -o+UUID {self.path}')) + lsblk = b''.join(SysCommand(f'lsblk -J -o+UUID {self.path}')) for partition in json.loads(lsblk.decode('UTF-8'))['blockdevices']: return partition.get('uuid', None) @@ -216,7 +216,7 @@ class Partition: This is more reliable than relying on /dev/disk/by-partuuid as it doesn't seam to be able to detect md raid partitions. """ - lsblk = b''.join(sys_command(f'lsblk -J -o+PARTUUID {self.path}')) + lsblk = b''.join(SysCommand(f'lsblk -J -o+PARTUUID {self.path}')) for partition in json.loads(lsblk.decode('UTF-8'))['blockdevices']: return partition.get('partuuid', None) return None @@ -236,7 +236,7 @@ class Partition: @property def real_device(self): - for blockdevice in json.loads(b''.join(sys_command('lsblk -J')).decode('UTF-8'))['blockdevices']: + for blockdevice in json.loads(b''.join(SysCommand('lsblk -J')).decode('UTF-8'))['blockdevices']: if parent := self.find_parent_of(blockdevice, os.path.basename(self.path)): return f"/dev/{parent}" # raise DiskError(f'Could not find appropriate parent for encrypted partition {self}') @@ -260,11 +260,11 @@ class Partition: temporary_path = pathlib.Path(temporary_mountpoint) temporary_path.mkdir(parents=True, exist_ok=True) - if (handle := sys_command(f'/usr/bin/mount {self.path} {temporary_mountpoint}')).exit_code != 0: + if (handle := SysCommand(f'/usr/bin/mount {self.path} {temporary_mountpoint}')).exit_code != 0: raise DiskError(f'Could not mount and check for content on {self.path} because: {b"".join(handle)}') files = len(glob.glob(f"{temporary_mountpoint}/*")) - sys_command(f'/usr/bin/umount {temporary_mountpoint}') + SysCommand(f'/usr/bin/umount {temporary_mountpoint}') temporary_path.rmdir() @@ -327,29 +327,29 @@ class Partition: log(f'Formatting {path} -> {filesystem}', level=logging.INFO) if filesystem == 'btrfs': - o = b''.join(sys_command(f'/usr/bin/mkfs.btrfs -f {path}')) + o = b''.join(SysCommand(f'/usr/bin/mkfs.btrfs -f {path}')) if b'UUID' not in o: raise DiskError(f'Could not format {path} with {filesystem} because: {o}') self.filesystem = 'btrfs' elif filesystem == 'vfat': - o = b''.join(sys_command(f'/usr/bin/mkfs.vfat -F32 {path}')) + o = b''.join(SysCommand(f'/usr/bin/mkfs.vfat -F32 {path}')) if (b'mkfs.fat' not in o and b'mkfs.vfat' not in o) or b'command not found' in o: raise DiskError(f'Could not format {path} with {filesystem} because: {o}') self.filesystem = 'vfat' elif filesystem == 'ext4': - if (handle := sys_command(f'/usr/bin/mkfs.ext4 -F {path}')).exit_code != 0: + if (handle := SysCommand(f'/usr/bin/mkfs.ext4 -F {path}')).exit_code != 0: raise DiskError(f'Could not format {path} with {filesystem} because: {b"".join(handle)}') self.filesystem = 'ext4' elif filesystem == 'xfs': - if (handle := sys_command(f'/usr/bin/mkfs.xfs -f {path}')).exit_code != 0: + if (handle := SysCommand(f'/usr/bin/mkfs.xfs -f {path}')).exit_code != 0: raise DiskError(f'Could not format {path} with {filesystem} because: {b"".join(handle)}') self.filesystem = 'xfs' elif filesystem == 'f2fs': - if (handle := sys_command(f'/usr/bin/mkfs.f2fs -f {path}')).exit_code != 0: + if (handle := SysCommand(f'/usr/bin/mkfs.f2fs -f {path}')).exit_code != 0: raise DiskError(f'Could not format {path} with {filesystem} because: {b"".join(handle)}') self.filesystem = 'f2fs' @@ -389,9 +389,9 @@ class Partition: try: if options: - sys_command(f'/usr/bin/mount -o {options} {self.path} {target}') + SysCommand(f'/usr/bin/mount -o {options} {self.path} {target}') else: - sys_command(f'/usr/bin/mount {self.path} {target}') + SysCommand(f'/usr/bin/mount {self.path} {target}') except SysCallError as err: raise err @@ -400,7 +400,7 @@ class Partition: def unmount(self): try: - exit_code = sys_command(f'/usr/bin/umount {self.path}').exit_code + exit_code = SysCommand(f'/usr/bin/umount {self.path}').exit_code except SysCallError as err: exit_code = err.exit_code @@ -450,7 +450,7 @@ class Filesystem: else: raise DiskError('Problem setting the partition format to GPT:', f'/usr/bin/parted -s {self.blockdevice.device} mklabel gpt') elif self.mode == MBR: - if sys_command(f'/usr/bin/parted -s {self.blockdevice.device} mklabel msdos').exit_code == 0: + if SysCommand(f'/usr/bin/parted -s {self.blockdevice.device} mklabel msdos').exit_code == 0: return self else: raise DiskError('Problem setting the partition format to GPT:', f'/usr/bin/parted -s {self.blockdevice.device} mklabel msdos') @@ -472,7 +472,7 @@ class Filesystem: # TODO: https://stackoverflow.com/questions/28157929/how-to-safely-handle-an-exception-inside-a-context-manager if len(args) >= 2 and args[1]: raise args[1] - b''.join(sys_command('sync')) + b''.join(SysCommand('sync')) return True def find_partition(self, mountpoint): @@ -481,7 +481,7 @@ class Filesystem: return partition def raw_parted(self, string: str): - x = sys_command(f'/usr/bin/parted -s {string}') + x = SysCommand(f'/usr/bin/parted -s {string}') return x def parted(self, string: str): @@ -495,7 +495,7 @@ class Filesystem: def use_entire_disk(self, root_filesystem_type='ext4'): log(f"Using and formatting the entire {self.blockdevice}.", level=logging.DEBUG) - if hasUEFI(): + if has_uefi(): self.add_partition('primary', start='1MiB', end='513MiB', format='fat32') self.set_name(0, 'EFI') self.set(0, 'boot on') @@ -572,7 +572,7 @@ def all_disks(*args, **kwargs): kwargs.setdefault("partitions", False) drives = OrderedDict() # for drive in json.loads(sys_command(f'losetup --json', *args, **lkwargs, hide_from_log=True)).decode('UTF_8')['loopdevices']: - for drive in json.loads(b''.join(sys_command('lsblk --json -l -n -o path,size,type,mountpoint,label,pkname,model', *args, **kwargs, hide_from_log=True)).decode('UTF_8'))['blockdevices']: + for drive in json.loads(b''.join(SysCommand('lsblk --json -l -n -o path,size,type,mountpoint,label,pkname,model', *args, **kwargs, hide_from_log=True)).decode('UTF_8'))['blockdevices']: if not kwargs['partitions'] and drive['type'] == 'part': continue @@ -605,7 +605,7 @@ def harddrive(size=None, model=None, fuzzy=False): def get_mount_info(path): try: - output = b''.join(sys_command(f'/usr/bin/findmnt --json {path}')) + output = b''.join(SysCommand(f'/usr/bin/findmnt --json {path}')) except SysCallError: return {} @@ -620,7 +620,7 @@ def get_mount_info(path): def get_partitions_in_use(mountpoint): try: - output = b''.join(sys_command(f'/usr/bin/findmnt --json -R {mountpoint}')) + output = b''.join(SysCommand(f'/usr/bin/findmnt --json -R {mountpoint}')) except SysCallError: return {} @@ -639,7 +639,7 @@ def get_partitions_in_use(mountpoint): def get_filesystem_type(path): try: - handle = sys_command(f"blkid -o value -s TYPE {path}") + handle = SysCommand(f"blkid -o value -s TYPE {path}") return b''.join(handle).strip().decode('UTF-8') except SysCallError: return None @@ -647,7 +647,7 @@ def get_filesystem_type(path): def disk_layouts(): try: - handle = sys_command("lsblk -f -o+TYPE,SIZE -J") + handle = SysCommand("lsblk -f -o+TYPE,SIZE -J") return json.loads(b''.join(handle).decode('UTF-8')) except SysCallError as err: log(f"Could not return disk layouts: {err}") diff --git a/archinstall/lib/general.py b/archinstall/lib/general.py index 7296b943..9fbf2654 100644 --- a/archinstall/lib/general.py +++ b/archinstall/lib/general.py @@ -42,7 +42,7 @@ def locate_binary(name): break # Don't recurse -class JSON_Encoder: +class JsonEncoder: def _encode(obj): if isinstance(obj, dict): # We'll need to iterate not just the value that default() usually gets passed @@ -54,12 +54,12 @@ class JSON_Encoder: # This, is a EXTREMELY ugly hack.. but it's the only quick way I can think of to trigger a encoding of sub-dictionaries. val = json.loads(json.dumps(val, cls=JSON)) else: - val = JSON_Encoder._encode(val) + val = JsonEncoder._encode(val) if type(key) == str and key[0] == '!': - copy[JSON_Encoder._encode(key)] = '******' + copy[JsonEncoder._encode(key)] = '******' else: - copy[JSON_Encoder._encode(key)] = val + copy[JsonEncoder._encode(key)] = val return copy elif hasattr(obj, 'json'): return obj.json() @@ -78,13 +78,13 @@ class JSON_Encoder: class JSON(json.JSONEncoder, json.JSONDecoder): def _encode(self, obj): - return JSON_Encoder._encode(obj) + return JsonEncoder._encode(obj) def encode(self, obj): return super(JSON, self).encode(self._encode(obj)) -class sys_command: +class SysCommand: """ Stolen from archinstall_gui """ @@ -336,4 +336,4 @@ def prerequisite_check(): def reboot(): - o = b''.join(sys_command("/usr/bin/reboot")) + o = b''.join(SysCommand("/usr/bin/reboot")) diff --git a/archinstall/lib/hardware.py b/archinstall/lib/hardware.py index d1723cde..f527b5da 100644 --- a/archinstall/lib/hardware.py +++ b/archinstall/lib/hardware.py @@ -3,7 +3,7 @@ import os import subprocess from typing import Optional -from .general import sys_command +from .general import SysCommand from .networking import list_interfaces, enrich_iface_types __packages__ = [ @@ -56,48 +56,48 @@ AVAILABLE_GFX_DRIVERS = { } -def hasWifi() -> bool: +def has_wifi() -> bool: return 'WIRELESS' in enrich_iface_types(list_interfaces().values()).values() -def hasAMDCPU() -> bool: +def has_amd_cpu() -> bool: if subprocess.check_output("lscpu | grep AMD", shell=True).strip().decode(): return True return False -def hasIntelCPU() -> bool: +def has_intel_cpu() -> bool: if subprocess.check_output("lscpu | grep Intel", shell=True).strip().decode(): return True return False -def hasUEFI() -> bool: +def has_uefi() -> bool: return os.path.isdir('/sys/firmware/efi') -def graphicsDevices() -> dict: +def graphics_devices() -> dict: cards = {} - for line in sys_command("lspci"): + for line in SysCommand("lspci"): if b' VGA ' in line: _, identifier = line.split(b': ', 1) cards[identifier.strip().lower().decode('UTF-8')] = line return cards -def hasNvidiaGraphics() -> bool: - return any('nvidia' in x for x in graphicsDevices()) +def has_nvidia_graphics() -> bool: + return any('nvidia' in x for x in graphics_devices()) -def hasAmdGraphics() -> bool: - return any('amd' in x for x in graphicsDevices()) +def has_amd_graphics() -> bool: + return any('amd' in x for x in graphics_devices()) -def hasIntelGraphics() -> bool: - return any('intel' in x for x in graphicsDevices()) +def has_intel_graphics() -> bool: + return any('intel' in x for x in graphics_devices()) -def cpuVendor() -> Optional[str]: +def cpu_vendor() -> Optional[str]: cpu_info = json.loads(subprocess.check_output("lscpu -J", shell=True).decode('utf-8'))['lscpu'] for info in cpu_info: if info.get('field', None): @@ -106,7 +106,7 @@ def cpuVendor() -> Optional[str]: return None -def isVM() -> bool: +def is_vm() -> bool: try: subprocess.check_call(["systemd-detect-virt"]) # systemd-detect-virt issues a non-zero exit code if it is not on a virtual machine return True diff --git a/archinstall/lib/installer.py b/archinstall/lib/installer.py index 9ddb8825..aa2ea920 100644 --- a/archinstall/lib/installer.py +++ b/archinstall/lib/installer.py @@ -135,8 +135,8 @@ class Installer: packages = packages[0] self.log(f'Installing packages: {packages}', level=logging.INFO) - if (sync_mirrors := sys_command('/usr/bin/pacman -Syy')).exit_code == 0: - if (pacstrap := sys_command(f'/usr/bin/pacstrap {self.target} {" ".join(packages)}', **kwargs)).exit_code == 0: + if (sync_mirrors := SysCommand('/usr/bin/pacman -Syy')).exit_code == 0: + if (pacstrap := SysCommand(f'/usr/bin/pacstrap {self.target} {" ".join(packages)}', **kwargs)).exit_code == 0: return True else: self.log(f'Could not strap in packages: {pacstrap.exit_code}', level=logging.INFO) @@ -149,7 +149,7 @@ class Installer: def genfstab(self, flags='-pU'): self.log(f"Updating {self.target}/etc/fstab", level=logging.INFO) - fstab = sys_command(f'/usr/bin/genfstab {flags} {self.target}').trace_log + fstab = SysCommand(f'/usr/bin/genfstab {flags} {self.target}').trace_log with open(f"{self.target}/etc/fstab", 'ab') as fstab_fh: fstab_fh.write(fstab) @@ -171,7 +171,7 @@ class Installer: with open(f'{self.target}/etc/locale.conf', 'w') as fh: fh.write(f'LANG={locale}.{encoding}\n') - return True if sys_command(f'/usr/bin/arch-chroot {self.target} locale-gen').exit_code == 0 else False + return True if SysCommand(f'/usr/bin/arch-chroot {self.target} locale-gen').exit_code == 0 else False def set_timezone(self, zone, *args, **kwargs): if not zone: @@ -181,7 +181,7 @@ class Installer: if (pathlib.Path("/usr") / "share" / "zoneinfo" / zone).exists(): (pathlib.Path(self.target) / "etc" / "localtime").unlink(missing_ok=True) - sys_command(f'/usr/bin/arch-chroot {self.target} ln -s /usr/share/zoneinfo/{zone} /etc/localtime') + SysCommand(f'/usr/bin/arch-chroot {self.target} ln -s /usr/share/zoneinfo/{zone} /etc/localtime') return True else: self.log( @@ -203,7 +203,7 @@ class Installer: raise ServiceException(f"Unable to start service {service}: {output}") def run_command(self, cmd, *args, **kwargs): - return sys_command(f'/usr/bin/arch-chroot {self.target} {cmd}') + return SysCommand(f'/usr/bin/arch-chroot {self.target} {cmd}') def arch_chroot(self, cmd, *args, **kwargs): if 'runas' in kwargs: @@ -232,7 +232,7 @@ class Installer: with open(f"{self.target}/etc/systemd/network/10-{nic}.network", "a") as netconf: netconf.write(str(conf)) - def copy_ISO_network_config(self, enable_services=False): + def copy_iso_network_config(self, enable_services=False): # Copy (if any) iwd password and config files if os.path.isdir('/var/lib/iwd/'): if psk_files := glob.glob('/var/lib/iwd/*.psk'): @@ -297,13 +297,13 @@ class Installer: mkinit.write(f"BINARIES=({' '.join(self.BINARIES)})\n") mkinit.write(f"FILES=({' '.join(self.FILES)})\n") mkinit.write(f"HOOKS=({' '.join(self.HOOKS)})\n") - sys_command(f'/usr/bin/arch-chroot {self.target} mkinitcpio {" ".join(flags)}') + SysCommand(f'/usr/bin/arch-chroot {self.target} mkinitcpio {" ".join(flags)}') def minimal_installation(self): - ## Add necessary packages if encrypting the drive - ## (encrypted partitions default to btrfs for now, so we need btrfs-progs) - ## TODO: Perhaps this should be living in the function which dictates - ## the partitioning. Leaving here for now. + # Add necessary packages if encrypting the drive + # (encrypted partitions default to btrfs for now, so we need btrfs-progs) + # TODO: Perhaps this should be living in the function which dictates + # the partitioning. Leaving here for now. for partition in self.partitions: if partition.filesystem == 'btrfs': @@ -325,11 +325,11 @@ class Installer: if 'encrypt' not in self.HOOKS: self.HOOKS.insert(self.HOOKS.index('filesystems'), 'encrypt') - if not hasUEFI(): + if not has_uefi(): self.base_packages.append('grub') - if not isVM(): - vendor = cpuVendor() + if not is_vm(): + vendor = cpu_vendor() if vendor == "AuthenticAMD": self.base_packages.append("amd-ucode") elif vendor == "GenuineIntel": @@ -343,7 +343,7 @@ class Installer: with open(f"{self.target}/etc/fstab", "a") as fstab: fstab.write("\ntmpfs /tmp tmpfs defaults,noatime,mode=1777 0 0\n") # Redundant \n at the start? who knows? - ## TODO: Support locale and timezone + # TODO: Support locale and timezone # os.remove(f'{self.target}/etc/localtime') # sys_command(f'/usr/bin/arch-chroot {self.target} ln -s /usr/share/zoneinfo/{localtime} /etc/localtime') # sys_command('/usr/bin/arch-chroot /mnt hwclock --hctosys --localtime') @@ -351,7 +351,7 @@ class Installer: self.set_locale('en_US') # TODO: Use python functions for this - sys_command(f'/usr/bin/arch-chroot {self.target} chmod 700 /root') + SysCommand(f'/usr/bin/arch-chroot {self.target} chmod 700 /root') self.mkinitcpio('-P') @@ -378,16 +378,16 @@ class Installer: if bootloader == 'systemd-bootctl': self.pacstrap('efibootmgr') - if not hasUEFI(): + if not has_uefi(): raise HardwareIncompatibilityError # TODO: Ideally we would want to check if another config # points towards the same disk and/or partition. # And in which case we should do some clean up. # Install the boot loader - if sys_command(f'/usr/bin/arch-chroot {self.target} bootctl --path=/boot install').exit_code != 0: + if SysCommand(f'/usr/bin/arch-chroot {self.target} bootctl --path=/boot install').exit_code != 0: # Fallback, try creating the boot loader without touching the EFI variables - sys_command(f'/usr/bin/arch-chroot {self.target} bootctl --no-variables --path=/boot install') + SysCommand(f'/usr/bin/arch-chroot {self.target} bootctl --no-variables --path=/boot install') # Modify or create a loader.conf if os.path.isfile(f'{self.target}/boot/loader/loader.conf'): @@ -409,8 +409,8 @@ class Installer: else: loader.write(f"{line}\n") - ## For some reason, blkid and /dev/disk/by-uuid are not getting along well. - ## And blkid is wrong in terms of LUKS. + # For some reason, blkid and /dev/disk/by-uuid are not getting along well. + # And blkid is wrong in terms of LUKS. # UUID = sys_command('blkid -s PARTUUID -o value {drive}{partition_2}'.format(**args)).decode('UTF-8').strip() # Setup the loader entry with open(f'{self.target}/boot/loader/entries/{self.init_time}.conf', 'w') as entry: @@ -418,8 +418,8 @@ class Installer: entry.write(f'# Created on: {self.init_time}\n') entry.write('title Arch Linux\n') entry.write('linux /vmlinuz-linux\n') - if not isVM(): - vendor = cpuVendor() + if not is_vm(): + vendor = cpu_vendor() if vendor == "AuthenticAMD": entry.write("initrd /amd-ucode.img\n") elif vendor == "GenuineIntel": @@ -427,8 +427,8 @@ class Installer: else: self.log("unknow cpu vendor, not adding ucode to systemd-boot config") entry.write('initrd /initramfs-linux.img\n') - ## blkid doesn't trigger on loopback devices really well, - ## so we'll use the old manual method until we get that sorted out. + # blkid doesn't trigger on loopback devices really well, + # so we'll use the old manual method until we get that sorted out. if real_device := self.detect_encryption(root_partition): # TODO: We need to detect if the encrypted device is a whole disk encryption, @@ -446,17 +446,17 @@ class Installer: elif bootloader == "grub-install": self.pacstrap('grub') - if hasUEFI(): + if has_uefi(): self.pacstrap('efibootmgr') - o = b''.join(sys_command(f'/usr/bin/arch-chroot {self.target} grub-install --target=x86_64-efi --efi-directory=/boot --bootloader-id=GRUB')) - sys_command('/usr/bin/arch-chroot /mnt grub-mkconfig -o /boot/grub/grub.cfg') + o = b''.join(SysCommand(f'/usr/bin/arch-chroot {self.target} grub-install --target=x86_64-efi --efi-directory=/boot --bootloader-id=GRUB')) + SysCommand('/usr/bin/arch-chroot /mnt grub-mkconfig -o /boot/grub/grub.cfg') return True else: root_device = subprocess.check_output(f'basename "$(readlink -f /sys/class/block/{root_partition.path.replace("/dev/", "")}/..)"', shell=True).decode().strip() if root_device == "block": root_device = f"{root_partition.path}" - o = b''.join(sys_command(f'/usr/bin/arch-chroot {self.target} grub-install --target=i386-pc /dev/{root_device}')) - sys_command('/usr/bin/arch-chroot /mnt grub-mkconfig -o /boot/grub/grub.cfg') + o = b''.join(SysCommand(f'/usr/bin/arch-chroot {self.target} grub-install --target=i386-pc /dev/{root_device}')) + SysCommand('/usr/bin/arch-chroot /mnt grub-mkconfig -o /boot/grub/grub.cfg') self.helper_flags['bootloader'] = bootloader return True else: @@ -484,13 +484,13 @@ class Installer: if groups is None: groups = [] self.log(f'Creating user {user}', level=logging.INFO) - o = b''.join(sys_command(f'/usr/bin/arch-chroot {self.target} useradd -m -G wheel {user}')) + o = b''.join(SysCommand(f'/usr/bin/arch-chroot {self.target} useradd -m -G wheel {user}')) if password: self.user_set_pw(user, password) if groups: for group in groups: - o = b''.join(sys_command(f'/usr/bin/arch-chroot {self.target} gpasswd -a {user} {group}')) + o = b''.join(SysCommand(f'/usr/bin/arch-chroot {self.target} gpasswd -a {user} {group}')) if sudo and self.enable_sudo(user): self.helper_flags['user'] = True @@ -502,13 +502,13 @@ class Installer: # This means the root account isn't locked/disabled with * in /etc/passwd self.helper_flags['user'] = True - o = b''.join(sys_command(f"/usr/bin/arch-chroot {self.target} sh -c \"echo '{user}:{password}' | chpasswd\"")) + o = b''.join(SysCommand(f"/usr/bin/arch-chroot {self.target} sh -c \"echo '{user}:{password}' | chpasswd\"")) pass def user_set_shell(self, user, shell): self.log(f'Setting shell for {user} to {shell}', level=logging.INFO) - o = b''.join(sys_command(f"/usr/bin/arch-chroot {self.target} sh -c \"chsh -s {shell} {user}\"")) + o = b''.join(SysCommand(f"/usr/bin/arch-chroot {self.target} sh -c \"chsh -s {shell} {user}\"")) pass def set_keyboard_language(self, language): diff --git a/archinstall/lib/luks.py b/archinstall/lib/luks.py index 1cb6777f..6fab9b94 100644 --- a/archinstall/lib/luks.py +++ b/archinstall/lib/luks.py @@ -78,7 +78,7 @@ class luks2: try: # Try to setup the crypt-device - cmd_handle = sys_command(cryptsetup_args) + cmd_handle = SysCommand(cryptsetup_args) except SysCallError as err: if err.exit_code == 256: log(f'{partition} is being used, trying to unmount and crypt-close the device and running one more attempt at encrypting the device.', level=logging.DEBUG) @@ -87,7 +87,7 @@ class luks2: # Get crypt-information about the device by doing a reverse lookup starting with the partition path # For instance: /dev/sda - devinfo = json.loads(b''.join(sys_command(f"lsblk --fs -J {partition.path}")).decode('UTF-8'))['blockdevices'][0] + devinfo = json.loads(b''.join(SysCommand(f"lsblk --fs -J {partition.path}")).decode('UTF-8'))['blockdevices'][0] # For each child (sub-partition/sub-device) if len(children := devinfo.get('children', [])): @@ -95,14 +95,14 @@ class luks2: # Unmount the child location if child_mountpoint := child.get('mountpoint', None): log(f'Unmounting {child_mountpoint}', level=logging.DEBUG) - sys_command(f"umount -R {child_mountpoint}") + SysCommand(f"umount -R {child_mountpoint}") # And close it if possible. log(f"Closing crypt device {child['name']}", level=logging.DEBUG) - sys_command(f"cryptsetup close {child['name']}") + SysCommand(f"cryptsetup close {child['name']}") # Then try again to set up the crypt-device - cmd_handle = sys_command(cryptsetup_args) + cmd_handle = SysCommand(cryptsetup_args) else: raise err @@ -128,7 +128,7 @@ class luks2: while pathlib.Path(partition.path).exists() is False and time.time() - wait_timer < 10: time.sleep(0.025) - sys_command(f'/usr/bin/cryptsetup open {partition.path} {mountpoint} --key-file {os.path.abspath(key_file)} --type luks2') + SysCommand(f'/usr/bin/cryptsetup open {partition.path} {mountpoint} --key-file {os.path.abspath(key_file)} --type luks2') if os.path.islink(f'/dev/mapper/{mountpoint}'): self.mapdev = f'/dev/mapper/{mountpoint}' unlocked_partition = Partition(self.mapdev, None, encrypted=True, filesystem=get_filesystem_type(self.mapdev), autodetect_filesystem=False) @@ -139,9 +139,9 @@ class luks2: if not mountpoint: mountpoint = self.mapdev - sys_command(f'/usr/bin/cryptsetup close {self.mapdev}') + SysCommand(f'/usr/bin/cryptsetup close {self.mapdev}') return os.path.islink(self.mapdev) is False def format(self, path): - if (handle := sys_command(f"/usr/bin/cryptsetup -q -v luksErase {path}")).exit_code != 0: + if (handle := SysCommand(f"/usr/bin/cryptsetup -q -v luksErase {path}")).exit_code != 0: raise DiskError(f'Could not format {path} with {self.filesystem} because: {b"".join(handle)}') diff --git a/archinstall/lib/mirrors.py b/archinstall/lib/mirrors.py index 55ec5a98..3215122f 100644 --- a/archinstall/lib/mirrors.py +++ b/archinstall/lib/mirrors.py @@ -15,9 +15,9 @@ def filter_mirrors_by_region(regions, destination='/etc/pacman.d/mirrorlist', tm region_list = [] for region in regions.split(','): region_list.append(f'country={region}') - o = b''.join(sys_command(f"/usr/bin/wget 'https://archlinux.org/mirrorlist/?{'&'.join(region_list)}&protocol=https&ip_version=4&ip_version=6&use_mirror_status=on' -O {tmp_dir}/mirrorlist")) - o = b''.join(sys_command(f"/usr/bin/sed -i 's/#Server/Server/' {tmp_dir}/mirrorlist")) - o = b''.join(sys_command(f"/usr/bin/mv {tmp_dir}/mirrorlist {destination}")) + o = b''.join(SysCommand(f"/usr/bin/wget 'https://archlinux.org/mirrorlist/?{'&'.join(region_list)}&protocol=https&ip_version=4&ip_version=6&use_mirror_status=on' -O {tmp_dir}/mirrorlist")) + o = b''.join(SysCommand(f"/usr/bin/sed -i 's/#Server/Server/' {tmp_dir}/mirrorlist")) + o = b''.join(SysCommand(f"/usr/bin/mv {tmp_dir}/mirrorlist {destination}")) return True @@ -71,7 +71,7 @@ def use_mirrors(regions: dict, destination='/etc/pacman.d/mirrorlist'): def re_rank_mirrors(top=10, *positionals, **kwargs): - if sys_command(f'/usr/bin/rankmirrors -n {top} /etc/pacman.d/mirrorlist > /etc/pacman.d/mirrorlist').exit_code == 0: + if SysCommand(f'/usr/bin/rankmirrors -n {top} /etc/pacman.d/mirrorlist > /etc/pacman.d/mirrorlist').exit_code == 0: return True return False diff --git a/archinstall/lib/networking.py b/archinstall/lib/networking.py index 1a5c403f..b0c4b569 100644 --- a/archinstall/lib/networking.py +++ b/archinstall/lib/networking.py @@ -5,7 +5,7 @@ import struct from collections import OrderedDict from .exceptions import * -from .general import sys_command +from .general import SysCommand from .storage import storage @@ -53,7 +53,7 @@ def wireless_scan(interface): if interfaces[interface] != 'WIRELESS': raise HardwareIncompatibilityError(f"Interface {interface} is not a wireless interface: {interfaces}") - sys_command(f"iwctl station {interface} scan") + SysCommand(f"iwctl station {interface} scan") if '_WIFI' not in storage: storage['_WIFI'] = {} @@ -72,5 +72,5 @@ def get_wireless_networks(interface): wireless_scan(interface) time.sleep(5) - for line in sys_command(f"iwctl station {interface} get-networks"): + for line in SysCommand(f"iwctl station {interface} get-networks"): print(line) diff --git a/archinstall/lib/output.py b/archinstall/lib/output.py index f69571c0..8bc6cacb 100644 --- a/archinstall/lib/output.py +++ b/archinstall/lib/output.py @@ -18,7 +18,7 @@ class LogLevels: Debug = 0b111 -class journald(dict): +class Journald(dict): @abc.abstractmethod def log(message, level=logging.DEBUG): try: @@ -161,7 +161,7 @@ def log(*args, **kwargs): return None try: - journald.log(string, level=kwargs.get('level', logging.INFO)) + Journald.log(string, level=kwargs.get('level', logging.INFO)) except ModuleNotFoundError: pass # Ignore writing to journald diff --git a/archinstall/lib/services.py b/archinstall/lib/services.py index 537a2f84..6f8f2a87 100644 --- a/archinstall/lib/services.py +++ b/archinstall/lib/services.py @@ -5,6 +5,6 @@ def service_state(service_name: str): if os.path.splitext(service_name)[1] != '.service': service_name += '.service' # Just to be safe - state = b''.join(sys_command(f'systemctl show --no-pager -p SubState --value {service_name}', environment_vars={'SYSTEMD_COLORS': '0'})) + state = b''.join(SysCommand(f'systemctl show --no-pager -p SubState --value {service_name}', environment_vars={'SYSTEMD_COLORS': '0'})) return state.strip().decode('UTF-8') diff --git a/archinstall/lib/user_interaction.py b/archinstall/lib/user_interaction.py index d490aeec..c640cb83 100644 --- a/archinstall/lib/user_interaction.py +++ b/archinstall/lib/user_interaction.py @@ -12,8 +12,8 @@ import time import tty from .exceptions import * -from .general import sys_command -from .hardware import AVAILABLE_GFX_DRIVERS, hasUEFI +from .general import SysCommand +from .hardware import AVAILABLE_GFX_DRIVERS, has_uefi from .locale_helpers import list_keyboard_languages, verify_keyboard_layout, search_keyboard_layout from .networking import list_interfaces from .output import log @@ -363,7 +363,7 @@ def ask_for_a_timezone(): def ask_for_bootloader() -> str: bootloader = "systemd-bootctl" - if not hasUEFI(): + if not has_uefi(): bootloader = "grub-install" else: bootloader_choice = input("Would you like to use GRUB as a bootloader instead of systemd-boot? [y/N] ").lower() @@ -705,7 +705,7 @@ def select_driver(options=AVAILABLE_GFX_DRIVERS): default_option = options["All open-source (default)"] if drivers: - lspci = sys_command('/usr/bin/lspci') + lspci = SysCommand('/usr/bin/lspci') for line in lspci.trace_log.split(b'\r\n'): if b' vga ' in line.lower(): if b'nvidia' in line.lower(): diff --git a/docs/conf.py b/docs/conf.py index 88a55e02..375ff434 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -8,8 +8,8 @@ sys.path.insert(0, os.path.abspath('..')) def process_docstring(app, what, name, obj, options, lines): spaces_pat = re.compile(r"( {8})") ll = [] - for l in lines: - ll.append(spaces_pat.sub(" ", l)) + for line in lines: + ll.append(spaces_pat.sub(" ", line)) lines[:] = ll diff --git a/examples/guided.py b/examples/guided.py index e9873ef0..177f4adb 100644 --- a/examples/guided.py +++ b/examples/guided.py @@ -3,7 +3,7 @@ import logging import time import archinstall -from archinstall.lib.hardware import hasUEFI +from archinstall.lib.hardware import has_uefi if archinstall.arguments.get('help'): print("See `man archinstall` for help.") @@ -257,7 +257,7 @@ def perform_installation_steps(): Once that's done, we'll hand over to perform_installation() """ mode = archinstall.GPT - if hasUEFI() is False: + if has_uefi() is False: mode = archinstall.MBR with archinstall.Filesystem(archinstall.arguments['harddrive'], mode) as fs: @@ -294,7 +294,7 @@ def perform_installation_steps(): else: fs.find_partition('/').mount('/mnt') - if hasUEFI(): + if has_uefi(): fs.find_partition('/boot').mount('/mnt/boot') perform_installation('/mnt') @@ -321,7 +321,7 @@ def perform_installation(mountpoint): installation.set_hostname(archinstall.arguments['hostname']) if archinstall.arguments['mirror-region'].get("mirrors", None) is not None: installation.set_mirrors(archinstall.arguments['mirror-region']) # Set the mirrors in the installation medium - if archinstall.arguments["bootloader"] == "grub-install" and hasUEFI(): + if archinstall.arguments["bootloader"] == "grub-install" and has_uefi(): installation.add_additional_packages("grub") installation.set_keyboard_language(archinstall.arguments['keyboard-language']) installation.add_bootloader(archinstall.arguments["bootloader"]) @@ -329,7 +329,7 @@ def perform_installation(mountpoint): # If user selected to copy the current ISO network configuration # Perform a copy of the config if archinstall.arguments.get('nic', {}) == 'Copy ISO network configuration to installation': - installation.copy_ISO_network_config(enable_services=True) # Sources the ISO network configuration to the install medium. + installation.copy_iso_network_config(enable_services=True) # Sources the ISO network configuration to the install medium. elif archinstall.arguments.get('nic', {}).get('NetworkManager', False): installation.add_additional_packages("networkmanager") installation.enable_service('NetworkManager.service') diff --git a/examples/minimal.py b/examples/minimal.py index 308a5e30..5da6f0c1 100644 --- a/examples/minimal.py +++ b/examples/minimal.py @@ -23,7 +23,7 @@ def install_on(mountpoint): # Optionally enable networking: if archinstall.arguments.get('network', None): - installation.copy_ISO_network_config(enable_services=True) + installation.copy_iso_network_config(enable_services=True) installation.add_additional_packages(['nano', 'wget', 'git']) installation.install_profile('minimal') -- cgit v1.2.3-54-g00ecf From da0da729c5f3ef0aaf832229901dda221de654ab Mon Sep 17 00:00:00 2001 From: Dylan Taylor Date: Tue, 18 May 2021 09:10:24 -0400 Subject: Proofreading: Fix misc. spelling and grammar issues --- CONTRIBUTING.md | 29 +++++++--------- README.md | 66 ++++++++++++++++++++----------------- archinstall/lib/mirrors.py | 4 +-- archinstall/lib/networking.py | 2 +- archinstall/lib/output.py | 4 +-- archinstall/lib/user_interaction.py | 4 +-- docs/examples/python.rst | 4 +-- docs/installing/guided.rst | 2 +- docs/pull_request_template.md | 4 +-- profiles/52-54-00-12-34-56.py | 2 +- profiles/i3.py | 2 +- 11 files changed, 62 insertions(+), 61 deletions(-) (limited to 'archinstall/lib/user_interaction.py') diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 1d490a44..a92cff91 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,32 +1,28 @@ # Contributing to archinstall -Any contributions through pull requests are welcome as this project aims to be a community based project to ease some -Arch Linux installation steps. Bear in mind that in the future this repo might be transferred to the -official [GitLab repo under Arch Linux](http://gitlab.archlinux.org/archlinux/) *(if GitLab becomes open to the general -public)*. +Any contributions through pull requests are welcome as this project aims to be a community based project to ease some Arch Linux installation steps. +Bear in mind that in the future this repo might be transferred to the official [GitLab repo under Arch Linux](http://gitlab.archlinux.org/archlinux/) + *(if GitLab becomes open to the general public)*. -Therefore guidelines and style changes to the code might come into affect as well as guidelines surrounding bug -reporting and discussions. +Therefore, guidelines and style changes to the code might come into effect as well as guidelines surrounding bug reporting and discussions. ## Branches -`master` is currently the default branch, and that's where all future feature work is being done, this means -that `master` is a living entity and will most likely never be in a fully stable state. For stable releases, please see -the tagged commits. +`master` is currently the default branch, and that's where all future feature work is being done, this means that `master` is a living entity and will most likely never be in a fully stable state. +For stable releases, please see the tagged commits. -Patch releases will be done against their own branches, branched from stable tagged releases and will be named according -to the version it will become on release *(Patches to `v2.1.4` will be done on branch `v2.1.5` for instance)*. +Patch releases will be done against their own branches, branched from stable tagged releases and will be named according to the version it will become on release. + *(Patches to `v2.1.4` will be done on branch `v2.1.5` for instance)*. ## Discussions -Currently, questions, bugs and suggestions should be reported -through [GitHub issue tracker](https://github.com/archlinux/archinstall/issues).
+Currently, questions, bugs and suggestions should be reported through [GitHub issue tracker](https://github.com/archlinux/archinstall/issues).
For less formal discussions there are also a [archinstall Discord server](https://discord.gg/cqXU88y). ## Coding convention -Archinstall's goal is to follow [PEP8](https://www.python.org/dev/peps/pep-0008/) as best as it can with some minor -exceptions.
+ArchInstall's goal is to follow [PEP8](https://www.python.org/dev/peps/pep-0008/) as best as it can with some minor exceptions.
+ The exceptions to PEP8 are: * Archinstall uses [tabs instead of spaces](https://www.python.org/dev/peps/pep-0008/#tabs-or-spaces) simply to make it @@ -46,8 +42,7 @@ There might therefore be older code which does not follow the coding convention ## Submitting Changes -Archinstall uses Github's pull-request workflow and all contributions in terms of code should be done through pull -requests.
+Archinstall uses GitHub's pull-request workflow and all contributions in terms of code should be done through pull requests.
Anyone interested in archinstall may review your code. One of the core developers will merge your pull request when they think it is ready. For every pull request, we aim to promptly either merge it or say why it is not yet ready; if you go diff --git a/README.md b/README.md index 63c8cefe..139dbd48 100644 --- a/README.md +++ b/README.md @@ -2,16 +2,16 @@ drawing # Arch Installer + Just another guided/automated [Arch Linux](https://wiki.archlinux.org/index.php/Arch_Linux) installer with a twist. The installer also doubles as a python library to install Arch Linux and manage services, packages and other things inside the installed system *(Usually from a live medium)*. - * archinstall [discord](https://discord.gg/cqXU88y) server - * archinstall [matrix.org](https://app.element.io/#/room/#archinstall:matrix.org) channel - * archinstall [#archinstall@freenode (IRC)](irc://#archinstall@FreeNode) - * archinstall [documentation](https://python-archinstall.readthedocs.io/en/latest/index.html) - +* archinstall [discord](https://discord.gg/cqXU88y) server +* archinstall [matrix.org](https://app.element.io/#/room/#archinstall:matrix.org) channel +* archinstall [#archinstall@freenode (IRC)](irc://#archinstall@FreeNode) +* archinstall [documentation](https://python-archinstall.readthedocs.io/en/latest/index.html) # Installation & Usage @@ -22,24 +22,27 @@ Or use `pip install --upgrade archinstall` to use as a library. ## Running the [guided](examples/guided.py) installer -Assuming you are on a Arch Linux live-ISO and booted into EFI mode. +Assuming you are on an Arch Linux live-ISO and booted into EFI mode. # python -m archinstall guided # Help? -Submit an issue here on Github, or submit a post in the discord help channel.
+Submit an issue here on GitHub, or submit a post in the discord help channel.
When doing so, attach the `/var/log/archinstall/install.log` to the issue ticket. This helps us help you! # Mission Statement -Archinstall promises to ship a [guided installer](https://github.com/archlinux/archinstall/blob/master/examples/guided.py) that follows the [Arch Principles](https://wiki.archlinux.org/index.php/Arch_Linux#Principles) as well as a library to manage services, packages and other Arch Linux aspects. +Archinstall promises to ship a [guided installer](https://github.com/archlinux/archinstall/blob/master/examples/guided.py) that follows +the [Arch Principles](https://wiki.archlinux.org/index.php/Arch_Linux#Principles) as well as a library to manage services, packages and other Arch Linux aspects. -The guided installer will provide user friendly options along the way, but the keyword here is options, they are optional and will never be forced upon anyone. The guided installer itself is also optional to use if so desired and not forced upon anyone. +The guided installer will provide user-friendly options along the way, but the keyword here is options, they are optional and will never be forced upon anyone. +The guided installer itself is also optional to use if so desired and not forced upon anyone. --- -Archinstall has one fundamental function which is to be a flexible library to manage services, packages and other aspects inside the installed system. This library is in turn used by the provided guided installer but is also for anyone who wants to script their own installations. +Archinstall has one fundamental function which is to be a flexible library to manage services, packages and other aspects inside the installed system. +This library is in turn used by the provided guided installer but is also for anyone who wants to script their own installations. Therefore, Archinstall will try its best to not introduce any breaking changes except for major releases which may break backwards compability after notifying about such changes. @@ -47,7 +50,7 @@ Therefore, Archinstall will try its best to not introduce any breaking changes e You could just copy [guided.py](examples/guided.py) as a starting point. -But assuming you're building your own ISO and want to create an automated install process, or you want to install virtual machines on to local disk images.
+However, assuming you're building your own ISO and want to create an automated installation process, or you want to install virtual machines on to local disk images.
This is probably what you'll need, a [minimal example](examples/minimal.py) of how to install using archinstall as a Python library. ```python @@ -99,33 +102,36 @@ with archinstall.Installer('/mnt') as installation: This installer will perform the following: - * Prompt the user to select a disk and disk-password - * Proceed to wipe the selected disk with a `GPT` partition table on a UEFI system and MBR on a bios system. - * Sets up a default 100% used disk with encryption. - * Installs a basic instance of Arch Linux *(base base-devel linux linux-firmware btrfs-progs efibootmgr)* - * Installs and configures a bootloader to partition 0 on uefi. on bios it sets the root to partition 0. - * Install additional packages *(nano, wget, git)* +* Prompt the user to select a disk and disk-password +* Proceed to wipe the selected disk with a `GPT` partition table on a UEFI system and MBR on a BIOS system. +* Sets up a default 100% used disk with encryption. +* Installs a basic instance of Arch Linux *(base base-devel linux linux-firmware btrfs-progs efibootmgr)* +* Installs and configures a bootloader to partition 0 on uefi. On BIOS, it sets the root to partition 0. +* Install additional packages *(nano, wget, git)* > **Creating your own ISO with this script on it:** Follow [ArchISO](https://wiki.archlinux.org/index.php/archiso)'s guide on how to create your own ISO or use a pre-built [guided ISO](https://hvornum.se/archiso/) to skip the python installation step, or to create auto-installing ISO templates. Further down are examples and cheat sheets on how to create different live ISO's. ## Unattended installation based on MAC address -Archinstall comes with a [unattended](examples/unattended.py) example which will look for a matching profile for the machine it is being run on, based on any local MAC address. For instance, if the machine that [unattended](examples/unattended.py) is run on has the MAC address `52:54:00:12:34:56` it will look for a profile called [profiles/52-54-00-12-34-56.py](profiles/52-54-00-12-34-56.py). If it's found, the unattended installation will commence and source that profile as it's installation proceedure. +Archinstall comes with a [unattended](examples/unattended.py) example which will look for a matching profile for the machine it is being run on, based on any local MAC address. +For instance, if the machine that [unattended](examples/unattended.py) is run on has the MAC address `52:54:00:12:34:56` it will look for a profile called [profiles/52-54-00-12-34-56.py](profiles/52-54-00-12-34-56.py). +If it's found, the unattended installation will commence and source that profile as it's installation procedure. # Testing ## Using a Live ISO Image -If you want to test a commit, branch or bleeding edge release from the repository using the vanilla Arch Live ISO image, you can replace the version of archinstall with a new version and run that with the steps described below. +If you want to test a commit, branch or bleeding edge release from the repository using the vanilla Arch Live ISO image, +you can replace the version of archinstall with a new version and run that with the steps described below: - 1. You need a working network connection - 2. Install the build requirements with `pacman -Sy; pacman -S git python-pip` - *(note that this may or may not work depending on your RAM and current state of the squashfs maximum filesystem free space)* - 3. Uninstall the previous version of archinstall with `pip uninstall archinstall` - 4. Now clone the latest repository with `git clone https://github.com/archlinux/archinstall` - 5. Enter the repository with `cd archinstall` - *At this stage, you can choose to check out a feature branch for instance with `git checkout torxed-v2.2.0`* - 6. Build the project and install it using `python setup.py install` +1. You need a working network connection +2. Install the build requirements with `pacman -Sy; pacman -S git python-pip` + *(note that this may or may not work depending on your RAM and current state of the squashfs maximum filesystem free space)* +3. Uninstall the previous version of archinstall with `pip uninstall archinstall` +4. Now clone the latest repository with `git clone https://github.com/archlinux/archinstall` +5. Enter the repository with `cd archinstall` + *At this stage, you can choose to check out a feature branch for instance with `git checkout torxed-v2.2.0`* +6. Build the project and install it using `python setup.py install` After this, running archinstall with `python -m archinstall` will run against whatever branch you chose in step 5. @@ -141,9 +147,9 @@ This can be done by installing `pacman -S arch-install-scripts util-linux` local # python -m archinstall guided # qemu-system-x86_64 -enable-kvm -machine q35,accel=kvm -device intel-iommu -cpu host -m 4096 -boot order=d -drive file=./testimage.img,format=raw -drive if=pflash,format=raw,readonly,file=/usr/share/ovmf/x64/OVMF_CODE.fd -drive if=pflash,format=raw,readonly,file=/usr/share/ovmf/x64/OVMF_VARS.fd -This will create a *5GB* `testimage.img` and create a loop device which we can use to format and install to.
-`archinstall` is installed and executed in [guided mode](#docs-todo). Once the installation is complete,
-~~you can use qemu/kvm to boot the test media.~~ *(You'd actually need to do some EFI magic in order to point the EFI vars to the partition 0 in the test medium so this won't work entirely out of the box, but gives you a general idea of what we're going for here)* +This will create a *5 GB* `testimage.img` and create a loop device which we can use to format and install to.
+`archinstall` is installed and executed in [guided mode](#docs-todo). Once the installation is complete, ~~you can use qemu/kvm to boot the test media.~~
+*(You'd actually need to do some EFI magic in order to point the EFI vars to the partition 0 in the test medium, so this won't work entirely out of the box, but that gives you a general idea of what we're going for here)* There's also a [Building and Testing](https://github.com/archlinux/archinstall/wiki/Building-and-Testing) guide.
It will go through everything from packaging, building and running *(with qemu)* the installer against a dev branch. diff --git a/archinstall/lib/mirrors.py b/archinstall/lib/mirrors.py index 5050df7d..4ef4fa49 100644 --- a/archinstall/lib/mirrors.py +++ b/archinstall/lib/mirrors.py @@ -10,8 +10,8 @@ def filter_mirrors_by_region(regions, destination='/etc/pacman.d/mirrorlist', tm This function will change the active mirrors on the live medium by filtering which regions are active based on `regions`. - :param region: A series of country codes separated by `,`. For instance `SE,US` for sweden and United States. - :type region: str + :param regions: A series of country codes separated by `,`. For instance `SE,US` for sweden and United States. + :type regions: str """ region_list = [] for region in regions.split(','): diff --git a/archinstall/lib/networking.py b/archinstall/lib/networking.py index 8a2f0edd..dbd510dd 100644 --- a/archinstall/lib/networking.py +++ b/archinstall/lib/networking.py @@ -31,7 +31,7 @@ def check_mirror_reachable(): check = SysCommand("pacman -Sy") return check.exit_code == 0 except: - return False + return False def enrich_iface_types(interfaces: dict): diff --git a/archinstall/lib/output.py b/archinstall/lib/output.py index 8bc6cacb..20b0df8d 100644 --- a/archinstall/lib/output.py +++ b/archinstall/lib/output.py @@ -26,7 +26,7 @@ class Journald(dict): except ModuleNotFoundError: return False - # For backwards compability, convert old style log-levels + # For backwards compatibility, convert old style log-levels # to logging levels (and warn about deprecated usage) # There's some code re-usage here but that should be fine. # TODO: Remove these in a few versions: @@ -135,7 +135,7 @@ def log(*args, **kwargs): # Unless the level is higher than we've decided to output interactively. # (Remember, log files still get *ALL* the output despite level restrictions) if 'level' in kwargs: - # For backwards compability, convert old style log-levels + # For backwards compatibility, convert old style log-levels # to logging levels (and warn about deprecated usage) # There's some code re-usage here but that should be fine. # TODO: Remove these in a few versions: diff --git a/archinstall/lib/user_interaction.py b/archinstall/lib/user_interaction.py index c640cb83..5f849607 100644 --- a/archinstall/lib/user_interaction.py +++ b/archinstall/lib/user_interaction.py @@ -242,7 +242,7 @@ class MiniCurses: if (mapped_char := mapper.get(char, None)) == 'BACKSPACE': if self._cursor_x <= self.input_pos: - # Don't backspace futher back than the cursor start position during input + # Don't backspace further back than the cursor start position during input return True # Move back to the current known position (BACKSPACE doesn't updated x-pos) sys.stdout.flush() @@ -694,7 +694,7 @@ def select_mirror_regions(mirrors, show_top_mirrors=True): def select_driver(options=AVAILABLE_GFX_DRIVERS): """ - Some what convoluted function, which's job is simple. + Some what convoluted function, whose job is simple. Select a graphics driver from a pre-defined set of popular options. (The template xorg is for beginner users, not advanced, and should diff --git a/docs/examples/python.rst b/docs/examples/python.rst index f6388738..342ec5c7 100644 --- a/docs/examples/python.rst +++ b/docs/examples/python.rst @@ -27,7 +27,7 @@ Lets create a `test_installer` - installer as an example. This is assuming that We begin by creating `./archinstall/examples/test_installer.py`. The placement here is important later. This script can now already be called using `python -m archinstall test_installer` after a successful installation of the library itself. -But the script won't do much. So we'll do something simple like list all the harddrives as an example. +But the script won't do much. So we'll do something simple like list all the hard drives as an example. To do this, we'll begin by importing `archinstall` in our `./archinstall/examples/test_installer.py` and call some functions. @@ -56,4 +56,4 @@ This should now print all available drives on your system. .. note:: - This should work on any system, not just Arch Linux based ones. But note that other functions in the library relies heavily on Arch Linux based commands to execute the installation steps. Such as `arch-chroot`. \ No newline at end of file + This should work on any system, not just Arch Linux based ones. But note that other functions in the library relies heavily on Arch Linux based commands to execute the installation steps. Such as `arch-chroot`. diff --git a/docs/installing/guided.rst b/docs/installing/guided.rst index 8699ae62..88b4e480 100644 --- a/docs/installing/guided.rst +++ b/docs/installing/guided.rst @@ -3,7 +3,7 @@ Guided installation This is the default scripted installation you'll encounter on the official Arch Linux Archinstall package as well as the unofficial ISO found on `https://archlinux.life `_. It will guide your through a very basic installation of Arch Linux. -The installer has two pre-requisits: +The installer has two pre-requisites: * A Physical or Virtual machine to install on * An active internet connection prior to running archinstall diff --git a/docs/pull_request_template.md b/docs/pull_request_template.md index 1cbcf76a..5e6daf51 100644 --- a/docs/pull_request_template.md +++ b/docs/pull_request_template.md @@ -3,11 +3,11 @@ # New features *(v2.2.0)* All future work towards *`v2.2.0`* is done against `master` now.
-Any patch work to existing verions will have to create a new branch against the tagged versions. +Any patch work to existing versions will have to create a new branch against the tagged versions. # Describe your PR -If the changes has been discussed in an Issue, please tag it so we can backtrace from the Issue later on.
+If the changes has been discussed in an Issue, please tag it so that we can backtrace from the issue later on.
If the PR is larger than ~20 lines, please describe it here unless described in an issue. # Testing diff --git a/profiles/52-54-00-12-34-56.py b/profiles/52-54-00-12-34-56.py index e1361073..68e15720 100644 --- a/profiles/52-54-00-12-34-56.py +++ b/profiles/52-54-00-12-34-56.py @@ -23,7 +23,7 @@ with archinstall.Filesystem(harddrive) as fs: fs.use_entire_disk('luks2') if harddrive.partition[1].size == '512M': - raise OSError('Trying to encrypt the boot partition for petes sake..') + raise OSError('Trying to encrypt the boot partition for Pete's sake..') harddrive.partition[0].format('fat32') with archinstall.luks2(harddrive.partition[1], 'luksloop', disk_password) as unlocked_device: diff --git a/profiles/i3.py b/profiles/i3.py index b26745a1..39977b28 100644 --- a/profiles/i3.py +++ b/profiles/i3.py @@ -62,7 +62,7 @@ if __name__ == 'i3': # Install dependency profiles archinstall.storage['installation_session'].install_profile('xorg') - # gaps is installed by deafult so we are overriding it here with lightdm + # gaps is installed by default so we are overriding it here with lightdm archinstall.storage['installation_session'].add_additional_packages(__packages__[4:]) # Auto start lightdm for all users -- cgit v1.2.3-54-g00ecf From 49e6cbdc545402e066bdc2daf6054abf6c1bf977 Mon Sep 17 00:00:00 2001 From: Anton Hvornum Date: Wed, 19 May 2021 14:45:13 +0000 Subject: Reworking SysCommand & Moving to localectl for locale related activities * Moving to `localectl` rather than local file manipulation *(both for listing locales and setting them)*. * Swapped `loadkeys` for localectl. * Renamed `main` to `maim` in awesome profile. * Created `archinstall.Boot()` which spawns a `systemd-nspawn` container against the installation target. * Exposing systemd.py's internals to archinstall global scope. * Re-worked `SysCommand` completely, it's now a wrapper for `SysCommandWorker` which supports interacting with the process in a different way. `SysCommand` should behave just like the old one, for backwards compatibility reasons. This fixes #68 and #69. * `SysCommand()` now has a `.decode()` function that defaults to `UTF-8`. * Adding back peak_output=True to pacstrap. Co-authored-by: Anton Hvornum Co-authored-by: Dylan Taylor --- .gitignore | 5 +- archinstall/__init__.py | 1 + archinstall/lib/disk.py | 29 ++- archinstall/lib/general.py | 398 +++++++++++++++++++----------------- archinstall/lib/installer.py | 53 ++++- archinstall/lib/locale_helpers.py | 46 +++-- archinstall/lib/networking.py | 13 +- archinstall/lib/output.py | 2 +- archinstall/lib/systemd.py | 82 ++++++++ archinstall/lib/user_interaction.py | 12 +- examples/guided.py | 19 +- profiles/awesome.py | 2 +- 12 files changed, 417 insertions(+), 245 deletions(-) (limited to 'archinstall/lib/user_interaction.py') diff --git a/.gitignore b/.gitignore index d4ee5091..b357b543 100644 --- a/.gitignore +++ b/.gitignore @@ -20,9 +20,10 @@ SAFETY_LOCK **/**.network **/**.target **/**.qcow2 -**/test.py +/test*.py **/archiso /guided.py /install.log venv -.idea/** \ No newline at end of file +.idea/** +**/install.log \ No newline at end of file diff --git a/archinstall/__init__.py b/archinstall/__init__.py index 18c83a31..075b6f50 100644 --- a/archinstall/__init__.py +++ b/archinstall/__init__.py @@ -13,6 +13,7 @@ from .lib.packages import * from .lib.profiles import * from .lib.services import * from .lib.storage import * +from .lib.systemd import * from .lib.user_interaction import * __version__ = "2.2.0.dev1" diff --git a/archinstall/lib/disk.py b/archinstall/lib/disk.py index f8703ae3..44f2742b 100644 --- a/archinstall/lib/disk.py +++ b/archinstall/lib/disk.py @@ -1,6 +1,7 @@ import glob import pathlib import re +import time from collections import OrderedDict from typing import Optional @@ -77,7 +78,7 @@ class BlockDevice: raise DiskError(f'Could not locate backplane info for "{self.path}"') if self.info['type'] == 'loop': - for drive in json.loads(b''.join(SysCommand(['losetup', '--json'], hide_from_log=True)).decode('UTF_8'))['loopdevices']: + for drive in json.loads(b''.join(SysCommand(['losetup', '--json'])).decode('UTF_8'))['loopdevices']: if not drive['name'] == self.path: continue @@ -264,7 +265,9 @@ class Partition: raise DiskError(f'Could not mount and check for content on {self.path} because: {b"".join(handle)}') files = len(glob.glob(f"{temporary_mountpoint}/*")) - SysCommand(f'/usr/bin/umount {temporary_mountpoint}') + iterations = 0 + while SysCommand(f"/usr/bin/umount -R {temporary_mountpoint}").exit_code != 0 and (iterations := iterations+1) < 10: + time.sleep(1) temporary_path.rmdir() @@ -425,7 +428,7 @@ class Partition: """ try: self.format(self.filesystem, '/dev/null', log_formatting=False, allow_formatting=True) - except SysCallError: + except (SysCallError, DiskError): pass # We supported it, but /dev/null is not formatable as expected so the mkfs call exited with an error code except UnknownFilesystemFormat as err: raise err @@ -572,7 +575,7 @@ def all_disks(*args, **kwargs): kwargs.setdefault("partitions", False) drives = OrderedDict() # for drive in json.loads(sys_command(f'losetup --json', *args, **lkwargs, hide_from_log=True)).decode('UTF_8')['loopdevices']: - for drive in json.loads(b''.join(SysCommand('lsblk --json -l -n -o path,size,type,mountpoint,label,pkname,model', *args, **kwargs, hide_from_log=True)).decode('UTF_8'))['blockdevices']: + for drive in json.loads(b''.join(SysCommand('lsblk --json -l -n -o path,size,type,mountpoint,label,pkname,model')).decode('UTF_8'))['blockdevices']: if not kwargs['partitions'] and drive['type'] == 'part': continue @@ -603,13 +606,17 @@ def harddrive(size=None, model=None, fuzzy=False): return collection[drive] -def get_mount_info(path): +def get_mount_info(path) -> dict: try: - output = b''.join(SysCommand(f'/usr/bin/findmnt --json {path}')) + output = SysCommand(f'/usr/bin/findmnt --json {path}') except SysCallError: return {} output = output.decode('UTF-8') + + if not output: + return {} + output = json.loads(output) if 'filesystems' in output: if len(output['filesystems']) > 1: @@ -618,15 +625,19 @@ def get_mount_info(path): return output['filesystems'][0] -def get_partitions_in_use(mountpoint): +def get_partitions_in_use(mountpoint) -> list: try: - output = b''.join(SysCommand(f'/usr/bin/findmnt --json -R {mountpoint}')) + output = SysCommand(f'/usr/bin/findmnt --json -R {mountpoint}') except SysCallError: - return {} + return [] mounts = [] output = output.decode('UTF-8') + + if not output: + return [] + output = json.loads(output) for target in output.get('filesystems', []): mounts.append(Partition(target['source'], None, filesystem=target.get('fstype', None), mountpoint=target['target'])) diff --git a/archinstall/lib/general.py b/archinstall/lib/general.py index 9fbf2654..65c83484 100644 --- a/archinstall/lib/general.py +++ b/archinstall/lib/general.py @@ -4,6 +4,7 @@ import logging import os import pty import shlex +import subprocess import sys import time from datetime import datetime, date @@ -41,6 +42,8 @@ def locate_binary(name): return os.path.join(root, file) break # Don't recurse + raise RequirementError(f"Binary {name} does not exist.") + class JsonEncoder: def _encode(obj): @@ -84,108 +87,125 @@ class JSON(json.JSONEncoder, json.JSONDecoder): return super(JSON, self).encode(self._encode(obj)) -class SysCommand: - """ - Stolen from archinstall_gui - """ - - def __init__(self, cmd, callback=None, start_callback=None, peak_output=False, environment_vars=None, *args, **kwargs): - if environment_vars is None: +class SysCommandWorker: + def __init__(self, cmd, callbacks=None, peak_output=False, environment_vars=None, logfile=None, working_directory='./'): + if not callbacks: + callbacks = {} + if not environment_vars: environment_vars = {} - kwargs.setdefault("worker_id", gen_uid()) - kwargs.setdefault("emulate", False) - kwargs.setdefault("suppress_errors", False) - self.log = kwargs.get('log', log) + if type(cmd) is str: + cmd = shlex.split(cmd) - if kwargs['emulate']: - self.log(f"Starting command '{cmd}' in emulation mode.", level=logging.DEBUG) - - if type(cmd) is list: - # if we get a list of arguments - self.raw_cmd = shlex.join(cmd) - self.cmd = cmd - else: - # else consider it a single shell string - # this should only be used if really necessary - self.raw_cmd = cmd - try: - self.cmd = shlex.split(cmd) - except Exception as e: - raise ValueError(f'Incorrect string to split: {cmd}\n{e}') + if cmd[0][0] != '/' and cmd[0][:2] != './': + # "which" doesn't work as it's a builtin to bash. + # It used to work, but for whatever reason it doesn't anymore. + # We there for fall back on manual lookup in os.PATH + cmd[0] = locate_binary(cmd[0]) - self.args = args - self.kwargs = kwargs + self.cmd = cmd + self.callbacks = callbacks self.peak_output = peak_output self.environment_vars = environment_vars + self.logfile = logfile + self.working_directory = working_directory - self.kwargs.setdefault("worker", None) - self.callback = callback - self.pid = None self.exit_code = None - self.started = time.time() + self._trace_log = b'' + self._trace_log_pos = 0 + self.poll_object = epoll() + self.child_fd = None + self.started = None self.ended = None - self.worker_id = kwargs['worker_id'] - self.trace_log = b'' - self.status = 'starting' - user_catalogue = os.path.expanduser('~') + def __contains__(self, key: bytes): + """ + Contains will also move the current buffert position forward. + This is to avoid re-checking the same data when looking for output. + """ + assert type(key) == bytes - if workdir := kwargs.get('workdir', None): - self.cwd = workdir - self.exec_dir = workdir - else: - self.cwd = f"{user_catalogue}/.cache/archinstall/workers/{kwargs['worker_id']}/" - self.exec_dir = f'{self.cwd}/{os.path.basename(self.cmd[0])}_workingdir' + if (contains := key in self._trace_log[self._trace_log_pos:]): + self._trace_log_pos += self._trace_log[self._trace_log_pos:].find(key) + len(key) - if not self.cmd[0][0] == '/': - # "which" doesn't work as it's a builtin to bash. - # It used to work, but for whatever reason it doesn't anymore. So back to square one.. + return contains + + def __iter__(self, *args, **kwargs): + for line in self._trace_log[self._trace_log_pos:self._trace_log.rfind(b'\n')].split(b'\n'): + if line: + yield line + b'\n' - # self.log('Worker command is not executed with absolute path, trying to find: {}'.format(self.cmd[0]), origin='spawn', level=5) - # self.log('This is the binary {} for {}'.format(o.decode('UTF-8'), self.cmd[0]), origin='spawn', level=5) - self.cmd[0] = locate_binary(self.cmd[0]) + self._trace_log_pos = self._trace_log.rfind(b'\n') - if not os.path.isdir(self.exec_dir): - os.makedirs(self.exec_dir) + def __repr__(self): + self.make_sure_we_are_executing() + return str(self._trace_log) - if start_callback: - start_callback(self, *args, **kwargs) - self.run() + def __enter__(self): + return self - def __iter__(self, *args, **kwargs): - for line in self.trace_log.split(b'\n'): - yield line + def __exit__(self, *args): + # b''.join(sys_command('sync')) # No need to, since the underlying fs() object will call sync. + # TODO: https://stackoverflow.com/questions/28157929/how-to-safely-handle-an-exception-inside-a-context-manager - def __repr__(self, *args, **kwargs): - return f"{self.cmd, self.trace_log}" + if self.child_fd: + try: + os.close(self.child_fd) + except: + pass - def decode(self, fmt='UTF-8'): - return self.trace_log.decode(fmt) + if self.peak_output: + # To make sure any peaked output didn't leave us hanging + # on the same line we were on. + sys.stdout.write("\n") + sys.stdout.flush() - def dump(self): - return { - 'status': self.status, - 'worker_id': self.worker_id, - 'worker_result': self.trace_log.decode('UTF-8'), - 'started': self.started, - 'ended': self.ended, - 'started_pprint': '{}-{}-{} {}:{}:{}'.format(*time.localtime(self.started)), - 'ended_pprint': '{}-{}-{} {}:{}:{}'.format(*time.localtime(self.ended)) if self.ended else None, - 'exit_code': self.exit_code, - } + if len(args) >= 2 and args[1]: + log(args[1], level=logging.ERROR, fg='red') + + if self.exit_code != 0: + raise SysCallError(f"{self.cmd} exited with abnormal exit code: {self.exit_code}") + + def is_alive(self): + self.poll() + + if self.started and self.ended is None: + return True + + return False + + def write(self, data: bytes, line_ending=True): + assert type(data) == bytes # TODO: Maybe we can support str as well and encode it + + self.make_sure_we_are_executing() + + os.write(self.child_fd, data + (b'\n' if line_ending else b'')) + + def make_sure_we_are_executing(self): + if not self.started: + return self.execute() + + def tell(self) -> int: + self.make_sure_we_are_executing() + return self._trace_log_pos + + def seek(self, pos): + self.make_sure_we_are_executing() + # Safety check to ensure 0 < pos < len(tracelog) + self._trace_log_pos = min(max(0, pos), len(self._trace_log)) def peak(self, output: Union[str, bytes]) -> bool: - if type(output) == bytes: - try: - output = output.decode('UTF-8') - except UnicodeDecodeError: + if self.peak_output: + if type(output) == bytes: + try: + output = output.decode('UTF-8') + except UnicodeDecodeError: + return False + + output = output.strip('\r\n ') + if len(output) <= 0: return False - output = output.strip('\r\n ') - if len(output) <= 0: - return False - if self.peak_output: from .user_interaction import get_terminal_width # Move back to the beginning of the terminal @@ -207,125 +227,127 @@ class SysCommand: sys.stdout.flush() return True - def run(self): - self.status = 'running' - old_dir = os.getcwd() - os.chdir(self.exec_dir) - self.pid, child_fd = pty.fork() - if not self.pid: # Child process - # Replace child process with our main process - if not self.kwargs['emulate']: - try: - os.execve(self.cmd[0], self.cmd, {**os.environ, **self.environment_vars}) - except FileNotFoundError: - self.status = 'done' - self.log(f"{self.cmd[0]} does not exist.", level=logging.DEBUG) - self.exit_code = 1 - return False - - os.chdir(old_dir) + def poll(self): + self.make_sure_we_are_executing() - poller = epoll() - poller.register(child_fd, EPOLLIN | EPOLLHUP) - - if 'events' in self.kwargs and 'debug' in self.kwargs: - self.log(f'[D] Using triggers for command: {self.cmd}', level=logging.DEBUG) - self.log(json.dumps(self.kwargs['events']), level=logging.DEBUG) - - alive = True - last_trigger_pos = 0 - while alive and not self.kwargs['emulate']: - for fileno, event in poller.poll(0.1): - try: - output = os.read(child_fd, 8192) - self.peak(output) - self.trace_log += output - except OSError: - alive = False - break - - if 'debug' in self.kwargs and self.kwargs['debug'] and len(output): - self.log(self.cmd, 'gave:', output.decode('UTF-8'), level=logging.DEBUG) - - if 'on_output' in self.kwargs: - self.kwargs['on_output'](self.kwargs['worker'], output) - - lower = output.lower() - broke = False - if 'events' in self.kwargs: - for trigger in list(self.kwargs['events']): - if type(trigger) != bytes: - original = trigger - trigger = bytes(original, 'UTF-8') - self.kwargs['events'][trigger] = self.kwargs['events'][original] - del self.kwargs['events'][original] - if type(self.kwargs['events'][trigger]) != bytes: - self.kwargs['events'][trigger] = bytes(self.kwargs['events'][trigger], 'UTF-8') - - if trigger.lower() in self.trace_log[last_trigger_pos:].lower(): - trigger_pos = self.trace_log[last_trigger_pos:].lower().find(trigger.lower()) - - if 'debug' in self.kwargs and self.kwargs['debug']: - self.log(f"Writing to subprocess {self.cmd[0]}: {self.kwargs['events'][trigger].decode('UTF-8')}", level=logging.DEBUG) - self.log(f"Writing to subprocess {self.cmd[0]}: {self.kwargs['events'][trigger].decode('UTF-8')}", level=logging.DEBUG) - - last_trigger_pos = trigger_pos - os.write(child_fd, self.kwargs['events'][trigger]) - del self.kwargs['events'][trigger] - broke = True - break - - if broke: - continue - - # Adding a exit trigger: - if len(self.kwargs['events']) == 0: - if 'debug' in self.kwargs and self.kwargs['debug']: - self.log(f"Waiting for last command {self.cmd[0]} to finish.", level=logging.DEBUG) - - if bytes(']$'.lower(), 'UTF-8') in self.trace_log[0 - len(']$') - 5:].lower(): - if 'debug' in self.kwargs and self.kwargs['debug']: - self.log(f"{self.cmd[0]} has finished.", level=logging.DEBUG) - alive = False - break - - self.status = 'done' - - if 'debug' in self.kwargs and self.kwargs['debug']: - self.log(f"{self.cmd[0]} waiting for exit code.", level=logging.DEBUG) - - if not self.kwargs['emulate']: + got_output = False + for fileno, event in self.poll_object.poll(0.1): + try: + output = os.read(self.child_fd, 8192) + got_output = True + self.peak(output) + self._trace_log += output + except OSError as err: + self.ended = time.time() + break + + if self.ended or (got_output is False and pid_exists(self.pid) is False): + self.ended = time.time() try: self.exit_code = os.waitpid(self.pid, 0)[1] except ChildProcessError: try: - self.exit_code = os.waitpid(child_fd, 0)[1] + self.exit_code = os.waitpid(self.child_fd, 0)[1] except ChildProcessError: self.exit_code = 1 - else: - self.exit_code = 0 - if 'debug' in self.kwargs and self.kwargs['debug']: - self.log(f"{self.cmd[0]} got exit code: {self.exit_code}", level=logging.DEBUG) + def execute(self) -> bool: + if (old_dir := os.getcwd()) != self.working_directory: + os.chdir(self.working_directory) + + # Note: If for any reason, we get a Python exception between here + # and until os.close(), the traceback will get locked inside + # stdout of the child_fd object. `os.read(self.child_fd, 8192)` is the + # only way to get the traceback without loosing it. + self.pid, self.child_fd = pty.fork() + os.chdir(old_dir) + + if not self.pid: + try: + os.execve(self.cmd[0], self.cmd, {**os.environ, **self.environment_vars}) + except FileNotFoundError: + log(f"{self.cmd[0]} does not exist.", level=logging.ERROR, fg="red") + self.exit_code = 1 + return False + + self.started = time.time() + self.poll_object.register(self.child_fd, EPOLLIN | EPOLLHUP) + + return True + + def decode(self, encoding='UTF-8'): + return self._trace_log.decode(encoding) + + +class SysCommand: + def __init__(self, cmd, callback=None, start_callback=None, peak_output=False, environment_vars=None, working_directory='./'): + _callbacks = {} + if callback: + _callbacks['on_end'] = callback + if start_callback: + _callbacks['on_start'] = start_callback + + self.cmd = cmd + self._callbacks = _callbacks + self.peak_output = peak_output + self.environment_vars = environment_vars + self.working_directory = working_directory - if 'ignore_errors' in self.kwargs: - self.exit_code = 0 + self.session = None + self.create_session() - if self.exit_code != 0 and not self.kwargs['suppress_errors']: - # self.log(self.trace_log.decode('UTF-8'), level=logging.DEBUG) - # self.log(f"'{self.raw_cmd}' did not exit gracefully, exit code {self.exit_code}.", level=logging.ERROR) - raise SysCallError( - message=f"{self.trace_log.decode('UTF-8')}\n'{self.raw_cmd}' did not exit gracefully (trace log above), exit code: {self.exit_code}", - exit_code=self.exit_code) + def __enter__(self): + return self.session - self.ended = time.time() - with open(f'{self.cwd}/trace.log', 'wb') as fh: - fh.write(self.trace_log) + def __exit__(self, *args, **kwargs): + # b''.join(sys_command('sync')) # No need to, since the underlying fs() object will call sync. + # TODO: https://stackoverflow.com/questions/28157929/how-to-safely-handle-an-exception-inside-a-context-manager + + if len(args) >= 2 and args[1]: + log(args[1], level=logging.ERROR, fg='red') + + def __iter__(self, *args, **kwargs): + + for line in self.session: + yield line + + def __repr__(self, *args, **kwargs): + return self.session._trace_log.decode('UTF-8') + + def __json__(self): + return { + 'cmd': self.cmd, + 'callbacks': self._callbacks, + 'peak': self.peak_output, + 'environment_vars': self.environment_vars, + 'session': True if self.session else False + } + + def create_session(self): + if self.session: + return True try: - os.close(child_fd) - except: - pass + self.session = SysCommandWorker(self.cmd, callbacks=self._callbacks, peak_output=self.peak_output, environment_vars=self.environment_vars) + + while self.session.ended is None: + self.session.poll() + + except SysCallError: + return False + + return True + + def decode(self, fmt='UTF-8'): + return self.session._trace_log.decode(fmt) + + @property + def exit_code(self): + return self.session.exit_code + + @property + def trace_log(self): + return self.session._trace_log def prerequisite_check(): @@ -337,3 +359,9 @@ def prerequisite_check(): def reboot(): o = b''.join(SysCommand("/usr/bin/reboot")) + +def pid_exists(pid :int): + try: + return any(subprocess.check_output(['/usr/bin/ps', '--no-headers', '-o', 'pid', '-p', str(pid)]).strip()) + except subprocess.CalledProcessError: + return False \ No newline at end of file diff --git a/archinstall/lib/installer.py b/archinstall/lib/installer.py index 61b0a3a1..29b3bc1a 100644 --- a/archinstall/lib/installer.py +++ b/archinstall/lib/installer.py @@ -1,8 +1,8 @@ from .disk import * from .hardware import * +from .locale_helpers import verify_x11_keyboard_layout from .mirrors import * from .storage import storage -from .systemd import Networkd from .user_interaction import * # Any package that the Installer() is responsible for (optional and the default ones) @@ -78,7 +78,6 @@ class Installer: # TODO: https://stackoverflow.com/questions/28157929/how-to-safely-handle-an-exception-inside-a-context-manager if len(args) >= 2 and args[1]: - # self.log(self.trace_log.decode('UTF-8'), level=logging.DEBUG) self.log(args[1], level=logging.ERROR, fg='red') self.sync_log_to_install_medium() @@ -136,7 +135,7 @@ class Installer: self.log(f'Installing packages: {packages}', level=logging.INFO) if (sync_mirrors := SysCommand('/usr/bin/pacman -Syy')).exit_code == 0: - if (pacstrap := SysCommand(f'/usr/bin/pacstrap {self.target} {" ".join(packages)}', **kwargs)).exit_code == 0: + if (pacstrap := SysCommand(f'/usr/bin/pacstrap {self.target} {" ".join(packages)}', peak_output=True)).exit_code == 0: return True else: self.log(f'Could not strap in packages: {pacstrap.exit_code}', level=logging.INFO) @@ -149,9 +148,8 @@ class Installer: def genfstab(self, flags='-pU'): self.log(f"Updating {self.target}/etc/fstab", level=logging.INFO) - fstab = SysCommand(f'/usr/bin/genfstab {flags} {self.target}').trace_log - with open(f"{self.target}/etc/fstab", 'ab') as fstab_fh: - fstab_fh.write(fstab) + with open(f"{self.target}/etc/fstab", 'a') as fstab_fh: + fstab_fh.write(SysCommand(f'/usr/bin/genfstab {flags} {self.target}').decode()) if not os.path.isfile(f'{self.target}/etc/fstab'): raise RequirementError(f'Could not generate fstab, strapping in packages most likely failed (disk out of space?)\n{fstab}') @@ -215,6 +213,8 @@ class Installer: subprocess.check_call(f"/usr/bin/arch-chroot {self.target}", shell=True) def configure_nic(self, nic, dhcp=True, ip=None, gateway=None, dns=None, *args, **kwargs): + from .systemd import Networkd + if dhcp: conf = Networkd(Match={"Name": nic}, Network={"DHCP": "yes"}) else: @@ -514,11 +514,44 @@ class Installer: o = b''.join(SysCommand(f"/usr/bin/arch-chroot {self.target} sh -c \"chsh -s {shell} {user}\"")) pass - def set_keyboard_language(self, language): + def set_keyboard_language(self, language: str) -> bool: if len(language.strip()): - with open(f'{self.target}/etc/vconsole.conf', 'w') as vconsole: - vconsole.write(f'KEYMAP={language}\n') - vconsole.write('FONT=lat9w-16\n') + if not verify_keyboard_layout(language): + self.log(f"Invalid keyboard language specified: {language}", fg="red", level=logging.ERROR) + return False + + # In accordance with https://github.com/archlinux/archinstall/issues/107#issuecomment-841701968 + # Setting an empty keymap first, allows the subsequent call to set layout for both console and x11. + from .systemd import Boot + + with Boot(self) as session: + session.SysCommand(["localectl", "set-keymap", '""']) + + if (output := session.SysCommand(["localectl", "set-keymap", language])).exit_code != 0: + raise ServiceException(f"Unable to set locale '{language}' for console: {output}") + + self.log(f"Keyboard language for this installation is now set to: {language}") else: self.log('Keyboard language was not changed from default (no language specified).', fg="yellow", level=logging.INFO) + + return True + + def set_x11_keyboard_language(self, language: str) -> bool: + """ + A fallback function to set x11 layout specifically and separately from console layout. + This isn't strictly necessary since .set_keyboard_language() does this as well. + """ + if len(language.strip()): + if not verify_x11_keyboard_layout(language): + self.log(f"Invalid x11-keyboard language specified: {language}", fg="red", level=logging.ERROR) + return False + + with Boot(self) as session: + session.SysCommand(["localectl", "set-x11-keymap", '""']) + + if (output := session.SysCommand(["localectl", "set-x11-keymap", language])).exit_code != 0: + raise ServiceException(f"Unable to set locale '{language}' for X11: {output}") + else: + self.log(f'X11-Keyboard language was not changed from default (no language specified).', fg="yellow", level=logging.INFO) + return True diff --git a/archinstall/lib/locale_helpers.py b/archinstall/lib/locale_helpers.py index 2db429fd..36228edc 100644 --- a/archinstall/lib/locale_helpers.py +++ b/archinstall/lib/locale_helpers.py @@ -1,23 +1,18 @@ -import os -import subprocess +import logging -from .exceptions import * - - -# from .general import sys_command +from .exceptions import ServiceException +from .general import SysCommand +from .output import log def list_keyboard_languages(): - locale_dir = '/usr/share/kbd/keymaps/' - - if not os.path.isdir(locale_dir): - raise RequirementError(f'Directory containing locales does not exist: {locale_dir}') + for line in SysCommand("localectl --no-pager list-keymaps", environment_vars={'SYSTEMD_COLORS': '0'}): + yield line.decode('UTF-8').strip() - for root, folders, files in os.walk(locale_dir): - for file in files: - if os.path.splitext(file)[1] == '.gz': - yield file.strip('.gz').strip('.map') +def list_x11_keyboard_languages(): + for line in SysCommand("localectl --no-pager list-x11-keymap-layouts", environment_vars={'SYSTEMD_COLORS': '0'}): + yield line.decode('UTF-8').strip() def verify_keyboard_layout(layout): @@ -27,11 +22,28 @@ def verify_keyboard_layout(layout): return False -def search_keyboard_layout(layout_filter): +def verify_x11_keyboard_layout(layout): + for language in list_x11_keyboard_languages(): + if layout.lower() == language.lower(): + return True + return False + + +def search_keyboard_layout(layout): for language in list_keyboard_languages(): - if layout_filter.lower() in language.lower(): + if layout.lower() in language.lower(): yield language def set_keyboard_language(locale): - return subprocess.call(['loadkeys', locale]) == 0 + if len(locale.strip()): + if not verify_keyboard_layout(locale): + log(f"Invalid keyboard locale specified: {locale}", fg="red", level=logging.ERROR) + return False + + if (output := SysCommand(f'localectl set-keymap {locale}')).exit_code != 0: + raise ServiceException(f"Unable to set locale '{locale}' for console: {output}") + + return True + + return False diff --git a/archinstall/lib/networking.py b/archinstall/lib/networking.py index dbd510dd..fdeefb84 100644 --- a/archinstall/lib/networking.py +++ b/archinstall/lib/networking.py @@ -1,5 +1,6 @@ import fcntl import os +import logging import socket import struct from collections import OrderedDict @@ -7,7 +8,7 @@ from collections import OrderedDict from .exceptions import * from .general import SysCommand from .storage import storage - +from .output import log def get_hw_addr(ifname): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) @@ -27,12 +28,12 @@ def list_interfaces(skip_loopback=True): def check_mirror_reachable(): - try: - check = SysCommand("pacman -Sy") - return check.exit_code == 0 - except: - return False + if (exit_code := SysCommand("pacman -Sy").exit_code) == 0: + return True + elif exit_code == 256: + log("check_mirror_reachable() uses 'pacman -Sy' which requires root.", level=logging.ERROR, fg="red") + return False def enrich_iface_types(interfaces: dict): result = {} diff --git a/archinstall/lib/output.py b/archinstall/lib/output.py index 20b0df8d..595e9693 100644 --- a/archinstall/lib/output.py +++ b/archinstall/lib/output.py @@ -155,7 +155,7 @@ def log(*args, **kwargs): log("Deprecated level detected in log message, please use new logging. instead for the following log message:", fg="red", level=logging.ERROR, force=True) kwargs['level'] = logging.DEBUG - if kwargs['level'] > storage.get('LOG_LEVEL', logging.INFO) and 'force' not in kwargs: + if kwargs['level'] < storage.get('LOG_LEVEL', logging.INFO) and 'force' not in kwargs: # Level on log message was Debug, but output level is set to Info. # In that case, we'll drop it. return None diff --git a/archinstall/lib/systemd.py b/archinstall/lib/systemd.py index 5607250b..e64ff7e0 100644 --- a/archinstall/lib/systemd.py +++ b/archinstall/lib/systemd.py @@ -1,3 +1,10 @@ +import logging + +from .general import SysCommand, SysCommandWorker, locate_binary +from .installer import Installer +from .output import log +from .storage import storage + class Ini: def __init__(self, *args, **kwargs): """ @@ -36,3 +43,78 @@ class Networkd(Systemd): """ Placeholder class to do systemd-network specific setups. """ + + +class Boot: + def __init__(self, installation: Installer): + self.instance = installation + self.container_name = 'archinstall' + self.session = None + self.ready = False + + def __enter__(self): + if (existing_session := storage.get('active_boot', None)) and existing_session.instance != self.instance: + raise KeyError("Archinstall only supports booting up one instance, and a active session is already active and it is not this one.") + + if existing_session: + self.session = existing_session.session + self.ready = existing_session.ready + else: + self.session = SysCommandWorker([ + '/usr/bin/systemd-nspawn', + '-D', self.instance.target, + '-b', + '--machine', self.container_name + ]) + + if not self.ready: + while self.session.is_alive(): + if b' login:' in self.session: + self.ready = True + break + + storage['active_boot'] = self + return self + + def __exit__(self, *args, **kwargs): + # b''.join(sys_command('sync')) # No need to, since the underlying fs() object will call sync. + # TODO: https://stackoverflow.com/questions/28157929/how-to-safely-handle-an-exception-inside-a-context-manager + + if len(args) >= 2 and args[1]: + log(args[1], level=logging.ERROR, fg='red') + log(f"The error above occured in a temporary boot-up of the installation {self.instance}", level=logging.ERROR, fg="red") + + SysCommand(f'machinectl shell {self.container_name} /bin/bash -c "shutdown now"') + + def __iter__(self): + if self.session: + for value in self.session: + yield value + + def __contains__(self, key: bytes): + if self.session is None: + return False + + return key in self.session + + def is_alive(self): + if self.session is None: + return False + + return self.session.is_alive() + + def SysCommand(self, cmd :list, *args, **kwargs): + if cmd[0][0] != '/' and cmd[0][:2] != './': + # This check is also done in SysCommand & SysCommandWorker. + # However, that check is done for `machinectl` and not for our chroot command. + # So this wrapper for SysCommand will do this additionally. + + cmd[0] = locate_binary(cmd[0]) + + return SysCommand(["machinectl", "shell", self.container_name, *cmd], *args, **kwargs) + + def SysCommandWorker(self, cmd :list, *args, **kwargs): + if cmd[0][0] != '/' and cmd[0][:2] != './': + cmd[0] = locate_binary(cmd[0]) + + return SysCommandWorker(["machinectl", "shell", self.container_name, *cmd], *args, **kwargs) \ No newline at end of file diff --git a/archinstall/lib/user_interaction.py b/archinstall/lib/user_interaction.py index 5f849607..50c62aa9 100644 --- a/archinstall/lib/user_interaction.py +++ b/archinstall/lib/user_interaction.py @@ -591,7 +591,7 @@ def select_profile(options): raise RequirementError("Selecting profiles require a least one profile to be given as an option.") -def select_language(options, show_only_country_codes=True): +def select_language(options, show_only_country_codes=True, input_text='Select one of the above keyboard languages (by number or full name): '): """ Asks the user to select a language from the `options` dictionary parameter. Usually this is combined with :ref:`archinstall.list_keyboard_languages`. @@ -613,14 +613,13 @@ def select_language(options, show_only_country_codes=True): languages = sorted(list(options)) if len(languages) >= 1: - for index, language in enumerate(languages): - print(f"{index}: {language}") + print_large_list(languages, margin_bottom=4) print(" -- You can choose a layout that isn't in this list, but whose name you know --") - print(" -- Also, you can enter '?' or 'help' to search for more languages, or skip to use US layout --") + print(f" -- Also, you can enter '?' or 'help' to search for more languages, or skip to use {default_keyboard_language} layout --") while True: - selected_language = input('Select one of the above keyboard languages (by name or full name): ') + selected_language = input(input_text) if not selected_language: return default_keyboard_language elif selected_language.lower() in ('?', 'help'): @@ -705,8 +704,7 @@ def select_driver(options=AVAILABLE_GFX_DRIVERS): default_option = options["All open-source (default)"] if drivers: - lspci = SysCommand('/usr/bin/lspci') - for line in lspci.trace_log.split(b'\r\n'): + for line in SysCommand('/usr/bin/lspci'): if b' vga ' in line.lower(): if b'nvidia' in line.lower(): print(' ** nvidia card detected, suggested driver: nvidia **') diff --git a/examples/guided.py b/examples/guided.py index cce06b29..b9b06a64 100644 --- a/examples/guided.py +++ b/examples/guided.py @@ -1,6 +1,7 @@ import json import logging import time +import os import archinstall from archinstall.lib.hardware import has_uefi @@ -51,7 +52,7 @@ def ask_user_questions(): else: archinstall.arguments['harddrive'] = archinstall.select_disk(archinstall.all_disks()) if archinstall.arguments['harddrive'] is None: - archinstall.arguments['target-mount'] = '/mnt' + archinstall.arguments['target-mount'] = archinstall.storage.get('MOUNT_POINT', '/mnt') # Perform a quick sanity check on the selected harddrive. # 1. Check if it has partitions @@ -291,14 +292,14 @@ def perform_installation_steps(): # unlocks the drive so that it can be used as a normal block-device within archinstall. with archinstall.luks2(fs.find_partition('/'), 'luksloop', archinstall.arguments.get('!encryption-password', None)) as unlocked_device: unlocked_device.format(fs.find_partition('/').filesystem) - unlocked_device.mount('/mnt') + unlocked_device.mount(archinstall.storage.get('MOUNT_POINT', '/mnt')) else: - fs.find_partition('/').mount('/mnt') + fs.find_partition('/').mount(archinstall.storage.get('MOUNT_POINT', '/mnt')) if has_uefi(): - fs.find_partition('/boot').mount('/mnt/boot') + fs.find_partition('/boot').mount(archinstall.storage.get('MOUNT_POINT', '/mnt')+'/boot') - perform_installation('/mnt') + perform_installation(archinstall.storage.get('MOUNT_POINT', '/mnt')) def perform_installation(mountpoint): @@ -324,7 +325,6 @@ def perform_installation(mountpoint): installation.set_mirrors(archinstall.arguments['mirror-region']) # Set the mirrors in the installation medium if archinstall.arguments["bootloader"] == "grub-install" and has_uefi(): installation.add_additional_packages("grub") - installation.set_keyboard_language(archinstall.arguments['keyboard-language']) installation.add_bootloader(archinstall.arguments["bootloader"]) # If user selected to copy the current ISO network configuration @@ -370,6 +370,10 @@ def perform_installation(mountpoint): if (root_pw := archinstall.arguments.get('!root-password', None)) and len(root_pw): installation.user_set_pw('root', root_pw) + # This step must be after profile installs to allow profiles to install language pre-requisits. + # After which, this step will set the language both for console and x11 if x11 was installed for instance. + installation.set_keyboard_language(archinstall.arguments['keyboard-language']) + if archinstall.arguments['profile'] and archinstall.arguments['profile'].has_post_install(): with archinstall.arguments['profile'].load_instructions(namespace=f"{archinstall.arguments['profile'].namespace}.py") as imported: if not imported._post_install(): @@ -389,7 +393,8 @@ def perform_installation(mountpoint): if not check_mirror_reachable(): - archinstall.log("Arch Linux mirrors are not reachable. Please check your internet connection and try again.", level=logging.INFO, fg="red") + log_file = os.path.join(archinstall.storage.get('LOG_PATH', None), archinstall.storage.get('LOG_FILE', None)) + archinstall.log(f"Arch Linux mirrors are not reachable. Please check your internet connection and the log file '{log_file}'.", level=logging.INFO, fg="red") exit(1) ask_user_questions() diff --git a/profiles/awesome.py b/profiles/awesome.py index 0c1b20ea..9648fc4a 100644 --- a/profiles/awesome.py +++ b/profiles/awesome.py @@ -9,7 +9,7 @@ is_top_level_profile = False __packages__ = [ "nemo", "gpicview", - "main", + "maim", "alacritty", ] -- cgit v1.2.3-54-g00ecf