def _test_klass(self): """ Generate a class from an interface spec and verify that it has two fields, "Properties" and "Methods". """ for name, spec in self._data.items(): klass = make_class(name, spec) self.assertTrue(hasattr(klass, "Properties")) properties = getattr(klass, "Properties") self.assertTrue(hasattr(klass, "Methods")) methods = getattr(klass, "Methods") for method in spec.findall("./method"): self._test_method(methods, method) for prop in spec.findall("./property"): self._test_property(properties, prop)
if timeout_int > MAXIMUM_DBUS_TIMEOUT_MS: raise StratisCliEnvironmentError( "The timeout value provided exceeds the largest acceptable value, %s." % MAXIMUM_DBUS_TIMEOUT_MS) # Convert from milliseconds to seconds return timeout_int / 1000 try: timeout = _get_timeout( environ.get("STRATIS_DBUS_TIMEOUT", DBUS_TIMEOUT_SECONDS * 1000)) filesystem_spec = ET.fromstring(SPECS[FILESYSTEM_INTERFACE]) Filesystem = make_class("Filesystem", filesystem_spec, timeout) MOFilesystem = managed_object_class("MOFilesystem", filesystem_spec) filesystems = mo_query_builder(filesystem_spec) pool_spec = ET.fromstring(SPECS[POOL_INTERFACE]) Pool = make_class("Pool", pool_spec, timeout) MOPool = managed_object_class("MOPool", pool_spec) pools = mo_query_builder(pool_spec) blockdev_spec = ET.fromstring(SPECS[BLOCKDEV_INTERFACE]) MODev = managed_object_class("MODev", blockdev_spec) devs = mo_query_builder(blockdev_spec) Manager = make_class("Manager", ET.fromstring(SPECS[_MANAGER_INTERFACE]), timeout)
# http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """ Classes to implement dbus interface. """ import xml.etree.ElementTree as ET from dbus_python_client_gen import make_class from ._data import SPECS TIME_OUT = 120 # In seconds ObjectManager = make_class( "ObjectManager", ET.fromstring(SPECS['org.freedesktop.DBus.ObjectManager']), TIME_OUT) Manager = make_class("Manager", ET.fromstring(SPECS['org.storage.stratis1.Manager']), TIME_OUT) Filesystem = make_class( "Filesystem", ET.fromstring(SPECS['org.storage.stratis1.filesystem']), TIME_OUT) Pool = make_class("Pool", ET.fromstring(SPECS['org.storage.stratis1.pool']), TIME_OUT)
_POOL_SPEC = ET.fromstring(SPECS["org.storage.stratis2.pool"]) _FILESYSTEM_SPEC = ET.fromstring(SPECS["org.storage.stratis2.filesystem"]) _BLOCKDEV_SPEC = ET.fromstring(SPECS["org.storage.stratis2.blockdev"]) pools = mo_query_builder(_POOL_SPEC) filesystems = mo_query_builder(_FILESYSTEM_SPEC) blockdevs = mo_query_builder(_BLOCKDEV_SPEC) MOPool = managed_object_class("MOPool", _POOL_SPEC) MOBlockDev = managed_object_class("MOBlockDev", _BLOCKDEV_SPEC) TIME_OUT = 120 # In seconds ObjectManager = make_class( "ObjectManager", ET.fromstring(SPECS["org.freedesktop.DBus.ObjectManager"]), TIME_OUT, ) Manager = make_class( "Manager", ET.fromstring(SPECS["org.storage.stratis2.Manager"]), TIME_OUT ) FetchProperties = make_class( "FetchProperties", ET.fromstring(SPECS["org.storage.stratis2.FetchProperties"]), TIME_OUT, ) Filesystem = make_class("Filesystem", _FILESYSTEM_SPEC, TIME_OUT) Pool = make_class("Pool", _POOL_SPEC, TIME_OUT) Blockdev = make_class("Blockdev", _BLOCKDEV_SPEC, TIME_OUT)
<arg name="return_string" type="s" direction="out"/> </method> </interface> """, } _SERVICE = "org.storage.stratis3" _INTROSPECTABLE_IFACE = "org.freedesktop.DBus.Introspectable" _MANAGER_IFACE = "org.storage.stratis3.Manager.r0" _POOL_IFACE = "org.storage.stratis3.pool.r0" _TIMEOUT = 120000 # pylint: disable=invalid-name Introspectable = make_class("Introspectable", ET.fromstring(SPECS[_INTROSPECTABLE_IFACE]), _TIMEOUT) Manager = make_class("Manager", ET.fromstring(SPECS[_MANAGER_IFACE]), _TIMEOUT) Pool = make_class("Pool", ET.fromstring(SPECS[_POOL_IFACE]), _TIMEOUT) OBJECT_MANAGER_INTERFACE = "org.freedesktop.DBus.ObjectManager" TOP_OBJECT_INTERFACE_PREFIXES = [ "org.storage.stratis3.Manager", "org.storage.stratis3.Report", ] POOL_OBJECT_INTERFACE_PREFIXES = ["org.storage.stratis3.pool"] BLOCKDEV_OBJECT_INTERFACE_PREFIXES = ["org.storage.stratis3.blockdev"] FILESYSTEM_OBJECT_INTERFACE_PREFIXES = ["org.storage.stratis3.filesystem"]
from dbus_client_gen import managed_object_class from dbus_client_gen import mo_query_builder from dbus_python_client_gen import make_class from ._data import SPECS _POOL_SPEC = ET.fromstring(SPECS["org.storage.stratis1.pool"]) _FILESYSTEM_SPEC = ET.fromstring(SPECS["org.storage.stratis1.filesystem"]) _BLOCKDEV_SPEC = ET.fromstring(SPECS["org.storage.stratis1.blockdev"]) pools = mo_query_builder(_POOL_SPEC) filesystems = mo_query_builder(_FILESYSTEM_SPEC) blockdevs = mo_query_builder(_BLOCKDEV_SPEC) MOPool = managed_object_class("MOPool", _POOL_SPEC) MOBlockDev = managed_object_class("MOBlockDev", _BLOCKDEV_SPEC) TIME_OUT = 120 # In seconds ObjectManager = make_class( "ObjectManager", ET.fromstring(SPECS["org.freedesktop.DBus.ObjectManager"]), TIME_OUT, ) Manager = make_class("Manager", ET.fromstring(SPECS["org.storage.stratis1.Manager"]), TIME_OUT) Filesystem = make_class("Filesystem", _FILESYSTEM_SPEC, TIME_OUT) Pool = make_class("Pool", _POOL_SPEC, TIME_OUT)
from ._introspect import SPECS assert hasattr(sys.modules.get("stratis_cli"), "run"), ( "This module is being loaded too eagerly. Make sure that loading it is " "deferred until after the stratis_cli module has been fully loaded.") DBUS_TIMEOUT_SECONDS = 120 try: # pylint: disable=invalid-name timeout = get_timeout( os.environ.get("STRATIS_DBUS_TIMEOUT", DBUS_TIMEOUT_SECONDS * 1000)) fetch_properties_spec = ET.fromstring(SPECS[FETCH_PROPERTIES_INTERFACE]) FetchProperties = make_class("FetchProperties", fetch_properties_spec, timeout) report_spec = ET.fromstring(SPECS[REPORT_INTERFACE]) Report = make_class("Report", report_spec, timeout) filesystem_spec = ET.fromstring(SPECS[FILESYSTEM_INTERFACE]) Filesystem = make_class("Filesystem", filesystem_spec, timeout) MOFilesystem = managed_object_class("MOFilesystem", filesystem_spec) filesystems = mo_query_builder(filesystem_spec) pool_spec = ET.fromstring(SPECS[POOL_INTERFACE]) Pool = make_class("Pool", pool_spec, timeout) MOPool = managed_object_class("MOPool", pool_spec) pools = mo_query_builder(pool_spec) blockdev_spec = ET.fromstring(SPECS[BLOCKDEV_INTERFACE])
# a minimal chunk of introspection data, enough for the methods needed. _SPECS = { "org.freedesktop.DBus.ObjectManager": """ <interface name="org.freedesktop.DBus.ObjectManager"> <method name="GetManagedObjects" /> </interface> """ } _TIMEOUT = 120000 _OBJECT_MANAGER_IFACE = "org.freedesktop.DBus.ObjectManager" _OBJECT_MANAGER = make_class("ObjectManager", ET.fromstring(_SPECS[_OBJECT_MANAGER_IFACE]), _TIMEOUT) def _interfaces_added(object_path, interfaces_added): """ Update the record with the interfaces added. :param str object_path: D-Bus object path :param dict interfaces_added: map of interfaces to D-Bus properties """ # pylint: disable=global-statement global _MO, _TOP_OBJECT if _MO is None: _MO = _OBJECT_MANAGER.Methods.GetManagedObjects(_TOP_OBJECT, {}) else:
<annotation name="org.freedesktop.DBus.Property.EmitsChangedSignal" value="false"/> </property> <property name="Uuid" type="s" access="read"> <annotation name="org.freedesktop.DBus.Property.EmitsChangedSignal" value="const"/> </property> </interface> """, } _FILESYSTEM_INTERFACE = 'org.storage.stratis1.filesystem' _POOL_INTERFACE = 'org.storage.stratis1.pool' _BLOCKDEV_INTERFACE = 'org.storage.stratis1.blockdev' try: filesystem_spec = ET.fromstring(SPECS[_FILESYSTEM_INTERFACE]) Filesystem = make_class("Filesystem", filesystem_spec) MOFilesystem = managed_object_class("MOFilesystem", filesystem_spec) filesystems = mo_query_builder(filesystem_spec) pool_spec = ET.fromstring(SPECS[_POOL_INTERFACE]) Pool = make_class("Pool", pool_spec) MOPool = managed_object_class("MOPool", pool_spec) pools = mo_query_builder(pool_spec) blockdev_spec = ET.fromstring(SPECS[_BLOCKDEV_INTERFACE]) MODev = managed_object_class("MODev", blockdev_spec) devs = mo_query_builder(blockdev_spec) Manager = make_class("Manager", ET.fromstring(SPECS['org.storage.stratis1.Manager']))
""" if interface_name == _BLOCKDEV_INTERFACE: return "block device" if interface_name == _FILESYSTEM_INTERFACE: return "filesystem" if interface_name == _POOL_INTERFACE: return "pool" raise StratisCliValueError(interface_name, "interface_name") try: filesystem_spec = ET.fromstring(SPECS[_FILESYSTEM_INTERFACE]) Filesystem = make_class("Filesystem", filesystem_spec, DBUS_TIMEOUT_SECONDS) MOFilesystem = managed_object_class("MOFilesystem", filesystem_spec) filesystems = mo_query_builder(filesystem_spec) pool_spec = ET.fromstring(SPECS[_POOL_INTERFACE]) Pool = make_class("Pool", pool_spec, DBUS_TIMEOUT_SECONDS) MOPool = managed_object_class("MOPool", pool_spec) pools = mo_query_builder(pool_spec) blockdev_spec = ET.fromstring(SPECS[_BLOCKDEV_INTERFACE]) MODev = managed_object_class("MODev", blockdev_spec) devs = mo_query_builder(blockdev_spec) Manager = make_class("Manager", ET.fromstring(SPECS[_MANAGER_INTERFACE]), DBUS_TIMEOUT_SECONDS)
<interface name="org.freedesktop.DBus.Properties"> <method name="GetAll"> <arg name="interface_name" type="s" direction="in"/> <arg name="props" type="a{sv}" direction="out"/> </method> </interface> """, } _TIMEOUT = 120000 _OBJECT_MANAGER_IFACE = "org.freedesktop.DBus.ObjectManager" _PROPERTIES_IFACE = "org.freedesktop.DBus.Properties" _OBJECT_MANAGER = make_class( "ObjectManager", ET.fromstring(_SPECS[_OBJECT_MANAGER_IFACE]), _TIMEOUT ) _PROPERTIES = make_class( "Properties", ET.fromstring(_SPECS[_PROPERTIES_IFACE]), _TIMEOUT ) def _make_mo(): """ Returns the result of calling ObjectManager.GetManagedObjects + the result of calling Properties.GetAll on the top object for selected interfaces. """ mos = _OBJECT_MANAGER.Methods.GetManagedObjects(_TOP_OBJECT, {}) mos[_TOP_OBJECT_PATH] = {}
MANAGER_INTERFACE, POOL_INTERFACE, REPORT_INTERFACE, ) from ._introspect import SPECS _POOL_SPEC = ET.fromstring(SPECS[POOL_INTERFACE]) _FILESYSTEM_SPEC = ET.fromstring(SPECS[FILESYSTEM_INTERFACE]) _BLOCKDEV_SPEC = ET.fromstring(SPECS[BLOCKDEV_INTERFACE]) pools = mo_query_builder(_POOL_SPEC) filesystems = mo_query_builder(_FILESYSTEM_SPEC) blockdevs = mo_query_builder(_BLOCKDEV_SPEC) MOPool = managed_object_class("MOPool", _POOL_SPEC) MOBlockDev = managed_object_class("MOBlockDev", _BLOCKDEV_SPEC) TIME_OUT = 120 # In seconds ObjectManager = make_class( "ObjectManager", ET.fromstring(SPECS["org.freedesktop.DBus.ObjectManager"]), TIME_OUT, ) Report = make_class("Report", ET.fromstring(SPECS[REPORT_INTERFACE]), TIME_OUT) Manager = make_class("Manager", ET.fromstring(SPECS[MANAGER_INTERFACE]), TIME_OUT) Filesystem = make_class("Filesystem", _FILESYSTEM_SPEC, TIME_OUT) Pool = make_class("Pool", _POOL_SPEC, TIME_OUT) Blockdev = make_class("Blockdev", _BLOCKDEV_SPEC, TIME_OUT)