Index: pykota/trunk/bin/lprngpykota
===================================================================
--- pykota/trunk/bin/lprngpykota (revision 1609)
+++ pykota/trunk/bin/lprngpykota (revision 1611)
@@ -24,4 +24,7 @@
 #
 # $Log$
+# Revision 1.3  2004/07/21 09:35:48  jalet
+# Software accounting seems to be OK with LPRng support now
+#
 # Revision 1.2  2004/07/20 22:47:38  jalet
 # Sanitizing
@@ -73,4 +76,121 @@
             return None
                 
+    def firstPass(self, policy, printer, user, userpquota) :            
+        """First pass done here."""
+        # first we have to check if previous job was correctly accounted for
+        if (printer.LastJob.JobAction != "DENY") and (printer.LastJob.JobSize is None) :
+            # here we know that previous job wasn't accounted for correctly
+            # we are sure (?) that it was hardware accounting which was used
+            # and that the second pass didn't work or wasn't even launched
+            # we know have to act just as if we were in second pass
+            raise PyKotaToolError, "Not implemented yet !"
+        
+        # tries to extract job-originating-hostname 
+        clienthost = self.getJobOriginatingHostname(printer.Name, user.Name, self.jobid)
+        self.logdebug("Client Hostname : %s" % (clienthost or "Unknown"))    
+        os.environ["PYKOTAJOBORIGINATINGHOSTNAME"] = str(clienthost or "")
+        
+        # indicates first pass
+        os.environ["PYKOTAPHASE"] = "BEFORE"
+        
+        # do we want strict or laxist quota enforcement ?
+        if self.config.getPrinterEnforcement(printer.Name) == "STRICT" :
+            self.softwareJobSize = self.precomputeJobSize()
+            self.softwareJobPrice = userpquota.computeJobPrice(self.softwareJobSize)
+            self.logdebug("Precomputed job's size is %s pages, price is %s units" % (self.softwareJobSize, self.softwareJobPrice))
+        os.environ["PYKOTAPRECOMPUTEDJOBSIZE"] = str(self.softwareJobSize)
+        os.environ["PYKOTAPRECOMPUTEDJOBPRICE"] = str(self.softwareJobPrice)
+        
+        # if no data to pass to real backend, probably a filter
+        # higher in the chain failed because of a misconfiguration.
+        # we deny the job in this case (nothing to print anyway)
+        if not self.jobSizeBytes :
+            self.printInfo(_("Job contains no data. Printing is denied."), "warn")
+            action = "DENY"
+        else :    
+            # checks the user's quota
+            action = self.warnUserPQuota(userpquota)
+        
+        # exports some new environment variables
+        os.environ["PYKOTAACTION"] = action
+        
+        # launches the pre hook
+        self.prehook(userpquota)
+        
+        self.logdebug("Job accounting begins.")
+        self.accounter.beginJob(userpquota)
+        
+        jobsize = None
+        if self.accounter.isSoftware :
+            self.accounter.endJob(userpquota)
+            jobsize = self.accounter.getJobSize()
+            self.logdebug("Job accounting ends.")
+            
+        if action == "DENY" :   
+            jobsize = 0
+            self.logdebug("Job size forced to 0 because printing was denied.")
+            
+        if jobsize is not None :    
+            # update the quota for the current user on this printer 
+            self.logdebug("Job size : %i" % jobsize)
+            self.logdebug("Updating user %s's quota on printer %s" % (user.Name, printer.Name))
+            jobprice = userpquota.increasePagesUsage(jobsize)
+            
+            printer.addJobToHistory(self.jobid, user, self.accounter.getLastPageCounter(), action, jobsize, jobprice, self.preserveinputfile, self.title, self.copies, self.options, clienthost, self.jobSizeBytes)
+            self.logdebug("Job added to history.")
+            
+            # exports some new environment variables
+            os.environ["PYKOTAPHASE"] = "AFTER"
+            os.environ["PYKOTAJOBSIZE"] = str(jobsize)
+            os.environ["PYKOTAJOBPRICE"] = str(jobprice)
+            
+            # then re-export user information with new value
+            self.exportUserInfo(userpquota)
+            
+            # Launches the post hook
+            self.posthook(userpquota)
+            
+            # here software accounting was completed.
+        else :
+            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)
+            self.logdebug("Job added to history during first pass : Job's size and price are still unknown.")
+            
+        if action == "DENY" :
+            return self.removeJob()
+        else :    
+            return self.acceptJob()
+        
+    def secondPass(self, policy, printer, user, userpquota) :    
+        """Second pass done here."""
+        # Last job for current printer has the same JobId than
+        # the current job, so we know we are in the second pass
+        if self.accounter.isSoftware :
+            # Software accounting method was used, and we are
+            # in second pass, so all work is already done, 
+            # now we just have to exit successfully
+            self.printInfo(_("Software accounting already done in first pass. Exiting."))
+        else :    
+            # Now we have to check if accounting was completely finished :
+            # if it is, then a big problem occured because with hardware
+            # accounting, only the second pass can know the job's size.
+            if (printer.LastJob.JobSize is not None) :
+                # this could only occur if configuration was modified 
+                # between first and second pass, and accounter changed
+                # from software to hardware during this short (?) time.
+                raise PyKotaToolError, _("Hardware accounting already finished ! This should be impossible, please report this problem ASAP.")
+                
+            # indicate phase change
+            os.environ["PYKOTAPHASE"] = "AFTER"
+            
+            # stops accounting. 
+            self.accounter.endJob(userpquota)
+            self.logdebug("Job accounting ends.")
+                
+            # retrieve the job size    
+            jobsize = self.accounter.getJobSize()
+            self.logdebug("Job size : %i" % jobsize)
+            raise PyKotaToolError, "Not implemented !"
+        return self.acceptJob()
+        
     def doWork(self, policy, printer, user, userpquota) :    
         """Most of the work is done here."""
