root / pykota / trunk / pykota / accounters / software.py @ 1475

Revision 1475, 3.9 kB (checked in by jalet, 20 years ago)

Code simplifications

  • 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.1  2004/05/13 13:59:30  jalet
25# Code simplifications
26#
27#
28#
29
30import sys
31import os
32import popen2
33import tempfile
34from pykota.accounter import AccounterBase, PyKotaAccounterError
35
36class Accounter(AccounterBase) :
37    def computeJobSize(self) :   
38        """Feeds an external command with our datas to let it compute the job size, and return its value."""
39        temporary = None   
40        if self.filter.inputfile is None :   
41            infile = sys.stdin
42            # we will have to duplicate our standard input
43            temporary = tempfile.TemporaryFile()
44        else :   
45            infile = open(self.filter.inputfile, "rb")
46           
47        # launches external accounter
48        # TODO : USE tempfile.mkstemp() instead ! Needs some work !
49        infilename = tempfile.mktemp()
50        outfilename = tempfile.mktemp()
51        errfilename = tempfile.mktemp()
52       
53        try :
54            # feed it with our data
55            fakeinput = open(infilename, "wb")
56            data = infile.read(256*1024)   
57            while data :
58                fakeinput.write(data)
59                if temporary is not None :
60                    temporary.write(data)
61                data = infile.read(256*1024)
62            fakeinput.close()
63       
64            # launches child process
65            command = "%s <%s >%s 2>%s" % (self.arguments, infilename, outfilename, errfilename)
66            retcode = os.system(command)
67           
68            # check exit status
69            if (os.WIFEXITED(retcode) and not os.WEXITSTATUS(retcode)) or os.stat(errfilename) :
70                # tries to extract the job size from the external accounter's
71                # standard output
72                childoutput = open(outfilename, "r")
73                try :
74                    pagecount = int(childoutput.readline().strip())
75                except (AttributeError, ValueError) :
76                    self.filter.logger.log_message(_("Unable to compute job size with external accounter %s") % self.arguments)
77                    pagecount = 0
78                childoutput.close()   
79            else :
80                self.filter.logger.log_message(_("Unable to compute job size with external accounter %s") % self.arguments)
81                pagecount = 0
82            os.remove(infilename)
83            os.remove(outfilename)
84            os.remove(errfilename)
85        except IOError, msg :   
86            # TODO : temporary files may remain on the filesystem...
87            msg = "%s : %s" % (self.arguments, msg) 
88            self.filter.logger.log_message(_("Unable to compute job size with external accounter %s") % msg)
89            pagecount = 0
90           
91        if temporary is not None :   
92            # this is a copy of our previous standard input
93            # flush, then rewind
94            temporary.flush()
95            temporary.seek(0, 0)
96            # our temporary file will be used later if the
97            # job is allowed.
98            self.filter.inputfile = temporary
99        else :
100            infile.close()
101        return pagecount   
102           
Note: See TracBrowser for help on using the browser.