[Popcon-commits] cvs commit to popularity-contest by pere

popcon-commits@lists.alioth.debian.org popcon-commits@lists.alioth.debian.org
Sun, 01 Feb 2004 18:15:00 +0100


Update of /cvsroot/popcon/popularity-contest
In directory quantz:/tmp/cvs-serv14255

Modified Files:
	prepop.py 
Log Message:
Add more error checking.


Index: prepop.py
===================================================================
RCS file: /cvsroot/popcon/popularity-contest/prepop.py,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- prepop.py	1 Feb 2004 16:46:40 -0000	1.2
+++ prepop.py	1 Feb 2004 17:14:58 -0000	1.3
@@ -6,6 +6,7 @@
 #
 
 import sys, string, os, time, re
+from sys import stderr
 
 dirname = 'popcon-entries'
 output = None
@@ -13,11 +14,23 @@
 now = time.time()
 
 msgstartre = re.compile("^From")
-md5sumre   = re.compile("^([a-f0-9]{32})$")
+md5sumre   = re.compile("^[a-f0-9]{32}$")
+timere     = re.compile("^[0-9]+$")
+
+def closereport(output, md5, mtime):
+    if output != None:
+	print "%s: %s" % (md5, time.ctime(mtime))
+	output.write(line)
+	output.close()
+	output = None
+	os.utime(fname, (mtime, mtime))
+    return output
 
 while 1:
     line = sys.stdin.readline()
-    if not line: break
+    if not line:
+	output = closereport(output, md5, mtime)
+	break
 
     split = string.split(line)
     if not split: continue
@@ -34,30 +47,29 @@
 		key, value = list
 	    except:
 		continue
-	    if key == 'ID' and md5sumre.match(value):
-		md5 = value
-		subdir = dirname + '/' + value[0:2]
-		try:
-		    os.mkdir(subdir)
-		except os.error:  # already exists
-		    pass
-		fname = subdir + '/' + md5
-		output = open(fname, "w")
-		output.write(line)
+	    if key == 'ID':
+		if not md5sumre.match(value):
+		    print >>stderr,"Bad hostid: %s" % value
+		else:
+		    md5 = value
+		    subdir = dirname + '/' + value[0:2]
+		    try:
+			os.mkdir(subdir)
+		    except os.error:  # already exists
+			pass
+		    fname = subdir + '/' + md5
+		    output = open(fname, "w")
+		    output.write(line)
 	    elif key == 'TIME':
-		mtime = float(value)
-		if (mtime > now):
+		if not timere.match(value):
+		    print >>stderr,"Bad mtime: %s" % value
+		else:
+		    mtime = float(value)
+		    if (mtime > now):
 			mtime = now
-		
-	
 
     elif split[0] == 'END-POPULARITY-CONTEST-0' or msgstartre.match(split[0]):
-	if output != None:
-	    print "%s: %s" % (md5, time.ctime(mtime))
-	    output.write(line)
-	    output.close()
-	    output = None
-	    os.utime(fname, (mtime, mtime))
+	output = closereport(output, md5, mtime)
 
     elif output != None:
 	output.write(line)