@@ -89,106 +209,9 @@
             if (not printer.LastJob.Exists) or (printer.LastJob.JobId != self.jobid) :
                 # Last job for current printer has a different JobId than
-                # the current job, so we know we are in the first pass
-                # tries to extract job-originating-hostname 
-                clienthost = self.getJobOriginatingHostname(printer.Name, user.Name, self.jobid)
-                self.logdebug("Client Hostname : %s" % (clienthost or "Unknown"))    
-                os.environ["PYKOTAJOBORIGINATINGHOSTNAME"] = str(clienthost or "")
-                
-                # indicates first pass
-                os.environ["PYKOTAPHASE"] = "BEFORE"
-                
-                # do we want strict or laxist quota enforcement ?
-                if self.config.getPrinterEnforcement(printer.Name) == "STRICT" :
-                    self.softwareJobSize = self.precomputeJobSize()
-                    self.softwareJobPrice = userpquota.computeJobPrice(self.softwareJobSize)
-                    self.logdebug("Precomputed job's size is %s pages, price is %s units" % (self.softwareJobSize, self.softwareJobPrice))
-                os.environ["PYKOTAPRECOMPUTEDJOBSIZE"] = str(self.softwareJobSize)
-                os.environ["PYKOTAPRECOMPUTEDJOBPRICE"] = str(self.softwareJobPrice)
-                
-                # if no data to pass to real backend, probably a filter
-                # higher in the chain failed because of a misconfiguration.
-                # we deny the job in this case (nothing to print anyway)
-                if not self.jobSizeBytes :
-                    self.printInfo(_("Job contains no data. Printing is denied."), "warn")
-                    action = "DENY"
-                else :    
-                    # checks the user's quota
-                    action = self.warnUserPQuota(userpquota)
-                
-                # exports some new environment variables
-                os.environ["PYKOTAACTION"] = action
-                
-                # launches the pre hook
-                self.prehook(userpquota)
-                
-                self.logdebug("Job accounting begins.")
-                self.accounter.beginJob(userpquota)
-                
-                jobsize = None
-                if self.accounter.isSoftware :
-                    self.accounter.endJob(userpquota)
-                    jobsize = self.accounter.getJobSize()
-                    self.logdebug("Job accounting ends.")
-                    
-                if action == "DENY" :   
-                    jobsize = 0
-                    self.logdebug("Job size forced to 0 because printing was denied.")
-                    
-                if jobsize is not None :    
-                    # update the quota for the current user on this printer 
-                    self.logdebug("Job size : %i" % jobsize)
-                    self.logdebug("Updating user %s's quota on printer %s" % (user.Name, printer.Name))
-                    jobprice = userpquota.increasePagesUsage(jobsize)
-                    
-                    printer.addJobToHistory(self.jobid, user, self.accounter.getLastPageCounter(), action, jobsize, jobprice, self.preserveinputfile, self.title, self.copies, self.options, clienthost, self.jobSizeBytes)
-                    self.logdebug("Job added to history.")
-                    
-                    # exports some new environment variables
-                    os.environ["PYKOTAPHASE"] = "AFTER"
-                    os.environ["PYKOTAJOBSIZE"] = str(jobsize)
-                    os.environ["PYKOTAJOBPRICE"] = str(jobprice)
-                    
-                    # then re-export user information with new value
-                    self.exportUserInfo(userpquota)
-                    
-                    # Launches the post hook
-                    self.posthook(userpquota)
-                    
-                    # here software accounting was completed.
-                else :
-                    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)
-                    self.logdebug("Job added to history during first pass : Job's size and price are still unknown.")
-                    
-                if action == "DENY" :
-                    return self.removeJob()
-                else :    
-                    return self.acceptJob()
+                # the current job, so we know we are in the first pass.
+                return self.firstPass(policy, printer, user, userpquota)
             else :    
-                # Last job for current printer has the same JobId than
-                # the current job, so we know we are in the second pass
-                if self.accounter.isSoftware :
-                    # Software accounting method was used, and we are
-                    # in second pass, so all work is already done, 
-                    # now we just have to exit successfully
-                    self.printInfo(_("Software accounting already done in first pass. Exiting."))
-                else :    
-                    # Now we have to check if accounting was completely finished :
-                    # if it is, then a big problem occured because with hardware
-                    # accounting, only the second pass can know the job's size.
-                    if (printer.LastJob.JobSize is not None) :
-                        raise PyKotaToolError, _("Hardware accounting already finished ! This should be impossible, please report this problem ASAP.")
-                        
-                    # indicate phase change
-                    os.environ["PYKOTAPHASE"] = "AFTER"
-                    
-                    # stops accounting. 
-                    self.accounter.endJob(userpquota)
-                    self.logdebug("Job accounting ends.")
-                        
-                    # retrieve the job size    
-                    jobsize = self.accounter.getJobSize()
-                    self.logdebug("Job size : %i" % jobsize)
-                    raise PyKotaToolError, "Not implemented !"
-                return self.acceptJob()
+                # and here we know we are in second pass.
+                return self.secondPass(policy, printer, user, userpquota)
             
 if __name__ == "__main__" :    
