improve source file lookup
[rofl0r-gdbpimp.git] / proc.py
blob693d254bf5e2e3834977296be4ba0137658138e1
1 import subprocess, time, select, sys
2 class Proc():
3 def __init__(self, command, shell=False, *args, **kwargs):
4 self.proc = subprocess.Popen(command, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE, shell=shell)
5 self.exitcode = None
6 def get_exitcode(self):
7 return self.exitcode
8 def _cleanup(self):
9 self.exitcode = self.proc.wait()
10 self.proc.stdout.close()
11 self.proc.stderr.close()
12 self.proc.stdin.close()
13 self.proc.stdout = -1
14 self.proc.stderr = -1
15 self.proc.stdin = -1
16 def canread(self, handle, timeout=0.0001):
17 # self.proc.poll()
18 if handle == -1 or self.exitcode != None: return False
19 a,b,c = select.select([handle], [], [], timeout)
20 if handle in c:
21 self._cleanup()
22 return False
23 return handle in a
24 def read_until(self, handle, marker):
25 s = ''
26 while not s.endswith(marker):
27 if not self.canread(handle, timeout=1): break
28 t = handle.read(1)
29 if t == '':
30 self._cleanup()
31 return s
32 s += t
33 return s
34 def stdout(self):
35 return self.proc.stdout
36 def stderr(self):
37 return self.proc.stderr
38 def stdin(self):
39 return self.proc.stdin
40 def close(self):
41 return self.proc.terminate()