From 318ba7e474da9b36bc636b0a314f6855ad086594 Mon Sep 17 00:00:00 2001 From: Cole Robinson Date: Tue, 9 Jul 2013 19:50:49 -0400 Subject: [PATCH] VirtualConnection: Deal with Guest objects, not raw XML We already do XML parsing, no need to open code it elsewhere --- virtManager/connection.py | 4 +++- virtManager/domain.py | 1 + virtinst/VirtualDisk.py | 27 +++++++++++---------------- virtinst/VirtualNetworkInterface.py | 19 +++++++------------ virtinst/connection.py | 7 +++++-- 5 files changed, 27 insertions(+), 31 deletions(-) diff --git a/virtManager/connection.py b/virtManager/connection.py index 038dded97..4bb6bac97 100644 --- a/virtManager/connection.py +++ b/virtManager/connection.py @@ -135,7 +135,9 @@ class vmmConnection(vmmGObject): ################# def _init_virtconn(self): - self._backend.cb_fetch_all_guests = lambda: self.vms.values() + self._backend.cb_fetch_all_guests = ( + lambda: [vm.get_guest_for_virtinst_func(refresh_if_nec=False) + for vm in self.vms.values()]) self._backend.cb_fetch_all_pools = lambda: self.pools.values() def _init_netdev(self): diff --git a/virtManager/domain.py b/virtManager/domain.py index e133815e0..ac739306a 100644 --- a/virtManager/domain.py +++ b/virtManager/domain.py @@ -404,6 +404,7 @@ class vmmDomain(vmmLibvirtObject): return self._build_guest(xml) return self._guest + get_guest_for_virtinst_func = _get_guest def _build_guest(self, xml): return virtinst.Guest(self.conn.get_backend(), diff --git a/virtinst/VirtualDisk.py b/virtinst/VirtualDisk.py index c2380dd3d..3c55d442e 100644 --- a/virtinst/VirtualDisk.py +++ b/virtinst/VirtualDisk.py @@ -467,24 +467,19 @@ class VirtualDisk(VirtualDevice): return vms = conn.fetch_all_guests() - - def count_cb(ctx): - template = "count(/domain/devices/disk[" - if check_conflict: - template += "not(shareable) and " - template += "source/@%s='%s'])" - - for dtype in VirtualDisk._target_props: - xpath = template % (dtype, util.xml_escape(path)) - if ctx.xpathEval(xpath): - return True - return False - names = [] for vm in vms: - xml = vm.get_xml(refresh_if_nec=False) - if util.get_xml_path(xml, func=count_cb): - names.append(vm.get_backend().name()) + found = False + for disk in vm.get_devices("disk"): + if disk.path != path: + continue + if check_conflict: + if disk.shareable: + continue + found = True + break + if found: + names.append(vm.name) return names diff --git a/virtinst/VirtualNetworkInterface.py b/virtinst/VirtualNetworkInterface.py index b90b29fff..cef9ad2df 100644 --- a/virtinst/VirtualNetworkInterface.py +++ b/virtinst/VirtualNetworkInterface.py @@ -316,18 +316,13 @@ class VirtualNetworkInterface(VirtualDevice): if searchmac is None: return (False, None) - def count_cb(ctx): - for mac in ctx.xpathEval("/domain/devices/interface/mac"): - macaddr = mac.xpathEval("attribute::address")[0].content - if macaddr and _compareMAC(searchmac, macaddr) == 0: - return True - return False - - for vm in self.conn.fetch_all_guests(): - xml = vm.get_xml(refresh_if_nec=False) - if util.get_xml_path(xml, func=count_cb): - return (True, _("The MAC address '%s' is in use " - "by another virtual machine.") % searchmac) + vms = self.conn.fetch_all_guests() + for vm in vms: + for nic in vm.get_devices("interface"): + nicmac = nic.macaddr or "" + if nicmac.lower() == searchmac.lower(): + return (True, _("The MAC address '%s' is in use " + "by another virtual machine.") % searchmac) return (False, None) def setup(self, meter=None): diff --git a/virtinst/connection.py b/virtinst/connection.py index ff9d76d16..3895a2c9e 100644 --- a/virtinst/connection.py +++ b/virtinst/connection.py @@ -19,13 +19,15 @@ import logging import os import re +import weakref import libvirt +from virtinst import Guest +from virtinst import CapabilitiesParser from virtinst import pollhelpers from virtinst import support from virtinst import util -from virtinst import CapabilitiesParser from virtinst.cli import parse_optstr _virtinst_uri_magic = "__virtinst_test__" @@ -183,7 +185,8 @@ class VirtualConnection(object): ignore, ignore, ret = pollhelpers.fetch_vms(self, {}, lambda obj, ignore: obj) - ret = [_FetchObjWrapper(obj) for obj in ret.values()] + ret = [Guest(weakref.ref(self), parsexml=obj.XMLDesc(0)) + for obj in ret.values()] if self.cache_object_fetch: self._fetch_cache[key] = ret return ret