tests/ide_tests/sikuliberemiz.py
branchwxPython4
changeset 3437 ce366d67a5b7
parent 3432 40b8b2ffb3fd
child 3446 de8cc85b688a
equal deleted inserted replaced
3436:ccaabb9da623 3437:ce366d67a5b7
     2 
     2 
     3 import os
     3 import os
     4 import sys
     4 import sys
     5 import subprocess
     5 import subprocess
     6 from threading import Thread, Event
     6 from threading import Thread, Event
       
     7 from time import time as timesec
     7 
     8 
     8 typeof=type
     9 typeof=type
     9 
    10 
    10 from sikuli import *
    11 from sikuli import *
    11 
    12 
   140         Wait for IDE to stop changing
   141         Wait for IDE to stop changing
   141         Parameters: 
   142         Parameters: 
   142             period (int): how many seconds with no change to consider idle
   143             period (int): how many seconds with no change to consider idle
   143             timeout (int): how long to wait for idle, in seconds
   144             timeout (int): how long to wait for idle, in seconds
   144         """
   145         """
   145         c = timeout/period
   146         c = max(timeout/period,1)
   146         while c > 0:
   147         while c > 0:
   147             self.idechanged = False
   148             self.idechanged = False
   148             wait(period)
   149             wait(period)
   149             if not self.idechanged:
   150             if not self.idechanged:
   150                 break
   151                 break
   163             proc (subprocess.Popen): Beremiz process, given by StartBeremizApp
   164             proc (subprocess.Popen): Beremiz process, given by StartBeremizApp
   164         """
   165         """
   165         self.proc = proc
   166         self.proc = proc
   166         self.stdoutchanged = False
   167         self.stdoutchanged = False
   167 
   168 
       
   169         self.changes = 0
       
   170         self.last_change_count = 0
       
   171 
       
   172         self.event = Event()
       
   173 
   168         self.thread = Thread(target = self._waitStdoutProc).start()
   174         self.thread = Thread(target = self._waitStdoutProc).start()
   169 
   175 
   170     def _waitStdoutProc(self):
   176     def _waitStdoutProc(self):
   171         while True:
   177         while True:
   172             a = self.proc.stdout.read(1)
   178             a = self.proc.stdout.readline()
   173             if len(a) == 0 or a is None: 
   179             if len(a) == 0 or a is None: 
   174                 break
   180                 break
   175             sys.stdout.write(a)
   181             # sys.stdout.write(a)
   176             self.idechanged = True
   182             self.changes = self.changes + 1
       
   183             self.event.set()
       
   184 
       
   185     def WaitForChangeAndIdle(self, period, timeout):
       
   186         """
       
   187         Wait for IDE'stdout to start changing
       
   188         Parameters: 
       
   189             timeout (int): how long to wait for change, in seconds
       
   190         """
       
   191         start_time = timesec()
       
   192         if self.changes == self.last_change_count:
       
   193             if self.event.wait(timeout):
       
   194                 self.event.clear()
       
   195                 self.last_change_count = self.changes
       
   196             else:
       
   197                 raise Exception("Stdout didn't become active before timeout")
       
   198 
       
   199         self.Wait(period, timeout - (timesec() - start_time))
   177 
   200 
   178     def Wait(self, period, timeout):
   201     def Wait(self, period, timeout):
   179         """
   202         """
   180         Wait for IDE'stdout to stop changing
   203         Wait for IDE'stdout to stop changing
   181         Parameters: 
   204         Parameters: 
   182             period (int): how many seconds with no change to consider idle
   205             period (int): how many seconds with no change to consider idle
   183             timeout (int): how long to wait for idle, in seconds
   206             timeout (int): how long to wait for idle, in seconds
   184         """
   207         """
   185         c = timeout/period
   208         c = max(timeout/period, 1)
   186         while c > 0:
   209         while c > 0:
   187             self.idechanged = False
   210             changes = self.changes
   188             wait(period)
   211             wait(period)
   189             if not self.idechanged:
   212             if self.changes == changes:
       
   213                 self.last_change_count = self.changes
   190                 break
   214                 break
   191             c = c - 1
   215             c = c - 1
   192 
   216 
   193         if c == 0:
   217         if c == 0:
   194             raise Exception("Stdout did not idle before timeout")
   218             raise Exception("Stdout did not idle before timeout")
   202         found = 0
   226         found = 0
   203         while True:
   227         while True:
   204             a = proc.stdout.readline()
   228             a = proc.stdout.readline()
   205             if len(a) == 0 or a is None: 
   229             if len(a) == 0 or a is None: 
   206                 raise Exception("App finished before producing expected stdout pattern")
   230                 raise Exception("App finished before producing expected stdout pattern")
   207             sys.stdout.write(a)
   231             # sys.stdout.write(a)
   208             if a.find(pattern) >= 0:
   232             if a.find(pattern) >= 0:
       
   233                 sys.stdout.write("found pattern in '" + a +"'")
   209                 found = found + 1
   234                 found = found + 1
   210                 if found >= count:
   235                 if found >= count:
   211                     success_event.set()
   236                     success_event.set()
   212                     break
   237                     break
   213 
   238