Show
Ignore:
Timestamp:
05/26/04 16:50:12 (20 years ago)
Author:
jalet
Message:

First try at saving the job-originating-hostname in the database

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/bin/cupspykota

    r1499 r1502  
    2424# 
    2525# $Log$ 
     26# Revision 1.53  2004/05/26 14:49:35  jalet 
     27# First try at saving the job-originating-hostname in the database 
     28# 
    2629# Revision 1.52  2004/05/25 09:15:13  jalet 
    2730# accounter.py : old code deleted 
     
    252255        return 0 
    253256         
     257    def getPageLogLocation(self) : 
     258        """Retrieves CUPS' page_log file location.""" 
     259        location = None 
     260        cupsroot = os.environ.get("CUPS_SERVERROOT", "/etc/cups") 
     261        cupsdconf = os.path.join(cupsroot, "cupsd.conf") 
     262        try : 
     263            conffile = open(cupsdconf, "r") 
     264        except IOError :     
     265            return # file doesn't exist or can't be read 
     266        else :     
     267            for line in conffile.readlines() : 
     268                linecopy = line.strip().lower() 
     269                if linecopy.startswith("pagelog ") : 
     270                    try : 
     271                        location = line.split()[1] 
     272                    except :     
     273                        pass # ignore errors, we take the last value in any case. 
     274            conffile.close()             
     275            return location             
     276             
     277    def getJobOriginatingHostname(self, printername, username, jobid) : 
     278        """Retrieves the job-originating-hostname from the CUPS page_log file if possible.""" 
     279        pagelogpath = self.getPageLogLocation() or "/var/log/cups/page_log" 
     280        try : 
     281            pagelog = open(pagelogpath, "r") 
     282        except IOError :     
     283            return # no page log or can't read it, originating hostname unknown yet 
     284        else :     
     285            # TODO : read backward so we could take first value seen 
     286            # TODO : here we read forward so we must take the last value seen 
     287            prefix = "%s %s %s" % (printername, username, jobid) 
     288            matchingline = None 
     289            while 1 : 
     290                line = pagelog.readline() 
     291                if not line : 
     292                    break 
     293                else : 
     294                    line = line.strip() 
     295                    if line.startswith(prefix) :     
     296                        matchingline = line # no break, because we read forward 
     297            pagelog.close()         
     298            if matchingline is None : 
     299                return # correct line not found, job-originating-hostname unknown 
     300            else :     
     301                return matchingline.split()[-1] 
     302                 
    254303    def doWork(self, policy, printer, user, userpquota) :     
    255304        """Most of the work is done here.""" 
     
    262311            # exports user information with initial values 
    263312            self.exportUserInfo(userpquota) 
     313             
     314            # tries to extract job-originating-hostname  
     315            clienthost = self.getJobOriginatingHostname(printer.Name, user.Name, self.jobid) 
     316            os.putenv("PYKOTAJOBORIGINATINGHOSTNAME", str(clienthost or "")) 
    264317             
    265318            # enters first phase 
     
    317370             
    318371            # adds the current job to history     
    319             printer.addJobToHistory(self.jobid, user, self.accounter.getLastPageCounter(), action, jobsize, jobprice, self.preserveinputfile, self.title, self.copies, self.options) 
     372            printer.addJobToHistory(self.jobid, user, self.accounter.getLastPageCounter(), action, jobsize, jobprice, self.preserveinputfile, self.title, self.copies, self.options, clienthost) 
    320373            self.logdebug("Job added to history.") 
    321374