]> 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 7451455671b9b52ab6797949f4de2cfaf712f1a9..a5f4a8e91baed1aa8c9f3dce7c0072fc28ca13da 100644 (file)
@@ -20,11 +20,13 @@ class Spawn(object):
     sent to the process, and responses waited for.
     """
 
-    def __init__(self, args):
+    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.
@@ -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,8 +56,12 @@ 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.
@@ -138,16 +146,20 @@ class Spawn(object):
                     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
                 tnow_s = time.time()
-                tdelta_ms = (tnow_s - tstart_s) * 1000
-                if tdelta_ms > self.timeout:
-                    raise Timeout()
-                events = self.poll.poll(self.timeout - tdelta_ms)
+                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)