Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

locale_gen: refactor module to use CmdRunner #6820

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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