-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyfile.py
445 lines (388 loc) · 12.5 KB
/
pyfile.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import shlex
import os
import sys
from subprocess import call
import shutil
import re
__author__ = 'Joshua Free jfre553 2646577'
# This is a global string that represents the current directory, it is an absolute path(in terms of the program)
#It also always has a "-" on the end
current_dir = '-'
def main():
commands = ['pwd', 'cd', 'ls', 'rls', 'tree', 'clear', 'create', 'add', 'cat', 'delete', 'dd', 'quit']
#Create A2dir if does not exist
if not os.path.exists('A2dir'):
os.makedirs('A2dir')
#Change into A2dir
os.chdir("A2dir")
while True:
redirected = not os.isatty(sys.stdin.fileno())
if redirected:
arg = sys.stdin.readline()
print('ffs> ' + arg, end='')
if not arg:
exit()
else:
#Get argument/present prompt
arg = input('ffs> ')
#Split argument into an array of strings
split_arg = string_to_list(arg)
#Execute methods associated to built in commands
if split_arg[0] == commands[0]:
execute_pwd() # implemented
elif split_arg[0] == commands[1]:
execute_cd(split_arg) # implemented
elif split_arg[0] == commands[2]:
execute_ls(split_arg)
elif split_arg[0] == commands[3]:
execute_rls() # implemented
elif split_arg[0] == commands[4]:
execute_tree(split_arg)
elif split_arg[0] == commands[5]:
execute_clear() # implemented
elif split_arg[0] == commands[6]:
execute_create(split_arg) # implemented
elif split_arg[0] == commands[7]:
execute_add(split_arg) # implemented
elif split_arg[0] == commands[8]:
execute_cat(split_arg) # implemented
elif split_arg[0] == commands[9]:
execute_delete(split_arg) # implemented
elif split_arg[0] == commands[10]:
execute_dd(split_arg)
elif split_arg[0] == commands[11]:
execute_quit() # implemented
else:
print('Input is not a recognised command')
def execute_pwd():
print(current_dir)
def execute_cd(args):
#Get reference to the global variable of the current directory.
global current_dir
temp_dir = current_dir
if len(args) == 1:
#If no arguments
temp_dir = '-'
elif args[1] == '..':
#If go up a directory
if len(current_dir) == 1:
pass
else:
#Find where all the dashes are in the pwd
dashes = [i.start() for i in re.finditer('-', current_dir)]
#Select the second to last dash
remove_char = dashes[-2]
#Cut the end off the string representing the current directory
temp_dir = temp_dir[:(remove_char - len(current_dir))]
#Reapped the end dash
temp_dir += '-'
elif args[1][0] == '-':
#If an absolute path
if args[1][-1] != '-':
#If there is no dash on the end of the string, append it
args[1] += '-'
temp_dir = args[1]
else:
#If a relative path
if args[1][-1] != '-':
#If there is no dash on the end append it
args[1] += '-'
temp_dir = temp_dir + args[1]
if find_folder(temp_dir):
#If there is a folder by the name inputted then continue
current_dir = temp_dir
else:
#Fail the cd command
print('No such directory')
def execute_ls(args):
full_folder_name = current_dir
list_printed = []
if len(args) == 1:
#If only one input
full_folder_name = current_dir
contents = list_all_in_folder(full_folder_name)
for thing in contents:
thing = thing.replace(full_folder_name, '',1)
if '-' in thing:
thing = thing.split('-')[0]
if thing in list_printed:
continue
print("d: " + thing)
list_printed.append(thing)
else:
print("f: " + thing)
list_printed.append(thing)
return
if args[1][-1] != '-':
#If no - at end add it.
args[1] += '-'
if args[1][0] == '-':
#If absolute path
full_folder_name = args[1]
if find_folder(full_folder_name):
#If file exists then append
contents = list_all_in_folder(full_folder_name)
for thing in contents:
thing = thing.replace(full_folder_name, '',1)
if '-' in thing:
thing = thing.split('-')[0]
if thing in list_printed:
continue
print("d: " + thing)
list_printed.append(thing)
else:
print("f: " + thing)
list_printed.append(thing)
else:
print('No such folder')
return
else:
#If relative path, make absolute
full_folder_name = current_dir + args[1]
if find_folder(full_folder_name):
#If file exists, append to it
contents = list_all_in_folder(full_folder_name)
for thing in contents:
thing = thing.replace(full_folder_name, '',1)
if '-' in thing:
thing = thing.split('-')[0]
if thing in list_printed:
continue
print("d: " + thing)
list_printed.append(thing)
else:
print("f: " + thing)
list_printed.append(thing)
else:
print('No such folder')
return
def execute_rls():
call(['ls', '-l'])
def execute_tree(args):
full_folder_name = current_dir
if len(args) == 1:
execute_tree_recursion(full_folder_name, 0)
return
if args[1][-1] != '-':
#If no - at end add it.
args[1] += '-'
if args[1][0] == '-':
#If absolute path
full_folder_name = args[1]
if find_folder(full_folder_name):
execute_tree_recursion(full_folder_name, 0)
else:
print('No such folder')
return
else:
#If relative path, make absolute
full_folder_name = current_dir + args[1]
if find_folder(full_folder_name):
execute_tree_recursion(full_folder_name, 0)
else:
print('No such folder')
return
def execute_tree_recursion(folder, indent):
folders_to_go = []
print(' '*indent + folder)
print(' '*indent + '='*len(folder))
contents = list_all_in_folder(folder)
for thing in contents:
thing = thing.replace(folder, '',1)
if '-' in thing:
rep_thing = thing.split('-', 1)[0] + '-'
if rep_thing in folders_to_go:
pass
else:
folders_to_go.append(rep_thing)
else:
print(' '*indent + thing)
indent += 1
for x in folders_to_go:
the_next = folder + x
execute_tree_recursion(the_next, indent)
return
def execute_clear():
#Move up a directory
os.chdir('..')
#Delete the folder we were just in
shutil.rmtree('A2dir')
#Replace the folder
#Create A2dir if does not exist
if not os.path.exists('A2dir'):
os.makedirs('A2dir')
#Change into A2dir
os.chdir("A2dir")
def execute_create(args):
if len(args) == 1:
#If only one input
print('No file name')
return
elif args[1][-1] == '-':
#If given directory rather than file
print("Invalid file name")
return
elif args[1][0] == '-':
#If absolute path
call(['touch', './' + args[1]])
else:
#If relative path, then create absolute path by appending
file_name = current_dir + args[1]
call(['touch', './' + file_name])
def execute_add(args):
if len(args) == 1:
#If only one input
print('No file name')
return
elif args[1][-1] == '-':
#If given directory not file
print("Invalid file name")
return
elif len(args) < 3:
#If nothing was input to be added to file
print("No text argument given")
return
elif args[1][0] == '-':
#If absolute path
if find_file(args[1]):
#If file exists then append
with open(args[1], "a") as my_file:
my_file.write(" ".join(args[2:]))
else:
print('No such file')
return
else:
#If relative path, make absolute
file_name = current_dir + args[1]
if find_file(file_name):
#If file exists, append to it
with open(file_name, "a") as my_file:
my_file.write(" ".join(args[2:]))
else:
print('No such file')
return
def execute_cat(args):
if len(args) == 1:
#Wrong number of inputs
print('No file name')
return
if args[1][-1] == '-':
#If no - at end add it.
args[1] += '-'
if args[1][0] == '-':
#Absolute path
if find_file(args[1]):
with open(args[1], "r") as my_file:
print(my_file.read())
else:
print('No such file')
return
else:
#relative path
file_name = current_dir + args[1]
if find_file(file_name):
with open(file_name, "r") as my_file:
print(my_file.read())
else:
print('No such file')
return
def execute_delete(args):
if len(args) == 1:
#If wrong input
print('No file name')
return
elif args[1][-1] == '-':
#If not file name
print("Invalid file name")
return
elif args[1][0] == '-':
#If absolute
if find_file(args[1]):
#If file exists, delete it
os.remove(args[1])
else:
print('No such file')
return
else:
#If relative
file_name = current_dir + args[1]
if find_file(file_name):
#If file exists delete it
os.remove(file_name)
else:
print('No such file')
return
def execute_dd(args):
if len(args) == 1:
#If wrong input
print('No folder name')
return
if args[1][-1] != '-':
#If no - at end add it.
print(args[1])
args[1] += '-'
print(args[1])
if args[1][0] == '-':
#If absolute
if find_folder(args[1]):
#If older exists, delete it
delete_folder(args[1])
else:
print('No such folder')
return
else:
#If relative
folder_name = current_dir + args[1]
if find_folder(folder_name):
#If folder exists delete it
delete_folder(folder_name)
else:
print('No such folder')
return
def execute_quit():
#Go back to original directory
os.chdir('..')
quit()
def find_file(full_file_name):
#Check if it is a file
return os.path.isfile(full_file_name)
def find_folder(full_folder_name):
#Check if string is a folder
files = os.listdir('.')
return any(full_folder_name in file for file in files)
def string_to_list(line):
#Code given by lecturer for use in assignments
lexer = shlex.shlex(line, posix=True)
lexer.whitespace_split = False
lexer.wordchars += '#$+-,./?@^='
args = list(lexer)
return args
def list_all_in_folder(full_folder_name):
contents = os.listdir('.')
final_list = []
for file in contents:
#If the folder name is longer than the file name
if len(full_folder_name) >= len(file):
#print(file + " less than " + full_folder_name)
continue
#If the folder name is not in the file name
if full_folder_name not in file:
#print(full_folder_name + " not in " + file)
continue
final_list.append(file)
return final_list
def delete_folder(full_folder_name):
#Get all file names
folders = os.listdir('.')
for folder in folders:
#If the folder name is longer than the file name
if len(full_folder_name) >= len(folder):
#print(folder + " less than " + full_folder_name)
continue
#If the folder name is not in the file name
if full_folder_name not in folder:
#print(full_folder_name + " not in " + folder)
continue
os.remove(folder)
if __name__ == '__main__':
main()