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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
|
#!/usr/bin/env python3
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library. If not, see
# <http://www.gnu.org/licenses/>.
#
# Check that external references between documentation HTML files are not broken.
import argparse
import os
import re
import sys
import xml.etree.ElementTree as ET
ns = {'html': 'http://www.w3.org/1999/xhtml'}
externallinks = []
externalimages = []
def get_file_list(prefix):
filelist = []
imagelist = []
imageformats = ['.jpg', '.svg', '.png']
for root, dir, files in os.walk(prefix):
for file in files:
ext = os.path.splitext(file)[1]
if ext == '.html':
# the 404 page doesn't play well
if '404.html' in file:
continue
filelist.append(os.path.join(root, file))
elif ext in imageformats:
imagelist.append(os.path.join(root, file))
filelist.sort()
imagelist.sort()
return filelist, imagelist
# loads an XHTML and extracts all anchors, local and remote links for the one file
def process_file(filename, project_uri):
tree = ET.parse(filename)
root = tree.getroot()
docname = root.get('data-sourcedoc')
dirname = os.path.dirname(filename)
if not docname:
docname = filename
anchors = [filename]
targets = []
images = []
projectlinks = []
for elem in root.findall('.//html:a', ns):
target = elem.get('href')
an = elem.get('id')
if an:
anchors.append(filename + '#' + an)
if target:
if re.search('://', target):
externallinks.append(target)
if project_uri is not None and target.startswith(project_uri):
projectlinks.append((target, docname))
elif target[0] != '#' and 'mailto:' not in target:
targetfull = os.path.normpath(os.path.join(dirname, target))
targets.append((filename, docname, targetfull, target))
# older docutils generate "<div class='section'"
for elem in root.findall('.//html:div/[@class=\'section\']', ns):
an = elem.get('id')
if an:
anchors.append(filename + '#' + an)
# modern docutils generate a <section element
for elem in root.findall('.//html:section', ns):
an = elem.get('id')
if an:
anchors.append(filename + '#' + an)
# find local images
for elem in root.findall('.//html:img', ns):
src = elem.get('src')
if src:
if re.search('://', src):
externalimages.append(src)
else:
imagefull = os.path.normpath(os.path.join(dirname, src))
images.append((imagefull, docname))
return (anchors, targets, images, projectlinks)
def process_all(filelist, project_uri):
anchors = []
targets = []
images = []
projectlinks = []
for file in filelist:
anchor, target, image, projectlink = process_file(file, project_uri)
targets = targets + target
anchors = anchors + anchor
images = images + image
projectlinks = projectlinks + projectlink
return (targets, anchors, images, projectlinks)
def check_targets(targets, anchors):
errors = []
for _, docname, target, targetorig in targets:
if target not in anchors:
errors.append((docname, targetorig))
if errors:
errors.sort()
for file, target in errors:
print(f'ERROR: \'{file}\': broken link to: \'{target}\'')
return True
return False
def check_usage_crawl(page, targets, visited):
visited.append(page)
tocrawl = []
for filename, docname, target, _ in targets:
if page != filename:
continue
targetpage = target.split("#", 1)[0]
if targetpage not in visited and targetpage not in tocrawl:
tocrawl.append(targetpage)
for crawl in tocrawl:
check_usage_crawl(crawl, targets, visited)
# crawls the document references starting from entrypoint and tries to find
# unreachable pages
def check_usage(targets, files, entrypoint):
visited = []
fail = False
check_usage_crawl(entrypoint, targets, visited)
for file in files:
if file not in visited:
brokendoc = file
for filename, docname, _, _ in targets:
if filename != file:
continue
if docname:
brokendoc = docname
break
print(f'ERROR: \'{brokendoc}\': is not referenced from anywhere')
fail = True
return fail
# checks that images present in the directory are being used and also that
# pages link to existing images. For favicons, which are not referenced from
# the '.html' files there's a builtin set of exceptions.
def check_images(usedimages, imagefiles, ignoreimages):
favicons = [
'android-chrome-192x192.png',
'android-chrome-256x256.png',
'apple-touch-icon.png',
'favicon-16x16.png',
'favicon-32x32.png',
'mstile-150x150.png',
]
fail = False
if ignoreimages:
favicons = favicons + ignoreimages
for usedimage, docname in usedimages:
if usedimage not in imagefiles:
print(f'ERROR: \'{docname}\' references image \'{usedimage}\' not among images')
fail = True
for imagefile in imagefiles:
used = False
if imagefile in (usedimage[0] for usedimage in usedimages):
used = True
else:
for favicon in favicons:
if favicon in imagefile:
used = True
break
if not used:
print(f'ERROR: Image \'{imagefile}\' is not used by any page')
fail = True
return fail
# checks that all links are accessed via https
def check_https(links):
fail = False
for link in links:
if link.startswith('http://'):
print(f'ERROR: URI \'{link}\' uses insecure "http" protocol')
fail = True
return fail
# checks prohibited external links to local files
def check_projectlinks(projectlinks, exceptions):
fail = False
for (link, filename) in projectlinks:
allowed = False
if exceptions is not None:
for exc in exceptions:
if exc in filename:
allowed = True
break
if not allowed:
print(f'ERROR: prohibited external URI \'{link}\' to local project in \'{filename}\'')
fail = True
return fail
parser = argparse.ArgumentParser(description='HTML reference checker')
parser.add_argument('--webroot', required=True,
help='path to the web root')
parser.add_argument('--entrypoint', default="index.html",
help='file name of web entry point relative to --webroot')
parser.add_argument('--external', action="store_true",
help='print external references instead')
parser.add_argument('--ignore-images', action='append',
help='paths to images that should be considered as used')
parser.add_argument('--require-https', action="store_true",
help='require secure https for external links')
parser.add_argument('--project-uri',
help='external prefix of the local project (e.g. https://libvirt.org; external links with that prefix are prohibited')
parser.add_argument('--project-uri-exceptions', action='append',
help='list of path prefixes excluded from the "--project-uri" checks')
args = parser.parse_args()
files, imagefiles = get_file_list(os.path.abspath(args.webroot))
entrypoint = os.path.join(os.path.abspath(args.webroot), args.entrypoint)
targets, anchors, usedimages, projectlinks = process_all(files, args.project_uri)
fail = False
if args.external:
prev = None
externallinks.sort()
for ext in externallinks:
if ext != prev:
print(f'link: {ext}')
prev = ext
externalimages.sort()
for ext in externalimages:
if ext != prev:
print(f'image: {ext}')
prev = ext
else:
if check_targets(targets, anchors):
fail = True
if check_usage(targets, files, entrypoint):
fail = True
if check_images(usedimages, imagefiles, args.ignore_images):
fail = True
if check_projectlinks(projectlinks, args.project_uri_exceptions):
fail = True
if args.require_https:
if check_https(externallinks):
fail = True
if check_https(externalimages):
fail = True
if fail:
sys.exit(1)
sys.exit(0)
|