System locale manipulation wired into Subiquity.

- We don't have cloud-init in WSL.
- Subiquity must perform any actions to support the choosen locale.
This commit is contained in:
Carlos Nihelton 2021-11-01 19:07:35 -03:00
parent 46dfba39c7
commit 887d63caf0
1 changed files with 61 additions and 0 deletions

View File

@ -14,7 +14,9 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import logging import logging
import os
from subiquity.server.controllers.locale import LocaleController from subiquity.server.controllers.locale import LocaleController
from subiquitycore.utils import arun_command
log = logging.getLogger('system_setup.server.controllers.locale') log = logging.getLogger('system_setup.server.controllers.locale')
@ -30,3 +32,62 @@ class WSLLocaleController(LocaleController):
.format(self.model.selected_language)) .format(self.model.selected_language))
super().start() super().start()
async def _run_locale_support_cmds(self, lang: str):
""" Final commands to be ran when a valid language is POST'ed. """
env = os.environ.copy()
# Ensure minimal console translation is enabled.
cmds = (["locale-gen"],
["update-locale", "LANG={}".format(lang)],
["bash", "-c", "\"apt", "install", "$(check-language-support",
"-l", lang[0:2], ")\""])
if self.app.opts.dry_run:
for cmd in cmds:
log.debug('Would run: ' + ' '.join(cmd))
else:
for cmd in cmds:
cp = await arun_command(cmd, env=env)
if cp.returncode:
log.debug('Command \"%s\" failed with return code %d',
cp.args, cp.returncode)
async def POST(self, data: str):
if data == self.autoinstall_default or data == os.environ.get("LANG"):
await super().POST(data)
return
fileContents: str
fname = "locale.gen"
env = os.environ.copy()
if self.app.opts.dry_run:
# For debug purposes.
fname = ".subiquity/" + fname
await arun_command(['cp', '/etc/locale.gen', '.subiquity/'],
env=env)
else:
fname = "/etc/" + fname
pendingWrite = False
with open(fname, "r") as localeGen:
# locale.gen is not so big.
fileContents = localeGen.read()
lineFound = fileContents.find(data)
if lineFound == -1:
# An unsupported locale coming from our UI is a bug.
raise AssertionError("Selected language {} not supported."
" Rolling back.".format(data))
commented = "# {}".format(data)
lineFound = fileContents.find(commented)
if lineFound != -1:
fileContents = fileContents.replace(commented, data)
pendingWrite = True
if pendingWrite:
with open(fname, "wt") as f:
f.write(fileContents)
# If we arrive here, data is a supported language.
await self._run_locale_support_cmds(data)
await super().POST(data)