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

Revision 1584, 3.5 kB (checked in by jalet, 20 years ago)

Better dispatching of error messages

  • 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.6  2004/07/01 19:56:43  jalet
25# Better dispatching of error messages
26#
27# Revision 1.5  2004/06/10 22:42:06  jalet
28# Better messages in logs
29#
30# Revision 1.4  2004/06/02 21:51:14  jalet
31# Moved the sigterm capturing elsewhere
32#
33# Revision 1.3  2004/05/24 22:45:49  jalet
34# New 'enforcement' directive added
35# Polling loop improvements
36#
37# Revision 1.2  2004/05/18 14:49:23  jalet
38# Big code changes to completely remove the need for "requester" directives,
39# jsut use "hardware(... your previous requester directive's content ...)"
40#
41# Revision 1.1  2004/05/13 13:59:30  jalet
42# Code simplifications
43#
44#
45
46import sys
47import os
48import popen2
49from pykota.accounter import AccounterBase, PyKotaAccounterError
50
51class Accounter(AccounterBase) :
52    def computeJobSize(self) :   
53        """Feeds an external command with our datas to let it compute the job size, and return its value."""
54        self.filter.printInfo(_("Launching SOFTWARE(%s)...") % self.arguments)
55        MEGABYTE = 1024*1024
56        self.filter.jobdatastream.seek(0)
57        child = popen2.Popen4(self.arguments)
58        try :
59            data = self.filter.jobdatastream.read(MEGABYTE)   
60            while data :
61                child.tochild.write(data)
62                data = self.filter.jobdatastream.read(MEGABYTE)
63            child.tochild.flush()
64            child.tochild.close()   
65        except (IOError, OSError), msg :   
66            msg = "%s : %s" % (self.arguments, msg) 
67            self.filter.printInfo(_("Unable to compute job size with accounter %s") % msg)
68       
69        pagecount = 0
70        try :
71            pagecount = int(child.fromchild.readline().strip())
72        except (AttributeError, ValueError) :
73            self.filter.printInfo(_("Unable to compute job size with accounter %s") % self.arguments)
74        except (IOError, OSError), msg :   
75            msg = "%s : %s" % (self.arguments, msg) 
76            self.filter.printInfo(_("Unable to compute job size with accounter %s") % msg)
77        child.fromchild.close()
78       
79        try :
80            retcode = child.wait()
81        except OSError, msg :   
82            self.filter.printInfo(_("Problem while waiting for software accounter pid %s to exit : %s") % (child.pid, msg))
83        else :   
84            if os.WIFEXITED(retcode) :
85                status = os.WEXITSTATUS(retcode)
86            else :   
87                status = retcode
88            self.filter.printInfo(_("Software accounter %s exit code is %s") % (self.arguments, repr(retcode)))
89        self.filter.logdebug("Software accounter %s said job is %s pages long." % (self.arguments, pagecount))
90        return pagecount   
91           
Note: See TracBrowser for help on using the browser.