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

Revision 1144, 5.9 kB (checked in by jalet, 21 years ago)

Character encoding added to please latest version of Python

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