diff options
Diffstat (limited to 'lib/ansible')
-rw-r--r-- | lib/ansible/cli/doc.py | 25 | ||||
-rw-r--r-- | lib/ansible/collections/__init__.py | 10 | ||||
-rw-r--r-- | lib/ansible/collections/list.py | 38 |
3 files changed, 43 insertions, 30 deletions
diff --git a/lib/ansible/cli/doc.py b/lib/ansible/cli/doc.py index cc1ee57d5a..8e96376ab6 100644 --- a/lib/ansible/cli/doc.py +++ b/lib/ansible/cli/doc.py @@ -18,33 +18,39 @@ from ansible import constants as C from ansible import context from ansible.cli import CLI from ansible.cli.arguments import option_helpers as opt_help -from ansible.collections.list import list_collection_dirs, get_collection_name_from_path +from ansible.collections.list import list_collection_dirs from ansible.errors import AnsibleError, AnsibleOptionsError -from ansible.module_utils._text import to_native +from ansible.module_utils._text import to_native, to_text from ansible.module_utils.common._collections_compat import Container, Sequence +from ansible.module_utils.common.json import AnsibleJSONEncoder from ansible.module_utils.six import string_types from ansible.parsing.metadata import extract_metadata from ansible.parsing.plugin_docs import read_docstub from ansible.parsing.yaml.dumper import AnsibleDumper from ansible.plugins.loader import action_loader, fragment_loader -from ansible.utils.collection_loader import set_collection_playbook_paths +from ansible.utils.collection_loader import set_collection_playbook_paths, get_collection_name_from_path from ansible.utils.display import Display from ansible.utils.plugin_docs import BLACKLIST, get_docstring, get_versioned_doclink + display = Display() def jdump(text): - display.display(json.dumps(text, sort_keys=True, indent=4)) + try: + display.display(json.dumps(text, cls=AnsibleJSONEncoder, sort_keys=True, indent=4)) + except TypeError as e: + raise AnsibleError('We could not convert all the documentation into JSON as there was a conversion issue: %s' % to_native(e)) def add_collection_plugins(plugin_list, plugin_type, coll_filter=None): # TODO: take into account routing.yml once implemented - colldirs = list_collection_dirs(coll_filter=coll_filter) - for path in colldirs: - collname = get_collection_name_from_path(path) + b_colldirs = list_collection_dirs(coll_filter=coll_filter) + for b_path in b_colldirs: + path = to_text(b_path, errors='surrogate_or_strict') + collname = get_collection_name_from_path(b_path) ptype = C.COLLECTION_PTYPE_COMPAT.get(plugin_type, plugin_type) - plugin_list.update(DocCLI.find_plugins(os.path.join(path, 'plugins', ptype), plugin_type, collname)) + plugin_list.update(DocCLI.find_plugins(os.path.join(path, 'plugins', ptype), plugin_type, collection=collname)) class RemovedPlugin(Exception): @@ -235,7 +241,7 @@ class DocCLI(CLI): # Some changes to how json docs are formatted for plugin, doc_data in plugin_docs.items(): try: - doc_data['return'] = yaml.load(doc_data['return']) + doc_data['return'] = yaml.safe_load(doc_data['return']) except Exception: pass @@ -474,6 +480,7 @@ class DocCLI(CLI): # Uses a list to get the order right ret = [] for i in finder._get_paths(subdirs=False): + i = to_text(i, errors='surrogate_or_strict') if i not in ret: ret.append(i) return os.pathsep.join(ret) diff --git a/lib/ansible/collections/__init__.py b/lib/ansible/collections/__init__.py index 512835aaf4..6b3e2a7d80 100644 --- a/lib/ansible/collections/__init__.py +++ b/lib/ansible/collections/__init__.py @@ -6,8 +6,9 @@ __metaclass__ = type import os +from ansible.module_utils._text import to_bytes -FLAG_FILES = frozenset(['MANIFEST.json', 'galaxy.yml']) +B_FLAG_FILES = frozenset([b'MANIFEST.json', b'galaxy.yml']) def is_collection_path(path): @@ -18,9 +19,10 @@ def is_collection_path(path): """ is_coll = False - if os.path.isdir(path): - for flag in FLAG_FILES: - if os.path.exists(os.path.join(path, flag)): + b_path = to_bytes(path) + if os.path.isdir(b_path): + for b_flag in B_FLAG_FILES: + if os.path.exists(os.path.join(b_path, b_flag)): is_coll = True break diff --git a/lib/ansible/collections/list.py b/lib/ansible/collections/list.py index 64837fae8f..cd207dafdf 100644 --- a/lib/ansible/collections/list.py +++ b/lib/ansible/collections/list.py @@ -9,7 +9,8 @@ import os from collections import defaultdict from ansible.collections import is_collection_path -from ansible.utils.collection_loader import AnsibleCollectionLoader, get_collection_name_from_path +from ansible.module_utils._text import to_bytes +from ansible.utils.collection_loader import AnsibleCollectionLoader from ansible.utils.display import Display display = Display() @@ -24,17 +25,20 @@ def list_valid_collection_paths(search_paths=None, warn=False): """ if search_paths is None: - search_paths = AnsibleCollectionLoader().n_collection_paths + search_paths = [] + + search_paths.extend(AnsibleCollectionLoader().n_collection_paths) for path in search_paths: - if not os.path.exists(path): + b_path = to_bytes(path) + if not os.path.exists(b_path): # warn for missing, but not if default if warn: display.warning("The configured collection path {0} does not exist.".format(path)) continue - if not os.path.isdir(path): + if not os.path.isdir(b_path): if warn: display.warning("The configured collection path {0}, exists, but it is not a directory.".format(path)) continue @@ -53,14 +57,14 @@ def list_collection_dirs(search_paths=None, coll_filter=None): collections = defaultdict(dict) for path in list_valid_collection_paths(search_paths): - if os.path.isdir(path): - coll_root = os.path.join(path, 'ansible_collections') - - if os.path.exists(coll_root) and os.path.isdir(coll_root): + b_path = to_bytes(path) + if os.path.isdir(b_path): + b_coll_root = to_bytes(os.path.join(path, 'ansible_collections')) + if os.path.exists(b_coll_root) and os.path.isdir(b_coll_root): coll = None if coll_filter is None: - namespaces = os.listdir(coll_root) + namespaces = os.listdir(b_coll_root) else: if '.' in coll_filter: (nsp, coll) = coll_filter.split('.') @@ -69,12 +73,12 @@ def list_collection_dirs(search_paths=None, coll_filter=None): namespaces = [nsp] for ns in namespaces: - namespace_dir = os.path.join(coll_root, ns) + b_namespace_dir = os.path.join(b_coll_root, to_bytes(ns)) - if os.path.isdir(namespace_dir): + if os.path.isdir(b_namespace_dir): if coll is None: - colls = os.listdir(namespace_dir) + colls = os.listdir(b_namespace_dir) else: colls = [coll] @@ -82,8 +86,8 @@ def list_collection_dirs(search_paths=None, coll_filter=None): # skip dupe collections as they will be masked in execution if collection not in collections[ns]: - coll_dir = os.path.join(namespace_dir, collection) - if is_collection_path(coll_dir): - cpath = os.path.join(namespace_dir, collection) - collections[ns][collection] = cpath - yield cpath + b_coll = to_bytes(collection) + b_coll_dir = os.path.join(b_namespace_dir, b_coll) + if is_collection_path(b_coll_dir): + collections[ns][collection] = b_coll_dir + yield b_coll_dir |