-
Notifications
You must be signed in to change notification settings - Fork 0
/
WBEMServerMockLoad.py
198 lines (172 loc) · 7.36 KB
/
WBEMServerMockLoad.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# WBEMServerMockLoad.py
#
# Copyright 2020 FarmerMike <[email protected]>
#
# 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.
#
#
"""
Sample python script to create a repository. This installs all of the
qualifier declarations and the classes and their dependencies defined by
the variable leaf_classes
NOTE: This script only works with python version >=3.5 and with pywbemtools
0.8.0.
To use this script with pywbemtools mock scripting. load the script and
save it into connections file
pywbemcli -m WBEMServrMockLoad.py
pywbemcli > connection save WBEMServerMock
From then on the cached WBEMServerMock will be used unless the file or any
of its dependent files is changed and then it will be recompiled.
This should speed up loading from many seconds to less than a second.
"""
""" First we import modules we will need """
import os
import xml.etree.ElementTree as ET
import pywbem
import pywbem_mock
# Get the directory containing this file. This is where the dependent
# files and directories should also exist.
SCRIPT_DIR = os.path.dirname(__file__)
def setup(conn, server, verbose):
# version definition of the DMTF schema. This is the version currently
# installed by default in pywbem github. Cloning pywbem to get the
# development components means this schema is already installed in the
# directory
# tests/schema
schema_version = (2, 51, 0)
# Schema directory must be contained in the same directory as this file
# This is the directory in which the schema will be installed.
testsuite_schema_dir = os.path.join(SCRIPT_DIR, 'schema')
if verbose is True:
conn.display_repository()
profile_fullname = 'DMTF_WBEM Server'
# Create the list of leaf classes from the leaflist.xml file which must
# be contained in the current working directory
profile_leaflist = "{}_leaflist.xml".format(profile_fullname)
# REMOVE profile_leaflist = profile_fullname + "_leaflist.xml"
profile_leaflist = os.path.join(SCRIPT_DIR, profile_leaflist)
# Read the list of leaf classes into a list.
leafentries = ET.parse(profile_leaflist)
llroot = leafentries.getroot()
unique_classes = [leaf.attrib['NAME'] for leaf in llroot.findall('ENTRY')]
# Replaced with list comprehenshion.
#for leaf in llroot.findall('ENTRY'):
# unique_class_list.append(leaf.attrib['NAME'])
"""
print('Finding CIM_Container in unique_leaf_classes')
print(unique_leaf_classes)
concount = 0
for container in unique_leaf_classes:
if container == 'CIM_Container':
concount = concount + 1
print('CIM_Container found in all_leaf_classes')
if concount == 0:
print('CIM_Container NOT FOUND in all_leaf_classes')
"""
# Compile dmtf schema version 2.51.0, the qualifier declarations and
# the classes in 'classes' and all dependent classes and keep the
# schema in directory my_schema_dir. This installs the schema into the
# default class which is root/cimv2.
print('Loading classes into the Mock Repository')
schema = pywbem_mock.DMTFCIMSchema(schema_version,
testsuite_schema_dir,
use_experimental=True,
verbose=False)
conn.compile_schema_classes(
unique_classes,
schema.schema_pragma_file,
namespace=conn.default_namespace,
verbose=False)
"""
print('Finding CIM_Container class definition in the Repository:')
try:
clssobj = conn.GetClass('CIM_Container', namespace='root/cimv2',
LocalOnly=False, verbose=False)
print(clssobj.tomof())
except pywbem.Error as exc:
print('CIM_Container class NOT FOUND in the Repository')
"""
if verbose:
conn.display_repository()
"""
Next we compile the instance mofs into our mock repository
"""
print('Loading instances into the Mock Repository')
mock_mof = "Mock{}Instances.mof".format(profile_fullname)
# REMOVED mock_mof = 'Mock' + fullname + 'Instances.mof'
mock_mof = os.path.join(SCRIPT_DIR, mock_mof)
conn.compile_mof_file(mock_mof, verbose=False)
print('DONE Loading instances into the Mock Repository')
# print('Listing Class definitions and their instances')
if verbose is True:
# conn.display_repository()
for clss in unique_classes:
if clss == 'CIM_Container':
try:
clssobj = conn.GetClass(clss, namespace='root/cimv2',
LocalOnly=False)
except pywbem.Error as exc:
print('Operation failed: %s' % exc)
print(clssobj.tomof())
print('Trying to collect on CIM_Container instances')
try:
insts = conn.EnumerateInstances(clss, namespace='root/cimv2',
LocalOnly=False)
except pywbem.Error as exc:
# continue
print('Operation failed: %s' % exc)
instcount = 0
for inst in insts:
instcount = instcount + 1
if instcount == 1:
if clss == 'CIM_Container':
print('Trying to GetClass on CIM_Container')
try:
clssobj = conn.GetClass(clss, namespace='root/cimv2',
LocalOnly=False)
except pywbem.Error as exc:
print('Operation failed: %s' % exc)
print(clssobj.tomof())
print(inst.tomof())
"""
# Next we will look for a registered profile in the mock repository
# that matches what was requested
print('Listing instances of CIM_ComputerSystem')
try:
compsyss = conn.EnumerateInstances('CIM_ComputerSystem',
namespace='root/cimv2',
LocalOnly=False)
except pywbem.Error as exc:
print('Operation failed: %s' % exc)
print('Retrieved %s instances' % (len(compsyss)))
for inst in compsyss:
print('path=%s' % inst.path)
print(inst.tomof())
print('Listing instances of CIM_Namespace')
try:
namesps = conn.EnumerateInstances('CIM_Namespace',
namespace='root/cimv2',
LocalOnly=False)
except pywbem.Error as exc:
print('Operation failed: %s' % exc)
print('Retrieved %s instances' % (len(namesps)))
for inst in namesps:
print('path=%s' % inst.path)
print(inst.tomof())
"""