diff options
| author | Daniele Nicolodi <daniele@grinta.net> | 2025-01-11 17:13:14 +0100 |
|---|---|---|
| committer | Jussi Pakkanen <jpakkane@gmail.com> | 2025-01-19 20:36:40 +0200 |
| commit | 74aab8a42c479cdeeda9371dbd591a19d070c48e (patch) | |
| tree | 27e6ab29308e99814550492c7fc0141820b1a765 /docs/validatelinks.py | |
| parent | 74bef61a26b6a910ecc826733126fbb1f123a87b (diff) | |
| download | meson-74aab8a42c479cdeeda9371dbd591a19d070c48e.tar.gz | |
docs: Add a test to validate URLs in markdown/Users.md
Avoid piling up dead URLs.
Diffstat (limited to 'docs/validatelinks.py')
| -rw-r--r-- | docs/validatelinks.py | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/docs/validatelinks.py b/docs/validatelinks.py new file mode 100644 index 000000000..69544ab0e --- /dev/null +++ b/docs/validatelinks.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 + +# SPDX-License-Identifier: Apache-2.0 +# Copyright 2025 The Meson development team + +import sys +import re +import aiohttp +import asyncio + +LINK = re.compile(r'\[(?P<name>[A-Za-z0-9 ]+)\]\((?P<url>.*?)\)') + + +async def fetch(session, name, url, timeout): + try: + async with session.get(url, timeout=timeout) as r: + if not r.ok: + return (name, url, r.status) + except Exception as e: + return (name, url, str(e)) + + +async def main(filename): + with open(filename) as f: + text = f.read() + timeout = aiohttp.ClientTimeout(total=60) + async with aiohttp.ClientSession() as session: + tasks = [] + for link in LINK.finditer(text): + name, url = link.groups() + task = asyncio.ensure_future(fetch(session, name, url, timeout)) + tasks.append(task) + responses = asyncio.gather(*tasks) + errors = [r for r in await responses if r is not None] + for name, url, result in errors: + print(f'"{name}" {url} {result}') + if errors: + sys.exit(1) + + +if __name__ == '__main__': + asyncio.run(main(sys.argv[1])) |
