-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAppImageManager.py
More file actions
executable file
·351 lines (284 loc) · 12 KB
/
Copy pathAppImageManager.py
File metadata and controls
executable file
·351 lines (284 loc) · 12 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
#!/usr/bin/env python3
import argparse
import os
import shutil
import subprocess
import sys
import tempfile
## ubuntu dependency
# sudo apt install libfuse2 -y
class AppImgApplication:
def __init__(self, Exec="", Name="", Icon="", Desktop=""):
self.Exec = Exec
self.Name = Name
self.Icon = Icon
self.Desktop = Desktop
def delete(self):
to_remove = [self.Exec, self.Desktop]
if self.Icon != "" and self.Icon != "application-x-executable":
to_remove.append(self.Icon)
for file_path in to_remove:
if os.path.exists(file_path):
try:
os.remove(file_path)
print(f"File '{file_path}' has been deleted.")
except Exception as e:
print(f"Error occurred while deleting the file: {e}")
else:
print(f"File '{file_path}' does not exist.")
def __str__(self):
return (
f"AppImgApplication(\n"
f" Exec: {self.Exec},\n"
f" Name: {self.Name},\n"
f" Icon: {self.Icon},\n"
f" Desktop: {self.Desktop}\n"
f")"
)
def __repr__(self):
return self.__str__()
class AppImgManager:
def __init__(self):
self.path_desktop = os.path.expanduser("~/.local/share/applications")
self._refresh()
def _load_desktop_file(self, desktop_file):
with open(desktop_file, "r") as f:
AppImg = AppImgApplication(Desktop=desktop_file)
for l in f.readlines():
l = l.strip()
start = l[:5]
if start == "Name=" and AppImg.Name == "":
AppImg.Name = l[5:]
elif start == "Exec=" and AppImg.Exec == "":
AppImg.Exec = l[5:].split(" ")[0]
elif start == "Icon=" and AppImg.Icon == "":
AppImg.Icon = l[5:]
return AppImg
def _refresh_desktop_files(self):
self.desktop_files = sorted(
[f for f in self.list_files(self.path_desktop) if f[-8:] == ".desktop"]
)
def _refresh_installed_appimages(self):
self.installed_appimages = [
self._load_desktop_file(f) for f in self.desktop_files
]
not_AppImage = {
i
for i in range(len(self.installed_appimages))
if ".AppImage" not in self.installed_appimages[i].Exec
}
self.installed_appimages = [
item
for i, item in enumerate(self.installed_appimages)
if i not in not_AppImage
]
self.desktop_files = [
item for i, item in enumerate(self.desktop_files) if i not in not_AppImage
]
def _refresh(self):
self._refresh_desktop_files()
self._refresh_installed_appimages()
def list(self):
return [(idx, a.Name) for (idx, a) in enumerate(self.installed_appimages)]
def delete(self, idx):
try:
app = self.installed_appimages.pop(idx)
app.delete()
self._refresh()
except IndexError:
print("invalid index")
def show(self, idx):
try:
app = self.installed_appimages[idx]
return str(app)
except IndexError:
return "invalid index"
def list_files(self, directory):
"""List all files in a directory and its subdirectories."""
file_list = []
for root, dirs, files in os.walk(directory):
for file in files:
file_list.append(os.path.join(root, file))
return file_list
def __str__(self):
return " ".join([str(a) for a in self.installed_appimages])
def __repr__(self):
return self.__str__()
class AppImgManagerCLI:
def __init__(self, Mgr: AppImgManager, subparsers: argparse._SubParsersAction):
self.Mgr = Mgr
self.subparsers = subparsers
list_parser = self.subparsers.add_parser("list", help="List all installed AppImages")
list_parser.set_defaults(func=self._list)
show_parser = self.subparsers.add_parser("show", help="Show details of a specific AppImage")
show_parser.add_argument("item_id", type=int, help="The ID of the AppImages to show")
show_parser.set_defaults(func=self._show)
delete_parser = self.subparsers.add_parser(
"delete", help="Delete a specific AppImage"
)
delete_parser.add_argument(
"item_id", type=int, help="The ID of the AppImage to be deleted"
)
delete_parser.set_defaults(func=self._delete)
install_parser = self.subparsers.add_parser("install", help="Install AppImage")
install_parser.add_argument(
"file_path", type=str, help="The file path of the AppImage to be installed."
)
install_parser.set_defaults(func=self._install)
def _list(self):
max_str_width = 35
print(f"{'ID':<5} | {'Name':<{max_str_width}}")
print("-" * (5 + max_str_width + 3)) # Separator line
for idx, name in self.Mgr.list():
print(f"{idx:>2} | {name:<{max_str_width}}")
def _show(self, idx):
print(self.Mgr.show(idx))
def _delete(self, idx):
print(f"delete {idx}")
self.Mgr.delete(idx)
def _install(self, file_path):
installer = AppImgInstaller()
installer.install(file_path)
class AppImgInstaller:
def __init__(self):
# Define the directory where AppImages and misc files are stored
self.APPIMAGE_DIR = os.path.expanduser("~/appimages")
self.INSTALL_DIR = os.path.expanduser("~/.local/share/applications")
self.THUMBNAIL_DIR = os.path.expanduser("~/.local/share/icons/")
def install(self, input_appimage_location):
# Create the directories if they do not exist
os.makedirs(self.THUMBNAIL_DIR, exist_ok=True)
os.makedirs(self.APPIMAGE_DIR, exist_ok=True)
os.makedirs(self.INSTALL_DIR, exist_ok=True)
# Check if the AppImage file exists
if os.path.exists(input_appimage_location):
# Copy the AppImage to the APPIMAGE_DIR
shutil.copy(input_appimage_location, self.APPIMAGE_DIR)
print(f"Copied {input_appimage_location} to {self.APPIMAGE_DIR}.")
appimage_path = os.path.join(
self.APPIMAGE_DIR, os.path.basename(input_appimage_location)
)
# Now install the AppImage
self._install_appimage(appimage_path)
print(f"Installed {input_appimage_location} to {appimage_path}")
else:
print(f"AppImage '{input_appimage_location}' does not exist.")
def _install_appimage(self, appimage_path):
"""Install the AppImage."""
appimage_name = os.path.basename(appimage_path).replace(".AppImage", "")
subprocess.run(["chmod", "u+x", appimage_path])
# Create a temporary directory for extraction
with tempfile.TemporaryDirectory() as temp_dir:
# Extract the AppImage to the temporary directory
subprocess.run(
[appimage_path, "--appimage-extract"], cwd=temp_dir, check=True
)
# Extract the thumbnail
icon_path = self._extract_thumbnail(temp_dir, appimage_name)
if icon_path is None:
icon_path = "application-x-executable"
# extract_desktop_file
desktop_file = self._extract_desktop_file(
temp_dir, appimage_path, icon_path
)
if desktop_file == "":
desktop_file = self._default_desktop_file(
appimage_name, appimage_path, icon_path
)
desktop_filename = appimage_name
for line in desktop_file.split("\n"):
if line[:5] == "Name=":
desktop_filename = line[5:]
break
desktop_file_path = os.path.join(
self.INSTALL_DIR, f"{desktop_filename}.desktop"
)
with open(desktop_file_path, "w") as f:
f.write(desktop_file)
print(f"Desktop file created at {desktop_file_path}.")
def _extract_desktop_file(self, temp_dir, appimage_path, icon_path):
temp_dir = os.path.join(temp_dir, "squashfs-root")
desktop_files = [f for f in os.listdir(temp_dir) if f[-8:] == ".desktop"]
if len(desktop_files) == 0:
return ""
elif len(desktop_files) > 1:
print("more than one .desktop file found in AppImage")
with open(os.path.join(temp_dir, desktop_files[0]), "r") as f:
desktop_file_lines = f.readlines()
for idx, line in enumerate(desktop_file_lines):
start = line[:5]
if start == "Exec=":
split = line.split(" ")
split[0] = f"Exec={appimage_path}"
desktop_file_lines[idx] = " ".join(split)
elif start == "Icon=":
split = line.split(" ")
split[0] = f"Icon={icon_path}"
desktop_file_lines[idx] = " ".join(split)
if desktop_file_lines[idx][-1:] != "\n":
desktop_file_lines[idx] += "\n"
return "".join(desktop_file_lines)
def _extract_thumbnail(self, temp_dir, appimage_name):
"""Extract the first thumbnail from the AppImage's extracted contents."""
icon_sources = [
os.path.join(temp_dir, p)
for p in [
"squashfs-root",
"squashfs-root/usr/share/icons/hicolor/1024x1024/apps",
"squashfs-root/usr/share/icons/hicolor/512x512/apps",
"squashfs-root/usr/share/icons/hicolor/256x256/apps",
"squashfs-root/usr/share/icons/hicolor/scalable/apps",
"squashfs-root/usr/bin/share/icons/hicolor/1024x1024/apps",
"squashfs-root/usr/bin/share/icons/hicolor/512x512/apps",
"squashfs-root/usr/bin/share/icons/hicolor/256x256/apps",
"squashfs-root/usr/bin/share/icons/hicolor/scalable/apps",
]
]
# Check if the icon directory exists
for icon_source_dir in icon_sources:
if os.path.exists(icon_source_dir):
for filename in os.listdir(icon_source_dir):
if filename.endswith(".png") or (
filename.endswith(".svg") and "tray" not in filename
):
# Copy the icon to the thumbnail directory
thumbnail_path = os.path.join(self.THUMBNAIL_DIR, filename)
shutil.copy(
os.path.join(icon_source_dir, filename), thumbnail_path
)
print(
f"Thumbnail for {appimage_name} copied to {thumbnail_path}."
)
return thumbnail_path # Return the full path of the icon
print(f"No thumbnail found for {appimage_name} in the extracted contents.")
return None # Return None if no icon was found
def _default_desktop_file(self, appimage_name, exec_path, icon_path):
"""Create a new .desktop file for the application."""
content = (
"[Desktop Entry]\n"
f"Name={appimage_name}\n"
f"Exec={exec_path}\n"
f"Icon={icon_path}\n"
"Type=Application\n"
"Categories=Utility;\n"
)
return content
def main():
parser = argparse.ArgumentParser(description="A simple CLI to manage AppImage applications.")
subparsers = parser.add_subparsers(dest="command")
Mgr = AppImgManager()
MgrCLI = AppImgManagerCLI(Mgr, subparsers)
# Parse the arguments
args = parser.parse_args()
# Check if a command was provided
if args.command is None:
parser.print_help()
elif args.command in {"show", "delete"}:
# Call the appropriate function based on the command
args.func(args.item_id)
elif args.command == "install":
args.func(args.file_path)
else:
args.func()
if __name__ == "__main__":
main()