-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractTool.py
185 lines (153 loc) · 5.26 KB
/
AbstractTool.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import subprocess
class ToolAbstract:
def __init__(self, name: str, path: str, *args):
self._rules = {}
self.args = args
self.path = path
self.name = name
self._process: subprocess.Popen = None
self._exit_code = None
self._results = None
self._stdout = None
self._stderr = None
def start(self):
if self._process is None:
self._process = subprocess.Popen(
[self.path, *self.args],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
else:
raise RuntimeError("Process already started")
@property
def stdout(self):
if self._stdout is None:
raise RuntimeError("Process not finished or started")
return self._stdout
@property
def stderr(self):
if self._stderr is None:
raise RuntimeError("Process not finished or started")
return self._stderr
@property
def exit_code(self):
if self._exit_code is None:
raise RuntimeError("Process not finished")
return self._exit_code
def _parse_implicit_rules(self):
self._results = {}
for method in dir(self.__class__):
if method.endswith("_rule"):
self.rule_register(method[:-5], getattr(self, method))
def rule_register(self, name: str, rule):
self._rules[name] = rule
def generate_results(self):
for rule in self._rules:
self._results[rule] = self._rules[rule](self._stdout)
def compile_results(self):
for rule in self._results:
setattr(self, rule + "_result", self._results[rule])
def _on_exit(self):
self._stdout, self._stderr = self._process.communicate()
self._stdout = self._stdout.decode()
self._stderr = self._stderr.decode()
self._parse_implicit_rules()
self.generate_results()
self.compile_results()
def wait(self):
if self._process is not None:
self._exit_code = self._process.wait()
self._on_exit()
else:
raise RuntimeError("Process not started")
def terminate(self):
if self._process is not None:
self._process.terminate()
self._on_exit()
else:
raise RuntimeError("Process not started")
def kill(self):
if self._process is not None:
self._process.kill()
self._on_exit()
else:
raise RuntimeError("Process not started")
def on_end(self):
self._on_exit()
import subprocess
import asyncio
class ToolAbstractAsync:
def __init__(self, name: str, path: str, *args):
self._rules = {}
self.args = args
self.path = path
self.name = name
self._process: subprocess.Popen = None
self._exit_code = None
self._results = None
self._stdout = None
self._stderr = None
async def start(self):
if self._process is None:
self._process = await asyncio.create_subprocess_exec(
self.path, *self.args,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
else:
raise RuntimeError("Process already started")
@property
def stdout(self):
if self._stdout is None:
raise RuntimeError("Process not finished or started")
return self._stdout
@property
def stderr(self):
if self._stderr is None:
raise RuntimeError("Process not finished or started")
return self._stderr
@property
def exit_code(self):
if self._exit_code is None:
raise RuntimeError("Process not finished")
return self._exit_code
def _parse_implicit_rules(self):
self._results = {}
for method in dir(self.__class__):
if method.endswith("_rule"):
self.rule_register(method[:-5], getattr(self, method))
def rule_register(self, name: str, rule):
self._rules[name] = rule
def generate_results(self):
for rule in self._rules:
self._results[rule] = self._rules[rule](self._stdout)
def compile_results(self):
for rule in self._results:
setattr(self, rule + "_result", self._results[rule])
async def _on_exit(self):
self._stdout, self._stderr = await self._process.communicate()
self._stdout = self._stdout.decode()
self._stderr = self._stderr.decode()
self._parse_implicit_rules()
self.generate_results()
self.compile_results()
async def wait(self):
if self._process is not None:
self._exit_code = await self._process.wait()
await self._on_exit()
else:
raise RuntimeError("Process not started")
async def terminate(self):
if self._process is not None:
self._process.terminate()
await self._on_exit()
else:
raise RuntimeError("Process not started")
async def kill(self):
if self._process is not None:
self._process.kill()
await self._on_exit()
else:
raise RuntimeError("Process not started")
async def on_end(self):
await self._on_exit()