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

Revision 1240, 4.8 kB (checked in by uid67467, 20 years ago)

Should be ok now.

  • 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.12  2003/12/27 16:49:25  uid67467
25# Should be ok now.
26#
27# Revision 1.10  2003/11/23 19:01:36  jalet
28# Job price added to history
29#
30# Revision 1.9  2003/11/21 14:28:45  jalet
31# More complete job history.
32#
33# Revision 1.8  2003/11/12 23:29:24  jalet
34# More work on new backend. This commit may be unstable.
35#
36# Revision 1.7  2003/10/07 09:07:28  jalet
37# Character encoding added to please latest version of Python
38#
39# Revision 1.6  2003/09/04 07:57:18  jalet
40# Problem with Python 2.3 fixed : a bug of me.
41#
42# Revision 1.5  2003/07/07 11:49:24  jalet
43# Lots of small fixes with the help of PyChecker
44#
45# Revision 1.4  2003/06/25 14:10:01  jalet
46# Hey, it may work (edpykota --reset excepted) !
47#
48# Revision 1.3  2003/05/27 23:00:21  jalet
49# Big rewrite of external accounting methods.
50# Should work well now.
51#
52# Revision 1.2  2003/05/13 13:54:20  jalet
53# Better handling of broken pipes
54#
55# Revision 1.1  2003/05/07 19:47:06  jalet
56# v1.07 Release of the Shame is out !
57#
58#
59#
60
61import sys
62import os
63import popen2
64import tempfile
65from pykota.accounter import AccounterBase, PyKotaAccounterError
66
67class Accounter(AccounterBase) :
68    def computeJobSize(self) :   
69        """Feeds an external command with our datas to let it compute the job size, and return its value."""
70        temporary = None   
71        if self.filter.inputfile is None :   
72            infile = sys.stdin
73            # we will have to duplicate our standard input
74            temporary = tempfile.TemporaryFile()
75        else :   
76            infile = open(self.filter.inputfile, "rb")
77           
78        # launches external accounter
79        # TODO : USE tempfile.mkstemp() instead ! Needs some work !
80        infilename = tempfile.mktemp()
81        outfilename = tempfile.mktemp()
82        errfilename = tempfile.mktemp()
83       
84        try :
85            # feed it with our data
86            fakeinput = open(infilename, "wb")
87            data = infile.read(256*1024)   
88            while data :
89                fakeinput.write(data)
90                if temporary is not None :
91                    temporary.write(data)
92                data = infile.read(256*1024)
93            fakeinput.close()
94       
95            # launches child process
96            command = "%s <%s >%s 2>%s" % (self.arguments, infilename, outfilename, errfilename)
97            retcode = os.system(command)
98           
99            # check exit status
100            if (os.WIFEXITED(retcode) and not os.WEXITSTATUS(retcode)) or os.stat(errfilename) :
101                # tries to extract the job size from the external accounter's
102                # standard output
103                childoutput = open(outfilename, "r")
104                try :
105                    pagecount = int(childoutput.readline().strip())
106                except (AttributeError, ValueError) :
107                    self.filter.logger.log_message(_("Unable to compute job size with external accounter %s") % self.arguments)
108                    pagecount = 0
109                childoutput.close()   
110            else :
111                self.filter.logger.log_message(_("Unable to compute job size with external accounter %s") % self.arguments)
112                pagecount = 0
113            os.remove(infilename)
114            os.remove(outfilename)
115            os.remove(errfilename)
116        except IOError, msg :   
117            # TODO : temporary files may remain on the filesystem...
118            msg = "%s : %s" % (self.arguments, msg) 
119            self.filter.logger.log_message(_("Unable to compute job size with external accounter %s") % msg)
120            pagecount = 0
121           
122        if temporary is not None :   
123            # this is a copy of our previous standard input
124            # flush, then rewind
125            temporary.flush()
126            temporary.seek(0, 0)
127            # our temporary file will be used later if the
128            # job is allowed.
129            self.filter.inputfile = temporary
130        else :
131            infile.close()
132        return pagecount   
133           
Note: See TracBrowser for help on using the browser.