1 | # -*- coding: UTF-8 -*- |
---|
2 | # |
---|
3 | # PyKota : Print Quotas for CUPS |
---|
4 | # |
---|
5 | # (c) 2003, 2004, 2005, 2006, 2007, 2008 Jerome Alet <alet@librelogiciel.com> |
---|
6 | # This program is free software: you can redistribute it and/or modify |
---|
7 | # it under the terms of the GNU General Public License as published by |
---|
8 | # the Free Software Foundation, either version 3 of the License, or |
---|
9 | # (at your option) any later version. |
---|
10 | # |
---|
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 |
---|
17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
18 | # |
---|
19 | # $Id$ |
---|
20 | # |
---|
21 | # |
---|
22 | |
---|
23 | import os |
---|
24 | import popen2 |
---|
25 | from pykota.accounter import AccounterBase, PyKotaAccounterError |
---|
26 | |
---|
27 | class 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.""" |
---|
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 ! |
---|
37 | |
---|
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 : |
---|
49 | try : |
---|
50 | from pkpgpdls import analyzer, pdlparser |
---|
51 | except ImportError : |
---|
52 | self.filter.printInfo("pkpgcounter is now distributed separately, please grab it from http://www.pykota.com/software/pkpgcounter", "error") |
---|
53 | self.filter.printInfo("Precomputed job size will be forced to 0 pages.", "error") |
---|
54 | else : |
---|
55 | try : |
---|
56 | parser = analyzer.PDLAnalyzer(self.filter.DataFile) |
---|
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 : |
---|
65 | try : |
---|
66 | if self.filter.Ticket.FileName is not None : |
---|
67 | # when a filename is passed as an argument, the backend |
---|
68 | # must generate the correct number of copies. |
---|
69 | jobsize *= self.filter.Ticket.Copies |
---|
70 | except AttributeError : # When not run from the cupspykota backend |
---|
71 | pass |
---|
72 | return jobsize |
---|
73 | |
---|
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))) |
---|
116 | |
---|
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 | |
---|
125 | pagecounter = pagecounter or 0 |
---|
126 | if self.filter.Ticket.FileName is not None : |
---|
127 | # when a filename is passed as an argument, the backend |
---|
128 | # must generate the correct number of copies. |
---|
129 | pagecounter *= self.filter.Ticket.Copies |
---|
130 | |
---|
131 | return pagecounter |
---|