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
76basedir = os .path .abspath (os .path .join (os .path .dirname (__file__ ), "../.." ))
@@ -19,9 +18,35 @@ class DontSaveQueries(object):
1918connection .queries = DontSaveQueries ()
2019
2120MAX_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
2651visited = set ()
2752urls = {} # url -> referrer
@@ -33,8 +58,11 @@ def strip_url(url):
3358 return url
3459
3560def 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
4674client = django .test .Client ()
4775
48- for url in initial :
76+ for url in initial_urls :
4977 urls [url ] = "[initial]"
5078
79+ errors = 0
80+
5181while 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 )
0 commit comments