-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappmarket.py
187 lines (164 loc) · 8.83 KB
/
appmarket.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
import kernel
import sys
import ekernel
import os
import requests
from blessed import Terminal
term = Terminal()
GITHUB_REPO = "https://api.github.com/repos/gauthamnair2005/ProcyonCLS-AppMarket/contents/apps"
GITHUB_APP_UPDATE_TAG_REPO = "https://api.github.com/repos/gauthamnair2005/ProcyonCLS-App-Updater/contents/"
if not os.path.exists("apps"):
os.makedirs("apps")
def fetch_apps():
try:
response = requests.get(GITHUB_REPO)
response.raise_for_status()
apps = response.json()
return [app['name'] for app in apps if app['type'] == 'file' and app['name'].endswith('.py')]
except requests.RequestException as e:
kernel.printError(f"Error fetching apps: {e}!")
return []
def fetch_app_version(app_name):
try:
appTag = app_name.split(".")[0] + ".txt"
appTagUrl = f"{GITHUB_APP_UPDATE_TAG_REPO}/{appTag}"
response = requests.get(appTagUrl)
response.raise_for_status()
appTagContent = response.json()
return requests.get(appTagContent['download_url']).text.strip()
except requests.RequestException as e:
kernel.printError(f"Error fetching app version: {e}!")
return None
def get_local_app_version(app_path):
try:
with open(app_path, 'r') as f:
for line in f:
if line.startswith("__version__"):
return line.split("=")[1].strip().strip('"').strip("'")
return None
except OSError as e:
kernel.printError(f"Error reading local app version: {e}")
return None
def main():
if len(sys.argv) >= 2:
if sys.argv[1] >= "2.3.5":
# Initialize with splash screen
ekernel.splashScreen("ProcyonCLS AppMarket", "Version 2.3.5")
while True:
# Display menu
kernel.clrscr()
ekernel.printHeader("AppMarket")
kernel.printInfo(("Welcome to ProcyonCLS AppMarket"))
print()
menu_items = [
"1. Browse Apps",
"2. Update Apps",
"3. Uninstall Apps",
"4. Exit"
]
for item in menu_items:
print(term.center(item))
print()
try:
kernel.println(("Enter choice:"))
choice = int(kernel.centered_input(term))
if choice == 1:
kernel.clrscr()
ekernel.printHeader("Browse Apps")
apps = fetch_apps()
if not apps:
kernel.printError(("Internet connection not available"))
else:
kernel.println(("Available Apps:"))
print()
for i, app in enumerate(apps):
appNoExtension = app.split(".")[0]
print(term.center(f"{i + 1}. {appNoExtension}"))
try:
print()
kernel.println(("Enter app number to install:"))
app_num = int(kernel.centered_input(term))
if 1 <= app_num <= len(apps):
app_name = apps[app_num - 1]
app_url = f"{GITHUB_REPO}/{app_name}"
response = requests.get(app_url)
response.raise_for_status()
app_content = response.json()
appInstallLoc = os.path.join("apps", app_name)
# Show progress
kernel.printInfo(("Downloading..."))
with open(appInstallLoc, 'w') as f:
f.write(requests.get(app_content['download_url']).text)
kernel.printSuccess((f"App {app_name} installed successfully"))
else:
kernel.printError(("Invalid selection"))
except ValueError:
kernel.printError(("Invalid input. Please enter a number."))
elif choice == 2:
kernel.clrscr()
ekernel.printHeader("Update Apps")
appInstalledList = [app for app in os.listdir("apps") if app.endswith('.py')]
if not appInstalledList:
kernel.printError(("No apps installed"))
else:
for i, app in enumerate(appInstalledList):
print(term.center(f"{i + 1}. {app}"))
appWithLoc = os.path.join("apps", app)
local_version = get_local_app_version(appWithLoc)
server_version = fetch_app_version(app)
if local_version and server_version:
if local_version == server_version:
kernel.printSuccess((f"App {app} is up to date"))
else:
kernel.printWarning((f"Update available for {app}"))
kernel.println(("Do you want to update? (y/n)"))
if kernel.centered_input(term).lower() == 'y':
app_url = f"{GITHUB_REPO}/{app}"
response = requests.get(app_url)
response.raise_for_status()
app_content = response.json()
with open(appWithLoc, 'w') as f:
f.write(requests.get(app_content['download_url']).text)
kernel.printSuccess((f"App {app} updated successfully"))
else:
kernel.printError((f"Could not determine version for {app}"))
elif choice == 3:
kernel.clrscr()
ekernel.printHeader("Uninstall Apps")
appInstalledList = [app for app in os.listdir("apps") if app.endswith('.py')]
if not appInstalledList:
kernel.printError(("No apps installed"))
else:
kernel.println(("Installed Apps:"))
print()
for i, app in enumerate(appInstalledList):
print(term.center(f"{i + 1}. {app}"))
try:
print()
kernel.println(("Enter app number to uninstall:"))
app_num = int(kernel.centered_input(term))
if 1 <= app_num <= len(appInstalledList):
app_name = appInstalledList[app_num - 1]
os.remove(os.path.join("apps", app_name))
kernel.printSuccess((f"App {app_name} uninstalled successfully"))
else:
kernel.printError(("Invalid selection"))
except ValueError:
kernel.printError(("Invalid input. Please enter a number."))
elif choice == 4:
kernel.printInfo(("Exiting AppMarket"))
break
else:
kernel.printError(("Invalid choice"))
kernel.println(("\nPress Enter to continue..."))
kernel.centered_input(term)
except ValueError:
kernel.printError(("Invalid input"))
kernel.println(("\nPress Enter to continue..."))
kernel.centered_input(term)
else:
kernel.printError("This version of market is incompatible with current version of ProcyonCLS")
else:
kernel.printError("OS Scope Error")
if __name__ == "__main__":
main()