예제 #1
0
 def run(self):
     fin = open(self.filename,"r")
     i = 0
     t0 = time.time()
     self.time = time.asctime()
     self.duration = 0
     self.result = "TIMEOUT"
     
     self.output_lines = readline.readlines(fin,self.timeout,self.max_lines)
     if len(self.output_lines) == self.max_lines:
         self.result = "INPUT_COMPLETE"
     
     if self.log:
         if self.log == "-":
             fout = sys.stdout
         else:
             fout = open(self.log,"w")
         fout.write("file    : " + self.filename + "\n")
         fout.write("result  : " + str(self.result) + "\n")
         fout.write("lines   : " + str(len(self.output_lines)))
         if self.max_lines:
             fout.write(" (expected: " + str(self.max_lines) + ")\n")
         fout.write("time    : " + self.time + "\n")
         fout.write("duration: %0.2f seconds\n" % self.duration)
         fout.write("output  :\n")
         fout.writelines(self.output_lines)
         if fout != sys.stdout:
             fout.close()        
예제 #2
0
    def execute(self):
        old_cwd = os.getcwd()
        old_env = dict(os.environ)
        
        os.chdir(self.working_directory)
        for var in self.env:
            os.environ[var] = self.env[var]

        self.execution_time = time.asctime()
        p = popen2.Popen4(self.cmdline)
        
        t0 = time.time()
        
        self.output_lines = readline.readlines(p.fromchild,self.timeout,0)
                
        dt = time.time() - t0
        
        while dt < self.timeout:
            self.result = p.poll()
            if self.result != -1: break
            time.sleep(1)
            dt = time.time() - t0
        
        if (self.timeout != 0 and dt >= self.timeout) or self.result == -1:
            #print "timeout = ",self.timeout," dt = ",dt," result = ",self.result
            self.result = "TIMEOUT"
            os.system("kill " + str(p.pid))
            time.sleep(1)
            os.system("kill -9 " + str(p.pid))
        
        self.duration = time.time() - t0
            
        os.chdir(old_cwd)
        os.environ = old_env
        
        if self.log:
            if self.log == "-":
                fout = sys.stdout
            else:
                fout = open(self.log,"w")
            fout.write("command : " + self.cmdline + "\n")
            fout.write("dir     : " + os.path.abspath(self.working_directory) + "\n")
            fout.write("result  : " + str(self.result) + " (expected: " + str(self.expect_result) + ")\n")
            fout.write("time    : " + self.execution_time + "\n")
            fout.write("duration: %0.2f seconds\n" % self.duration)
            fout.write("output  :\n")
            fout.writelines(self.output_lines)
            if fout != sys.stdout:
                fout.close()
        
        return self.result == self.expect_result