root / pykota / trunk / bin / pykota @ 701

Revision 701, 4.7 kB (checked in by jalet, 21 years ago)

Typo

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