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

Revision 1536, 3.4 kB (checked in by jalet, 20 years ago)

Better messages in logs

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