-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-desktop-files.py
59 lines (47 loc) · 1.64 KB
/
create-desktop-files.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
import os
from pathlib import Path
import cmdlib
base_template = """\
[Desktop Entry]
Name={name}
Comment={comment}
Type=Application
Icon=utilities-terminal"""
term_template = base_template + """
Exec=sh -c "{command}; read"
Terminal=true"""
nokeep_template = base_template + """
Exec=sh -c "{command}"
Terminal=true"""
noterm_template = base_template + """
Exec={command}
Terminal=false"""
desktopfiles_dir = Path(__file__).resolve().parent / "desktopfiles"
def get_deleted_commands():
from_env = os.environ.get("XDG_DATA_HOME")
if not (from_env and (xdg_data_home := Path(from_env)).is_absolute()):
xdg_data_home = Path.home() / ".local" / "share"
installed_commands = set(
file.name for file in (xdg_data_home / "applications").glob("command-*.desktop")
)
new_commands = set(file.name for file in desktopfiles_dir.glob("*.desktop"))
return installed_commands - new_commands
for cmd in cmdlib.cmd_dict.values():
# populate template
if not cmd.terminal:
desktop_str = noterm_template.format(
name=cmd.name, comment=cmd.comment, command=cmd.command
)
elif not cmd.keep:
desktop_str = nokeep_template.format(
name=cmd.name, comment=cmd.comment, command=cmd.command
)
else:
desktop_str = term_template.format(
name=cmd.name, comment=cmd.comment, command=cmd.command
)
# save .desktop file
filename = "command-" + cmd.name.replace(" ","-") + ".desktop"
(desktopfiles_dir / filename).write_text(desktop_str)
# print deleted commands to pipe them into xdg-desktop-menu uninstall
print(" ".join(get_deleted_commands()))