File: handle_detected_updates.py

package info (click to toggle)
scap-security-guide 0.1.78-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 114,600 kB
  • sloc: xml: 245,305; sh: 84,381; python: 33,093; makefile: 27
file content (81 lines) | stat: -rw-r--r-- 2,640 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python3
import logging
import sys
from typing import List, Dict, Tuple

logging.basicConfig(level=logging.INFO, format="%(message)s")


def handle_detected_updates(file_path: str) -> Tuple[
    List[str],
    List[Dict[str, str]],
    List[str]
]:
    """
    Handle the detected updated files in the file which contains the PR's updated
    files.

    This function processes a file that includes paths of updated files.
    It extracts and categorizes the updated controls, profiles, rules, and variables
    based on the file paths.

    Args:
    file_path (str): The path to the file that contains the list of updated file paths.

    Returns:
        Tuple containing:
        - List of control names
        - List of profile dictionaries (with profile_name and product)
        - List of rule and variable names
    """

    controls: List[str] = []
    profile: Dict[str, str] = {}
    profiles: List[Dict[str, str]] = []
    rules: List[str] = []
    # Open the file and process it line by line
    with open(file_path, 'r') as file:
        for line in file:
            line = line.strip()
            if 'controls/' in line:
                controlname = line.split('/')[-1].split('.')[0]
                controls.append(controlname)
            elif '.profile' in line:
                profile["profile_name"] = line.split('/')[-1].split('.')[0]
                profile["product"] = line.split('/')[1]
                profiles.append(f'{profile}')
            elif 'rule.yml' in line:
                rulename = line.split('/')[-2]
                rules.append(rulename)
            elif '.var' in line and line.endswith('.var'):
                rulename = line.split('/')[-1].split('.')[0]
                rules.append(rulename)
    controls = handle_controls(controls)
    return controls, profiles, rules


def handle_controls(controls: str) -> str:
    # Handle some special cases
    for i, item in enumerate(controls):
        if "section-" in item:
            controls[i] = "cis_ocp_1_4_0"
        if "SRG-" in item:
            controls.remove(item)
    return list(dict.fromkeys(controls))


def main(file_path):
    controls, profiles, rules = handle_detected_updates(file_path)
    for i in [controls, profiles, rules]:
        logging.info(" ".join(i))


if __name__ == "__main__":
    # Ensure that the script is run with the correct number of arguments
    if len(sys.argv) != 2:
        logging.warning("Usage: \
            python handle_detected_updates.py 'file_path'")
        sys.exit(1)
    # Extract arguments
    file_path = sys.argv[1]  # The argument is file_path
    main(file_path)