forked from Opticos/GWSL-Source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exe_layer.py
99 lines (85 loc) · 3.34 KB
/
exe_layer.py
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# import time
# import os
from subprocess import PIPE, Popen
import subprocess
from threading import Thread
from queue import Queue, Empty
class cmd:
def __init__(self, command, console=False):
self.proc = None
self.console = console
self.q = []
self.done = False
self.error = False
if console == False:
self.start(command, shell=False)
else:
self.start(command, shell=True)
def start(self, command, shell=False):
if shell == False:
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = subprocess.SW_HIDE
self.proc = Popen(command,
stdout=PIPE, stdin=PIPE, stderr=PIPE, bufsize=1, startupinfo=startupinfo,
universal_newlines=True)
def listener(out, queue):
for line in iter(out.readline, b''):
queue.put(line)
out.close()
def active_listener():
while True:
try:
line = read_queue.get_nowait()
except Empty:
pass
else:
if line != "":
pass # print(line)
if "fatal error" in line.lower():
self.error = True
self.kill()
if "exit" in line.lower() or "done" in line.lower():
for i in self.q:
removes = []
if i.lower() in line.lower():
removes.append(i)
for i in removes:
self.q.remove(i)
if len(self.q) == 0:
self.done = True
read_queue = Queue()
stdout_listener = Thread(target=listener, args=[self.proc.stdout, read_queue])
stdout_listener.daemon = True
stdout_listener.start()
stderr_listener = Thread(target=listener, args=[self.proc.stderr, read_queue])
stderr_listener.daemon = True
stderr_listener.start()
queue = Thread(target=active_listener, args=[])
queue.daemon = True
queue.start()
else:
p = Popen(command, bufsize=1, universal_newlines=True)
# print(p.pid)
# while True:
# running = get_running("GWSL_plink.exe")
# if running == True:
# time.sleep(1)
# else:
# break
def kill(self):
self.proc.kill()
def run(self, command, wait=False, ident=None):
if self.proc != None and self.console == False:
if wait == True:
command += " &"
if " " in command:
commands = command.split(" ")
for c in commands:
self.proc.stdin.write(c + " ")
self.proc.stdin.write("\n")
else:
self.proc.stdin.write(command + " \n")
if wait == True:
self.q.append(ident)
self.proc.stdin.write("wait \n")