diff options
author | Cole Robinson <crobinso@redhat.com> | 2019-02-07 17:42:25 -0500 |
---|---|---|
committer | Cole Robinson <crobinso@redhat.com> | 2019-02-07 17:42:55 -0500 |
commit | fb5aadc63636349491fbf76c74705b9c3c7ed82c (patch) | |
tree | 0d56a092e3b79976ae9b633f76f7d876af11f04a | |
parent | 10973c793221c0d74cb0898b4340f3f95eccc094 (diff) | |
download | virt-manager-fb5aadc63636349491fbf76c74705b9c3c7ed82c.tar.gz |
guest: Move compare_device to Device class
It operates on a Device object, so it makes sense
-rw-r--r-- | virtinst/devices/device.py | 53 | ||||
-rw-r--r-- | virtinst/guest.py | 48 |
2 files changed, 54 insertions, 47 deletions
diff --git a/virtinst/devices/device.py b/virtinst/devices/device.py index ada82c17..4ed5f17d 100644 --- a/virtinst/devices/device.py +++ b/virtinst/devices/device.py @@ -114,3 +114,56 @@ class Device(XMLBuilder): @property def DEVICE_TYPE(self): return self.XML_NAME + + def compare_device(self, newdev, idx): + """ + Attempt to compare this device against the passed @newdev, + using various heuristics. For example, when removing a device + from both active and inactive XML, the device XML my be very + different or the devices may appear in different orders, so + we have to do some fuzzy matching to determine if the devices + are a 'match' + """ + devprops = { + "disk": ["target", "bus"], + "interface": ["macaddr", "xmlindex"], + "input": ["bus", "type", "xmlindex"], + "sound": ["model", "xmlindex"], + "video": ["model", "xmlindex"], + "watchdog": ["xmlindex"], + "hostdev": ["type", "managed", "xmlindex", + "product", "vendor", + "function", "domain", "slot"], + "serial": ["type", "target_port"], + "parallel": ["type", "target_port"], + "console": ["type", "target_type", "target_port"], + "graphics": ["type", "xmlindex"], + "controller": ["type", "index"], + "channel": ["type", "target_name"], + "filesystem": ["target", "xmlindex"], + "smartcard": ["mode", "xmlindex"], + "redirdev": ["bus", "type", "xmlindex"], + "tpm": ["type", "xmlindex"], + "rng": ["type", "xmlindex"], + "panic": ["type", "xmlindex"], + "vsock": ["xmlindex"], + } + + if id(self) == id(newdev): + return True + + if not isinstance(self, type(newdev)): + return False + + for devprop in devprops[self.DEVICE_TYPE]: + if devprop == "xmlindex": + origval = self.get_xml_idx() + newval = idx + else: + origval = getattr(self, devprop) + newval = getattr(newdev, devprop) + + if origval != newval: + return False + + return True diff --git a/virtinst/guest.py b/virtinst/guest.py index 6966e6c3..13b88cf2 100644 --- a/virtinst/guest.py +++ b/virtinst/guest.py @@ -22,52 +22,6 @@ from .xmlbuilder import XMLBuilder, XMLProperty, XMLChildProperty _ignore = Device -def compare_device(origdev, newdev, idx): - devprops = { - "disk": ["target", "bus"], - "interface": ["macaddr", "xmlindex"], - "input": ["bus", "type", "xmlindex"], - "sound": ["model", "xmlindex"], - "video": ["model", "xmlindex"], - "watchdog": ["xmlindex"], - "hostdev": ["type", "managed", "xmlindex", - "product", "vendor", - "function", "domain", "slot"], - "serial": ["type", "target_port"], - "parallel": ["type", "target_port"], - "console": ["type", "target_type", "target_port"], - "graphics": ["type", "xmlindex"], - "controller": ["type", "index"], - "channel": ["type", "target_name"], - "filesystem": ["target", "xmlindex"], - "smartcard": ["mode", "xmlindex"], - "redirdev": ["bus", "type", "xmlindex"], - "tpm": ["type", "xmlindex"], - "rng": ["type", "xmlindex"], - "panic": ["type", "xmlindex"], - "vsock": ["xmlindex"], - } - - if id(origdev) == id(newdev): - return True - - if not isinstance(origdev, type(newdev)): - return False - - for devprop in devprops[origdev.DEVICE_TYPE]: - if devprop == "xmlindex": - origval = origdev.get_xml_idx() - newval = idx - else: - origval = getattr(origdev, devprop) - newval = getattr(newdev, devprop) - - if origval != newval: - return False - - return True - - class _DomainDevices(XMLBuilder): XML_NAME = "devices" _XML_PROP_ORDER = ['disk', 'controller', 'filesystem', 'interface', @@ -446,7 +400,7 @@ class Guest(XMLBuilder): """ devlist = getattr(self.devices, origdev.DEVICE_TYPE) for idx, dev in enumerate(devlist): - if compare_device(origdev, dev, idx): + if origdev.compare_device(dev, idx): return dev return None |