forked from Unidata/MetPy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter_links.py
executable file
·45 lines (37 loc) · 1.54 KB
/
filter_links.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
#!/usr/bin/env python
# Copyright (c) 2021 MetPy Developers.
"""Filter links from Sphinx's linkcheck."""
import json
import subprocess
import sys
def get_failing_links(fname):
"""Yield links with problematic statuses."""
with open(fname) as linkfile:
links = json.loads('[' + ','.join(linkfile) + ']')
for link in links:
if link['status'] not in {'working', 'ignored', 'unchecked'}:
yield link
def get_added():
"""Get all lines added in the most recent merge."""
revs = subprocess.check_output(['git', 'rev-list', '--parents', '-n', '1', 'HEAD'])
merge_commit, target, _ = revs.decode('utf-8').split()
diff = subprocess.check_output(['git', 'diff', f'{target}...{merge_commit}'])
return '\n'.join(line for line in diff.decode('utf-8').split('\n')
if line.startswith('+') and not line.startswith('+++'))
if __name__ == '__main__':
# If the second argument is true, then we only want links in the most recent merge,
# otherwise we print all failing links.
if sys.argv[2] in ('true', 'True'):
print('Checking only links in the diff')
added = get_added()
check_link = lambda link: link['uri'] in added
else:
print('Checking all links')
check_link = lambda link: True
ret = 0
for link in get_failing_links(sys.argv[1]):
if check_link(link):
ret = 1
print(f'{link["filename"]}:{link["lineno"]}: {link["uri"]} -> '
f'{link["status"]} {link["info"]}')
sys.exit(ret)