Completed
Push — master ( 291ee9...9d841e )
by Matěj
20s queued 12s
created

ssg._xccdf.get_profile_choices_for_input()   C

Complexity

Conditions 8

Size

Total Lines 52
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 31
nop 3
dl 0
loc 52
rs 5.5452
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
"""
2
A couple generic XCCDF utilities used by build-all-guides.py and
3
build-all-remediation-roles.py
4
5
Author: Martin Preisler <[email protected]>
6
"""
7
8
import re
9
10
from ssg._constants import XCCDF11_NS, XCCDF12_NS
11
12
# if a profile ID ends with a string listed here we skip it
13
PROFILE_ID_BLACKLIST = ["test", "index", "default"]
14
# filler XCCDF 1.2 prefix which we will strip to avoid very long filenames
15
PROFILE_ID_PREFIX = ("^xccdf_org.*content_profile_")
16
17
18
def get_benchmark_ids_titles_for_input(input_tree):
0 ignored issues
show
Coding Style Naming introduced by
The name get_benchmark_ids_titles_for_input does not conform to the function naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
19
    ret = {}
20
21
    def scrape_benchmarks(root_element, namespace, dest):
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
22
        candidates = \
23
            list(root_element.findall(".//{%s}Benchmark" % (namespace)))
24
        if root_element.tag == "{%s}Benchmark" % (namespace):
25
            candidates.append(root_element)
26
27
        for elem in candidates:
28
            id_ = elem.get("id")
29
            if id_ is None:
30
                continue
31
32
            title = "<unknown>"
33
            for element in elem.findall("{%s}title" % (namespace)):
34
                title = element.text
35
                break
36
37
            dest[id_] = title
38
39
    input_root = input_tree.getroot()
40
41
    scrape_benchmarks(
42
        input_root, XCCDF11_NS, ret
43
    )
44
    scrape_benchmarks(
45
        input_root, XCCDF12_NS, ret
46
    )
47
48
    return ret
49
50
51
def get_profile_choices_for_input(input_tree, benchmark_id, tailoring_tree):
52
    """Returns a dictionary that maps profile_ids to their respective titles.
53
    """
54
55
    # Ideally oscap would have a command line to do this, but as of now it
56
    # doesn't so we have to implement it ourselves. Importing openscap Python
57
    # bindings is nasty and overkill for this.
58
59
    ret = {}
60
61
    def scrape_profiles(root_element, namespace, dest):
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
62
        candidates = \
63
            list(root_element.findall(".//{%s}Benchmark" % (namespace)))
64
        if root_element.tag == "{%s}Benchmark" % (namespace):
65
            candidates.append(root_element)
66
67
        for benchmark in candidates:
68
            if benchmark.get("id") != benchmark_id:
69
                continue
70
71
            for elem in benchmark.findall(".//{%s}Profile" % (namespace)):
72
                id_ = elem.get("id")
73
                if id_ is None:
74
                    continue
75
76
                title = "<unknown>"
77
                for element in elem.findall("{%s}title" % (namespace)):
78
                    title = element.text
79
                    break
80
81
                dest[id_] = title
82
83
    input_root = input_tree.getroot()
84
85
    scrape_profiles(
86
        input_root, XCCDF11_NS, ret
87
    )
88
    scrape_profiles(
89
        input_root, XCCDF12_NS, ret
90
    )
91
92
    if tailoring_tree is not None:
93
        tailoring_root = tailoring_tree.getroot()
94
95
        scrape_profiles(
96
            tailoring_root, XCCDF11_NS, ret
97
        )
98
        scrape_profiles(
99
            tailoring_root, XCCDF12_NS, ret
100
        )
101
102
    return ret
103
104
105
def get_profile_short_id(long_id):
106
    """If given profile ID is the XCCDF 1.2 long ID this function shortens it
107
    """
108
109
    if re.search(PROFILE_ID_PREFIX, long_id):
110
        return long_id[re.search(PROFILE_ID_PREFIX, long_id).end():]
111
112
    return long_id
113