| 1 |
# SPDX-FileCopyrightText: Peter Pentchev <[email protected]> |
| 2 |
# SPDX-License-Identifier: BSD-2-Clause |
| 3 |
|
| 4 |
"""Query a program's list of features.""" |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
import dataclasses |
| 9 |
import sys |
| 10 |
from typing import Final |
| 11 |
|
| 12 |
import click |
| 13 |
|
| 14 |
|
| 15 |
try: |
| 16 |
import simplejson as js |
| 17 |
except ImportError: |
| 18 |
import json as js # type: ignore[no-redef] |
| 19 |
|
| 20 |
from . import defs |
| 21 |
from . import expr as fexpr |
| 22 |
from . import obtain |
| 23 |
from . import parser as fparser |
| 24 |
from . import version as fver |
| 25 |
|
| 26 |
|
| 27 |
@dataclasses.dataclass(frozen=True) |
| 28 |
class Config: |
| 29 |
"""Runtime configuration for this program.""" |
| 30 |
|
| 31 |
args: list[str] |
| 32 |
display_version: bool |
| 33 |
features_prefix: str |
| 34 |
option_name: str |
| 35 |
output_format: str |
| 36 |
|
| 37 |
program: str = "(unknown)" |
| 38 |
|
| 39 |
|
| 40 |
def arg_version(_ctx: click.Context, _self: click.Parameter, value: bool) -> bool: # noqa: FBT001 |
| 41 |
"""Display program version information.""" |
| 42 |
if not value: |
| 43 |
return value |
| 44 |
|
| 45 |
print(f"feature-check {defs.VERSION}") |
| 46 |
sys.exit(0) |
| 47 |
|
| 48 |
|
| 49 |
def arg_features(_ctx: click.Context, _self: click.Parameter, value: bool) -> bool: # noqa: FBT001 |
| 50 |
"""Display program features information.""" |
| 51 |
if not value: |
| 52 |
return value |
| 53 |
|
| 54 |
print(f"{defs.DEFAULT_PREFIX}feature-check={defs.VERSION} single=1.0 list=1.0 simple=1.0") |
| 55 |
sys.exit(0) |
| 56 |
|
| 57 |
|
| 58 |
def output_tsv(data: dict[str, fver.Version]) -> None: |
| 59 |
"""List the obtained features as tab-separated name/value pairs.""" |
| 60 |
for feature in sorted(data.keys()): |
| 61 |
print(f"{feature}\t{data[feature].value}") |
| 62 |
|
| 63 |
|
| 64 |
def output_json(data: dict[str, fver.Version]) -> None: |
| 65 |
"""List the obtained features as a JSON object.""" |
| 66 |
print( |
| 67 |
js.dumps( |
| 68 |
{name: value.value for name, value in data.items()}, |
| 69 |
sort_keys=True, |
| 70 |
indent=2, |
| 71 |
), |
| 72 |
) |
| 73 |
|
| 74 |
|
| 75 |
OUTPUT = {"tsv": output_tsv, "json": output_json} |
| 76 |
|
| 77 |
|
| 78 |
def process(mode: defs.Mode, cfg: Config, data: dict[str, fver.Version]) -> None: |
| 79 |
"""Perform the requested feature-check operation.""" |
| 80 |
match mode: |
| 81 |
case defs.ModeList(): |
| 82 |
OUTPUT[cfg.output_format](data) |
| 83 |
|
| 84 |
case defs.ModeSingle(feature, _): |
| 85 |
if feature in data: |
| 86 |
if cfg.display_version: |
| 87 |
print(data[feature].value) |
| 88 |
sys.exit(0) |
| 89 |
else: |
| 90 |
sys.exit(1) |
| 91 |
|
| 92 |
case defs.ModeSimple(ast): |
| 93 |
res: Final = ast.evaluate(data) |
| 94 |
if not isinstance(res, fexpr.ResultBool): |
| 95 |
sys.exit(f"Internal error: did not expect a {type(res).__name__} object") |
| 96 |
sys.exit(not res.value) |
| 97 |
|
| 98 |
case _: |
| 99 |
sys.exit(f"Internal error: process(mode={mode!r}, cfg={cfg!r}, data={data!r}") |
| 100 |
|
| 101 |
|
| 102 |
@click.command(name="feature-check") |
| 103 |
@click.help_option("--help", "-h") |
| 104 |
@click.option( |
| 105 |
"--version", |
| 106 |
"-V", |
| 107 |
is_flag=True, |
| 108 |
is_eager=True, |
| 109 |
callback=arg_version, |
| 110 |
help="display program version information and exit", |
| 111 |
) |
| 112 |
@click.option( |
| 113 |
"--features", |
| 114 |
is_flag=True, |
| 115 |
is_eager=True, |
| 116 |
callback=arg_features, |
| 117 |
help="display supported features and exit", |
| 118 |
) |
| 119 |
@click.option( |
| 120 |
"-v", |
| 121 |
"--display-version", |
| 122 |
is_flag=True, |
| 123 |
help="display the feature version", |
| 124 |
) |
| 125 |
@click.option( |
| 126 |
"-P", |
| 127 |
"--features-prefix", |
| 128 |
type=str, |
| 129 |
default=defs.DEFAULT_PREFIX, |
| 130 |
help="the features prefix in the program output", |
| 131 |
) |
| 132 |
@click.option( |
| 133 |
"-l", |
| 134 |
"--list", |
| 135 |
"opt_list", |
| 136 |
is_flag=True, |
| 137 |
help="list the features supported by a program", |
| 138 |
) |
| 139 |
@click.option( |
| 140 |
"-O", |
| 141 |
"--option-name", |
| 142 |
type=str, |
| 143 |
default=defs.DEFAULT_OPTION, |
| 144 |
help="the query-features option to pass", |
| 145 |
) |
| 146 |
@click.option( |
| 147 |
"-o", |
| 148 |
"--output-format", |
| 149 |
type=click.Choice(sorted(OUTPUT.keys())), |
| 150 |
default=defs.DEFAULT_OUTPUT_FMT, |
| 151 |
help="specify the output format for the list", |
| 152 |
) |
| 153 |
@click.argument("program", type=str) |
| 154 |
@click.argument("feature", type=str, required=False) |
| 155 |
@click.argument("op", type=str, required=False) |
| 156 |
@click.argument("feature_version", type=str, required=False, metavar="VERSION") |
| 157 |
@click.pass_context |
| 158 |
def main( # noqa: PLR0913 # yep, we accept a lot of command-line options |
| 159 |
ctx: click.Context, |
| 160 |
*, |
| 161 |
program: str, |
| 162 |
feature: str | None, |
| 163 |
op: str | None, |
| 164 |
display_version: bool, |
| 165 |
feature_version: str | None, |
| 166 |
features: bool, |
| 167 |
features_prefix: str, |
| 168 |
opt_list: bool, |
| 169 |
option_name: str, |
| 170 |
output_format: str, |
| 171 |
version: bool, |
| 172 |
) -> None: |
| 173 |
"""Parse command-line arguments, do things.""" |
| 174 |
|
| 175 |
def parse_mode() -> defs.Mode: |
| 176 |
"""Determine the mode of operation.""" |
| 177 |
if opt_list: |
| 178 |
return defs.ModeList() |
| 179 |
|
| 180 |
if not cfg.args: |
| 181 |
ctx.fail("No feature specified") |
| 182 |
expr: Final = " ".join(cfg.args) |
| 183 |
try: |
| 184 |
return fparser.parse_expr(expr) |
| 185 |
except fparser.ParseError: |
| 186 |
ctx.fail("Only querying a single feature supported so far") |
| 187 |
|
| 188 |
if features or version: |
| 189 |
ctx.fail(f"How did we get to main() with {features=!r} and {version=!r}?") |
| 190 |
|
| 191 |
cfg: Final = Config( |
| 192 |
program=program, |
| 193 |
args=[value for value in [feature, op, feature_version] if value is not None], |
| 194 |
display_version=display_version, |
| 195 |
features_prefix=features_prefix, |
| 196 |
option_name=option_name, |
| 197 |
output_format=output_format, |
| 198 |
) |
| 199 |
|
| 200 |
mode: Final = parse_mode() |
| 201 |
|
| 202 |
try: |
| 203 |
data: Final = obtain.obtain_features(cfg.program, cfg.option_name, cfg.features_prefix) |
| 204 |
except obtain.ObtainError as exc: |
| 205 |
sys.exit(exc.code) |
| 206 |
|
| 207 |
process(mode, cfg, data) |
| 208 |
|
| 209 |
|
| 210 |
if __name__ == "__main__": |
| 211 |
main() |