Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the legacy CLI Alias code. #21743

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 0 additions & 123 deletions src/python/pants/option/alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,11 @@

from __future__ import annotations

import logging
import re
import shlex
from dataclasses import dataclass, field
from itertools import chain
from typing import Generator

from pants.option.errors import OptionsError
from pants.option.option_types import DictOption
from pants.option.scope import ScopeInfo
from pants.option.subsystem import Subsystem
from pants.util.docutil import bin_name
from pants.util.frozendict import FrozenDict
from pants.util.strutil import softwrap

logger = logging.getLogger(__name__)


class CliAliasError(OptionsError):
pass


class CliAliasCycleError(CliAliasError):
pass


class CliAliasInvalidError(CliAliasError):
pass


class CliOptions(Subsystem):
options_scope = "cli"
Expand All @@ -57,102 +33,3 @@ class CliOptions(Subsystem):
"""
),
)


@dataclass(frozen=True)
class CliAlias:
definitions: FrozenDict[str, tuple[str, ...]] = field(default_factory=FrozenDict)

def __post_init__(self):
valid_alias_re = re.compile(r"(--)?\w(\w|-)*\w$", re.IGNORECASE)
for alias in self.definitions.keys():
if not re.match(valid_alias_re, alias):
raise CliAliasInvalidError(
softwrap(
f"""
Invalid alias in `[cli].alias` option: {alias!r}. May only contain alpha
numerical letters and the separators `-` and `_`. Flags can be defined using
`--`. A single dash is not allowed.
"""
)
)

@classmethod
def from_dict(cls, aliases: dict[str, str]) -> CliAlias:
definitions = {key: tuple(shlex.split(value)) for key, value in aliases.items()}

def expand(
definition: tuple[str, ...], *trail: str
) -> Generator[tuple[str, ...], None, None]:
for arg in definition:
if arg not in definitions:
yield (arg,)
else:
if arg in trail:
raise CliAliasCycleError(
"CLI alias cycle detected in `[cli].alias` option:\n"
+ " -> ".join([arg, *trail])
)
yield from expand(definitions[arg], arg, *trail)

return cls(
FrozenDict(
{
alias: tuple(chain.from_iterable(expand(definition)))
for alias, definition in definitions.items()
}
)
)

def check_name_conflicts(
self, known_scopes: dict[str, ScopeInfo], known_flags: dict[str, frozenset[str]]
) -> None:
for alias in self.definitions.keys():
scope = known_scopes.get(alias)

if scope:
raise CliAliasInvalidError(
softwrap(
f"""
Invalid alias in `[cli].alias` option: {alias!r}. This is already a
registered {"goal" if scope.is_goal else "subsystem"}.
"""
)
)

for scope_name, args in known_flags.items():
for alias in self.definitions.keys():
if alias in args:
scope_name = scope_name or "global"
raise CliAliasInvalidError(
softwrap(
f"""
Invalid flag-like alias in `[cli].alias` option: {alias!r}. This is
already a registered flag in the {scope_name!r} scope.
"""
)
)

def expand_args(self, args: tuple[str, ...]) -> tuple[str, ...]:
if not self.definitions:
return args
return tuple(self._do_expand_args(args))

def _do_expand_args(self, args: tuple[str, ...]) -> Generator[str, None, None]:
args_iter = iter(args)
for arg in args_iter:
if arg == "--":
# Do not expand pass through arguments.
yield arg
yield from args_iter
return

expanded = self.maybe_expand(arg)
if expanded:
logger.debug(f"Expanded [cli.alias].{arg} => {' '.join(expanded)}")
yield from expanded
else:
yield arg

def maybe_expand(self, arg: str) -> tuple[str, ...] | None:
return self.definitions.get(arg)
236 changes: 0 additions & 236 deletions src/python/pants/option/alias_test.py

This file was deleted.

Loading