#!/usr/bin/python

# Copyright 2009 Hewlett-Packard Development Company, L.P.
# HP Confidential.  No photocopying.

# Author: Tim Potter <tpot@hp.com>

#
# Dump a list of CIM instances in CIM-XML declaration format.
#

import os
from xml.dom.minidom import Element
import pywbem

def makeConnection(namespace):
    """Return a PyWBEM connection to run tests on."""

    if os.access('/tmp/sfcbHttpSocket', os.F_OK):
        return pywbem.SFCBUDSConnection(default_namespace = namespace)
    elif os.access('/var/run/tog-pegasus/cimxml.socket', os.F_OK):
        return pywbem.PegasusUDSConnection(default_namespace = namespace)

    raise RuntimeError('Unable to detect CIMOM type for local connection')

NAMESPACE = 'root/hpq'
CLASSLIST = ['CIM_Processor']

if __name__ == '__main__':

    # Set up outer elements

    doc = Element('CIM')
    doc.setAttribute('CIMVERSION', '2.9')
    doc.setAttribute('DTDVERSION', '2.2')

    decl = Element('DECLARATION')
    doc.appendChild(decl)

    declgroup = Element('DECLGROUP')
    decl.appendChild(declgroup)

    # Get instances

    cli = makeConnection(NAMESPACE)

    for cl in CLASSLIST:

        # Get instances for one class

        try:

            insts = cli.EnumerateInstances(cl)

        except pywbem.CIMError, arg:

            # Swallow CIM errors

            continue

        # Append instances to declgroup

        for inst in insts:
            inst.path = None
            elt = Element('VALUE.OBJECT')
            elt.appendChild(inst.tocimxml())
            declgroup.appendChild(elt)

    # Print entire document to stdout

    print doc.toprettyxml(indent = '  '),
