def _get_rnotes(self): # We first look for rnotes in paths containing the language, then in # directories without the language component. You know, just in case. paths = ["/tmp/updates/pixmaps/rnotes/", "/tmp/product/pixmaps/rnotes/", "/usr/share/anaconda/pixmaps/rnotes/"] all_lang_pixmaps = [] for path in paths: all_lang_pixmaps += self._do_globs(path + "/*") pixmap_langs = [pixmap.split(os.path.sep)[-2] for pixmap in all_lang_pixmaps] best_lang = find_best_locale_match(os.environ["LANG"], pixmap_langs) if not best_lang: # nothing found, try the default language best_lang = find_best_locale_match(DEFAULT_LANG, pixmap_langs) if not best_lang: # nothing found even for the default language, try non-localized rnotes non_localized = [] for path in paths: non_localized += self._do_globs(path) return non_localized best_lang_pixmaps = [] for path in paths: best_lang_pixmaps += self._do_globs(path + best_lang) return best_lang_pixmaps
def get_license_file_name(): """Get filename of the license file best matching current localization settings. :return: filename of the license file or None if no license file found :rtype: str or None """ all_eulas = glob.glob(LICENSE_FILE_GLOB) non_localized_eulas = [] langs = set() for eula in all_eulas: if "EULA_" in eula: # license file for a specific locale lang = eula.rsplit("EULA_", 1)[1] if lang: langs.add(lang) else: non_localized_eulas.append(eula) best_lang = find_best_locale_match(os.environ["LANG"], langs) if not best_lang: # nothing found for the current language, try the default one best_lang = find_best_locale_match(DEFAULT_LANG, langs) if not best_lang: # nothing found even for the default language, use non-localized or None if non_localized_eulas: best_eula = non_localized_eulas[0] else: return None else: # use first of the best-matching EULA files (there should be only one) best_eula = glob.glob(LICENSE_FILE_GLOB + ("_%s" % best_lang))[0] return best_eula
def _get_rnotes(self): # We first look for rnotes in paths containing the language, then in # directories without the language component. You know, just in case. paths = [ "/tmp/updates/pixmaps/rnotes/", "/tmp/product/pixmaps/rnotes/", "/usr/share/anaconda/pixmaps/rnotes/" ] all_lang_pixmaps = [] for path in paths: all_lang_pixmaps += self._do_globs(path + "/*") pixmap_langs = [ pixmap.split(os.path.sep)[-2] for pixmap in all_lang_pixmaps ] best_lang = find_best_locale_match(os.environ["LANG"], pixmap_langs) if not best_lang: # nothing found, try the default language best_lang = find_best_locale_match(DEFAULT_LANG, pixmap_langs) if not best_lang: # nothing found even for the default language, try non-localized rnotes non_localized = [] for path in paths: non_localized += self._do_globs(path) return non_localized best_lang_pixmaps = [] for path in paths: best_lang_pixmaps += self._do_globs(path + best_lang) return best_lang_pixmaps
def test_find_best_locale_match(self): """Finding best locale matches should work as expected.""" # can find best matches assert localization.find_best_locale_match( "cs_CZ", ["cs", "cs_CZ", "en", "en_US"]) == "cs_CZ" assert localization.find_best_locale_match( "cs", ["cs_CZ", "cs", "en", "en_US"]) == "cs" assert localization.find_best_locale_match("pt_BR", ["pt", "pt_BR"]) == "pt_BR" assert localization.find_best_locale_match( "pt_BR", ["pt", "pt_BR", "pt_PT"]) == "pt_BR" assert localization.find_best_locale_match("cs_CZ.UTF-8", ["cs", "cs_CZ", "cs_CZ.UTF-8"]) == \ "cs_CZ.UTF-8" assert localization.find_best_locale_match( "cs_CZ.UTF-8@latin", ["cs", "cs_CZ@latin", "cs_CZ.UTF-8"]) == "cs_CZ@latin" # no matches assert localization.find_best_locale_match("pt_BR", ["en_BR", "en"]) is None assert localization.find_best_locale_match("cs_CZ.UTF-8", ["en", "en.UTF-8"]) is None # nonsense assert localization.find_best_locale_match("ja", ["blah"]) is None assert localization.find_best_locale_match("blah", ["en_US.UTF-8"]) is None
def find_best_locale_match_test(self): """Finding best locale matches should work as expected.""" # can find best matches self.assertEqual( localization.find_best_locale_match( "cs_CZ", ["cs", "cs_CZ", "en", "en_US"]), "cs_CZ") self.assertEqual( localization.find_best_locale_match( "cs", ["cs_CZ", "cs", "en", "en_US"]), "cs") self.assertEqual( localization.find_best_locale_match("pt_BR", ["pt", "pt_BR"]), "pt_BR") self.assertEqual( localization.find_best_locale_match("pt_BR", ["pt", "pt_BR", "pt_PT"]), "pt_BR") self.assertEqual( localization.find_best_locale_match( "cs_CZ.UTF-8", ["cs", "cs_CZ", "cs_CZ.UTF-8"]), "cs_CZ.UTF-8") self.assertEqual( localization.find_best_locale_match( "cs_CZ.UTF-8@latin", ["cs", "cs_CZ@latin", "cs_CZ.UTF-8"]), "cs_CZ@latin") # no matches self.assertIsNone( localization.find_best_locale_match("pt_BR", ["en_BR", "en"])) self.assertIsNone( localization.find_best_locale_match("cs_CZ.UTF-8", ["en", "en.UTF-8"])) # nonsense self.assertIsNone(localization.find_best_locale_match("ja", ["blah"])) self.assertIsNone( localization.find_best_locale_match("blah", ["en_US.UTF-8"]))
def test_find_best_locale_match_posix(self): """Finding best POSIX matches should work as expected.""" match = localization.find_best_locale_match("C", ["C.UTF-8"]) self.assertEqual(match, "C.UTF-8") match = localization.find_best_locale_match("C.UTF-8", ["en_US"]) self.assertEqual(match, "en_US") match = localization.find_best_locale_match("en_US", ["C.UTF-8"]) self.assertEqual(match, None) match = localization.find_best_locale_match("cs_CZ", ["C.UTF-8"]) self.assertEqual(match, None)
def test_find_best_locale_match_posix(self): """Finding best POSIX matches should work as expected.""" match = localization.find_best_locale_match("C", ["C.UTF-8"]) assert match == "C.UTF-8" match = localization.find_best_locale_match("C.UTF-8", ["en_US"]) assert match == "en_US" match = localization.find_best_locale_match("en_US", ["C.UTF-8"]) assert match is None match = localization.find_best_locale_match("cs_CZ", ["C.UTF-8"]) assert match is None
def collect_language_requirements(dnf_base): """Collect requirements for supported languages. :param dnf_base: a DNF base :return: a list of requirements """ requirements = [] localization_proxy = LOCALIZATION.get_proxy() locales = [localization_proxy.Language] + localization_proxy.LanguageSupport # Find all available langpacks. packages = dnf_base.sack.query().available().filter(name__glob="langpacks-*") # Get all valid langcodes. codes = [p.name.split('-', 1)[1] for p in packages] codes = list(filter(is_valid_langcode, codes)) # Find the best langpacks to install. for locale in locales: best_locale = find_best_locale_match(locale, codes) if not best_locale: log.warning("Selected locale '%s' does not match " "any available langpacks.", locale) continue requirements.append(Requirement.for_package( package_name="langpacks-" + best_locale, reason="Required to support the locale '{}'.".format(locale) )) return requirements
def _get_best_help_file(help_folder, help_file): """ Return the path to the best help file for the current language and available help content :param str help_folder: a path to folder where we should look for the help files :param str help_file: name of the requested help file :return: path to the best help file or ``None`` is no match is found :rtype: str or NoneType """ current_lang = os.environ["LANG"] # list all available languages for the Anaconda help # * content is stored in folders named after the given language code # (en-US, cs-CZ, jp-JP, etc.) # * we check if the given folder contains the currently needed help file # and only consider it fit to use if it does have the file if not os.path.exists(help_folder): log.warning("help folder %s for help file %s does not exist", help_folder, help_file) return None help_langs = [ l for l in os.listdir(help_folder) if os.path.isfile(os.path.join(help_folder, l, help_file)) ] best_lang = find_best_locale_match(current_lang, help_langs) if not best_lang and current_lang != DEFAULT_LANG: # nothing found for current language, fallback to the default language, # if available & different from current language log.warning( "help file %s not found in lang %s, falling back to default lang (%s)", help_file, current_lang, DEFAULT_LANG) best_lang = find_best_locale_match(DEFAULT_LANG, help_langs) # did we get something usable ? if best_lang: # we already checked that the full path exists when enumerating suitable # help content above, so we can just return the part here without # checking it again return os.path.join(help_folder, best_lang, help_file) else: log.warning("no help content found for file %s", help_file) return None
def _get_best_help_file(help_folder, help_file): """ Return the path to the best help file for the current language and available help content :param str help_folder: a path to folder where we should look for the help files :param str help_file: name of the requested help file :return: path to the best help file or ``None`` is no match is found :rtype: str or NoneType """ current_lang = os.environ["LANG"] # list all available languages for the Anaconda help # * content is stored in folders named after the given language code # (en-US, cs-CZ, jp-JP, etc.) # * we check if the given folder contains the currently needed help file # and only consider it fit to use if it does have the file if not os.path.exists(help_folder): log.warning("help folder %s for help file %s does not exist", help_folder, help_file) return None help_langs = [l for l in os.listdir(help_folder) if os.path.isfile(os.path.join(help_folder, l, help_file))] best_lang = find_best_locale_match(current_lang, help_langs) if not best_lang and current_lang != DEFAULT_LANG: # nothing found for current language, fallback to the default language, # if available & different from current language log.warning("help file %s not found in lang %s, falling back to default lang (%s)", help_file, current_lang, DEFAULT_LANG) best_lang = find_best_locale_match(DEFAULT_LANG, help_langs) # did we get something usable ? if best_lang: # we already checked that the full path exists when enumerating suitable # help content above, so we can just return the part here without # checking it again return os.path.join(help_folder, best_lang, help_file) else: log.warning("no help content found for file %s", help_file) return None
def find_best_locale_match_test(self): """Finding best locale matches should work as expected.""" # can find best matches self.assertEqual(localization.find_best_locale_match("cs_CZ", ["cs", "cs_CZ", "en", "en_US"]), "cs_CZ") self.assertEqual(localization.find_best_locale_match("cs", ["cs_CZ", "cs", "en", "en_US"]), "cs") self.assertEqual(localization.find_best_locale_match("pt_BR", ["pt", "pt_BR"]), "pt_BR") self.assertEqual(localization.find_best_locale_match("pt_BR", ["pt", "pt_BR", "pt_PT"]), "pt_BR") self.assertEqual(localization.find_best_locale_match("cs_CZ.UTF-8", ["cs", "cs_CZ", "cs_CZ.UTF-8"]), "cs_CZ.UTF-8") self.assertEqual(localization.find_best_locale_match("cs_CZ.UTF-8@latin", ["cs", "cs_CZ@latin", "cs_CZ.UTF-8"]), "cs_CZ@latin") # no matches self.assertIsNone(localization.find_best_locale_match("pt_BR", ["en_BR", "en"])) self.assertIsNone(localization.find_best_locale_match("cs_CZ.UTF-8", ["en", "en.UTF-8"]))
def _find_best_help_file(current_locale, available_files): """Find the best help file for the specified locale. :param str current_locale: a valid locale (e.g. en_US.UTF-8) :param dict available_files: a dictionary of langcodes and help paths :return str: a path to the best help file or None """ for locale in (current_locale, DEFAULT_LANG): best_lang = find_best_locale_match(locale, available_files.keys()) best_path = available_files.get(best_lang, None) if best_path: return best_path return None
def _get_best_help_file(help_folder, help_file): """ Return the path to the best help file for the current language and available help content :param str help_folder: a path to folder where we should look for the help files :param str help_file: name of the requested help file :return: path to the best help file or ``None`` is no match is found :rtype: str or NoneType """ current_lang = os.environ["LANG"] # list all available languages for the Anaconda help # * content is stored in folders named after the given language code # (en-US, cs-CZ, jp-JP, etc.) # * we check if the given folder contains the currently needed help file # and only consider it fit to use if it does have the file if not os.path.exists(help_folder): log.warning("help folder %s for help file %s does not exist", help_folder, help_file) return None # Collect languages and files that provide the help content. help_langs = {} for lang in os.listdir(help_folder): # Does the help file exist for this language? path = os.path.join(help_folder, lang, help_file) if not os.path.isfile(path): continue # Create a valid langcode. For example, use en_US instead of en-US. code = lang.replace('-', '_') help_langs[code] = path # Find the best help file. for locale in (current_lang, DEFAULT_LANG): best_lang = find_best_locale_match(locale, help_langs.keys()) best_path = help_langs.get(best_lang, None) if best_path: return best_path # No file found. log.warning("no help content found for file %s", help_file) return None
def _is_language_support_installed(self, lang): """Is the support for the specified language installed? The language is considered to be supported if we are not able to determine the supported locales due to missing tools. :param lang: a value for the LANG locale variable :return: False if the locale is known to be not supported, otherwise True """ try: output = execWithCapture("locale", ["-a"], root=self._sysroot) except OSError as e: log.warning("Couldn't get supported locales: %s", e) return True match = find_best_locale_match(lang, output.splitlines()) log.debug("The '%s' locale matched '%s'.", lang, match) return bool(match)