From 2fabbe5ff4f922ed7de7dce1a28cb203b05d0173 Mon Sep 17 00:00:00 2001 From: codefiles <11915375+codefiles@users.noreply.github.com> Date: Wed, 5 Oct 2022 20:35:08 -0400 Subject: [PATCH] Rework system locale --- archinstall/lib/installer.py | 5 +- archinstall/lib/locale_helpers.py | 83 ++++++++++--------- .../lib/user_interaction/locale_conf.py | 2 +- 3 files changed, 46 insertions(+), 44 deletions(-) diff --git a/archinstall/lib/installer.py b/archinstall/lib/installer.py index c3e4afe827..1576e1f03c 100644 --- a/archinstall/lib/installer.py +++ b/archinstall/lib/installer.py @@ -441,10 +441,7 @@ def set_hostname(self, hostname: str, *args :str, **kwargs :str) -> None: def set_locale(self, locale :str, encoding :str = 'UTF-8', *args :str, **kwargs :str) -> bool: locales = [Locale(locale, encoding)] - if LocaleUtils(locales, self.target).run(): - return True - - return False + return LocaleUtils(locales, self.target).run() def set_timezone(self, zone :str, *args :str, **kwargs :str) -> bool: if not zone: diff --git a/archinstall/lib/locale_helpers.py b/archinstall/lib/locale_helpers.py index 53912025d4..28cd6403ac 100644 --- a/archinstall/lib/locale_helpers.py +++ b/archinstall/lib/locale_helpers.py @@ -70,14 +70,11 @@ def __repr__(self) -> str: def __eq__(self, other) -> bool: # Locale names are not checked for a match since they can differ and still be the same locale. # Encodings are formatted (no dashes and lowercase) before comparison since encodings in the list of generated locales are in this format. - if ( + return ( self.language == other.language and self.encoding.replace('-', '').lower() == other.encoding.replace('-', '').lower() and self.modifier == other.modifier - ): - return True - - return False + ) def __lt__(self, other) -> bool: return self.str < other.str @@ -91,7 +88,7 @@ def __init__(self, locales: List[Locale] = [], target: str = ''): :param locales: A list of locales, the first locale is intended as the system locale. :type locales: List[Locale] - :param target: The installation mount point, if omitted default to the local system. + :param target: An installation mount point, if omitted default to the local system. :type target: str """ self.locales = locales @@ -116,7 +113,7 @@ def list_supported(self) -> List[Locale]: def verify_locales(self) -> bool: """ Check if the locales match supported locales. - If a match is found then update the name of the locale to the name of the matching entry in case they differ. + If a match is found update the name of the locale to the name of the matching entry if they differ. :return: If matched return True else False. :rtype: bool @@ -128,7 +125,8 @@ def verify_locales(self) -> bool: found = False for entry in supported: if locale == entry: - locale.name = entry.name + if locale.name != entry.name: + locale.name = entry.name found = True break @@ -166,14 +164,12 @@ def match_uncommented(self) -> bool: :return: If matched return True else False. :rtype: bool """ - if sorted(self.locales) == sorted(self.list_uncommented()): - return True - - return False + return sorted(self.locales) == sorted(self.list_uncommented()) def uncomment(self) -> bool: """ - Uncomment the locales matching entries in the locale-gen configuration file and comment all other entries. + Uncomment entries in the locale-gen configuration file. + Comment all other uncommented entries and append the locales that do not match entries. :return: If updated return True else False. :rtype: bool @@ -193,7 +189,7 @@ def uncomment(self) -> bool: if entry[0] != '#': contents[index] = f'#{contents[index]}' - # Uncomment the locales matching entries. + # Uncomment entries that match with a locale. for locale in self.locales: index = 0 @@ -208,7 +204,7 @@ def uncomment(self) -> bool: locales.remove(locale) break - # Append locales that are supported but did not match entries. + # Append the locales that did not match entries. for locale in locales: contents.append(f'{locale.name} {locale.encoding}\n') @@ -220,7 +216,7 @@ def uncomment(self) -> bool: log(f"Permission denied to write to the locale-gen configuration file: '{self.locale_gen}'", fg="red", level=logging.ERROR) return False - log(f'Uncommented entries in locale-gen configuration file', level=logging.INFO) + log('Uncommented entries in locale-gen configuration file', level=logging.INFO) for locale in self.list_uncommented(): log(f' {locale.name} {locale.encoding}', level=logging.INFO) @@ -240,10 +236,13 @@ def list_generated(self) -> List[Locale]: if self.target: command = f'/usr/bin/arch-chroot {self.target} {command}' - for line in SysCommand(command).decode().split(): - # Eliminate duplicates by filtering out names that do not contain an encoding. - if '.' in line: - generated.append(Locale(line)) + if (output := SysCommand(command)).exit_code != 0: + log(f"Failed to get list of generated locales: '{output}'", fg="red", level=logging.ERROR) + else: + for line in output.decode('UTF-8').split(): + # Eliminate duplicates by filtering out names that do not contain an encoding. + if '.' in line: + generated.append(Locale(line)) return generated @@ -254,10 +253,7 @@ def match_generated(self) -> bool: :return: If matched return True else False. :rtype: bool """ - if sorted(self.locales) == self.list_generated(): - return True - - return False + return sorted(self.locales) == self.list_generated() def remove_generated(self) -> bool: """ @@ -282,25 +278,27 @@ def generate(self) -> bool: :return: If generated return True else False. :rtype: bool """ - # Before installing the locale check if the locale archive already exists and remove it if it does. + # Before generating locales remove the locale archive if it already exists. if not self.remove_generated(): return False - log(f'Generating locales...', level=logging.INFO) + command = 'localedef -i {} -c -f {} -A /usr/share/locale/locale.alias {}' - for locale in self.list_uncommented(): - command = f'localedef -i {locale.language} -c -f {locale.encoding} -A /usr/share/locale/locale.alias {locale.name}' + if self.target: + command = f'/usr/bin/arch-chroot {self.target} ' + command - if self.target: - command = f'/usr/bin/arch-chroot {self.target} {command}' + log('Generating locales...', level=logging.INFO) + + for locale in self.list_uncommented(): + formatted_command = command.format(locale.language, locale.encoding, locale.name) log(f' {locale}...', level=logging.INFO) - if (output := SysCommand(command)).exit_code != 0: + if (output := SysCommand(formatted_command)).exit_code != 0: log(f'Failed to generate locale: {output}', fg='red', level=logging.ERROR) return False - log(f'Generation complete.', level=logging.INFO) + log('Generation complete.', level=logging.INFO) return True def get_system_locale(self) -> str: @@ -318,7 +316,7 @@ def get_system_locale(self) -> str: else: # Set up a regular expression pattern of a line beginning with 'LANG=' # followed by and ending in a locale in optional double quotes. - pattern = re.compile(rf'^LANG="?(.+?)"?$') + pattern = re.compile(r'^LANG="?(.+?)"?$') for line in lines: if (match_obj := pattern.match(line)) is not None: @@ -363,7 +361,7 @@ def set_system_locale(self) -> bool: log(f"Permission denied to write to the locale configuration file: '{self.locale_conf}'", fg="red", level=logging.ERROR) return False - log(f"System locale set to {locale.name}", level=logging.INFO) + log(f'System locale set to {locale.name}', level=logging.INFO) return True def run(self) -> bool: @@ -374,7 +372,7 @@ def run(self) -> bool: :rtype: bool """ if not len(self.locales): - log(f"No locales to generate or to set as the system locale.", fg="yellow", level=logging.WARNING) + log('No locales to generate or to set as the system locale.', fg='yellow', level=logging.WARNING) return True if not self.verify_locales(): @@ -396,6 +394,14 @@ def run(self) -> bool: def list_locales(target: str = '') -> List[str]: + """ + Get a list of locales. + + :param target: An installation mount point, if omitted default to the local system. + :type target: str + :return: A list of locales. + :rtype: List[str] + """ supported = f'{target}/usr/share/i18n/SUPPORTED' try: @@ -403,10 +409,9 @@ def list_locales(target: str = '') -> List[str]: locales = fh.readlines() except FileNotFoundError: log(f"Supported locale file not found: '{supported}'", fg="red", level=logging.ERROR) - return None - - # Remove C.UTF-8 since it is provided by the glibc package. - locales.remove('C.UTF-8 UTF-8\n') + else: + # Remove C.UTF-8 since it is provided by the glibc package. + locales.remove('C.UTF-8 UTF-8\n') return locales diff --git a/archinstall/lib/user_interaction/locale_conf.py b/archinstall/lib/user_interaction/locale_conf.py index 5457c33883..2ea0546dfe 100644 --- a/archinstall/lib/user_interaction/locale_conf.py +++ b/archinstall/lib/user_interaction/locale_conf.py @@ -12,7 +12,7 @@ def select_locale_lang(preset: str = None) -> str: locales = list_locales() - locale_lang = set([locale.split('.', 1)[0] if '.' in locale else locale.split()[0] for locale in locales]) + locale_lang = set([locale.split('.')[0] if '.' in locale else locale.split()[0] for locale in locales]) selected_locale = Menu( _('Choose which locale language to use'),