calamares/src/modules/localecfg/main.py

80 lines
2.7 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
2015-02-18 15:06:10 +01:00
# -*- coding: utf-8 -*-
#
# === This file is part of Calamares - <http://github.com/calamares> ===
#
# Copyright 2014, Anke Boersma <demm@kaosx.us>
2015-02-17 14:38:30 +01:00
# Copyright 2015, Philip Müller <philm@manjaro.org>
#
# Calamares is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Calamares is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Calamares. If not, see <http://www.gnu.org/licenses/>.
import os
import shutil
import libcalamares
def run():
""" Create locale """
2016-05-30 16:14:26 +02:00
en_us_locale = '#en_US'
2014-11-26 18:15:39 +01:00
locale = libcalamares.globalstorage.value("lcLocale")
2015-06-14 13:04:52 +02:00
if not locale:
locale = 'en_US.UTF-8 UTF-8'
install_path = libcalamares.globalstorage.value("rootMountPoint")
# restore backup if available
if os.path.exists('/etc/locale.gen.bak'):
shutil.copy2("{!s}/etc/locale.gen.bak".format(install_path),
2015-02-17 14:38:30 +01:00
"{!s}/etc/locale.gen".format(install_path))
# run locale-gen if detected
if os.path.exists('/etc/locale.gen'):
text = []
2015-06-14 13:04:52 +02:00
2015-02-17 14:38:30 +01:00
with open("{!s}/etc/locale.gen".format(install_path), "r") as gen:
text = gen.readlines()
# always enable en_US
2015-02-17 14:38:30 +01:00
with open("{!s}/etc/locale.gen".format(install_path), "w") as gen:
for line in text:
2016-05-30 16:14:26 +02:00
if en_us_locale in line and line[0] == "#":
# uncomment line
2016-05-27 18:18:10 +02:00
line = line[1:].lstrip()
2015-06-14 13:04:52 +02:00
if locale in line and line[0] == "#":
# uncomment line
2016-05-27 18:18:10 +02:00
line = line[1:].lstrip()
2015-06-14 13:04:52 +02:00
gen.write(line)
libcalamares.utils.target_env_call(['locale-gen'])
print('locale.gen done')
2016-05-30 16:14:26 +02:00
# write /etc/locale.conf
locale_conf_path = os.path.join(install_path, "etc/locale.conf")
with open(locale_conf_path, "w") as locale_conf:
2015-02-17 14:38:30 +01:00
locale_split = locale.split(' ')[0]
locale_conf.write("LANG={!s}\n".format(locale_split))
2016-05-30 16:14:26 +02:00
# write /etc/default/locale if /etc/default exists and is a dir
etc_default_path = os.path.join(install_path, "etc/default")
if os.path.isdir(etc_default_path):
with open(os.path.join(etc_default_path, "locale"), "w") as etc_default_locale:
locale_split = locale.split(' ')[0]
2016-05-30 17:10:18 +02:00
etc_default_locale.write("LANG={!s}\n".format(locale_split))
2016-05-30 16:14:26 +02:00
return None