From f8fc18412c62f2351c374abec292e35051ef664b Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Thu, 6 Jul 2023 21:38:59 +0200 Subject: [PATCH] [PR #6820/601fa0df backport][stable-7] locale_gen: refactor module to use CmdRunner (#6867) locale_gen: refactor module to use CmdRunner (#6820) * refactor module to use CmdRunner * add changelog frag (cherry picked from commit 601fa0df6281db8ed9c7af8916f098faa9528728) Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com> --- .../fragments/6820-locale-gen-cmdrunner.yml | 2 ++ plugins/module_utils/locale_gen.py | 31 +++++++++++++++++++ plugins/modules/locale_gen.py | 30 +++++++++++------- 3 files changed, 52 insertions(+), 11 deletions(-) create mode 100644 changelogs/fragments/6820-locale-gen-cmdrunner.yml create mode 100644 plugins/module_utils/locale_gen.py diff --git a/changelogs/fragments/6820-locale-gen-cmdrunner.yml b/changelogs/fragments/6820-locale-gen-cmdrunner.yml new file mode 100644 index 0000000000..e27caefb23 --- /dev/null +++ b/changelogs/fragments/6820-locale-gen-cmdrunner.yml @@ -0,0 +1,2 @@ +minor_changes: + - locale_gen - module now using ``CmdRunner`` to execute external commands (https://github.com/ansible-collections/community.general/pull/6820). diff --git a/plugins/module_utils/locale_gen.py b/plugins/module_utils/locale_gen.py new file mode 100644 index 0000000000..ca35e17d3c --- /dev/null +++ b/plugins/module_utils/locale_gen.py @@ -0,0 +1,31 @@ +# -*- coding: utf-8 -*- +# Copyright (c) 2023, Alexei Znamensky +# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) +# SPDX-License-Identifier: GPL-3.0-or-later + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + +from ansible_collections.community.general.plugins.module_utils.cmd_runner import CmdRunner, cmd_runner_fmt + + +def locale_runner(module): + runner = CmdRunner( + module, + command=["locale", "-a"], + check_rc=True, + ) + return runner + + +def locale_gen_runner(module): + runner = CmdRunner( + module, + command="locale-gen", + arg_formats=dict( + name=cmd_runner_fmt.as_list(), + purge=cmd_runner_fmt.as_fixed('--purge'), + ), + check_rc=True, + ) + return runner diff --git a/plugins/modules/locale_gen.py b/plugins/modules/locale_gen.py index 36c304ea69..4c276d46ce 100644 --- a/plugins/modules/locale_gen.py +++ b/plugins/modules/locale_gen.py @@ -48,10 +48,11 @@ EXAMPLES = ''' import os import re -from subprocess import Popen, PIPE, call from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.common.text.converters import to_native +from ansible_collections.community.general.plugins.module_utils.locale_gen import locale_runner, locale_gen_runner + LOCALE_NORMALIZATION = { ".utf8": ".UTF-8", @@ -66,6 +67,8 @@ LOCALE_NORMALIZATION = { ".euctw": ".EUC-TW", } +module = None + # =========================================== # location module specific support methods. @@ -95,8 +98,9 @@ def is_available(name, ubuntuMode): def is_present(name): """Checks if the given locale is currently installed.""" - output = Popen(["locale", "-a"], stdout=PIPE).communicate()[0] - output = to_native(output) + runner = locale_runner(module) + with runner() as ctx: + dummy, output, dummy = ctx.run() return any(fix_case(name) == fix_case(line) for line in output.splitlines()) @@ -134,6 +138,8 @@ def apply_change(targetState, name): targetState -- Desired state, either present or absent. name -- Name including encoding such as de_CH.UTF-8. """ + runner = locale_gen_runner(module) + if targetState == "present": # Create locale. set_locale(name, enabled=True) @@ -141,9 +147,8 @@ def apply_change(targetState, name): # Delete locale. set_locale(name, enabled=False) - localeGenExitValue = call("locale-gen") - if localeGenExitValue != 0: - raise EnvironmentError(localeGenExitValue, "locale.gen failed to execute, it returned " + str(localeGenExitValue)) + with runner() as ctx: + ctx.run() def apply_change_ubuntu(targetState, name): @@ -153,10 +158,13 @@ def apply_change_ubuntu(targetState, name): targetState -- Desired state, either present or absent. name -- Name including encoding such as de_CH.UTF-8. """ + runner = locale_gen_runner(module) + if targetState == "present": # Create locale. # Ubuntu's patched locale-gen automatically adds the new locale to /var/lib/locales/supported.d/local - localeGenExitValue = call(["locale-gen", name]) + with runner() as ctx: + ctx.run() else: # Delete locale involves discarding the locale from /var/lib/locales/supported.d/local and regenerating all locales. try: @@ -174,13 +182,13 @@ def apply_change_ubuntu(targetState, name): f.close() # Purge locales and regenerate. # Please provide a patch if you know how to avoid regenerating the locales to keep! - localeGenExitValue = call(["locale-gen", "--purge"]) - - if localeGenExitValue != 0: - raise EnvironmentError(localeGenExitValue, "locale.gen failed to execute, it returned " + str(localeGenExitValue)) + with runner("purge") as ctx: + ctx.run() def main(): + global module + module = AnsibleModule( argument_spec=dict( name=dict(type='str', required=True),