Debian Codegraph @

lib/ansible/collections/list.py

raw
1 # (c) 2019 Ansible Project
2 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3 from __future__ import annotations
4
5 from ansible.errors import AnsibleError
6 from ansible.cli.galaxy import with_collection_artifacts_manager
7 from ansible.galaxy.collection import find_existing_collections
8 from ansible.module_utils.common.text.converters import to_bytes
9 from ansible.utils.collection_loader._collection_finder import _get_collection_name_from_path
10 from ansible.utils.display import Display
11
12 display = Display()
13
14
15 @with_collection_artifacts_manager
16 def list_collections(coll_filter=None, search_paths=None, dedupe=True, artifacts_manager=None):
17
18 collections = {}
19 for candidate in list_collection_dirs(search_paths=search_paths, coll_filter=coll_filter, artifacts_manager=artifacts_manager, dedupe=dedupe):
20 if collection := _get_collection_name_from_path(candidate):
21 collections[collection] = candidate
22 else:
23 display.debug(f'Skipping invalid collection in path: {candidate!r}')
24 return collections
25
26
27 @with_collection_artifacts_manager
28 def list_collection_dirs(search_paths=None, coll_filter=None, artifacts_manager=None, dedupe=True):
29 """
30 Return paths for the specific collections found in passed or configured search paths
31 :param search_paths: list of text-string paths, if none load default config
32 :param coll_filter: limit collections to just the specific namespace or collection, if None all are returned
33 :return: list of collection directory paths
34 """
35
36 namespace_filter = None
37 collection_filter = None
38 has_pure_namespace_filter = False # whether at least one coll_filter is a namespace-only filter
39 if coll_filter is not None:
40 if isinstance(coll_filter, str):
41 coll_filter = [coll_filter]
42 namespace_filter = set()
43 for coll_name in coll_filter:
44 if '.' in coll_name:
45 try:
46 namespace, collection = coll_name.split('.')
47 except ValueError:
48 raise AnsibleError("Invalid collection pattern supplied: %s" % coll_name)
49 namespace_filter.add(namespace)
50 if not has_pure_namespace_filter:
51 if collection_filter is None:
52 collection_filter = []
53 collection_filter.append(collection)
54 else:
55 namespace_filter.add(coll_name)
56 has_pure_namespace_filter = True
57 collection_filter = None
58 namespace_filter = sorted(namespace_filter)
59
60 for req in find_existing_collections(search_paths, artifacts_manager, namespace_filter=namespace_filter,
61 collection_filter=collection_filter, dedupe=dedupe):
62
63 if not has_pure_namespace_filter and coll_filter is not None and req.fqcn not in coll_filter:
64 continue
65 yield to_bytes(req.src)