Changeset 1901 for pykota

Show
Ignore:
Timestamp:
11/06/04 23:35:58 (20 years ago)
Author:
jalet
Message:

Added a miniparser for IPP messages (RFC 2910). The job-originating-host-name
retrieval is now fiable, unless the CUPS developpers change something...

Location:
pykota/trunk
Files:
1 added
3 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/bin/cupspykota

    r1897 r1901  
    2424# 
    2525# $Log$ 
     26# Revision 1.76  2004/11/06 22:35:58  jalet 
     27# Added a miniparser for IPP messages (RFC 2910). The job-originating-host-name 
     28# retrieval is now fiable, unless the CUPS developpers change something... 
     29# 
    2630# Revision 1.75  2004/11/01 15:45:25  jalet 
    2731# Added many debug messages. 
     
    299303from pykota.storage import PyKotaStorageError 
    300304from pykota.accounter import PyKotaAccounterError 
     305from pykota.ipp import IPPMessage, PyKotaIPPError 
    301306     
    302307class PyKotaPopen4(popen2.Popen4) : 
     
    338343        return 0 
    339344         
    340     def getPageLogLocation(self) : 
    341         """Retrieves CUPS' page_log file location.""" 
    342         location = None 
     345    def getCupsConfigDirectives(self, directives=[]) : 
     346        """Retrieves some CUPS directives from its configuration file.""" 
     347        dirvalues = {}  
    343348        cupsroot = os.environ.get("CUPS_SERVERROOT", "/etc/cups") 
    344349        cupsdconf = os.path.join(cupsroot, "cupsd.conf") 
     
    347352        except IOError :     
    348353            self.logdebug("Unable to open %s" % cupsdconf) 
    349             return # file doesn't exist or can't be read 
    350354        else :     
    351355            for line in conffile.readlines() : 
    352356                linecopy = line.strip().lower() 
    353                 if linecopy.startswith("pagelog ") : 
    354                     try : 
    355                         location = line.split()[1] 
    356                     except :     
    357                         pass # ignore errors, we take the last value in any case. 
     357                for di in [d.lower() for d in directives] : 
     358                    if linecopy.startswith("%s " % di) : 
     359                        try : 
     360                            val = line.split()[1] 
     361                        except :     
     362                            pass # ignore errors, we take the last value in any case. 
     363                        else :     
     364                            dirvalues[di] = val 
    358365            conffile.close()             
    359             return location             
    360              
    361     def getJobOriginatingHostname(self, printername, username, jobid) : 
     366        return dirvalues        
     367             
     368    def getJobOriginatingHostnameFromPageLog(self, cupsconfig, printername, username, jobid) : 
    362369        """Retrieves the job-originating-hostname from the CUPS page_log file if possible.""" 
    363         pagelogpath = self.getPageLogLocation() or "/var/log/cups/page_log" 
     370        pagelogpath = cupsconfig.get("pagelog", "/var/log/cups/page_log") 
     371        self.logdebug("Trying to extract job-originating-host-name from %s" % pagelogpath) 
    364372        try : 
    365373            pagelog = open(pagelogpath, "r") 
     
    383391            if matchingline is None : 
    384392                self.logdebug("No matching line found in %s" % pagelogpath) 
    385                 return # correct line not found, job-originating-hostname unknown 
     393                return # correct line not found, job-originating-host-name unknown 
    386394            else :     
    387395                return matchingline.split()[-1] 
     
    398406            self.exportUserInfo(userpquota) 
    399407             
    400             # tries to extract job-originating-hostname  
    401             clienthost = self.getJobOriginatingHostname(printer.Name, user.Name, self.jobid) 
     408            # tries to extract job-originating-host-name and other information 
     409            cupsdconf = self.getCupsConfigDirectives(["PageLog", "RequestRoot"]) 
     410            requestroot = cupsdconf.get("requestroot", "/var/spool/cups") 
     411            if (len(self.jobid) < 5) and self.jobid.isdigit() : 
     412                ippmessagefile = "c%05i" % int(self.jobid) 
     413            else :     
     414                ippmessagefile = "c%s" % self.jobid 
     415            ippmessagefile = os.path.join(requestroot, ippmessagefile) 
     416            ippmessage = {} 
     417            try : 
     418                ippdatafile = open(ippmessagefile) 
     419            except :     
     420                self.printInfo("Unable to open IPP message file %s" % ippmessagefile, "warn") 
     421            else :     
     422                self.logdebug("Parsing of IPP message file %s begins." % ippmessagefile) 
     423                ippmessage = IPPMessage(ippdatafile.read()) 
     424                self.logdebug("Parsing of IPP message file %s ends." % ippmessagefile) 
     425                ippdatafile.close() 
     426                 
     427            clienthost = ippmessage.get("job-originating-host-name") \ 
     428                         or self.getJobOriginatingHostnameFromPageLog(cupsdconf, printer.Name, user.Name, self.jobid) 
    402429            self.logdebug("Client Hostname : %s" % (clienthost or "Unknown"))     
    403430            os.environ["PYKOTAJOBORIGINATINGHOSTNAME"] = str(clienthost or "") 
     431             
     432            # TODO : extract username (double check ?) and billing code too 
    404433             
    405434            # enters first phase 
  • pykota/trunk/NEWS

    r1894 r1901  
    2222PyKota NEWS : 
    2323 
     24    - 1.21alpha1 : 
     25     
     26        - added an IPP messages miniparser. This will allow PyKota 
     27          to extract informations from the CUPS request files directly 
     28          without needing to use the CUPS API.  
     29         
    2430    - 1.20 : 
    2531     
  • pykota/trunk/pykota/version.py

    r1894 r1901  
    2222# 
    2323 
    24 __version__ = "1.20_unofficial" 
     24__version__ = "1.21alpha1_unofficial" 
    2525 
    2626__doc__ = """PyKota : a complete Printing Quota Solution for CUPS and LPRng."""