bitbake: bin/utils: Ensure locale en_US.UTF-8 is available on the system

Get rid of the duplicate code and add extra check that the
locale en_US.UTF-8 is available on the system. This new helper
method is now located right above the method filter_environment()
which sets LC_ALL environment variable to 'en_US.UTF-8'.

[YOCTO #10165]

(Bitbake rev: a4ce040a6fd540a1cac52f808f909f9fcf8c961c)

Signed-off-by: Frank de Brabander <debrabander@gmail.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
This commit is contained in:
Frank de Brabander
2022-12-06 19:18:05 +01:00
committed by Richard Purdie
parent 2015bf3eb8
commit 79d689f5da
4 changed files with 21 additions and 6 deletions

View File

@@ -25,8 +25,7 @@ except RuntimeError as exc:
from bb import cookerdata from bb import cookerdata
from bb.main import bitbake_main, BitBakeConfigParameters, BBMainException from bb.main import bitbake_main, BitBakeConfigParameters, BBMainException
if sys.getfilesystemencoding() != "utf-8": bb.utils.check_system_locale()
sys.exit("Please use a locale setting which supports UTF-8 (such as LANG=en_US.UTF-8).\nPython can't change the filesystem locale after loading so we need a UTF-8 when Python starts or things won't work.")
__version__ = "2.2.0" __version__ = "2.2.0"

View File

@@ -12,8 +12,9 @@ warnings.simplefilter("default")
import logging import logging
sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(sys.argv[0])), 'lib')) sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(sys.argv[0])), 'lib'))
if sys.getfilesystemencoding() != "utf-8": import bb
sys.exit("Please use a locale setting which supports UTF-8 (such as LANG=en_US.UTF-8).\nPython can't change the filesystem locale after loading so we need a UTF-8 when Python starts or things won't work.")
bb.utils.check_system_locale()
# Users shouldn't be running this code directly # Users shouldn't be running this code directly
if len(sys.argv) != 11 or not sys.argv[1].startswith("decafbad"): if len(sys.argv) != 11 or not sys.argv[1].startswith("decafbad"):

View File

@@ -24,8 +24,7 @@ import subprocess
from multiprocessing import Lock from multiprocessing import Lock
from threading import Thread from threading import Thread
if sys.getfilesystemencoding() != "utf-8": bb.utils.check_system_locale()
sys.exit("Please use a locale setting which supports UTF-8 (such as LANG=en_US.UTF-8).\nPython can't change the filesystem locale after loading so we need a UTF-8 when Python starts or things won't work.")
# Users shouldn't be running this code directly # Users shouldn't be running this code directly
if len(sys.argv) != 2 or not sys.argv[1].startswith("decafbad"): if len(sys.argv) != 2 or not sys.argv[1].startswith("decafbad"):

View File

@@ -13,6 +13,7 @@ import errno
import logging import logging
import bb import bb
import bb.msg import bb.msg
import locale
import multiprocessing import multiprocessing
import fcntl import fcntl
import importlib import importlib
@@ -608,6 +609,21 @@ def preserved_envvars():
] ]
return v + preserved_envvars_exported() return v + preserved_envvars_exported()
def check_system_locale():
"""Make sure the required system locale are available and configured"""
default_locale = locale.getlocale(locale.LC_CTYPE)
try:
locale.setlocale(locale.LC_CTYPE, ("en_US", "UTF-8"))
except:
sys.exit("Please make sure locale 'en_US.UTF-8' is available on your system")
else:
locale.setlocale(locale.LC_CTYPE, default_locale)
if sys.getfilesystemencoding() != "utf-8":
sys.exit("Please use a locale setting which supports UTF-8 (such as LANG=en_US.UTF-8).\n"
"Python can't change the filesystem locale after loading so we need a UTF-8 when Python starts or things won't work.")
def filter_environment(good_vars): def filter_environment(good_vars):
""" """
Create a pristine environment for bitbake. This will remove variables that Create a pristine environment for bitbake. This will remove variables that