-
Notifications
You must be signed in to change notification settings - Fork 37
/
ostools.py
350 lines (279 loc) · 10.1 KB
/
ostools.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#******************************************************************************
# * Copyright (c) 2019, XtremeDV. All rights reserved.
# *
# * Licensed under the Apache License, Version 2.0 (the "License");
# * you may not use this file except in compliance with the License.
# * You may obtain a copy of the License at
# *
# * http://www.apache.org/licenses/LICENSE-2.0
# *
# * Unless required by applicable law or agreed to in writing, software
# * distributed under the License is distributed on an "AS IS" BASIS,
# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# * See the License for the specific language governing permissions and
# * limitations under the License.
# *
# * Author: Jude Zhang, Email: zhajio.1988@gmail.com
# *******************************************************************************
# Copyright (c) 2014-2018, Lars Asplund lars.anders.asplund@gmail.com
"""
Provides operating systems dependent functionality that can be easily
stubbed for testing
"""
from __future__ import print_function
import time
import subprocess
import threading
import psutil
import shutil
import sys
import signal
try:
# Python 3.x
from queue import Queue, Empty
except ImportError:
# Python 2.7
from Queue import Queue, Empty # pylint: disable=import-error
from os.path import exists, getmtime, dirname, relpath, splitdrive
import os
import io
import logging
LOGGER = logging.getLogger(__name__)
class ProgramStatus(object):
"""
Maintain global program status to support graceful shutdown
"""
def __init__(self):
self._lock = threading.Lock()
self._shutting_down = False
@property
def is_shutting_down(self):
with self._lock: # pylint: disable=not-context-manager
return self._shutting_down
def check_for_shutdown(self):
if self.is_shutting_down:
raise KeyboardInterrupt
def shutdown(self):
with self._lock: # pylint: disable=not-context-manager
LOGGER.debug("ProgramStatus.shutdown")
self._shutting_down = True
def reset(self):
with self._lock: # pylint: disable=not-context-manager
self._shutting_down = False
PROGRAM_STATUS = ProgramStatus()
class InterruptableQueue(object):
"""
A Queue which can be interrupted
"""
def __init__(self):
self._queue = Queue()
def get(self):
"""
Get a value from the queue
"""
while True:
PROGRAM_STATUS.check_for_shutdown()
try:
return self._queue.get(timeout=0.1)
except Empty:
pass
def put(self, value):
self._queue.put(value)
def empty(self):
return self._queue.empty()
class Process(object):
"""
A simple process interface which supports asynchronously consuming the stdout and stderr
of the process while it is running.
"""
class NonZeroExitCode(Exception):
pass
def __init__(self, cmd, cwd=None, env=None):
self._cmd = cmd
self._cwd = cwd
# Create process with new process group
# Sending a signal to a process group will send it to all children
# Hopefully this way no orphaned processes will be left behind
self._process = subprocess.Popen(
self._cmd,
cwd=self._cwd,
stdout=subprocess.PIPE,
stdin=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True,
shell=True,
bufsize=0,
# Create new process group on POSIX, setpgrp does not exist on Windows
#preexec_fn=os.setsid)
preexec_fn=os.setpgrp) # pylint: disable=no-member
LOGGER.debug("Started process with pid=%i: '%s'", self._process.pid, (" ".join(self._cwd)))
self._queue = InterruptableQueue()
self._reader = AsynchronousFileReader(self._process.stdout, self._queue)
self._reader.start()
def write(self, *args, **kwargs):
""" Write to stdin """
if not self._process.stdin.closed:
self._process.stdin.write(*args, **kwargs)
def writeline(self, line):
""" Write a line to stdin """
if not self._process.stdin.closed:
self._process.stdin.write(line + "\n")
self._process.stdin.flush()
def next_line(self):
"""
Return either the next line or the exit code
"""
if not self._reader.eof():
# Show what we received from standard output.
msg = self._queue.get()
if msg is not None:
return msg
retcode = self.wait()
return retcode
def wait(self):
"""
Wait while without completely blocking to avoid
deadlock when shutting down
"""
while self._process.poll() is None:
PROGRAM_STATUS.check_for_shutdown()
time.sleep(0.05)
LOGGER.debug("Waiting for process with pid=%i to stop", self._process.pid)
return self._process.returncode
def is_alive(self):
"""
Returns true if alive
"""
return self._process.poll() is None
def consume_output(self, callback=print):
"""
Consume the output of the process.
The output is interpreted as UTF-8 text.
@param callback Called for each line of output
@raises Process.NonZeroExitCode when the process does not exit with code zero
"""
def default_callback(*args, **kwargs):
pass
if not callback:
callback = default_callback
while not self._reader.eof():
line = self._queue.get()
if line is None:
break
if callback(line) is not None:
return
retcode = None
while retcode is None:
retcode = self.wait()
if retcode != 0:
raise Process.NonZeroExitCode
def terminate(self):
"""
Terminate the process
"""
if self._process.poll() is None:
process = psutil.Process(self._process.pid)
proc_list = process.children(recursive=True)
#proc_list.reverse()
for proc in proc_list:
proc.kill()
process.kill()
# Let's be tidy and join the threads we've started.
if self._process.poll() is None:
LOGGER.debug("Terminating process with pid=%i", self._process.pid)
self._process.terminate()
if self._process.poll() is None:
time.sleep(0.05)
if self._process.poll() is None:
LOGGER.debug("Killing process with pid=%i", self._process.pid)
self._process.kill()
if self._process.poll() is None:
LOGGER.debug("Waiting for process with pid=%i", self._process.pid)
self.wait()
LOGGER.debug("Process with pid=%i terminated with code=%i",
self._process.pid,
self._process.returncode)
self._reader.join()
self._process.stdout.close()
self._process.stdin.close()
def __del__(self):
try:
self.terminate()
except KeyboardInterrupt:
LOGGER.debug("Process.__del__: Ignoring KeyboardInterrupt")
class AsynchronousFileReader(threading.Thread):
"""
Helper class to implement asynchronous reading of a file
in a separate thread. Pushes read lines on a queue to
be consumed in another thread.
"""
def __init__(self, fd, queue, encoding="utf-8"):
threading.Thread.__init__(self)
# If Python 3 change encoding of TextIOWrapper to utf-8 ignoring decode errors
if isinstance(fd, io.TextIOWrapper):
fd = io.TextIOWrapper(fd.buffer, encoding=encoding, errors="ignore")
self._fd = fd
self._queue = queue
self._encoding = encoding
def run(self):
"""The body of the thread: read lines and put them on the queue."""
for line in iter(self._fd.readline, ""):
if PROGRAM_STATUS.is_shutting_down:
break
# Convert string into utf-8 if necessary
if sys.version_info.major == 2:
string = line[:-1].decode(encoding=self._encoding, errors="ignore")
else:
string = line[:-1]
self._queue.put(string)
self._queue.put(None)
def eof(self):
"""Check whether there is no more content to expect."""
return not self.is_alive() and self._queue.empty()
def read_file(file_name, encoding="utf-8", newline=None):
""" To stub during testing """
try:
with io.open(file_name, "r", encoding=encoding, newline=newline) as file_to_read:
data = file_to_read.read()
except UnicodeDecodeError:
LOGGER.warning("Could not decode file %s using encoding %s, ignoring encoding errors",
file_name, encoding)
with io.open(file_name, "r", encoding=encoding, errors="ignore", newline=newline) as file_to_read:
data = file_to_read.read()
return data
def write_file(file_name, contents, encoding="utf-8"):
""" To stub during testing """
path = dirname(file_name)
if path == "":
path = "."
if not file_exists(path):
os.makedirs(path)
with io.open(file_name, "wb") as file_to_write:
file_to_write.write(contents.encode(encoding=encoding))
def file_exists(file_name):
""" To stub during testing """
return exists(file_name)
def get_modification_time(file_name):
""" To stub during testing """
return getmtime(file_name)
def get_time():
""" To stub during testing """
return time.time()
def renew_path(path):
"""
Ensure path directory exists and is empty
"""
if exists(path):
shutil.rmtree(path)
os.makedirs(path)
def simplify_path(path):
"""
Return relative path towards current working directory
unless it is a separate Windows drive
"""
cwd = os.getcwd()
drive_cwd = splitdrive(cwd)[0]
drive_path = splitdrive(path)[0]
if drive_path == drive_cwd:
return relpath(path, cwd)
return path