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

generate_bash_remediation_functions.main()   B

Complexity

Conditions 4

Size

Total Lines 54
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 40
nop 0
dl 0
loc 54
ccs 0
cts 37
cp 0
crap 20
rs 8.92
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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