~ubuntu-branches/ubuntu/quantal/virtinst/quantal-proposed

« back to all changes in this revision

Viewing changes to virtinst/NodeDeviceParser.py

  • Committer: Bazaar Package Importer
  • Author(s): Guido Günther
  • Date: 2009-12-07 10:04:22 UTC
  • mto: (1.6.3 sid)
  • mto: This revision was merged to the branch mainline in revision 20.
  • Revision ID: james.westby@ubuntu.com-20091207100422-2izjffd5ljvqun47
Tags: upstream-0.500.1
Import upstream version 0.500.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18
18
# MA 02110-1301 USA.
19
19
 
20
 
import libxml2
21
 
import libvirt
22
20
from virtinst import _virtinst as _
 
21
import support
 
22
import _util
23
23
 
24
24
# class USBDevice
25
25
 
51
51
        @returns Device description string
52
52
        @rtype C{str}
53
53
        """
54
 
        raise NotImplementedError
 
54
        ignore = child_dev
 
55
        return self.name
55
56
 
56
57
    def _parseNodeXML(self, node):
57
58
        child = node.children
253
254
        self.removable = False
254
255
        self.media_available = False
255
256
        self.media_size = 0
 
257
        self.media_label = None
256
258
 
257
259
        self.hotpluggable = False
258
260
 
281
283
                            self.media_available = bool(int(rmchild.content))
282
284
                        elif rmchild.name == "media_size":
283
285
                            self.media_size = int(rmchild.content)
 
286
                        elif rmchild.name == "media_label":
 
287
                            self.media_label = rmchild.content
284
288
                        rmchild = rmchild.next
285
289
            else:
286
290
                self._parseValueHelper(child, val_map)
319
323
                    "protocol" : "protocol" }
320
324
        self._parseHelper(node, val_map)
321
325
 
322
 
    def pretty_name(self, child_dev=None):
323
 
        ignore = child_dev
324
 
        return self.name
325
326
 
326
327
class SCSIDevice(NodeDevice):
327
328
    def __init__(self, node):
343
344
                    "type" : "type"}
344
345
        self._parseHelper(node, val_map)
345
346
 
346
 
    def pretty_name(self, child_dev=None):
347
 
        ignore = child_dev
348
 
        return self.name
349
 
 
350
347
class SCSIBus(NodeDevice):
351
348
    def __init__(self, node):
352
349
        NodeDevice.__init__(self, node)
353
350
 
354
351
        self.host = None
355
352
 
 
353
        self.vport_ops = False
 
354
 
 
355
        self.fc_host = False
 
356
        self.wwnn = None
 
357
        self.wwpn = None
 
358
 
356
359
        self.parseXML(self._getCapabilityNode(node))
357
360
 
358
361
    def parseXML(self, node):
359
362
        val_map = { "host" : "host" }
360
 
        self._parseHelper(node, val_map)
361
 
 
362
 
    def pretty_name(self, child_dev=None):
363
 
        ignore = child_dev
364
 
        return self.name
 
363
 
 
364
        child = node.children
 
365
        while child:
 
366
            if child.name == "capability":
 
367
                captype = child.prop("type")
 
368
 
 
369
                if captype == "vport_ops":
 
370
                    self.vport_ops = True
 
371
                elif captype == "fc_host":
 
372
                    self.fc_host = True
 
373
                    fcchild = child.children
 
374
                    while fcchild:
 
375
                        if fcchild.name == "wwnn":
 
376
                            self.wwnn = fcchild.content
 
377
                        elif fcchild.name == "wwpn":
 
378
                            self.wwpn = fcchild.content
 
379
                        fcchild = fcchild.next
 
380
            else:
 
381
                self._parseValueHelper(child, val_map)
 
382
 
 
383
            child = child.next
365
384
 
366
385
def is_nodedev_capable(conn):
367
386
    """
372
391
 
373
392
    @rtype: C{bool}
374
393
    """
375
 
    if not conn:
376
 
        return False
377
 
    if not isinstance(conn, libvirt.virConnect):
378
 
        raise ValueError(_("'conn' must be a virConnect instance."))
379
 
 
380
 
    if dir(libvirt).count("virNodeDevice") == 0:
381
 
        # Local libvirt doesn't support it
382
 
        return False
383
 
 
384
 
    try:
385
 
        conn.listDevices(None, 0)
386
 
        return True
387
 
    except Exception, e:
388
 
        if (e.get_error_code() == libvirt.VIR_ERR_RPC or
389
 
            e.get_error_code() == libvirt.VIR_ERR_NO_SUPPORT):
390
 
            return False
391
 
 
392
 
    return True
 
394
    return support.check_conn_support(conn, support.SUPPORT_CONN_NODEDEV)
393
395
 
394
396
def is_pci_detach_capable(conn):
395
397
    """
400
402
 
401
403
    @rtype: C{bool}
402
404
    """
403
 
    if not conn:
404
 
        return False
405
 
    if not isinstance(conn, libvirt.virConnect):
406
 
        raise ValueError(_("'conn' must be a virConnect instance."))
407
 
 
408
 
    if (hasattr(libvirt, "virNodeDevice") and
409
 
        hasattr(libvirt.virNodeDevice, "dettach")):
410
 
        return True
411
 
 
412
 
    return False
 
405
    return support.check_conn_support(conn,
 
406
                                      support.SUPPORT_NODEDEV_PCI_DETACH)
413
407
 
414
408
def lookupNodeName(conn, name):
415
409
    """
440
434
 
441
435
    @returns: L{NodeDevice} instance
442
436
    """
443
 
 
444
 
    class ErrorHandler:
445
 
        def __init__(self):
446
 
            self.msg = ""
447
 
        def handler(self, ignore, s):
448
 
            self.msg += s
449
 
    error = ErrorHandler()
450
 
    libxml2.registerErrorHandler(error.handler, None)
451
 
 
452
 
    try:
453
 
        # try/except/finally is only available in python-2.5
454
 
        try:
455
 
            doc = libxml2.readMemory(xml, len(xml),
456
 
                                     None, None,
457
 
                                     libxml2.XML_PARSE_NOBLANKS)
458
 
        except (libxml2.parserError, libxml2.treeError), e:
459
 
            raise ValueError("%s\n%s" % (e, error.msg))
460
 
    finally:
461
 
        libxml2.registerErrorHandler(None, None)
462
 
 
463
 
    try:
464
 
        root = doc.getRootElement()
465
 
        if root.name != "device":
466
 
            raise ValueError("Root element is not 'device'")
467
 
 
 
437
    def _parse_func(root):
468
438
        t = _findNodeType(root)
469
439
        devclass = _typeToDeviceClass(t)
470
440
        device = devclass(root)
471
 
    finally:
472
 
        doc.freeDoc()
 
441
        return device
473
442
 
474
 
    return device
 
443
    return _util.parse_node_helper(xml, "device", _parse_func)
475
444
 
476
445
def _findNodeType(node):
477
446
    child = node.children
498
467
        return SCSIBus
499
468
    elif t == CAPABILITY_TYPE_SCSIDEV:
500
469
        return SCSIDevice
501
 
 
502
 
    raise ValueError(_("Unknown host device capability '%s'.") % t)
 
470
    else:
 
471
        return NodeDevice