-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaimcli.py
More file actions
195 lines (171 loc) · 5.7 KB
/
aimcli.py
File metadata and controls
195 lines (171 loc) · 5.7 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
import json
import subprocess
import argparse
import sys
import os
scriptdir = os.path.dirname(os.path.abspath(__file__))+"/"
version = "18.10.23"
def getConfig():
try:
with open(scriptdir+"config.json") as f:
return json.load(f)
except:
open(scriptdir+"config.json", 'w+').close()
data = {}
data["apps_path"] = scriptdir+"/apps/"
subprocess.run(['mkdir', scriptdir+'apps/'])
data["downloads_path"] = ""
data["cli_quit_when_run"] = "true"
dumpConfig(data)
with open(scriptdir+"config.json") as f:
return json.load(f)
def setConfig(key:str, value):
try:
data = getConfig()
data[key] = value
with open(scriptdir+'config.json', "w") as s:
json.dump(data, s, indent=4, sort_keys=True)
except:
open(scriptdir+"config.json", 'w+').close()
with open(scriptdir+"config.json") as f:
return json.load(f)
def dumpConfig(data):
try:
with open(scriptdir+'config.json', "w") as s:
json.dump(data, s, indent=4, sort_keys=True)
except:
open(scriptdir+"config.json", 'w+').close()
with open(scriptdir+"config.json") as f:
json.dump(data, s, indent=4, sort_keys=True)
def process_command(command):
if command[0] == "install":
install()
elif command[0] == "find":
try:
find(command[1])
except IndexError:
find()
elif command[0] == "run":
try:
run(command[1])
except:
print("No app name given.")
elif command[0] == "delete":
try:
delete(command[1])
except:
print("No app name given.")
elif command[0] == "set":
try:
setConfig(command[1], command[2])
print(f"Value {command[1]} set: {getConfig()[command[1]]}")
except:
print("Missing something; set <key> <value>")
elif command[0] == "get":
try:
v = getConfig()[command[1]]
print(v)
except IndexError:
print("Missing key name; get <key>")
except KeyError:
print("Value not found.")
elif command[0] == "help":
print("===========================")
print("AppImage-CLI "+version)
print("")
print("Use any of these commands as the launch argument for this script.\n")
print("install: Checks the download directory for appimages and moves them to the apps directory.\n")
print("run <appname>: Runs the app from the apps directory. Only partial name is required, e.i. run yout > this will find any file with yout in the name, like youtube.\n")
print("find <optional appname>: Searches app directory for apps. If no name is supplied, lists all. If a name is given, will find all apps with a matching name.")
print("set <key> <value>: Sets the config variables.")
print("get <key>: Shows the config value.")
print("delete <appname>: Deletes the app from the apps directory. Only partial name is required.\n")
print("help: This help menu... not that you needed this help menu to tell you that it was a help menu, but thats just a thing help menus seem to do. Hi, I'm a help menu, I'm here to help. But I can't help you with help, I can only help those who help themself...... help me.")
print("===========================")
def install():
pth = getConfig()['downloads_path']
to_pth = getConfig()['apps_path']
files = []
for file in os.listdir(pth):
filename = os.fsdecode(file)
if filename.lower().endswith(".appimage"):
files.append(filename)
fstr = ', '.join(files)
while True:
r = input(f"Install these files? ({fstr}) y/n\n")
if r == "n":
print("Cancelling.")
break
elif r == "y":
print("Begin install...")
for filename in files:
os.rename(pth+filename, to_pth+filename)
print("Installing "+filename+"\n")
break
else:
print("Respond with either y or n.")
def find(appname = ""):
pth = getConfig()['apps_path']
print("=======================================")
if appname == "":
for file in os.listdir(pth):
filename = os.fsdecode(file)
if filename.lower().endswith(".appimage"):
print(filename)
else:
for file in os.listdir(pth):
filename = os.fsdecode(file)
if filename.lower().endswith(".appimage") and appname.lower() in filename.lower():
print(filename)
print("=======================================")
def run(app):
pth = getConfig()['apps_path']
print(f"Runing {app}")
if ".appimage" not in app.lower():
print(f"Finding closest match to {app}.")
for file in os.listdir(pth):
filename = os.fsdecode(file)
if filename.lower().endswith(".appimage") and app.lower() in filename.lower():
app = filename
print(f"Matched with {app}")
break
try:
subprocess.Popen(getConfig()['apps_path']+app)
except PermissionError:
print(f"Setting executable permissions for {app}...")
subprocess.run(['chmod', 'u+x', getConfig()['apps_path']+app])
subprocess.Popen(getConfig()['apps_path']+app)
except Exception as e:
print(e)
def delete(app):
print(f"Del {app}")
def cliloop(command):
subprocess.run(['clear'])
while True:
print("AppImage Manager\nInteractive Mode. ('exit' or 'quit' to close. Clear the terminal text with 'clear'.)")
f = input("Input command: ")
command = f.split(" ")
if command[0] == "exit" or command[0] == "quit":
break
if command[0] == "clear":
subprocess.run(['clear'])
process_command(command)
if command[0] == "run" and getConfig()['cli_quit_when_run'].lower() == "true":
break
if __name__ == "__main__":
command = sys.argv[1:]
if len(command) > 0:
process_command(command)
else:
subprocess.run(['clear'])
while True:
print("AppImage Manager\nInteractive Mode. ('exit' or 'quit' to close. Clear the terminal text with 'clear'.)")
f = input("Input command: ")
command = f.split(" ")
if command[0] == "exit" or command[0] == "quit":
break
if command[0] == "clear":
subprocess.run(['clear'])
process_command(command)
if command[0] == "run" and getConfig()['cli_quit_when_run'].lower() == "true":
break