diff options
author | Brian Coca <bcoca@users.noreply.github.com> | 2020-11-10 11:46:15 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-10 11:46:15 -0500 |
commit | 4f0e2fff957c67415958e71a03fe4fc7dabce87e (patch) | |
tree | e41c67d1071d4b3744a8d25f449319adc63ac92f /lib/ansible/collections | |
parent | 48c08f410cd368b129fed61f9a58a0cc2b1df458 (diff) | |
download | ansible-4f0e2fff957c67415958e71a03fe4fc7dabce87e.tar.gz |
avoid fatal exception on invalid collection name (#72296)
* avoid fatal exception on invalid collection name used in ansible-doc filter
Diffstat (limited to 'lib/ansible/collections')
-rw-r--r-- | lib/ansible/collections/list.py | 34 |
1 files changed, 21 insertions, 13 deletions
diff --git a/lib/ansible/collections/list.py b/lib/ansible/collections/list.py index 8e8bc66490..a1d9901706 100644 --- a/lib/ansible/collections/list.py +++ b/lib/ansible/collections/list.py @@ -8,6 +8,7 @@ import os from collections import defaultdict +from ansible.errors import AnsibleError from ansible.collections import is_collection_path from ansible.module_utils._text import to_bytes from ansible.utils.collection_loader import AnsibleCollectionConfig @@ -54,6 +55,17 @@ def list_collection_dirs(search_paths=None, coll_filter=None): :return: list of collection directory paths """ + collection = None + namespace = None + if coll_filter is not None: + if '.' in coll_filter: + try: + (namespace, collection) = coll_filter.split('.') + except ValueError: + raise AnsibleError("Invalid collection pattern supplied: %s" % coll_filter) + else: + namespace = coll_filter + collections = defaultdict(dict) for path in list_valid_collection_paths(search_paths): @@ -62,32 +74,28 @@ def list_collection_dirs(search_paths=None, coll_filter=None): 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: + + if namespace is None: namespaces = os.listdir(b_coll_root) else: - if '.' in coll_filter: - (nsp, coll) = coll_filter.split('.') - else: - nsp = coll_filter - namespaces = [nsp] + namespaces = [namespace] for ns in namespaces: b_namespace_dir = os.path.join(b_coll_root, to_bytes(ns)) if os.path.isdir(b_namespace_dir): - if coll is None: + if collection is None: colls = os.listdir(b_namespace_dir) else: - colls = [coll] + colls = [collection] - for collection in colls: + for mycoll in colls: # skip dupe collections as they will be masked in execution - if collection not in collections[ns]: - b_coll = to_bytes(collection) + if mycoll not in collections[ns]: + b_coll = to_bytes(mycoll) b_coll_dir = os.path.join(b_namespace_dir, b_coll) if is_collection_path(b_coll_dir): - collections[ns][collection] = b_coll_dir + collections[ns][mycoll] = b_coll_dir yield b_coll_dir |