Skip to content

Commit

Permalink
Improve filtering.
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfontein committed Nov 2, 2020
1 parent 7ba1cf9 commit 072410c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
41 changes: 30 additions & 11 deletions antsibull/data/collection-enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,19 @@ def load_plugin(loader, plugin_type, plugin):
return result


def ansible_doc_coll_filter(coll_filter):
return coll_filter[0] if coll_filter and len(coll_filter) == 1 else None


def match_filter(name, coll_filter):
if coll_filter is None or name in coll_filter:
return True
for filter in coll_filter:
if name.startswith(filter + '.'):
return True
return False


def load_all_plugins(plugin_type, basedir, coll_filter):
loader = getattr(plugin_loader, '%s_loader' % plugin_type)

Expand All @@ -87,25 +100,27 @@ def load_all_plugins(plugin_type, basedir, coll_filter):

plugin_list = set()

if coll_filter is None:
if match_filter('ansible.builtin', coll_filter):
paths = loader._get_paths_with_context()
for path_context in paths:
plugin_list.update(
doc.DocCLI.find_plugins(path_context.path, path_context.internal, plugin_type))

doc.add_collection_plugins(plugin_list, plugin_type, coll_filter=coll_filter)
doc.add_collection_plugins(
plugin_list, plugin_type, coll_filter=ansible_doc_coll_filter(coll_filter))

result = {}
for plugin in plugin_list:
result[plugin] = load_plugin(loader, plugin_type, plugin)
if match_filter(plugin, coll_filter):
result[plugin] = load_plugin(loader, plugin_type, plugin)

return result


def main(args):
parser = argparse.ArgumentParser(
prog=args[0], description='Bulk extraction of Ansible plugin docs.')
parser.add_argument('args', nargs='?', help='Collection filter', metavar='collection_filter')
parser.add_argument('args', nargs='*', help='Collection filter', metavar='collection_filter')
parser.add_argument('--pretty', action='store_true', help='Pretty-print JSON')
opt_help.add_basedir_options(parser)

Expand All @@ -127,17 +142,21 @@ def main(args):
result['plugins'][plugin_type] = load_all_plugins(plugin_type, basedir, coll_filter)

# Export collection data
b_colldirs = list_collection_dirs(coll_filter=coll_filter)
b_colldirs = list_collection_dirs(coll_filter=ansible_doc_coll_filter(coll_filter))
for b_path in b_colldirs:
collection = CollectionRequirement.from_path(b_path, False, fallback_metadata=True)

result['collections']['{0}.{1}'.format(collection.namespace, collection.name)] = {
'path': to_native(b_path),
'version': collection.metadata.version if collection.metadata.version != '*' else None,
collection_name = '{0}.{1}'.format(collection.namespace, collection.name)
if match_filter(collection_name, coll_filter):
version = collection.metadata.version
result['collections'][collection_name] = {
'path': to_native(b_path),
'version': version if version != '*' else None,
}
if match_filter('ansible.builtin', coll_filter):
result['collections']['ansible.builtin'] = {
'version': ansible_release.__version__,
}
result['collections']['ansible.builtin'] = {
'version': ansible_release.__version__,
}

print(json.dumps(
result, cls=AnsibleJSONEncoder, sort_keys=True, indent=4 if arguments.pretty else None))
Expand Down
6 changes: 3 additions & 3 deletions antsibull/docs_parsing/ansible_internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ async def get_ansible_plugin_info(venv: t.Union['VenvRunner', 'FakeVenvRunner'],
with tempfile.NamedTemporaryFile() as tmp_file:
tmp_file.write(get_antsibull_data('collection-enum.py'))
collection_enum_args = [tmp_file.name]
if collection_names and len(collection_names) == 1:
# Script allows to filter by one collection
collection_enum_args.append(collection_names[0])
if collection_names is not None:
# Script allows to filter by collections
collection_enum_args.extend(collection_names)
collection_enum_cmd = venv_python(*collection_enum_args, _env=env)
raw_result = collection_enum_cmd.stdout.decode('utf-8', errors='surrogateescape')
result = json.loads(_filter_non_json_lines(raw_result)[0])
Expand Down

0 comments on commit 072410c

Please sign in to comment.