maintainers/scripts/remove-old-aliases: add support for creating warnings
Also revert the addition of the "elif '"' in line:" condition: lines with quotes are generally OK. Co-authored-by: Emily <vcs@emily.moe>
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
#!/usr/bin/env nix-shell
|
#!/usr/bin/env nix-shell
|
||||||
#!nix-shell -i python3 -p "python3.withPackages(ps: with ps; [ ])" nix
|
#!nix-shell -i python3 -p "python3.withPackages(ps: with ps; [ ])" nix
|
||||||
"""
|
"""
|
||||||
A program to remove old aliases or convert old aliases to throws
|
Converts old aliases to warnings, converts old warnings to throws, and removes old throws.
|
||||||
Example usage:
|
Example usage:
|
||||||
./maintainers/scripts/remove-old-aliases.py --year 2018 --file ./pkgs/top-level/aliases.nix
|
./maintainers/scripts/remove-old-aliases.py --year 2018 --file ./pkgs/top-level/aliases.nix
|
||||||
|
|
||||||
@@ -31,21 +31,40 @@ def process_args() -> argparse.Namespace:
|
|||||||
arg_parser.add_argument(
|
arg_parser.add_argument(
|
||||||
"--only-throws",
|
"--only-throws",
|
||||||
action="store_true",
|
action="store_true",
|
||||||
help="only operate on throws. e.g remove throws older than $date",
|
help="Deprecated, use --only throws instead",
|
||||||
|
)
|
||||||
|
arg_parser.add_argument(
|
||||||
|
"--only",
|
||||||
|
choices=["aliases", "warnings", "throws"],
|
||||||
|
help="Only act on the specified types"
|
||||||
|
"(i.e. only act on entries that are 'normal' aliases, warnings, or throws)."
|
||||||
|
"Can be repeated.",
|
||||||
|
action="append",
|
||||||
|
dest="operate_on",
|
||||||
)
|
)
|
||||||
arg_parser.add_argument("--file", required=True, type=Path, help="alias file")
|
arg_parser.add_argument("--file", required=True, type=Path, help="alias file")
|
||||||
arg_parser.add_argument(
|
arg_parser.add_argument(
|
||||||
"--dry-run", action="store_true", help="don't modify files, only print results"
|
"--dry-run", action="store_true", help="don't modify files, only print results"
|
||||||
)
|
)
|
||||||
return arg_parser.parse_args()
|
|
||||||
|
parsed = arg_parser.parse_args()
|
||||||
|
|
||||||
|
if parsed.only_throws:
|
||||||
|
parsed.operate_on.append("throws")
|
||||||
|
|
||||||
|
if parsed.operate_on is None:
|
||||||
|
parsed.operate_on = ["aliases", "warnings", "throws"]
|
||||||
|
|
||||||
|
return parsed
|
||||||
|
|
||||||
|
|
||||||
def get_date_lists(
|
def get_date_lists(
|
||||||
txt: list[str], cutoffdate: datetimedate, only_throws: bool
|
txt: list[str], cutoffdate: datetimedate
|
||||||
) -> tuple[list[str], list[str], list[str], list[str]]:
|
) -> tuple[list[str], list[str], list[str], list[str], list[str]]:
|
||||||
"""get a list of lines in which the date is older than $cutoffdate"""
|
"""get a list of lines in which the date is older than $cutoffdate"""
|
||||||
date_older_list: list[str] = []
|
date_older_list: list[str] = []
|
||||||
date_older_throw_list: list[str] = []
|
date_older_throw_list: list[str] = []
|
||||||
|
date_older_warning_list: list[str] = []
|
||||||
date_sep_line_list: list[str] = []
|
date_sep_line_list: list[str] = []
|
||||||
date_too_complex_list: list[str] = []
|
date_too_complex_list: list[str] = []
|
||||||
|
|
||||||
@@ -71,8 +90,7 @@ def get_date_lists(
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
if line.lstrip().startswith("inherit (") and ";" in line:
|
if line.lstrip().startswith("inherit (") and ";" in line:
|
||||||
if not only_throws:
|
date_older_list.append(line)
|
||||||
date_older_list.append(line)
|
|
||||||
elif "=" not in line:
|
elif "=" not in line:
|
||||||
date_sep_line_list.append(f"{lineno:>5} {line}")
|
date_sep_line_list.append(f"{lineno:>5} {line}")
|
||||||
# 'if' lines could be complicated
|
# 'if' lines could be complicated
|
||||||
@@ -80,13 +98,14 @@ def get_date_lists(
|
|||||||
date_too_complex_list.append(f"{lineno:>5} {line}")
|
date_too_complex_list.append(f"{lineno:>5} {line}")
|
||||||
elif "= with " in line:
|
elif "= with " in line:
|
||||||
date_too_complex_list.append(f"{lineno:>5} {line}")
|
date_too_complex_list.append(f"{lineno:>5} {line}")
|
||||||
elif "lib.warnOnInstantiate" in line:
|
elif "lib.warnOnInstantiate" in line or "warning" in line:
|
||||||
date_too_complex_list.append(f"{lineno:>5} {line}")
|
if 'lib.warnOnInstantiate "' in line:
|
||||||
elif '"' in line:
|
date_older_warning_list.append(line)
|
||||||
date_too_complex_list.append(f"{lineno:>5} {line}")
|
else:
|
||||||
|
date_too_complex_list.append(f"{lineno:>5} {line}")
|
||||||
elif " = throw" in line:
|
elif " = throw" in line:
|
||||||
date_older_throw_list.append(line)
|
date_older_throw_list.append(line)
|
||||||
elif not only_throws:
|
else:
|
||||||
date_older_list.append(line)
|
date_older_list.append(line)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -94,13 +113,14 @@ def get_date_lists(
|
|||||||
date_sep_line_list,
|
date_sep_line_list,
|
||||||
date_too_complex_list,
|
date_too_complex_list,
|
||||||
date_older_throw_list,
|
date_older_throw_list,
|
||||||
|
date_older_warning_list,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def convert_to_throw(date_older_list: list[str]) -> list[tuple[str, str]]:
|
def convert(lines: list[str], convert_to: str) -> list[tuple[str, str]]:
|
||||||
"""convert a list of lines to throws"""
|
"""convert a list of lines to either "throws" or "warnings"."""
|
||||||
converted_list = []
|
converted_lines = {}
|
||||||
for line in date_older_list.copy():
|
for line in lines.copy():
|
||||||
indent: str = " " * (len(line) - len(line.lstrip()))
|
indent: str = " " * (len(line) - len(line.lstrip()))
|
||||||
|
|
||||||
if "=" not in line:
|
if "=" not in line:
|
||||||
@@ -117,10 +137,14 @@ def convert_to_throw(date_older_list: list[str]) -> list[tuple[str, str]]:
|
|||||||
before_equal = ""
|
before_equal = ""
|
||||||
after_equal = ""
|
after_equal = ""
|
||||||
try:
|
try:
|
||||||
before_equal, after_equal = (x.strip() for x in line.split("=", maxsplit=2))
|
before_equal, after_equal = (
|
||||||
|
x.strip() for x in line.split("=", maxsplit=2)
|
||||||
|
)
|
||||||
|
if after_equal.startswith("lib.warnOnInstantiate"):
|
||||||
|
after_equal = after_equal.split("\"", maxsplit=3)[2].strip()
|
||||||
except ValueError as err:
|
except ValueError as err:
|
||||||
print(err, line, "\n")
|
print(err, line, "\n")
|
||||||
date_older_list.remove(line)
|
lines.remove(line)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
alias = before_equal
|
alias = before_equal
|
||||||
@@ -129,32 +153,40 @@ def convert_to_throw(date_older_list: list[str]) -> list[tuple[str, str]]:
|
|||||||
alias_unquoted = alias.strip('"')
|
alias_unquoted = alias.strip('"')
|
||||||
replacement = replacement.removeprefix("pkgs.")
|
replacement = replacement.removeprefix("pkgs.")
|
||||||
|
|
||||||
converted = (
|
if convert_to == "throws":
|
||||||
f"{indent}{alias} = throw \"'{alias_unquoted}' has been"
|
converted = (
|
||||||
f" renamed to/replaced by '{replacement}'\";"
|
f"{indent}{alias} = throw \"'{alias_unquoted}' has been"
|
||||||
f" # Converted to throw {datetime.today().strftime('%Y-%m-%d')}"
|
f" renamed to/replaced by '{replacement}'\";"
|
||||||
)
|
f" # Converted to throw {datetime.today().strftime('%Y-%m-%d')}"
|
||||||
converted_list.append((line, converted))
|
)
|
||||||
|
converted_lines[line] = converted
|
||||||
|
elif convert_to == "warnings":
|
||||||
|
converted = (
|
||||||
|
f"{indent}{alias} = lib.warnOnInstantiate \"'{alias_unquoted}' has been"
|
||||||
|
f" renamed to/replaced by '{replacement}'\" {replacement};"
|
||||||
|
f" # Converted to warning {datetime.today().strftime('%Y-%m-%d')}"
|
||||||
|
)
|
||||||
|
converted_lines[line] = converted
|
||||||
|
else:
|
||||||
|
raise ValueError("'convert_to' must be either 'throws' or 'warnings'")
|
||||||
|
|
||||||
return converted_list
|
return converted_lines
|
||||||
|
|
||||||
|
|
||||||
def generate_text_to_write(
|
def generate_text_to_write(
|
||||||
txt: list[str],
|
txt: list[str],
|
||||||
date_older_list: list[str],
|
converted_lines: dict[str, str],
|
||||||
converted_to_throw: list[tuple[str, str]],
|
|
||||||
date_older_throw_list: list[str],
|
|
||||||
) -> list[str]:
|
) -> list[str]:
|
||||||
"""generate a list of text to be written to the aliasfile"""
|
"""generate a list of text to be written to the aliasfile"""
|
||||||
text_to_write: list[str] = []
|
text_to_write: list[str] = []
|
||||||
for line in txt:
|
for line in txt:
|
||||||
text_to_append: str = ""
|
text_to_append: str = ""
|
||||||
if converted_to_throw:
|
try:
|
||||||
for tupl in converted_to_throw:
|
new_line = converted_lines[line]
|
||||||
if line == tupl[0]:
|
if new_line is not None:
|
||||||
text_to_append = f"{tupl[1]}\n"
|
text_to_write.append(f"{new_line}\n")
|
||||||
if line not in date_older_list and line not in date_older_throw_list:
|
except KeyError:
|
||||||
text_to_append = f"{line}\n"
|
text_to_write.append(f"{line}\n")
|
||||||
if text_to_append:
|
if text_to_append:
|
||||||
text_to_write.append(text_to_append)
|
text_to_write.append(text_to_append)
|
||||||
|
|
||||||
@@ -195,15 +227,15 @@ def main() -> None:
|
|||||||
"""main"""
|
"""main"""
|
||||||
args = process_args()
|
args = process_args()
|
||||||
|
|
||||||
only_throws = args.only_throws
|
|
||||||
aliasfile = Path(args.file).absolute()
|
aliasfile = Path(args.file).absolute()
|
||||||
cutoffdate = (datetime.strptime(f"{args.year}-{args.month}-01", "%Y-%m-%d")).date()
|
cutoffdate = (datetime.strptime(f"{args.year}-{args.month}-01", "%Y-%m-%d")).date()
|
||||||
|
|
||||||
txt: list[str] = (aliasfile.read_text(encoding="utf-8")).splitlines()
|
txt: list[str] = (aliasfile.read_text(encoding="utf-8")).splitlines()
|
||||||
|
|
||||||
date_older_list: list[str] = []
|
date_older_list: list[str] = []
|
||||||
date_sep_line_list: list[str] = []
|
date_older_warning_list: list[str] = []
|
||||||
date_older_throw_list: list[str] = []
|
date_older_throw_list: list[str] = []
|
||||||
|
date_sep_line_list: list[str] = []
|
||||||
date_too_complex_list: list[str] = []
|
date_too_complex_list: list[str] = []
|
||||||
|
|
||||||
(
|
(
|
||||||
@@ -211,18 +243,27 @@ def main() -> None:
|
|||||||
date_sep_line_list,
|
date_sep_line_list,
|
||||||
date_too_complex_list,
|
date_too_complex_list,
|
||||||
date_older_throw_list,
|
date_older_throw_list,
|
||||||
) = get_date_lists(txt, cutoffdate, only_throws)
|
date_older_warning_list,
|
||||||
|
) = get_date_lists(txt, cutoffdate)
|
||||||
|
|
||||||
converted_to_throw: list[tuple[str, str]] = []
|
converted_lines: dict[str, str] = {}
|
||||||
if date_older_list:
|
|
||||||
converted_to_throw = convert_to_throw(date_older_list)
|
if date_older_list and "aliases" in args.operate_on:
|
||||||
print(" Will be converted to throws. ".center(100, "-"))
|
converted_lines.update(convert(date_older_list, "warnings"))
|
||||||
|
print(" Will be converted to warnings. ".center(100, "-"))
|
||||||
for l_n in date_older_list:
|
for l_n in date_older_list:
|
||||||
print(l_n)
|
print(l_n)
|
||||||
|
|
||||||
if date_older_throw_list:
|
if date_older_warning_list and "warnings" in args.operate_on:
|
||||||
|
converted_lines.update(convert(date_older_warning_list, "throws"))
|
||||||
|
print(" Will be converted to throws. ".center(100, "-"))
|
||||||
|
for l_n in date_older_warning_list:
|
||||||
|
print(l_n)
|
||||||
|
|
||||||
|
if date_older_throw_list and "throws" in args.operate_on:
|
||||||
print(" Will be removed. ".center(100, "-"))
|
print(" Will be removed. ".center(100, "-"))
|
||||||
for l_n in date_older_throw_list:
|
for l_n in date_older_throw_list:
|
||||||
|
converted_lines[l_n] = None
|
||||||
print(l_n)
|
print(l_n)
|
||||||
|
|
||||||
if date_too_complex_list:
|
if date_too_complex_list:
|
||||||
@@ -236,9 +277,7 @@ def main() -> None:
|
|||||||
print(l_n)
|
print(l_n)
|
||||||
|
|
||||||
if not args.dry_run:
|
if not args.dry_run:
|
||||||
text_to_write = generate_text_to_write(
|
text_to_write = generate_text_to_write(txt, converted_lines)
|
||||||
txt, date_older_list, converted_to_throw, date_older_throw_list
|
|
||||||
)
|
|
||||||
write_file(aliasfile, text_to_write)
|
write_file(aliasfile, text_to_write)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user