Test Failed
Pull Request — master (#4702)
by Matěj
02:46 queued 26s
created

preprocess_source()   A

Complexity

Conditions 5

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 13
nop 2
dl 0
loc 16
ccs 0
cts 11
cp 0
crap 30
rs 9.2833
c 0
b 0
f 0
1
#!/usr/bin/env python2
2
3
import sys
4
import os
5
import os.path
6
import argparse
7
import codecs
8
9
import ssg.xml
10
import ssg.jinja
11
12
13
def preprocess_source(context, filepath):
14
    raw = ssg.jinja.process_file(filepath, context)
15
16
    source = ""
17
    for line in raw.splitlines(True):
18
        if line.startswith("source ") and line.endswith(".sh\n"):
19
            included_file = line.strip()[7:]  # skip "source "
20
            with codecs.open(os.path.join(
21
                os.path.dirname(filepath), included_file), "r", encoding="utf-8"
22
            ) as f:
23
                source += f.read()
24
                source += "\n"
25
        else:
26
            source += line
27
28
    return source
29
30
31
def main():
32
    p = argparse.ArgumentParser()
33
    p.add_argument("--input_dir", required=True)
34
    p.add_argument("--output", type=argparse.FileType("wb"), required=True)
35
36
    args, unknown = p.parse_known_args()
37
    if unknown:
38
        sys.stderr.write(
39
            "Unknown positional arguments " + ",".join(unknown) + ".\n"
40
        )
41
        sys.exit(1)
42
43
    root = ssg.xml.ElementTree.Element("Group")
44
    root.set("id", "remediation_functions")
45
    title = ssg.xml.ElementTree.SubElement(root, "title")
46
    title.text = "Remediation functions used by the SCAP Security Guide Project"
47
    description = ssg.xml.ElementTree.SubElement(root, "description")
48
    description.text = "XCCDF form of the various remediation functions as " \
49
        "used by remediation scripts from the SCAP Security Guide Project."
50
51
    context = ssg.jinja.load_macros()
52
    for file_ in os.listdir(args.input_dir):
53
        if not file_.endswith(".sh"):
54
            sys.stderr.write(
55
                "File '%s' does not appear to be a bash script. Skipping!"
56
                % (file_)
57
            )
58
            continue
59
60
        filename, ext = os.path.splitext(file_)
61
62
        source = preprocess_source(context, os.path.join(args.input_dir, file_))
63
64
        value_element = ssg.xml.ElementTree.SubElement(root, "Value")
65
        value_element.set("id", "function_%s" % (filename))
66
        value_element.set("type", "string")
67
        value_element.set("operator", "equals")
68
        value_element.set("interactive", "0")
69
        value_element.set("hidden", "true")
70
        value_element.set("prohibitChanges", "true")
71
72
        title = ssg.xml.ElementTree.SubElement(value_element, "title")
73
        title.text = "Remediation function %s" % (filename)
74
75
        desc = ssg.xml.ElementTree.SubElement(value_element, "description")
76
        desc.text = "Shared bash remediation function. Not intended to be " \
77
            "changed by tailoring."
78
79
        inner_value = ssg.xml.ElementTree.SubElement(value_element, "value")
80
        inner_value.set("selector", "")
81
        inner_value.text = source
82
83
    tree = ssg.xml.ElementTree.ElementTree(root)
84
    tree.write(args.output)
85
86
87
if __name__ == "__main__":
88
    main()
89