virtinst: util: Move URI helpers to their own file

Helps us avoid some relative imports, and makes things cleaner.
This commit is contained in:
Cole Robinson 2013-04-11 12:04:28 -04:00
parent 69d3f21da6
commit 927bcc11cd
11 changed files with 210 additions and 169 deletions

View File

@ -34,6 +34,7 @@ import virtinst
import virtinst.CapabilitiesParser
import virtinst.cli as cli
import virtinst.util as util
from virtinst import uriutil
from virtinst.VirtualDevice import VirtualDevice
from virtinst.cli import fail, print_stdout, print_stderr
@ -287,7 +288,7 @@ def get_virt_type(conn, options):
if (not req_virt_type and
not req_hv_type and
req_accel and
util.is_qemu(conn) and
uriutil.is_qemu(conn) and
capsguest.arch in ["i686", "x86_64"] and
not capsdomain.is_accelerated()):
logging.warn("KVM acceleration not available, using '%s'",

View File

@ -24,6 +24,7 @@ from gi.repository import Gtk
from gi.repository import GConf
import virtinst
from virtinst import uriutil
from virtManager.keyring import vmmKeyring, vmmSecret
@ -201,7 +202,7 @@ class vmmConfig(object):
suffix = "connection_prefs/%s" % GConf.escape_key(uri, len(uri))
return self._perobj_helper(suffix, pref_func, func_type, value)
def _perhost_helper(self, uri, pref_func, func_type, value=None):
host = virtinst.util.get_uri_hostname(uri)
host = uriutil.get_uri_hostname(uri)
if not host:
host = "localhost"
suffix = "connection_prefs/hosts/%s" % host

View File

@ -30,6 +30,7 @@ import traceback
import libvirt
import virtinst
from virtinst import uriutil
from virtManager import util
from virtManager import connectauth
@ -320,7 +321,7 @@ class vmmConnection(vmmGObject):
return socket.gethostbyaddr(socket.gethostname())[0]
def get_uri_hostname(self):
return virtinst.util.get_uri_hostname(self.get_uri())
return uriutil.get_uri_hostname(self.get_uri())
def get_short_hostname(self):
hostname = self.get_hostname()
@ -337,10 +338,10 @@ class vmmConnection(vmmGObject):
return self.get_uri_hostname()
def get_transport(self):
return virtinst.util.get_uri_transport(self.get_uri())
return uriutil.get_uri_transport(self.get_uri())
def get_driver(self):
return virtinst.util.get_uri_driver(self.get_uri())
return uriutil.get_uri_driver(self.get_uri())
def is_local(self):
return bool(self.get_uri_hostname() == "localhost")
@ -352,51 +353,51 @@ class vmmConnection(vmmGObject):
if self._is_virtinst_test_uri:
self.get_uri().count(",lxc")
return virtinst.util.uri_split(self.get_uri())[0].startswith("lxc")
return uriutil.uri_split(self.get_uri())[0].startswith("lxc")
def is_openvz(self):
return virtinst.util.uri_split(self.get_uri())[0].startswith("openvz")
return uriutil.uri_split(self.get_uri())[0].startswith("openvz")
def is_xen(self):
if self._is_virtinst_test_uri:
return self.get_uri().count(",xen")
scheme = virtinst.util.uri_split(self.get_uri())[0]
scheme = uriutil.uri_split(self.get_uri())[0]
return scheme.startswith("xen")
def is_qemu(self):
if self._is_virtinst_test_uri:
return self.get_uri().count(",qemu")
scheme = virtinst.util.uri_split(self.get_uri())[0]
scheme = uriutil.uri_split(self.get_uri())[0]
return scheme.startswith("qemu")
def is_remote(self):
return virtinst.util.is_uri_remote(self.get_uri())
return uriutil.is_uri_remote(self.get_uri())
def is_qemu_system(self):
(scheme, ignore, ignore,
path, ignore, ignore) = virtinst.util.uri_split(self.get_uri())
path, ignore, ignore) = uriutil.uri_split(self.get_uri())
if path == "/system" and scheme.startswith("qemu"):
return True
return False
def is_qemu_session(self):
(scheme, ignore, ignore,
path, ignore, ignore) = virtinst.util.uri_split(self.get_uri())
path, ignore, ignore) = uriutil.uri_split(self.get_uri())
if path == "/session" and scheme.startswith("qemu"):
return True
return False
def is_test_conn(self):
(scheme, ignore, ignore,
ignore, ignore, ignore) = virtinst.util.uri_split(self.get_uri())
ignore, ignore, ignore) = uriutil.uri_split(self.get_uri())
if scheme.startswith("test"):
return True
return False
def is_session_uri(self):
path = virtinst.util.uri_split(self.get_uri())[3]
path = uriutil.uri_split(self.get_uri())[3]
return path == "/session"
# Connection capabilities debug helpers
@ -441,7 +442,7 @@ class vmmConnection(vmmGObject):
return match_whole_string(orig, "[0-9.]+")
(scheme, username, hostname,
path, ignore, ignore) = virtinst.util.uri_split(self.get_uri())
path, ignore, ignore) = uriutil.uri_split(self.get_uri())
hv = ""
rest = ""

