-
Notifications
You must be signed in to change notification settings - Fork 51
/
shell.py
357 lines (270 loc) · 10.4 KB
/
shell.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
351
352
353
354
355
356
357
"""
shell
=====
A better way to run shell commands in Python.
If you just need to quickly run a command, you can use the ``shell`` shortcut
function::
>>> from shell import shell
>>> ls = shell('ls')
>>> for file in ls.output():
... print file
'another.txt'
If you need to extend the behavior, you can also use the ``Shell`` object::
>>> from shell import Shell
>>> sh = Shell(has_input=True)
>>> cat = sh.run('cat -u')
>>> cat.write('Hello, world!')
>>> cat.output()
['Hello, world!']
"""
import shlex
import subprocess
import sys
__author__ = 'Daniel Lindsley'
__license__ = 'New BSD'
__version__ = (1, 0, 1)
class ShellException(Exception):
"""The base exception for all shell-related errors."""
pass
class MissingCommandException(ShellException):
"""Thrown when no command was setup."""
pass
class CommandError(ShellException):
"""Thrown when a command fails."""
def __init__(self, message, code, stderr):
self.message = message
self.code = code
self.stderr = stderr
super(CommandError, self).__init__(message)
class Shell(object):
"""
Handles executing commands & recording output.
Optionally accepts a ``has_input`` parameter, which should be a boolean.
If set to ``True``, the command will wait to execute until you call the
``Shell.write`` method & send input. (Default: ``False``)
Optionally accepts a ``record_output`` parameter, which should be a boolean.
If set to ``True``, the stdout from the command will be recorded.
(Default: ``True``)
Optionally accepts a ``record_errors`` parameter, which should be a boolean.
If set to ``True``, the stderr from the command will be recorded.
(Default: ``True``)
Optionally accepts a ``strip_empty`` parameter, which should be a boolean.
If set to ``True``, only non-empty lines from ``Shell.output`` or
``Shell.errors`` will be returned. (Default: ``True``)
Optionally accepts a ``die`` parameter, which should be a boolean.
If set to ``True``, raises a CommandError if the command exits with a
non-zero return code. (Default: ``False``)
Optionally accepts a ``verbose`` parameter, which should be a boolean.
If set to ``True``, prints stdout to stdout and stderr to stderr as
execution happens. (Default: ``False``)
"""
def __init__(self, has_input=False, record_output=True, record_errors=True,
strip_empty=True, die=False, verbose=False):
self.has_input = has_input
self.record_output = record_output
self.record_errors = record_errors
self.strip_empty = strip_empty
self.die = die
self.verbose = verbose
self.last_command = ''
self.line_breaks = '\n'
self.pid = None
self.code = 0
self._popen = None
self._stdout = ''
self._stderr = ''
def _split_command(self, command):
"""
Splits a string command into the individual args to pass to ``Popen``.
If the ``command`` is an array, it is left untouched.
"""
if isinstance(command, (tuple, list)):
return command
return shlex.split(command)
def _handle_output(self, stdout, stderr):
"""
Takes stdout/stderr & optionally retains them internally.
Requires a ``stdout`` parameter, which should either be the output as
a string or ``None``.
Requires a ``stderr`` parameter, which should either be the errors as
a string or ``None``.
Records nothing if the ``record_*`` options have been set to ``False``.
"""
if stdout != None:
if self.record_output:
self._stdout += stdout
if self.verbose:
sys.stdout.write(stdout)
sys.stdout.flush()
if stderr != None:
if self.record_errors:
self._stderr += stderr
if self.verbose:
sys.stderr.write(stderr)
sys.stderr.flush()
def _communicate(self, the_input=None):
"""
Handles communicating with the process & optionally sends input.
Optionally accepts a ``the_input`` parameter, which can be a string
to send to the process. (Default: ``None``)
"""
stdout, stderr = self._popen.communicate(input=the_input)
self._handle_output(stdout, stderr)
if self._popen.returncode is not None:
self.code = self._popen.returncode
if self.die and self.code != 0:
raise CommandError(
message='Command exited with code {}'.format(self.code),
code=self.code,
stderr=stderr)
def run(self, command):
"""
Runs a given command.
Requires a ``command`` parameter should be either a string command
(easier) or an array of arguments to send as the command (if you know
what you're doing).
Returns the ``Shell`` instance.
Example::
>>> from shell import Shell
>>> sh = Shell()
>>> sh.run('ls -alh')
"""
self.last_command = command
command_bits = self._split_command(command)
kwargs = {
'stdout': subprocess.PIPE,
'stderr': subprocess.PIPE,
'universal_newlines': True,
}
if self.has_input:
kwargs['stdin'] = subprocess.PIPE
self._popen = subprocess.Popen(
command_bits,
**kwargs
)
self.pid = self._popen.pid
if not self.has_input:
self._communicate()
return self
def write(self, the_input):
"""
If you're working with an interactive process, sends that input to
the process.
This needs to be used in conjunction with the ``has_input=True``
parameter.
Requires a ``the_input`` parameter, which should be a string of the
input to send to the command.
Returns the ``Shell`` instance.
Example::
>>> from shell import Shell
>>> sh = Shell(has_input=True)
>>> sh.run('cat -u')
>>> sh.write('Hello world!')
"""
if not self._popen:
raise MissingCommandException(
"No command has been provided. Please call ``run`` first."
)
self._communicate(the_input)
return self
def kill(self):
"""
Kills a given process.
Example::
>>> from shell import Shell
>>> sh = Shell()
>>> sh.run('some_long_running_thing')
>>> sh.kill()
"""
if not self._popen:
raise MissingCommandException(
"No command has been provided. Please call ``run`` first."
)
self._popen.kill()
return self
def output(self, raw=False):
"""
Returns the output from running a command.
Optionally accepts a ``raw`` parameter, which should be a boolean. If
``raw`` is set to ``False``, you get an array of lines of output. If
``raw`` is set to ``True``, the raw string of output is returned.
(Default: ``False``)
Example::
>>> from shell import Shell
>>> sh = Shell()
>>> sh.run('ls ~')
>>> sh.output()
[
'hello.txt',
'world.txt',
]
"""
if raw:
return self._stdout
lines = self._stdout.split(self.line_breaks)
if self.strip_empty:
lines = [line for line in lines if line]
return lines
def errors(self, raw=False):
"""
Returns the errors from running a command.
Optionally accepts a ``raw`` parameter, which should be a boolean. If
``raw`` is set to ``False``, you get an array of lines of errors. If
``raw`` is set to ``True``, the raw string of errors is returned.
(Default: ``False``)
Example::
>>> from shell import Shell
>>> sh = Shell()
>>> sh.run('ls /there-s-no-way-anyone/has/this/directory/please')
>>> sh.errors()
[
'ls /there-s-no-way-anyone/has/this/directory/please: No such file or directory'
]
"""
if raw:
return self._stderr
lines = self._stderr.split(self.line_breaks)
if self.strip_empty:
lines = [line for line in lines if line]
return lines
def shell(command, has_input=False, record_output=True, record_errors=True,
strip_empty=True, die=False, verbose=False):
"""
A convenient shortcut for running commands.
Requires a ``command`` parameter should be either a string command
(easier) or an array of arguments to send as the command (if you know
what you're doing).
Optionally accepts a ``has_input`` parameter, which should be a boolean.
If set to ``True``, the command will wait to execute until you call the
``Shell.write`` method & send input. (Default: ``False``)
Optionally accepts a ``record_output`` parameter, which should be a boolean.
If set to ``True``, the stdout from the command will be recorded.
(Default: ``True``)
Optionally accepts a ``record_errors`` parameter, which should be a boolean.
If set to ``True``, the stderr from the command will be recorded.
(Default: ``True``)
Optionally accepts a ``strip_empty`` parameter, which should be a boolean.
If set to ``True``, only non-empty lines from ``Shell.output`` or
``Shell.errors`` will be returned. (Default: ``True``)
Optionally accepts a ``die`` parameter, which should be a boolean.
If set to ``True``, raises a CommandError if the command exits with a
non-zero return code. (Default: ``False``)
Optionally accepts a ``verbose`` parameter, which should be a boolean.
If set to ``True``, prints stdout to stdout and stderr to stderr as
execution happens. (Default: ``False``)
Returns the ``Shell`` instance, which has been run with the given command.
Example::
>>> from shell import shell
>>> sh = shell('ls -alh *py')
>>> sh.output()
['hello.py', 'world.py']
"""
sh = Shell(
has_input=has_input,
record_output=record_output,
record_errors=record_errors,
strip_empty=strip_empty,
die=die,
verbose=verbose
)
return sh.run(command)