1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
#!/usr/bin/env python3
#
# utils.py
"""
General utilities.
"""
#
# Copyright © 2021 Dominic Davis-Foster <dominic@davis-foster.co.uk>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
# OR OTHER DEALINGS IN THE SOFTWARE.
#
# stdlib
from typing import Dict, Iterable, Optional, Type, cast
# 3rd party
import click
from domdf_python_tools.words import Plural
from pyproject_parser.cli import ConfigTracebackHandler
# this package
from whey.builder import AbstractBuilder
from whey.config.whey import get_entry_points
from whey.foreman import Foreman
__all__ = ("WheyTracebackHandler", "parse_custom_builders", "print_builder_names")
_builder = Plural("builder", "builders")
class WheyTracebackHandler(ConfigTracebackHandler):
"""
Custom :class:`consolekit.tracebacks.TracebackHandler`.
"""
class WheyBackendTBHandler(WheyTracebackHandler):
def __init__(self, exception: BaseException = SystemExit(1)):
super().__init__(exception)
def parse_custom_builders(builders: Optional[Iterable[str]]) -> Dict[str, Type[AbstractBuilder]]:
"""
Parse the custom builders passed using the ``--builder NAME`` option on the command line.
:param builders:
"""
custom_builders: Dict[str, Type[AbstractBuilder]] = {}
if builders is None:
return custom_builders
entry_points = get_entry_points()
for builder_name in builders:
if builder_name not in entry_points:
raise click.BadArgumentUsage(
f"Unknown builder {builder_name!r}. \n"
f"Is it registered as an entry point under 'whey.builder'?"
)
else:
custom_builders[builder_name] = cast(Type[AbstractBuilder], entry_points[builder_name].load())
return custom_builders
def print_builder_names(
foreman: Foreman,
custom_builders: Dict[str, Type[AbstractBuilder]],
**opts: bool,
) -> None:
"""
Prints the name(s) of the builders which will be used.
:param foreman:
:param custom_builders:
:param opts: Keyword arguments for builder categories.
Corresponds to the ``--sdist``, ``--wheel`` and ``--binary`` options.
"""
builders = []
for builder_name, enabled in opts.items():
if enabled:
builder_obj = foreman.config["builders"][builder_name] # pylint: disable=loop-invariant-statement
builders.append(f" {builder_name}: {builder_obj.__module__}.{builder_obj.__qualname__}")
for builder_name, builder_obj in custom_builders.items(): # pylint: disable=use-list-copy
builders.append(f" {builder_name}: {builder_obj.__module__}.{builder_obj.__qualname__}")
click.echo(f"Using the following {_builder(len(builders))}:")
for line in builders:
click.echo(line)
click.echo()
|