|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Check a prepared root/latest deployment without making network requests.""" |
| 3 | +import argparse |
| 4 | +import csv |
| 5 | +from collections import defaultdict |
| 6 | +from html.parser import HTMLParser |
| 7 | +from pathlib import Path |
| 8 | +from urllib.parse import unquote, urljoin, urlsplit |
| 9 | +import xml.etree.ElementTree as ET |
| 10 | + |
| 11 | + |
| 12 | +class Page(HTMLParser): |
| 13 | + def __init__(self, path): |
| 14 | + super().__init__() |
| 15 | + self.canonical = [] |
| 16 | + self.robots = [] |
| 17 | + self.refresh = [] |
| 18 | + self.links = [] |
| 19 | + self.h1 = 0 |
| 20 | + self.title = '' |
| 21 | + self.in_title = False |
| 22 | + self.og_url = None |
| 23 | + self.description = '' |
| 24 | + self.feed(path.read_text()) |
| 25 | + |
| 26 | + def handle_starttag(self, tag, attrs): |
| 27 | + a = dict(attrs) |
| 28 | + if tag == 'h1': |
| 29 | + self.h1 += 1 |
| 30 | + if tag == 'title': |
| 31 | + self.in_title = True |
| 32 | + if tag == 'link' and a.get('rel') == 'canonical': |
| 33 | + self.canonical.append(a['href']) |
| 34 | + if tag == 'meta': |
| 35 | + if a.get('name') == 'robots': |
| 36 | + self.robots.append(a['content']) |
| 37 | + if a.get('name') == 'description': |
| 38 | + self.description = a['content'] |
| 39 | + if a.get('http-equiv', '').lower() == 'refresh': |
| 40 | + self.refresh.append(a['content']) |
| 41 | + if a.get('property') == 'og:url': |
| 42 | + self.og_url = a['content'] |
| 43 | + if tag == 'a' and 'href' in a: |
| 44 | + self.links.append(a['href']) |
| 45 | + if tag in ('img', 'script') and 'src' in a: |
| 46 | + self.links.append(a['src']) |
| 47 | + if tag == 'link' and a.get('rel') == 'stylesheet': |
| 48 | + self.links.append(a['href']) |
| 49 | + |
| 50 | + def handle_endtag(self, tag): |
| 51 | + if tag == 'title': |
| 52 | + self.in_title = False |
| 53 | + |
| 54 | + def handle_data(self, text): |
| 55 | + if self.in_title: |
| 56 | + self.title += text |
| 57 | + |
| 58 | + |
| 59 | +def check(latest, root, origin, inventory): |
| 60 | + origin = origin.rstrip('/') |
| 61 | + |
| 62 | + def local(url): |
| 63 | + path = unquote(urlsplit(url).path) |
| 64 | + file = latest / path.removeprefix('/latest/') if path.startswith('/latest/') else root / path.lstrip('/') |
| 65 | + return file / 'index.html' if path.endswith('/') else file |
| 66 | + |
| 67 | + urls = [e.text for e in ET.parse(root / 'sitemap.xml').iter('{http://www.sitemaps.org/schemas/sitemap/0.9}loc')] |
| 68 | + assert origin + '/' in urls and origin + '/latest/' not in urls |
| 69 | + assert len(urls) == len(set(urls)), 'Duplicate sitemap URLs' |
| 70 | + titles = defaultdict(list) |
| 71 | + for url in urls: |
| 72 | + page = Page(local(url)) |
| 73 | + assert not page.refresh, url |
| 74 | + assert page.canonical == [url], (url, page.canonical) |
| 75 | + assert page.robots == ['index, follow'], (url, page.robots) |
| 76 | + assert page.h1 == 1 and page.description.strip(), url |
| 77 | + assert page.og_url == url, (url, page.og_url) |
| 78 | + titles[page.title].append(url) |
| 79 | + if url != origin + '/': |
| 80 | + assert page.title.endswith(' | OpenMower'), (url, page.title) |
| 81 | + for link in page.links: |
| 82 | + target = urljoin(url, link) |
| 83 | + parsed = urlsplit(target) |
| 84 | + if parsed.netloc != urlsplit(origin).netloc or parsed.path.startswith('/archive/'): |
| 85 | + continue |
| 86 | + assert local(target).is_file(), (url, link) |
| 87 | + assert all(len(v) == 1 for v in titles.values()), dict(titles) |
| 88 | + assert f'Sitemap: {origin}/sitemap.xml' in (root / 'robots.txt').read_text() |
| 89 | + assert (root / 'sitemap.xml').read_bytes() == (latest / 'sitemap.xml').read_bytes() |
| 90 | + for route in ('search/', 'tags/', 'tags/mapping/', 'categories/'): |
| 91 | + url = origin + '/latest/' + route |
| 92 | + assert url not in urls |
| 93 | + assert Page(local(url)).robots == ['noindex, follow'], url |
| 94 | + rows = list(csv.DictReader(inventory.open())) |
| 95 | + rows.append({'old_path': '/latest/', 'replacement_path': '/'}) |
| 96 | + for row in rows: |
| 97 | + target = origin + row['replacement_path'] |
| 98 | + page = Page(local(origin + row['old_path'])) |
| 99 | + assert page.canonical == [target], row |
| 100 | + assert page.refresh == [f'0; url={target}'], row |
| 101 | + assert not Page(local(target)).refresh, row |
| 102 | + home = (root / 'index.html').read_text() |
| 103 | + for asset in ('asciinema-player', 'carousel.js', 'carousel.css', 'medium-zoom'): |
| 104 | + assert asset not in home, asset |
| 105 | + print(f'Passed: {len(urls)} sitemap pages, {len(rows)} redirects, metadata, headings, internal links/assets, and indexing rules') |
| 106 | + |
| 107 | + |
| 108 | +if __name__ == '__main__': |
| 109 | + parser = argparse.ArgumentParser(description=__doc__) |
| 110 | + parser.add_argument('--latest', type=Path, default=Path('public')) |
| 111 | + parser.add_argument('--root', type=Path, default=Path('public-root')) |
| 112 | + parser.add_argument('--origin', default='https://openmower.de') |
| 113 | + parser.add_argument('--inventory', type=Path, default=Path('data/redirects.csv')) |
| 114 | + args = parser.parse_args() |
| 115 | + check(args.latest, args.root, args.origin, args.inventory) |
0 commit comments