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

Revision 1041, 5.5 kB (checked in by jalet, 21 years ago)

Hey, it may work (edpykota --reset excepted) !

  • 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.4  2003/06/25 14:10:01  jalet
24# Hey, it may work (edpykota --reset excepted) !
25#
26# Revision 1.3  2003/05/27 23:00:21  jalet
27# Big rewrite of external accounting methods.
28# Should work well now.
29#
30# Revision 1.2  2003/05/13 13:54:20  jalet
31# Better handling of broken pipes
32#
33# Revision 1.1  2003/05/07 19:47:06  jalet
34# v1.07 Release of the Shame is out !
35#
36#
37#
38
39import sys
40import os
41import popen2
42import tempfile
43from pykota.accounter import AccounterBase, PyKotaAccounterError
44
45class Accounter(AccounterBase) :
46    def doAccounting(self, printer, user) :
47        """Deletgates the computation of the job size to an external command.
48       
49           The command must print the job size on its standard output and exit successfully.
50        """
51        # get the job size, which is real job size * number of copies.
52        jobsize = self.getJobSize() * self.filter.copies
53           
54        # get last job information for this printer
55        if not printer.LastJob.Exists :
56            # The printer hasn't been used yet, from PyKota's point of view
57            counterbeforejob = 0
58        else :   
59            # get last job size and page counter from Quota Storage
60            # Last lifetime page counter before actual job is
61            # last page counter + last job size
62            counterbeforejob = int(printer.LastJob.PrinterPageCounter or 0) + int(printer.LastJob.JobSize or 0)
63           
64        # Is the current user allowed to print at all ?
65        userpquota = self.filter.storage.getUserPQuota(user, printer)
66        action = self.filter.warnUserPQuota(userpquota)
67       
68        # update the quota for the current user on this printer, if allowed to print
69        if action == "DENY" :
70            jobsize = 0
71        else :   
72            userpquota.increasePagesUsage(jobsize)
73       
74        # adds the current job to history   
75        printer.addJobToHistory(self.filter.jobid, user, counterbeforejob, action, jobsize)
76           
77        return action
78       
79    def getJobSize(self) :   
80        """Feeds an external command with our datas to let it compute the job size, and return its value."""
81        temporary = None   
82        if self.filter.inputfile is None :   
83            infile = sys.stdin
84            # we will have to duplicate our standard input
85            temporary = tempfile.TemporaryFile()
86        else :   
87            infile = open(self.filter.inputfile, "rb")
88           
89        # launches external accounter
90        infilename = tempfile.mktemp()
91        outfilename = tempfile.mktemp()
92        errfilename = tempfile.mktemp()
93       
94        try :
95            # feed it with our data
96            fakeinput = open(infilename, "wb")
97            data = infile.read(256*1024)   
98            while data :
99                fakeinput.write(data)
100                if temporary is not None :
101                    temporary.write(data)
102                data = infile.read(256*1024)
103            fakeinput.close()
104       
105            # launches child process
106            command = "%s <%s >%s 2>%s" % (self.arguments, infilename, outfilename, errfilename)
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.