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

Revision 1678, 4.0 kB (checked in by jalet, 20 years ago)

Tries to fix problem with subprocesses outputting more datas than needed

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