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

Revision 1743, 4.9 kB (checked in by jalet, 20 years ago)

Did a pass of PyChecker?

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[1475]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$
[1743]24# Revision 1.11  2004/09/24 21:19:48  jalet
25# Did a pass of PyChecker
26#
[1687]27# Revision 1.10  2004/08/31 23:29:53  jalet
28# Introduction of the new 'onaccountererror' configuration directive.
29# Small fix for software accounter's return code which can't be None anymore.
30# Make software and hardware accounting code look similar : will be factorized
31# later.
32#
[1680]33# Revision 1.9  2004/08/25 22:34:39  jalet
34# Now both software and hardware accounting raise an exception when no valid
35# result can be extracted from the subprocess' output.
36# Hardware accounting now reads subprocess' output until an integer is read
37# or data is exhausted : it now behaves just like software accounting in this
38# aspect.
39#
[1678]40# Revision 1.8  2004/08/22 14:04:47  jalet
41# Tries to fix problem with subprocesses outputting more datas than needed
42#
[1665]43# Revision 1.7  2004/08/06 13:45:51  jalet
44# Fixed french translation problem.
45# Fixed problem with group quotas and strict enforcement.
46#
[1584]47# Revision 1.6  2004/07/01 19:56:43  jalet
48# Better dispatching of error messages
49#
[1536]50# Revision 1.5  2004/06/10 22:42:06  jalet
51# Better messages in logs
52#
[1514]53# Revision 1.4  2004/06/02 21:51:14  jalet
54# Moved the sigterm capturing elsewhere
55#
[1495]56# Revision 1.3  2004/05/24 22:45:49  jalet
57# New 'enforcement' directive added
58# Polling loop improvements
59#
[1483]60# Revision 1.2  2004/05/18 14:49:23  jalet
61# Big code changes to completely remove the need for "requester" directives,
62# jsut use "hardware(... your previous requester directive's content ...)"
63#
[1475]64# Revision 1.1  2004/05/13 13:59:30  jalet
65# Code simplifications
66#
67#
68
69import os
70import popen2
71from pykota.accounter import AccounterBase, PyKotaAccounterError
72
73class Accounter(AccounterBase) :
74    def computeJobSize(self) :   
75        """Feeds an external command with our datas to let it compute the job size, and return its value."""
[1584]76        self.filter.printInfo(_("Launching SOFTWARE(%s)...") % self.arguments)
[1495]77        MEGABYTE = 1024*1024
78        self.filter.jobdatastream.seek(0)
79        child = popen2.Popen4(self.arguments)
[1475]80        try :
[1495]81            data = self.filter.jobdatastream.read(MEGABYTE)   
[1475]82            while data :
[1495]83                child.tochild.write(data)
84                data = self.filter.jobdatastream.read(MEGABYTE)
85            child.tochild.flush()
86            child.tochild.close()   
87        except (IOError, OSError), msg :   
88            msg = "%s : %s" % (self.arguments, msg) 
[1584]89            self.filter.printInfo(_("Unable to compute job size with accounter %s") % msg)
[1475]90       
[1680]91        pagecounter = None
[1495]92        try :
[1678]93            answer = child.fromchild.read()
[1495]94        except (IOError, OSError), msg :   
[1475]95            msg = "%s : %s" % (self.arguments, msg) 
[1584]96            self.filter.printInfo(_("Unable to compute job size with accounter %s") % msg)
[1678]97        else :   
98            lines = [l.strip() for l in answer.split("\n")]
99            for i in range(len(lines)) : 
100                try :
[1680]101                    pagecounter = int(lines[i])
[1678]102                except (AttributeError, ValueError) :
[1680]103                    self.filter.printInfo(_("Line [%s] skipped in accounter's output. Trying again...") % lines[i])
[1678]104                else :   
105                    break
[1495]106        child.fromchild.close()
107       
108        try :
[1687]109            status = child.wait()
[1495]110        except OSError, msg :   
[1584]111            self.filter.printInfo(_("Problem while waiting for software accounter pid %s to exit : %s") % (child.pid, msg))
[1495]112        else :   
[1687]113            if os.WIFEXITED(status) :
114                status = os.WEXITSTATUS(status)
[1665]115            self.filter.printInfo(_("Software accounter %s exit code is %s") % (self.arguments, str(status)))
[1475]116           
[1680]117        if pagecounter is None :   
[1687]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
[1680]123           
[1687]124        self.filter.logdebug("Software accounter %s said job is %s pages long." % (self.arguments, repr(pagecounter)))
125        return pagecounter or 0
Note: See TracBrowser for help on using the browser.