-
Notifications
You must be signed in to change notification settings - Fork 0
/
desktop_launchers.py
executable file
·379 lines (351 loc) · 15.6 KB
/
desktop_launchers.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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/usr/bin/python
# -*- coding: utf-8 -*-
import gi
import os
gi.require_version("Gtk", "3.0")
os.environ["DISPLAY"] = ":0"
from gi.repository import Gtk
from gi.repository import GLib
import locale
import re
from collections import defaultdict
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.urls import open_url
import collections
DOCUMENTATION = '''
---
module: desktop_launchers
author: JayBee
version_added: "2.0.0"
short_description: Create desktop files according the freedesktop.org specifications
description: Ansible Module to create .desktop files according the freedesktop.org specifications: https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#recognized-keys
options:
source:
description: when provided, input desktop file will be used as template and arguments provided will override the template
required: no
target:
description: command (freedesktop_Type is Application) or URL (freedesktop_Type is Link) to the target
required: yes
location:
description: path of the output desktop file
required: yes
head:
description: header to be copied from template / updated to location Desktop Entry by default. Do not put [] brackets.
required: no
freedesktop_Version:
description: Version argument (see freedesktop specifications)
required: no
freedesktop_Name:
description: Name argument. Name[en_US] and Name[<your local>] will be also filled with the same value. (see freedesktop specifications)
required: no
freedesktop_GenericName:
description: GenericName argument (see freedesktop specifications)
required: no
freedesktop_NoDisplay:
description: NoDisplay argument (see freedesktop specifications)
required: no
freedesktop_Comment:
description: Comment argument (see freedesktop specifications)
required: no
freedesktop_Icon:
description: Icon argument. If not precised, path will be taken from the binary (freedesktop_Type is Application) or favicon (freedesktop_Type is Link) or freedesktop_Name otherwise (see freedesktop specifications)
required: no
freedesktop_OnlyShowIn:
description: OnlyShowIn argument (see freedesktop specifications)
required: no
freedesktop_NotShowIn:
description: NotShowIn argument (see freedesktop specifications)
required: no
freedesktop_DBusActivatable:
description: DBusActivatable argument (see freedesktop specifications)
required: no
freedesktop_TryExec:
description: TryExec argument. If not precised and freedesktop_Type is Application, it will be taken from the path of freedesktop_Name otherwise (see freedesktop specifications)
required: no
freedesktop_Path:
description: Path argument. If not precised and freedesktop_Type is Application, it will be taken from the dir of freedesktop_Name otherwise (see freedesktop specifications)
required: no
freedesktop_Terminal:
description: Terminal argument (see freedesktop specifications)
required: no
freedesktop_Actions:
description: Actions argument (see freedesktop specifications)
required: no
freedesktop_MimeType:
description: MimeType argument (see freedesktop specifications)
required: no
freedesktop_Categories:
description: Categories argument (see freedesktop specifications)
required: no
freedesktop_Implements:
description: Implements argument (see freedesktop specifications)
required: no
freedesktop_Keywords:
description: Keywords argument (see freedesktop specifications)
required: no
freedesktop_StartupNotify:
description: StartupNotify argument (see freedesktop specifications)
required: no
freedesktop_StartupWMClass:
description: StartupWMClass argument (see freedesktop specifications)
required: no
freedesktop_PrefersNonDefaultGPU:
description: PrefersNonDefaultGPU argument (see freedesktop specifications)
required: no
'''
EXAMPLES = '''
- name: "Creating VLC application shortcut"
desktop_launchers:
target: "/usr/bin/vlc --started-from-file %U"
location: "$HOME/Desktop/vlc.desktop"
register: testout
- name: "Creating GitHub URL shortcut"
desktop_launchers:
target: "https://github.com/JayBeeDe/"
location: "$HOME/Desktop/JayBeeDe.desktop"
freedesktop_Type: "Link"
register: testout
'''
RETURN = '''
results:
description: return the dict describing the created desktop file
'''
def resolvePath(path, partialResolve=False):
# this function will convert language dependant path to resolved paths, and if partialResolve flag enabled, will then try to replace by env variable such as $HOME, etc.
xdg_resolvable = ["$DIRECTORY_DESKTOP", "$DIRECTORY_DOCUMENTS", "$DIRECTORY_DOWNLOAD", "$DIRECTORY_MUSIC", "$DIRECTORY_PICTURES", "$DIRECTORY_PUBLIC_SHARE", "$DIRECTORY_TEMPLATES", "$DIRECTORY_VIDEOS", "$N_DIRECTORIES"]
for item in xdg_resolvable:
if item in path:
newItem = re.sub(r"^\$", "", item)
try:
resolvedItem = GLib.get_user_special_dir(eval("GLib.UserDirectory." + newItem))
path = re.sub("\$" + newItem, resolvedItem, path)
except Exception:
pass
# resolve system language dependant folders
path = os.path.expandvars(path)
# resolve environment variables as well
if partialResolve:
for k, v in sorted(os.environ.items(), key=lambda item: item[1]):
if re.search(r"^\/.*$", v):
if v in path:
path = re.sub(v, "$" + k, path)
# return k + " " + v + " " + path
# replace all resolved value by env variables when possible
return path
def main():
module = AnsibleModule(
argument_spec=dict(
source=dict(required=False, type="str"),
target=dict(required=True, type="str"),
location=dict(required=True, type="str"),
head=dict(required=False, type="str", default="Desktop Entry"),
freedesktop_Type=dict(required=False, type="str", default="Application", choices=["Application", "Link"]),
freedesktop_Version=dict(required=False, type="float", default=1.0),
freedesktop_Name=dict(required=False, type="str"),
freedesktop_GenericName=dict(required=False, type="str"),
freedesktop_NoDisplay=dict(required=False, type="bool", default=False),
freedesktop_Comment=dict(required=False, type="str"),
freedesktop_Icon=dict(required=False, type="str"),
freedesktop_OnlyShowIn=dict(required=False, type="str"),
freedesktop_NotShowIn=dict(required=False, type="str"),
freedesktop_DBusActivatable=dict(required=False, type="bool", default=False),
freedesktop_TryExec=dict(required=False, type="str"),
freedesktop_Path=dict(required=False, type="str"),
freedesktop_Terminal=dict(required=False, type="bool", default=False),
freedesktop_Actions=dict(required=False, type="str"),
freedesktop_MimeType=dict(required=False, type="str"),
freedesktop_Categories=dict(required=False, type="str"),
freedesktop_Implements=dict(required=False, type="str"),
freedesktop_Keywords=dict(required=False, type="str"),
freedesktop_StartupNotify=dict(required=False, type="bool", default=True),
freedesktop_StartupWMClass=dict(required=False, type="str"),
freedesktop_PrefersNonDefaultGPU=dict(required=False, type="bool"),
)
)
desktop = defaultdict(dict)
source = module.params.get("source")
if source is not None:
source = resolvePath(source)
target = resolvePath(module.params.get("target"), partialResolve=True)
# target = module.params.get("target")
location = resolvePath(str(module.params.get("location")))
head = module.params.get("head")
newDir = None
newPath = None
if re.search(r"^http(s?):\/\/(www\.)?(.*)\.(.*)$", target):
desktop["Type"] == "Link"
else:
desktop["Type"] == "Application"
cursorFlag = False
if source is not None and os.path.isfile(source):
f = open(source, "r")
for line in f:
lineShk = re.sub(r"\n$", "", line)
if re.search(r"^\[" + head + "\]$", lineShk):
cursorFlag = True
elif re.search(r"^\S*=.*$", lineShk) and cursorFlag:
k = re.compile(r"^(\S+) *=(.*)$").match(lineShk).groups()[0]
v = re.compile(r"^(\S+) *=(.*)$").match(lineShk).groups()[1]
desktop[k] = v
else:
if cursorFlag and not re.search(r"^ *$", lineShk):
cursorFlag = False
# read data from template
f.close()
for k, v in module.params.items():
if v is not None:
if re.search(r"^freedesktop_.*", k):
newKey = re.compile(r"^(freedesktop_)(.*)$").match(k).groups()[1]
desktop[newKey] = v
# override template or just add parameters
if desktop["Type"] == "Link":
desktop["URL"] = target
else:
print(target)
desktop["Exec"] = target
if desktop["Type"] == "Link":
target = re.sub(r"\/*$", "", target)
newName = re.sub(r"\.", " ", re.sub(r"\.\S+$", "", target.split("/")[-1])).title()
else:
if re.search(r"^((\/\S*)+) .*$", target):
# "/usr/bin/vlc --started-from-file %U"s
newName = os.path.basename(re.compile(r"^((\/\S*)+) .*$").match(target).groups()[0])
elif re.search(r"^(\S+) .*$", target):
newName = re.compile(r"^(\S+) .*$").match(target).groups()[0]
else:
newName = target
if "Name" not in desktop:
desktop["Name"] = newName
desktop["Name[en_US]"] = desktop["Name"]
syslocale = locale.getdefaultlocale()[0]
if syslocale != "en_US":
desktop["Name[" + syslocale + "]"] = desktop["Name"]
iconIsURL = False
if "Icon" not in desktop and desktop["Type"] == "Link":
iconIsURL = True
if "Icon" in desktop and desktop["Type"] == "Application":
if re.search(r"^http(s?):\/\/(www\.)?(.*)\.(.*)$", desktop["Icon"]):
iconIsURL = True
webBody = None
iconData = None
if iconIsURL:
if os.path.isdir("/usr/share/icons/ansible/"):
iconDir = "/usr/share/icons/ansible/"
else:
try:
os.makedirs("/usr/share/icons/ansible/")
os.chmod("/usr/share/icons/ansible/", 0o755)
iconDir = "/usr/share/icons/ansible/"
except Exception:
if not os.path.isdir(os.environ["HOME"] + "/.local/share/icons/ansible/"):
os.makedirs(os.environ["HOME"] + "/.local/share/icons/ansible/")
os.chmod(os.environ["HOME"] + "/.local/share/icons/ansible/", 0o755)
iconDir = os.environ["HOME"] + "/.local/share/icons/ansible/"
try:
webBody = open_url(desktop["URL"], method="GET", validate_certs=False).read()
except Exception:
webBody = None
if webBody is not None:
if re.search("<link rel=\\\"icon\\\" ", webBody):
matchs = re.compile(r"^.*<link rel=\"icon\" .*href=\"(\S+)\".*\/>$", re.MULTILINE)
iconURLs = []
for match in matchs.finditer(webBody):
iconURLs.append(match.groups()[0])
iconURLs = sorted(iconURLs, reverse=True)
# we take the biggest image (trust alphabetic order !!) first
iconData = None
for iconURL in iconURLs:
if not re.search(r"^http(s?):\/\/(www\.)?(.*)\.(.*)$", iconURL):
iconURL = desktop["URL"] + iconURL
# module.exit_json(changed=False, results=iconURL)
iconPath = iconDir + desktop["Name"] + re.compile(r"^.*(\.\S+)$").match(iconURL).groups()[0]
try:
iconData = open_url(iconURL, method="GET", validate_certs=False).read()
with open(iconPath, "wb") as image:
image.write(iconData)
os.chmod(iconPath, 0o755)
break
except Exception:
iconData = None
if iconData is not None:
desktop["Icon"] = iconPath
if not "Icon" in desktop and (webBody is None or iconData is None or not iconIsURL):
iconTheme = Gtk.IconTheme.get_default()
icon = iconTheme.lookup_icon(desktop["Name"], 48, 0)
if icon:
desktop["Icon"] = icon.get_filename()
else:
desktop["Icon"] = desktop["Name"]
if desktop["Type"] == "Application":
if re.search(r"^((\/\S*)+) .*$", target):
newDir = os.path.dirname(re.compile(r"^((\/\S*)+) .*$").match(target).groups()[0])
if newDir is not None:
newPath = newDir + "/" + desktop["Name"].lower()
else:
newPath = desktop["Name"].lower()
if "Path" not in desktop and newDir is not None:
desktop["Path"] = newDir
# if "TryExec" not in desktop:
# desktop["TryExec"] = newPath
if re.search(r"^.*\/$", location):
location = location + desktop["Name"] + ".desktop"
else:
if not re.search(r"^.*\.desktop$", location):
location = location + ".desktop"
odesktop = collections.OrderedDict(sorted(desktop.items()))
from pprint import pprint as pprint
pprint(odesktop)
hasChanged = False
cnt = 0
newLines = []
if os.path.exists(location):
f = open(location, "r")
for line in f.readlines():
lineShk = re.sub(r"\n$", "", line)
if re.search(r"^\[" + head + "\]$", lineShk):
cursorFlag = True
elif re.search(r"^\S+ *=.*$", lineShk) and cursorFlag:
k = re.compile(r"^(\S+) *=(.*)$").match(lineShk).groups()[0]
v = re.compile(r"^(\S+) *=(.*)$").match(lineShk).groups()[1]
if k in odesktop:
if str(v).lower() in ["true", "false"]:
if str(odesktop[k]).lower() != str(v).lower():
hasChanged = True
else:
if str(odesktop[k]) != str(v):
hasChanged = True
else:
hasChanged = True
cnt = cnt + 1
else:
if cursorFlag and not re.search(r"^ *$", lineShk):
cursorFlag = False
if not re.search(r"^ *$", lineShk):
newLines.append(lineShk)
f.close()
if cnt != len(odesktop):
hasChanged = True
# read data from exiting file to check if content is the same
if hasChanged:
f = open(location, "w+")
for newLine in newLines:
f.write(newLine + "\n")
f.close()
# remove section containing old data: everything will be appended later
else:
hasChanged = True
if not os.path.isdir(os.path.dirname(location)):
os.makedirs(os.path.dirname(location))
os.chmod(os.path.dirname(location), 0o755)
if hasChanged:
with open(location, "a") as f:
f.write("[" + head + "]\n")
for k, v in odesktop.items():
if isinstance(v, bool):
f.write(k + "=" + str(v).lower() + "\n")
else:
f.write(k + "=" + str(v) + "\n")
os.chmod(location, 0o755)
module.exit_json(changed=hasChanged, ansible_module_results=odesktop)
if __name__ == "__main__":
main()