-
Notifications
You must be signed in to change notification settings - Fork 1
/
extproc.py
executable file
·665 lines (541 loc) · 20.1 KB
/
extproc.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
#!/usr/bin/env python2
#-*- coding: utf-8 -*-
"""
extproc: fork-exec and pipe with I/O redirection
extproc is a layer on top of subprocess. The subprocess module supports
a rich API but is clumsy for many common use cases, namely sync/async
fork-exec, command substitution and pipelining, all of which is trivial
to do on system shells. [1][2]
The goal is to make Python a sane alternative to non-trivial shell scripts.
Features:
* Easy to fork-exec commands, wait or no wait
* Easy to capture stdout/stderr of children (command substitution)
* Easy to express I/O redirections
* Easy to construct pipelines
* Use short names for easy interactive typing
The main interpreter process had better be a single thread, since
forking multithreaded programs is not well understood by mortals. [3]
This module depends on Python 2.6, or where subprocess is available.
Doctests require /bin/sh to pass. Tested on Linux.
This is an alpha release. Expect bugs.
Reference:
[1] sh(1) -- http://heirloom.sourceforge.net/sh/sh.1.html
[2] The Scheme Shell -- http://www.scsh.net/docu/html/man.html
[3] http://golang.org/src/pkg/syscall/exec_unix.go
"""
import collections
import os
import shlex
import subprocess
import sys
import signal
import tempfile
import threading
import time
import py_popen
import pdb
STDIN, STDOUT, STDERR = 0, 1, 2
DEFAULT_FD = {STDIN: 0, STDOUT: 1, STDERR: 2}
SILENCE = {0: os.devnull, 1: os.devnull, 2: os.devnull}
PIPE = subprocess.PIPE # should be -1
_ORIG_STDOUT = subprocess.STDOUT # should be -2
CLOSE = None
JOBS = []
Capture = collections.namedtuple("Capture", "stdout stderr exit_status")
def _is_fileno(n, f):
return (f is n) or (hasattr(f, 'fileno') and f.fileno() == n)
def _name_or_self(f):
return (hasattr(f, 'name') and f.name) or f
class FakeP(object):
pass
class InvalidArgsException(Exception):
pass
class Process(object):
def _check_redirect_target(self, fd_target, fd_dict):
ret_fd_dict = {}
if _is_fileno(fd_target, fd_dict[fd_target]):
ret_fd_dict[fd_target] = tempfile.TemporaryFile()
return ret_fd_dict
else:
raise ValueError(
"cannot capture the child's %d stream: it was redirected to %r"
% (fd_target, _name_or_self(fd_target)))
def _verify_capture_args(self, fd_a, fd_dict):
ret_fd_dict = {}
if fd_a not in [1,2]:
raise NotImplementedError(
"can only capture a subset of fd [1, 2] for now")
ret_fd_dict = self._check_redirect_target(fd_a, fd_dict)
return ret_fd_dict
def _cleanup_capture_dict(self, fd, fd_dict):
if fd == STDOUT:
target = STDERR
else:
target = STDOUT
target_obj = fd_dict[target]
if not _is_fileno(target, target_obj) and isinstance(target_obj, file):
fd_dict[target].close()
def capture(self, *fd, **kwargs):
"""
Fork-exec the Cmd and wait for its termination, capturing the
output and/or error.
:param fd: a list of file descriptors to capture,
should be a subset of [1, 2] where
* 1 represents the child's stdout
* 2 represents the child's stderr
Return a namedtuple (stdout, stderr, exit_status) where
stdout and stderr are captured file objects or None.
Don't forget to close the file objects!
>>> Cmd("/bin/sh -c 'echo -n foo'").capture(1).stdout.read()
'foo'
>>> Cmd("/bin/sh -c 'echo -n bar >&2'").capture(2).stderr.read()
'bar'
"""
if len(fd) == 0:
fd = [1]
for stream_num in fd:
fd_update_dict = self._verify_capture_args(stream_num, self.fd_objs)
self.fd_objs.update(fd_update_dict)
proc_objs = [0]
def runit():
proc_objs[0] = self._popen()
if kwargs.get('timeout'):
timeout = kwargs.get('timeout')
proc_thread = threading.Thread(target=runit)
proc_thread.start()
proc_thread.join(timeout)
kill_timeout = kwargs.get('kill_timeout', 0)
time.sleep(kill_timeout)
self.kill()
else:
runit()
p = proc_objs[0]
if p.fd_objs[STDIN]:
p.fd_objs[STDIN].close()
p.wait()
if not set(fd) == set([1,2]):
self._cleanup_capture_dict(fd[0], p.fd_objs)
for stream_number in fd:
self.fd_objs[stream_number].seek(0)
self.kill()
return Capture(self.fd_objs[1], self.fd_objs[2], p.returncode)
def _process_fd_pair(self, stream_num, fd_descriptor):
"""for now this just does error checking
fd_descriptor is what is passed into the function
"""
if not isinstance(stream_num, int):
raise TypeError("fd keys must have type int")
elif stream_num < 0 or stream_num >= 3:
raise NotImplementedError(
"redirection {%s: %s} not supported" % (
stream_num, fd_descriptor))
if isinstance(fd_descriptor, basestring):
new_fd = open(fd_descriptor, 'r' if stream_num == 0 else 'w')
return new_fd
elif isinstance(fd_descriptor, int):
if stream_num == 2 and fd_descriptor == 1:
return _ORIG_STDOUT
elif (fd_descriptor in (0, 1, 2)):
raise NotImplementedError(
"redirection {%s: %s} not supported"
% (stream_num, fd_descriptor))
return fd_descriptor
elif isinstance(fd_descriptor, file):
return fd_descriptor
else:
assert 1==2, "fd_descriptors must be a string\
stream number or file"
@property
def popen_args(self):
return dict(
args=self.cmd, cwd=self.cd, env=self.env,
stdin=self.fd_objs[0],
stdout=self.fd_objs[1],
stderr=self.fd_objs[2])
def pipe_to(self, cmd_obj):
return Pipe(self, cmd_obj)
def __or__(self, cmd_obj):
return Pipe(self, cmd_obj)
class Cmd(Process):
def _make_cmd(self, cmd_arg):
if isinstance(cmd_arg, basestring):
self.cmd = shlex.split(cmd_arg)
elif isinstance(cmd_arg, (list, tuple)):
self.cmd = cmd_arg
else:
raise TypeError(
"'cmd' argument must be either of type string, list or tuple")
"""
fd_objs is used for processed file descriptor arguments, open file
objects, or number flags
"""
def __init__(self, cmd, fd={}, e={}, cd=None, stdin_data=None):
"""
Prepare for a fork-exec of 'cmd' with information about changing
of working directory, extra environment variables and I/O
redirections if necessary.
:param cmd: a list of command argurments. If a string, it
is passed to shlex.split().
:param e: a dict of *extra* enviroment variables.
:param fd: a dict mapping k in [0, 1, 2] → v of type [file, string, int]
Whatever is pointed to by fd[0], fd[1] and fd[2] will become the
child's stdin, stdout and stderr, respectively.
If any of key [0, 1, 2] is not specified, then it takes the
values [0, 1, 2] respectively -- in effect, reusing the parent's
[stdin, stdout, stderr].
The value fd[k] can be of type
* file: always works and offer the most control over mode of operation
* string: works if can be open()'ed with mode 'r' when k == 0,
or mode 'w' for k in [1, 2]
* int: works for redirection {2: 1}
or {k: v} when v ≥ 3 and v is an existing file descriptor
Note that the constructor only saves information in the object and
does not actually execute anything.
>>> Cmd("/bin/sh -c 'echo foo'")
Cmd(['/bin/sh', '-c', 'echo foo'], fd={0: 0, 1: 1, 2: 2}, e={}, cd=None)
>>> Cmd(['grep', 'my stuff']) == Cmd('grep "my stuff"')
True
"""
self._make_cmd(cmd)
self.cd = cd
self.env = os.environ.copy()
if e:
self.e = e
self.env.update(self.e)
else:
self.e = {}
self.fd_objs = DEFAULT_FD.copy()
self.fd_objs.update(fd)
self.stdin_data = stdin_data
if self.stdin_data:
if STDIN in fd:
raise InvalidArgsException(
"Can't specify a file for STDIN and stdin_data ")
#tf_file_path = tempfile.mktemp()
#self.tf = open(tf_file_path, "w")
#pdb.set_trace()
#self.tf.write(stdin_data)
#self.fd_objs.update({STDIN:open(tf_file_path)})
for stream_num, fd_num in fd.iteritems():
self.fd_objs[stream_num] = self._process_fd_pair(stream_num, fd_num)
def __repr__(self):
return "Cmd(%r, fd=%r, e=%r, cd=%r)" % (
self.cmd,
dict((k, _name_or_self(v)) for k, v in self.fd_objs.iteritems()),
self.e, self.cd)
def __eq__(self, other):
return (self.cmd == other.cmd) and (self.fd_objs == other.fd_objs) and\
(self.env == other.env) and (self.cd == other.cd)
def kill(self):
if not getattr(self, 'p', False):
raise Exception('No process to kill')
try:
return self.p.kill()
except OSError:
pass
finally:
for job in JOBS:
if job is self:
JOBS.remove(self)
def wait(self, func=None):
if not getattr(self, 'p', False):
raise Exception('No process to kill')
try:
return self.p.wait()
finally:
for job in JOBS:
if job is self:
JOBS.remove(self)
if func:
func()
def run(self):
"""
Fork-exec the Cmd and waits for its termination.
Return the child's exit status.
>>> Cmd(['/bin/sh', '-c', 'exit 1']).run()
1
"""
return subprocess.call(**self.popen_args)
def spawn(self, append_to_jobs=True):
"""
Fork-exec the Cmd but do not wait for its termination.
Return a subprocess.Popen object (which is also stored in 'self.p')
"""
if getattr(self, 'p', False):
raise Exception('can only spawn once per cmd object')
self._popen()
if append_to_jobs:
JOBS.append(self)
return self.p
@property
def running_fd_objs(self):
return self.p.fd_objs
@property
def returncode(self):
self.p.poll()
return self.p.returncode
def _popen(self, **kwargs):
basic_popen_args = self.popen_args
basic_popen_args.update(kwargs)
ab = subprocess.Popen(**basic_popen_args)
self.p = decorate_popen(ab)
return self.p
def decorate_popen(popen_obj):
popen_obj.fd_objs = {
STDIN:popen_obj.stdin,
STDOUT:popen_obj.stdout,
STDERR:popen_obj.stderr}
return popen_obj
class Sh(Cmd):
def __init__(self, cmd, fd={}, e={}, cd=None):
"""
Prepare for a fork-exec of a shell command.
Equivalent to Cmd(['/bin/sh', '-c', cmd], **kwargs).
"""
super(Sh, self).__init__(['/bin/sh', '-c', cmd], fd=fd, e=e, cd=cd)
def __repr__(self):
return "Sh(%r, fd=%r, e=%r, cd=%r)" % (self.cmd[2], dict(
(k, _name_or_self(v)) for k, v in self.fd_objs.iteritems()
), self.e, self.cd)
class LiveCapture(object):
def __init__(self, pipe_obj):
self.pipe_obj = pipe_obj
@property
def stdout(self):
if self.returncode is not None:
self.pipe_obj.fd_objs[STDOUT].seek(0)
return self.pipe_obj.fd_objs[STDOUT].read()
@property
def stderr(self):
if self.returncode is not None:
self.pipe_obj.fd_objs[STDERR].seek(0)
return self.pipe_obj.fd_objs[STDERR].read()
@property
def returncode(self):
return self.pipe_obj.returncode
class Pipe(Process):
def __init__(self, *cmds, **kwargs):
"""
Prepare a pipeline from a list of Cmd's.
:parameter e: extra environment variables to be exported to all
sub-commands, must be a keyword argument
"""
self.env = os.environ.copy()
e = kwargs.get('e', {})
if e:
self.e = e
self.env.update(self.e)
else:
self.e = {}
for c in cmds:
c.e.update(self.e)
c.env.update(self.e)
for c in cmds[:-1]:
if _is_fileno(1, c.fd_objs[STDOUT]):
c.fd_objs[STDOUT] = PIPE
self.fd_objs = {STDIN: cmds[0].fd_objs[STDIN],
STDOUT: cmds[-1].fd_objs[STDOUT],
STDERR: cmds[-1].fd_objs[STDERR]}
self.cmds = cmds
self.cmd = "PIPE, not a real command"
self.cd = self.cmds[0].cd
def __repr__(self):
return "Pipe(%s)" % (",\n ".join(map(repr, self.cmds)),)
def run(self):
"""
Fork-exec the pipeline and wait for its termination.
Return an array of all children's exit status.
"""
prev = self.cmds[0].fd_objs[STDIN]
for c in self.cmds:
c._popen(stdin=prev)
prev = c.running_fd_objs[STDOUT]
for c in self.cmds:
c.wait()
for c in self.cmds[:-1]:
if c.fd_objs[STDOUT] == PIPE:
c.running_fd_objs[STDOUT].close()
return self.returncode
@property
def returncode(self):
for c in self.cmds:
if not c.returncode == 0:
return c.returncode
return 0
@property
def returncodes(self):
return [c.returncode for c in self.cmds]
@property
def running_fd_objs(self):
return {STDIN:self.cmds[0].running_fd_objs[STDIN],
STDOUT:self.cmds[-1].running_fd_objs[STDOUT],
STDERR:self.cmds[-1].running_fd_objs[STDERR]}
def spawn(self):
"""
Fork-exec the pipeline but do not wait for its termination.
After spawned, each self.cmd[i] will have a 'p' attribute that is
the spawned subprocess.Popen object.
Remember that all of [c.p.stdout for c in self.cmd] are open files.
"""
if getattr(self, 'p', False):
raise Exception('you can only spawn a Cmd object once')
prev = self.cmds[0].fd_objs[STDIN]
for c in self.cmds[:-1]:
c._popen(stdin=prev, stdout=PIPE)
prev = c.running_fd_objs[STDOUT]
basic_popen_args = self.popen_args
self.cmds[-1]._popen(
stdin=prev,
stdout=basic_popen_args['stdout'],
stderr=basic_popen_args['stderr'])
JOBS.append(self)
return self
def kill(self):
try:
for c in self.cmds:
c.kill()
finally:
for job in JOBS:
if job is self:
JOBS.remove(self)
def wait(self, func=None):
try:
return self.cmds[-1].wait()
finally:
for job in JOBS:
if job is self:
JOBS.remove(self)
if func:
func()
def _capture_core(self, *fd, **kwargs):
"""
like capture except this returns immediately.
"""
if len(fd) == 0:
fd = [1]
for descriptor in fd:
fd_update_dict = self._verify_capture_args(descriptor, self.fd_objs)
self.fd_objs.update(fd_update_dict)
if STDERR in fd:
self.fd_objs[STDERR] = tempfile.TemporaryFile()
def runit():
## start piping
prev = self.cmds[0].fd_objs[0]
for c in self.cmds[:-1]:
if not _is_fileno(STDIN, c.fd_objs[STDIN]):
prev = c.fd_objs[STDIN]
if STDERR in fd and _is_fileno(STDERR, c.fd_objs[STDERR]):
c.fd_objs[STDERR] = self.fd_objs[STDERR]
c._popen(stdin=prev)
prev = c.running_fd_objs[STDOUT]
## prepare and fork the last child
c = self.cmds[-1]
if not _is_fileno(STDIN, c.fd_objs[STDIN]):
prev = c.fd_objs[STDIN]
if STDOUT in fd:
## we made sure that c.fd[STDOUT] had not been redirected before
c.fd_objs[STDOUT] = tempfile.TemporaryFile()
self.fd_objs[STDOUT] = c.fd_objs[STDOUT]
if STDERR in fd and _is_fileno(STDERR, c.fd_objs[STDERR]):
c.fd_objs[STDERR] = self.fd_objs[STDERR]
c._popen(stdin=prev)
def cleanup():
## close all unneeded files
for c in self.cmds[:-1]:
if c.fd_objs[STDOUT] == PIPE:
c.running_fd_objs[STDOUT].close()
if not set(fd) == set([1,2]):
self._cleanup_capture_dict(fd[0], self.fd_objs)
for descriptor in fd:
#self.running_fd_objs[descriptor].seek(0)
self.fd_objs[descriptor].seek(0)
return runit, cleanup
def capture(self, *fd, **kwargs):
runit, cleanup = self._capture_core(*fd, **kwargs)
if kwargs.get('timeout'):
timeout = kwargs.get('timeout')
proc_thread = threading.Thread(target=runit)
proc_thread.start()
proc_thread.join(timeout)
kill_timeout = kwargs.get('kill_timeout', 0)
time.sleep(kill_timeout)
self.kill()
else:
runit()
#we only need to wait on the last in the pipeline, the rest
#will die off, and since the point of capture is to grab the
#output, once the last cmd is dead, there can be no more output
self.cmds[-1].wait()
## close all unneeded files
cleanup()
self.kill()
return Capture(
self.fd_objs[STDOUT],
self.fd_objs[STDERR],
self.cmds[-1].returncode)
def capture_spawn(self, *fd, **kwargs):
runit, fd = self._capture_core(*fd, **kwargs)
if kwargs.get('timeout'):
timeout = kwargs.get('timeout')
proc_thread = threading.Thread(target=runit)
proc_thread.start()
proc_thread.join(timeout)
kill_timeout = kwargs.get('kill_timeout', 0)
time.sleep(kill_timeout)
self.kill()
else:
runit()
JOBS.append(self)
return LiveCapture(self)
def _popen(self, **kwargs):
"""
Fork-exec the pipeline and wait for its termination.
Return an array of all children's exit status.
"""
basic_popen_args = self.popen_args
basic_popen_args.update(kwargs)
prev = basic_popen_args['stdin']
for c in self.cmds[:-1]:
c._popen(stdin=prev, stdout=PIPE)
prev = c.running_fd_objs[STDOUT]
self.cmds[-1]._popen(
stdin=prev,
stdout=basic_popen_args['stdout'])
class PythonProc(Cmd):
def __init__(self, py_func, fd={}, e={}, cd=None):
"""
"""
self.py_func = py_func
self.cd = cd
self.e = e
self.env = os.environ.copy()
self.env.update(e)
self.fd_objs = DEFAULT_FD.copy()
self.fd_objs.update(fd)
for stream_num, fd_num in fd.iteritems():
self.fd_objs[stream_num] = self._process_fd_pair(stream_num, fd_num)
@property
def popen_args(self):
return dict(
py_func=self.py_func, cwd=self.cd, env=self.env,
stdin=self.fd_objs[0],
stdout=self.fd_objs[1],
stderr=self.fd_objs[2])
def _popen(self, **kwargs):
basic_popen_args = self.popen_args
basic_popen_args.update(kwargs)
ab = py_popen.PyPopen(**basic_popen_args)
self.p = decorate_popen(ab)
return self.p
def fork_dec(f):
return PythonProc(f)
def make_echoer(data_string):
@fork_dec
def echo_f(stdin, stdout, stderr):
stdout.write(data_string)
return echo_f
if __name__ == '__main__':
import doctest
n = doctest.testmod().failed
if n > 0:
sys.exit(n)