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

dconf - Try to find a Python interpreter that has gi.repository.GLib (#6491)

* dconf - Try to find a Python interpreter that has gi.repository.GLib

If we're invoked in a Python interpreter that doesn't have access to
`gi.repository.GLib`, try to find one that does and respawn the task
in that interpreter.

* ChangeLog fragment for #6491

* Update changelogs/fragments/6491-dconf-respawn.yml

Co-authored-by: Felix Fontein <felix@fontein.de>

* Simplify import code

* Get rid of ModuleNotFoundError

---------

Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
Jonathan Kamens 2023-05-08 13:42:49 -04:00 committed by GitHub
parent febe7a2fb4
commit 9f3c86a589
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 0 deletions

View file

@ -0,0 +1,2 @@
minor_changes:
- dconf - if ``gi.repository.GLib`` is missing, try to respawn in a Python interpreter that has it (https://github.com/ansible-collections/community.general/pull/6491).

View file

@ -143,11 +143,19 @@ EXAMPLES = r"""
import os import os
import sys
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.respawn import (
has_respawned,
probe_interpreters_for_module,
respawn_module,
)
from ansible.module_utils.common.text.converters import to_native from ansible.module_utils.common.text.converters import to_native
from ansible_collections.community.general.plugins.module_utils import deps from ansible_collections.community.general.plugins.module_utils import deps
glib_module_name = 'gi.repository.GLib'
try: try:
from gi.repository.GLib import Variant, GError from gi.repository.GLib import Variant, GError
except ImportError: except ImportError:
@ -415,6 +423,34 @@ def main():
], ],
) )
if Variant is None:
# This interpreter can't see the GLib module. To try to fix that, we'll
# look in common locations for system-owned interpreters that can see
# it; if we find one, we'll respawn under it. Otherwise we'll proceed
# with degraded performance, without the ability to parse GVariants.
# Later (in a different PR) we'll actually deprecate this degraded
# performance level and fail with an error if the library can't be
# found.
if has_respawned():
# This shouldn't be possible; short-circuit early if it happens.
module.fail_json(
msg="%s must be installed and visible from %s." %
(glib_module_name, sys.executable))
interpreters = ['/usr/bin/python3', '/usr/bin/python2',
'/usr/bin/python']
interpreter = probe_interpreters_for_module(
interpreters, glib_module_name)
if interpreter:
# Found the Python bindings; respawn this module under the
# interpreter where we found them.
respawn_module(interpreter)
# This is the end of the line for this process, it will exit here
# once the respawned module has completed.
# Try to be forgiving about the user specifying a boolean as the value, or # Try to be forgiving about the user specifying a boolean as the value, or
# more accurately about the fact that YAML and Ansible are quite insistent # more accurately about the fact that YAML and Ansible are quite insistent
# about converting strings that look like booleans into booleans. Convert # about converting strings that look like booleans into booleans. Convert