-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileManager.py
More file actions
159 lines (131 loc) · 5.33 KB
/
FileManager.py
File metadata and controls
159 lines (131 loc) · 5.33 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
156
157
158
159
import os
class CommandException(Exception):
def __init__(self, message):
self.message = message
class CommandFormatException(CommandException):
pass
class CommandExecutionException(CommandException):
pass
class FileUtils:
@staticmethod
def copy(args):
source = os.path.join(os.getcwd(), args[0])
destination = os.path.join(os.getcwd(), args[1])
if not os.path.exists(source):
raise CommandExecutionException("Source doesn't exist")
if not os.path.exists(destination) and os.path.isdir(destination):
raise CommandExecutionException("Destination doesn't exist")
if os.path.isfile(source):
if os.path.isdir(destination):
FileUtils.__copy_file_to_folder(source, destination)
else:
FileUtils.__copy_file_to_file(source, destination)
else:
if os.path.isfile(destination):
raise CommandExecutionException("Source is a folder, but destination is a file")
else:
FileUtils.__copy_folder_to_folder(source, destination)
os.removedirs(source)
@staticmethod
def print_working_directory(args):
print(os.getcwd())
@staticmethod
def change_directory(args):
try:
os.chdir(args[0])
except Exception as e:
raise CommandExecutionException(str(e))
@staticmethod
def make_directory(args):
path = os.path.join(os.getcwd(), args[0])
if os.path.exists(path):
raise CommandExecutionException("Path already exists")
os.mkdir(os.path.join(os.getcwd(), args[0]))
@staticmethod
def list_files(args):
print(os.listdir(os.getcwd()))
@staticmethod
def remove(args):
path = os.path.join(os.getcwd(), args[0])
if not os.path.exists(path):
raise CommandExecutionException("Path doesn't exist")
if os.path.isfile(path):
os.remove(path)
else:
os.removedirs(path)
@staticmethod
def move(args):
source = os.path.join(os.getcwd(), args[0])
destination = os.path.join(os.getcwd(), args[1])
if not os.path.exists(source):
raise CommandExecutionException("Source doesn't exist")
if not os.path.exists(destination):
raise CommandExecutionException("Destination doesn't exist")
if os.path.isfile(source):
if os.path.isdir(destination):
FileUtils.__copy_file_to_folder(source, destination)
FileUtils.remove([source])
else:
if os.path.dirname(source) == os.path.dirname(destination):
os.rename(source, destination)
else:
FileUtils.__copy_file_to_file(source, destination)
FileUtils.remove([source])
else:
if os.path.isfile(destination):
raise CommandExecutionException("Source is a folder, but destination is a file")
else:
FileUtils.__copy_folder_to_folder(source, destination)
os.removedirs(source)
@staticmethod
def __copy_file_to_folder(source, destination):
try:
new_file = os.path.join(destination, os.path.basename(source))
FileUtils.__copy_file_to_file(source, new_file)
except Exception as e:
raise CommandExecutionException(str(e))
@staticmethod
def __copy_file_to_file(source, destination):
try:
with open(destination, 'w') as nf:
with open(source, 'r') as src:
nf.write(src.read())
except Exception as e:
raise CommandExecutionException(str(e))
@staticmethod
def __copy_folder_to_folder(source, destination):
try:
new_folder = os.path.join(destination, os.path.basename(source))
os.mkdir(new_folder)
for file in os.listdir(source):
if os.path.isfile(file):
FileUtils.__copy_file_to_folder(file, destination)
else:
FileUtils.__copy_folder_to_folder(file, new_folder)
except Exception as e:
raise CommandExecutionException(str(e))
class Shell:
def __init__(self):
self.current_directory = os.getcwd()
self.commands = {'cp': (FileUtils.copy, 2), 'exit': (self._exit, 0), 'pwd': (FileUtils.print_working_directory, 0),
'cd': (FileUtils.change_directory, 1), 'mkdir': (FileUtils.make_directory, 1),
'ls': (FileUtils.list_files, 0), 'rm': (FileUtils.remove, 1), 'mv': (FileUtils.move, 2)}
self.exit_flag = False
def _exit(self, args):
self.exit_flag = True
shell = Shell()
while not shell.exit_flag:
try:
commandLine = input().split(' ')
if len(commandLine) == 0:
continue
command = commandLine[0]
if command in shell.commands.keys():
if len(commandLine) != 1 + shell.commands[command][1]:
raise CommandFormatException('Invalid number of arguments')
args = commandLine[1:] if len(commandLine) > 1 else 0
shell.commands[command][0](args)
else:
raise CommandFormatException('Invalid command')
except CommandException as e:
print(e.message)