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

Revision 1123, 5.8 kB (checked in by jalet, 21 years ago)

Problem with Python 2.3 fixed : a bug of me.

  • 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.6  2003/09/04 07:57:18  jalet
24# Problem with Python 2.3 fixed : a bug of me.
25#
26# Revision 1.5  2003/07/07 11:49:24  jalet
27# Lots of small fixes with the help of PyChecker
28#
29# Revision 1.4  2003/06/25 14:10:01  jalet
30# Hey, it may work (edpykota --reset excepted) !
31#
32# Revision 1.3  2003/05/27 23:00:21  jalet
33# Big rewrite of external accounting methods.
34# Should work well now.
35#
36# Revision 1.2  2003/05/13 13:54:20  jalet
37# Better handling of broken pipes
38#
39# Revision 1.1  2003/05/07 19:47:06  jalet
40# v1.07 Release of the Shame is out !
41#
42#
43#
44
45import sys
46import os
47import popen2
48import tempfile
49from pykota.accounter import AccounterBase, PyKotaAccounterError
50
51class Accounter(AccounterBase) :
52    def doAccounting(self, printer, user) :
53        """Deletgates the computation of the job size to an external command.
54       
55           The command must print the job size on its standard output and exit successfully.
56        """
57        # get the job size, which is real job size * number of copies.
58        jobsize = self.getJobSize() * self.filter.copies
59           
60        # get last job information for this printer
61        if not printer.LastJob.Exists :
62            # The printer hasn't been used yet, from PyKota's point of view
63            counterbeforejob = 0
64        else :   
65            # get last job size and page counter from Quota Storage
66            # Last lifetime page counter before actual job is
67            # last page counter + last job size
68            counterbeforejob = int(printer.LastJob.PrinterPageCounter or 0) + int(printer.LastJob.JobSize or 0)
69           
70        # Is the current user allowed to print at all ?
71        userpquota = self.filter.storage.getUserPQuota(user, printer)
72        action = self.filter.warnUserPQuota(userpquota)
73       
74        # update the quota for the current user on this printer, if allowed to print
75        if action == "DENY" :
76            jobsize = 0
77        else :   
78            userpquota.increasePagesUsage(jobsize)
79       
80        # adds the current job to history   
81        printer.addJobToHistory(self.filter.jobid, user, counterbeforejob, action, jobsize)
82           
83        return action
84       
85    def getJobSize(self) :   
86        """Feeds an external command with our datas to let it compute the job size, and return its value."""
87        temporary = None   
88        if self.filter.inputfile is None :   
89            infile = sys.stdin
90            # we will have to duplicate our standard input
91            temporary = tempfile.TemporaryFile()
92        else :   
93            infile = open(self.filter.inputfile, "rb")
94           
95        # launches external accounter
96        # TODO : USE tempfile.mkstemp() instead ! Needs some work !
97        infilename = tempfile.mktemp()
98        outfilename = tempfile.mktemp()
99        errfilename = tempfile.mktemp()
100       
101        try :
102            # feed it with our data
103            fakeinput = open(infilename, "wb")
104            data = infile.read(256*1024)   
105            while data :
106                fakeinput.write(data)
107                if temporary is not None :
108                    temporary.write(data)
109                data = infile.read(256*1024)
110            fakeinput.close()
111       
112            # launches child process
113            command = "%s <%s >%s 2>%s" % (self.arguments, infilename, outfilename, errfilename)
114            retcode = os.system(command)
115           
116            # check exit status
117            if (os.WIFEXITED(retcode) and not os.WEXITSTATUS(retcode)) or os.stat(errfilename) :
118                # tries to extract the job size from the external accounter's
119                # standard output
120                childoutput = open(outfilename, "r")
121                try :
122                    pagecount = int(childoutput.readline().strip())
123                except (AttributeError, ValueError) :
124                    self.filter.logger.log_message(_("Unable to compute job size with external accounter %s") % self.arguments)
125                    pagecount = 0
126                childoutput.close()   
127            else :
128                self.filter.logger.log_message(_("Unable to compute job size with external accounter %s") % self.arguments)
129                pagecount = 0
130            os.remove(infilename)
131            os.remove(outfilename)
132            os.remove(errfilename)
133        except IOError, msg :   
134            # TODO : temporary files may remain on the filesystem...
135            msg = "%s : %s" % (self.arguments, msg) 
136            self.filter.logger.log_message(_("Unable to compute job size with external accounter %s") % msg)
137            pagecount = 0
138           
139        if temporary is not None :   
140            # this is a copy of our previous standard input
141            # flush, then rewind
142            temporary.flush()
143            temporary.seek(0, 0)
144            # our temporary file will be used later if the
145            # job is allowed.
146            self.filter.inputfile = temporary
147        else :
148            infile.close()
149           
150        return pagecount   
151           
Note: See TracBrowser for help on using the browser.