#!/usr/bin/python2 -tt ## # Copyright (C) 2004 by Duke University # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA # 02111-1307, USA. # # Author: Konstantin Ryabitsev # import os, sys, time REPOSITORY = '/var/lib/epylog/repository/' headerSent = 0 TEMPLATE = '''
%(prevlink)s %(dropdown)s %(nextlink)s

''' def errout(msg, ecode=1): printHeader('text/plain') sys.stdout.write('ERROR: %s\n' % msg) sys.exit(ecode) def printHeader(ctype='text/html'): global headerSent if not headerSent: sys.stdout.write('Content-type: %s\r\n\r\n' % ctype) def getSubDirs(topdir): ret = [] for name in os.listdir(topdir): fullpath = os.path.join(topdir, name) if os.path.isdir(fullpath): ret.append(fullpath) return ret def getAllFilesInDir(topdir, extfilter='.html'): allfiles = [] for name in os.listdir(topdir): if name[-len(extfilter):] == extfilter: allfiles.append(os.path.join(topdir, name)) return allfiles def mkLink(loc, text=None): try: script = os.environ['SCRIPT_NAME'] except KeyError: errout('No SCRIPT_NAME found!') linkloc = loc[len(REPOSITORY):] link = '%s?%s' % (script, linkloc) if text is None: return link ret = '%s' % (link, text) return ret def mkDropDown(mtimes, myfile): ret = '
Jump To:
\n' return ret if not os.path.isdir(REPOSITORY): errout('%s is not a directory' % REPOSITORY) try: qstring = os.environ['QUERY_STRING'] except KeyError: errout('No QUERY_STRING found') subdirs = getSubDirs(REPOSITORY) filemap = {} for subdir in subdirs: allfiles = getAllFilesInDir(subdir, '.html') for report in allfiles: mtime = os.stat(report)[8] filemap[mtime] = report mtimes = filemap.keys() if not mtimes: print "Content-type: text/plain\n\n" print "No reports found!" sys.exit(1) mtimes.sort() prev = None next = None myfile = None if not qstring: myfile = mtimes[-1] try: prev = mtimes[-2] except IndexError: pass else: for i in range(0, len(mtimes)): if i != 0: prev = mtimes[i-1] try: next = mtimes[i+1] except IndexError: next = None try: if filemap[mtimes[i]].index(qstring): myfile = mtimes[i] break except ValueError: pass if myfile is None: errout('Requested report not found.') if prev is not None: prevlink = mkLink(filemap[prev], '<<Previous') else: prevlink = '(No previous report)' if next is not None: nextlink = mkLink(filemap[next], 'Next>>') else: nextlink = '(No next report)' dropdown = mkDropDown(mtimes, myfile) outdata = {'prevlink': prevlink, 'nextlink': nextlink, 'dropdown': dropdown} out = TEMPLATE % outdata repfile = filemap[myfile] fh = open(repfile, 'r') report = fh.read() fh.close() report = report.replace('', '\n%s\n' % out) report = report.replace('', '%s\n\n' % out) printHeader(ctype='text/html') sys.stdout.write(report)