-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.py
More file actions
660 lines (572 loc) · 23.2 KB
/
shell.py
File metadata and controls
660 lines (572 loc) · 23.2 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
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
from classes_functions import *
import json, datetime
class Shell:
"""Class that represents the shell of the system.
It contains the main loop of the program, as well as the commands and
users in the system.
It's important to note that:
* the shell is the only class that has access to the commands, users and units.
* the shell not only executes the prompts, but validates the user's role."""
def __init__(self):
self.currentUser: User | None = None
self.path: str = "C:/"
self.backup_file = ""
self.disables = []
# List of commands in the system
Command(
"help",
"any",
"help - muestra los comandos disponibles y una breve descripción de los mismos",
"""help
Proporciona una lista de los comandos disponibles en el sistema.""",
help,
)
Command(
"man",
"any",
"man - muestra la descripción del comando especificado",
"""man [comando]
Describe el comando especificado y su uso.""",
man,
)
Command(
"login",
"any",
"login - inicia sesión en el sistema",
"""login
Inicia prompt para ingresar usuario y contraseña.""",
login,
)
Command(
"exit",
"any",
"exit - cierra la sesión actual",
"""exit
Cierra la sesión actual y regresa al prompt de login.""",
exit,
)
Command(
"dir",
"any",
"dir - lista los archivos y carpetas en la ubicación dada",
"""dir [unidad:ubicación]
Lista los archivos y carpetas en la unidad y ubicación dadas.
También es posible especificar los siguientes argumentos:
* [criterio]: -LastUpdate, -creation, -range
Crea la lista según el criterio especificado.
-LastUpdate: lista los archivos y carpetas según la fecha de última modificación.
-creation: lista los archivos y carpetas según la fecha de creación.
-range: lista los archivos y carpetas según un rango de tamaño.
posterior a él, se especifica el rango como:
* min-max: intervalo de tamaño en bytes de la forma min-max.
* tamaño=|>|<: tamaño exacto, mayor o menor al especificado.
al listar con range, es posible especificar el argumento -ext para filtrar por extensión.
* [orden]: asc o desc
Ordena los resultados de la lista de forma ascendente o descendente.""",
dir,
)
Command(
"shu",
"any",
"shu - lista las unidades",
"""shu [unidad]
Lista las unidades disponibles en el sistema""",
shu,
)
Command(
"cd",
"any",
"cd - cambia el directorio de trabajo",
""" cd [path]
Cambia el directorio de acuerdo al path indicado.
Admite:
* Path absoluto - C:/a/b/...
* Path relativo - /a/b/...
* Vuelta a raíz - ..""",
self.cd,
)
Command(
"mkdir",
"any",
"mkdir - crea un directorio",
""" mkdir [nombre]
Crea un directorio en el path actual, o de especificarse, en el path dado.
También admite:
* Path relativo - /a/b/nombre
""",
self.mkdir,
)
Command(
"rmdir",
"any",
"rmdir - borra un directorio",
""" rmdir [nombre]
Borra un directorio en el path actual, o de especificarse, en el path dado.
También admite:
* Path relativo - /a/b/nombre
""",
self.rmdir,
)
Command(
"type",
"any",
"type - crea un archivo",
""" type [nombre] [contenido]
Crea el archivo con la extensión dada en el path actual, o de especificare,
en el path actual.
También admite:
* Path relativo - /a/b/nombre""",
self.type,
)
# User for testing purposes
User("admin", "admin", "admin")
Command(
"log",
"any",
"log - muestra el log del sistema",
"""log
Muestra el log del sistema""",
Logs.print_logs,
)
Command(
"clear-log",
"any",
"clear-log - limpia el log del sistema",
"clear-log limpia el log del sistema y elimina su copia del sistema",
Logs.clear_logs,
)
Command(
"ls",
"any",
"ls - lista los archivos y carpetas en la ubicación dada",
"""ls [unidad:ubicación]
Lista los archivos y carpetas en la unidad y ubicación dadas""",
self.ls,
)
def loop(self):
try:
import readline
readline.parse_and_bind("tab: complete")
readline.set_completer(completation)
except ImportError:
print(
"readline no esta dispobible en el sistema, no se podra usar el autocompletado"
)
# make complete with commands
prompt = ""
print(
"Bienvenido a la MFShell",
"para ver los comandos disponibles escriba help",
sep="\n",
)
while True:
# User setup
name = ""
if self.currentUser != None:
name = self.currentUser.name
else:
name = "guest"
# Prompt loop
prompt = ""
try:
prompt = input(name + "@" + name + f":~ {self.path}$ ")
except KeyboardInterrupt:
exit(0)
prompt = prompt.split(" ")
if self.currentUser == None and prompt[0] not in ["login", "help", "exit"]:
Logs.append(
Log(
" ".join(prompt),
prompt[0],
"no hay usuario logueado, por favor utilizar comando login",
)
)
print("no hay usuario logueado, por favor utilizar comando login")
continue
# Command role validation and execution
if prompt[0] in Command.commands and prompt[0] not in self.disables:
command = Command.commands[prompt[0]]
# Validate role
if command.role != "any":
if self.currentUser == None:
print("must login to access command")
continue
elif self.currentUser.role != command.role:
print("user doesn't have access to command")
continue
# Command execution if it hasn't been invalidated
if len(prompt) > 1:
try:
command(prompt[1:])
except TypeError:
print("function doesn't take arguments")
else:
# Check for login command to update current user
if command != Command.commands["login"]:
try:
command()
except TypeError:
print("function needs arguments")
else:
self.currentUser = login()
else:
Logs.append(Log(" ".join(prompt), prompt[0], "comando no encontrado"))
print("comando no encontrado")
# load the data from the json file and create the objects in memory
# Henry: No entiendo nada de esto, pero yo confío
def load(self):
Logs.load_logs()
config_data = {}
try:
with open("config.json", "r") as configs:
config_data = json.load(configs)
self.backup_file = config_data["backup"]
self.disables = config_data["disables"]
except FileNotFoundError:
print(
RED,
"no se encontro el archivo de configuracion",
DEFAULT,
"\nCree un archivo de configuracion con el nombre config.json y las siguientes llaves: backup, disables",
)
exit(1)
try:
with open(self.backup_file, "r") as file:
data = json.load(file)
except FileNotFoundError:
print(
RED + "no se encontro el archivo de backup",
DEFAULT,
"\nse iniciara con un sistema vacio y se creara el archivo de backup",
)
unit = Unit("C", 500, "ssd")
with open(self.backup_file, "w") as file:
json.dump(
{
"Units": [
{
"name": "C",
"folders": [],
"type": "ssd",
"totalSize": 500,
}
],
"Users": [],
},
file,
)
return
for unit in data["Units"]:
unidad = Unit(unit["name"], unit["totalSize"], unit["type"])
for folder in unit["folders"]:
fold = Folder(
folder["name"], folder["creationDate"], folder["modifyDate"]
)
fol_folders, fol_files = self.loadFolder(folder)
for fol in fol_folders:
fold.append(fol)
for fil in fol_files:
fold.append(fil)
unidad.append(fold)
for user in data["Users"]:
User(user["name"], user["password"], user["role"])
# load the folders and files from the json file
def loadFolder(self, folder):
folders = []
files = []
for file in folder["files"]:
files.append(
File(
file["name"],
file["size"],
file["creationDate"],
file["modifyDate"],
file["extension"],
file["content"],
)
)
for folder in folder["folders"]:
fold = Folder(folder["name"], folder["creationDate"], folder["modifyDate"])
folders_iter, files_iter = self.loadFolder(folder)
for fol in folders_iter:
fold.append(fol)
for fil in files_iter:
fold.append(fil)
folders.append(fold)
return folders, files
# Functions that interact directly with the shell and its files
def cd(self, args: list):
# Validate args
if len(args) == 1:
path = args[0]
# Check for paths and change shell current path
if path == "..":
if self.path not in ["C:/", "D:/", "F:/"]:
self.path = self.path.rstrip("/")
self.path = "/".join(self.path.split("/")[:-1]) + "/"
else:
print("ya estas en la raiz")
Logs.append(Log("cd " + args[0], "cd", "ya estas en la raiz"))
# Check valid path (both absolute and relative)
elif self.valid_path(path):
# Absolute path
if path[1] == ":":
self.path = path
if path[-1] != "/":
self.path += "/"
# Relative path
else:
self.path = join(self.path, path) + "/"
else:
print("argumentos invalidos")
Logs.append(Log("cd " + args[0], "cd", "argumentos invalidos"))
def mkdir(self, args: list):
# validate the arguments
if len(args) == 1:
# get the path
path = args[0]
# validate if the path is absolute or relative
if path[1] != ":":
# join the relative path with the current path
path = join(self.path, path.strip("/"))
# get the name of the folder
names = path.split("/")
name = names[-1]
# get the path without the name
path = "/".join(names[:-1]) + "/"
# validate if the path is valid
if self.valid_path(path):
# validate if file already exists
if self.valid_path(path + name):
print("el directorio ya existe")
Logs.append(
Log("mkdir " + args[0], "mkdir", "el directorio ya existe")
)
return
# Create data for dir
date = datetime.datetime.now().strftime("%Y-%m-%d")
# Check if the path is the root
if path == "C:/":
# append the folder to the unit
Unit.units[path[0]].append(Folder(name, date, date))
# save the current state of the system
self.backup()
return
else:
folder = self.get_dir(path)
if folder != None and isinstance(folder.data, Folder):
folder.data.append(Folder(name, date, date))
self.backup()
else:
print("path invalido")
Logs.append(Log("mkdir " + args[0], "mkdir", "path invalido"))
else:
print("argumentos invalidos")
Logs.append(Log("mkdir " + args[0], "mkdir", "argumentos invalidos"))
def rmdir(self, args: list):
# validate the arguments
if len(args) == 1:
# get the path
path = args[0]
# validate if the path is absolute or relative
if path[1] != ":":
# join the relative path with the current path
path = join(self.path, path.strip("/"))
# get the name of the folder
names = path.split("/")
name = names[-1]
# get the path without the name
path = "/".join(names[:-1]) + "/"
# validate if a path is the root
if path == "C:/":
# get the unit
current_unit = Unit.units[path[0]]
# remove the folder from the unit
current_unit.remove(name)
# save the current state of the system
self.backup()
return
# validate path
if self.valid_path(path):
current_folder = self.get_dir(path)
if current_folder == None:
print("no se encontro el directorio")
Logs.append(
Log("rmdir " + args[0], "rmdir", "no se encontro el directorio")
)
return
# validate if the dir is actually a folder
if not isinstance(current_folder.data, Folder):
print("no es un directorio")
Logs.append(Log("rmdir " + args[0], "rmdir", "no es un directorio"))
return
# remove the folder from the folder
current_folder.data.remove(name)
# save the current state of the system
self.backup()
else:
print("argumentos invalidos")
Logs.append(Log("rmdir " + " ".join(args), "rmdir", "argumentos invalidos"))
def type(self, args: list):
# validate the arguments
if len(args) < 2:
print("faltan argumentos")
Logs.append(Log("type " + " ".join(args), "type", "faltan argumentos"))
return
# get the path and the text
path = args[0]
text = " ".join(args[1:])
# validate if the path is absolute or relative
if not ":" in path:
path = join(self.path, path)
# get the name of the file
name = path.rstrip("/").split("/")[-1]
# validate if a name have a extension
if not "." in name:
print("el archivo debe tener una extension")
Logs.append(
Log("type " + args[0], "type", "el archivo debe tener una extension")
)
return
path = "/".join(path.split("/")[:-1]) + "/"
name, extension = name.split(".")
# only txt files are allowed
if extension != "txt":
print("solo se permiten archivos txt")
Logs.append(Log("type " + args[0], "type", "solo se permiten archivos txt"))
return
if self.valid_path(path):
# validate if file already exists
if self.valid_path(path + "/" + name):
print("el archivo ya existe")
Logs.append(Log("type " + args[0], "type", "el archivo ya existe"))
return
# if the path is the root ,dont create the file
if path == "C:/":
print("no se puede crear un archivo en la raiz")
Logs.append(
Log(
"type " + args[0],
"type",
"no se puede crear un archivo en la raiz",
)
)
return
# get the dir
folder = self.get_dir(path)
date = datetime.datetime.now().strftime("%Y-%m-%d")
file = File(name, len(text), date, date, extension, text)
# append the file to the folder
if folder != None and isinstance(folder.data, Folder):
folder.data.append(file)
# save the current state of the system
self.backup()
def ls(self, args: list | None = None):
# if the args are none, list the current path
if args == None:
dir([self.path])
else:
# list the path given
dir([self.path] + args)
def valid_path(self, path: str) -> bool:
# Get caller function
caller = inspect.stack()[1].function
# Relative path: Add shell's path to path for validation
if path == ".." and caller == "cd":
return True
elif path[1] != ":":
path = self.path + path.strip("/")
# Validate path
# Separate unit and actual path
try:
unidad, path = path.split(":")
except ValueError:
Logs.append(Log(caller + " " + path, caller, "path invalido"))
print("path invalido")
return False
# Validate root folder or given path
if path == "/":
return True
else:
# Get every folder name involved in path
# /F1/F2 -> [F1, F2]
names = path.rstrip("/").split("/")[1:]
if len(names) < 1:
Logs.append(Log(caller + " " + path, caller, "path inválido"))
return False
# Iterate over the names and check for its existence over the current tree
for i in range(len(names)):
if i == 0:
current_folder = Unit.search(unidad, names[i])
else:
current_folder = current_folder.data.search(names[i])
# Checking wether the folder was found or not
if current_folder == None:
break
# Log error if path was not found, return True otherwise
return current_folder != None
def get_dir(self, path: str) -> Node | NodeL | None:
"""Given a path, searches every node involved along the data tree.
This function is used after the path has been validated, so the dir
we're looking for actually exists.
Args:
path (str): Node name descriptor
Returns:
Folder: Search result
"""
# Getting unit and names to search per directory
unidad, path = path.split(":")
names = path.rstrip("/").split("/")[1:]
current_folder = Unit.search(unidad, names[0])
for i in range(1, len(names)):
if current_folder != None and isinstance(current_folder.data, Folder):
current_folder = current_folder.data.search(names[i])
return current_folder
def backup(self):
# data object to save
data = {"Units": [], "Users": []}
for unit in Unit.units:
# get the unit
unit_var: Unit = Unit.units[unit]
# get the folders and files
unit_child = unit_var.childrens.head
root = list()
while (
unit_child != None
and unit_child.data != None
and isinstance(unit_child, NodeL)
):
# get the files and folders
folders, files = unit_child.data.to_list()
# create the root folder
root_fol = {
"name": unit_child.data.name,
"folders": folders,
"files": files,
"creationDate": unit_child.data.creationDate,
"modifyDate": unit_child.data.modifyDate,
}
root.append(root_fol)
# go to the next folder
unit_child = unit_child.next
# create the unit
root_unit = {
"name": unit_var.name,
"folders": root,
"type": unit_var.type,
"totalSize": unit_var.totalSize,
}
# append the unit to the data
data["Units"].append(root_unit)
# get the users
for user in User.users:
data["Users"].append(User.users[user].__dict__)
# save the data
with open(self.backup_file, "w") as file:
json.dump(data, file)
def completation(text, state):
options = [i for i in Command.commands if i.startswith(text)]
if state < len(options):
return options[state]
else:
return None