#!/usr/bin/env python # ovine.py version 1.0.3 # dated 8 December 2000 # written by Julius Welby # object oriented version of my queue manager module "rc5". # for info see http://www.outwardlynormal.com/python/ovine.htm # # *** EDIT CONFIG TO USE THIS MODULE *** # config is a tuple of tuples to hold project configuration details. Each tuple contains: # 1) project name, 2)folder path, 3)buffer name, 4)empty size (bytes) and 5)Status: 1 = active 0 = ignore this project # The redundancy is deliberate, as I want to preserve the generality of the script. config = (('rc5', 'C:\Crypto', 'buff-in.rc5', 'buff-out.rc5", 8, 1), ('ogr', 'C:\Crypto', 'buff-in.ogr', 8, 0)) import os, sys, string, glob class Project: def __init__(self): self.name = config[proj][0] # read the configuration into the object attributes self.folder = config[proj][1] self.buffin = config[proj][2] self.buffout = config[proj][3] self.empty = config[proj][4] self.active = config[proj][5] self.statdict = {} # define statdict as a dictionary self.file = os.path.join(self.folder, self.buffin) # create a file attribute, the full path of the buffer self.buffout = self.pathcheck = os.path.exists(self.file) # check that the file attribute is a real file # create current list of possible candidates (plus current buffer) def candidates(self): self.statdict ={} self.filenames = glob.glob1(self.folder, '*'+ self.buffin) # glob.glob1 looks into the folder (arg1) for matches to arg2, returns list of matches self.number = len(self.filenames) # set self.number to the number of candidates (+ 1, the active buffer) for name in self.filenames: # loop through the names of files in the dir path = os.path.join(self.folder, name) stats = os.stat(path) # collect stats of candidate created = stats[8] # this is the last modification date self.statdict[created] = path # add the "last mod" and "path" to the dictionary as index, value pair def candidate_messages(self): if self.number >= 2: print 'Files waiting in the queue =', self.number-1 elif self.number<=1: print 'There are no candidate files waiting in the queue.' def promote(self): # identify find oldest candidate file in the list of candidates and promote it to be the new buffer if self.number >0: # if there is at least one candidate keylist = self.statdict.keys() # create list of last updated dates smallest = min(keylist) # identify smallest number (=oldest file) oldest = self.statdict[smallest] # get the name of the oldest candidate print 'Making', oldest, 'the new active buffer.' os.rename(oldest, self.file) # rename the file to be new buffer print 'New buffer created successfully.' self.candidate_messages() # print info about queue length else: self.candidate_messages() print 'Please download new work into', self.folder + ', renaming the files appropriately.' def delete_file(self, file): print 'Removing empty file.' os.remove(file) # delete the file at the filepath passed in print 'Empty file removed.' def main_function(self): print print 'Project', self.name, 'is active.' #Does the buffer file exist? pathcheck = os.path.exists(self.file) if pathcheck: #Check if file is empty stats = os.stat(self.file) # get the stats of the file size = stats[6] # extract the file size in bytes from the stats list print 'There are', size, 'characters in', self.buffin # just for info, print number of characters (bytes) if size > self.empty: # if there are more characters than an empty file print 'The file contains work. No action required.' # do nothing self.candidates() self.candidate_messages() else: self.delete_file(self.file) # delete the empty file self.candidates() # check for candidates self.promote() # run the promote function else: # file never existed, so try to promote print "There is no file called", self.buffin, "in the directory", self.folder + "." self.candidates() # check for candidates self.promote() # run the promote function # Does the file exist now? pathcheck = os.path.exists(self.file) if pathcheck ==0: # new buffer file still does not exist print print "If you have already downloaded work into the correct folder, you should check the script configuration settings, and the way the files have been renamed." print else: pass proj = 0 # set project counter to 0 print print '*'*30 for item in config: # for each project entry in the config tuple p = Project() # create instance p of class "Project" if p.active == 1: # if active for this project p.main_function() # run the main-function else: print print 'Project', p.name, 'is not active.' proj = proj + 1