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

Revision 3260, 6.1 kB (checked in by jerome, 16 years ago)

Changed license to GNU GPL v3 or later.
Changed Python source encoding from ISO-8859-15 to UTF-8 (only ASCII
was used anyway).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[3260]1# -*- coding: UTF-8 -*-
[1475]2#
[3260]3# PyKota : Print Quotas for CUPS
[1475]4#
[3133]5# (c) 2003, 2004, 2005, 2006, 2007 Jerome Alet <alet@librelogiciel.com>
[3260]6# This program is free software: you can redistribute it and/or modify
[1475]7# it under the terms of the GNU General Public License as published by
[3260]8# the Free Software Foundation, either version 3 of the License, or
[1475]9# (at your option) any later version.
[3260]10#
[1475]11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
[3260]17# along with this program.  If not, see <http://www.gnu.org/licenses/>.
[1475]18#
19# $Id$
20#
[2074]21#
[1475]22
23import os
24import popen2
25from pykota.accounter import AccounterBase, PyKotaAccounterError
26
27class Accounter(AccounterBase) :
28    def computeJobSize(self) :   
29        """Feeds an external command with our datas to let it compute the job size, and return its value."""
[2635]30        if (not self.isPreAccounter) and \
31            (self.filter.accounter.arguments == self.filter.preaccounter.arguments) :
32            # if precomputing has been done and both accounter and preaccounter are
33            # configured the same, no need to launch a second pass since we already
34            # know the result.
35            self.filter.logdebug("Precomputing pass told us that job is %s pages long." % self.filter.softwareJobSize)
36            return self.filter.softwareJobSize   # Optimize : already computed !
[1680]37           
[2635]38        if self.arguments :
39            self.filter.logdebug("Using external script %s to compute job's size." % self.arguments)
40            return self.withExternalScript()
41        else :   
42            self.filter.logdebug("Using internal parser to compute job's size.")
43            return self.withInternalParser()
44       
45    def withInternalParser(self) :   
46        """Does software accounting through an external script."""
47        jobsize = 0
48        if self.filter.JobSizeBytes :
[2074]49            try :
[2635]50                from pkpgpdls import analyzer, pdlparser
51            except ImportError :   
[2909]52                self.filter.printInfo("pkpgcounter is now distributed separately, please grab it from http://www.pykota.com/software/pkpgcounter", "error")
[2635]53                self.filter.printInfo("Precomputed job size will be forced to 0 pages.", "error")
54            else :     
55                try :
[3256]56                    parser = analyzer.PDLAnalyzer(self.filter.DataFile)
[2635]57                    jobsize = parser.getJobSize()
58                except pdlparser.PDLParserError, msg :   
59                    # Here we just log the failure, but
60                    # we finally ignore it and return 0 since this
61                    # computation is just an indication of what the
62                    # job's size MAY be.
63                    self.filter.printInfo(_("Unable to precompute the job's size with the generic PDL analyzer : %s") % msg, "warn")
64                else :   
[3245]65                    try :
[3252]66                        if self.filter.Ticket.FileName is not None :
[3245]67                            # when a filename is passed as an argument, the backend
68                            # must generate the correct number of copies.
[3252]69                            jobsize *= self.filter.Ticket.Copies
[3256]70                    except AttributeError : # When not run from the cupspykota backend
[3245]71                        pass
[2635]72        return jobsize       
[2074]73               
[2635]74    def withExternalScript(self) :   
75        """Does software accounting through an external script."""
76        self.filter.printInfo(_("Launching SOFTWARE(%s)...") % self.arguments)
77        MEGABYTE = 1024*1024
78        infile = open(self.filter.DataFile, "rb")
79        child = popen2.Popen4(self.arguments)
80        try :
81            data = infile.read(MEGABYTE)   
82            while data :
83                child.tochild.write(data)
84                data = infile.read(MEGABYTE)
85            child.tochild.flush()
86            child.tochild.close()   
87        except (IOError, OSError), msg :   
88            msg = "%s : %s" % (self.arguments, msg) 
89            self.filter.printInfo(_("Unable to compute job size with accounter %s") % msg)
90        infile.close()
91        pagecounter = None
92        try :
93            answer = child.fromchild.read()
94        except (IOError, OSError), msg :   
95            msg = "%s : %s" % (self.arguments, msg) 
96            self.filter.printInfo(_("Unable to compute job size with accounter %s") % msg)
97        else :   
98            lines = [l.strip() for l in answer.split("\n")]
99            for i in range(len(lines)) : 
100                try :
101                    pagecounter = int(lines[i])
102                except (AttributeError, ValueError) :
103                    self.filter.printInfo(_("Line [%s] skipped in accounter's output. Trying again...") % lines[i])
104                else :   
105                    break
106        child.fromchild.close()
107       
108        try :
109            status = child.wait()
110        except OSError, msg :   
111            self.filter.printInfo(_("Problem while waiting for software accounter pid %s to exit : %s") % (child.pid, msg))
112        else :   
113            if os.WIFEXITED(status) :
114                status = os.WEXITSTATUS(status)
115            self.filter.printInfo(_("Software accounter %s exit code is %s") % (self.arguments, str(status)))
[2074]116           
[2635]117        if pagecounter is None :   
118            message = _("Unable to compute job size with accounter %s") % self.arguments
119            if self.onerror == "CONTINUE" :
120                self.filter.printInfo(message, "error")
121            else :
122                raise PyKotaAccounterError, message
123        self.filter.logdebug("Software accounter %s said job is %s pages long." % (self.arguments, repr(pagecounter)))
124           
[3000]125        pagecounter = pagecounter or 0   
[3252]126        if self.filter.Ticket.FileName is not None :
[3000]127            # when a filename is passed as an argument, the backend
128            # must generate the correct number of copies.
[3252]129            pagecounter *= self.filter.Ticket.Copies
[3000]130                       
131        return pagecounter
Note: See TracBrowser for help on using the browser.