annotate flatpak/libervia_wrapper-dbus.py @ 173:0d2eb834fc59

screenshots: more 0.8 screenshots + apng screencast
author Goffi <goffi@goffi.org>
date Tue, 30 Nov 2021 22:14:01 +0100
parents a213053a03be
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
171
a213053a03be flatpak: update files following names change + Python 3 update:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python3
a213053a03be flatpak: update files following names change + Python 3 update:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 """This script launch Libervia backend if it's not before running command"""
a213053a03be flatpak: update files following names change + Python 3 update:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
a213053a03be flatpak: update files following names change + Python 3 update:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 import os
a213053a03be flatpak: update files following names change + Python 3 update:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 import sys
a213053a03be flatpak: update files following names change + Python 3 update:
Goffi <goffi@goffi.org>
parents:
diff changeset
6 import dbus
a213053a03be flatpak: update files following names change + Python 3 update:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 import subprocess
a213053a03be flatpak: update files following names change + Python 3 update:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 from sat.tools import config
a213053a03be flatpak: update files following names change + Python 3 update:
Goffi <goffi@goffi.org>
parents:
diff changeset
9
a213053a03be flatpak: update files following names change + Python 3 update:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 command = "##COMMAND##"
a213053a03be flatpak: update files following names change + Python 3 update:
Goffi <goffi@goffi.org>
parents:
diff changeset
11 const_INT_PREFIX = "org.libervia.LiberviaCLI"
a213053a03be flatpak: update files following names change + Python 3 update:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 const_OBJ_PATH = '/org/libervia/Libervia/bridge'
a213053a03be flatpak: update files following names change + Python 3 update:
Goffi <goffi@goffi.org>
parents:
diff changeset
13
a213053a03be flatpak: update files following names change + Python 3 update:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 const_INT_PREFIX = config.getConfig(
a213053a03be flatpak: update files following names change + Python 3 update:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 config.parseMainConf(),
a213053a03be flatpak: update files following names change + Python 3 update:
Goffi <goffi@goffi.org>
parents:
diff changeset
16 "",
a213053a03be flatpak: update files following names change + Python 3 update:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 "bridge_dbus_int_prefix",
a213053a03be flatpak: update files following names change + Python 3 update:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 "org.libervia.Libervia")
a213053a03be flatpak: update files following names change + Python 3 update:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
a213053a03be flatpak: update files following names change + Python 3 update:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 # we remove any path with "/home" from PYTHONPATH because we have
a213053a03be flatpak: update files following names change + Python 3 update:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 # "--filesystem=home" permission and packages in .local/lib/pythonX/site-packages may
a213053a03be flatpak: update files following names change + Python 3 update:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 # conflict with the ones from flatpak
a213053a03be flatpak: update files following names change + Python 3 update:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 clean_pythonpath = ':'.join(p for p in sys.path if p and not p.startswith('/home'))
a213053a03be flatpak: update files following names change + Python 3 update:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 os.environ['PYTHONPATH'] = clean_pythonpath
a213053a03be flatpak: update files following names change + Python 3 update:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 os.environ['PYTHONNOUSERSITE'] = '1'
a213053a03be flatpak: update files following names change + Python 3 update:
Goffi <goffi@goffi.org>
parents:
diff changeset
26
a213053a03be flatpak: update files following names change + Python 3 update:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 try:
a213053a03be flatpak: update files following names change + Python 3 update:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 sessions_bus = dbus.SessionBus()
a213053a03be flatpak: update files following names change + Python 3 update:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 db_object = sessions_bus.get_object(const_INT_PREFIX,
a213053a03be flatpak: update files following names change + Python 3 update:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 const_OBJ_PATH)
a213053a03be flatpak: update files following names change + Python 3 update:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 except dbus.exceptions.DBusException as e:
a213053a03be flatpak: update files following names change + Python 3 update:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 if e._dbus_error_name != 'org.freedesktop.DBus.Error.ServiceUnknown':
a213053a03be flatpak: update files following names change + Python 3 update:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 raise e
a213053a03be flatpak: update files following names change + Python 3 update:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 # backend not found, we need to launch it
a213053a03be flatpak: update files following names change + Python 3 update:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 print("Launching Libervia backend")
a213053a03be flatpak: update files following names change + Python 3 update:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 subprocess.check_call("libervia-backend")
a213053a03be flatpak: update files following names change + Python 3 update:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 backend_launched = True
a213053a03be flatpak: update files following names change + Python 3 update:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 else:
a213053a03be flatpak: update files following names change + Python 3 update:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 backend_launched = False
a213053a03be flatpak: update files following names change + Python 3 update:
Goffi <goffi@goffi.org>
parents:
diff changeset
40
a213053a03be flatpak: update files following names change + Python 3 update:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 subprocess.run([command, *sys.argv[1:]])
a213053a03be flatpak: update files following names change + Python 3 update:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 if backend_launched:
a213053a03be flatpak: update files following names change + Python 3 update:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 print("Stopping Libervia backend")
a213053a03be flatpak: update files following names change + Python 3 update:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 subprocess.check_call(["libervia-backend", "stop"])