From afd8902d6b819b7607f720c71909d63a327bb6c5 Mon Sep 17 00:00:00 2001 From: Steven Robertson Date: Tue, 26 May 2020 01:02:02 -0700 Subject: [PATCH] hackily sorta got imp.find_module to load module_utils but it breaks further package discovery, needs fix --- mitogen/master.py | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/mitogen/master.py b/mitogen/master.py index c21319a2..62f7bf48 100644 --- a/mitogen/master.py +++ b/mitogen/master.py @@ -700,24 +700,26 @@ class ParentEnumerationMethod(FinderMethod): return path, source, is_pkg def _find_one_component(self, modname, search_path): + """ + Creates an __init__.py if one doesn't exist in the search path dirs for ansible collections + This will help imp load packages like `.ansible/collections/ansible_collections/.....plugins/module_utils` + that don't get loaded from Ansible via sys.modules + Unfortunately this leaves __init__.py files around in collections that don't have them + TODO: delete these when Mitogen exits? + Tried to hack types.ModuleType instead but no luck + Appears imp loads modules old-style with a required __init__.py + TODO: can the __init__ stuff be moved to ansible_mitogen somewhere, really want it to be there instead + """ + for path in search_path: + if "collections/ansible_collections" in path: + init_file = os.path.join(path, modname, "__init__.py") + if not os.path.isfile(init_file): + with open(init_file, "w") as f: + pass + try: #fp, path, (suffix, _, kind) = imp.find_module(modname, search_path) - # if modname == "ansible_collections": - # print(dir(sys.modules['ansible_collections'])) - # A - # taco - # filename = sys.modules['ansible_collections'].__file__ + ".py" - # return open(filename), filename, ('.py', 'r', imp.PY_SOURCE) - # regular imp.find_module doesn't work here, but perhaps we can try the loader? - - if modname.startswith("ansible_collections"): - try: - return imp.find_module(modname, search_path) - except ImportError: - # `touch search_path/__init__.py` works in this case - # TODO - else: - return imp.find_module(modname, search_path) + return imp.find_module(modname, search_path) except ImportError: e = sys.exc_info()[1] LOG.debug('%r: imp.find_module(%r, %r) -> %s',