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

Revision 2147, 3.8 kB (checked in by jerome, 19 years ago)

Removed all references to $Log$

  • 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#
24
25import os
26import popen2
27from pykota.accounter import AccounterBase, PyKotaAccounterError
28
29class Accounter(AccounterBase) :
30    def computeJobSize(self) :   
31        """Feeds an external command with our datas to let it compute the job size, and return its value."""
32        self.filter.printInfo(_("Launching SOFTWARE(%s)...") % self.arguments)
33        if not self.arguments :
34            pagecounter = self.filter.softwareJobSize   # Optimize : already computed !
35            self.filter.logdebug("Internal software accounter said job is %s pages long." % repr(pagecounter))
36        else :
37            MEGABYTE = 1024*1024
38            self.filter.jobdatastream.seek(0)
39            child = popen2.Popen4(self.arguments)
40            try :
41                data = self.filter.jobdatastream.read(MEGABYTE)   
42                while data :
43                    child.tochild.write(data)
44                    data = self.filter.jobdatastream.read(MEGABYTE)
45                child.tochild.flush()
46                child.tochild.close()   
47            except (IOError, OSError), msg :   
48                msg = "%s : %s" % (self.arguments, msg) 
49                self.filter.printInfo(_("Unable to compute job size with accounter %s") % msg)
50           
51            pagecounter = None
52            try :
53                answer = child.fromchild.read()
54            except (IOError, OSError), msg :   
55                msg = "%s : %s" % (self.arguments, msg) 
56                self.filter.printInfo(_("Unable to compute job size with accounter %s") % msg)
57            else :   
58                lines = [l.strip() for l in answer.split("\n")]
59                for i in range(len(lines)) : 
60                    try :
61                        pagecounter = int(lines[i])
62                    except (AttributeError, ValueError) :
63                        self.filter.printInfo(_("Line [%s] skipped in accounter's output. Trying again...") % lines[i])
64                    else :   
65                        break
66            child.fromchild.close()
67           
68            try :
69                status = child.wait()
70            except OSError, msg :   
71                self.filter.printInfo(_("Problem while waiting for software accounter pid %s to exit : %s") % (child.pid, msg))
72            else :   
73                if os.WIFEXITED(status) :
74                    status = os.WEXITSTATUS(status)
75                self.filter.printInfo(_("Software accounter %s exit code is %s") % (self.arguments, str(status)))
76               
77            if pagecounter is None :   
78                message = _("Unable to compute job size with accounter %s") % self.arguments
79                if self.onerror == "CONTINUE" :
80                    self.filter.printInfo(message, "error")
81                else :
82                    raise PyKotaAccounterError, message
83            self.filter.logdebug("Software accounter %s said job is %s pages long." % (self.arguments, repr(pagecounter)))
84           
85        return pagecounter or 0
Note: See TracBrowser for help on using the browser.