##// END OF EJS Templates
Make it more probably to get colors in shell commands.
Gael Varoquaux -
Show More
@@ -1,70 +1,74 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """
2 """
3 Object for encapsulating process execution by using callbacks for stdout,
3 Object for encapsulating process execution by using callbacks for stdout,
4 stderr and stdin.
4 stderr and stdin.
5 """
5 """
6 __docformat__ = "restructuredtext en"
6 __docformat__ = "restructuredtext en"
7
7
8 #-------------------------------------------------------------------------------
8 #-------------------------------------------------------------------------------
9 # Copyright (C) 2008 The IPython Development Team
9 # Copyright (C) 2008 The IPython Development Team
10 #
10 #
11 # Distributed under the terms of the BSD License. The full license is in
11 # Distributed under the terms of the BSD License. The full license is in
12 # the file COPYING, distributed as part of this software.
12 # the file COPYING, distributed as part of this software.
13 #-------------------------------------------------------------------------------
13 #-------------------------------------------------------------------------------
14
14
15 #-------------------------------------------------------------------------------
15 #-------------------------------------------------------------------------------
16 # Imports
16 # Imports
17 #-------------------------------------------------------------------------------
17 #-------------------------------------------------------------------------------
18 from killableprocess import Popen, PIPE
18 from killableprocess import Popen, PIPE
19 from threading import Thread
19 from threading import Thread
20 from time import sleep
20 from time import sleep
21 import os
21
22
22 class PipedProcess(Thread):
23 class PipedProcess(Thread):
23 """ Class that encapsulates process execution by using callbacks for
24 """ Class that encapsulates process execution by using callbacks for
24 stdout, stderr and stdin, and providing a reliable way of
25 stdout, stderr and stdin, and providing a reliable way of
25 killing it.
26 killing it.
26 """
27 """
27
28
28 def __init__(self, command_string, out_callback,
29 def __init__(self, command_string, out_callback,
29 end_callback=None,):
30 end_callback=None,):
30 """ command_string: the command line executed to start the
31 """ command_string: the command line executed to start the
31 process.
32 process.
32
33
33 out_callback: the python callable called on stdout/stderr.
34 out_callback: the python callable called on stdout/stderr.
34
35
35 end_callback: an optional callable called when the process
36 end_callback: an optional callable called when the process
36 finishes.
37 finishes.
37
38
38 These callbacks are called from a different thread as the
39 These callbacks are called from a different thread as the
39 thread from which is started.
40 thread from which is started.
40 """
41 """
41 self.command_string = command_string
42 self.command_string = command_string
42 self.out_callback = out_callback
43 self.out_callback = out_callback
43 self.end_callback = end_callback
44 self.end_callback = end_callback
44 Thread.__init__(self)
45 Thread.__init__(self)
45
46
46
47
47 def run(self):
48 def run(self):
48 """ Start the process and hook up the callbacks.
49 """ Start the process and hook up the callbacks.
49 """
50 """
51 env = os.environ
52 env['TERM'] = 'xterm'
50 process = Popen((self.command_string + ' 2>&1', ), shell=True,
53 process = Popen((self.command_string + ' 2>&1', ), shell=True,
54 env=env,
51 universal_newlines=True,
55 universal_newlines=True,
52 stdout=PIPE, stdin=PIPE, )
56 stdout=PIPE, stdin=PIPE, )
53 self.process = process
57 self.process = process
54 while True:
58 while True:
55 out_char = process.stdout.read(1)
59 out_char = process.stdout.read(1)
56 if out_char == '':
60 if out_char == '':
57 if process.poll() is not None:
61 if process.poll() is not None:
58 # The process has finished
62 # The process has finished
59 break
63 break
60 else:
64 else:
61 # The process is not giving any interesting
65 # The process is not giving any interesting
62 # output. No use polling it immediatly.
66 # output. No use polling it immediatly.
63 sleep(0.1)
67 sleep(0.1)
64 else:
68 else:
65 self.out_callback(out_char)
69 self.out_callback(out_char)
66
70
67 if self.end_callback is not None:
71 if self.end_callback is not None:
68 self.end_callback()
72 self.end_callback()
69
73
70
74
General Comments 0
You need to be logged in to leave comments. Login now