File: linkfix.py

package info (click to toggle)
python-scrapy 2.4.1-2%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,748 kB
  • sloc: python: 32,888; xml: 199; makefile: 90; sh: 7
file content (70 lines) | stat: -rwxr-xr-x 1,969 bytes parent folder | download
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
#!/usr/bin/python

"""

Linkfix - a companion to sphinx's linkcheck builder.

Uses the linkcheck's output file to fix links in docs.

Originally created for this issue:
https://github.com/scrapy/scrapy/issues/606

Author: dufferzafar
"""

import re


def main():

    # Used for remembering the file (and its contents)
    # so we don't have to open the same file again.
    _filename = None
    _contents = None

    # A regex that matches standard linkcheck output lines
    line_re = re.compile(r'(.*)\:\d+\:\s\[(.*)\]\s(?:(.*)\sto\s(.*)|(.*))')

    # Read lines from the linkcheck output file
    try:
        with open("build/linkcheck/output.txt") as out:
            output_lines = out.readlines()
    except IOError:
        print("linkcheck output not found; please run linkcheck first.")
        exit(1)

    # For every line, fix the respective file
    for line in output_lines:
        match = re.match(line_re, line)

        if match:
            newfilename = match.group(1)
            errortype = match.group(2)

            # Broken links can't be fixed and
            # I am not sure what do with the local ones.
            if errortype.lower() in ["broken", "local"]:
                print("Not Fixed: " + line)
            else:
                # If this is a new file
                if newfilename != _filename:

                    # Update the previous file
                    if _filename:
                        with open(_filename, "w") as _file:
                            _file.write(_contents)

                    _filename = newfilename

                    # Read the new file to memory
                    with open(_filename) as _file:
                        _contents = _file.read()

                _contents = _contents.replace(match.group(3), match.group(4))
        else:
            # We don't understand what the current line means!
            print("Not Understood: " + line)


if __name__ == '__main__':
    main()