aboutsummaryrefslogtreecommitdiffstats
path: root/tools/check_links.py
diff options
context:
space:
mode:
authorAndrey Konovalov <andreyknvl@google.com>2017-10-26 18:48:54 +0200
committerAndrey Konovalov <andreyknvl@gmail.com>2017-10-27 10:04:34 +0200
commit26d265c811929d03c4d27e5fe53f7de5bde32215 (patch)
treeb74bc3965e7f36489b2cea0b66fd74efb555c483 /tools/check_links.py
parent0a4d6e56b9194cd5c1022b6737367ff5bc368350 (diff)
docs, tools: add local link checker
This commit adds tools/check_links.py script, that checks that all local links from documentation files are valid; fixes some of the invalid links that we had; and makes travis buildbot check them as well.
Diffstat (limited to 'tools/check_links.py')
-rwxr-xr-xtools/check_links.py66
1 files changed, 66 insertions, 0 deletions
diff --git a/tools/check_links.py b/tools/check_links.py
new file mode 100755
index 000000000..14a8517c1
--- /dev/null
+++ b/tools/check_links.py
@@ -0,0 +1,66 @@
+#!/usr/bin/python
+
+import os
+import re
+import sys
+
+link_re = re.compile('\[' + '[^\[\]]+' + '\]' + '\(' + '([^\(\)]+)' + '\)')
+
+if len(sys.argv) < 3:
+ print 'Usage: <root_dir> <doc_files>...'
+ sys.exit(1)
+
+root = sys.argv[1]
+docs = sys.argv[2:]
+
+links = []
+
+for doc in docs:
+ with open(doc) as f:
+ data = f.read()
+ r = link_re.findall(data)
+ for link in r:
+ links += [(doc, link)]
+
+def filter_link((doc, link)):
+ if link.startswith('http'):
+ return False
+ if link.startswith('#'):
+ return False
+ return True
+
+links = filter(filter_link, links)
+
+def fix_link((doc, link)):
+ link = link.split('#')[0]
+ link = link.split('?')[0]
+ return (doc, link)
+
+links = map(fix_link, links)
+
+errors = []
+
+def check_link((doc, link)):
+ path = os.path.dirname(doc)
+ full_link = None
+ if link[0] == '/':
+ link = link[1:]
+ full_link = os.path.join(root, link)
+ else:
+ full_link = os.path.join(root, path, link)
+ if not os.path.exists(full_link):
+ return False
+ return True
+
+for link in links:
+ if not check_link(link):
+ errors += [link]
+
+if len(errors) == 0:
+ print '%d links checked: OK' % (len(links),)
+ sys.exit(0)
+
+for (doc, link) in errors:
+ print 'File %s linked from %s not found' % (link, doc)
+
+sys.exit(2)