From 1ae1f2ff1144d502830834ba5a64262f7d195e91 Mon Sep 17 00:00:00 2001 From: Himadri Bhattacharjee <107522312+lavafroth@users.noreply.github.com> Date: Wed, 28 Jun 2023 11:42:53 +0000 Subject: Refactor installer and general design patterns (#1895) * fix: refactor clear_vt100_escape_codes * fix: check for structure being a dict after handling potential parsing errors * refactor: use short circuit logic than if-elif-else chains * fix: use or for nullish moutpoint attribute * fix: better error handling for JSON from urls and paths * chore: json_stream_to_structure documentation * refactor: dry up relative and chroot path for custom command scripts * refactor: use write_text for pathlib.Path object * refactor: use sets to find intersection instead of filter and list * refactor: replace loop with dictionary comprehension in preparing luks partition * refactor: use walrus operator to check if luks_handler exists * refactor: use read_text and splitlines for potential Path object * fix: use keepends in splitlines for compatibility * fix: use keepends in splitlines for compatibility * feat: set pacman_conf Path as an attribute of installer * fix: empty string is a part of any string, avoid tuples * refactor: use iterator patterns to uncomment multilib and testing blocks * fix: don't json.loads an already loaded structure * fix: use fstab_path uniformly in genfstab * fix: remove unused variable matched * refactor: create separate class to modify pacman.conf in a single pass * fix: remove unused attribute pacman_conf from installer * fix: remove unused attribute pacman_conf from installer * feat: add persist method for pacman.conf, rewrite only when needed * fix: use path.write_text for locale.conf * use `or` operator for nullish new_conf * refactor: Installer.target is always a pathlib.Path object, do not check for string type * fix: use Optional[str] in function type definition instead of sumtype of str and None * fix: mypy type annotation * fix: make flake8 happy * chore: move pacman config and repo into pacman module * refactor: use Pacman object instead of Installer's pacstrap method * fix: break after first sync * fix: keep old build script for now * use nullish operator for base_packages and disk_encryption of Installer * feat: use shutil.which instead of rolling our own implementation * fix: check for binary only if list is not empty * fix: import Enum and fix mypy errors * refactor: use nullish operator for default values * refactor: linear search for key in Installer._trace_log only once * fix: use logs instead of the entirety of self._trace_log when searching for key * refactor: do not copy slice of bytes for search * refactor: use rfind only once to iterate over logs, do not raise ValueError in clear_vt100_escape_codes since TYPE_CHECKING will take care of it. * refactor: try decoding trace log before falling back to strigification * refactor: use an empty dict as default for callbacks in SysCommand.__init__ * refactor: use nullish or operator for slice start and end when not specified * refactor: use nullish or operator for SysCommand session * refactor: use pre-existing decode method in __repr__ for SysCommand * fix: overindentation * fix: use shallow copy of callbacks to prevent mutating the key-value relationships of the argument dict * refactor: use truthy value of self.session is not None for json encoding SysCommand * refactor: directly assign to SysCommand.session in create_session since it short circuits to True if already present * refactor: use dict.items() instead of manually retrieving the value using the key * refactor: user_config_to_json method sounds pretty self explanatory * refactor: store path validity as boolean for return * refactor: use pathlib.Path.write_text to write configs to destinations * fix: cannot use assignment expressions with expression * fix: use config_output.save for saving both config and creds * refactor: switch dictionary keys and values for options to avoid redundancy * refactor: use itertools.takewhile to collect locale.gen entries until the empty line * refactor: use iterative approach for nvidia driver fix * refactor: install packages if not nvidia * refactor: return early if no profile is selected * refactor: use strip to remove commented lines * fix: install additional packages only when we have a driver * fix: path with one command is matched as relative to '.' * fix: remove translation for debug log --------- Co-authored-by: Anton Hvornum --- archinstall/lib/profile/profiles_handler.py | 66 ++++++++++++++--------------- 1 file changed, 32 insertions(+), 34 deletions(-) (limited to 'archinstall/lib/profile') diff --git a/archinstall/lib/profile/profiles_handler.py b/archinstall/lib/profile/profiles_handler.py index 4e7c3d2b..74c21824 100644 --- a/archinstall/lib/profile/profiles_handler.py +++ b/archinstall/lib/profile/profiles_handler.py @@ -98,14 +98,19 @@ class ProfileHandler: profile = self.get_profile_by_name(main) if main else None valid: List[Profile] = [] + details: List[str] = profile_config.get('details', []) + if details: + valid = [] + invalid = [] - if details := profile_config.get('details', []): - resolved = {detail: self.get_profile_by_name(detail) for detail in details if detail} - valid = [p for p in resolved.values() if p is not None] - invalid = ', '.join([k for k, v in resolved.items() if v is None]) + for detail in filter(None, details): + if profile := self.get_profile_by_name(detail): + valid.append(profile) + else: + invalid.append(detail) if invalid: - info(f'No profile definition found: {invalid}') + info('No profile definition found: {}'.format(', '.join(invalid))) custom_settings = profile_config.get('custom_settings', {}) for profile in valid: @@ -123,14 +128,12 @@ class ProfileHandler: """ List of all available default_profiles """ - if self._profiles is None: - self._profiles = self._find_available_profiles() + self._profiles = self._profiles or self._find_available_profiles() return self._profiles @cached_property def _local_mac_addresses(self) -> List[str]: - ifaces = list_interfaces() - return list(ifaces.keys()) + return list(list_interfaces()) def add_custom_profiles(self, profiles: Union[TProfile, List[TProfile]]): if not isinstance(profiles, list): @@ -190,25 +193,20 @@ class ProfileHandler: def install_gfx_driver(self, install_session: 'Installer', driver: Optional[GfxDriver]): try: - driver_pkgs = driver.packages() if driver else [] - pkg_names = [p.value for p in driver_pkgs] - additional_pkg = ' '.join(['xorg-server', 'xorg-xinit'] + pkg_names) if driver is not None: - # Find the intersection between the set of known nvidia drivers - # and the selected driver packages. Since valid intesections can - # only have one element or none, we iterate and try to take the - # first element. - if driver_pkg := next(iter({GfxPackage.Nvidia, GfxPackage.NvidiaOpen} & set(driver_pkgs)), None): - if any(kernel in install_session.base_packages for kernel in ("linux-lts", "linux-zen")): - for kernel in install_session.kernels: - # Fixes https://github.com/archlinux/archinstall/issues/585 - install_session.add_additional_packages(f"{kernel}-headers") + driver_pkgs = driver.packages() + pkg_names = [p.value for p in driver_pkgs] + for driver_pkg in {GfxPackage.Nvidia, GfxPackage.NvidiaOpen} & set(driver_pkgs): + for kernel in {"linux-lts", "linux-zen"} & set(install_session.kernels): + # Fixes https://github.com/archlinux/archinstall/issues/585 + install_session.add_additional_packages(f"{kernel}-headers") # I've had kernel regen fail if it wasn't installed before nvidia-dkms - install_session.add_additional_packages(['dkms', 'xorg-server', 'xorg-xinit', f'{driver_pkg}-dkms']) - return - elif 'amdgpu' in driver_pkgs: + install_session.add_additional_packages(['dkms', 'xorg-server', 'xorg-xinit', f'{driver_pkg.value}-dkms']) + # Return after first driver match, since it is impossible to use both simultaneously. + return + if 'amdgpu' in driver_pkgs: # The order of these two are important if amdgpu is installed #808 if 'amdgpu' in install_session.modules: install_session.modules.remove('amdgpu') @@ -218,23 +216,24 @@ class ProfileHandler: install_session.modules.remove('radeon') install_session.modules.append('radeon') - install_session.add_additional_packages(additional_pkg) + install_session.add_additional_packages(pkg_names) except Exception as err: warn(f"Could not handle nvidia and linuz-zen specific situations during xorg installation: {err}") # Prep didn't run, so there's no driver to install - install_session.add_additional_packages(['xorg-server', 'xorg-xinit']) + install_session.add_additional_packages(['xorg-server', 'xorg-xinit']) def install_profile_config(self, install_session: 'Installer', profile_config: ProfileConfiguration): profile = profile_config.profile - if profile: - profile.install(install_session) + if not profile: + return - if profile and profile_config.gfx_driver: - if profile.is_xorg_type_profile() or profile.is_desktop_type_profile(): - self.install_gfx_driver(install_session, profile_config.gfx_driver) + profile.install(install_session) - if profile and profile_config.greeter: + if profile_config.gfx_driver and (profile.is_xorg_type_profile() or profile.is_desktop_type_profile()): + self.install_gfx_driver(install_session, profile_config.gfx_driver) + + if profile_config.greeter: self.install_greeter(install_session, profile_config.greeter) def _import_profile_from_url(self, url: str): @@ -312,8 +311,7 @@ class ProfileHandler: debug(f'Importing profile: {file}') try: - spec = importlib.util.spec_from_file_location(name, file) - if spec is not None: + if spec := importlib.util.spec_from_file_location(name, file): imported = importlib.util.module_from_spec(spec) if spec.loader is not None: spec.loader.exec_module(imported) -- cgit v1.2.3-70-g09d2