Changeset 1611 for pykota/trunk/bin

Show
Ignore:
Timestamp:
07/21/04 11:35:48 (20 years ago)
Author:
jalet
Message:

Software accounting seems to be OK with LPRng support now

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/bin/lprngpykota

    r1609 r1611  
    2424# 
    2525# $Log$ 
     26# Revision 1.3  2004/07/21 09:35:48  jalet 
     27# Software accounting seems to be OK with LPRng support now 
     28# 
    2629# Revision 1.2  2004/07/20 22:47:38  jalet 
    2730# Sanitizing 
     
    7376            return None 
    7477                 
     78    def firstPass(self, policy, printer, user, userpquota) :             
     79        """First pass done here.""" 
     80        # first we have to check if previous job was correctly accounted for 
     81        if (printer.LastJob.JobAction != "DENY") and (printer.LastJob.JobSize is None) : 
     82            # here we know that previous job wasn't accounted for correctly 
     83            # we are sure (?) that it was hardware accounting which was used 
     84            # and that the second pass didn't work or wasn't even launched 
     85            # we know have to act just as if we were in second pass 
     86            raise PyKotaToolError, "Not implemented yet !" 
     87         
     88        # tries to extract job-originating-hostname  
     89        clienthost = self.getJobOriginatingHostname(printer.Name, user.Name, self.jobid) 
     90        self.logdebug("Client Hostname : %s" % (clienthost or "Unknown"))     
     91        os.environ["PYKOTAJOBORIGINATINGHOSTNAME"] = str(clienthost or "") 
     92         
     93        # indicates first pass 
     94        os.environ["PYKOTAPHASE"] = "BEFORE" 
     95         
     96        # do we want strict or laxist quota enforcement ? 
     97        if self.config.getPrinterEnforcement(printer.Name) == "STRICT" : 
     98            self.softwareJobSize = self.precomputeJobSize() 
     99            self.softwareJobPrice = userpquota.computeJobPrice(self.softwareJobSize) 
     100            self.logdebug("Precomputed job's size is %s pages, price is %s units" % (self.softwareJobSize, self.softwareJobPrice)) 
     101        os.environ["PYKOTAPRECOMPUTEDJOBSIZE"] = str(self.softwareJobSize) 
     102        os.environ["PYKOTAPRECOMPUTEDJOBPRICE"] = str(self.softwareJobPrice) 
     103         
     104        # if no data to pass to real backend, probably a filter 
     105        # higher in the chain failed because of a misconfiguration. 
     106        # we deny the job in this case (nothing to print anyway) 
     107        if not self.jobSizeBytes : 
     108            self.printInfo(_("Job contains no data. Printing is denied."), "warn") 
     109            action = "DENY" 
     110        else :     
     111            # checks the user's quota 
     112            action = self.warnUserPQuota(userpquota) 
     113         
     114        # exports some new environment variables 
     115        os.environ["PYKOTAACTION"] = action 
     116         
     117        # launches the pre hook 
     118        self.prehook(userpquota) 
     119         
     120        self.logdebug("Job accounting begins.") 
     121        self.accounter.beginJob(userpquota) 
     122         
     123        jobsize = None 
     124        if self.accounter.isSoftware : 
     125            self.accounter.endJob(userpquota) 
     126            jobsize = self.accounter.getJobSize() 
     127            self.logdebug("Job accounting ends.") 
     128             
     129        if action == "DENY" :    
     130            jobsize = 0 
     131            self.logdebug("Job size forced to 0 because printing was denied.") 
     132             
     133        if jobsize is not None :     
     134            # update the quota for the current user on this printer  
     135            self.logdebug("Job size : %i" % jobsize) 
     136            self.logdebug("Updating user %s's quota on printer %s" % (user.Name, printer.Name)) 
     137            jobprice = userpquota.increasePagesUsage(jobsize) 
     138             
     139            printer.addJobToHistory(self.jobid, user, self.accounter.getLastPageCounter(), action, jobsize, jobprice, self.preserveinputfile, self.title, self.copies, self.options, clienthost, self.jobSizeBytes) 
     140            self.logdebug("Job added to history.") 
     141             
     142            # exports some new environment variables 
     143            os.environ["PYKOTAPHASE"] = "AFTER" 
     144            os.environ["PYKOTAJOBSIZE"] = str(jobsize) 
     145            os.environ["PYKOTAJOBPRICE"] = str(jobprice) 
     146             
     147            # then re-export user information with new value 
     148            self.exportUserInfo(userpquota) 
     149             
     150            # Launches the post hook 
     151            self.posthook(userpquota) 
     152             
     153            # here software accounting was completed. 
     154        else : 
     155            printer.addJobToHistory(self.jobid, user, self.accounter.getLastPageCounter(), action, filename=self.preserveinputfile, title=self.title, copies=self.copies, options=self.options, clienthost=clienthost, jobsizebytes=self.jobSizeBytes) 
     156            self.logdebug("Job added to history during first pass : Job's size and price are still unknown.") 
     157             
     158        if action == "DENY" : 
     159            return self.removeJob() 
     160        else :     
     161            return self.acceptJob() 
     162         
     163    def secondPass(self, policy, printer, user, userpquota) :     
     164        """Second pass done here.""" 
     165        # Last job for current printer has the same JobId than 
     166        # the current job, so we know we are in the second pass 
     167        if self.accounter.isSoftware : 
     168            # Software accounting method was used, and we are 
     169            # in second pass, so all work is already done,  
     170            # now we just have to exit successfully 
     171            self.printInfo(_("Software accounting already done in first pass. Exiting.")) 
     172        else :     
     173            # Now we have to check if accounting was completely finished : 
     174            # if it is, then a big problem occured because with hardware 
     175            # accounting, only the second pass can know the job's size. 
     176            if (printer.LastJob.JobSize is not None) : 
     177                # this could only occur if configuration was modified  
     178                # between first and second pass, and accounter changed 
     179                # from software to hardware during this short (?) time. 
     180                raise PyKotaToolError, _("Hardware accounting already finished ! This should be impossible, please report this problem ASAP.") 
     181                 
     182            # indicate phase change 
     183            os.environ["PYKOTAPHASE"] = "AFTER" 
     184             
     185            # stops accounting.  
     186            self.accounter.endJob(userpquota) 
     187            self.logdebug("Job accounting ends.") 
     188                 
     189            # retrieve the job size     
     190            jobsize = self.accounter.getJobSize() 
     191            self.logdebug("Job size : %i" % jobsize) 
     192            raise PyKotaToolError, "Not implemented !" 
     193        return self.acceptJob() 
     194         
    75195    def doWork(self, policy, printer, user, userpquota) :     
    76196        """Most of the work is done here.""" 
     
    89209            if (not printer.LastJob.Exists) or (printer.LastJob.JobId != self.jobid) : 
    90210                # Last job for current printer has a different JobId than 
    91                 # the current job, so we know we are in the first pass 
    92                 # tries to extract job-originating-hostname  
    93                 clienthost = self.getJobOriginatingHostname(printer.Name, user.Name, self.jobid) 
    94                 self.logdebug("Client Hostname : %s" % (clienthost or "Unknown"))     
    95                 os.environ["PYKOTAJOBORIGINATINGHOSTNAME"] = str(clienthost or "") 
    96                  
    97                 # indicates first pass 
    98                 os.environ["PYKOTAPHASE"] = "BEFORE" 
    99                  
    100                 # do we want strict or laxist quota enforcement ? 
    101                 if self.config.getPrinterEnforcement(printer.Name) == "STRICT" : 
    102                     self.softwareJobSize = self.precomputeJobSize() 
    103                     self.softwareJobPrice = userpquota.computeJobPrice(self.softwareJobSize) 
    104                     self.logdebug("Precomputed job's size is %s pages, price is %s units" % (self.softwareJobSize, self.softwareJobPrice)) 
    105                 os.environ["PYKOTAPRECOMPUTEDJOBSIZE"] = str(self.softwareJobSize) 
    106                 os.environ["PYKOTAPRECOMPUTEDJOBPRICE"] = str(self.softwareJobPrice) 
    107                  
    108                 # if no data to pass to real backend, probably a filter 
    109                 # higher in the chain failed because of a misconfiguration. 
    110                 # we deny the job in this case (nothing to print anyway) 
    111                 if not self.jobSizeBytes : 
    112                     self.printInfo(_("Job contains no data. Printing is denied."), "warn") 
    113                     action = "DENY" 
    114                 else :     
    115                     # checks the user's quota 
    116                     action = self.warnUserPQuota(userpquota) 
    117                  
    118                 # exports some new environment variables 
    119                 os.environ["PYKOTAACTION"] = action 
    120                  
    121                 # launches the pre hook 
    122                 self.prehook(userpquota) 
    123                  
    124                 self.logdebug("Job accounting begins.") 
    125                 self.accounter.beginJob(userpquota) 
    126                  
    127                 jobsize = None 
    128                 if self.accounter.isSoftware : 
    129                     self.accounter.endJob(userpquota) 
    130                     jobsize = self.accounter.getJobSize() 
    131                     self.logdebug("Job accounting ends.") 
    132                      
    133                 if action == "DENY" :    
    134                     jobsize = 0 
    135                     self.logdebug("Job size forced to 0 because printing was denied.") 
    136                      
    137                 if jobsize is not None :     
    138                     # update the quota for the current user on this printer  
    139                     self.logdebug("Job size : %i" % jobsize) 
    140                     self.logdebug("Updating user %s's quota on printer %s" % (user.Name, printer.Name)) 
    141                     jobprice = userpquota.increasePagesUsage(jobsize) 
    142                      
    143                     printer.addJobToHistory(self.jobid, user, self.accounter.getLastPageCounter(), action, jobsize, jobprice, self.preserveinputfile, self.title, self.copies, self.options, clienthost, self.jobSizeBytes) 
    144                     self.logdebug("Job added to history.") 
    145                      
    146                     # exports some new environment variables 
    147                     os.environ["PYKOTAPHASE"] = "AFTER" 
    148                     os.environ["PYKOTAJOBSIZE"] = str(jobsize) 
    149                     os.environ["PYKOTAJOBPRICE"] = str(jobprice) 
    150                      
    151                     # then re-export user information with new value 
    152                     self.exportUserInfo(userpquota) 
    153                      
    154                     # Launches the post hook 
    155                     self.posthook(userpquota) 
    156                      
    157                     # here software accounting was completed. 
    158                 else : 
    159                     printer.addJobToHistory(self.jobid, user, self.accounter.getLastPageCounter(), action, filename=self.preserveinputfile, title=self.title, copies=self.copies, options=self.options, clienthost=clienthost, jobsizebytes=self.jobSizeBytes) 
    160                     self.logdebug("Job added to history during first pass : Job's size and price are still unknown.") 
    161                      
    162                 if action == "DENY" : 
    163                     return self.removeJob() 
    164                 else :     
    165                     return self.acceptJob() 
     211                # the current job, so we know we are in the first pass. 
     212                return self.firstPass(policy, printer, user, userpquota) 
    166213            else :     
    167                 # Last job for current printer has the same JobId than 
    168                 # the current job, so we know we are in the second pass 
    169                 if self.accounter.isSoftware : 
    170                     # Software accounting method was used, and we are 
    171                     # in second pass, so all work is already done,  
    172                     # now we just have to exit successfully 
    173                     self.printInfo(_("Software accounting already done in first pass. Exiting.")) 
    174                 else :     
    175                     # Now we have to check if accounting was completely finished : 
    176                     # if it is, then a big problem occured because with hardware 
    177                     # accounting, only the second pass can know the job's size. 
    178                     if (printer.LastJob.JobSize is not None) : 
    179                         raise PyKotaToolError, _("Hardware accounting already finished ! This should be impossible, please report this problem ASAP.") 
    180                          
    181                     # indicate phase change 
    182                     os.environ["PYKOTAPHASE"] = "AFTER" 
    183                      
    184                     # stops accounting.  
    185                     self.accounter.endJob(userpquota) 
    186                     self.logdebug("Job accounting ends.") 
    187                          
    188                     # retrieve the job size     
    189                     jobsize = self.accounter.getJobSize() 
    190                     self.logdebug("Job size : %i" % jobsize) 
    191                     raise PyKotaToolError, "Not implemented !" 
    192                 return self.acceptJob() 
     214                # and here we know we are in second pass. 
     215                return self.secondPass(policy, printer, user, userpquota) 
    193216             
    194217if __name__ == "__main__" :