1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
#!/usr/bin/python
# Accept popularity-contest entries on stdin and drop them into a
# subdirectory with a name based on their MD5 ID.
#
# Only the most recent entry with a given MD5 ID is kept.
#
import sys, string, os, time
dirname = 'popcon-entries'
output = None
while 1:
line = sys.stdin.readline()
if not line: break
split = string.split(line)
if not split: continue
if split[0] == 'POPULARITY-CONTEST-0':
if output != None:
output.close()
output = None
mtime = 0
for s in split[1:]:
key, value = string.split(s, ':')
if key == 'ID' and len(value) > 2:
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 = long(value)
elif split[0] == 'END-POPULARITY-CONTEST-0':
if output != None:
print "%s: %s" % (md5, time.ctime(mtime))
output.write(line)
output.close()
output = None
os.utime(fname, (mtime, mtime))
elif output != None:
output.write(line)
|