root / pykota / trunk / bin / pykota @ 740

Revision 740, 5.1 kB (checked in by jalet, 21 years ago)

Avoid a possible future name clash

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