-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconvience.py
More file actions
58 lines (52 loc) · 1.3 KB
/
convience.py
File metadata and controls
58 lines (52 loc) · 1.3 KB
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
import tempfile
from extproc import Sh, Cmd, Pipe
def here(string):
"""
Make a temporary file from a string for use in redirection.
"""
t = tempfile.TemporaryFile()
t.write(string)
t.seek(0)
return t
def run(cmd, fd={}, e={}, cd=None):
"""
Perform a fork-exec-wait of a Cmd and return its exit status.
"""
return Cmd(cmd, fd=fd, e=e, cd=cd).run()
def cmd(cmd, fd={}, e={}, cd=None):
"""
Perform a fork-exec-wait of a Cmd and return the its stdout
as a byte string.
"""
f = Cmd(cmd, fd=fd, e=e, cd=cd).capture(1).stdout
try:
s = f.read()
finally:
f.close()
return s
def sh(cmd, fd={}, e={}, cd=None):
"""
Perform a fork-exec-wait of a Sh command and return its stdout
as a byte string.
"""
f = Sh(cmd, fd=fd, e=e, cd=cd).capture(1).stdout
try:
s = f.read()
finally:
f.close()
return s
def pipe(*cmds, **kwargs):
"""
Run the pipeline with given Cmd's, then returns its stdout as a byte string.
"""
f = Pipe(*cmds, **kwargs).capture(1).stdout
try:
s = f.read()
finally:
f.close()
return s
def spawn(cmd, fd={}, e={}, cd=None, sh=False):
if sh:
return Sh(cmd, fd=fd, e=e, cd=cd).spawn()
else:
return Cmd(cmd, fd=fd, e=e, cd=cd).spawn()