-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpysh.py
54 lines (51 loc) · 1.35 KB
/
mpysh.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
#!/usr/bin/env python3
import os
# execute commands
def exec_comm(com, com_, exists):
# check for line end and and statements
split = com.split(';')
split2 = com.split('&&')
if len(split) > 1:
print("mpysh: don't use ; in commands.")
elif len(split2) > 1:
print("mpysh: don't use && in commands.")
# exec builtin
elif com_ == "exec":
os.system("~/mpysh-bin/" + com)
# normal command
elif exists == True:
os.system(com)
# command does not exist
else:
print("mpysh: " + com_ + ": not found")
# exists in path helper
def exists_in_system_path(name):
from distutils.spawn import find_executable
return find_executable(name) is not None
# home var
home = os.getenv("HOME")
while True:
# set prompt
pwd = os.getcwd()
prompt = pwd
if pwd == home:
prompt = "~"
com = input(prompt + " $ ")
# misc
com_ = com.split(" ")
exists = exists_in_system_path(com_[0])
if not exists == False:
exists == True
# cd command
if com_[0] == "cd":
change = com_[1].replace("~", home)
try:
os.chdir(change)
except:
print(com_[1] + " does not exist or is not a directory")
# exit command
elif com_[0] == "exit":
break
else:
# execute command
exec_comm(com, com_[0], exists)