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

Revision 991, 4.5 kB (checked in by jalet, 21 years ago)

v1.07 Release of the Shame is out !

  • 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.1  2003/05/07 19:47:06  jalet
24# v1.07 Release of the Shame is out !
25#
26#
27#
28
29import sys
30import os
31import popen2
32import tempfile
33from pykota.accounter import AccounterBase, PyKotaAccounterError
34
35class Accounter(AccounterBase) :
36    def doAccounting(self, printerid, userid) :
37        """Deletgates the computation of the job size to an external command.
38       
39           The command must print the job size on its standard output and exit successfully.
40        """
41        # get the job size   
42        jobsize = self.getJobSize()
43           
44        # get last job information for this printer
45        pgc = self.filter.storage.getPrinterPageCounter(printerid)   
46        if pgc is None :
47            # The printer hasn't been used yet, from PyKota's point of view
48            counterbeforejob = 0
49        else :   
50            # get last job size and page counter from Quota Storage
51            # Last lifetime page counter before actual job is
52            # last page counter + last job size
53            counterbeforejob = (pgc["pagecounter"] or 0) + (pgc["jobsize"] or 0)
54           
55        # Is the current user allowed to print at all ?
56        action = self.filter.warnUserPQuota(self.filter.username, self.filter.printername)
57       
58        # update the quota for the current user on this printer, if allowed to print
59        if action == "DENY" :
60            jobsize = 0
61        else :   
62            self.filter.storage.updateUserPQuota(userid, printerid, jobsize)
63       
64        # adds the current job to history   
65        self.filter.storage.addJobToHistory(self.filter.jobid, self.filter.storage.getUserId(self.filter.username), printerid, counterbeforejob, action, jobsize)
66           
67        return action
68       
69    def getJobSize(self) :   
70        """Feeds an external command with our datas to let it compute the job size, and return its value."""
71        temporary = None   
72        if self.filter.inputfile is None :   
73            infile = sys.stdin
74            # we will have to duplicate our standard input
75            temporary = tempfile.TemporaryFile()
76        else :   
77            infile = open(self.filter.inputfile, "rb")
78           
79        # launches external accounter
80        process = popen2.Popen3("%s" % self.arguments)
81       
82        # feed it with our data
83        data = infile.read(256*1024)   
84        while data :
85            process.tochild.write(data)
86            temporary.write(data)
87            data = infile.read(256*1024)
88        process.tochild.close()
89       
90        # wait for child process to exit (or die)
91        retcode = process.wait()
92       
93        # check exit status
94        if os.WIFEXITED(retcode) and not os.WEXITSTATUS(retcode) :
95            # tries to extract the job size from the external accounter's
96            # standard output
97            try :
98                pagecount = int(process.fromchild.readline().strip())
99            except (AttributeError, ValueError) :
100                self.filter.logger.log_message(_("Unable to compute job size with external accounter %s") % self.arguments)
101                pagecount = 0
102        else :
103            self.filter.logger.log_message(_("Unable to compute job size with external accounter %s") % self.arguments)
104            pagecount = 0
105        process.fromchild.close()   
106           
107        if temporary is not None :   
108            # this is a copy of our previous standard input
109            # flush, then rewind
110            temporary.flush()
111            temporary.seek(0, 0)
112            # our temporary file will be used later if the
113            # job is allowed.
114            self.filter.inputfile = temporary
115        else :
116            infile.close()
117           
118        return pagecount   
119           
Note: See TracBrowser for help on using the browser.