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

Revision 1257, 4.8 kB (checked in by jalet, 20 years ago)

Copyright year changed.

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