-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.py
147 lines (134 loc) · 4.64 KB
/
commands.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
import os
import re
import time
from pathlib import Path
import json
class Commands:
def __init__(self, console, umask):
self.console = console
self.umask = umask
def cat(self, lest, direc):
for i in lest[1:]:
stringcat = ""
if os.path.isfile(f"{direc}/{i}"):
stringcat += i
else:
self.console.print(f"{i} not found or not enough permission")
def rmdir(self, lest, direc):
content = lest[1:]
for i in content:
try:
os.rmdir(f"{direc}/{i}")
os.remove(f"prop/{direc}/{i}.prop")
except FileExistsError or FileNotFoundError:
self.console.print("Error: File not found")
def mkdir(self, lest, direc):
content = lest[1:]
for i in content:
try:
os.mkdir(f"{direc}/{i}")
except FileExistsError or FileNotFoundError:
self.console.print("Error: File already exists")
info = Path(f"prop/{direc}/{i}.prop")
properties = 777
properties -= int(self.umask)
write = json.dumps(properties)
info.write_text(write)
@staticmethod
def cd(lest, direc):
i = lest[1]
if i == ".":
lul = direc.split("/")
if os.path.isdir(lul.pop(len(lul) - 1)):
print("no dir" + lul)
direc = "/".join(lul)
else:
pass
elif os.path.isdir(direc + "/" + i):
direc += "/" + i
return direc
def ls(self, direc):
stringprint = ""
for i in os.listdir(direc):
binperm = Path("prop/" + direc + f"/{i}.prop")
perms = binperm.read_text()
permstring = ""
if os.path.isdir(direc + f"/{i}"):
permstring += "d"
for j in perms:
j = str(bin(int(j)))[2:]
if j[0] == "1":
permstring += "r"
else:
permstring += "-"
if j[1] == "1":
permstring += "w"
else:
permstring += "-"
if j[2] == "1":
permstring += "x"
else:
permstring += "-"
stringprint += f"\n{permstring}\t{i}"
self.console.print(stringprint)
def echo(self, lest, direc):
check = 0
print(lest)
cont = " ".join(lest)
stringer = str(re.findall("(\".+\")", cont)[0])
stringer = stringer.replace('"', "")
stringer = stringer.encode().decode("unicode_escape")
for i in lest[1:]:
if i == ">" and len(lest) >= 3:
try:
num = lest.index(">")
except ValueError:
self.console.print("Error: Syntax Error")
break
title = lest[num + 1]
file = Path(f"{direc}/{title}")
file.write_text(stringer)
properties = 666
properties -= int(self.umask)
info = Path(f"prop/{direc}/{title}.prop")
write = json.dumps(properties)
info.write_text(write)
check = 1
elif i == ">>" and len(lest) >= 3:
try:
num = lest.index(">>")
except ValueError:
self.console.print("Error: Syntax Error")
break
title = lest[num + 1]
file = Path(f"{direc}/{title}")
try:
past = file.read_text()
file.write_text(past + "\n" + stringer)
except FileNotFoundError:
file.write_text(stringer)
properties = 666
properties -= int(self.umask)
info = Path(f"prop/{direc}/{title}.prop")
write = json.dumps(properties)
info.write_text(write)
check = 1
if check == 0:
self.console.print(stringer)
def pwd(self, direc):
direc = direc.replace("virtualenv", "@")
self.console.print("\n"+direc)
def id(self, utype):
usertypes = ["guest", "root"]
user = usertypes[utype]
self.console.print("\n"+user)
def reload(self):
self.console.print("Reloading")
time.sleep(0.8)
text = json.dumps(1)
file = Path("status/reload.stat")
file.write_text(text)
os.system("py terminal.py")
@staticmethod
def clear():
os.system("cls")