Show
Ignore:
Timestamp:
02/19/05 19:16:06 (19 years ago)
Author:
jalet
Message:

Optimize print job parsing by avoiding to pass the job's datas through
PyKota's internal parser if the special construct "software()" is used
with no argument in the 'accounter' directive.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/pykota/accounters/software.py

    r1743 r2074  
    2222# 
    2323# $Log$ 
     24# Revision 1.12  2005/02/19 18:16:06  jalet 
     25# Optimize print job parsing by avoiding to pass the job's datas through 
     26# PyKota's internal parser if the special construct "software()" is used 
     27# with no argument in the 'accounter' directive. 
     28# 
    2429# Revision 1.11  2004/09/24 21:19:48  jalet 
    2530# Did a pass of PyChecker 
     
    7580        """Feeds an external command with our datas to let it compute the job size, and return its value.""" 
    7681        self.filter.printInfo(_("Launching SOFTWARE(%s)...") % self.arguments) 
    77         MEGABYTE = 1024*1024 
    78         self.filter.jobdatastream.seek(0) 
    79         child = popen2.Popen4(self.arguments) 
    80         try : 
    81             data = self.filter.jobdatastream.read(MEGABYTE)     
    82             while data : 
    83                 child.tochild.write(data) 
    84                 data = self.filter.jobdatastream.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          
    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))) 
     82        if not self.arguments : 
     83            pagecounter = self.filter.softwareJobSize   # Optimize : already computed ! 
     84            self.filter.logdebug("Internal software accounter said job is %s pages long." % repr(pagecounter)) 
     85        else : 
     86            MEGABYTE = 1024*1024 
     87            self.filter.jobdatastream.seek(0) 
     88            child = popen2.Popen4(self.arguments) 
     89            try : 
     90                data = self.filter.jobdatastream.read(MEGABYTE)     
     91                while data : 
     92                    child.tochild.write(data) 
     93                    data = self.filter.jobdatastream.read(MEGABYTE) 
     94                child.tochild.flush() 
     95                child.tochild.close()     
     96            except (IOError, OSError), msg :     
     97                msg = "%s : %s" % (self.arguments, msg)  
     98                self.filter.printInfo(_("Unable to compute job size with accounter %s") % msg) 
    11699             
    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 
     100            pagecounter = None 
     101            try : 
     102                answer = child.fromchild.read() 
     103            except (IOError, OSError), msg :     
     104                msg = "%s : %s" % (self.arguments, msg)  
     105                self.filter.printInfo(_("Unable to compute job size with accounter %s") % msg) 
     106            else :     
     107                lines = [l.strip() for l in answer.split("\n")] 
     108                for i in range(len(lines)) :  
     109                    try : 
     110                        pagecounter = int(lines[i]) 
     111                    except (AttributeError, ValueError) : 
     112                        self.filter.printInfo(_("Line [%s] skipped in accounter's output. Trying again...") % lines[i]) 
     113                    else :     
     114                        break 
     115            child.fromchild.close() 
    123116             
    124         self.filter.logdebug("Software accounter %s said job is %s pages long." % (self.arguments, repr(pagecounter))) 
     117            try : 
     118                status = child.wait() 
     119            except OSError, msg :     
     120                self.filter.printInfo(_("Problem while waiting for software accounter pid %s to exit : %s") % (child.pid, msg)) 
     121            else :     
     122                if os.WIFEXITED(status) : 
     123                    status = os.WEXITSTATUS(status) 
     124                self.filter.printInfo(_("Software accounter %s exit code is %s") % (self.arguments, str(status))) 
     125                 
     126            if pagecounter is None :     
     127                message = _("Unable to compute job size with accounter %s") % self.arguments 
     128                if self.onerror == "CONTINUE" : 
     129                    self.filter.printInfo(message, "error") 
     130                else : 
     131                    raise PyKotaAccounterError, message 
     132            self.filter.logdebug("Software accounter %s said job is %s pages long." % (self.arguments, repr(pagecounter))) 
     133             
    125134        return pagecounter or 0