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 <ajak@gentoo.org>
pull/5349/head
John Helmert III 2022-06-08 16:57:07 -05:00
parent e47845ab3a
commit 9189f7a6bf
No known key found for this signature in database
GPG Key ID: A05EADBE69AFC9B5
1 changed files with 27 additions and 6 deletions

View File

@ -228,11 +228,24 @@ EXAMPLES = '''
import os import os
import re 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 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): def query_package(module, package, action):
if package.startswith('@'): if package.startswith('@'):
return query_set(module, package, action) return query_set(module, package, action)
@ -240,10 +253,12 @@ def query_package(module, package, action):
def query_atom(module, atom, action): def query_atom(module, atom, action):
cmd = '%s list %s' % (module.equery_path, atom) vdb = vartree.vardbapi()
try:
rc, out, err = module.run_command(cmd) exists = vdb.match(atom)
return rc == 0 except InvalidAtom:
return False
return bool(exists)
def query_set(module, set, action): def query_set(module, set, action):
@ -506,8 +521,14 @@ def main():
supports_check_mode=True, 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.emerge_path = module.get_bin_path('emerge', required=True)
module.equery_path = module.get_bin_path('equery', required=True)
p = module.params p = module.params