Show
Ignore:
Timestamp:
05/25/04 00:45:49 (20 years ago)
Author:
jalet
Message:

New 'enforcement' directive added
Polling loop improvements

Files:
1 modified

Legend:

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

    r1483 r1495  
    2222# 
    2323# $Log$ 
     24# Revision 1.3  2004/05/24 22:45:49  jalet 
     25# New 'enforcement' directive added 
     26# Polling loop improvements 
     27# 
    2428# Revision 1.2  2004/05/18 14:49:23  jalet 
    2529# Big code changes to completely remove the need for "requester" directives, 
     
    3034# 
    3135# 
    32 # 
    3336 
    3437import sys 
    3538import os 
    3639import popen2 
    37 import tempfile 
    3840from pykota.accounter import AccounterBase, PyKotaAccounterError 
    3941 
     
    4143    def computeJobSize(self) :     
    4244        """Feeds an external command with our datas to let it compute the job size, and return its value.""" 
    43         temporary = None     
    44         if self.filter.inputfile is None :     
    45             infile = sys.stdin 
    46             # we will have to duplicate our standard input 
    47             temporary = tempfile.TemporaryFile() 
    48         else :     
    49             infile = open(self.filter.inputfile, "rb") 
    50              
    51         # launches software accounter 
    52         # TODO : USE tempfile.mkstemp() instead ! Needs some work ! 
    53         infilename = tempfile.mktemp() 
    54         outfilename = tempfile.mktemp() 
    55         errfilename = tempfile.mktemp() 
     45        self.filter.logdebug("Launching software accounter %s" % self.arguments) 
     46        MEGABYTE = 1024*1024 
     47        self.filter.jobdatastream.seek(0) 
     48        child = popen2.Popen4(self.arguments) 
     49        try : 
     50            data = self.filter.jobdatastream.read(MEGABYTE)     
     51            while data : 
     52                child.tochild.write(data) 
     53                data = self.filter.jobdatastream.read(MEGABYTE) 
     54            child.tochild.flush() 
     55            child.tochild.close()     
     56        except (IOError, OSError), msg :     
     57            msg = "%s : %s" % (self.arguments, msg)  
     58            self.filter.logger.log_message(_("Unable to compute job size with accounter %s") % msg) 
     59         
     60        pagecount = 0 
     61        try : 
     62            pagecount = int(child.fromchild.readline().strip()) 
     63        except (AttributeError, ValueError) : 
     64            self.filter.logger.log_message(_("Unable to compute job size with accounter %s") % self.arguments) 
     65        except (IOError, OSError), msg :     
     66            msg = "%s : %s" % (self.arguments, msg)  
     67            self.filter.logger.log_message(_("Unable to compute job size with accounter %s") % msg) 
     68        child.fromchild.close() 
    5669         
    5770        try : 
    58             # feed it with our data 
    59             fakeinput = open(infilename, "wb") 
    60             data = infile.read(256*1024)     
    61             while data : 
    62                 fakeinput.write(data) 
    63                 if temporary is not None : 
    64                     temporary.write(data) 
    65                 data = infile.read(256*1024) 
    66             fakeinput.close() 
    67          
    68             # launches child process 
    69             command = "%s <%s >%s 2>%s" % (self.arguments, infilename, outfilename, errfilename) 
    70             retcode = os.system(command) 
    71              
    72             # check exit status 
    73             if (os.WIFEXITED(retcode) and not os.WEXITSTATUS(retcode)) or os.stat(errfilename) : 
    74                 # tries to extract the job size from the software accounter's 
    75                 # standard output 
    76                 childoutput = open(outfilename, "r") 
    77                 try : 
    78                     pagecount = int(childoutput.readline().strip()) 
    79                 except (AttributeError, ValueError) : 
    80                     self.filter.logger.log_message(_("Unable to compute job size with accounter %s") % self.arguments) 
    81                     pagecount = 0 
    82                 childoutput.close()     
    83             else : 
    84                 self.filter.logger.log_message(_("Unable to compute job size with accounter %s") % self.arguments) 
    85                 pagecount = 0 
    86             os.remove(infilename) 
    87             os.remove(outfilename) 
    88             os.remove(errfilename) 
    89         except IOError, msg :     
    90             # TODO : temporary files may remain on the filesystem... 
    91             msg = "%s : %s" % (self.arguments, msg)  
    92             self.filter.logger.log_message(_("Unable to compute job size with accounter %s") % msg) 
    93             pagecount = 0 
    94              
    95         if temporary is not None :     
    96             # this is a copy of our previous standard input 
    97             # flush, then rewind 
    98             temporary.flush() 
    99             temporary.seek(0, 0) 
    100             # our temporary file will be used later if the 
    101             # job is allowed. 
    102             self.filter.inputfile = temporary 
    103         else : 
    104             infile.close() 
     71            retcode = child.wait() 
     72        except OSError, msg :     
     73            self.filter.logger.log_message(_("Problem while waiting for software accounter pid %s to exit") % child.pid) 
     74        else :     
     75            if os.WIFEXITED(retcode) : 
     76                status = os.WEXITSTATUS(retcode) 
     77            else :     
     78                status = retcode 
     79            self.filter.logger.log_message(_("Software accounter %s exit code is %s") % (self.arguments, repr(retcode))) 
     80        self.filter.logdebug("Software accounter %s said job is %s pages long." % (self.arguments, pagecount)) 
    10581        return pagecount     
    10682