22import re
33import traceback
44import urllib2 as urllib
5+ from datetime import datetime
56
67from ietf .utils import soup2text as html2text
78from difflib import unified_diff
@@ -91,6 +92,16 @@ def filetext(filename):
9192 file .close ()
9293 return chunk
9394
95+
96+ prev_note_time = datetime .utcnow ()
97+ def note (string ):
98+ global prev_note_time
99+ """Like a print function, but adds a leading timestamp line"""
100+ now = datetime .utcnow ()
101+ print "Time" , now .strftime ("%Y-%m-%d_%H:%M" ), "+%ds" % (now - prev_note_time ).seconds
102+ print string
103+ prev_note_time = datetime .utcnow ()
104+
94105class UrlTestCase (TestCase ):
95106 def setUp (self ):
96107 from django .test .client import Client
@@ -171,26 +182,26 @@ def doRedirectsTest(self, lst):
171182 code = str (response .status_code )
172183 if code == "301" :
173184 if response ['Location' ] == url :
174- print "OK %s %s -> %s" % (code , testurl , url )
185+ note ( "OK %s %s -> %s" % (code , testurl , url ) )
175186 res = ("OK" , code )
176187 else :
177- print "Miss %3s %s ->" % (code , testurl )
188+ note ( "Miss %3s %s ->" % (code , testurl ) )
178189 print " %s" % (response ['Location' ])
179190 print " (wanted %s)" % (url )
180191 print ""
181192 #res = ("Fail", "wrong-reponse")
182193 else :
183- print "Fail %s %s" % (code , testurl )
194+ note ( "Fail %s %s" % (code , testurl ) )
184195 res = ("Fail" , code )
185196 except :
186197 res = ("Fail" , "Exc" )
187- print "Exception for URL '%s'" % testurl
198+ note ( "Exception for URL '%s'" % testurl )
188199 traceback .print_exc ()
189200 if not res in response_count :
190201 response_count [res ] = 0
191202 response_count [res ] += 1
192203 if response_count :
193- print "Response count:"
204+ note ( "Response count:" )
194205 for res in response_count :
195206 ind , code = res
196207 print " %-4s %s: %s " % (ind , code , response_count [res ])
@@ -210,22 +221,22 @@ def doUrlsTest(self, lst):
210221 response = self .client .get (baseurl , args )
211222 code = str (response .status_code )
212223 if code in codes :
213- print "OK %s %s" % (code , url )
224+ note ( "OK %s %s" % (code , url ) )
214225 res = ("OK" , code )
215226 else :
216- print "Fail %s %s" % (code , url )
227+ note ( "Fail %s %s" % (code , url ) )
217228 res = ("Fail" , code )
218229 except :
219230 res = ("Fail" , "Exc" )
220- print "Exception for URL '%s'" % url
231+ note ( "Exception for URL '%s'" % url )
221232 traceback .print_exc ()
222233 if master :
223234 try :
224235 #print "Fetching", master, "...",
225236 mfile = urllib .urlopen (master )
226237 goodhtml = mfile .read ()
227238 except urllib .URLError , e :
228- print "Failed retrieving master text for comparison: %s" % e
239+ note ( "Failed retrieving master text for comparison: %s" % e )
229240 try :
230241 mfile .close ()
231242 if goodhtml and response .content :
@@ -243,7 +254,7 @@ def sorted(l):
243254 testtext = reduce (response .content )
244255 goodtext = reduce (goodhtml )
245256 if testtext == goodtext :
246- print "OK cmp %s" % (url )
257+ note ( "OK cmp %s" % (url ) )
247258 else :
248259 contextlines = 0
249260 difflist = list (unified_diff (goodtext , testtext , master , url , "" , "" , contextlines , lineterm = "" ))
@@ -267,17 +278,17 @@ def sorted(l):
267278 else :
268279 okdiff = ""
269280 if diff .strip () == okdiff .strip ():
270- print "OK cmp %s" % (url )
281+ note ( "OK cmp %s" % (url ) )
271282 else :
272- print "Diff: %s" % (url )
283+ note ( "Diff: %s" % (url ) )
273284 print "\n " .join (difflist [:100 ])
274285 if len (difflist ) > 100 :
275286 print "... (skipping %s lines of diff)" % (len (difflist )- 100 )
276287 else :
277- print "OK cmp %s" % (url )
288+ note ( "OK cmp %s" % (url ) )
278289
279290 except :
280- print "Exception occurred for url %s" % (url )
291+ note ( "Exception occurred for url %s" % (url ) )
281292 traceback .print_exc ()
282293 #raise
283294
@@ -287,7 +298,7 @@ def sorted(l):
287298 else :
288299 pass
289300 if response_count :
290- print "Response count:"
301+ note ( "Response count:" )
291302 for res in response_count :
292303 ind , code = res
293304 print " %-4s %s: %s " % (ind , code , response_count [res ])
@@ -296,15 +307,15 @@ def sorted(l):
296307 self .assertEqual (ind , "OK" , "Found %s cases of result code: %s" % (response_count [res ], code ))
297308
298309 def testUrlsList (self ):
299- print "\n Testing specified URLs:"
310+ note ( "\n Testing specified URLs:" )
300311 self .doUrlsTest (self .testtuples )
301312
302313 def testRedirectsList (self ):
303- print "\n Testing specified Redirects:"
314+ note ( "\n Testing specified Redirects:" )
304315 self .doRedirectsTest (self .testtuples )
305316
306317 def testUrlsFallback (self ):
307- print "\n Fallback: Test access to URLs which don't have an explicit test entry:"
318+ note ( "\n Fallback: Test access to URLs which don't have an explicit test entry:" )
308319 lst = []
309320 for pattern in self .patterns :
310321 if pattern .startswith ("^" ) and pattern .endswith ("$" ):
0 commit comments