From 2d96df978f68119a0ad8889b1bbcd751e5882a55 Mon Sep 17 00:00:00 2001 From: Jan Rodak Date: Thu, 18 Jan 2024 11:47:15 +0100 Subject: [PATCH] Fix python2 support --- .../utils/test_generate_most_used_rules.py | 3 +++ utils/profile_tool/most_used_rules.py | 23 +++++++++++++------ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/tests/unit/utils/test_generate_most_used_rules.py b/tests/unit/utils/test_generate_most_used_rules.py index 7900093bebde..d213fd6c0ea8 100644 --- a/tests/unit/utils/test_generate_most_used_rules.py +++ b/tests/unit/utils/test_generate_most_used_rules.py @@ -1,4 +1,6 @@ import os +import sys +import pytest from argparse import Namespace from utils.profile_tool import command_most_used_rules @@ -14,6 +16,7 @@ def get_fake_args(): ) +@pytest.mark.skipif(sys.version_info[0] < 3, reason="requires python3") def test_command(capsys): command_most_used_rules(get_fake_args()) captured = capsys.readouterr() diff --git a/utils/profile_tool/most_used_rules.py b/utils/profile_tool/most_used_rules.py index bccde04d1f65..3400d6c9f575 100644 --- a/utils/profile_tool/most_used_rules.py +++ b/utils/profile_tool/most_used_rules.py @@ -1,12 +1,18 @@ +import sys import json from ssg.build_profile import XCCDFBenchmark -from .profile import get_profile -from ..controleval import ( - load_controls_manager, - get_available_products, - get_product_profiles_files, -) + + +PYTHON_2 = sys.version_info[0] < 3 + +if not PYTHON_2: + from .profile import get_profile + from ..controleval import ( + load_controls_manager, + get_available_products, + get_product_profiles_files, + ) def _count_rules_per_rules_list(rules_list, rules): @@ -33,6 +39,9 @@ def _get_profiles_for_product(ctrls_mgr, product): def _process_all_products_from_controls(rules): + if PYTHON_2: + raise Exception("This feature is not supported for python2.") + for product in get_available_products(): controls_manager = load_controls_manager("./controls/", product) for profile in _get_profiles_for_product(controls_manager, product): @@ -50,7 +59,7 @@ def _sorted_rules(rules): def command_most_used_rules(args): rules = {} - if len(args.BENCHMARKS) == 0: + if args.BENCHMARKS: _process_all_products_from_controls(rules) else: for benchmark in args.BENCHMARKS: