Changeset 1483 for pykota/trunk/pykota
- Timestamp:
- 05/18/04 16:49:34 (20 years ago)
- Location:
- pykota/trunk/pykota
- Files:
-
- 2 removed
- 7 modified
Legend:
- Unmodified
- Added
- Removed
-
pykota/trunk/pykota/accounter.py
r1285 r1483 22 22 # 23 23 # $Log$ 24 # Revision 1.14 2004/05/18 14:49:19 jalet 25 # Big code changes to completely remove the need for "requester" directives, 26 # jsut use "hardware(... your previous requester directive's content ...)" 27 # 24 28 # Revision 1.13 2004/01/12 22:43:40 jalet 25 29 # New formula to compute a job's price … … 63 67 64 68 import sys 69 from pykota import pdlanalyzer 65 70 66 71 class PyKotaAccounterError(Exception): … … 80 85 self.arguments = arguments 81 86 self.isDelayed = 0 # Accounting is immediate by default 87 self.firstPassSize = None 88 89 def getSoftwareJobSize(self) : 90 """Pre-computes the job's size with a software method.""" 91 if self.filter.preserveinputfile is None : 92 raise PyKotaAccounterError, "Only supports raw jobs for now.""" 93 else : 94 fname = self.filter.preserveinputfile 95 parser = pdfanalyzer.PDLAnalyzer(fname) 96 try : 97 jobsize = parser.getJobSize() 98 except TypeError, msg : 99 raise PyKotaAccounterError, msg 100 else : 101 self.firstPassSize = jobsize 82 102 83 103 def getLastPageCounter(self) : 84 104 """Returns last internal page counter value (possibly faked).""" 85 105 try : 86 return self.LastPageCounter 106 return self.LastPageCounter or 0 87 107 except : 88 108 return 0 -
pykota/trunk/pykota/accounters/hardware.py
r1475 r1483 22 22 # 23 23 # $Log$ 24 # Revision 1.2 2004/05/18 14:49:22 jalet 25 # Big code changes to completely remove the need for "requester" directives, 26 # jsut use "hardware(... your previous requester directive's content ...)" 27 # 24 28 # Revision 1.1 2004/05/13 13:59:30 jalet 25 29 # Code simplifications … … 30 34 import sys 31 35 import os 36 import popen2 32 37 from pykota.accounter import AccounterBase, PyKotaAccounterError 33 from pykota.requester import openRequester, PyKotaRequesterError34 38 35 39 class Accounter(AccounterBase) : … … 37 41 """Initializes querying accounter.""" 38 42 AccounterBase.__init__(self, kotabackend, arguments) 39 self.requester = openRequester(kotabackend, kotabackend.printername)40 43 self.isDelayed = 1 # With the pykota filter, accounting is delayed by one job 41 44 … … 44 47 self.filter.logdebug("Reading printer's internal page counter...") 45 48 try : 46 counter = self. requester.getPrinterPageCounter(self.filter.printerhostname)47 except PyKota RequesterError, msg :49 counter = self.askPrinterPageCounter(self.filter.printerhostname) 50 except PyKotaAccounterError, msg : 48 51 # can't get actual page counter, assume printer is off or warming up 49 52 # log the message anyway. … … 97 100 userpquota.Printer.addJobToHistory(self.filter.jobid, userpquota.User, counterbeforejob, action, filename=self.filter.preserveinputfile, title=self.filter.title, copies=self.filter.copies, options=self.filter.options) 98 101 return action 102 103 def askPrinterPageCounter(self, printer) : 104 """Returns the page counter from the printer via an external command. 105 106 The external command must report the life time page number of the printer on stdout. 107 """ 108 commandline = self.arguments.strip() % locals() 109 if printer is None : 110 raise PyKotaAccounterError, _("Unknown printer address in HARDWARE(%s) for printer %s") % (commandline, self.filter.printername) 111 error = 1 112 pagecounter = None 113 child = popen2.Popen4(commandline) 114 try : 115 pagecounter = int(child.fromchild.readline().strip()) 116 except ValueError : 117 pass 118 except IOError : 119 # we were interrupted by a signal, certainely a SIGTERM 120 # caused by the user cancelling the current job 121 try : 122 os.kill(child.pid, signal.SIGTERM) 123 except : 124 pass # already killed ? 125 self.filter.logger.log_message(_("SIGTERM was sent to hardware accounter %s (pid: %s)") % (commandline, child.pid), "info") 126 else : 127 error = 0 128 child.fromchild.close() 129 child.tochild.close() 130 status = child.wait() 131 if (not error) and os.WIFEXITED(status) and (not os.WEXITSTATUS(status)) : 132 return pagecounter 133 else : 134 raise PyKotaAccounterError, _("Unable to query printer %s via HARDWARE(%s)") % (printer, commandline) 99 135 -
pykota/trunk/pykota/accounters/software.py
r1475 r1483 22 22 # 23 23 # $Log$ 24 # Revision 1.2 2004/05/18 14:49:23 jalet 25 # Big code changes to completely remove the need for "requester" directives, 26 # jsut use "hardware(... your previous requester directive's content ...)" 27 # 24 28 # Revision 1.1 2004/05/13 13:59:30 jalet 25 29 # Code simplifications … … 45 49 infile = open(self.filter.inputfile, "rb") 46 50 47 # launches externalaccounter51 # launches software accounter 48 52 # TODO : USE tempfile.mkstemp() instead ! Needs some work ! 49 53 infilename = tempfile.mktemp() … … 68 72 # check exit status 69 73 if (os.WIFEXITED(retcode) and not os.WEXITSTATUS(retcode)) or os.stat(errfilename) : 70 # tries to extract the job size from the externalaccounter's74 # tries to extract the job size from the software accounter's 71 75 # standard output 72 76 childoutput = open(outfilename, "r") … … 74 78 pagecount = int(childoutput.readline().strip()) 75 79 except (AttributeError, ValueError) : 76 self.filter.logger.log_message(_("Unable to compute job size with externalaccounter %s") % self.arguments)80 self.filter.logger.log_message(_("Unable to compute job size with accounter %s") % self.arguments) 77 81 pagecount = 0 78 82 childoutput.close() 79 83 else : 80 self.filter.logger.log_message(_("Unable to compute job size with externalaccounter %s") % self.arguments)84 self.filter.logger.log_message(_("Unable to compute job size with accounter %s") % self.arguments) 81 85 pagecount = 0 82 86 os.remove(infilename) … … 86 90 # TODO : temporary files may remain on the filesystem... 87 91 msg = "%s : %s" % (self.arguments, msg) 88 self.filter.logger.log_message(_("Unable to compute job size with externalaccounter %s") % msg)92 self.filter.logger.log_message(_("Unable to compute job size with accounter %s") % msg) 89 93 pagecount = 0 90 94 -
pykota/trunk/pykota/config.py
r1475 r1483 22 22 # 23 23 # $Log$ 24 # Revision 1.47 2004/05/18 14:49:20 jalet 25 # Big code changes to completely remove the need for "requester" directives, 26 # jsut use "hardware(... your previous requester directive's content ...)" 27 # 24 28 # Revision 1.46 2004/05/13 13:59:28 jalet 25 29 # Code simplifications … … 311 315 validaccounters = [ "hardware", "software" ] 312 316 fullaccounter = self.getPrinterOption(printername, "accounter").strip() 313 if fullaccounter.lower().startswith("software") : 317 flower = fullaccounter.lower() 318 if flower.startswith("software") or flower.startswith("hardware") : 314 319 try : 315 320 (accounter, args) = [x.strip() for x in fullaccounter.split('(', 1)] 316 321 except ValueError : 317 raise PyKotaConfigError, _("Invalid externalaccounter %s for printer %s") % (fullaccounter, printername)322 raise PyKotaConfigError, _("Invalid accounter %s for printer %s") % (fullaccounter, printername) 318 323 if args.endswith(')') : 319 324 args = args[:-1] 320 325 if not args : 321 raise PyKotaConfigError, _("Invalid externalaccounter %s for printer %s") % (fullaccounter, printername)326 raise PyKotaConfigError, _("Invalid accounter %s for printer %s") % (fullaccounter, printername) 322 327 return (accounter.lower(), args) 323 el if fullaccounter.lower() not in validaccounters:328 else : 324 329 raise PyKotaConfigError, _("Option accounter in section %s only supports values in %s") % (printername, str(validaccounters)) 325 else :326 return (fullaccounter.lower(), None)327 330 328 331 def getPreHook(self, printername) : … … 340 343 return # No command to launch in the post-hook 341 344 342 def getRequesterBackend(self, printername) :343 """Returns the requester backend to use for a given printer, with its arguments."""344 try :345 fullrequester = self.getPrinterOption(printername, "requester")346 except PyKotaConfigError :347 # No requester defined, maybe it is not needed if accounting method348 # is not set to 'hardware', but if we are called, then the accounting349 # method really IS 'hardware', and so there's a big problem.350 raise PyKotaConfigError, _("Option requester for printer %s was not set") % printername351 else :352 try :353 (requester, args) = [x.strip() for x in fullrequester.split('(', 1)]354 except ValueError :355 raise PyKotaConfigError, _("Invalid requester %s for printer %s") % (fullrequester, printername)356 if args.endswith(')') :357 args = args[:-1]358 if not args :359 raise PyKotaConfigError, _("Invalid requester %s for printer %s") % (fullrequester, printername)360 validrequesters = [ "external" ]361 requester = requester.lower()362 if requester not in validrequesters :363 raise PyKotaConfigError, _("Option requester for printer %s only supports values in %s") % (printername, str(validrequesters))364 return (requester, args)365 366 345 def getPrinterPolicy(self, printername) : 367 346 """Returns the default policy for the current printer.""" -
pykota/trunk/pykota/Makefile.am
r1482 r1483 7 7 logger.py \ 8 8 reporter.py \ 9 requester.py \10 9 storage.py \ 11 10 tool.py \ … … 13 12 version.py 14 13 15 SUBDIRS = accounters loggers reporters requestersstorages14 SUBDIRS = accounters loggers reporters storages 16 15 -
pykota/trunk/pykota/tool.py
r1481 r1483 22 22 # 23 23 # $Log$ 24 # Revision 1.88 2004/05/18 14:49:20 jalet 25 # Big code changes to completely remove the need for "requester" directives, 26 # jsut use "hardware(... your previous requester directive's content ...)" 27 # 24 28 # Revision 1.87 2004/05/17 19:14:59 jalet 25 29 # Now catches SIGPIPE and SIGCHLD … … 764 768 self.options, \ 765 769 self.originalbackend) = self.extractInfoFromCupsOrLprng() 766 self.username = self.username or 'root' 770 self.username = self.username or 'root' # when printing test page from CUPS web interface, username is empty 767 771 if self.config.getUserNameToLower() : 768 772 self.username = self.username.lower() -
pykota/trunk/pykota/version.py
r1478 r1483 22 22 # 23 23 24 __version__ = "1.19alpha 9_unofficial"24 __version__ = "1.19alpha10_unofficial" 25 25 26 26 __doc__ = """PyKota : a complete Printing Quota Solution for CUPS and LPRng."""