From 2030d2be1d36509b33b3ad3f60cdc0295b01ae05 Mon Sep 17 00:00:00 2001 From: Rafael dos Santos Date: Dec 07 2018 12:13:18 +0000 Subject: summarizer: read metadata from url in cmdline It's useful to have a summary of modules from a repository given its url without having to cache it first. Signed-off-by: Rafael dos Santos --- diff --git a/_fedmod/cli.py b/_fedmod/cli.py index f76c060..aa76245 100644 --- a/_fedmod/cli.py +++ b/_fedmod/cli.py @@ -199,8 +199,12 @@ def lint(modulemd, min_level): help="Modulemd files to check. " "Causes cached metadata to be ignored. " "Can be given multiple times.") +@click.option("--add-url", "-u", metavar="URL", multiple=True, + help="Repositories to read module metadata from." + "Causes cached metadata to be ignored. " + "Can be given multiple times.") @click.option("--tree", "-t", is_flag=True, default=False, help="Print output as a tree") -def summarize(modules, add_file, tree): +def summarize(modules, add_file, add_url, tree): """Prints a summary of available modules""" - summarize_modules(modules, add_file, tree) + summarize_modules(modules, add_file, add_url, tree) diff --git a/_fedmod/modulemd_summarizer.py b/_fedmod/modulemd_summarizer.py index aa5d351..e7f7e0f 100644 --- a/_fedmod/modulemd_summarizer.py +++ b/_fedmod/modulemd_summarizer.py @@ -24,11 +24,12 @@ import gi gi.require_version('Modulemd', '1.0') # noqa: E402 from gi.repository import Modulemd +import tempfile import smartcols from fnmatch import fnmatch from collections import defaultdict -from . import _repodata +from . import _repodata, _fetchrepodata def _print_summary(profiles, sdefaults, pdefaults, deps, mfilter, as_tree): @@ -83,11 +84,9 @@ def _print_summary(profiles, sdefaults, pdefaults, deps, mfilter, as_tree): print('\nHint: [d]efault') -def _add_module_metadata(yaml, profiles, dstreams, dprofiles, deps): - mmd_index, failures = Modulemd.index_from_file(yaml) - assert len(failures) == 0, failures - - for module_name, index in mmd_index.items(): +def _parse_mmd(mmd_index, profiles, dstreams, dprofiles, deps): + for index in mmd_index: + module_name = index.get_name() for module in index.get_streams().values(): # local modulemd files might miss some info like context or # version. So let's make sure nsvc is consistent @@ -114,12 +113,12 @@ def _add_module_metadata(yaml, profiles, dstreams, dprofiles, deps): dprofiles[module_name][s] = pset.get() -def summarize_modules(mfilter=None, yamls=None, as_tree=False): +def summarize_modules(mfilter=None, yamls=None, urls=None, as_tree=False): """ - Load Modulemd objects from each yaml file in the `yamls` list and print a - summary of the modules found with streams, context, version, profiles and - dependencies information. - If no files are passed, local cached metadata is used instead. + Load Modulemd objects from each yaml file in the `yamls` list or each url + in `urls` list and print a summary of the modules found with streams, + context, version, profiles and dependencies information. + If no files and no ulrs are passed, local cached metadata is used instead. *mfilter*: if present, restricts output to the nsvc supplied. Glob pattern can be used, e.g for filtering by stream: '*:master:*' @@ -127,6 +126,9 @@ def summarize_modules(mfilter=None, yamls=None, as_tree=False): *yamls*: list of yaml files to parse. If any, locally cached metadata will be ignored + *urls*: list of repositories to read from. If any, locally cached metadata + will be ignored. + *as_tree*: print the summary in tree format, grouping information. """ @@ -134,7 +136,16 @@ def summarize_modules(mfilter=None, yamls=None, as_tree=False): if yamls: for yaml in yamls: assert yaml.endswith('.yaml'), f"Not a yaml file: {yaml}" - _add_module_metadata(yaml, profiles, dstreams, dprofiles, deps) + index, failures = Modulemd.index_from_file(yaml) + assert len(failures) == 0, failures + _parse_mmd(index.values(), profiles, dstreams, dprofiles, deps) + elif urls: + for url in urls: + with tempfile.TemporaryDirectory() as local_path: + rp = _fetchrepodata.RepoPaths(url, local_path) + _fetchrepodata._download_metadata_files(rp) + indexes = _fetchrepodata._read_modules(rp) + _parse_mmd(indexes, profiles, dstreams, dprofiles, deps) else: profiles = _repodata.get_modules_profiles_lookup() dstreams = _repodata.get_modules_default_streams_lookup()