-
Notifications
You must be signed in to change notification settings - Fork 6
/
get_references.py
executable file
·40 lines (32 loc) · 1.17 KB
/
get_references.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
#!/usr/bin/env python
import re
import sys
import errno
import os
if sys.version_info[0] == 2:
from urllib import unquote
else:
from urllib.parse import unquote
pattern = re.compile(r"\[[^\]]+?]\(([^)]+?)\)")
doc_relevant = set()
for arg in sys.argv[1].split('\n'):
if not os.path.exists(arg):
if sys.version_info[0] == 2:
raise IOError(errno.ENOENT, os.strerror(errno.ENOENT), arg)
else:
raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), arg)
else:
(dirname, basename) = os.path.split(arg)
doc_relevant.add(arg)
for i, line in enumerate(open(arg)):
for match in re.finditer(pattern, line):
for group in match.groups():
ref_file = '{}/{}'.format(dirname, group.rsplit('#page=', 1)[0])
if os.path.exists(ref_file):
doc_relevant.add(ref_file)
elif os.path.exists(unquote(ref_file)):
doc_relevant.add(unquote(ref_file))
else:
sys.stderr.write("ignoring reference to nonlocal file: {}{}".format(ref_file, os.linesep))
for item in doc_relevant:
print(item)