"Fossies" - the Fresh Open Source Software Archive

Member "snapcraft-3.8/snapcraft/cli/extensions.py" (9 Sep 2019, 2697 Bytes) of package /linux/misc/snapcraft-3.8.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "extensions.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 3.7.2_vs_3.8.

    1 # -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
    2 #
    3 # Copyright (C) 2018 Canonical Ltd
    4 #
    5 # This program is free software: you can redistribute it and/or modify
    6 # it under the terms of the GNU General Public License version 3 as
    7 # published by the Free Software Foundation.
    8 #
    9 # This program is distributed in the hope that it will be useful,
   10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
   11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12 # GNU General Public License for more details.
   13 #
   14 # You should have received a copy of the GNU General Public License
   15 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
   16 
   17 import click
   18 import collections
   19 import inspect
   20 import sys
   21 import tabulate
   22 
   23 from ._options import get_project
   24 from snapcraft.internal import project_loader
   25 from snapcraft import yaml_utils
   26 
   27 
   28 @click.group()
   29 def extensioncli(**kwargs):
   30     pass
   31 
   32 
   33 @extensioncli.command("list-extensions")
   34 def list_extensions(**kwargs):
   35     """List available extensions.
   36 
   37     This command has an alias of `extensions`.
   38     """
   39 
   40     extension_names = project_loader.supported_extension_names()
   41 
   42     if not extension_names:
   43         click.echo("No extensions supported")
   44         return
   45 
   46     extensions = []
   47     for extension_name in sorted(extension_names):
   48         extension = project_loader.find_extension(extension_name)
   49         extension_info = collections.OrderedDict()
   50         extension_info["Extension name"] = extension_name.replace("_", "-")
   51         extension_info["Supported bases"] = ", ".join(
   52             sorted(extension.get_supported_bases())
   53         )
   54         extensions.append(extension_info)
   55 
   56     click.echo(tabulate.tabulate(extensions, headers="keys"))
   57 
   58 
   59 @extensioncli.command()
   60 @click.pass_context
   61 @click.argument("name")
   62 def extension(ctx, name, **kwargs):
   63     """Show contents of extension."""
   64 
   65     extension_cls = project_loader.find_extension(name)
   66 
   67     # Not using inspect.getdoc here since it'll fall back to the base class
   68     docstring = extension_cls.__doc__
   69     if not docstring:
   70         raise project_loader.errors.ExtensionMissingDocumentationError(name)
   71 
   72     formatter = ctx.make_formatter()
   73     formatter.write_text(inspect.cleandoc(docstring))
   74     click.echo(formatter.getvalue().rstrip("\n"))
   75 
   76 
   77 @extensioncli.command("expand-extensions")
   78 def expand_extensions(**kwargs):
   79     """Display snapcraft.yaml with all extensions applied."""
   80 
   81     project = get_project(**kwargs)
   82     yaml_with_extensions = project_loader.apply_extensions(
   83         project.info.get_raw_snapcraft()
   84     )
   85 
   86     # Loading the config applied all the extensions, so just dump it back out
   87     yaml_utils.dump(yaml_with_extensions, stream=sys.stdout)