summaryrefslogtreecommitdiff
path: root/branding/templates/snap/local/pre/pack_installers
blob: 58134cc0653398c633eaf972c6b51b01fad5c61e (plain)
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
#!/usr/bin/env python3
import os
import subprocess
from base64 import encodestring as encode

HELPDIR = '../../../../../helpers'
INSTALL = 'hooks/install'

POLKIT_FILE = os.environ.get('POLKIT_FILE')
APP_NAME = os.environ.get('APP_NAME')

with open(POLKIT_FILE) as polkit:
    b64_polkit = encode(polkit.read().encode())

with open(INSTALL, 'w') as install:
    install.write('#!/usr/bin/env python3\n')
    install.write('# DO NOT MODIFY MANUALLY\n')
    install.write('# This helper installs the polkit policy file\n')
    install.write('# for the ${applicationName} snap.\n')
    install.write('# It is generated automatically\n')
    install.write('# by the script at "snap/local/pre/pack_installers"\n')
    install.write('import subprocess\n')
    install.write('import os\n')
    install.write('from base64 import decodestring as decode\n')
    install.write("""
POLKIT = {polkit}

with open('/usr/share/polkit-1/actions/{polkit_file}', 'w') as polkit:
    lines = decode(POLKIT).split(b"\\n")
    for line in lines:
        polkit.write(line.decode() + "\\n")

with open('/etc/os-release') as f:
    release = f.read()
# this is a workaround for the fact that debian does not place snap desktop entries in a system+wide path.
if 'ID=debian' in release:
    desktop_path = "/usr/share/applications/{app_name}.desktop"
    if os.path.exists(desktop_path):
        os.remove(desktop_path)
    os.symlink("/snap/{app_name}/current/snap/meta/gui/{app_name}.desktop", desktop_path)
    subprocess.call(['update-desktop-database'])
""".format(
        polkit=b64_polkit,
        polkit_file=POLKIT_FILE,
        app_name=APP_NAME))

subprocess.Popen(["chmod", "+x", INSTALL])
print("[+] Done packing installers into the snap install hook...")