2024-05-17 23:04:34 +08:00
|
|
|
import os
|
|
|
|
from logging import getLogger
|
|
|
|
from builder.build import filesystem
|
|
|
|
from builder.lib.context import ArchBuilderContext
|
|
|
|
from builder.lib.config import ArchBuilderConfigError
|
|
|
|
from builder.lib.utils import open_config
|
|
|
|
log = getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def reset_locale(ctx: ArchBuilderContext):
|
2024-05-20 09:56:42 +08:00
|
|
|
"""
|
|
|
|
Remove old locale settings
|
|
|
|
"""
|
2024-05-17 23:04:34 +08:00
|
|
|
root = ctx.get_rootfs()
|
|
|
|
archive = os.path.join(root, "usr/lib/locale/locale-archive")
|
|
|
|
if os.path.exists(archive): os.remove(archive)
|
|
|
|
|
|
|
|
|
|
|
|
def enable_all(ctx: ArchBuilderContext):
|
2024-05-20 09:56:42 +08:00
|
|
|
"""
|
|
|
|
Add all enabled locale for build locale-archive
|
|
|
|
"""
|
2024-05-17 23:04:34 +08:00
|
|
|
root = ctx.get_rootfs()
|
2024-05-20 09:56:42 +08:00
|
|
|
|
|
|
|
# default none
|
2024-05-17 23:04:34 +08:00
|
|
|
locales = ctx.get("locale.enable", [])
|
|
|
|
log.info("setup enabled locale")
|
2024-05-20 09:56:42 +08:00
|
|
|
|
|
|
|
# create locale.gen
|
2024-05-17 23:04:34 +08:00
|
|
|
file = os.path.join(root, "etc/locale.gen")
|
|
|
|
with open_config(file) as f:
|
|
|
|
for line in locales:
|
|
|
|
log.debug(f"adding locale {line}")
|
|
|
|
f.write(line)
|
2024-05-20 09:56:42 +08:00
|
|
|
f.write(os.linesep)
|
2024-05-17 23:04:34 +08:00
|
|
|
if len(locales) == 0:
|
|
|
|
f.write("# No any locales enabled\n")
|
2024-05-20 09:56:42 +08:00
|
|
|
|
|
|
|
# run locale-gen
|
2024-05-17 23:04:34 +08:00
|
|
|
filesystem.chroot_run(ctx, "locale-gen")
|
|
|
|
|
|
|
|
|
|
|
|
def set_default(ctx: ArchBuilderContext):
|
2024-05-20 09:56:42 +08:00
|
|
|
"""
|
|
|
|
Setup default locale
|
|
|
|
"""
|
2024-05-17 23:04:34 +08:00
|
|
|
root = ctx.get_rootfs()
|
2024-05-20 09:56:42 +08:00
|
|
|
|
|
|
|
# default to C
|
|
|
|
default = ctx.get("locale.default", "C")
|
2024-05-17 23:04:34 +08:00
|
|
|
log.info(f"default locale: {default}")
|
2024-05-20 09:56:42 +08:00
|
|
|
|
|
|
|
# default locale config (see man:locale.conf(5))
|
2024-05-17 23:04:34 +08:00
|
|
|
conf = os.path.join(root, "etc/locale.conf")
|
|
|
|
with open_config(conf) as f:
|
|
|
|
f.write(f"LANG={default}\n")
|
|
|
|
|
|
|
|
|
|
|
|
def set_timezone(ctx: ArchBuilderContext):
|
2024-05-20 09:56:42 +08:00
|
|
|
"""
|
|
|
|
Setup tzdata timezone info
|
|
|
|
"""
|
2024-05-17 23:04:34 +08:00
|
|
|
root = ctx.get_rootfs()
|
2024-05-20 09:56:42 +08:00
|
|
|
|
|
|
|
# default to UTC
|
|
|
|
timezone = ctx.get("timezone", "UTC")
|
2024-05-17 23:04:34 +08:00
|
|
|
log.info(f"timezone: {timezone}")
|
2024-05-20 09:56:42 +08:00
|
|
|
|
|
|
|
# tzdata install path
|
2024-05-17 23:04:34 +08:00
|
|
|
dst = os.path.join("/usr/share/zoneinfo", timezone)
|
|
|
|
real = os.path.join(root, dst[1:])
|
|
|
|
if not os.path.exists(real): raise ArchBuilderConfigError(
|
|
|
|
f"timezone {timezone} not found"
|
|
|
|
)
|
2024-05-20 09:56:42 +08:00
|
|
|
|
|
|
|
# localtime symbolic link (see man:tzset(3))
|
2024-05-17 23:04:34 +08:00
|
|
|
lnk = os.path.join(root, "etc/localtime")
|
|
|
|
if os.path.exists(lnk): os.remove(lnk)
|
|
|
|
os.symlink(dst, lnk)
|
2024-05-20 09:56:42 +08:00
|
|
|
|
|
|
|
# timezone file
|
2024-05-17 23:04:34 +08:00
|
|
|
conf = os.path.join(root, "etc/timezone")
|
|
|
|
with open(conf, "w") as f:
|
|
|
|
f.write(timezone)
|
|
|
|
f.write(os.linesep)
|
|
|
|
|
|
|
|
|
|
|
|
def proc_locale(ctx: ArchBuilderContext):
|
2024-05-20 09:56:42 +08:00
|
|
|
"""
|
|
|
|
Setup user locale settings
|
|
|
|
"""
|
2024-05-17 23:04:34 +08:00
|
|
|
reset_locale(ctx)
|
|
|
|
enable_all(ctx)
|
|
|
|
set_default(ctx)
|
|
|
|
set_timezone(ctx)
|