コード例 #1
0
    def restrict_imports(path, name, messages, restrict_to_module_paths):
        """Restrict available imports.
        :type path: str
        :type name: str
        :type messages: set[str]
        :type restrict_to_module_paths: bool
        """
        restricted_loader = RestrictedModuleLoader(path, name, restrict_to_module_paths)

        # noinspection PyTypeChecker
        sys.meta_path.insert(0, restricted_loader)
        sys.path_importer_cache.clear()

        try:
            yield
        finally:
            if import_type == 'plugin':
                from ansible.utils.collection_loader._collection_finder import _AnsibleCollectionFinder
                _AnsibleCollectionFinder._remove()  # pylint: disable=protected-access

            if sys.meta_path[0] != restricted_loader:
                report_message(path, 0, 0, 'metapath', 'changes to sys.meta_path[0] are not permitted', messages)

            while restricted_loader in sys.meta_path:
                # noinspection PyTypeChecker
                sys.meta_path.remove(restricted_loader)

            sys.path_importer_cache.clear()
コード例 #2
0
def reset_collections_loader_state(metapath_finder=None):
    _AnsibleCollectionFinder._remove()

    nuke_module_prefix('ansible_collections')
    nuke_module_prefix('ansible.modules')
    nuke_module_prefix('ansible.plugins')

    # FIXME: better to move this someplace else that gets cleaned up automatically?
    _AnsibleCollectionLoader._redirected_package_map = {}

    AnsibleCollectionConfig._default_collection = None
    AnsibleCollectionConfig._on_collection_load = _EventSource()

    if metapath_finder:
        metapath_finder._install()