-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteractive_cmd.py
164 lines (150 loc) · 6 KB
/
interactive_cmd.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
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import logging
import re
import select
import subprocess
import sys
import time
class InteractiveCommand:
def __init__(self, args, log_level=logging.WARNING, log_name=None,
ignore_eperm=False, shell=True):
self._args = args
self._ignore_eperm = ignore_eperm
self._shell = shell
self._is_running = False
self._pending = 0
logger_name = log_name or self._args.split()[0]
self._logger = logging.getLogger('iCMD:{}'.format(logger_name))
self._logger.setLevel(log_level)
ch = logging.StreamHandler()
ch.setLevel(log_level)
formatter = logging.Formatter(
("%(asctime)s - %(name)s - %(levelname)s -"
" %(message)s"))
ch.setFormatter(formatter)
self._logger.addHandler(ch)
def __enter__(self):
self.start()
return self
def __exit__(self, exc_type, exc_value, traceback):
try:
self.kill()
except PermissionError:
if not self._ignore_eperm:
raise
self._is_running = False
def start(self):
self._logger.info("Starting command. Args: %s" % self._args)
self._proc = subprocess.Popen(
self._args, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, shell=self._shell)
self._is_running = True
self._poller = select.poll()
self._logger.debug("Registering poller")
self._poller.register(self._proc.stdout, select.POLLIN)
def kill(self):
self._logger.info("Killing...")
if self._is_running:
# explicitly closing files to make test not complain about leaking
# them (GC race condition?)
self._logger.debug("Closing pipes...")
self._close_fds([self._proc.stdin, self._proc.stdout])
try:
self._logger.debug("Check if process died after closing pipes")
self._proc.wait(timeout=0.1)
except subprocess.TimeoutExpired:
self._logger.debug("wait() timed out.")
self._logger.debug("Terminating...")
self._proc.terminate()
self._is_running = False
self._logger.debug("Waiting for process to terminate...")
self._proc.wait()
self._logger.info("Killed.")
def writeline(self, line, sleep=0.1):
self._logger.info("Writing to process: %s" % line)
if not self._is_running:
self._logger.warning("Process is not running!")
raise Exception('Process is not running')
try:
self._proc.stdin.write((line + '\n').encode(sys.stdin.encoding))
self._logger.debug("Flushing...")
self._proc.stdin.flush()
except BrokenPipeError:
self._logger.warning("Broken pipe when sending to the process!")
self._close_fds([self._proc.stdin])
raise
time.sleep(sleep)
def wait_for_output(self, timeout=5):
self._logger.info("Waiting for output. Timeout: %s" % timeout)
events = self._poller.poll(timeout * 1000)
if not events:
self._logger.debug("Process generated no output")
self._pending = 0
return None
else:
self._pending = len(self._proc.stdout.peek())
self._logger.debug("Process generated %s" % self._pending)
return self._pending
def wait_until_matched(self, pattern, timeout):
self._logger.info("Waiting until matched. Pattern: %s" % pattern)
assert timeout >= 0, "cannot wait until past times"
deadline = time.time() + timeout
output = ''
while timeout > 0:
self.wait_for_output(timeout)
output += self.read_all()
re_match = re.search(pattern, output)
if re_match:
self._logger.debug("Pattern matched")
return re_match
self._logger.debug("Pattern not matched")
timeout = deadline - time.time()
self._logger.info("Timeout exhausted")
return None
def read_all(self):
self._logger.info("Reading all")
if not self._pending:
self._logger.debug("Nothing to read")
return ''
else:
raw = self._proc.stdout.read(self._pending)
self._pending = 0
decoded = raw.decode(sys.stdout.encoding)
self._logger.debug("Read %s bytes. : %s" % (len(raw), decoded))
return decoded
def write_repeated(self, command, pattern, attempts, timeout):
"""
Write `command`, try matching `pattern` in the response and repeat
for `attempts` times if pattern not matched.
return True if it matched at any point. Or
return False if attempts were depleted and pattern hasn't been matched
"""
self._logger.info("Write repeated called. Attempts: %s" % attempts)
matched = None
while not matched and attempts > 0:
self.writeline(command)
matched = self.wait_until_matched(pattern, timeout)
if matched:
break
attempts -= 1
return matched
@property
def is_running(self):
return self._is_running
def _close_fds(self, fds):
for pipe in fds:
try:
pipe.close()
except BrokenPipeError:
pass