1
|
1 |
|
from __future__ import absolute_import |
|
|
|
|
2
|
1 |
|
from __future__ import print_function |
3
|
|
|
|
4
|
1 |
|
import platform |
5
|
|
|
|
6
|
1 |
|
from .constants import xml_version, oval_header, timestamp |
7
|
|
|
|
8
|
|
|
|
9
|
1 |
|
try: |
10
|
1 |
|
from xml.etree import cElementTree as ElementTree |
11
|
|
|
except ImportError: |
12
|
|
|
import cElementTree as ElementTree |
13
|
|
|
|
14
|
|
|
|
15
|
1 |
|
def oval_generated_header(product_name, schema_version, ssg_version): |
|
|
|
|
16
|
1 |
|
return xml_version + oval_header + \ |
17
|
|
|
""" |
18
|
|
|
<generator> |
19
|
|
|
<oval:product_name>%s from SCAP Security Guide</oval:product_name> |
20
|
|
|
<oval:product_version>ssg: %s, python: %s</oval:product_version> |
21
|
|
|
<oval:schema_version>%s</oval:schema_version> |
22
|
|
|
<oval:timestamp>%s</oval:timestamp> |
23
|
|
|
</generator>""" % (product_name, ssg_version, platform.python_version(), |
24
|
|
|
schema_version, timestamp) |
25
|
|
|
|
26
|
|
|
|
27
|
1 |
|
def parse_file(filename): |
28
|
|
|
""" |
29
|
|
|
Given a filename, return the root of the ElementTree |
30
|
|
|
""" |
31
|
|
|
return ElementTree.parse(filename).getroot() |
32
|
|
|
|
33
|
|
|
|
34
|
1 |
|
def map_elements_to_their_ids(tree, xpath_expr): |
35
|
|
|
""" |
36
|
|
|
Given an ElementTree and an XPath expression, |
37
|
|
|
iterate through matching elements and create 1:1 id->element mapping. |
38
|
|
|
|
39
|
|
|
Raises AssertionError if a matching element doesn't have the ``id`` |
40
|
|
|
attribute. |
41
|
|
|
|
42
|
|
|
Returns mapping as a dictionary |
43
|
|
|
""" |
44
|
|
|
aggregated = {} |
45
|
|
|
for element in tree.findall(xpath_expr): |
46
|
|
|
element_id = element.get("id") |
47
|
|
|
assert element_id is not None |
48
|
|
|
aggregated[element_id] = element |
49
|
|
|
return aggregated |
50
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.