#!/usr/local/bin/python
# CGI program to generate the current status of the mirrors
# $Id: status.py,v 1.1 2003/02/28 19:21:56 preisl Exp $
import os, time, sys
import mirrorstats
import log4py
template = []
categoryloop = []
mirrorloop = []
stats = None
log4py = log4py.Logger("FALSE").get_instance()
def parse_template(template_file):
try:
f = open(template_file)
except:
log4py.error("Template file not found: %s" % template_file)
sys.exit(1)
target = template
while 1:
line = f.readline()
if not line: break
if (line.strip().lower() == "$categories_start$"):
target.append("$categoryloop$")
target = categoryloop
elif (line.strip().lower() == "$categories_end$"):
target = template
elif (line.strip().lower() == "$mirrors_start$"):
target.append("$mirrorloop$")
target = mirrorloop
elif (line.strip().lower() == "$mirrors_end$"):
target = categoryloop
else:
target.append(line)
f.close()
def print_mirrors(mirrors):
for mirror in mirrors:
lastcheck = stats.getlastcheck(mirror)
# Always a lastcheck otherwise no log files
lcinfo = stats.getinfo(mirror, lastcheck)
lastupdate = stats.getlastupdate(mirror)
if lastupdate == None:
# No updates yet - just set to last check
luinfo = lcinfo
else:
luinfo = stats.getinfo(mirror, lastupdate)
active = stats.isactive(mirror)
tmp = lcinfo.path.split("/")
lc_url = "./" + "/".join((tmp[-2], tmp[-1]))
tmp = luinfo.path.split("/")
lu_url = "./" + "/".join((tmp[-2], tmp[-1]))
for line in mirrorloop:
if line.find("$title$") != -1:
print line.replace("$title$", mirror)
elif line.find("$status$") != -1:
if active:
print line.replace("$status$", "<img class=\"status\" src=\"active.gif\" alt=\"active\"> [" + mirrorstats.fmttime(time.time()) + "]")
else:
gif = mirrorstats.get_status_gif(lcinfo)
print line.replace("$status$", "<A HREF=\"%s\"><img class=\"status\" src=\"%s.gif\" alt=\"%s\"> [" % (lc_url, gif, gif) + mirrorstats.fmttime(lastcheck) + "]" + "</A>")
elif line.find("$start$") != -1:
print line.replace("$start$", mirrorstats.fmttime(luinfo.starttime))
elif line.find("$end$") != -1:
print line.replace("$end$", "<A HREF=\"%s\">" % lu_url + mirrorstats.fmttime(luinfo.endtime) + "</A>")
elif line.find("$duration$") != -1:
print line.replace("$duration$", mirrorstats.fmtdur(luinfo.endtime - luinfo.starttime))
elif line.find("$size$") != -1:
print line.replace("$size$", mirrorstats.fmtnum(luinfo.nrbytesretr) + " Bytes")
elif line.find("$rate$") != -1:
if (luinfo.endtime - luinfo.starttime > 0):
rate = luinfo.nrbytesretr / 1024 / (luinfo.endtime - luinfo.starttime)
else:
rate = 0.0
print line.replace("$rate$", mirrorstats.fmtnum(rate) + " KBytes/s")
else:
print line
def print_categories(categories):
for cat in categories:
for line in categoryloop:
if line.find("$mirrorloop$") != -1:
mirrors = stats.getmirrorsforcategory(cat)
if not mirrors: continue
mirrors.sort()
print_mirrors(mirrors)
elif line.find("$category$") != -1:
print line.replace("$category$", cat)
else:
print line
def print_output():
for line in template:
if line.find("$categoryloop$") != -1:
categories = stats.getcategories()
if not categories: continue
categories.sort()
print_categories(categories)
elif line.find("$date$") != -1:
print line.replace("$date$", mirrorstats.fmttime(time.time()))
else:
print line
# Main program
if __name__ == "__main__":
print "Content-type: text/html\n"
if os.environ.has_key('EMIRROR_LOGDIR'):
logdir = os.environ['EMIRROR_LOGDIR']
else:
logdir = os.getcwd()
if os.environ.has_key('EMIRROR_LOCKDIR'):
lockdir = os.environ['EMIRROR_LOCKDIR']
else:
lockdir = "/var/tmp/emirror-locks/"
if os.environ.has_key('EMIRROR_TEMPLATEDIR'):
templatedir = os.environ['EMIRROR_TEMPLATEDIR']
else:
templatedir = os.getcwd()
stats = mirrorstats.MirrorStats()
stats.update(logdir, lockdir)
parse_template(os.path.normpath(templatedir + "/status.template"))
print_output()
sys.exit(0)
|