view flatpak/libervia_wrapper-dbus_keep.py @ 171:a213053a03be

flatpak: update files following names change + Python 3 update: - `build_manifest.py` has been fixed to work with recent SàT/Libervia - filenames/scripts have been udpated to reflect project name change - installation now uses `requirements.txt` when dev version is requested - there are now 3 types of commands wrapper: * `libervia_wrapper.py` uses `pb` bridge, starts backend before frontend, and stops it when frontend is stopped. It's used by `Libervia Desktop` (Cagou) * `libervia_wrapper-dbus.py` uses `dbus` bridge, starts backend before frontend, and stops it when frontend is stopped. It's used for `Libervia TUI` (Primitivus) * `libervia_wrapper-dbus.py` uses `dbus` bridge, starts backend if necessary and doesn't stop it (to avoid waiting for backend start next time). It's used by `Libervia CLI` (jp).
author Goffi <goffi@goffi.org>
date Tue, 30 Nov 2021 21:42:06 +0100
parents
children
line wrap: on
line source

#!/usr/bin/env python3

"""This script launch Libervia backend if it's not before running command, and keep it running afterwards"""

# We keep the backend running to avoid the backend launching cost each time the frontend is used

import os
import sys
import dbus
import subprocess
from sat.tools import config

command = "##COMMAND##"
const_INT_PREFIX = "org.libervia.LiberviaCLI"
const_OBJ_PATH = '/org/libervia/Libervia/bridge'

const_INT_PREFIX = config.getConfig(
    config.parseMainConf(),
    "",
    "bridge_dbus_int_prefix",
    "org.libervia.Libervia")

# we remove any path with "/home" from PYTHONPATH because we have
# "--filesystem=home" permission and packages in .local/lib/pythonX/site-packages may
# conflict with the ones from flatpak
clean_pythonpath = ':'.join(p for p in sys.path if p and not p.startswith('/home'))
os.environ['PYTHONPATH'] = clean_pythonpath
os.environ['PYTHONNOUSERSITE'] = '1'

try:
    sessions_bus = dbus.SessionBus()
    db_object = sessions_bus.get_object(const_INT_PREFIX,
                                        const_OBJ_PATH)
except dbus.exceptions.DBusException as e:
    if e._dbus_error_name != 'org.freedesktop.DBus.Error.ServiceUnknown':
        raise e
    # backend not found, we need to launch it
    print("Launching Libervia backend")
    subprocess.check_call("libervia-backend")

os.execlp(command, command, *sys.argv[1:])