-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirectory.py
293 lines (221 loc) · 9.46 KB
/
directory.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import os
import shutil
from management import *
def user_root_dir(user):
os_root_dir = 'root'
user_root = os_root_dir + '\\' + user.username
if not os.path.exists(user_root):
os.makedirs(user_root)
add_record(user.username, 'Directory', user.username,
'Directory Created Successfully')
return user_root
def home_command(user, temp_list=None):
temp_dir = user_root_dir(user)
if not os.path.exists(temp_dir + '\\home'):
os.makedirs(temp_dir + '\\home')
add_record(user.username, 'Home-Directory', user.username +
'\home', 'Directory Created Successfully')
if temp_list != 'No_dir_change':
user.current_dir = 'home'
user.real_current_dir = temp_dir + '\\home'
add_record(user.username, 'HOME', user.real_current_dir + '\\',
'Directory Changed Successfully')
return 'Directory Changed!'
def show_command(user, temp_list=None):
temp_address = user.real_current_dir
if not temp_list:
temp_inc = os.listdir(temp_address)
if temp_inc:
temp_out = 'including:'
for i in temp_inc:
temp_out += '\t' + i
else:
temp_out = 'Directory is Empty!'
add_record(user.username, 'SHOW', 'show',
'Success. Sub-Directories and Files Showed-Up!')
return temp_out
def chd_command(user, temp_list=None):
home_command(user, 'No_dir_change')
temp_address = 'root\\' + user.username + '\\'
if temp_list[0].lower() == '-f':
temp_address += temp_list[1].replace("\\", "\\")
elif temp_list[0] == '..':
temp_x = user.real_current_dir.split('\\')
temp_address = ''
for i in temp_x[:-1]:
temp_address += i + '\\'
temp_address = temp_address[:-1]
elif not '.' in temp_list[0]:
temp_address = user.real_current_dir + '\\' + \
temp_list[0].replace("\\", "\\")
else:
add_record(user.username, 'CHD',
user.real_current_dir + '\\' + temp_list[0], 'Failed. the Directory does not exist')
return 'the Directory does not exist'
if not os.path.exists(temp_address):
add_record(user.username, 'CHD',
user.real_current_dir + '\\' + temp_list[0], 'Failed. the Directory does not exist')
return 'the Directory does not exist'
elif temp_address == 'root':
add_record(user.username, 'CHD',
user.real_current_dir + '\\' + temp_list[0], 'Failed. Try Reaching the Root!')
return 'the Directory does not exist, You are at Root'
else:
temp_x = temp_address.split('\\')
user.current_dir = ''
for i in temp_x[2:]:
user.current_dir += i + '\\'
user.real_current_dir = temp_address
add_record(user.username, 'CHD', user.real_current_dir + '\\',
'Success. Directory Changed!')
return 'Directory Changed!'
def mkd_command(user, temp_list=None):
if temp_list:
temp_a = temp_list[0].split('\\')
if len(temp_a) > 1:
if temp_a[0] == user.username:
temp_address = 'root\\' + temp_list[0]
if not os.path.exists(temp_address):
os.makedirs(temp_address)
temp_x = temp_address.split('\\')
user.current_dir = ''
for i in temp_x[2:]:
user.current_dir += i + '\\'
user.real_current_dir = temp_address
add_record(user.username, 'MKD', temp_address + '\\',
'Success. Directory Created!')
return 'Directory Created Successfully!'
else:
add_record(user.username, 'MKD', temp_address + '\\',
'Fail. Directory already exist!')
return 'Directory already exist!'
else:
add_record(user.username, 'MKD', temp_list[0] + '\\',
'Fail. Not Valid!')
return 'Enter a Valid Name or Address!'
else:
temp_address = user.real_current_dir + '\\' + temp_list[0]
if not os.path.exists(temp_address):
os.makedirs(temp_address)
temp_x = temp_address.split('\\')
user.current_dir = ''
for i in temp_x[2:]:
user.current_dir += i + '\\'
user.real_current_dir = temp_address
add_record(user.username, 'MKD', temp_address + '\\',
'Success. Directory Created!')
return 'Directory Created Successfully!'
else:
add_record(user.username, 'MKD', temp_address + '\\',
'Fail. Directory already exist!')
return 'Directory already exist!'
def ded_command(user, temp_list=None):
rf_flag = False
if not temp_list:
add_record(user.username, 'DED', '',
'Failed. Empty Input!')
return 'Please Type (HELP DED) For more Information!'
elif temp_list[0].lower() == '-rf':
trash_t = temp_list.pop(0)
rf_flag = True
temp_a = temp_list[0].split('\\')
len_temp_a = len(temp_a)
if len_temp_a == 1:
temp_address = user.real_current_dir + '\\' + temp_a[0]
elif len_temp_a > 1:
temp_address = 'root\\' + user.username
for i in temp_a:
temp_address += '\\' + i
if os.path.exists(temp_address):
if not os.listdir(temp_address):
os.rmdir(temp_address)
add_record(user.username, 'DED', temp_address + '\\',
'Success. the Directory Deleted!')
return 'the Directory Deleted Successfully!'
elif rf_flag:
shutil.rmtree(temp_address)
add_record(user.username, 'DED RF', temp_address + '\\',
'Success. the Directory and its Content Deleted!')
return 'the Directory and its Content Deleted Successfully!'
else:
add_record(user.username, 'DED', temp_address + '\\',
'Failed. Directory is Not Empty!')
return 'Directory is Not Empty. Use (-rf) to Delete Non-Empty Directories!'
else:
add_record(user.username, 'DED', temp_address + '\\',
'Failed. Directory Does Not Exists!')
return 'Directory Does Not Exists!'
def cpd_command(user, temp_list=None):
if len(temp_list) != 2:
temp_f = ''
for i in temp_list:
temp_f += i + ', '
add_record(user.username, 'CPD', temp_f,
'Failed. Invalid Parameters!')
return 'Invalid Parameters For CPD. Type HELP CPD for more Information.'
src = 'root\\' + temp_list[0]
des = 'root\\' + temp_list[1]
try:
temp_c = shutil.copytree(src, des)
except FileNotFoundError:
add_record(user.username, 'CPD', src + ' --> ' + des,
'Failed. Directory Not Found!')
return 'Directory Not Found!'
except FileExistsError:
add_record(user.username, 'CPD', src + ' --> ' + des,
'Failed. Directory Already Exist!')
return 'Directory Already Exist!'
add_record(user.username, 'CPD', src + ' --> ' + des,
'Success. Directory Copied!')
return 'Directory Copied Successfully!'
def mvd_command(user, temp_list=None):
if len(temp_list) != 2:
temp_f = ''
for i in temp_list:
temp_f += i + ', '
add_record(user.username, 'MVD', temp_f,
'Failed. Invalid Parameters!')
return 'Invalid Parameters For MVD. Type HELP MVD for more Information.'
src = 'root\\' + temp_list[0]
des = 'root\\' + temp_list[1]
try:
temp_c = shutil.move(src, des)
except FileNotFoundError:
add_record(user.username, 'MVD', src + ' --> ' + des,
'Failed. Directory Not Found!')
return 'Directory Not Found!'
except FileExistsError:
add_record(user.username, 'MVD', src + ' --> ' + des,
'Failed. Directory Already Exist!')
return 'Directory Already Exist!'
except shutil.Error:
add_record(user.username, 'MVD', src + ' --> ' + des,
'Failed. Directory Already Exist!')
return 'Directory Already Exist!'
add_record(user.username, 'MVD', src + ' --> ' + des,
'Success. Directory Moved!')
return 'Directory Moved Successfully!'
def ren_command(user, temp_list=None):
if not temp_list or len(temp_list) < 2 or len(temp_list) > 3:
temp_f = ''
for i in temp_list:
temp_f += i + ', '
add_record(user.username, 'REN', temp_f,
'Failed. Invalid Parameters!')
return 'Invalid Parameters For REN. Type HELP REN for more Information.'
if len(temp_list) == 2:
src = 'root\\' + temp_list[0]
des = 'root\\' + temp_list[1]
if len(temp_list) == 3:
address = 'root\\' + temp_list[0]
src = address + '\\' + temp_list[1]
des = address + '\\' + temp_list[2]
try:
os.rename(src, des)
except FileNotFoundError:
add_record(user.username, 'REN', src + ' --> ' + des,
'Failed. File Not Found!')
return 'File Not Found!'
add_record(user.username, 'REN', src + ' --> ' + des,
'Renamed Successfully!')
return 'Renamed Successfully!'