Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 43 additions & 17 deletions utils/count_oval_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,31 @@ def load_xml(file_name):
try:
it = ET.iterparse(file_name)
for _, el in it:
el.tag = el.tag.split('}', 1)[1] # strip all namespaces
if '}'in el.tag:
el.tag = el.tag.split('}', 1)[1] # strip all namespaces
root = it.root
return root
except:
sys.stderr.write("Error while loading file " + file_name + ".\n")
exit(-1)


def get_ext_def_tests(oval_root, def_refs):
t = []
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please name the variable clearly, for example extended_tests.

for d in oval_root.findall(".//definition"):
if d.attrib.get('id') == def_refs:
definition = d
break
if definition is not None:
for criterion in definition.findall(".//criterion"):
test_ref = criterion.attrib["test_ref"]
t.append(test_ref)
for extend_def in definition.findall(".//extend_definition"):
extend_ref = extend_def.attrib["definition_ref"]
t = t + (get_ext_def_tests(oval_root, extend_ref))
return t


def find_oval_objects(oval_refs):
''' Finds OVAL objects according to definitions ID '''
tests = []
Expand All @@ -54,29 +71,38 @@ def find_oval_objects(oval_refs):
definition = d
break
if definition is not None:
extend_refs = []
for criterion in definition.findall(".//criterion"):
test_ref = criterion.attrib["test_ref"]
tests.append(test_ref)
for extend_def in definition.findall(".//extend_definition"):
extend_ref = extend_def.attrib["definition_ref"]
t = get_ext_def_tests(oval_root, extend_ref)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here as well, name the variable more significantly.

tests += t
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I know, there shouldn't be difference under the hood, but lets be consistent and use tests.extend()


# find references to objects in tests
for test in tests:
test_element = None
for t in oval_root.findall("tests/*"):
if t.attrib.get('id') == test:
test_element = t
break
if test_element is not None:
for object_element in test_element.findall(".//*"):
if 'object_ref' in object_element.attrib:
object_ref = object_element.attrib['object_ref']
object_refs.append(object_ref)
for key in oval_files:
oval_root = oval_files[key]
for test in tests:
test_element = None
for t in oval_root.findall("tests/*"):
if t.attrib.get('id') == test:
test_element = t
break
if test_element is not None:
for object_element in test_element.findall(".//*"):
if 'object_ref' in object_element.attrib:
object_ref = object_element.attrib['object_ref']
object_refs.append(object_ref)

# find objects
for r in object_refs:
for obj in oval_root.findall("objects/*"):
if obj.attrib.get('id') == r:
objects.append(obj.tag)
break
for key in oval_files:
oval_root = oval_files[key]
for r in object_refs:
for obj in oval_root.findall("objects/*"):
if obj.attrib.get('id') == r:
objects.append(obj.tag)
break

return set(objects)

Expand Down