1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

[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 601fa0df62)

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
This commit is contained in:
patchback[bot] 2023-07-06 21:38:59 +02:00 committed by GitHub
parent abd2a85709
commit f8fc18412c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 11 deletions

View file

@ -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).

View file

@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2023, Alexei Znamensky <russoz@gmail.com>
# 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

View file

@ -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),