-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall_desktop.py
More file actions
84 lines (63 loc) · 2.11 KB
/
install_desktop.py
File metadata and controls
84 lines (63 loc) · 2.11 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
# coding:utf-8
"""
:module: install_desktop.py
:description: Create desktop file and copy supplied icon or install an app to Linux
:author: Michel 'Mitch' Pecqueur
:date: 2026.01
"""
import shutil
import sys
import traceback
from pathlib import Path
def install_desktop(exe_path: Path, icon_file: Path | None = None, app_name: str | None = None,
comment: str | None = None) -> Path:
"""
Create desktop file and copy supplied icon
:param exe_path: Full path to the executable
:param app_name: Application Name
:param icon_file: .png icon file
:param comment: Optional comment
:return: Path to created desktop file
"""
home = Path.home()
p = Path(exe_path)
desktop_dir = home / '.local/share/applications'
icons_dir = home / '.local/share/icons'
desktop_dir.mkdir(parents=True, exist_ok=True)
# Copy icon
icon_path = None
if Path(icon_file).is_file():
icon_path = icons_dir / f'{p.stem}.png'
print(icon_path)
shutil.copy(icon_file, icon_path)
# Write desktop file
app_name = app_name or p.stem
desktop_file = desktop_dir / f'{p.stem}.desktop'
desktop_str = (f'[Desktop Entry]\n'
f'Type=Application\n'
f'Name={app_name}\n'
f'Exec={p}\n'
f'Path={p.parent}\n')
if icon_path:
desktop_str += f'Icon={icon_path.stem}\n'
if comment:
desktop_str += f'Comment={comment}\n'
desktop_str += f'Terminal=false\nCategories=Audio;'
with open(desktop_file, 'w') as f:
f.write(desktop_str)
desktop_file.chmod(0o755)
print(f"Installed {desktop_file}")
return desktop_file
def main():
if len(sys.argv) >= 2:
try:
install_desktop(*sys.argv[1:])
except Exception:
traceback.print_exc()
input('Press ENTER to continue...')
else:
sys.stderr.write("Wrong number or arguments\n"
"Please provide at least an executable\n")
input('Press ENTER to continue...')
if __name__ == "__main__":
main()