Skip to content

Commit

Permalink
[PR #6820/601fa0df backport][stable-7] locale_gen: refactor module to…
Browse files Browse the repository at this point in the history
… use CmdRunner (#6867)

locale_gen: refactor module to use CmdRunner (#6820)

* refactor module to use CmdRunner

* add changelog frag

(cherry picked from commit 601fa0d)

Co-authored-by: Alexei Znamensky <[email protected]>
  • Loading branch information
patchback[bot] and russoz authored Jul 6, 2023
1 parent abd2a85 commit f8fc184
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 11 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/6820-locale-gen-cmdrunner.yml
Original file line number Diff line number Diff line change
@@ -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).
31 changes: 31 additions & 0 deletions plugins/module_utils/locale_gen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2023, Alexei Znamensky <[email protected]>
# 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
30 changes: 19 additions & 11 deletions plugins/modules/locale_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@

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",
Expand All @@ -66,6 +67,8 @@
".euctw": ".EUC-TW",
}

module = None


# ===========================================
# location module specific support methods.
Expand Down Expand Up @@ -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())


Expand Down Expand Up @@ -134,16 +138,17 @@ 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)
else:
# 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):
Expand All @@ -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:
Expand All @@ -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),
Expand Down

0 comments on commit f8fc184

Please sign in to comment.