/usr/lib/python3/dist-packages/oneconf/dbusconnect.py is in python3-oneconf 0.3.9.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # Copyright (C) 2010 Canonical
#
# Authors:
# Didier Roche <didrocks@ubuntu.com>
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; version 3.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import dbus
import dbus.service
from gi.repository import GLib
import logging
import sys
from gettext import gettext as _
LOG = logging.getLogger(__name__)
from oneconf.enums import ONECONF_SERVICE_NAME
HOSTS_OBJECT_NAME = "/com/ubuntu/oneconf/HostsHandler"
PACKAGE_SET_INTERFACE = "com.ubuntu.OneConf.HostsHandler.PackageSetHandler"
HOSTS_INTERFACE = "com.ubuntu.OneConf.HostsHandler.Hosts"
ONECONF_DBUS_TIMEOUT = 300
def none_to_null(var):
'''return var in dbus compatible format'''
if not var:
var = ''
return var
class DbusHostsService(dbus.service.Object):
"""
Dbus service, daemon side
"""
def __init__(self, loop):
'''registration over dbus'''
bus_name = dbus.service.BusName(ONECONF_SERVICE_NAME,
bus=dbus.SessionBus())
dbus.service.Object.__init__(self, bus_name, HOSTS_OBJECT_NAME)
# Only import oneconf module now for only getting it on server side
from oneconf.hosts import Hosts
self.hosts = Hosts()
self._packageSetHandler = None
self.activity = False
self.synchandler = None
self.loop = loop
# TODO: can be a decorator, handling null case and change the API so that if it returns
# the None value -> no result
def get_packageSetHandler(self):
'''Ensure we load the package set handler at the right time'''
if not self._packageSetHandler:
from oneconf.packagesethandler import PackageSetHandler, PackageSetInitError
try:
self._packageSetHandler = PackageSetHandler(self.hosts)
except PackageSetInitError as e:
LOG.error (e)
self._packageSetHandler = None
return self._packageSetHandler
@dbus.service.method(HOSTS_INTERFACE)
def get_all_hosts(self):
self.activity = True
return self.hosts.get_all_hosts()
@dbus.service.method(HOSTS_INTERFACE)
def set_share_inventory(self, share_inventory, hostid, hostname):
self.activity = True
if share_inventory: # map to boolean to avoid difference in dbus call and direct
share_inventory = True
else:
share_inventory = False
return self.hosts.set_share_inventory(share_inventory, hostid, hostname)
@dbus.service.method(PACKAGE_SET_INTERFACE)
def get_packages(self, hostid, hostname, only_manual):
self.activity = True
if not self.get_packageSetHandler():
return ''
return none_to_null(self.get_packageSetHandler().get_packages(hostid, hostname, only_manual))
@dbus.service.method(PACKAGE_SET_INTERFACE)
def diff(self, hostid, hostname):
self.activity = True
if not self.get_packageSetHandler():
return ('', '')
return self.get_packageSetHandler().diff(hostid, hostname)
@dbus.service.method(PACKAGE_SET_INTERFACE)
def update(self):
self.activity = True
if self.get_packageSetHandler():
self.get_packageSetHandler().update()
@dbus.service.method(PACKAGE_SET_INTERFACE)
def async_update(self):
self.activity = True
if self.get_packageSetHandler():
GLib.timeout_add_seconds(1, self.get_packageSetHandler().update)
@dbus.service.signal(HOSTS_INTERFACE)
def hostlist_changed(self):
LOG.debug("Send host list changed dbus signal")
@dbus.service.signal(PACKAGE_SET_INTERFACE)
def packagelist_changed(self, hostid):
LOG.debug("Send package list changed dbus signal for hostid: %s" % hostid)
@dbus.service.signal(HOSTS_INTERFACE)
def logo_changed(self, hostid):
LOG.debug("Send logo changed dbus signal for hostid: %s" % hostid)
@dbus.service.signal(HOSTS_INTERFACE)
def latestsync_changed(self, timestamp):
LOG.debug("Send last sync timestamp: %s" % timestamp)
@dbus.service.method(HOSTS_INTERFACE)
def get_last_sync_date(self):
self.activity = True
return self.hosts.get_last_sync_date()
@dbus.service.method(HOSTS_INTERFACE)
def stop_service(self):
LOG.debug("Request for stopping OneConf service")
self.loop.quit()
return True
class DbusConnect(object):
"""
Dbus request sender, daemon connection
"""
def __init__(self):
'''connect to the bus and get packagesethandler object'''
self.bus = dbus.SessionBus()
self.hosts_dbus_object = self.bus.get_object(ONECONF_SERVICE_NAME,
HOSTS_OBJECT_NAME)
def _get_package_handler_dbusobject(self):
'''get package handler dbus object'''
return dbus.Interface(self.hosts_dbus_object, PACKAGE_SET_INTERFACE)
def _get_hosts_dbusobject(self):
'''get hosts dbus object'''
return dbus.Interface(self.hosts_dbus_object, HOSTS_INTERFACE)
def get_all_hosts(self):
'''get a dictionnary of all available hosts'''
return self._get_hosts_dbusobject().get_all_hosts()
def set_share_inventory(self, share_inventory, hostid='', hostname=''):
'''update if we share the chosen host inventory on the server'''
self._get_hosts_dbusobject().set_share_inventory(share_inventory,
hostid, hostname,
timeout=ONECONF_DBUS_TIMEOUT)
def get_packages(self, hostid, hostname, only_manual):
'''trigger getpackages handling'''
try:
return self._get_package_handler_dbusobject().get_packages(hostid,
hostname, only_manual)
except dbus.exceptions.DBusException as e:
print(e)
sys.exit(1)
def diff(self, hostid, hostname):
'''trigger diff handling'''
try:
return self._get_package_handler_dbusobject().diff(hostid,
hostname,
timeout=ONECONF_DBUS_TIMEOUT)
except dbus.exceptions.DBusException as e:
print(e)
sys.exit(1)
def update(self):
'''trigger update handling'''
self._get_package_handler_dbusobject().update(timeout=ONECONF_DBUS_TIMEOUT)
def async_update(self):
'''trigger update handling'''
self._get_package_handler_dbusobject().async_update()
def get_last_sync_date(self):
'''just send a kindly ping to retrieve the last sync date'''
return self._get_hosts_dbusobject().get_last_sync_date(timeout=ONECONF_DBUS_TIMEOUT)
def stop_service(self):
'''kindly ask the oneconf service to stop'''
try:
self._get_hosts_dbusobject().stop_service()
except dbus.exceptions.DBusException as e:
print(_("Wasn't able to request stopping the service: %s" % e))
sys.exit(1)
|