2018-05-13 02:18:22 +00:00
|
|
|
# Copyright 2017, David Wilson
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are met:
|
|
|
|
#
|
|
|
|
# 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
# this list of conditions and the following disclaimer.
|
|
|
|
#
|
|
|
|
# 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
# this list of conditions and the following disclaimer in the documentation
|
|
|
|
# and/or other materials provided with the distribution.
|
|
|
|
#
|
|
|
|
# 3. Neither the name of the copyright holder nor the names of its contributors
|
|
|
|
# may be used to endorse or promote products derived from this software without
|
|
|
|
# specific prior written permission.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
2018-05-08 12:22:19 +00:00
|
|
|
|
2018-05-12 13:10:19 +00:00
|
|
|
from __future__ import absolute_import
|
2018-05-13 00:47:11 +00:00
|
|
|
import collections
|
2018-05-08 12:22:19 +00:00
|
|
|
import imp
|
|
|
|
import os
|
|
|
|
|
2018-05-12 13:10:19 +00:00
|
|
|
import mitogen.master
|
2018-05-08 12:22:19 +00:00
|
|
|
|
|
|
|
|
2018-05-12 13:10:19 +00:00
|
|
|
PREFIX = 'ansible.module_utils.'
|
2018-05-08 12:22:19 +00:00
|
|
|
|
|
|
|
|
2018-05-13 00:47:11 +00:00
|
|
|
Module = collections.namedtuple('Module', 'name path kind parent')
|
2018-05-08 12:22:19 +00:00
|
|
|
|
2018-05-12 13:10:19 +00:00
|
|
|
|
2018-05-13 00:47:11 +00:00
|
|
|
def get_fullname(module):
|
2018-05-13 02:18:22 +00:00
|
|
|
"""
|
|
|
|
Reconstruct a Module's canonical path by recursing through its parents.
|
|
|
|
"""
|
2018-05-13 00:47:11 +00:00
|
|
|
bits = [str(module.name)]
|
|
|
|
while module.parent:
|
|
|
|
bits.append(str(module.parent.name))
|
|
|
|
module = module.parent
|
|
|
|
return '.'.join(reversed(bits))
|
2018-05-12 13:10:19 +00:00
|
|
|
|
2018-05-08 12:22:19 +00:00
|
|
|
|
2018-05-13 00:47:11 +00:00
|
|
|
def get_code(module):
|
2018-05-13 02:18:22 +00:00
|
|
|
"""
|
|
|
|
Compile and return a Module's code object.
|
|
|
|
"""
|
2018-05-13 00:47:11 +00:00
|
|
|
fp = open(module.path)
|
|
|
|
try:
|
|
|
|
return compile(fp.read(), str(module.name), 'exec')
|
|
|
|
finally:
|
|
|
|
fp.close()
|
|
|
|
|
|
|
|
|
|
|
|
def is_pkg(module):
|
2018-05-13 02:18:22 +00:00
|
|
|
"""
|
|
|
|
Return :data:`True` if a Module represents a package.
|
|
|
|
"""
|
2018-05-13 00:47:11 +00:00
|
|
|
return module.kind == imp.PKG_DIRECTORY
|
2018-05-08 12:22:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
def find(name, path=(), parent=None):
|
|
|
|
"""
|
2018-05-13 02:18:22 +00:00
|
|
|
Return a Module instance describing the first matching module found on the
|
|
|
|
given search path.
|
|
|
|
|
|
|
|
:param str name:
|
|
|
|
Module name.
|
|
|
|
:param str path:
|
|
|
|
Search path.
|
|
|
|
:param Module parent:
|
|
|
|
If given, make the found module a child of this module.
|
2018-05-08 12:22:19 +00:00
|
|
|
"""
|
2018-05-12 13:10:19 +00:00
|
|
|
head, _, tail = name.partition('.')
|
2018-05-08 12:22:19 +00:00
|
|
|
try:
|
2018-05-12 13:10:19 +00:00
|
|
|
tup = imp.find_module(head, list(path))
|
2018-05-08 12:22:19 +00:00
|
|
|
except ImportError:
|
|
|
|
return parent
|
|
|
|
|
|
|
|
fp, path, (suffix, mode, kind) = tup
|
|
|
|
if fp:
|
|
|
|
fp.close()
|
|
|
|
|
2018-05-12 13:10:19 +00:00
|
|
|
if kind == imp.PKG_DIRECTORY:
|
|
|
|
path = os.path.join(path, '__init__.py')
|
|
|
|
module = Module(head, path, kind, parent)
|
|
|
|
if tail:
|
|
|
|
return find_relative(module, tail, path)
|
2018-05-08 12:22:19 +00:00
|
|
|
return module
|
|
|
|
|
|
|
|
|
|
|
|
def find_relative(parent, name, path=()):
|
2018-05-12 13:10:19 +00:00
|
|
|
path = [os.path.dirname(parent.path)] + list(path)
|
2018-05-08 12:22:19 +00:00
|
|
|
return find(name, path, parent=parent)
|
|
|
|
|
|
|
|
|
2018-05-12 13:10:19 +00:00
|
|
|
def scan_fromlist(code):
|
|
|
|
for level, modname_s, fromlist in mitogen.master.scan_code_imports(code):
|
|
|
|
for name in fromlist:
|
|
|
|
yield level, '%s.%s' % (modname_s, name)
|
|
|
|
if not fromlist:
|
|
|
|
yield level, modname_s
|
2018-05-08 12:22:19 +00:00
|
|
|
|
|
|
|
|
2018-05-12 13:10:19 +00:00
|
|
|
def scan(module_name, module_path, search_path):
|
2018-05-13 02:18:22 +00:00
|
|
|
module = Module(module_name, module_path, imp.PY_SOURCE, None)
|
2018-05-12 13:10:19 +00:00
|
|
|
stack = [module]
|
|
|
|
seen = set()
|
2018-05-08 12:22:19 +00:00
|
|
|
|
2018-05-12 13:10:19 +00:00
|
|
|
while stack:
|
|
|
|
module = stack.pop(0)
|
2018-05-13 00:47:11 +00:00
|
|
|
for level, fromname in scan_fromlist(get_code(module)):
|
2018-05-12 13:10:19 +00:00
|
|
|
if not fromname.startswith(PREFIX):
|
|
|
|
continue
|
2018-05-08 12:22:19 +00:00
|
|
|
|
2018-05-12 13:10:19 +00:00
|
|
|
imported = find(fromname[len(PREFIX):], search_path)
|
|
|
|
if imported is None or imported in seen:
|
|
|
|
continue
|
2018-05-08 12:22:19 +00:00
|
|
|
|
2018-05-12 13:10:19 +00:00
|
|
|
seen.add(imported)
|
2018-05-13 00:47:11 +00:00
|
|
|
stack.append(imported)
|
2018-05-12 13:10:19 +00:00
|
|
|
parent = imported.parent
|
|
|
|
while parent:
|
2018-05-13 02:18:22 +00:00
|
|
|
fullname = get_fullname(parent)
|
|
|
|
module = Module(fullname, parent.path, parent.kind, None)
|
2018-05-12 13:10:19 +00:00
|
|
|
if module not in seen:
|
|
|
|
seen.add(module)
|
|
|
|
stack.append(module)
|
|
|
|
parent = parent.parent
|
|
|
|
|
|
|
|
return sorted(
|
2018-05-13 00:47:11 +00:00
|
|
|
(PREFIX + get_fullname(module), module.path, is_pkg(module))
|
2018-05-12 13:10:19 +00:00
|
|
|
for module in seen
|
|
|
|
)
|