]> git.sur5r.net Git - u-boot/blobdiff - test/py/u_boot_spawn.py
ARM: at91: sama5d2: configure the L2 cache memory
[u-boot] / test / py / u_boot_spawn.py
index 1baee63df25c272e33de70e14b4234d5d9c1d14a..a5f4a8e91baed1aa8c9f3dce7c0072fc28ca13da 100644 (file)
@@ -12,23 +12,25 @@ import select
 import time
 
 class Timeout(Exception):
-    '''An exception sub-class that indicates that a timeout occurred.'''
+    """An exception sub-class that indicates that a timeout occurred."""
     pass
 
 class Spawn(object):
-    '''Represents the stdio of a freshly created sub-process. Commands may be
+    """Represents the stdio of a freshly created sub-process. Commands may be
     sent to the process, and responses waited for.
-    '''
+    """
 
-    def __init__(self, args):
-        '''Spawn (fork/exec) the sub-process.
+    def __init__(self, args, cwd=None):
+        """Spawn (fork/exec) the sub-process.
 
         Args:
-            args: array of processs arguments. argv[0] is the command to execute.
+            args: array of processs arguments. argv[0] is the command to
+              execute.
+            cwd: the directory to run the process in, or None for no change.
 
         Returns:
             Nothing.
-        '''
+        """
 
         self.waited = False
         self.buf = ''
@@ -44,6 +46,8 @@ class Spawn(object):
                 # run under "go" (www.go.cd). Perhaps this happens under any
                 # background (non-interactive) system?
                 signal.signal(signal.SIGHUP, signal.SIG_DFL)
+                if cwd:
+                    os.chdir(cwd)
                 os.execvp(args[0], args)
             except:
                 print 'CHILD EXECEPTION:'
@@ -52,30 +56,34 @@ class Spawn(object):
             finally:
                 os._exit(255)
 
-        self.poll = select.poll()
-        self.poll.register(self.fd, select.POLLIN | select.POLLPRI | select.POLLERR | select.POLLHUP | select.POLLNVAL)
+        try:
+            self.poll = select.poll()
+            self.poll.register(self.fd, select.POLLIN | select.POLLPRI | select.POLLERR | select.POLLHUP | select.POLLNVAL)
+        except:
+            self.close()
+            raise
 
     def kill(self, sig):
-        '''Send unix signal "sig" to the child process.
+        """Send unix signal "sig" to the child process.
 
         Args:
             sig: The signal number to send.
 
         Returns:
             Nothing.
-        '''
+        """
 
         os.kill(self.pid, sig)
 
     def isalive(self):
-        '''Determine whether the child process is still running.
+        """Determine whether the child process is still running.
 
         Args:
             None.
 
         Returns:
             Boolean indicating whether process is alive.
-        '''
+        """
 
         if self.waited:
             return False
@@ -88,19 +96,19 @@ class Spawn(object):
         return False
 
     def send(self, data):
-        '''Send data to the sub-process's stdin.
+        """Send data to the sub-process's stdin.
 
         Args:
             data: The data to send to the process.
 
         Returns:
             Nothing.
-        '''
+        """
 
         os.write(self.fd, data)
 
     def expect(self, patterns):
-        '''Wait for the sub-process to emit specific data.
+        """Wait for the sub-process to emit specific data.
 
         This function waits for the process to emit one pattern from the
         supplied list of patterns, or for a timeout to occur.
@@ -116,12 +124,13 @@ class Spawn(object):
         Notable exceptions:
             Timeout, if the process did not emit any of the patterns within
             the expected time.
-        '''
+        """
 
         for pi in xrange(len(patterns)):
             if type(patterns[pi]) == type(''):
                 patterns[pi] = re.compile(patterns[pi])
 
+        tstart_s = time.time()
         try:
             while True:
                 earliest_m = None
@@ -131,18 +140,26 @@ class Spawn(object):
                     m = pattern.search(self.buf)
                     if not m:
                         continue
-                    if earliest_m and m.start() > earliest_m.start():
+                    if earliest_m and m.start() >= earliest_m.start():
                         continue
                     earliest_m = m
                     earliest_pi = pi
                 if earliest_m:
                     pos = earliest_m.start()
-                    posafter = earliest_m.end() + 1
+                    posafter = earliest_m.end()
                     self.before = self.buf[:pos]
                     self.after = self.buf[pos:posafter]
                     self.buf = self.buf[posafter:]
                     return earliest_pi
-                events = self.poll.poll(self.timeout)
+                tnow_s = time.time()
+                if self.timeout:
+                    tdelta_ms = (tnow_s - tstart_s) * 1000
+                    poll_maxwait = self.timeout - tdelta_ms
+                    if tdelta_ms > self.timeout:
+                        raise Timeout()
+                else:
+                    poll_maxwait = None
+                events = self.poll.poll(poll_maxwait)
                 if not events:
                     raise Timeout()
                 c = os.read(self.fd, 1024)
@@ -156,7 +173,7 @@ class Spawn(object):
                 self.logfile_read.flush()
 
     def close(self):
-        '''Close the stdio connection to the sub-process.
+        """Close the stdio connection to the sub-process.
 
         This also waits a reasonable time for the sub-process to stop running.
 
@@ -165,7 +182,7 @@ class Spawn(object):
 
         Returns:
             Nothing.
-        '''
+        """
 
         os.close(self.fd)
         for i in xrange(100):