import json,sys,urllib.request,urllib.parse,concurrent.futures from pathlib import Path from html.parser import HTMLParser r=Path(__file__).resolve().parents[1]; base=sys.argv[1].rstrip('/') articles=json.loads((r/'web/data/articles.json').read_text()) paths=['/','/about/','/tools/']+['/articles/'+urllib.parse.quote(a['slug'])+'/' for a in articles]+[urllib.parse.quote(a['legacyPath']) for a in articles] class Links(HTMLParser): def __init__(self):super().__init__();self.links=set() def handle_starttag(self,t,attrs): a=dict(attrs) for k in ['href','src']: v=a.get(k,'') if v.startswith('/') and not v.startswith('//'):self.links.add(v.split('#')[0]) assets=set(); results=[] for path in paths: with urllib.request.urlopen(base+path,timeout=20) as response: assert response.status==200,path text=response.read().decode(); assert 'ProgCode' in text,path assert 'mc.yandex.ru' not in text and 'googletagmanager' not in text,path p=Links();p.feed(text);assets.update(p.links);results.append(dict(path=path,status=200)) for path in sorted(assets): u=urllib.parse.quote(urllib.parse.unquote(path),safe='/:?=&%') with urllib.request.urlopen(base+u,timeout=20) as response:assert response.status==200,path try:urllib.request.urlopen(base+'/does-not-exist-progcode-test/',timeout=20);raise AssertionError('404 route returned success') except urllib.error.HTTPError as e:assert e.code==404 print(json.dumps(dict(base=base,pages=len(results),internalLinksAndAssets=len(assets),missingRoute=404,results=results),ensure_ascii=False,indent=2))