Skip to content

Commit 591e90a

Browse files
committed
Put a proper command-line interface on the test crawler, spit out number of errors (if any), also add wrapper script for running the test-crawler and idindex generation scripts, in the future other (safe, non-mutating) scripts operating on real data can be added too
- Legacy-Id: 7107
1 parent be8eb96 commit 591e90a

7 files changed

Lines changed: 104 additions & 8 deletions

File tree

ietf/bin/run-real-data-tests

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env python
2+
#
3+
# Run some non-modifying tests on top of the real database, to
4+
# exercise the code with real data.
5+
#
6+
7+
import os, subprocess, datetime
8+
9+
base_dir = os.path.relpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."))
10+
11+
path = os.path.abspath(os.path.join(base_dir, ".."))
12+
if os.environ.get("PYTHONPATH"):
13+
path += ":" + os.environ.get("PYTHONPATH")
14+
os.environ["PYTHONPATH"] = path
15+
16+
17+
18+
def run_script(script, *args):
19+
script_base = os.path.splitext(os.path.basename(script))[0]
20+
script_path = os.path.join(base_dir, script)
21+
output_path = os.path.join(base_dir, script_base)
22+
arg_str = " " + " ".join(args) if args else ""
23+
cmd_line = "%s%s > %s.output" % (script_path, arg_str, output_path)
24+
print "Running %s" % cmd_line
25+
before = datetime.datetime.now()
26+
returncode = subprocess.call(cmd_line, shell=True)
27+
print " (took %.3f seconds)" % (datetime.datetime.now() - before).total_seconds()
28+
return returncode
29+
30+
# idindex
31+
run_script("idindex/generate_id_abstracts_txt.py")
32+
run_script("idindex/generate_id_index_txt.py")
33+
run_script("idindex/generate_all_id_txt.py")
34+
run_script("idindex/generate_all_id2_txt.py")
35+
36+
# test crawler
37+
crawl_input = os.path.join(base_dir, "utils/crawlurls.txt")
38+
run_script("bin/test-crawl", "--urls %s" % crawl_input)

ietf/bin/test-crawl

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/usr/bin/env python
22

3-
import os, sys, re, datetime, optparse, traceback
4-
import syslog
3+
import os, sys, re, datetime, argparse, traceback
54

65
# boilerplate
76
basedir = os.path.abspath(os.path.join(os.path.dirname(__file__), "../.."))
@@ -19,9 +18,35 @@ class DontSaveQueries(object):
1918
connection.queries = DontSaveQueries()
2019

2120
MAX_URL_LENGTH = 500
22-
SLOW_THRESHOLD = 1.0
2321

24-
initial = ["/doc/all/", "/doc/in-last-call/", "/iesg/decisions/"]
22+
# args
23+
parser = argparse.ArgumentParser(
24+
description="""Perform a test crawl of the project. For each found URL, the HTTP
25+
response status is printed. If it's not OK/redirect, FAIL is
26+
printed - in case of errors, a stacktrace is also included.""")
27+
parser.add_argument('urls', metavar='URL', nargs='*',
28+
help='One or more URLs to start the crawl from')
29+
parser.add_argument('--urls', '-u', dest='url_file',
30+
help='file with URLs to start the crawl from')
31+
parser.add_argument('--slow', dest='slow_threshold', type=float, default=1.0,
32+
help='responses taking longer than this (in seconds) results in SLOW being printed')
33+
34+
args = parser.parse_args()
35+
36+
slow_threshold = args.slow_threshold
37+
38+
initial_urls = []
39+
initial_urls.extend(args.urls)
40+
41+
if args.url_file:
42+
with open(args.url_file) as f:
43+
for line in f:
44+
line = line.partition("#")[0].strip()
45+
if line:
46+
initial_urls.append(line)
47+
48+
if not initial_urls:
49+
initial_urls.append("/")
2550

2651
visited = set()
2752
urls = {} # url -> referrer
@@ -33,8 +58,11 @@ def strip_url(url):
3358
return url
3459

3560
def extract_html_urls(content):
36-
for m in re.finditer(r'<(?:a|link) [^>]*href="([^"]+)"', content):
37-
url = strip_url(m.group(1))
61+
for m in re.finditer(r'(<(?:a|link) [^>]*href=[\'"]([^"]+)[\'"][^>]*>)', content):
62+
if re.search(r'rel=["\']?nofollow["\']', m.group(1)):
63+
continue
64+
65+
url = strip_url(m.group(2))
3866
if len(url) > MAX_URL_LENGTH:
3967
continue # avoid infinite GET parameter appendages
4068

@@ -45,9 +73,11 @@ def extract_html_urls(content):
4573

4674
client = django.test.Client()
4775

48-
for url in initial:
76+
for url in initial_urls:
4977
urls[url] = "[initial]"
5078

79+
errors = 0
80+
5181
while urls:
5282
url, referrer = urls.popitem()
5383

@@ -65,6 +95,7 @@ while urls:
6595
print "============="
6696
print traceback.format_exc()
6797
print "============="
98+
errors += 1
6899
else:
69100
tags = []
70101

@@ -90,9 +121,13 @@ while urls:
90121
print "============="
91122
else:
92123
tags.append(u"FAIL (from %s)" % referrer)
124+
errors += 1
93125

94-
if elapsed.total_seconds() > SLOW_THRESHOLD:
126+
if elapsed.total_seconds() > slow_threshold:
95127
tags.append("SLOW")
96128

97129
print r.status_code, "%.3fs" % elapsed.total_seconds(), url, " ".join(tags)
98130

131+
if errors > 0:
132+
sys.stderr.write("Found %s errors, grep output for FAIL for details\n" % errors)
133+
sys.exit(1)

ietf/idindex/generate_all_id2_txt.py

100644100755
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/env python
12
# Portions Copyright (C) 2009-2010 Nokia Corporation and/or its subsidiary(-ies).
23
# All rights reserved. Contact: Pasi Eronen <pasi.eronen@nokia.com>
34
#

ietf/idindex/generate_all_id_txt.py

100644100755
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/env python
12
# Portions Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
23
# All rights reserved. Contact: Pasi Eronen <pasi.eronen@nokia.com>
34
#

ietf/idindex/generate_id_abstracts_txt.py

100644100755
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/env python
12
# Portions Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
23
# All rights reserved. Contact: Pasi Eronen <pasi.eronen@nokia.com>
34
#

ietf/idindex/generate_id_index_txt.py

100644100755
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/env python
12
# Portions Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
23
# All rights reserved. Contact: Pasi Eronen <pasi.eronen@nokia.com>
34
#

ietf/utils/crawlurls.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# List of starting points for the test crawler (see
2+
# bin/run-real-data-tests). Add URLs that have no link from inside the
3+
# project.
4+
5+
/
6+
/doc/all/
7+
/doc/iesg/last-call/
8+
/doc/in-last-call/
9+
/iesg/decisions/
10+
/iesg/agenda/documents.txt
11+
/iesg/agenda/agenda.json
12+
/iesg/agenda/scribe_template.html
13+
/wg/1wg-summary.txt
14+
/wg/1wg-summary-by-acronym.txt
15+
/wg/1wg-charters.txt
16+
/wg/1wg-charters-by-acronym.txt
17+
/sitemap.xml
18+
/sitemap-ipr.xml
19+
/sitemap-liaison.xml

0 commit comments

Comments
 (0)