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