View File

@ -26,8 +26,9 @@ from gi.repository import Gdk
from gi.repository import GLib
from gi.repository import Gtk
import virtinst
import libvirt
import virtinst
from virtinst import uriutil
from virtManager import util
from virtManager.baseclass import vmmGObjectUI
@ -273,7 +274,7 @@ class vmmMigrateDialog(vmmGObjectUI):
return self.edit_uri(srcuri, desthost, None)
def edit_uri(self, uri, hostname, port):
split = list(virtinst.util.uri_split(uri))
split = list(uriutil.uri_split(uri))
hostname = hostname or split[2]
if port:

View File

@ -452,7 +452,7 @@ class Installer(XMLBuilderDomain.XMLBuilderDomain):
@type L{Guest}
"""
if util.is_uri_remote(guest.conn.getURI(), conn=guest.conn):
if guest.is_remote():
# XXX: Use block peek for this?
return True

View File

@ -17,9 +17,11 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA.
import platform
import os
import util
import platform
import uriutil
class User(object):
"""Defines a particular user account."""
@ -52,7 +54,7 @@ class User(object):
return self._euid == 0
if priv == self.PRIV_CREATE_NETWORK:
return (self._euid == 0) or util.is_qemu_system(conn)
return (self._euid == 0) or uriutil.is_qemu_system(conn)
if platform.system() == 'SunOS':
return self._sun_has_priv(priv)

View File

@ -31,6 +31,8 @@ import urlgrabber.progress as progress
import libvirt
import virtinst
from virtinst import uriutil
import util
import Storage
from VirtualDevice import VirtualDevice
@ -208,7 +210,7 @@ def _check_if_path_managed(conn, path):
pool = trypool
if not vol and not pool:
if not util.is_uri_remote(conn.getURI(), conn=conn):
if not uriutil.is_uri_remote(conn.getURI(), conn=conn):
# Building local disk
return None, None, False
@ -355,7 +357,7 @@ class VirtualDisk(VirtualDevice):
"""
Check if path exists. If we can't determine, return False
"""
is_remote = util.is_uri_remote(conn.getURI(), conn=conn)
is_remote = uriutil.is_uri_remote(conn.getURI(), conn=conn)
try:
vol = None
path_is_pool = False
@ -385,7 +387,7 @@ class VirtualDisk(VirtualDevice):
"""
if path is None:
return []
if util.is_uri_remote(conn.getURI(), conn=conn):
if uriutil.is_uri_remote(conn.getURI(), conn=conn):
return []
try:

View File

