From 2a54644cfa904d994ddf4fcc19bee7d603bff7ec Mon Sep 17 00:00:00 2001 From: ajakk Date: Fri, 14 Oct 2022 20:31:02 +0000 Subject: [PATCH] portage: drop usage of gentoolkit, add knobs for --with-bdeps, --backtrack (#5349) * portage: drop dependency on gentoolkit (provides equery) Portage installs a Python module, which is available anywhere that Portage itself is available. We can use that instead of calling a shell command. Signed-off-by: John Helmert III * portage: add knob for emerge's --backtrack flag Signed-off-by: John Helmert III * portage: add knob for portage's --with-bdeps option Also, this option does not accept "True" like other options. Instead, it only uses 'y' and 'n', so parse booleans properly into these chars. Signed-off-by: John Helmert III * Add changelog entry for #5349 Signed-off-by: John Helmert III Signed-off-by: John Helmert III --- .../5349-drop-gentoolkit-more-knobs.yml | 3 ++ plugins/modules/packaging/os/portage.py | 54 ++++++++++++++++--- 2 files changed, 50 insertions(+), 7 deletions(-) create mode 100644 changelogs/fragments/5349-drop-gentoolkit-more-knobs.yml diff --git a/changelogs/fragments/5349-drop-gentoolkit-more-knobs.yml b/changelogs/fragments/5349-drop-gentoolkit-more-knobs.yml new file mode 100644 index 0000000000..bc2d79e7eb --- /dev/null +++ b/changelogs/fragments/5349-drop-gentoolkit-more-knobs.yml @@ -0,0 +1,3 @@ +minor_changes: + - portage - use Portage's python module instead of calling gentoolkit-provided program in shell (https://github.com/ansible-collections/community.general/pull/5349). + - portage - add knobs for Portage's ``--backtrack`` and ``--with-bdeps`` options (https://github.com/ansible-collections/community.general/pull/5349). diff --git a/plugins/modules/packaging/os/portage.py b/plugins/modules/packaging/os/portage.py index 22f258cf1a..4c91c9d0b6 100644 --- a/plugins/modules/packaging/os/portage.py +++ b/plugins/modules/packaging/os/portage.py @@ -42,6 +42,12 @@ options: type: bool default: false + backtrack: + description: + - Set backtrack value (C(--backtrack)). + type: int + version_added: 5.8.0 + deep: description: - Consider the entire dependency tree of packages (--deep) @@ -160,6 +166,12 @@ options: - --load-average setting values type: float + withbdeps: + description: + - Specifies that build time dependencies should be installed. + type: bool + version_added: 5.8.0 + quietbuild: description: - Redirect all build output to logs alone, and do not display it @@ -228,11 +240,24 @@ EXAMPLES = ''' import os import re +import sys +import traceback -from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.basic import AnsibleModule, missing_required_lib +from ansible.module_utils.common.respawn import has_respawned, respawn_module from ansible.module_utils.common.text.converters import to_native +try: + from portage.dbapi import vartree + from portage.exception import InvalidAtom + HAS_PORTAGE = True + PORTAGE_IMPORT_ERROR = None +except ImportError: + HAS_PORTAGE = False + PORTAGE_IMPORT_ERROR = traceback.format_exc() + + def query_package(module, package, action): if package.startswith('@'): return query_set(module, package, action) @@ -240,10 +265,12 @@ def query_package(module, package, action): def query_atom(module, atom, action): - cmd = '%s list %s' % (module.equery_path, atom) - - rc, out, err = module.run_command(cmd) - return rc == 0 + vdb = vartree.vardbapi() + try: + exists = vdb.match(atom) + except InvalidAtom: + return False + return bool(exists) def query_set(module, set, action): @@ -336,6 +363,8 @@ def emerge_packages(module, packages): emerge_flags = { 'jobs': '--jobs', 'loadavg': '--load-average', + 'backtrack': '--backtrack', + 'withbdeps': '--with-bdeps', } for flag, arg in emerge_flags.items(): @@ -351,7 +380,10 @@ def emerge_packages(module, packages): continue """Add the --flag=value pair.""" - args.extend((arg, to_native(flag_val))) + if isinstance(p[flag], bool): + args.extend((arg, to_native('y' if flag_val else 'n'))) + else: + args.extend((arg, to_native(flag_val))) cmd, (rc, out, err) = run_emerge(module, packages, *args) if rc != 0: @@ -475,6 +507,7 @@ def main(): choices=portage_present_states + portage_absent_states, ), update=dict(default=False, type='bool'), + backtrack=dict(default=None, type='int'), deep=dict(default=False, type='bool'), newuse=dict(default=False, type='bool'), changed_use=dict(default=False, type='bool'), @@ -493,6 +526,7 @@ def main(): keepgoing=dict(default=False, type='bool'), jobs=dict(default=None, type='int'), loadavg=dict(default=None, type='float'), + withbdeps=dict(default=None, type='bool'), quietbuild=dict(default=False, type='bool'), quietfail=dict(default=False, type='bool'), ), @@ -506,8 +540,14 @@ def main(): supports_check_mode=True, ) + if not HAS_PORTAGE: + if sys.executable != '/usr/bin/python' and not has_respawned(): + respawn_module('/usr/bin/python') + else: + module.fail_json(msg=missing_required_lib('portage'), + exception=PORTAGE_IMPORT_ERROR) + module.emerge_path = module.get_bin_path('emerge', required=True) - module.equery_path = module.get_bin_path('equery', required=True) p = module.params