status.py :  » Business-Application » ECLiPt-Mirroring-Tool » emirror-2.2.0-0.4 » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Business Application » ECLiPt Mirroring Tool 
ECLiPt Mirroring Tool » emirror 2.2.0 0.4 » status.py
#!/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)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.