From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id 4FFE4A0471 for ; Tue, 13 Aug 2019 07:33:48 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 411DF1BE84; Tue, 13 Aug 2019 07:33:48 +0200 (CEST) Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by dpdk.org (Postfix) with ESMTP id D7FDD1B203 for ; Tue, 13 Aug 2019 07:33:46 +0200 (CEST) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 12 Aug 2019 22:33:46 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.64,380,1559545200"; d="scan'208";a="170289888" Received: from dpdk-moyufen01.sh.intel.com ([10.67.111.77]) by orsmga008.jf.intel.com with ESMTP; 12 Aug 2019 22:33:45 -0700 From: yufengmx To: dts@dpdk.org Cc: yufengmx Date: Tue, 13 Aug 2019 13:34:44 +0800 Message-Id: <1565674484-21165-2-git-send-email-yufengx.mo@intel.com> X-Mailer: git-send-email 1.9.3 In-Reply-To: <1565674484-21165-1-git-send-email-yufengx.mo@intel.com> References: <1565674484-21165-1-git-send-email-yufengx.mo@intel.com> Subject: [dts] [PATCH V1]framework\qemu_libvirt: fix xml format and pep8 issue X-BeenThere: dts@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: test suite reviews and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dts-bounces@dpdk.org Sender: "dts" *. check vm xml file existence before remove it to fix warning of using except. *. format libvirt xml content with pretty style to make vm config file easier to set manually. *. fix pep8 issue. Signed-off-by: yufengmx --- framework/qemu_libvirt.py | 60 ++++++++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 24 deletions(-) diff --git a/framework/qemu_libvirt.py b/framework/qemu_libvirt.py index 66fc54b..ab62ab8 100644 --- a/framework/qemu_libvirt.py +++ b/framework/qemu_libvirt.py @@ -44,6 +44,7 @@ from config import VIRTCONF from exception import StartVMFailedException import xml.etree.ElementTree as ET from xml.etree.ElementTree import ElementTree +from xml.dom import minidom class LibvirtKvm(VirtBase): @@ -102,7 +103,8 @@ class LibvirtKvm(VirtBase): """ arch = self.host_session.send_expect('uname -m', '# ') if arch == 'aarch64': - out = self.host_session.send_expect('service libvirtd status', "# ") + out = self.host_session.send_expect( + 'service libvirtd status', "# ") if 'active (running)' not in out: return False return True @@ -214,13 +216,12 @@ class LibvirtKvm(VirtBase): os = self.domain.find('os') if 'loader' in options.keys(): loader = ET.SubElement( - os, 'loader', {'readonly': 'yes', 'type': 'pflash'}) + os, 'loader', {'readonly': 'yes', 'type': 'pflash'}) loader.text = options['loader'] if 'nvram' in options.keys(): nvram = ET.SubElement(os, 'nvram') nvram.text = options['nvram'] - def set_vm_default_aarch64(self): os = ET.SubElement(self.domain, 'os') type = ET.SubElement( @@ -231,7 +232,7 @@ class LibvirtKvm(VirtBase): ET.SubElement(features, 'acpi') ET.SubElement(self.domain, 'cpu', - {'mode': 'host-passthrough', 'check': 'none'}) + {'mode': 'host-passthrough', 'check': 'none'}) def set_vm_default_x86_64(self): os = ET.SubElement(self.domain, 'os') @@ -252,7 +253,6 @@ class LibvirtKvm(VirtBase): set_default_func = getattr(self, 'set_vm_default_' + arch) if callable(set_default_func): set_default_func() - # qemu-kvm for emulator device = ET.SubElement(self.domain, 'devices') @@ -338,13 +338,14 @@ class LibvirtKvm(VirtBase): if 'opt_controller' in options: controller = ET.SubElement(devices, 'controller', - {'type': bus, - 'index': hex(self.controllerindex)[2:], - 'model': options['opt_controller']}) + {'type': bus, + 'index': hex(self.controllerindex)[2:], + 'model': options['opt_controller']}) self.controllerindex += 1 - ET.SubElement(controller, 'address', + ET.SubElement( + controller, 'address', {'type': 'pci', 'domain': '0x0000', 'bus': hex(self.pciindex), - 'slot': '0x00', 'function': '0x00'}) + 'slot': '0x00', 'function': '0x00'}) self.pciindex += 1 def add_vm_serial_port(self, **options): @@ -356,18 +357,26 @@ class LibvirtKvm(VirtBase): else: serial_type = 'unix' if serial_type == 'pty': - serial = ET.SubElement(devices, 'serial', {'type': serial_type}) + serial = ET.SubElement( + devices, 'serial', {'type': serial_type}) ET.SubElement(serial, 'target', {'port': '0'}) elif serial_type == 'unix': - serial = ET.SubElement(devices, 'serial', {'type': serial_type}) + serial = ET.SubElement( + devices, 'serial', {'type': serial_type}) self.serial_path = "/tmp/%s_serial.sock" % self.vm_name - ET.SubElement(serial, 'source', {'mode': 'bind', 'path': self.serial_path}) + ET.SubElement( + serial, + 'source', + {'mode': 'bind', 'path': self.serial_path}) ET.SubElement(serial, 'target', {'port': '0'}) else: - print utils.RED("Serial type %s is not supported!" % serial_type) + print utils.RED( + "Serial type %s is not supported!" % serial_type) return False - console = ET.SubElement(devices, 'console', {'type': serial_type}) - ET.SubElement(console, 'target', {'type': 'serial', 'port': '0'}) + console = ET.SubElement( + devices, 'console', {'type': serial_type}) + ET.SubElement( + console, 'target', {'type': 'serial', 'port': '0'}) def add_vm_login(self, **options): """ @@ -396,14 +405,14 @@ class LibvirtKvm(VirtBase): bus = m.group(1) slot = m.group(2) func = m.group(3) - dom = '0' + dom = '0' return (bus, slot, func, dom) m = re.match(pci_regex_domain, pci_address) if m is not None: bus = m.group(2) slot = m.group(3) func = m.group(4) - dom = m.group(1) + dom = m.group(1) return (bus, slot, func, dom) return None @@ -427,7 +436,6 @@ class LibvirtKvm(VirtBase): print utils.RED("Missing opt_host for device option!!!") return False - pci = self.__parse_pci(pci_addr) if pci is None: return False @@ -449,8 +457,8 @@ class LibvirtKvm(VirtBase): return False bus, slot, func, dom = pci ET.SubElement(hostdevice, 'address', { - 'type': 'pci', 'domain': '0x%s' % dom, 'bus': '0x%s' % bus, - 'slot': '0x%s' % slot, 'function': '0x%s' % func}) + 'type': 'pci', 'domain': '0x%s' % dom, 'bus': '0x%s' % bus, + 'slot': '0x%s' % slot, 'function': '0x%s' % func}) # save host and guest pci address mapping pci_map = {} pci_map['hostpci'] = pci_addr @@ -570,11 +578,15 @@ class LibvirtKvm(VirtBase): def _start_vm(self): xml_file = "/tmp/%s.xml" % self.vm_name - try: + if os.path.exists(xml_file): os.remove(xml_file) - except: - pass self.root.write(xml_file) + with open(xml_file, 'rb') as fp: + content = fp.read() + doc = minidom.parseString(content) + vm_content = doc.toprettyxml(indent=' ') + with open(xml_file, 'wb') as fp: + fp.write(vm_content) self.host_session.copy_file_to(xml_file) time.sleep(2) -- 1.9.3