@ -26,11 +26,13 @@ import libvirt
import libxml2
import CapabilitiesParser
import uriutil
import util
_xml_refs_lock = threading.Lock()
_xml_refs = {}
def _unref_doc(doc):
if not doc:
return
@ -421,7 +423,7 @@ class XMLBuilderDomain(object):
raise ValueError(_("'conn' must be a virConnect instance."))
self._conn = val
self._conn_uri = self._conn.getURI()
self.__remote = util.is_uri_remote(self._conn_uri, conn=self._conn)
self.__remote = uriutil.is_uri_remote(self._conn_uri, conn=self._conn)
conn = property(get_conn, set_conn)
def get_uri(self):
@ -435,13 +437,13 @@ class XMLBuilderDomain(object):
def is_remote(self):
return bool(self.__remote)
def is_qemu(self):
return util.is_qemu(self.conn, self.get_uri())
return uriutil.is_qemu(self.conn, self.get_uri())
def is_qemu_system(self):
return util.is_qemu_system(self.conn, self.get_uri())
return uriutil.is_qemu_system(self.conn, self.get_uri())
def is_session_uri(self):
return util.is_session_uri(self.conn, self.get_uri())
return uriutil.is_session_uri(self.conn, self.get_uri())
def is_xen(self):
return util.is_xen(self.conn, self.get_uri())
return uriutil.is_xen(self.conn, self.get_uri())
def _check_bool(self, val, name):
if val not in [True, False]:

View File

@ -20,7 +20,7 @@
# MA 02110-1301 USA.
import libvirt
import util
import uriutil
# Flags for check_conn_support
@ -350,7 +350,7 @@ def _daemon_lib_ver(conn, uri, force_version, minimum_libvirt_version):
else:
default_ret = 100000000000
if not util.is_uri_remote(uri, conn=conn):
if not uriutil.is_uri_remote(uri, conn=conn):
return _local_lib_ver()
if not _has_command("getLibVersion", obj=conn):
@ -363,7 +363,7 @@ def _daemon_lib_ver(conn, uri, force_version, minimum_libvirt_version):
# Return the hypervisor version
def _hv_ver(conn, uri):
drv_type = util.get_uri_driver(uri)
drv_type = uriutil.get_uri_driver(uri)
args = ()
cmd = _get_command("getVersion", obj=conn)
@ -423,7 +423,7 @@ def _check_support(conn, feature, data=None):
return support_info.get(key)
uri = conn.getURI()
drv_type = util.get_uri_driver(uri)
drv_type = uriutil.get_uri_driver(uri)
is_rhel6 = _get_rhel6()
force_version = get_value("force_version") or False

165
virtinst/uriutil.py Normal file
View File

