Changeset 3413 for pykota/trunk/pykota/accounters/software.py
- Timestamp:
- 09/27/08 22:02:37 (16 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
pykota/trunk/pykota/accounters/software.py
r3411 r3413 8 8 # the Free Software Foundation, either version 3 of the License, or 9 9 # (at your option) any later version. 10 # 10 # 11 11 # This program is distributed in the hope that it will be useful, 12 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 14 # GNU General Public License for more details. 15 # 15 # 16 16 # You should have received a copy of the GNU General Public License 17 17 # along with this program. If not, see <http://www.gnu.org/licenses/>. … … 30 30 31 31 class Accounter(AccounterBase) : 32 def computeJobSize(self) : 32 def computeJobSize(self) : 33 33 """Feeds an external command with our datas to let it compute the job size, and return its value.""" 34 34 if (not self.isPreAccounter) and \ … … 39 39 self.filter.logdebug("Precomputing pass told us that job is %s pages long." % self.filter.softwareJobSize) 40 40 return self.filter.softwareJobSize # Optimize : already computed ! 41 41 42 42 if self.arguments : 43 43 self.filter.logdebug("Using external script %s to compute job's size." % self.arguments) 44 44 return self.withExternalScript() 45 else : 45 else : 46 46 self.filter.logdebug("Using internal parser to compute job's size.") 47 47 return self.withInternalParser() 48 49 def withInternalParser(self) : 48 49 def withInternalParser(self) : 50 50 """Does software accounting through an external script.""" 51 51 jobsize = 0 … … 53 53 try : 54 54 from pkpgpdls import analyzer, pdlparser 55 except ImportError : 55 except ImportError : 56 56 self.filter.printInfo("pkpgcounter is now distributed separately, please grab it from http://www.pykota.com/software/pkpgcounter", "error") 57 57 self.filter.printInfo("Precomputed job size will be forced to 0 pages.", "error") 58 else : 58 else : 59 59 try : 60 60 parser = analyzer.PDLAnalyzer(self.filter.DataFile) 61 61 jobsize = parser.getJobSize() 62 except pdlparser.PDLParserError, msg : 62 except pdlparser.PDLParserError, msg : 63 63 # Here we just log the failure, but 64 64 # we finally ignore it and return 0 since this … … 66 66 # job's size MAY be. 67 67 self.filter.printInfo(_("Unable to precompute the job's size with the generic PDL analyzer : %s") % msg, "warn") 68 else : 68 else : 69 69 try : 70 70 if self.filter.Ticket.FileName is not None : 71 # when a filename is passed as an argument, the backend 71 # when a filename is passed as an argument, the backend 72 72 # must generate the correct number of copies. 73 73 jobsize *= self.filter.Ticket.Copies 74 74 except AttributeError : # When not run from the cupspykota backend 75 75 pass 76 return jobsize 77 78 def withExternalScript(self) : 76 return jobsize 77 78 def withExternalScript(self) : 79 79 """Does software accounting through an external script.""" 80 80 self.filter.logdebug(_("Launching SOFTWARE(%s)...") % self.arguments) … … 83 83 try : 84 84 answer = child.read() 85 except (IOError, OSError), msg : 86 msg = "%s : %s" % (self.arguments, msg) 85 except (IOError, OSError), msg : 86 msg = "%s : %s" % (self.arguments, msg) 87 87 self.filter.printInfo(_("Unable to compute job size with accounter %s") % msg, "warn") 88 else : 88 else : 89 89 lines = [l.strip() for l in answer.split("\n")] 90 for i in range(len(lines)) : 90 for i in range(len(lines)) : 91 91 try : 92 92 pagecounter = int(lines[i]) 93 93 except (AttributeError, ValueError) : 94 94 self.filter.logdebug(_("Line [%s] skipped in accounter's output. Trying again...") % lines[i]) 95 else : 95 else : 96 96 break 97 97 98 98 status = child.close() 99 99 try : 100 100 if os.WIFEXITED(status) : 101 101 status = os.WEXITSTATUS(status) 102 except TypeError : 102 except TypeError : 103 103 pass # None means no error occured. 104 104 self.filter.logdebug(_("Software accounter %s exit code is %s") % (self.arguments, str(status))) 105 106 if pagecounter is None : 105 106 if pagecounter is None : 107 107 message = _("Unable to compute job size with accounter %s") % self.arguments 108 108 if self.onerror == "CONTINUE" : … … 111 111 raise PyKotaAccounterError, message 112 112 self.filter.logdebug("Software accounter %s said job is %s pages long." % (self.arguments, repr(pagecounter))) 113 114 pagecounter = pagecounter or 0 113 114 pagecounter = pagecounter or 0 115 115 try : 116 116 if self.filter.Ticket.FileName is not None : 117 # when a filename is passed as an argument, the backend 117 # when a filename is passed as an argument, the backend 118 118 # must generate the correct number of copies. 119 119 pagecounter *= self.filter.Ticket.Copies 120 except AttributeError : 120 except AttributeError : 121 121 pass # when called from pykotme. TODO : clean this mess some day. 122 122 123 123 return pagecounter