head 1.2; access; symbols; locks georg:1.2; strict; comment @# @; 1.2 date 2003.06.28.21.03.16; author georg; state Exp; branches; next 1.1; 1.1 date 2003.06.28.21.02.32; author georg; state Exp; branches; next ; desc @@ 1.2 log @*** empty log message *** @ text @#!/usr/bin/env python import string,sys import time,os,stat import traceback version = "$Id: fte-image-checker.py,v 1.9 2002/07/01 11:03:06 georg Exp georg $" maxage=100 # seconds workdir="/home/httpd/htdocs/sunpendulum/fte/img" offline="off.jpg" files="""azores.jpg bermuda.jpg cairo.jpg dubai.jpg ensenada.jpg granada.jpg maui.jpg neworleans.jpg hongkong.jpg""" class Picture: def __init__ (self,fpath,offline): self.fpath = fpath self.offline = offline self.isOffline = 0 self.doit() def __str__(self): return ("%14s, Age: %6d, Next check: %3d" % (self.fpath,self.getAge(),self.checkInSec())) def getAge(self): # return Age try: age = time.time()-os.stat(self.fpath)[stat.ST_MTIME] except e: sys.stdout.write("%s %s\n" % (str(e),str(self))) sys.stdout.flush() return age def checkInSec(self): u = maxage - self.getAge() if u > 0: return u else: return maxage def setOffline(self): # copy the offline thing if not self.isOffline: # Status change self.isOffline = 1 sys.stdout.write("setting offline: %s\n" % str(self)) sys.stdout.flush() offline_mtime = os.stat(self.fpath)[stat.ST_MTIME] o=open(self.offline) i=open(self.fpath,"w") i.write(o.read()) i.close() o.close() os.utime(self.fpath,(offline_mtime,offline_mtime)) def setOnline(self): if self.isOffline: # Status change self.isOffline = 0 sys.stdout.write("recovered: %s\n" % str(self)) sys.stdout.flush() def isOk(self): # no data manipulatio tooOld = self.getAge() > maxage if not tooOld: sys.stdout.write(" online: %s\n" % str(self)) sys.stdout.flush() else: sys.stdout.write( " OFFLINE:%s\n" % str(self)) sys.stdout.flush() return (not tooOld) def doit(self): if self.isOk(): self.setOnline() else: self.setOffline() def __cmp__ (self,other): a = self.checkInSec() b = other.checkInSec() return int(((a > b) - 0.5)*2) if __name__ == '__main__': os.chdir(workdir) f=string.split(files,""" """) l=[] for i in f: try: l.append(Picture(i,offline)) except: sys.stdout.write("Error with %s\n" % i) sys.stdout.flush() l.sort() while 1: for i in l: # sys.stdout.write( i try: i.doit() except: traceback.print_stack () l.sort() sleep=l[0].checkInSec() if sleep >= maxage: sys.stdout.write( "All are offline\n") sys.stdout.flush() sys.stdout.write( " sleeping: %d\n" % sleep) sys.stdout.flush() time.sleep(sleep) @ 1.1 log @Initial revision @ text @d10 1 a10 1 workdir="/home/httpd/sunpendulum/fte/img" d19 2 a20 1 neworleans.jpg""" @