forked from NixOS/nixpkgs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove-old-aliases.py
More file actions
executable file
·285 lines (244 loc) · 9.35 KB
/
Copy pathremove-old-aliases.py
File metadata and controls
executable file
·285 lines (244 loc) · 9.35 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/bin/env nix-shell
#!nix-shell -i python3 -p "python3.withPackages(ps: with ps; [ ])" nix
"""
Converts old aliases to warnings, converts old warnings to throws, and removes old throws.
Example usage:
./maintainers/scripts/remove-old-aliases.py --year 2018 --file ./pkgs/top-level/aliases.nix
Check this file with mypy after every change!
$ mypy --strict maintainers/scripts/remove-old-aliases.py
"""
import argparse
import shutil
import subprocess
from datetime import date as datetimedate
from datetime import datetime
from pathlib import Path
def process_args() -> argparse.Namespace:
"""process args"""
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument(
"--year", required=True, type=int, help="operate on aliases older than $year"
)
arg_parser.add_argument(
"--month",
type=int,
default=1,
help="operate on aliases older than $year-$month",
)
arg_parser.add_argument(
"--only-throws",
action="store_true",
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(
"--dry-run", action="store_true", help="don't modify files, only print results"
)
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(
txt: list[str], cutoffdate: datetimedate
) -> tuple[list[str], list[str], list[str], list[str], list[str]]:
"""get a list of lines in which the date is older than $cutoffdate"""
date_older_list: list[str] = []
date_older_throw_list: list[str] = []
date_older_warning_list: list[str] = []
date_sep_line_list: list[str] = []
date_too_complex_list: list[str] = []
for lineno, line in enumerate(txt, start=1):
line = line.rstrip()
my_date = None
for string in line.split():
string = string.strip(":")
try:
# strip ':' incase there is a string like 2019-11-01:
my_date = datetime.strptime(string, "%Y-%m-%d").date()
except ValueError:
try:
my_date = datetime.strptime(string, "%Y-%m").date()
except ValueError:
continue
if (
my_date is None
or my_date > cutoffdate
or "preserve, reason:" in line.lower()
):
continue
if line.lstrip().startswith("inherit (") and ";" in line:
date_older_list.append(line)
elif "=" not in line:
date_sep_line_list.append(f"{lineno:>5} {line}")
# 'if' lines could be complicated
elif "if " in line and "if =" not in line:
date_too_complex_list.append(f"{lineno:>5} {line}")
elif "= with " in line:
date_too_complex_list.append(f"{lineno:>5} {line}")
elif "warnAlias" in line or "warning" in line:
if 'warnAlias "' in line:
date_older_warning_list.append(line)
else:
date_too_complex_list.append(f"{lineno:>5} {line}")
elif " = throw" in line:
date_older_throw_list.append(line)
else:
date_older_list.append(line)
return (
date_older_list,
date_sep_line_list,
date_too_complex_list,
date_older_throw_list,
date_older_warning_list,
)
def convert(lines: list[str], convert_to: str) -> list[tuple[str, str]]:
"""convert a list of lines to either "throws" or "warnings"."""
converted_lines = {}
for line in lines.copy():
indent: str = " " * (len(line) - len(line.lstrip()))
if "=" not in line:
assert "inherit (" in line
before, sep, after = line.partition("inherit (")
inside, sep, after = after.partition(")")
if not sep:
print(f"FAILED ON {line}")
continue
alias, *_ = after.strip().split(";")[0].split()
replacement = f"{inside.strip()}.{alias}"
else:
before_equal = ""
after_equal = ""
try:
before_equal, after_equal = (
x.strip() for x in line.split("=", maxsplit=2)
)
if after_equal.startswith("warnAlias"):
after_equal = after_equal.split("\"", maxsplit=3)[2].strip()
except ValueError as err:
print(err, line, "\n")
lines.remove(line)
continue
alias = before_equal
replacement = next(x.strip(";:") for x in after_equal.split())
alias_unquoted = alias.strip('"')
replacement = replacement.removeprefix("pkgs.")
if convert_to == "throws":
converted = (
f"{indent}{alias} = throw \"'{alias_unquoted}' has been"
f" renamed to/replaced by '{replacement}'\";"
f" # Converted to throw {datetime.today().strftime('%Y-%m-%d')}"
)
converted_lines[line] = converted
elif convert_to == "warnings":
converted = (
f"{indent}{alias} = warnAlias \"'{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_lines
def generate_text_to_write(
txt: list[str],
converted_lines: dict[str, str],
) -> list[str]:
"""generate a list of text to be written to the aliasfile"""
text_to_write: list[str] = []
for line in txt:
text_to_append: str = ""
try:
new_line = converted_lines[line]
if new_line is not None:
text_to_write.append(f"{new_line}\n")
except KeyError:
text_to_write.append(f"{line}\n")
if text_to_append:
text_to_write.append(text_to_append)
return text_to_write
def write_file(
aliasfile: Path,
text_to_write: list[str],
) -> None:
"""write file"""
temp_aliasfile = Path(f"{aliasfile}.raliases")
with open(temp_aliasfile, "w", encoding="utf-8") as far:
for line in text_to_write:
far.write(line)
print("\nChecking the syntax of the new aliasfile")
try:
subprocess.run(
["nix-instantiate", "--eval", temp_aliasfile],
check=True,
stdout=subprocess.DEVNULL,
)
except subprocess.CalledProcessError:
print(
"\nSyntax check failed,",
"there may have been a line which only has\n"
'aliasname = "reason why";\n'
"when it should have been\n"
'aliasname = throw "reason why";',
)
temp_aliasfile.unlink()
return
shutil.move(f"{aliasfile}.raliases", aliasfile)
print(f"{aliasfile} modified! please verify with 'git diff'.")
def main() -> None:
"""main"""
args = process_args()
aliasfile = Path(args.file).absolute()
cutoffdate = (datetime.strptime(f"{args.year}-{args.month}-01", "%Y-%m-%d")).date()
txt: list[str] = (aliasfile.read_text(encoding="utf-8")).splitlines()
date_older_list: list[str] = []
date_older_warning_list: list[str] = []
date_older_throw_list: list[str] = []
date_sep_line_list: list[str] = []
date_too_complex_list: list[str] = []
(
date_older_list,
date_sep_line_list,
date_too_complex_list,
date_older_throw_list,
date_older_warning_list,
) = get_date_lists(txt, cutoffdate)
converted_lines: dict[str, str] = {}
if date_older_list and "aliases" in args.operate_on:
converted_lines.update(convert(date_older_list, "warnings"))
print(" Will be converted to warnings. ".center(100, "-"))
for l_n in date_older_list:
print(l_n)
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, "-"))
for l_n in date_older_throw_list:
converted_lines[l_n] = None
print(l_n)
if date_too_complex_list:
print(" Too complex, resolve manually. ".center(100, "-"))
for l_n in date_too_complex_list:
print(l_n)
if date_sep_line_list:
print(" On separate line, resolve manually. ".center(100, "-"))
for l_n in date_sep_line_list:
print(l_n)
if not args.dry_run:
text_to_write = generate_text_to_write(txt, converted_lines)
write_file(aliasfile, text_to_write)
if __name__ == "__main__":
main()