|
1 | 1 | import json |
2 | 2 | import os |
3 | 3 | from os.path import join, dirname |
| 4 | +from color import error, success, warning |
4 | 5 |
|
5 | 6 | file = os.path.realpath(__file__) |
6 | 7 | base = dirname(dirname(file)) |
7 | 8 | source = join(base, "source") |
8 | 9 | linkcheck_build = join(base, "build", "linkcheck") |
9 | 10 | output_file = join(linkcheck_build, "output.json") |
10 | 11 |
|
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") |
14 | 22 |
|
15 | 23 | cached_replacements = {} |
16 | 24 | replacements = [] |
| 25 | +broken = 0 |
17 | 26 | for record in records: |
18 | 27 | status = record['status'] |
19 | 28 | uri = record['uri'] |
| 29 | + |
20 | 30 | if status == "working": |
21 | 31 | continue |
22 | 32 | elif status == "unchecked": |
23 | | - #print("INFO: Url {!r} is not checked.".format(uri)) |
| 33 | + ##print("INFO: Url {!r} is not checked.".format(uri)) |
24 | 34 | continue |
25 | 35 | elif status == "redirected": |
26 | 36 | # check whether the redirect is permanent |
27 | 37 | 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)) |
29 | 39 | continue |
30 | 40 | replace_with = record['info'] |
31 | 41 | elif status == "broken": |
32 | 42 | try: |
33 | 43 | replace_with = cached_replacements[uri] |
34 | 44 | 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") |
38 | 53 | continue |
39 | 54 | elif status == "ignored": |
40 | 55 | continue |
41 | 56 | 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)) |
43 | 58 |
|
44 | 59 | # used for :target: special casing |
45 | 60 | if replace_with == '.': |
46 | 61 | continue |
47 | 62 |
|
48 | 63 | replacements.append((record, replace_with)) |
49 | 64 |
|
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.") |
52 | 67 | else: |
53 | | - print(f"Changed {len(replacements)} links.") |
| 68 | + success(f"Changed {len(replacements)}/{broken} broken links.") |
54 | 69 |
|
55 | 70 | for record, replacement in replacements: |
56 | 71 | rst_file = join(source, record['filename']) |
|
62 | 77 | if uri not in data: |
63 | 78 | alternative_uri = uri.split(":", 1)[0] |
64 | 79 | 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'])) |
66 | 81 | continue |
67 | 82 | uri = alternative_uri |
68 | 83 |
|
|
0 commit comments