root / pykota / trunk / pykota / accounters / external.py @ 1000

Revision 1000, 5.6 kB (checked in by jalet, 21 years ago)

Big rewrite of external accounting methods.
Should work well now.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1# PyKota
2#
3# PyKota - Print Quotas for CUPS and LPRng
4#
5# (c) 2003 Jerome Alet <alet@librelogiciel.com>
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
18# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
19#
20# $Id$
21#
22# $Log$
23# Revision 1.3  2003/05/27 23:00:21  jalet
24# Big rewrite of external accounting methods.
25# Should work well now.
26#
27# Revision 1.2  2003/05/13 13:54:20  jalet
28# Better handling of broken pipes
29#
30# Revision 1.1  2003/05/07 19:47:06  jalet
31# v1.07 Release of the Shame is out !
32#
33#
34#
35
36import sys
37import os
38import popen2
39import tempfile
40from pykota.accounter import AccounterBase, PyKotaAccounterError
41
42class Accounter(AccounterBase) :
43    def doAccounting(self, printerid, userid) :
44        """Deletgates the computation of the job size to an external command.
45       
46           The command must print the job size on its standard output and exit successfully.
47        """
48        # get the job size, which is real job size * number of copies.
49        jobsize = self.getJobSize() * self.filter.copies
50           
51        # get last job information for this printer
52        pgc = self.filter.storage.getPrinterPageCounter(printerid)   
53        if pgc is None :
54            # The printer hasn't been used yet, from PyKota's point of view
55            counterbeforejob = 0
56        else :   
57            # get last job size and page counter from Quota Storage
58            # Last lifetime page counter before actual job is
59            # last page counter + last job size
60            counterbeforejob = (pgc["pagecounter"] or 0) + (pgc["jobsize"] or 0)
61           
62        # Is the current user allowed to print at all ?
63        action = self.filter.warnUserPQuota(self.filter.username, self.filter.printername)
64       
65        # update the quota for the current user on this printer, if allowed to print
66        if action == "DENY" :
67            jobsize = 0
68        else :   
69            self.filter.storage.updateUserPQuota(userid, printerid, jobsize)
70       
71        # adds the current job to history   
72        self.filter.storage.addJobToHistory(self.filter.jobid, self.filter.storage.getUserId(self.filter.username), printerid, counterbeforejob, action, jobsize)
73           
74        return action
75       
76    def getJobSize(self) :   
77        """Feeds an external command with our datas to let it compute the job size, and return its value."""
78        temporary = None   
79        if self.filter.inputfile is None :   
80            infile = sys.stdin
81            # we will have to duplicate our standard input
82            temporary = tempfile.TemporaryFile()
83        else :   
84            infile = open(self.filter.inputfile, "rb")
85           
86        # launches external accounter
87        infilename = tempfile.mktemp()
88        outfilename = tempfile.mktemp()
89        errfilename = tempfile.mktemp()
90       
91        try :
92            # feed it with our data
93            fakeinput = open(infilename, "wb")
94            data = infile.read(256*1024)   
95            while data :
96                fakeinput.write(data)
97                if temporary is not None :
98                    temporary.write(data)
99                data = infile.read(256*1024)
100            fakeinput.close()
101       
102            # launches child process
103            command = "%s <%s >%s 2>%s" % (self.arguments, infilename, outfilename, errfilename)
104            o = open("/tmp/comm", "w")
105            o.write("%s\n" % command)
106            o.close()
107            retcode = os.system(command)
108           
109            # check exit status
110            if (os.WIFEXITED(retcode) and not os.WEXITSTATUS(retcode)) or os.stat(errfilename) :
111                # tries to extract the job size from the external accounter's
112                # standard output
113                childoutput = open(outfilename, "r")
114                try :
115                    pagecount = int(childoutput.readline().strip())
116                except (AttributeError, ValueError) :
117                    self.filter.logger.log_message(_("Unable to compute job size with external accounter %s") % self.arguments)
118                    pagecount = 0
119                childoutput.close()   
120            else :
121                self.filter.logger.log_message(_("Unable to compute job size with external accounter %s") % self.arguments)
122                pagecount = 0
123            os.remove(infilename)
124            os.remove(outfilename)
125            os.remove(errfilename)
126        except IOError, msg :   
127            # TODO : temporary files may remain on the filesystem...
128            msg = "%s : %s" % (self.arguments, msg) 
129            self.filter.logger.log_message(_("Unable to compute job size with external accounter %s") % msg)
130            pagecount = 0
131           
132        if temporary is not None :   
133            # this is a copy of our previous standard input
134            # flush, then rewind
135            temporary.flush()
136            temporary.seek(0, 0)
137            # our temporary file will be used later if the
138            # job is allowed.
139            self.filter.inputfile = temporary
140        else :
141            infile.close()
142           
143        return pagecount   
144           
Note: See TracBrowser for help on using the browser.