-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell_utils.py
More file actions
127 lines (115 loc) · 4.25 KB
/
Copy pathshell_utils.py
File metadata and controls
127 lines (115 loc) · 4.25 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
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
from sultan.api import Sultan
import subprocess
import time
from collections import deque
import os
from . import consts
class shellRunner:
def __init__(self):
pass
def feed(self, command): # string.split()
self._process_command(command)
def get_bash_var(self, var):
return os.environ.get(var)
def run_script(self, script):
try:
subprocess.check_call(script, shell=True, executable='/bin/bash')
except subprocess.CalledProcessError as ex:
print("command failed: ", script)
return 1
return 0
def _process_command(self, command):
if self._is_complex(command):
return self._process_complex(command)
return self._process_one_command(command)
def _process_one_command(self, command):
command = command.split()
streaming = False
output = subprocess.run('which ' + command[0],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True)
path = output.stdout.decode('utf-8').strip()
if path and command[0] in consts.DISPLAY:
try:
subprocess.check_call(command)
except subprocess.CalledProcessError:
print("[pyshell] command failed...")
return 1
except KeyboardInterrupt:
print("keyboard interupted")
return 0
if command[0] == 'export':
if len(command) < 2:
print("exporting nothing is not allowed")
return
split_cmd = command[1].split('=')
if len(split_cmd) > 1:
os.environ[split_cmd[0]] = split_cmd[1]
idx = 1
elif '=' in command[0]:
split_cmd = command[0].split('=')
if len(split_cmd) < 2:
print("Error: cannot set = to nothing")
return 1
os.environ[split_cmd[0]] = split_cmd[1]
return 0
elif 'cd' in command[0]:
if len(command) < 2:
print('cannot change dir to nothing')
return
os.chdir(command[1])
return 0
elif 'pip' in command[0]:
if 'VIRTUAL_ENV' in os.environ:
s = Sultan.load(src=os.path.join(os.environ['VIRTUAL_ENV'], 'bin', 'activate'),
executable='/bin/bash')
else:
s = Sultan.load()
streaming = True
bas_cmd = 'pip'
idx = 1
else:
if command[0] in consts.STREAMING:
streaming = True
bas_cmd = command[1] if command[0] == 'sudo' else command[0]
s = Sultan.load(sudo=True if command[0] == 'sudo' else False)
idx = 2 if command[0] == 'sudo' else 1
if bas_cmd == 'apt-get':
bas_cmd = 'apt__get'
options = ''
while idx < len(command):
options += command[idx] + " "
idx += 1
if streaming:
cmd_string = 'global result; result = s.' + bas_cmd + '(\'' + options + '\')' + '.run(streaming=True)'
try:
exec(cmd_string, globals(), locals())
complete = False
while not complete:
complete = result.is_complete
for line in result.stdout:
print(line)
for line in result.stderr:
print(line)
time.sleep(.5)
except Exception as e:
print(e)
print("command failed!")
return 1
except KeyboardInterrupt:
print("keyboard interupted")
else:
cmd_string = 'global result; result = s.' + bas_cmd + '(\'' + options + '\')' + '.run()'
try:
exec(cmd_string, globals(), locals())
result.print_stdout()
except Exception as e:
print(e)
print("command failed!")
return 1
def _is_complex(self, command):
return False
# TODO: add complex bash command handling
def _process_complex(self, command):
pass