Skip to content

Commit 0407ee2

Browse files
committed
Updated scripts/linkfix.py
1 parent 1507bdc commit 0407ee2

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

scripts/linkfix.py

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,71 @@
11
import json
22
import os
33
from os.path import join, dirname
4+
from color import error, success, warning
45

56
file = os.path.realpath(__file__)
67
base = dirname(dirname(file))
78
source = join(base, "source")
89
linkcheck_build = join(base, "build", "linkcheck")
910
output_file = join(linkcheck_build, "output.json")
1011

11-
with open(output_file, 'r', encoding = 'utf-8') as f:
12-
# sphinx produces multiple records instead of a list of records for broken links
13-
records = [json.loads(i) for i in f.readlines()]
12+
try:
13+
with open(output_file, 'r', encoding = 'utf-8') as f:
14+
# sphinx produces multiple records instead of a list of records for broken links
15+
records = [json.loads(i) for i in f.readlines()]
16+
except FileNotFoundError:
17+
error("ERROR: Run 'make linkcheck' first.")
18+
exit()
19+
20+
21+
warning("If you do a mistake while replacing the urls you can just terminate the script before it ends (CTRL + C).\n")
1422

1523
cached_replacements = {}
1624
replacements = []
25+
broken = 0
1726
for record in records:
1827
status = record['status']
1928
uri = record['uri']
29+
2030
if status == "working":
2131
continue
2232
elif status == "unchecked":
23-
#print("INFO: Url {!r} is not checked.".format(uri))
33+
##print("INFO: Url {!r} is not checked.".format(uri))
2434
continue
2535
elif status == "redirected":
2636
# check whether the redirect is permanent
2737
if record['code'] != 301:
28-
#print("INFO: Passing {!r} since the redirection is not permanent.".format(uri))
38+
##print("INFO: Passing {!r} since the redirection is not permanent.".format(uri))
2939
continue
3040
replace_with = record['info']
3141
elif status == "broken":
3242
try:
3343
replace_with = cached_replacements[uri]
3444
except KeyError:
35-
replace_with = input("\nThe URL {!r} is broken, what do you want to change it with? You can also pass this question if you want to do nothing.\n > ".format(uri))
36-
if not replace_with:
37-
print("Passing.")
45+
broken += 1
46+
print("File:\t" + record['filename'])
47+
print("url:\t" + uri)
48+
replace_with = input("The above url is broken, what do you want to change it with? You can also pass this question if you want to do nothing.\n > ")
49+
if replace_with and not replace_with.isspace():
50+
success("Replaced the link.\n\n")
51+
else:
52+
warning("Passing.\n\n")
3853
continue
3954
elif status == "ignored":
4055
continue
4156
else:
42-
raise ValueError("ERROR: Unknown status for URL {!r}: {!r}".format(uri, status))
57+
raise ValueError("Unknown status for URL {!r}: {!r}".format(uri, status))
4358

4459
# used for :target: special casing
4560
if replace_with == '.':
4661
continue
4762

4863
replacements.append((record, replace_with))
4964

50-
if len(replacements) == 0:
51-
print("All links are uptodate, not any broken links.")
65+
if broken == 0:
66+
success("All links are uptodate, not any broken links.")
5267
else:
53-
print(f"Changed {len(replacements)} links.")
68+
success(f"Changed {len(replacements)}/{broken} broken links.")
5469

5570
for record, replacement in replacements:
5671
rst_file = join(source, record['filename'])
@@ -62,7 +77,7 @@
6277
if uri not in data:
6378
alternative_uri = uri.split(":", 1)[0]
6479
if alternative_uri not in data:
65-
print("WARNING: Can't find the URL {!r} in file {}:{}. Might be about a :target: directive.".format(uri, rst_file, record['lineno']))
80+
warning("WARNING: Can't find the URL {!r} in file {}:{}. Might be about a :target: directive.".format(uri, rst_file, record['lineno']))
6681
continue
6782
uri = alternative_uri
6883

scripts/move_documents.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def check_dir(d, hint = ""):
4646

4747

4848
# fix this, we shouldn't be deleting it altogether
49+
# also we might want to preserve the output files (PDF, EPUB) in a permanent directory in case the script gets killed just after deleting docs
4950
if exists(docs):
5051
shutil.rmtree(docs)
5152

0 commit comments

Comments
 (0)