2022-04-15 11:34:17 +00:00
|
|
|
"""Check for alias collisions within the codebase"""
|
|
|
|
|
2022-11-22 15:24:15 +00:00
|
|
|
from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser
|
2022-04-15 11:34:17 +00:00
|
|
|
from pathlib import Path
|
|
|
|
from dataclasses import dataclass
|
|
|
|
import itertools
|
|
|
|
import re
|
|
|
|
|
|
|
|
|
2022-11-22 16:57:15 +00:00
|
|
|
ERROR_MESSAGE_TEMPLATE = (
|
|
|
|
"Alias `%s` defined in `%s` already exists as alias `%s` in `%s`."
|
|
|
|
)
|
2022-04-15 11:34:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
def dir_path(path_string: str) -> Path:
|
|
|
|
if Path(path_string).is_dir():
|
|
|
|
return Path(path_string)
|
|
|
|
else:
|
|
|
|
raise NotADirectoryError(path_string)
|
|
|
|
|
|
|
|
|
|
|
|
def parse_arguments():
|
|
|
|
parser = ArgumentParser(
|
|
|
|
description=__doc__,
|
|
|
|
formatter_class=ArgumentDefaultsHelpFormatter,
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"folder",
|
|
|
|
type=dir_path,
|
|
|
|
help="Folder to check",
|
|
|
|
)
|
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
|
|
class Alias:
|
|
|
|
|
|
|
|
alias: str
|
|
|
|
value: str
|
|
|
|
module: Path
|
|
|
|
|
|
|
|
|
2022-11-22 16:57:15 +00:00
|
|
|
@dataclass(frozen=True)
|
|
|
|
class Collision:
|
|
|
|
|
|
|
|
existing_alias: Alias
|
|
|
|
new_alias: Alias
|
|
|
|
|
|
|
|
|
|
|
|
def find_aliases_in_file(file: Path) -> list[Alias]:
|
2022-04-15 11:34:17 +00:00
|
|
|
matches = re.findall(r"^alias (.*)='(.*)'", file.read_text(), re.M)
|
|
|
|
return [Alias(match[0], match[1], file) for match in matches]
|
|
|
|
|
|
|
|
|
2022-11-22 16:57:15 +00:00
|
|
|
def find_all_aliases(path: Path) -> list:
|
2022-04-15 11:34:17 +00:00
|
|
|
files = list(path.rglob("*.zsh"))
|
|
|
|
aliases = [find_aliases_in_file(file) for file in files]
|
|
|
|
return list(itertools.chain(*aliases))
|
|
|
|
|
|
|
|
|
2022-11-22 16:57:15 +00:00
|
|
|
def check_for_duplicates(aliases: list[Alias]) -> list[Collision]:
|
2022-11-22 15:24:15 +00:00
|
|
|
elements = {}
|
2022-11-22 16:57:15 +00:00
|
|
|
collisions = []
|
2022-04-15 11:34:17 +00:00
|
|
|
for alias in aliases:
|
|
|
|
if alias.alias in elements:
|
|
|
|
existing = elements[alias.alias]
|
2022-11-22 16:57:15 +00:00
|
|
|
collisions.append(Collision(existing, alias))
|
|
|
|
else:
|
|
|
|
elements[alias.alias] = alias
|
|
|
|
return collisions
|
|
|
|
|
|
|
|
|
|
|
|
def print_collisions(collisions: dict[Alias, Alias]) -> None:
|
|
|
|
if collisions:
|
|
|
|
print(f"Found {len(collisions)} alias collisions:\n")
|
|
|
|
for collision in collisions:
|
|
|
|
print(
|
2022-04-15 11:34:17 +00:00
|
|
|
ERROR_MESSAGE_TEMPLATE
|
|
|
|
% (
|
2022-11-22 16:57:15 +00:00
|
|
|
f"{collision.new_alias.alias}={collision.new_alias.value}",
|
|
|
|
collision.new_alias.module.name,
|
|
|
|
f"{collision.existing_alias.alias}={collision.existing_alias.value}",
|
|
|
|
collision.existing_alias.module.name,
|
2022-04-15 11:34:17 +00:00
|
|
|
)
|
|
|
|
)
|
2022-11-22 16:57:15 +00:00
|
|
|
print("\nConsider renaming your aliases.")
|
|
|
|
else:
|
|
|
|
print("Found no collisions")
|
2022-04-15 11:34:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
"""main"""
|
|
|
|
args = parse_arguments()
|
|
|
|
aliases = find_all_aliases(args.folder)
|
2022-11-22 16:57:15 +00:00
|
|
|
collisions = check_for_duplicates(aliases)
|
|
|
|
print_collisions(collisions)
|
2022-11-22 16:58:17 +00:00
|
|
|
# TODO enable once all collisions are fixed
|
|
|
|
# if collisions:
|
|
|
|
# exit(-1)
|
2022-04-15 11:34:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|