-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshell.py
executable file
·276 lines (224 loc) · 7.73 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/env python3
import json
import os
import platform
import subprocess
import sys
import shlex
import time
import readline
import hushExtLoader
# Constants
HISTORY_FILE = os.path.expanduser("~/.hush_history")
RC_FILE = os.path.expanduser("~/.hushrc")
DEFAULT_THEME = "personalize.colored_bash"
WINDOWS_PLATFORM = "Windows"
# Global Variables
registry = {}
startTime = time.time()
startTime_formatted = time.ctime()
themeSet = DEFAULT_THEME
homePath = os.path.expanduser("~")
last_changed_work_dir = ''
last_completer_list = []
hushLog = []
pathChar = ':'
enableCompleter = False
if platform.system() == WINDOWS_PLATFORM:
pathChar = ';'
print("Auto-completion has been disabled because this system is Windows.")
else:
enableCompleter = True
def changeDirectory(path: str):
"""Changes the current working directory."""
global last_changed_work_dir
try:
targetPath = homePath if path == "~" else path
os.chdir(targetPath)
last_changed_work_dir = targetPath
except FileNotFoundError:
print(f"hush: cd: no such file or directory: {path}")
except PermissionError:
print(f"hush: cd: permission denied: {path}")
def processVariable(command: str):
"""Replaces variables in a command string with their values from the registry."""
for key, value in registry.items():
command = command.replace(f"${key}", value)
return command
def completer(text, state):
"""Provides tab-completion suggestions."""
global last_completer_list
commands = os.listdir()
matches = []
if text:
for cmd in commands:
if cmd.lower().startswith(text.lower()):
matches.append(cmd)
customCommands = [
'_ext_list_plugins',
'_ext_list_theme',
'_ext_list_commands',
'_ext_list_functions',
'_ext_set_theme',
'_ext_set_quiet',
'_list_var',
'_checkfile',
'_dump',
'cd',
'exit',
'quit',
'export',
]
for customCommand in customCommands:
if customCommand.lower().startswith(text.lower()):
matches.append(customCommand)
for pluginCommand in hushExtLoader.registeredCommands:
if pluginCommand.lower().startswith(text.lower()):
matches.append(pluginCommand)
else:
matches = commands
last_completer_list = matches
try:
return matches[state] if matches else None
except IndexError:
return None
def writeHistory(string):
"""Appends a command to the history file and in-memory log."""
timestamp = int(startTime)
formattedTime = time.ctime()
history_entry = f"Session [{timestamp}] at {formattedTime}: {string}\n"
with open(HISTORY_FILE, "a+") as f:
f.write(history_entry)
hushLog.append(history_entry)
def findExecutable(filename):
"""Finds the full path to an executable file."""
if platform.system() == WINDOWS_PLATFORM:
filename += ".exe"
searchPaths = registry.get('PATH', '').split(pathChar) + [os.getcwd()]
for path in searchPaths:
full_path = os.path.join(path, filename)
if os.path.exists(full_path):
return full_path
return False
def exit():
"""Exits the shell."""
writeHistory("Exiting...")
sys.exit()
def isSerializable(obj):
"""Checks if an object can be serialized to JSON."""
try:
json.dumps(obj)
return True
except TypeError:
return False
readline.set_completer(completer)
readline.parse_and_bind("tab: complete")
def execute(shinput: str, args: list = []):
global themeSet
if not shinput:
return 0
pluginCommand = hushExtLoader.registeredCommands.get(args[0], None)
executablePath = findExecutable(args[0])
if pluginCommand:
pluginCommand(args)
elif shinput in ("quit", "exit"):
exit()
elif shinput.startswith("cd"):
targetPath = shinput.split(" ", 1)[-1] if " " in shinput else "~"
changeDirectory(targetPath)
elif shinput.startswith("_ext_set_theme"):
themeSet = shinput.split(" ")[-1]
elif shinput == "_list_var":
for key, value in registry.items():
print(f"{key}: {value}")
elif shinput == "_ext_list_plugins":
for plugin in hushExtLoader.loadedPlugins:
print(plugin)
elif shinput == '_ext_list_commands':
for command in hushExtLoader.registeredCommands:
print(command)
elif shinput == '_ext_list_functions':
for function in hushExtLoader.allFoundFunctions:
print(function)
elif shinput.startswith("__ext_list_theme"):
print("List of theme available:")
for Name, Preview in hushExtLoader.themes.items():
print(f'\nTheme "{Name}" preview:\n{Preview}\n')
elif shinput.startswith("_dump"):
nowTime = time.time()
globals_filtered = {
key: value for key, value in globals().items() if isSerializable(value)
}
globals_filtered.update({
'systemType': platform.system(),
'systemVersion': platform.release(),
'systemArch': platform.machine(),
'pythonVersion': platform.python_version(),
'dumpTime': nowTime,
'dumpTimeFormated': time.ctime(),
'hushExtLoaderLog': hushExtLoader.Dump()
})
filename = f"dump_{int(nowTime)}.json"
print(f"Dumped to {filename}")
with open(filename, "w") as f:
json.dump(globals_filtered, f, indent=4, ensure_ascii=False)
elif shinput.startswith("export"):
args = shlex.split(shinput)
if len(args) > 1 and "=" in args[1]:
key, value = args[1].split("=", 1)
registry[key] = value.strip()
else:
for i in registry:
print(f'{i}={registry[i]}')
elif shinput.startswith("_checkfile "):
filename = shinput.split(" ")[1]
print(findExecutable(filename))
elif executablePath:
try:
process_return = subprocess.run(
args, env=registry, shell=False)
writeHistory(
f'Executed "{shinput}", return code: {process_return.returncode}.')
if process_return.stderr:
print(f"Error: {process_return.stderr}")
except Exception as e:
print(f"hush: {e}")
writeHistory(f'Executed "{shinput}", but an error occurred: {e}.')
else:
print(f"hush: {args[0]}: not found")
writeHistory(f'Executed "{shinput}", but this command is not found.')
def main():
"""Main shell loop."""
global themeSet, startTime_formatted
registry.update(os.environ) # load system variable
registry['SHELL'] = '/usr/bin/hush'
hushExtLoader.Load()
hushExtLoader.runPluginInit()
try:
writeHistory(f"A new session start by using {os.ttyname(0)}.")
except AttributeError:
writeHistory("A new session start by using cmd.")
if os.path.exists(RC_FILE):
with open(RC_FILE, 'r+') as f:
lines = f.read()
for line in lines.splitlines():
args = shlex.split(line)
execute(line, args)
while True:
hushExtLoader.runPluginPreHook()
hushExtLoader.themeRefresh()
theme = hushExtLoader.themes.get(themeSet, " $")
try:
shinput = processVariable(input(theme))
writeHistory(f'Executing "{shinput}".')
args = shlex.split(shinput)
except EOFError:
print()
exit()
hushExtLoader.runPluginAfterHook()
execute(shinput, args)
try:
main()
except KeyboardInterrupt:
print("\n")
pass