#!/usr/bin/python3
"""
Set system language.
Sets the system language to the given `language`, which must be a valid locale
identifier, like "en_US.UTF-8".
Removes `/etc/locale.conf` and then uses `systemd-firstboot` from the buildhost,
with the `--locale` flag, which will write a new `/etc/locale.conf` in the
target system with `LANG={language}`.
"""
import json
import subprocess
import sys
import os
SCHEMA = """
"additionalProperties": false,
"required": ["language"],
"properties": {
"language": {
"type": "string",
"description": "Locale identifier (like 'en_US.UTF-8') for system LANG"
}
}
"""
def main(tree, options):
language = options["language"]
# We need to remove the /etc/locale.conf file first, because it is created while we install RPM packages.
# systemd-firstboot expects that if /etc/locale.conf exists it is a user-defined value and does not change it, but
# the assumption is wrong, because it contains a default value from RPM package.
try:
os.remove(f"{tree}/etc/locale.conf")
print("/etc/locale.conf already exists. Replacing.")
except FileNotFoundError:
pass
subprocess.run(["systemd-firstboot", f"--root={tree}", f"--locale={language}"], check=True)
if __name__ == '__main__':
args = json.load(sys.stdin)
r = main(args["tree"], args["options"])
sys.exit(r)