-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.py
More file actions
156 lines (132 loc) · 6.64 KB
/
Copy pathController.py
File metadata and controls
156 lines (132 loc) · 6.64 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
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
from time import sleep
from Command import Command_Moduler
from Process.Process_Module import Process_Module, clock_running
from File_Module import File_Module, Ret_State
from memo import MemoryManager
import threading
from threading import Event, Thread, current_thread
import time
import os
class Controller:
def __init__(self):
# 初始化操作系统模块
self.command_moduler = Command_Moduler()
self.disk_path = os.path.abspath(r".") + "\\MYDISK"
self.file_module = File_Module(self.disk_path)
self.memory_module = MemoryManager(self.file_module)
self.process_module = Process_Module(self.memory_module)
self.process_module.setDaemon(True)
self.process_module.executing = True
self.process_module.start() # P作为线程开始运行,使用重定义的run函数
self.current_user = "chafakao"
self.command_dict = {
"cd": "cd [file name or path]",
"ls": "ls [file name or path]",
"mkdir": "mkdir [directory path]",
"vi": "vi [file name]",
"touch": "touch [file name]",
"rm": "rm [-option] [file name or dir]",
"chmod": "chmod [file name or path] [authority]",
"run": "run [file name or path]", # 运行进程文件
"fmode": "fmode, display file module state",
"pmode": "pmode, display process module state",
"mmode": "mmode, display memory module state",
"exit": "exit"
}
def print_error_info(self, error_info, ret_code=None, command=None):
if ret_code and ret_code == Ret_State.Success:
return
# 输出错误信息
print('\033[1;31m' + '[error]: ' + '\033[0m' + error_info)
if ret_code:
if ret_code == Ret_State.Error_File_Exist:
print('\033[1;31m ' + "the file already exist." + '\033[0m')
elif ret_code == Ret_State.Error_File_Not_Exist:
print('\033[1;31m ' + "the file doesn't exist." + '\033[0m')
elif ret_code == Ret_State.Error_Path_Not_Exist:
print('\033[1;31m ' + "the path doesn't exist." + '\033[0m')
elif ret_code == Ret_State.Error_Dir_Exist:
print('\033[1;31m ' + "the dir already exist." + '\033[0m')
elif ret_code == Ret_State.Error_Dir_Not_Exist:
print('\033[1;31m ' + "the dir doesn't exist." + '\033[0m')
if command:
print('\033[1;31m' + command + " : " + self.command_dict[command] + '\033[0m')
# 运行操作系统,循环检测控制台输入并调用相应的模块
def execute(self):
while True:
commands_list = self.command_moduler.get_command_list(self.current_user, self.file_module.work_path)
# 每个元素是一个列表,整体长度是当前行输入指令的个数
if len(commands_list) == 0:
continue
for command in commands_list:
# 输入的指令不存在于操作系统指令集中
argc = len(command)
if argc == 0: # 空指令,比如换行
continue
if command[0] not in self.command_dict.keys():
self.print_error_info("command not found")
# 遍历指令库,查看相似指令并给出提示
command_count = 0
for k, v in self.command_dict.items():
if k.__contains__(command[0]):
if command_count == 0:
print('\033[1;31m' + " Did you mean these commands:" + '\033[0m')
print('\033[1;31m' + " " + k + " : " + v + '\033[0m')
command_count += 1
continue
if command[0] == 'run': # 运行进程文件指令,可以指定多个进程文件进行合并
# 后期添加执行文件权限
if argc == 1:
self.print_error_info("command error ", command="run")
else:
# 列出可执行文件列表
for file_name in command[1:]:
self.process_module.create_process(file_name, 1)
elif command[0] == 'touch':
if argc != 2:
self.print_error_info("command error", command="touch")
else:
ret_code = self.file_module.touch(command[1])
self.print_error_info("touch error", ret_code=ret_code)
elif command[0] == "cd":
if argc != 2:
self.print_error_info("command error", command="cd")
else:
ret_code = self.file_module.cd(command[1])
self.print_error_info("cd error", ret_code=ret_code)
elif command[0] == "ls":
if argc != 1:
self.print_error_info("command error", command="ls")
else:
self.file_module.ls()
elif command[0] == "mkdir":
if argc != 2:
self.print_error_info("command error", command="mkdir")
else:
ret_code = self.file_module.mkdir(command[1])
self.print_error_info("mkdir error", ret_code=ret_code)
elif command[0] == "vi":
if argc != 2:
self.print_error_info("command error", command="vi")
else:
ret_code = self.file_module.vi(command[1])
self.print_error_info("vi error", ret_code=ret_code)
elif command[0] == "rm":
if argc != 2 and argc != 3:
self.print_error_info("command error", command="rm")
else:
ret_code=None
if argc == 2:
ret_code=self.file_module.rm(command[1])
else:
ret_code=self.file_module.rm(mode=command[1],name=command[2])
self.print_error_info("rm error", ret_code=ret_code)
elif command[0] == "kill":
self.process_module.kill_process(int(command[1]))
elif command[0] == "exit":
self.process_module.executing = False
return
if __name__ == '__main__':
os_controller = Controller()
os_controller.execute()
clock_running = False