Test Failed
Push — master ( 738e3d...d883b3 )
by Matěj
01:17 queued 13s
created

ssg.rules.find_rule_dirs()   A

Complexity

Conditions 4

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 6
nop 1
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 4
rs 10
c 0
b 0
f 0
1 2
from __future__ import absolute_import
2 2
from __future__ import print_function
3
4 2
import os
5
6
7 2
def get_rule_dir_yaml(dir_path):
8
    """
9
    Returns the path to the yaml metadata for a rule directory,
10
    regardless of if it exists.
11
    """
12 2
    return os.path.join(dir_path, "rule.yml")
13
14
15 2
def get_rule_dir_id(path):
16
    """
17
    Returns the ID of a rule directory; correctly handles being passed
18
    either the directory path or the yaml metadata path.
19
    """
20 2
    dir_path = path
21
22 2
    if path.endswith("rule.yml"):
23 2
        dir_path = os.path.dirname(path)
24
25 2
    return os.path.basename(dir_path)
26
27
28 2
def is_rule_dir(dir_path):
29
    """
30
    Returns True iff dir_path is a valid rule directory which exists
31
32
    To be valid, dir_path must exist and be a directory and the file
33
    returned by get_rule_dir_yaml(dir_path) must exist.
34
    """
35 2
    rule_yaml = get_rule_dir_yaml(dir_path)
36
37 2
    is_dir = os.path.isdir(dir_path)
38 2
    has_rule_yaml = os.path.exists(rule_yaml)
39
40 2
    return is_dir and has_rule_yaml
41
42
43 2
def applies_to_product(file_name, product):
44
    """
45
    A OVAL or fix is filtered by product iff product is Falsy, file_name is
46
    "shared", or file_name is product. Note that this does not filter by
47
    contents of the fix or check, only by the name of the file.
48
    """
49
50 2
    return not product or (file_name == "shared" or file_name == product)
51
52
53 2
def get_rule_dir_ovals(dir_path, product=None):
54
    """
55
    Gets a list of OVALs contained in a rule directory. If product is
56
    None, returns all OVALs. If product is not None, returns applicable
57
    OVALs in order of priority:
58
59
        {{{ product }}}.xml -> shared.xml
60
61
    Only returns OVALs which exist.
62
    """
63
64 2
    if not is_rule_dir(dir_path):
65
        return []
66
67 2
    oval_dir = os.path.join(dir_path, "oval")
68 2
    has_oval_dir = os.path.isdir(oval_dir)
69 2
    if not has_oval_dir:
70
        return []
71
72 2
    results = []
73 2
    for oval_file in os.listdir(oval_dir):
74 2
        file_name, ext = os.path.splitext(oval_file)
75 2
        oval_path = os.path.join(oval_dir, oval_file)
76
77 2
        if ext == ".xml" and applies_to_product(file_name, product):
78 2
            if file_name == 'shared':
79 2
                results.append(oval_path)
80
            else:
81 2
                results.insert(0, oval_path)
82
83 2
    return results
84
85
86 2
def find_rule_dirs(base_dir):
87
    """
88
    Generator which yields all rule directories within a given base_dir, recursively
89
    """
90 2
    for root, dirs, _ in os.walk(base_dir):
91 2
        for dir_name in dirs:
92 2
            dir_path = os.path.join(root, dir_name)
93 2
            if is_rule_dir(dir_path):
94 2
                yield dir_path
95
96
97 2
def find_rule_dirs_in_paths(base_dirs):
98
    """
99
    Generator which yields all rule directories within a given directories list, recursively
100
    """
101
    if base_dirs:
102
        for cur_dir in base_dirs:
103
            for d in find_rule_dirs(cur_dir):
104
                yield d
105