|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import os, sys, re, datetime, optparse, traceback |
| 4 | +import syslog |
| 5 | + |
| 6 | +# boilerplate |
| 7 | +basedir = os.path.abspath(os.path.join(os.path.dirname(__file__), "../..")) |
| 8 | +sys.path = [ basedir ] + sys.path |
| 9 | + |
| 10 | +from ietf import settings |
| 11 | +from django.core import management |
| 12 | +management.setup_environ(settings) |
| 13 | + |
| 14 | +import django.test |
| 15 | +from django.conf import settings |
| 16 | + |
| 17 | +# prevent memory from leaking when settings.DEBUG=True |
| 18 | +from django.db import connection |
| 19 | +class DontSaveQueries(object): |
| 20 | + def append(self, x): |
| 21 | + pass |
| 22 | +connection.queries = DontSaveQueries() |
| 23 | + |
| 24 | +MAX_URL_LENGTH = 500 |
| 25 | +SLOW_THRESHOLD = 1.0 |
| 26 | + |
| 27 | +def strip_url(url): |
| 28 | + if url.startswith("http://testserver"): |
| 29 | + url = url[len("http://testserver"):] |
| 30 | + return url |
| 31 | + |
| 32 | +def extract_html_urls(content): |
| 33 | + for m in re.finditer(r'<a.*href="([^"]+)">', content): |
| 34 | + url = strip_url(m.group(1)) |
| 35 | + if len(url) > MAX_URL_LENGTH: |
| 36 | + continue # avoid infinite GET parameter appendages |
| 37 | + |
| 38 | + if not url.startswith("/"): |
| 39 | + continue |
| 40 | + |
| 41 | + yield url |
| 42 | + |
| 43 | + |
| 44 | +visited = set() |
| 45 | +blacklist = set() |
| 46 | +urls = set(["/doc/all/"]) |
| 47 | + |
| 48 | +client = django.test.Client() |
| 49 | + |
| 50 | +while urls: |
| 51 | + url = urls.pop() |
| 52 | + |
| 53 | + visited.add(url) |
| 54 | + |
| 55 | + try: |
| 56 | + timestamp = datetime.datetime.now() |
| 57 | + r = client.get(url) |
| 58 | + elapsed = datetime.datetime.now() - timestamp |
| 59 | + except KeyboardInterrupt: |
| 60 | + print "was fetching", url |
| 61 | + sys.exit(1) |
| 62 | + except: |
| 63 | + print "FAIL", url |
| 64 | + print "=============" |
| 65 | + traceback.print_exc() |
| 66 | + print "=============" |
| 67 | + else: |
| 68 | + tags = [] |
| 69 | + |
| 70 | + if r.status_code in (301, 302): |
| 71 | + u = strip_url(r["Location"]) |
| 72 | + if u not in visited and u not in urls: |
| 73 | + urls.add(u) |
| 74 | + |
| 75 | + elif r.status_code == 200: |
| 76 | + ctype = r["Content-Type"] |
| 77 | + if ";" in ctype: |
| 78 | + ctype = ctype[:ctype.index(";")] |
| 79 | + |
| 80 | + if ctype == "text/html": |
| 81 | + for u in extract_html_urls(r.content): |
| 82 | + if u not in visited and u not in urls: |
| 83 | + urls.add(u) |
| 84 | + else: |
| 85 | + tags.append("FAIL") |
| 86 | + |
| 87 | + if elapsed.total_seconds() > SLOW_THRESHOLD: |
| 88 | + tags.append("SLOW") |
| 89 | + |
| 90 | + print r.status_code, "%.3fs" % elapsed.total_seconds(), url, " ".join(tags) |
| 91 | + |
0 commit comments