-
Notifications
You must be signed in to change notification settings - Fork 0
/
intek-sh.py
executable file
·165 lines (147 loc) · 4.63 KB
/
intek-sh.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
#!/usr/bin/env python3
from os import environ, chdir, getcwd, path
import sys
import subprocess
def call_subprocess(command, args):
"""
Call subprocess from external binaries
@param: command: command name
@param: args: command's arguments
"""
if "./" in command:
try:
subprocess.call([command] + args)
except FileNotFoundError:
print("intek-sh: " + command + "No such file or directory")
except PermissionError:
print("intek-sh: " + command + ": Permission denied")
elif 'PATH' in environ.keys():
sub_paths = environ["PATH"].split(":")
isFound = False
for sub_path in sub_paths:
command_path = sub_path + "/" + command
if path.exists(command_path):
isFound = True
subprocess.call([command_path] + args)
break
if not isFound:
print("intek-sh: " + command + ": command not found")
else:
print("intek-sh: " + command + ": command not found")
def call_cd(args):
"""
Call cd built_in function
@param: args: arguments of cd funtion
"""
if not args or "$HOME" in args[0]:
if "HOME" in environ.keys():
home_dir = environ["HOME"]
chdir(home_dir)
else:
print("intek-sh: cd: HOME not set")
else:
path_location = args[0]
if path.isdir(path_location):
chdir(path_location)
else:
print("intek-sh: cd: " + path_location +
": No such file or directory")
def call_printenv(args):
"""
Call printenv built_in function
@param: args: arguments of printenv funtion
"""
if not args:
for key, value in environ.items():
print(key + '=' + value)
else:
for arg in args:
if arg in environ.keys():
print(environ[arg])
def call_export(args):
"""
Call export built_in function
@param: args: arguments of export funtion
"""
if not args:
for key, value in environ.items():
print("declare -x " + key + '=' + value)
else:
for arg in args:
if "=" in arg:
key = arg.split("=")[0]
value = arg.split("=")[1]
environ[key] = value
else:
environ[arg] = ""
def call_unset(args):
"""
Call unset built_in function
@param: args: arguments of unset funtion
"""
for arg in args:
if arg in environ.keys():
environ.pop(arg)
def call_exit(args):
"""
Call exit built_in function
@param: args: arguments of exit funtion
"""
if args:
if len(args) > 1:
print("intek-sh: exit: too many arguments")
else:
try:
print("exit")
sys.exit(int(args[0]))
except ValueError:
print('intek-sh: exit: ', end='')
sys.exit(args[0])
else:
print("exit")
sys.exit()
def call_function(command, args):
"""
Call built_in function
@param: command: command name
@param: args: command's arguments
"""
call_command = {"cd": call_cd,
"printenv": call_printenv,
"export": call_export,
"unset": call_unset,
"exit": call_exit
}
call_command[command](args)
def main():
"""
This is main function
"""
# Mark flag to run while loop
isLoop = True
# Keep looping until there is any error occur
while isLoop:
# Try and except if there is end of file error
try:
# Take a list of input arguments
list_input = input("intek-sh$ ").split()
# If there is no input arguments -> loop until there is input arg
while not list_input:
list_input = input("intek-sh$ ").split()
# The first argument is command name
command = list_input[0]
# The left arguments are command's arguments
args = list_input[1:]
# built_in functions in this program
built_in = ["cd", "printenv", "export", "unset", "exit"]
# Check if command is built_in function or external function
# If command is external function, call subprocess
if command not in built_in:
call_subprocess(command, args)
# If command is built_in function, call built_in function
else:
call_function(command, args)
except EOFError:
isLoop = False
if __name__ == '__main__':
main()