-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexternalprocess.py
More file actions
58 lines (46 loc) · 1.49 KB
/
externalprocess.py
File metadata and controls
58 lines (46 loc) · 1.49 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 logging
import tempfile
import subprocess
import os
class CommandRunError(Exception):
pass
def getCommandOut(cmd):
"""
cmd - command to execute
gathers output of command (stderr and stdout) into a temp file
returns the output of the command
"""
logging.debug('starting %s' % cmd)
temp = tempfile.TemporaryFile('w+t')
try:
p = subprocess.Popen(cmd.split(), stderr=subprocess.STDOUT, stdout=temp.fileno())
#pid, status = os.waitpid(p.pid,0) #@UnusedVariable
status = p.wait()
temp.seek(0)
out = temp.read()
if status != 0:
raise CommandRunError("COMMAND: %s\tFAILED: %s%s%s" % (cmd, status, os.linesep, out))
logging.debug('finished %s' % cmd)
finally:
temp.close()
return out
def getPipedCommandOut(cmd):
"""
cmd - command to execute
gathers output of command (stderr and stdout) into a temp file
returns the output of the command
"""
logging.debug('starting %s' % cmd)
temp = tempfile.TemporaryFile('w+t')
try:
p = subprocess.Popen(cmd, stderr=subprocess.STDOUT, stdout=temp.fileno(), shell=True)
#pid, status = os.waitpid(p.pid,0) #@UnusedVariable
status = p.wait()
temp.seek(0)
out = temp.read()
if status != 0:
raise CommandRunError("COMMAND: %s\tFAILED: %s%s%s" % (cmd, status, os.linesep, out))
logging.debug('finished %s' % cmd)
finally:
temp.close()
return out