This patch adds a few functions to virtinst.util: 1. is_storage_capable: receives an active connection and attempts to determine if it supports storage management. This is used in a few places to maintain existing behavior and error paths for connections to older libvirt versions. 2. is_remote: receives a uri and determines if it specifies a remote connection. Used for various validation checks. 3. get_xml_path: Receives an xml blob and an xPath, and returns that path from the xml. This is largely a convenience function since we have this code duplicated in about 10 places throughout virtinst and virt-manager. Thanks, Cole
# HG changeset patch # User "Cole Robinson <crobinso@xxxxxxxxxx>" # Date 1217990239 14400 # Node ID 3fd6f72be4ee2a841ecf7ef8428284c5c365a6ea # Parent 4fc92b37ef96adb71f3890343e7e3ca77c7bdce8 Add is_remote, is_storage_capable, and get_xml_path helpers to util. diff -r 4fc92b37ef96 -r 3fd6f72be4ee virtinst/util.py --- a/virtinst/util.py Tue Aug 05 21:17:37 2008 -0400 +++ b/virtinst/util.py Tue Aug 05 22:37:19 2008 -0400 @@ -23,6 +23,7 @@ import random import os.path import re +import libxml2 import logging from sys import stderr @@ -297,3 +298,80 @@ if platform.system() == "SunOS": return "/usr/lib/xen/bin/pygrub" return "/usr/bin/pygrub" + +def is_remote(uri): + def uri_split(uri): + username = netloc = query = fragment = '' + i = uri.find(":") + if i > 0: + scheme, uri = uri[:i].lower(), uri[i+1:] + if uri[:2] == '//': + netloc, uri = splitnetloc(uri, 2) + offset = netloc.find("@") + if offset > 0: + username = netloc[0:offset] + netloc = netloc[offset+1:] + if '#' in uri: + uri, fragment = uri.split('#', 1) + if '?' in uri: + uri, query = uri.split('?', 1) + else: + scheme = uri.lower() + + return scheme, username, netloc, uri, query, fragment + + def splitnetloc(url, start=0): + for c in '/?#': # the order is important! + delim = url.find(c, start) + if delim >= 0: + break + else: + delim = len(url) + return url[start:delim], url[delim:] + + try: + (scheme, username, netloc, path, query, fragment) = uri_split(uri) + if netloc == "": + return False + return True + except Exception, e: + logging.exception("Error parsing URI in is_remote: %s" % e) + return True + +def is_storage_capable(conn): + """check if virConnectPtr passed has storage API support""" + if not isinstance(conn, libvirt.virConnect): + raise ValueError(_("'conn' must be a virConnect instance.")) + try: + if not dir(conn).count("listStoragePools"): + return False + n = conn.listStoragePools() + except libvirt.libvirtError, e: + if e.get_error_code() == libvirt.VIR_ERR_RPC: + return False + return True + +def get_xml_path(xml, path): + """return the xpath from the passed xml""" + doc = None + ctx = None + result = None + try: + doc = libxml2.parseDoc(xml) + ctx = doc.xpathNewContext() + ret = ctx.xpathEval(path) + str = None + if ret != None: + if type(ret) == list: + if len(ret) == 1: + str = ret[0].content + else: + str = ret + result = str + finally: + if doc: + doc.freeDoc() + if ctx: + ctx.xpathFreeContext() + return result +
_______________________________________________ et-mgmt-tools mailing list et-mgmt-tools@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/et-mgmt-tools