forked from drewdeponte/sublime_guard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
guard_wrapper
executable file
·74 lines (66 loc) · 2.29 KB
/
guard_wrapper
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python
import sys
import os
import subprocess
import threading
class GuardWrapper:
def __init__(self):
self.running = False
self.proc = None
self.stdout_thread = None
self.stderr_thread = None
self.log = open('/tmp/guard_wrapper.log', 'w')
def run_cmd(self, cmd_array):
self.proc = subprocess.Popen(cmd_array, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
self.running = True
if self.proc.stdout:
self.stdout_thread = threading.Thread(target=self.read_stdout, args=())
self.stdout_thread.start()
if self.proc.stderr:
self.stderr_thread = threading.Thread(target=self.read_stderr, args=())
self.stderr_thread.start()
while True:
self.log.write("read stdin iteration\n")
self.log.flush()
data = sys.stdin.readline()
if data != "":
self.log.write("Received: " + data + "\n")
self.log.flush()
if data == "e\n":
self.proc.stdin.write(data)
self.proc.stdin.flush()
break
else:
self.proc.stdin.write(data)
self.proc.stdin.flush()
else:
self.log.write("got empty string, :-(\n")
self.log.flush()
self.proc.stdin.write("e\n")
self.proc.stdin.flush()
self.proc.stdin.close()
break
self.stdout_thread.join()
self.stderr_thread.join()
def read_stdout(self):
while True:
data = os.read(self.proc.stdout.fileno(), 2 ** 15)
if data != "":
sys.stdout.write(data)
sys.stdout.flush()
else:
self.proc.stdout.close()
self.running = False
break
def read_stderr(self):
while True:
data = os.read(self.proc.stderr.fileno(), 2 ** 15)
if data != "":
sys.stderr.write(data)
sys.stderr.flush()
else:
self.proc.stderr.close()
self.running = False
break
wrap = GuardWrapper()
wrap.run_cmd(sys.argv[1:])