source: calamares/trunk/fuentes/src/modules/localecfg/main.py @ 7538

Last change on this file since 7538 was 7538, checked in by kbut, 13 months ago

sync with github

File size: 3.3 KB
Line 
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3#
4# === This file is part of Calamares - <https://github.com/calamares> ===
5#
6#   Copyright 2014, Anke Boersma <demm@kaosx.us>
7#   Copyright 2015, Philip Müller <philm@manjaro.org>
8#   Copyright 2016, Teo Mrnjavac <teo@kde.org>
9#
10#   Calamares is free software: you can redistribute it and/or modify
11#   it under the terms of the GNU General Public License as published by
12#   the Free Software Foundation, either version 3 of the License, or
13#   (at your option) any later version.
14#
15#   Calamares is distributed in the hope that it will be useful,
16#   but WITHOUT ANY WARRANTY; without even the implied warranty of
17#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18#   GNU General Public License for more details.
19#
20#   You should have received a copy of the GNU General Public License
21#   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
22
23import os
24import shutil
25
26import libcalamares
27
28
29def run():
30    """ Create locale """
31    en_us_locale = 'en_US.UTF-8'
32    locale_conf = libcalamares.globalstorage.value("localeConf")
33
34    if not locale_conf:
35        locale_conf = {
36            'LANG': 'en_US.UTF-8',
37            'LC_NUMERIC': 'en_US.UTF-8',
38            'LC_TIME': 'en_US.UTF-8',
39            'LC_MONETARY': 'en_US.UTF-8',
40            'LC_PAPER': 'en_US.UTF-8',
41            'LC_NAME': 'en_US.UTF-8',
42            'LC_ADDRESS': 'en_US.UTF-8',
43            'LC_TELEPHONE': 'en_US.UTF-8',
44            'LC_MEASUREMENT': 'en_US.UTF-8',
45            'LC_IDENTIFICATION': 'en_US.UTF-8'
46        }
47
48    install_path = libcalamares.globalstorage.value("rootMountPoint")
49
50    # restore backup if available
51    if os.path.exists('/etc/locale.gen.bak'):
52        shutil.copy2("{!s}/etc/locale.gen.bak".format(install_path),
53                     "{!s}/etc/locale.gen".format(install_path))
54
55    # run locale-gen if detected
56    if os.path.exists('/etc/locale.gen'):
57        text = []
58
59        with open("{!s}/etc/locale.gen".format(install_path), "r") as gen:
60            text = gen.readlines()
61
62        # we want unique values, so locale_values should have 1 or 2 items
63        locale_values = set(locale_conf.values())
64
65        with open("{!s}/etc/locale.gen".format(install_path), "w") as gen:
66            for line in text:
67                # always enable en_US
68                if line.startswith("#" + en_us_locale):
69                    # uncomment line
70                    line = line[1:].lstrip()
71
72                for locale_value in locale_values:
73                    if line.startswith("#" + locale_value):
74                        # uncomment line
75                        line = line[1:].lstrip()
76
77                gen.write(line)
78
79        libcalamares.utils.target_env_call(['locale-gen'])
80        print('locale.gen done')
81
82    # write /etc/locale.conf
83    locale_conf_path = os.path.join(install_path, "etc/locale.conf")
84    with open(locale_conf_path, "w") as lcf:
85        for k, v in locale_conf.items():
86            lcf.write("{!s}={!s}\n".format(k, v))
87
88    # write /etc/default/locale if /etc/default exists and is a dir
89    etc_default_path = os.path.join(install_path, "etc/default")
90    if os.path.isdir(etc_default_path):
91        with open(os.path.join(etc_default_path, "locale"), "w") as edl:
92            for k, v in locale_conf.items():
93                edl.write("{!s}={!s}\n".format(k, v))
94
95    return None
Note: See TracBrowser for help on using the repository browser.