# HG changeset patch # User Thomas De Schampheleire # Date 1561405322 -7200 # Node ID d4bcbe1b06f4431212ea7bf3fd84b6ac8ea0426c # Parent f2900ebaac0d98c0310a4f6494c70a60d0c1b4e7 lib/locale: move locale check to separate file Create kallithea.lib.locale to group locale-related wrappers/checks. As a first step, move existing locale-related logic into it. A subsequent commit will add more. diff -r f2900ebaac0d -r d4bcbe1b06f4 kallithea/config/app_cfg.py --- a/kallithea/config/app_cfg.py Mon Jun 24 19:42:02 2019 +0200 +++ b/kallithea/config/app_cfg.py Mon Jun 24 21:42:02 2019 +0200 @@ -30,6 +30,7 @@ from sqlalchemy import create_engine import mercurial +import kallithea.lib.locale from kallithea.lib.middleware.permanent_repo_url import PermanentRepoUrl from kallithea.lib.middleware.https_fixup import HttpsFixup from kallithea.lib.middleware.simplegit import SimpleGit @@ -121,19 +122,7 @@ def setup_configuration(app): config = app.config - # Verify that things work when Dulwich passes unicode paths to the file system layer. - # Note: UTF-8 is preferred, but for example ISO-8859-1 or mbcs should also work under the right cirumstances. - try: - u'\xe9'.encode(sys.getfilesystemencoding()) # Test using é (é) - except UnicodeEncodeError: - log.error("Cannot encode Unicode paths to file system encoding %r", sys.getfilesystemencoding()) - for var in ['LC_ALL', 'LC_CTYPE', 'LANG']: - if var in os.environ: - val = os.environ[var] - log.error("Note: Environment variable %s is %r - perhaps change it to some other value from 'locale -a', like 'C.UTF-8' or 'en_US.UTF-8'", var, val) - break - else: - log.error("Note: No locale setting found in environment variables - perhaps set LC_CTYPE to some value from 'locale -a', like 'C.UTF-8' or 'en_US.UTF-8'") + if not kallithea.lib.locale.current_locale_is_valid(): log.error("Terminating ...") sys.exit(1) diff -r f2900ebaac0d -r d4bcbe1b06f4 kallithea/lib/locale.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/kallithea/lib/locale.py Mon Jun 24 21:42:02 2019 +0200 @@ -0,0 +1,37 @@ +# -*- coding: utf-8 -*- +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +import logging +import os +import sys + +log = logging.getLogger(__name__) + +def current_locale_is_valid(): + """Verify that things work when Dulwich passes unicode paths to the file system layer. + + Note: UTF-8 is preferred, but for example ISO-8859-1 or mbcs should also + work under the right circumstances.""" + try: + u'\xe9'.encode(sys.getfilesystemencoding()) # Test using é (é) + except UnicodeEncodeError: + log.error("Cannot encode Unicode paths to file system encoding %r", sys.getfilesystemencoding()) + for var in ['LC_ALL', 'LC_CTYPE', 'LANG']: + if var in os.environ: + val = os.environ[var] + log.error("Note: Environment variable %s is %r - perhaps change it to some other value from 'locale -a', like 'C.UTF-8' or 'en_US.UTF-8'", var, val) + break + else: + log.error("Note: No locale setting found in environment variables - perhaps set LC_CTYPE to some value from 'locale -a', like 'C.UTF-8' or 'en_US.UTF-8'") + return False + return True