[695] | 1 | #! /usr/bin/env python |
---|
| 2 | |
---|
| 3 | # PyKota accounting filter |
---|
| 4 | # |
---|
| 5 | # PyKota - Print Quotas for CUPS |
---|
| 6 | # |
---|
| 7 | # (c) 2003 Jerome Alet <alet@librelogiciel.com> |
---|
| 8 | # You're welcome to redistribute this software under the |
---|
| 9 | # terms of the GNU General Public Licence version 2.0 |
---|
| 10 | # or, at your option, any higher version. |
---|
| 11 | # |
---|
| 12 | # You can read the complete GNU GPL in the file COPYING |
---|
| 13 | # which should come along with this software, or visit |
---|
| 14 | # the Free Software Foundation's WEB site http://www.fsf.org |
---|
| 15 | # |
---|
| 16 | # $Id$ |
---|
| 17 | # |
---|
| 18 | # $Log$ |
---|
[704] | 19 | # Revision 1.5 2003/02/05 22:45:25 jalet |
---|
| 20 | # Forgotten import |
---|
| 21 | # |
---|
[703] | 22 | # Revision 1.4 2003/02/05 22:42:51 jalet |
---|
| 23 | # Typo |
---|
| 24 | # |
---|
[701] | 25 | # Revision 1.3 2003/02/05 22:38:39 jalet |
---|
| 26 | # Typo |
---|
| 27 | # |
---|
[699] | 28 | # Revision 1.2 2003/02/05 22:16:20 jalet |
---|
| 29 | # DEVICE_URI is undefined outside of CUPS, i.e. for normal command line tools |
---|
| 30 | # |
---|
[695] | 31 | # Revision 1.1 2003/02/05 21:28:17 jalet |
---|
| 32 | # Initial import into CVS |
---|
| 33 | # |
---|
| 34 | # |
---|
| 35 | # |
---|
| 36 | |
---|
| 37 | import sys |
---|
[704] | 38 | import os |
---|
[695] | 39 | |
---|
| 40 | from pykota.tool import PyKotaTool, PyKotaToolError |
---|
| 41 | from pykota import requester |
---|
| 42 | |
---|
[703] | 43 | class PyKotaFilter(PyKotaTool) : |
---|
[695] | 44 | """Class for the PyKota filter.""" |
---|
[701] | 45 | def __init__(self, username) : |
---|
[695] | 46 | PyKotaTool.__init__(self, isfilter=1) |
---|
| 47 | self.username = username |
---|
| 48 | self.requester = requester.openRequester(self.config, self.printername) |
---|
[699] | 49 | self.printerhostname = self.getPrinterHostname() |
---|
[695] | 50 | |
---|
[699] | 51 | def getPrinterHostname(self) : |
---|
| 52 | """Returns the printer hostname.""" |
---|
| 53 | device_uri = os.environ.get("DEVICE_URI", "") |
---|
| 54 | # TODO : check this for more complex urls than ipp://myprinter.dot.com:631/printers/lp |
---|
| 55 | try : |
---|
| 56 | (backend, destination) = device_uri.split(":", 1) |
---|
| 57 | except ValueError : |
---|
| 58 | raise PyKotaToolError, "Invalid DEVICE_URI : %s\n" % device_uri |
---|
| 59 | while destination.startswith("/") : |
---|
| 60 | destination = destination[1:] |
---|
| 61 | return destination.split("/")[0].split(":")[0] |
---|
| 62 | |
---|
[695] | 63 | def filterInput(self, inputfile) : |
---|
| 64 | """Transparent filter.""" |
---|
| 65 | mustclose = 0 |
---|
| 66 | if inputfile is not None : |
---|
| 67 | infile = open(inputfile, "rb") |
---|
| 68 | mustclose = 1 |
---|
| 69 | else : |
---|
| 70 | infile = sys.stdin |
---|
| 71 | data = infile.read(65536) |
---|
| 72 | while data : |
---|
| 73 | sys.stdout.write(data) |
---|
| 74 | data = infile.read(65536) |
---|
| 75 | if mustclose : |
---|
| 76 | infile.close() |
---|
| 77 | |
---|
| 78 | def main() : |
---|
| 79 | """Do it, and do it right !""" |
---|
| 80 | # |
---|
| 81 | # This is a CUPS filter, so we should act and die like a CUPS filter when needed |
---|
| 82 | narg = len(sys.argv) |
---|
| 83 | if narg not in (6, 7) : |
---|
| 84 | sys.stderr.write("ERROR: %s job-id user title copies options [file]\n" % sys.argv[0]) |
---|
| 85 | return 1 |
---|
| 86 | elif narg == 7 : |
---|
| 87 | # input file |
---|
| 88 | inputfile = sys.argv[6] |
---|
| 89 | else : |
---|
| 90 | # stdin |
---|
| 91 | inputfile = None |
---|
| 92 | |
---|
| 93 | # |
---|
| 94 | # According to CUPS documentation, the username is the third command line argument |
---|
| 95 | username = sys.argv[2].strip() |
---|
| 96 | |
---|
| 97 | # Initializes the current tool |
---|
| 98 | tool = PyKotaFilter(username) |
---|
| 99 | |
---|
| 100 | # Get the page counter directly from the printer itself |
---|
| 101 | counterbeforejob = tool.requester.getPrinterPageCounter(tool.printerhostname) # TODO use printername instead, make them match from CUPS' config files |
---|
| 102 | |
---|
| 103 | # Get the last page counter and last username from the Quota Storage backend |
---|
| 104 | pgc = tool.storage.getPrinterPageCounter(tool.printername) |
---|
| 105 | if pgc is None : |
---|
| 106 | # The printer is unknown from the Quota Storage perspective |
---|
| 107 | # we let the job pass through, but log a warning message |
---|
| 108 | tool.logger.log_message("Printer %s not registered in the PyKota system" % tool.printername, "warn") |
---|
| 109 | else : |
---|
| 110 | (lastpagecounter, lastusername) = (pgc["pagecounter"], pgc["lastusername"]) |
---|
| 111 | |
---|
| 112 | # Update the last page counter and last username in the Quota Storage backend |
---|
| 113 | # set them to current user and |
---|
| 114 | tool.storage.updatePrinterPageCounter(tool.printername, username, counterbeforejob) # TODO : allow or deny users not in quota system, and die cleanly if needed |
---|
| 115 | |
---|
| 116 | # Is the current user allowed to print at all ? |
---|
| 117 | action = tool.warnQuotaPrinter(username) |
---|
| 118 | if action == "DENY" : |
---|
| 119 | # No, just die cleanly |
---|
| 120 | return 1 |
---|
| 121 | |
---|
| 122 | # Yes |
---|
| 123 | if (lastpagecounter is None) or (lastusername is None) : |
---|
| 124 | lastusername = username |
---|
| 125 | lastpagecounter = counterbeforejob |
---|
| 126 | jobsize = (counterbeforejob - lastpagecounter) |
---|
| 127 | if jobsize >= 0: |
---|
| 128 | tool.storage.updateUserPQuota(lastusername, tool.printername, jobsize) |
---|
| 129 | tool.warnQuotaPrinter(lastusername) |
---|
| 130 | else : |
---|
| 131 | tool.logger.log_message("Error in page count value %i for user %s on printer %s" % (jobsize, tool.printername, lastusername), "error") |
---|
| 132 | |
---|
| 133 | # pass the job untouched to the underlying layer |
---|
| 134 | tool.filterInput(inputfile) |
---|
| 135 | |
---|
| 136 | return 0 |
---|
| 137 | |
---|
| 138 | if __name__ == "__main__" : |
---|
| 139 | sys.exit(main() or 0) |
---|