Skip to content

Commit

Permalink
Rework system locale
Browse files Browse the repository at this point in the history
  • Loading branch information
codefiles committed Oct 6, 2022
1 parent 7314c2b commit 2fabbe5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 44 deletions.
5 changes: 1 addition & 4 deletions archinstall/lib/installer.py
Expand Up @@ -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:
Expand Down
83 changes: 44 additions & 39 deletions archinstall/lib/locale_helpers.py
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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')

Expand All @@ -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)
Expand All @@ -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

Expand All @@ -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:
"""
Expand All @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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():
Expand All @@ -396,17 +394,24 @@ 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:
with open(supported, 'r') as fh:
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

Expand Down
2 changes: 1 addition & 1 deletion archinstall/lib/user_interaction/locale_conf.py
Expand Up @@ -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'),
Expand Down

0 comments on commit 2fabbe5

Please sign in to comment.