-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.py
53 lines (42 loc) · 1.61 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
import subprocess
import sys
if sys.version_info < (3, 0):
# python2.7 version
def execute_shell(cmd, input=''):
"""
:param cmd:
:param input: sent to sdtin
:return: returncode, stdout, stderr.
"""
proc_stdin = subprocess.PIPE if input != '' else None
proc_input = input if input != '' else None
p = subprocess.Popen(cmd,
stdin=proc_stdin,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True, # so we can use piping | in cmd
universal_newlines=True) # so we communicate using strings (instead of bytes)
if proc_input:
out, err = p.communicate(proc_input)
else:
out, err = p.communicate()
return p.returncode, out, err
else:
def execute_shell(cmd, input=''):
"""
:param cmd:
:param input: sent to sdtin
:return: returncode, stdout, stderr.
"""
proc_stdin = subprocess.PIPE if input != '' else None
proc_input = input if input != '' else None
p = subprocess.Popen(cmd,
stdin=proc_stdin,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True)
if proc_input:
out, err = p.communicate(bytes(proc_input, encoding='utf-8'))
else:
out, err = p.communicate()
return p.returncode, str(out, encoding='utf-8'), str(err, encoding='utf-8')