-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoolbox-export.py
executable file
·109 lines (89 loc) · 3.48 KB
/
toolbox-export.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
#!/bin/python3
import glob, os, shutil, sys
def check_args() -> None:
args = sys.argv
if len(args) == 1:
error("No application name")
def check_container() -> None:
if not os.path.exists("/run/.containerenv"):
error("Not running from toolbox")
def error(msg: str) -> None:
print(f"\033[31;1m[ERROR]\033[0m {msg}")
exit(1)
def info(msg: str) -> None:
print(f"\033[32;1m[INFO]\033[0m {msg}")
def get_desktop_files() -> list[str]:
files: list[str] = []
files.extend(glob.glob("/usr/share/applications/*.desktop"))
files.extend(glob.glob("/usr/local/share/applications/*.desktop"))
applications: list[str] = []
for file in files:
with open(file) as f:
text: str = f.read()
if "NotShowIn" in text:
continue
applications.append(file)
matched_files: list[str] = []
for app in applications:
with open(app) as f:
lines: list[str] = f.readlines()
for line in lines:
if line.startswith("Exec=") and sys.argv[1] in line:
if (
sys.argv[1]
== line.strip().strip("Exec=").split(" ")[0].split("/")[-1]
):
matched_files.append(app)
break
if matched_files == []:
error("No application found")
else:
return matched_files
def export() -> None:
desktop_dir: str = os.path.expanduser("~") + "/.local/share/applications"
os.makedirs(desktop_dir, exist_ok=True)
icon_names: list[str] = []
for file in get_desktop_files():
text: str = ""
with open(file) as f:
for line in f.readlines():
if line.startswith("Icon="):
icon_name = line.split("=")[1].strip()
if not icon_name in icon_names:
icon_names.append(icon_name)
if line.startswith("Exec=") and sys.argv[1] in line:
with open("/run/.containerenv") as c:
for l in c.readlines():
if l.startswith("name"):
name = l.split("=")[1].strip().replace('"', "")
break
text += line.replace(
"Exec=", f"Exec=/usr/bin/toolbox run -c {name} "
)
elif line.startswith("Name="):
text += line.replace("\n", " (toolbox)\n")
else:
text += line
file_name: str = desktop_dir + "/" + file.split("/")[-1]
with open(file_name, "w") as f:
f.write(text)
info(f"Exported desktop file: {file_name}")
for icon_name in icon_names:
if icon_name != "":
files: list[str] = []
files.extend(
glob.glob(f"/usr/share/icons/**/{icon_name}.*", recursive=True)
)
files.extend(
glob.glob(f"/usr/share/pixmaps/**/{icon_name}.*", recursive=True)
)
icon_dir: str = f"{os.path.expanduser('~')}/.local/share/icons"
for file in files:
if file.endswith("png") or file.endswith("svg"):
file_name = f"{icon_dir}/{file.split('/')[-1]}"
shutil.copy(file, file_name)
info(f"Exported icon: {file_name}")
if __name__ == "__main__":
check_container()
check_args()
export()