@ -0,0 +1,165 @@
#
# Copyright 2006-2013 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA.
#
import logging
def uri_split(uri):
"""
Parse a libvirt hypervisor uri into it's individual parts
@returns: tuple of the form (scheme (ex. 'qemu', 'xen+ssh'), username,
hostname, path (ex. '/system'), 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:]
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 is_uri_remote(uri, conn=None):
if conn and hasattr(conn, "_virtinst__fake_conn_remote"):
# Testing hack
return True
try:
split_uri = uri_split(uri)
netloc = split_uri[2]
if netloc == "":
return False
return True
except Exception, e:
logging.exception("Error parsing URI in is_remote: %s", e)
return True
def get_uri_hostname(uri):
try:
split_uri = uri_split(uri)
netloc = split_uri[2]
if netloc != "":
return netloc
except Exception, e:
logging.warning("Cannot parse URI %s: %s", uri, str(e))
return "localhost"
def get_uri_transport(uri):
try:
split_uri = uri_split(uri)
scheme = split_uri[0]
username = split_uri[1]
if scheme:
offset = scheme.index("+")
if offset > 0:
return [scheme[offset + 1:], username]
except:
pass
return [None, None]
def get_uri_driver(uri):
try:
split_uri = uri_split(uri)
scheme = split_uri[0]
if scheme:
offset = scheme.find("+")
if offset > 0:
return scheme[:offset]
return scheme
except Exception:
pass
return "xen"
def _get_uri_to_split(conn, uri):
if not conn and not uri:
return None
if type(conn) is str:
uri = conn
elif uri is None:
uri = conn.getURI()
return uri
def is_qemu_system(conn, uri=None):
uri = _get_uri_to_split(conn, uri)
if not uri:
return False
(scheme, ignore, ignore,
path, ignore, ignore) = uri_split(uri)
if path == "/system" and scheme.startswith("qemu"):
return True
return False
def is_session_uri(conn, uri=None):
uri = _get_uri_to_split(conn, uri)
if not uri:
return False
(ignore, ignore, ignore,
path, ignore, ignore) = uri_split(uri)
return bool(path and path == "/session")
def is_qemu(conn, uri=None):
uri = _get_uri_to_split(conn, uri)
if not uri:
return False
scheme = uri_split(uri)[0]
return scheme.startswith("qemu")
def is_xen(conn, uri=None):
uri = _get_uri_to_split(conn, uri)
if not uri:
return False
scheme = uri_split(uri)[0]
return scheme.startswith("xen")

View File

@ -35,6 +35,7 @@ import subprocess
import libvirt
import libxml2
import uriutil
def listify(l):
if l is None:
@ -312,7 +313,7 @@ def default_bridge(conn=None):
dev = default_route()
if (dev is not None and
(not conn or not is_uri_remote(conn.getURI(), conn=conn))):
(not conn or not uriutil.is_uri_remote(conn.getURI(), conn=conn))):
# New style peth0 == phys dev, eth0 == bridge, eth0 == default route
if os.path.exists("/sys/class/net/%s/bridge" % dev):
return ["bridge", dev]
@ -331,51 +332,6 @@ def default_bridge(conn=None):
return None
def _get_uri_to_split(conn, uri):
if not conn and not uri:
return None
if type(conn) is str:
uri = conn
elif uri is None:
uri = conn.getURI()
return uri
def is_qemu_system(conn, uri=None):
uri = _get_uri_to_split(conn, uri)
if not uri:
return False
(scheme, ignore, ignore,
path, ignore, ignore) = uri_split(uri)
if path == "/system" and scheme.startswith("qemu"):
return True
return False
def is_session_uri(conn, uri=None):
uri = _get_uri_to_split(conn, uri)
if not uri:
return False
(ignore, ignore, ignore,
path, ignore, ignore) = uri_split(uri)
return bool(path and path == "/session")
def is_qemu(conn, uri=None):
uri = _get_uri_to_split(conn, uri)
if not uri:
return False
scheme = uri_split(uri)[0]
return scheme.startswith("qemu")
def is_xen(conn, uri=None):
uri = _get_uri_to_split(conn, uri)
if not uri:
return False
scheme = uri_split(uri)[0]
return scheme.startswith("xen")
def parse_node_helper(xml, root_name, callback, exec_class=ValueError):
"""
@ -706,96 +662,6 @@ def default_keymap():
return keymap
def uri_split(uri):
"""
Parse a libvirt hypervisor uri into it's individual parts
@returns: tuple of the form (scheme (ex. 'qemu', 'xen+ssh'), username,
hostname, path (ex. '/system'), 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:]
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 is_uri_remote(uri, conn=None):
if conn and hasattr(conn, "_virtinst__fake_conn_remote"):
# Testing hack
return True
try:
split_uri = uri_split(uri)
netloc = split_uri[2]
if netloc == "":
return False
return True
except Exception, e:
logging.exception("Error parsing URI in is_remote: %s", e)
return True
def get_uri_hostname(uri):
try:
split_uri = uri_split(uri)
netloc = split_uri[2]
if netloc != "":
return netloc
except Exception, e:
logging.warning("Cannot parse URI %s: %s", uri, str(e))
return "localhost"
def get_uri_transport(uri):
try:
split_uri = uri_split(uri)
scheme = split_uri[0]
username = split_uri[1]
if scheme:
offset = scheme.index("+")
if offset > 0:
return [scheme[offset + 1:], username]
except:
pass
return [None, None]
def get_uri_driver(uri):
try:
split_uri = uri_split(uri)
scheme = split_uri[0]
if scheme:
offset = scheme.find("+")
if offset > 0:
return scheme[:offset]
return scheme
except Exception:
pass
return "xen"
def is_storage_capable(conn):
"""check if virConnectPtr passed has storage API support"""
import support