forked from poldrack/python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_shell_cmd.py
33 lines (29 loc) · 1020 Bytes
/
run_shell_cmd.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
"""
utility functions to run shell commands
"""
import subprocess
def run_shell_cmd(cmd,echo=False,cwd=[]):
""" run a command in the shell using Popen
"""
stdout_holder=[]
if cwd:
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,cwd=cwd)
else:
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
for line in process.stdout:
if echo:
print line.strip()
stdout_holder.append(line.strip())
process.wait()
return stdout_holder
def run_logged_cmd(cmd,cmdfile):
outfile=open(cmdfile,'a')
subcode=cmdfile.split('/')[-2]
outfile.write('\n%s: Running:'%subcode+cmd+'\n')
p = sub.Popen(cmd.split(' '),stdout=sub.PIPE,stderr=sub.PIPE)
output, errors = p.communicate()
outfile.write('%s: Output: '%subcode+output+'\n')
if errors:
outfile.write('%s: ERROR: '%subcode+errors+'\n')
print '%s: ERROR: '%subcode+errors
outfile.close()