/
bpo_redirecter.py
executable file
·175 lines (143 loc) · 5.18 KB
/
bpo_redirecter.py
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
#!/usr/bin/env python3
"""
Update BPO links to GH issues in a file or directory of files
"""
from __future__ import annotations
import argparse
import difflib
import logging
import os
import re
import sys
from functools import cache
from pathlib import Path
from typing import Iterable
import httpx # pip install httpx
import httpx_cache # pip install httpx-cache
from termcolor import colored, cprint # pip install termcolor
try:
from rich.logging import RichHandler # pip install rich
except ImportError:
RichHandler = None
pass
# For example:
# http://bugs.python.org/issue5237
# https://bugs.python.org/issue9633
BPO_URL_REGEX = re.compile(r"https?://bugs\.python\.org/issue(\d+)")
# For example:
# :issue:`45440`
BPO_ROLE_REGEX = re.compile(r":issue:`(\d+)`")
@cache
def redirect(client: httpx.Client, bpo_number: int) -> str:
redirect_link = f"https://bugs.python.org/issue?@action=redirect&bpo={bpo_number}"
logging.info("Redirect link:\t%s", redirect_link)
r = client.get(redirect_link, follow_redirects=True)
logging.info("GitHub link:\t%s", r.url)
return str(r.url)
def color_diff(diff: Iterable[str]) -> Iterable[str]:
for line in diff:
if line.startswith("+"):
yield colored(line, "green")
elif line.startswith("-"):
yield colored(line, "red")
else:
yield line
def do_file(filename: str, dry_run: bool = False) -> None:
with open(filename) as f:
old_lines = f.readlines()
new_lines = do_lines(old_lines, filename)
if not dry_run and new_lines != old_lines:
with open(filename, "w") as f:
f.writelines(new_lines)
def do_lines(old_lines: list[str], filename: str) -> list[str]:
with httpx_cache.Client(cache=httpx_cache.FileCache()) as client:
changes = 0
new_lines = []
for line in old_lines:
# print(line.strip())
matches = 0
ms = BPO_URL_REGEX.findall(line.strip())
new_line = line
for m in ms:
logging.info("Old line:\t%s", line.rstrip())
# bpo_link = f"https://bugs.python.org/issue{m}"
# logging.info("BPO link:\t%s", bpo_link)
bpo_number = int(m)
logging.info("BPO number:\t%d", bpo_number)
gh_link = redirect(client, bpo_number)
new_line = BPO_URL_REGEX.sub(gh_link, new_line, count=1)
logging.info("New line:\t%s", new_line.rstrip())
if line != new_line:
changes += 1
matches += 1
new_lines.append(new_line)
ms = BPO_ROLE_REGEX.findall(line.strip())
new_line = line
for m in ms:
logging.info("Old line:\t%s", line.rstrip())
# bpo_role = f":issue:`{m}`"
# logging.info("BPO link:\t%s", bpo_role)
bpo_number = int(m)
logging.info("BPO number:\t%d", bpo_number)
gh_link = redirect(client, bpo_number)
gh_number = gh_link.split("/")[-1]
new_role = f":gh:`{gh_number}`"
logging.info("New role:\t%s", new_role)
new_line = BPO_ROLE_REGEX.sub(new_role, new_line, count=1)
logging.info("New line:\t%s", new_line.rstrip())
if line != new_line:
changes += 1
matches += 1
new_lines.append(new_line)
if matches == 0:
new_lines.append(line)
if changes:
diff = color_diff(
difflib.unified_diff(
old_lines, new_lines, fromfile=filename, tofile=filename
)
)
print("".join(diff))
else:
cprint(f"no change for {filename}", "yellow")
if changes:
return new_lines
return old_lines
def do_file_or_path(file_or_path: str, dry_run: bool = False) -> None:
if os.path.isfile(file_or_path):
do_file(file_or_path, dry_run)
else:
for p in sorted(Path(file_or_path).rglob("*")):
if p.suffix in (".py", ".rst", ".txt") and p.is_file():
logging.info(p)
do_file(str(p), dry_run)
# print()
def main() -> None:
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument("input", help="File or directory to redirect")
parser.add_argument(
"-n", "--dry-run", action="store_true", help="Show but don't save changes"
)
parser.add_argument(
"-v",
"--verbose",
action="store_const",
dest="loglevel",
const=logging.INFO,
default=logging.WARNING,
help="Print extra messages to stderr",
)
args = parser.parse_args()
if RichHandler:
logging.basicConfig(
level=args.loglevel, format="%(message)s", handlers=[RichHandler()]
)
else:
logging.basicConfig(level=args.loglevel, format="%(message)s")
do_file_or_path(args.input, args.dry_run)
logging.info(redirect.cache_info())
if __name__ == "__main__":
sys.exit(main())