Passed
Push — master ( ea7c4a...d61422 )
by Matěj
01:18 queued 12s
created

xccdf2csv-stig._node_to_text()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
#!/usr/bin/env python2
2
3
"""
4
This script creates a CSV file from an XCCDF file formatted in the
5
structure of a STIG.  This should enable its ingestion into VMS,
6
as well as its comparison with VMS output.
7
"""
8
9
from __future__ import absolute_import
10
from __future__ import print_function
11
12
import sys
13
import csv
14
15
import ssg.xml
16
import ssg.constants
17
18
19
def _reflist(refs):
20
    refstring = ', '.join(refs)
21
    return refstring
22
23
24
def _node_to_text(node):
25
    textslist = node.xpath(".//text()")
26
    return ''.join(textslist)
27
28
29
def main():
30
    if len(sys.argv) < 2:
31
        print("Provide an XCCDF file to convert into a CSV file.")
32
        sys.exit(1)
33
34
    xccdffile = sys.argv[1]
35
    xccdftree = ssg.xml.parse_file(xccdffile)
36
    rules = xccdftree.findall(".//{%s}Rule" % ssg.constants.XCCDF11_NS)
37
    rulewriter = csv.writer(sys.stdout, quoting=csv.QUOTE_ALL)
38
39
    for rule in rules:
40
        args = (ssg.constants.XCCDF11_NS, ssg.constants.disa_cciuri)
41
        cci_refs = [ref.text for ref in rule.findall("{%s}ident[@system='%s']"
42
                                                     % args)]
43
        srg_refs = [ref.text for ref in rule.findall("{%s}ident[@system='%s']"
44
                                                     % args)]
45
        title = rule.find("{%s}title" % ssg.constants.XCCDF11_NS).text
46
        description = _node_to_text(rule.find("{%s}description" % ssg.constants.XCCDF11_NS))
47
        fixtext = _node_to_text(rule.find("{%s}fixtext" % ssg.constants.XCCDF11_NS))
48
        checktext = _node_to_text(rule.find(".//{%s}check-content" % ssg.constants.XCCDF11_NS))
49
        row = [_reflist(cci_refs), _reflist(srg_refs), title,
50
               description, fixtext, checktext]
51
        rulewriter.writerow(row)
52
53
    sys.exit(0)
54
55
56
if __name__ == "__main__":
57
    main()
58