From 69d079e63a00caf9268575a6ca4789962776761b Mon Sep 17 00:00:00 2001 From: advaithm Date: Wed, 12 May 2021 15:45:45 +0530 Subject: some type hint fixes and a bad catch fix --- archinstall/lib/profiles.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'archinstall/lib/profiles.py') diff --git a/archinstall/lib/profiles.py b/archinstall/lib/profiles.py index 06237c1c..1feba1cd 100644 --- a/archinstall/lib/profiles.py +++ b/archinstall/lib/profiles.py @@ -1,3 +1,4 @@ +from typing import Optional import os, urllib.request, urllib.parse, ssl, json, re import importlib.util, sys, glob, hashlib, logging from collections import OrderedDict @@ -49,7 +50,7 @@ def list_profiles(filter_irrelevant_macs=True, subpath='', filter_top_level_prof except urllib.error.HTTPError as err: print(f'Error: Listing profiles on URL "{profiles_url}" resulted in:', err) return cache - except: + except json.decoder.JSONDecodeError as err: print(f'Error: Could not decode "{profiles_url}" result as JSON:', err) return cache @@ -215,7 +216,7 @@ class Profile(Script): return True @property - def packages(self) -> list: + def packages(self) -> Optional[list]: """ Returns a list of packages baked into the profile definition. If no package definition has been done, .packages() will return None. -- cgit v1.2.3-70-g09d2 From 1674b7088d66873712dea57f99d3221daad4db0b Mon Sep 17 00:00:00 2001 From: Anton Hvornum Date: Fri, 14 May 2021 16:13:03 +0200 Subject: Fixes string index error. --- archinstall/lib/profiles.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'archinstall/lib/profiles.py') diff --git a/archinstall/lib/profiles.py b/archinstall/lib/profiles.py index 1feba1cd..9225a129 100644 --- a/archinstall/lib/profiles.py +++ b/archinstall/lib/profiles.py @@ -36,7 +36,7 @@ def list_profiles(filter_irrelevant_macs=True, subpath='', filter_top_level_prof description = '' with open(os.path.join(root, file), 'r') as fh: first_line = fh.readline() - if first_line[0] == '#': + if len(first_line) and first_line[0] == '#': description = first_line[1:].strip() cache[file[:-3]] = {'path' : os.path.join(root, file), 'description' : description, 'tailored' : tailored} -- cgit v1.2.3-70-g09d2 From bd9992be29c5fa5b5a47e67d2dbd250b9a5a8828 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 14 May 2021 21:22:30 -0400 Subject: Don't list __init__.py files in profile list --- archinstall/lib/profiles.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'archinstall/lib/profiles.py') diff --git a/archinstall/lib/profiles.py b/archinstall/lib/profiles.py index 9225a129..42fd4c24 100644 --- a/archinstall/lib/profiles.py +++ b/archinstall/lib/profiles.py @@ -26,6 +26,8 @@ def list_profiles(filter_irrelevant_macs=True, subpath='', filter_top_level_prof for PATH_ITEM in storage['PROFILE_PATH']: 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 if os.path.splitext(file)[1] == '.py': tailored = False if len(mac := re.findall('(([a-zA-z0-9]{2}[-:]){5}([a-zA-z0-9]{2}))', file)): -- cgit v1.2.3-70-g09d2 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/profiles.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-70-g09d2 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/profiles.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-70-g09d2 From 3b20adb7d216cbe72bf1770e219207216bee96ee Mon Sep 17 00:00:00 2001 From: Dylan Taylor Date: Sat, 15 May 2021 13:59:37 -0400 Subject: Whitespace changes --- archinstall/lib/disk.py | 16 ++++++++++------ archinstall/lib/general.py | 2 ++ archinstall/lib/hardware.py | 2 +- archinstall/lib/installer.py | 5 ++--- archinstall/lib/mirrors.py | 2 +- archinstall/lib/profiles.py | 6 +++--- archinstall/lib/services.py | 2 +- docs/pull_request_template.md | 3 +-- examples/guided.py | 12 ++++++------ profiles/gnome.py | 4 ++-- profiles/xorg.py | 4 ++-- 11 files changed, 31 insertions(+), 27 deletions(-) (limited to 'archinstall/lib/profiles.py') diff --git a/archinstall/lib/disk.py b/archinstall/lib/disk.py index 2241ac8e..62f1785f 100644 --- a/archinstall/lib/disk.py +++ b/archinstall/lib/disk.py @@ -152,8 +152,9 @@ class BlockDevice(): def flush_cache(self): self.part_cache = OrderedDict() + class Partition(): - def __init__(self, path :str, block_device :BlockDevice, part_id=None, size=-1, filesystem=None, mountpoint=None, encrypted=False, autodetect_filesystem=True): + 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) @@ -506,9 +507,9 @@ class Filesystem(): self.blockdevice.partition[0].allow_formatting = True self.blockdevice.partition[1].allow_formatting = True else: - #we don't need a seprate boot partition it would be a waste of space + # we don't need a seprate boot partition it would be a waste of space self.add_partition('primary', start='1MB', end='100%') - self.blockdevice.partition[0].filesystem=root_filesystem_type + self.blockdevice.partition[0].filesystem = root_filesystem_type log(f"Set the root partition {self.blockdevice.partition[0]} to use filesystem {root_filesystem_type}.", level=logging.DEBUG) self.blockdevice.partition[0].target_mountpoint = '/' self.blockdevice.partition[0].allow_formatting = True @@ -558,28 +559,31 @@ def device_state(name, *args, **kwargs): return return True + # lsblk --json -l -n -o path 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(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']: if not kwargs['partitions'] and drive['type'] == 'part': continue drives[drive['path']] = BlockDevice(drive['path'], drive) return drives + def convert_to_gigabytes(string): unit = string.strip()[-1] size = float(string.strip()[:-1]) if unit == 'M': - size = size/1024 + size = size / 1024 elif unit == 'T': - size = size*1024 + size = size * 1024 return size + def harddrive(size=None, model=None, fuzzy=False): collection = all_disks() for drive in collection: diff --git a/archinstall/lib/general.py b/archinstall/lib/general.py index 9f6de666..f9b9078a 100644 --- a/archinstall/lib/general.py +++ b/archinstall/lib/general.py @@ -83,10 +83,12 @@ class JSON(json.JSONEncoder, json.JSONDecoder): def encode(self, obj): return super(JSON, self).encode(self._encode(obj)) + class sys_command: """ Stolen from archinstall_gui """ + def __init__(self, cmd, callback=None, start_callback=None, peak_output=False, environment_vars={}, *args, **kwargs): kwargs.setdefault("worker_id", gen_uid()) kwargs.setdefault("emulate", False) diff --git a/archinstall/lib/hardware.py b/archinstall/lib/hardware.py index 9eaff22e..3203daee 100644 --- a/archinstall/lib/hardware.py +++ b/archinstall/lib/hardware.py @@ -108,7 +108,7 @@ def cpuVendor() -> Optional[str]: def isVM() -> 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 + 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 except: return False diff --git a/archinstall/lib/installer.py b/archinstall/lib/installer.py index eccd2c49..cacdff69 100644 --- a/archinstall/lib/installer.py +++ b/archinstall/lib/installer.py @@ -153,7 +153,7 @@ class Installer(): return True - def set_hostname(self, hostname :str, *args, **kwargs): + def set_hostname(self, hostname: str, *args, **kwargs): with open(f'{self.target}/etc/hostname', 'w') as fh: fh.write(hostname + '\n') @@ -423,7 +423,6 @@ 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)): # 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) @@ -446,7 +445,7 @@ class Installer(): sys_command('/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() + 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}')) diff --git a/archinstall/lib/mirrors.py b/archinstall/lib/mirrors.py index e53b356a..4b05b96a 100644 --- a/archinstall/lib/mirrors.py +++ b/archinstall/lib/mirrors.py @@ -22,7 +22,7 @@ def filter_mirrors_by_region(regions, destination='/etc/pacman.d/mirrorlist', tm return True -def add_custom_mirrors(mirrors:list, *args, **kwargs): +def add_custom_mirrors(mirrors: list, *args, **kwargs): """ This will append custom mirror definitions in pacman.conf diff --git a/archinstall/lib/profiles.py b/archinstall/lib/profiles.py index 8b5525b4..2f97231c 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 @@ -47,7 +47,7 @@ def list_profiles(filter_irrelevant_macs=True, subpath='', filter_top_level_prof if len(first_line) and first_line[0] == '#': description = first_line[1:].strip() - cache[file[:-3]] = {'path' : os.path.join(root, file), 'description' : description, 'tailored' : tailored} + cache[file[:-3]] = {'path': os.path.join(root, file), 'description': description, 'tailored': tailored} break # Grab profiles from upstream URL @@ -70,7 +70,7 @@ def list_profiles(filter_irrelevant_macs=True, subpath='', filter_top_level_prof continue tailored = True - cache[profile[:-3]] = {'path' : os.path.join(storage["UPSTREAM_URL"]+subpath, profile), 'description' : profile_list[profile], 'tailored' : tailored} + cache[profile[:-3]] = {'path': os.path.join(storage["UPSTREAM_URL"] + subpath, profile), 'description': profile_list[profile], 'tailored': tailored} if filter_top_level_profiles: for profile in list(cache.keys()): diff --git a/archinstall/lib/services.py b/archinstall/lib/services.py index 46aa7846..537a2f84 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(sys_command(f'systemctl show --no-pager -p SubState --value {service_name}', environment_vars={'SYSTEMD_COLORS': '0'})) return state.strip().decode('UTF-8') diff --git a/docs/pull_request_template.md b/docs/pull_request_template.md index 18d01ab2..1cbcf76a 100644 --- a/docs/pull_request_template.md +++ b/docs/pull_request_template.md @@ -12,5 +12,4 @@ 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. +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 ae5c5f54..ca50f838 100644 --- a/examples/guided.py +++ b/examples/guided.py @@ -85,7 +85,7 @@ def ask_user_questions(): # 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): ") + "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 @@ -316,12 +316,12 @@ def perform_installation(mountpoint): time.sleep(1) # Set mirrors used by pacstrap (outside of installation) if archinstall.arguments.get('mirror-region', None): - archinstall.use_mirrors(archinstall.arguments['mirror-region']) # Set the mirrors for the live medium + archinstall.use_mirrors(archinstall.arguments['mirror-region']) # Set the mirrors for the live medium if installation.minimal_installation(): 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: + installation.set_mirrors(archinstall.arguments['mirror-region']) # Set the mirrors in the installation medium + if archinstall.arguments["bootloader"] == "grub-install" and hasUEFI() == True: installation.add_additional_packages("grub") installation.set_keyboard_language(archinstall.arguments['keyboard-language']) installation.add_bootloader(archinstall.arguments["bootloader"]) @@ -329,8 +329,8 @@ 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. - elif archinstall.arguments.get('nic', {}).get('NetworkManager',False): + 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') # Otherwise, if a interface was selected, configure that interface diff --git a/profiles/gnome.py b/profiles/gnome.py index 09fac1bb..7bf5b7fd 100644 --- a/profiles/gnome.py +++ b/profiles/gnome.py @@ -37,5 +37,5 @@ if __name__ == 'gnome': archinstall.storage['installation_session'].add_additional_packages(__packages__) archinstall.storage['installation_session'].enable_service('gdm') # Gnome Display Manager - # We could also start it via xinitrc since we do have Xorg, - # but for gnome that's deprecated and wayland is preferred. +# We could also start it via xinitrc since we do have Xorg, +# but for gnome that's deprecated and wayland is preferred. diff --git a/profiles/xorg.py b/profiles/xorg.py index 3351e4e5..e18a4f03 100644 --- a/profiles/xorg.py +++ b/profiles/xorg.py @@ -30,11 +30,11 @@ if __name__ == 'xorg': try: if "nvidia" in _gfx_driver_packages: if "linux-zen" in archinstall.storage['installation_session'].base_packages or "linux-lts" in archinstall.storage['installation_session'].base_packages: - archinstall.storage['installation_session'].add_additional_packages("dkms")#I've had kernel regen fail if it wasn't installed before nvidia-dkms + archinstall.storage['installation_session'].add_additional_packages("dkms") # I've had kernel regen fail if it wasn't installed before nvidia-dkms archinstall.storage['installation_session'].add_additional_packages("xorg-server xorg-xinit nvidia-dkms") else: archinstall.storage['installation_session'].add_additional_packages(f"xorg-server xorg-xinit {' '.join(_gfx_driver_packages)}") else: archinstall.storage['installation_session'].add_additional_packages(f"xorg-server xorg-xinit {' '.join(_gfx_driver_packages)}") except: - archinstall.storage['installation_session'].add_additional_packages("xorg-server xorg-xinit") # Prep didn't run, so there's no driver to install \ No newline at end of file + archinstall.storage['installation_session'].add_additional_packages("xorg-server xorg-xinit") # Prep didn't run, so there's no driver to install -- cgit v1.2.3-70-g09d2 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/profiles.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-70-g09d2 From 55b09aa1eb4e5ec83d826b8a3dd10670d674aad7 Mon Sep 17 00:00:00 2001 From: Dylan Taylor Date: Sat, 15 May 2021 15:11:59 -0400 Subject: Fix E713 test for membership should be 'not in' --- archinstall/lib/networking.py | 6 +++--- archinstall/lib/output.py | 10 +++++----- archinstall/lib/profiles.py | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) (limited to 'archinstall/lib/profiles.py') diff --git a/archinstall/lib/networking.py b/archinstall/lib/networking.py index e12d9cc3..1a5c403f 100644 --- a/archinstall/lib/networking.py +++ b/archinstall/lib/networking.py @@ -55,9 +55,9 @@ def wireless_scan(interface): sys_command(f"iwctl station {interface} scan") - if not '_WIFI' in storage: + if '_WIFI' not in storage: storage['_WIFI'] = {} - if not interface in storage['_WIFI']: + if interface not in storage['_WIFI']: storage['_WIFI'][interface] = {} storage['_WIFI'][interface]['scanning'] = True @@ -66,7 +66,7 @@ def wireless_scan(interface): # TODO: Full WiFi experience might get evolved in the future, pausing for now 2021-01-25 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: + if '_WIFI' not in storage or interface not in storage['_WIFI'] or storage['_WIFI'][interface].get('scanning', False) is False: import time wireless_scan(interface) diff --git a/archinstall/lib/output.py b/archinstall/lib/output.py index cce9e88c..f69571c0 100644 --- a/archinstall/lib/output.py +++ b/archinstall/lib/output.py @@ -83,11 +83,11 @@ def stylize_output(text: str, *opts, **kwargs): 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)} - RESET = '0' + reset = '0' code_list = [] if text == '' and len(opts) == 1 and opts[0] == 'reset': - return '\x1b[%sm' % RESET + return '\x1b[%sm' % reset for k, v in kwargs.items(): if k == 'fg': code_list.append(foreground[v]) @@ -97,7 +97,7 @@ def stylize_output(text: str, *opts, **kwargs): if o in opt_dict: code_list.append(opt_dict[o]) if 'noreset' not in opts: - text = '%s\x1b[%sm' % (text or '', RESET) + text = '%s\x1b[%sm' % (text or '', reset) return '%s%s' % (('\x1b[%sm' % ';'.join(code_list)), text or '') @@ -112,7 +112,7 @@ def log(*args, **kwargs): # If a logfile is defined in storage, # we use that one to output everything - if (filename := storage.get('LOG_FILE', None)): + if filename := storage.get('LOG_FILE', None): absolute_logfile = os.path.join(storage.get('LOG_PATH', './'), filename) try: @@ -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 not 'force' 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/profiles.py b/archinstall/lib/profiles.py index 894b8fcb..62190cbf 100644 --- a/archinstall/lib/profiles.py +++ b/archinstall/lib/profiles.py @@ -154,7 +154,7 @@ class Script: return self def execute(self): - if not self.namespace in sys.modules or self.spec is None: + if self.namespace not in sys.modules or self.spec is None: self.load_instructions() self.spec.loader.exec_module(sys.modules[self.namespace]) -- cgit v1.2.3-70-g09d2 From 96a48664e2d69d138f58967fdd75c605ad21478e Mon Sep 17 00:00:00 2001 From: Dylan Taylor Date: Sat, 15 May 2021 15:24:34 -0400 Subject: Fix mutable default arguments https://docs.python-guide.org/writing/gotchas/#mutable-default-arguments --- archinstall/lib/general.py | 4 +++- archinstall/lib/installer.py | 10 ++++++++-- archinstall/lib/profiles.py | 4 +++- 3 files changed, 14 insertions(+), 4 deletions(-) (limited to 'archinstall/lib/profiles.py') diff --git a/archinstall/lib/general.py b/archinstall/lib/general.py index 816fa755..7296b943 100644 --- a/archinstall/lib/general.py +++ b/archinstall/lib/general.py @@ -89,7 +89,9 @@ class sys_command: Stolen from archinstall_gui """ - def __init__(self, cmd, callback=None, start_callback=None, peak_output=False, environment_vars={}, *args, **kwargs): + def __init__(self, cmd, callback=None, start_callback=None, peak_output=False, environment_vars=None, *args, **kwargs): + if environment_vars is None: + environment_vars = {} kwargs.setdefault("worker_id", gen_uid()) kwargs.setdefault("emulate", False) kwargs.setdefault("suppress_errors", False) diff --git a/archinstall/lib/installer.py b/archinstall/lib/installer.py index cdf69273..9ddb8825 100644 --- a/archinstall/lib/installer.py +++ b/archinstall/lib/installer.py @@ -34,7 +34,11 @@ class Installer: """ - def __init__(self, target, *, base_packages=__packages__[:3], kernels=['linux']): + def __init__(self, target, *, base_packages=None, kernels=None): + if base_packages is None: + base_packages = __packages__[:3] + if kernels is None: + kernels = ['linux'] self.target = target self.init_time = time.strftime('%Y-%m-%d_%H-%M-%S') self.milliseconds = int(str(time.time()).split('.')[1]) @@ -476,7 +480,9 @@ class Installer: sudoers.write(f'{"%" if group else ""}{entity} ALL=(ALL) ALL\n') return True - def user_create(self, user: str, password=None, groups=[], sudo=False): + def user_create(self, user: str, password=None, groups=None, sudo=False): + 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}')) if password: diff --git a/archinstall/lib/profiles.py b/archinstall/lib/profiles.py index 62190cbf..871e6223 100644 --- a/archinstall/lib/profiles.py +++ b/archinstall/lib/profiles.py @@ -163,8 +163,10 @@ class Script: class Profile(Script): - def __init__(self, installer, path, args={}): + def __init__(self, installer, path, args=None): super(Profile, self).__init__(path, installer) + if args is None: + args = {} def __dump__(self, *args, **kwargs): return {'path': self.path} -- cgit v1.2.3-70-g09d2 From 9cae2374a835b798ac209d48a22bfa70445f2f89 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 17 May 2021 21:53:53 -0400 Subject: Add missing urllib.error import Fixes an unresolved reference Add missing urllib.error import --- archinstall/lib/mirrors.py | 1 + archinstall/lib/packages.py | 1 + archinstall/lib/profiles.py | 1 + 3 files changed, 3 insertions(+) (limited to 'archinstall/lib/profiles.py') diff --git a/archinstall/lib/mirrors.py b/archinstall/lib/mirrors.py index 3215122f..5050df7d 100644 --- a/archinstall/lib/mirrors.py +++ b/archinstall/lib/mirrors.py @@ -1,3 +1,4 @@ +import urllib.error import urllib.request from .general import * diff --git a/archinstall/lib/packages.py b/archinstall/lib/packages.py index e16ed99e..0ea195d2 100644 --- a/archinstall/lib/packages.py +++ b/archinstall/lib/packages.py @@ -1,5 +1,6 @@ import json import ssl +import urllib.error import urllib.parse import urllib.request diff --git a/archinstall/lib/profiles.py b/archinstall/lib/profiles.py index 871e6223..d4913e7e 100644 --- a/archinstall/lib/profiles.py +++ b/archinstall/lib/profiles.py @@ -4,6 +4,7 @@ import json import re import ssl import sys +import urllib.error import urllib.parse import urllib.request from typing import Optional -- cgit v1.2.3-70-g09d2