- Timestamp:
- 11/13/03 00:29:24 (21 years ago)
- Location:
- pykota/trunk
- Files:
-
- 6 modified
Legend:
- Unmodified
- Added
- Removed
-
pykota/trunk/bin/cupspykota
r1178 r1180 24 24 # 25 25 # $Log$ 26 # Revision 1.3 2003/11/12 23:27:44 jalet 27 # More work on new backend. This commit may be unstable. 28 # 26 29 # Revision 1.2 2003/11/12 09:33:34 jalet 27 30 # New CUPS backend supports device enumeration … … 120 123 # we let the job pass through, but log a warning message 121 124 thebackend.logger.log_message(_("Printer %s not registered in the PyKota system") % thebackend.printername, "warn") 125 action = "ALLOW" 122 126 else : 123 127 for dummy in range(2) : … … 157 161 break 158 162 159 # if user exists, do accounting160 163 if user.Exists : 161 164 # Is the current user allowed to print at all ? … … 168 171 return 0 169 172 170 MAXTRIES = 12 # maximum number of tries to get the printer's internal page counter171 TIMETOSLEEP = 10 # number of seconds to sleep between two tries to get the printer's internal page counter172 requester = openRequester(thebackend.config, thebackend.printername)173 def getPPC(requester, backend) :174 for i in range(MAXTRIES) :175 try :176 counterbeforejob = requester.getPrinterPageCounter(backend.printerhostname)177 except PyKotaRequesterError, msg :178 # can't get actual page counter, assume printer is off or warming up179 # log the message anyway.180 backend.logger.log_message("%s" % msg, "warn")181 counterbeforejob = None182 else :183 # printer answered, it is on so we can exit the loop184 break185 time.sleep(TIMETOSLEEP)186 return counterbeforejob187 188 173 if action not in ["ALLOW", "WARN"] : 189 174 # if not allowed to print then die, else proceed. … … 193 178 else : 194 179 # pass the job untouched to the underlying layer 195 # but get printer page counter before and after 196 # print job is submitted to the hardware. 197 198 # get page counter before job 199 before = getPPC(requester, thebackend) 200 201 # executes backend 202 # TODO : use correct original backend. 180 # and starts accounting at the same time 181 thebackend.accounter.beginJob(printer, user) 182 183 # executes original backend 184 #mustclose = 0 185 #if thebackend.inputfile is not None : 186 # if hasattr(thebackend.inputfile, "read") : 187 # infile = thebackend.inputfile 188 # else : 189 # infile = open(thebackend.inputfile, "rb") 190 # mustclose = 1 191 #else : 192 # infile = sys.stdin 193 203 194 realbackend = os.path.join(os.path.split(sys.argv[0])[0], thebackend.originalbackend) 204 195 retcode = os.spawnve(os.P_WAIT, realbackend, [os.environ["DEVICE_URI"]] + sys.argv[1:], os.environ) 205 196 206 # get page counter after job 207 after = getPPC(requester, thebackend) 208 209 # Computes the last job size as the difference between internal page 210 # counter in the printer and last page counter taken from the Quota 211 # Storage database for this particular printer 212 try : 213 jobsize = (after - before) 214 except : 215 jobsize = 0 216 197 # stops accounting. 198 thebackend.accounter.endJob(printer, user) 199 200 # retrieve the job size 201 jobsize = thebackend.accounter.getJobSize() 202 217 203 # update the quota for the current user on this printer 218 204 if printer.Exists : … … 223 209 224 210 # adds the current job to history 225 printer.addJobToHistory(thebackend.jobid, user, after, action, jobsize)211 printer.addJobToHistory(thebackend.jobid, user, thebackend.accounter.getLastPageCounter(), action, jobsize) 226 212 227 213 return retcode … … 242 228 if status is None : 243 229 for d in devices : 230 # each line is of the form : 'xxxx xxxx "xxxx xxx" "xxxx xxx"' 231 # so we have to decompose it carefully 244 232 fdevice = cStringIO.StringIO("%s" % d) 245 233 tokenizer = shlex.shlex(fdevice) … … 253 241 break 254 242 fdevice.close() 255 (devicetype, device, name, fullname) = arguments 256 if name.startswith('"') and name.endswith('"') : 257 name = name[1:-1] 258 if fullname.startswith('"') and fullname.endswith('"') : 259 fullname = fullname[1:-1] 260 print '%s cupspykota:%s "PyKota+%s" "PyKota managed %s"' % (devicetype, device, name, fullname) 243 try : 244 (devicetype, device, name, fullname) = arguments 245 except ValueError : 246 pass # ignore this 'bizarre' device 247 else : 248 if name.startswith('"') and name.endswith('"') : 249 name = name[1:-1] 250 if fullname.startswith('"') and fullname.endswith('"') : 251 fullname = fullname[1:-1] 252 print '%s cupspykota:%s "PyKota+%s" "PyKota managed %s"' % (devicetype, device, name, fullname) 261 253 retcode = 0 262 254 elif len(sys.argv) not in (6, 7) : -
pykota/trunk/bin/edpykota
r1179 r1180 24 24 # 25 25 # $Log$ 26 # Revision 1.62 2003/11/12 23:28:38 jalet 27 # More work on new backend. This commit may be unstable. 28 # 26 29 # Revision 1.61 2003/11/12 13:06:35 jalet 27 30 # Bug fix wrt no user/group name command line argument to edpykota … … 516 519 self.logger.log_message(_("Invalid user name %s") % entry.Name) 517 520 518 if not entrypquota.Exists:521 if entry.Exists and (not entrypquota.Exists) : 519 522 # not found 520 523 if options["add"] : -
pykota/trunk/pykota/accounter.py
r1144 r1180 22 22 # 23 23 # $Log$ 24 # Revision 1.6 2003/11/12 23:28:55 jalet 25 # More work on new backend. This commit may be unstable. 26 # 24 27 # Revision 1.5 2003/10/07 09:07:28 jalet 25 28 # Character encoding added to please latest version of Python … … 58 61 self.arguments = arguments 59 62 63 def getLastPageCounter(self) : 64 """Returns last internal page counter value (possibly faked).""" 65 try : 66 return self.LastPageCounter 67 except : 68 return 0 69 60 70 def filterInput(self, inputfile) : 61 71 """Transparent filter.""" -
pykota/trunk/pykota/accounters/external.py
r1144 r1180 22 22 # 23 23 # $Log$ 24 # Revision 1.8 2003/11/12 23:29:24 jalet 25 # More work on new backend. This commit may be unstable. 26 # 24 27 # Revision 1.7 2003/10/07 09:07:28 jalet 25 28 # Character encoding added to please latest version of Python … … 54 57 55 58 class Accounter(AccounterBase) : 59 def beginJob(self, printer, user) : 60 """Saves the computed job size.""" 61 # computes job's size 62 self.JobSize = self.computeJobSize() 63 64 # get last job information for this printer 65 if not printer.LastJob.Exists : 66 # The printer hasn't been used yet, from PyKota's point of view 67 self.LastPageCounter = 0 68 else : 69 # get last job size and page counter from Quota Storage 70 # Last lifetime page counter before actual job is 71 # last page counter + last job size 72 self.LastPageCounter = int(printer.LastJob.PrinterPageCounter or 0) + int(printer.LastJob.JobSize or 0) 73 74 def endJob(self, printer, user) : 75 """Do nothing.""" 76 pass 77 78 def getJobSize(self) : 79 """Returns the actual job size.""" 80 try : 81 return self.JobSize 82 except AttributeError : 83 return 0 84 56 85 def doAccounting(self, printer, user) : 57 86 """Deletgates the computation of the job size to an external command. … … 59 88 The command must print the job size on its standard output and exit successfully. 60 89 """ 90 self.beginJob(printer, user) 91 61 92 # get the job size, which is real job size * number of copies. 62 93 jobsize = self.getJobSize() * self.filter.copies 63 64 # get last job information for this printer65 if not printer.LastJob.Exists :66 # The printer hasn't been used yet, from PyKota's point of view67 counterbeforejob = 068 else :69 # get last job size and page counter from Quota Storage70 # Last lifetime page counter before actual job is71 # last page counter + last job size72 counterbeforejob = int(printer.LastJob.PrinterPageCounter or 0) + int(printer.LastJob.JobSize or 0)73 94 74 95 # Is the current user allowed to print at all ? … … 83 104 84 105 # adds the current job to history 85 printer.addJobToHistory(self.filter.jobid, user, counterbeforejob, action, jobsize) 106 printer.addJobToHistory(self.filter.jobid, user, self.getLastPageCounter(), action, jobsize) 107 108 self.endJob(printer, user) 86 109 87 110 return action 88 111 89 def getJobSize(self) :112 def computeJobSize(self) : 90 113 """Feeds an external command with our datas to let it compute the job size, and return its value.""" 91 114 temporary = None -
pykota/trunk/pykota/accounters/querying.py
r1144 r1180 22 22 # 23 23 # $Log$ 24 # Revision 1.7 2003/11/12 23:29:24 jalet 25 # More work on new backend. This commit may be unstable. 26 # 24 27 # Revision 1.6 2003/10/07 09:07:29 jalet 25 28 # Character encoding added to please latest version of Python … … 44 47 45 48 import sys 49 import os 46 50 import time 47 51 from pykota.accounter import AccounterBase, PyKotaAccounterError … … 52 56 53 57 class Accounter(AccounterBase) : 58 def __init__(self, kotabackend, arguments) : 59 """Initializes querying accounter.""" 60 AccounterBase.__init__(self, kotabackend, arguments) 61 self.requester = openRequester(kotabackend.config, kotabackend.printername) 62 63 def getPrinterInternalPageCounter(self) : 64 """Returns the printer's internal page counter.""" 65 global MAXTRIES, TIMETOSLEEP 66 for i in range(MAXTRIES) : 67 try : 68 counter = self.requester.getPrinterPageCounter(self.filter.printerhostname) 69 except PyKotaRequesterError, msg : 70 # can't get actual page counter, assume printer is off or warming up 71 # log the message anyway. 72 self.filter.logger.log_message("%s" % msg, "warn") 73 counter = None 74 else : 75 # printer answered, it is on so we can exit the loop 76 break 77 time.sleep(TIMETOSLEEP) 78 return counter 79 80 def beginJob(self, printer, user) : 81 """Saves printer internal page counter at start of job.""" 82 # save page counter before job 83 self.LastPageCounter = self.counterbefore = self.getPrinterInternalPageCounter() 84 85 def endJob(self, printer, user) : 86 """Saves printer internal page counter at end of job.""" 87 # save page counter after job 88 self.LastPageCounter = self.counterafter = self.getPrinterInternalPageCounter() 89 90 def getJobSize(self) : 91 """Returns the actual job size.""" 92 try : 93 jobsize = (self.counterafter - self.counterbefore) 94 if jobsize < 0 : 95 # Try to take care of HP printers 96 # Their internal page counter is saved to NVRAM 97 # only every 10 pages. If the printer was switched 98 # off then back on during the job, and that the 99 # counters difference is negative, we know 100 # the formula (we can't know if more than eleven 101 # pages were printed though) : 102 if jobsize > -10 : 103 jobsize += 10 104 else : 105 # here we may have got a printer being replaced 106 # DURING the job. This is HIGHLY improbable ! 107 jobsize = 0 108 except : 109 # takes care of the case where one counter (or both) was never set. 110 jobsize = 0 111 return jobsize 112 54 113 def doAccounting(self, printer, user) : 55 114 """Does print accounting and returns if the job status is ALLOW or DENY.""" … … 57 116 # Tries MAXTRIES times, sleeping two seconds each time, in case the printer is sleeping. 58 117 # This was seen with my Apple LaserWriter 16/600 PS which doesn't answer before having warmed up. 59 global MAXTRIES, TIMETOSLEEP 60 requester = openRequester(self.filter.config, self.filter.printername) 61 for i in range(MAXTRIES) : 62 try : 63 counterbeforejob = requester.getPrinterPageCounter(self.filter.printerhostname) 64 except PyKotaRequesterError, msg : 65 # can't get actual page counter, assume printer is off or warming up 66 # log the message anyway. 67 self.filter.logger.log_message("%s" % msg, "warn") 68 counterbeforejob = None 69 printerIsOff = 1 70 else : 71 # printer answered, it is on so we can exit the loop 72 printerIsOff = 0 73 break 74 time.sleep(TIMETOSLEEP) 118 counterbeforejob = self.getPrinterInternalPageCounter() 75 119 76 120 # get last job information for this printer … … 85 129 86 130 # if printer is off then we assume the correct counter value is the last one 87 if printerIsOff:131 if counterbeforejob is None : 88 132 counterbeforejob = lastpagecounter 89 133 -
pykota/trunk/pykota/accounters/stupid.py
r1144 r1180 22 22 # 23 23 # $Log$ 24 # Revision 1.6 2003/11/12 23:29:24 jalet 25 # More work on new backend. This commit may be unstable. 26 # 24 27 # Revision 1.5 2003/10/07 09:07:29 jalet 25 28 # Character encoding added to please latest version of Python … … 46 49 47 50 class Accounter(AccounterBase) : 51 def beginJob(self) : 52 """Saves the computed job size.""" 53 self.JobSize = self.computeJobSize() 54 55 # get last job information for this printer 56 if not printer.LastJob.Exists : 57 # The printer hasn't been used yet, from PyKota's point of view 58 self.LastPageCounter = 0 59 else : 60 # get last job size and page counter from Quota Storage 61 # Last lifetime page counter before actual job is 62 # last page counter + last job size 63 self.LastPageCounter = int(printer.LastJob.PrinterPageCounter or 0) + int(printer.LastJob.JobSize or 0) 64 65 def endJob(self) : 66 """Do nothing.""" 67 pass 68 69 def getJobSize(self) : 70 """Returns the actual job size.""" 71 try : 72 return self.JobSize 73 except AttributeError : 74 return 0 75 48 76 def doAccounting(self, printer, user) : 49 77 """Does print accounting by stupidly counting the 'showpage' postscript instructions in the document. … … 55 83 56 84 # get the job size 57 jobsize = self. getJobSize() * self.filter.copies85 jobsize = self.computeJobSize() * self.filter.copies 58 86 59 87 # get last job information for this printer … … 82 110 return action 83 111 84 def getJobSize(self) :112 def computeJobSize(self) : 85 113 """Computes the job size and return its value. 86 114