-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.py
More file actions
272 lines (237 loc) · 7.01 KB
/
commands.py
File metadata and controls
272 lines (237 loc) · 7.01 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
import os
import shutil
import subprocess
from pathlib import Path
from datetime import datetime
import requests
import sys
import time
import calendar
import re
import random
from colorama import init,Fore,Style
init(autoreset=True)
def cmd_ls(args):
path = args[0] if args else '.'
lis = os.listdir(path)
for i in lis:
print(i)
def cmd_cd(args):
if args:
os.chdir(args[0])
else:
print("Usage: cd <directory>")
def cmd_pwd(args):
print(os.getcwd())
def cmd_mkdir(args):
if args:
os.mkdir(args[0])
else:
print("Usage: mkdir <foldername>")
def cmd_touch(args):
if args:
Path(args[0]).touch(exist_ok=True)
else:
print("Usage: touch <filename>")
def cmd_rm(args):
if args:
target = args[0]
if os.path.isdir(target):
shutil.rmtree(target)
else:
os.remove(target)
else:
print("Usage: rm <filename|foldername>")
def cmd_rename(args):
if len(args) == 2:
os.rename(args[0], args[1])
else:
print("Usage: rename <old_name> <new_name>")
def cmd_edit(args):
if not args:
print("Usage: edit <filename>")
return
filename = args[0]
if not os.path.exists(filename):
Path(filename).touch()
print("Enter text (type '.save' on a new line to finish):")
with open(filename, 'a+') as file:
a = 1
while True :
line = input(f'{a} ')
if line == ".save":
print(f'{filename} has been saved')
break
else:
file.write(f'\n{line}')
a = a+1
def cmd_runpy(args):
if args:
subprocess.run(['python', args[0]])
else:
print("Usage: runpy <script.py>")
def cmd_find(args):
filename = args[0]
matches = []
for root, dirs, files in os.walk(os.getcwd()):
if filename in files:
matches.append(os.path.join(root, filename))
if matches:
for m in matches:
print(m)
else:
print("File not found.")
def cmd_cmatrix(args):
try:
while True:
line = "".join(random.choice("01") for _ in range(60))
print(Fore.GREEN + line)
time.sleep(0.05)
except KeyboardInterrupt:
print(Style.RESET_ALL)
def cmd_cat(args):
if len(args)==1:
with open(args[0],'r') as f:
print(f.read())
else:
print("Usage: cat <filename>")
def cmd_cp(args):
with open(args[0],'r') as f:
text = f.read()
with open(args[1],'w') as f2:
f2.write(text)
def cmd_uptime(start_time):
uptime_seconds = int(time.time() - start_time)
minutes, seconds = divmod(uptime_seconds, 60)
print(f"Uptime: {minutes}m {seconds}s")
def cmd_cal(args):
now = time.localtime()
print(calendar.month(now.tm_year, now.tm_mon))
def cmd_pip(args):
if not args:
print("Usage: \npip install <package>\npip uninstall <package>\npip update <package>")
else:
if args[0] == "install":
subprocess.run(f"pip install {args[1]}", shell=True)
elif args[0] =="uninstall":
cons = input(f"are you sure to uninstall {args[1]} (Y/n): ")
if cons == "Y":
subprocess.run(f"pip uninstall {args[1]}", shell=True)
elif args[0] == "update":
subprocess.run(f"pip update {args[1]}", shell=True)
else:
print("Usage: \npip install <package>\npip uninstall <package>\npip update <package>")
def cmd_clear(args):
os.system('cls' if os.name == 'nt' else 'clear')
def cmd_date(args):
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
def cmd_help(args):
help_text = """
Available Commands:
ls - List directory contents
cd <dir> - Change directory
pwd - Show current directory
mkdir <name> - Create a new directory
touch <file> - Create a new empty file
rm <target> - Delete a file or directory
rename <old> <new> - Rename a file or directory
cat <filename> - show a file content
cp <realfile> <copiedfile> - copy a file
edit <file> - Edit a text file
runpy <script.py> - Run a Python script
pip <command> <pkg> - Use pip
clear - Clear the screen
date - Show current date and time
echo <text> - Print text to screen
find <filename> - Searches for a file
cmatrix - Starts matrix rain
grep <filename> <text> - Searches text inside a file
uptime - Shows uptime
rev <text> - Reverses text
wget <url> <save as>- Download file from web
echo <text> - Print text to screen
defstore <command> - Use defstore
td <appname> - Use a third party app
help - Show available commands
exit - Exit the OS
"""
print(help_text)
def cmd_echo(args):
print(' '.join(args))
def cmd_rev(args):
text = args
combined = " ".join(text)
print(combined[::-1])
def installer(url, filename, chunk_size=4096):
response = requests.get(url, stream=True)
total_size = int(response.headers.get("content-length", 0))
downloaded = 0
bar_length = 50
with open(filename, "wb") as f:
for chunk in response.iter_content(chunk_size=chunk_size):
if not chunk:
continue
f.write(chunk)
downloaded += len(chunk)
if total_size > 0:
percent = downloaded / total_size
filled = int(bar_length * percent)
bar = "#" * filled + "-" * (bar_length - filled)
sys.stdout.write(
f"\r[{bar}] {percent*100:5.1f}%"
)
sys.stdout.flush()
def cmd_wget(args):
if len(args) == 2:
url = args[0]
filename = args[1]
installer(url,filename)
print("\nDownload completed")
else:
print("Usage: wget <url> <save as>")
def cmd_defstore(args):
url = "https://raw.githubusercontent.com/samail-islam/Defstore/refs/heads/main/"
if len(args) == 2 and args[0] == "install":
app = args[1].lower()
appfname = app + ".py"
url += appfname
installer(url,appfname)
print("\ninstallation completed")
elif len(args) == 2 and args[0] == "uninstall":
app = args[1].lower()
appfname = app + ".py"
old = [appfname]
cons = input(f"are you sure to uninstall {app}? (Y/n): ")
if cons == "Y":
cmd_rm(old)
print("\nUnistalled")
elif len(args) == 2 and args[0] == "update":
app = args[1].lower()
appfname = app + ".py"
old = [appfname]
cmd_rm(old)
url += appfname
installer(url,appfname)
print("\nupdated")
elif len(args) == 1 and args[0] == "apps":
url += "apps.txt"
response = requests.get(url)
print(response.text)
else:
print("""
Usage:
defstore apps - shows apps availavle for install
defstore install <appname> - installs the app
defstore uninstall <appname> - uninstalls the app
defstore update <appname> - updates the app if installed""")
def cmd_td(args):
if len(args) == 1:
app = args[0]
appfname = app + ".py"
args = [appfname]
cmd_runpy(args)
else:
print("Usage: td <appname>")
def cmd_exit(args):
print("Exiting PyOS...")
exit()