Skip to content

Commit

Permalink
Merge pull request ComplianceAsCode#12023 from Honny1/add-not-used-pl…
Browse files Browse the repository at this point in the history
…atforms

Add not used platforms and rules to  `most-used-components` command
  • Loading branch information
jan-cerny authored May 31, 2024
2 parents d6c8d5e + c9729a6 commit 56d38b9
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
11 changes: 10 additions & 1 deletion build-scripts/profile_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,14 +315,23 @@ def parse_most_used_components(subparsers):
default=get_available_products_with_components_root(),
)
parser_most_used_components.add_argument(
"--used-rules",
"--rules",
default=False,
action="store_true",
help=(
"For every component, show the usage of each component's rule "
"in the profiles in given product."
),
)
parser_most_used_components.add_argument(
"--all",
default=False,
action="store_true",
help=(
"List all components and rules in the output, including unused "
"components and unused rules."
),
)


def get_available_products_with_components_root():
Expand Down
11 changes: 9 additions & 2 deletions docs/manual/developer/05_tools_and_utilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,18 @@ Optionally, you can use this command to limit the statistics for a specific prod
$ ./build-scripts/profile_tool.py most-used-components --products rhel9
```

You can also get a list of the most used components with used rules for the RHEL9 product, you can use the `--used-rules` flag.
You can also get a list of the most used components with used rules for the RHEL9 product, you can use the `--rules` flag.
As shown in this command:

```bash
$ ./build-scripts/profile_tool.py most-used-components --products rhel9 --used-rules
$ ./build-scripts/profile_tool.py most-used-components --products rhel9 --rules
```

You can also use the `--all` flag to get a list of all components and rules in the output, including unused components and unused rules.
As shown in this command:

```bash
$ ./build-scripts/profile_tool.py most-used-components --products rhel9 --all
```

The result will be a list of rules with the number of uses in the profiles.
Expand Down
4 changes: 4 additions & 0 deletions utils/profile_tool/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ def merge_dicts(dict_a, dict_b, delim):
value_b = dict_b.get(key, {})
out[key] = str(value) + _format_value_b(value_b, delim)
return out


def remove_zero_counts(dict_):
return {key: value for key, value in dict_.items() if value != 0}
26 changes: 20 additions & 6 deletions utils/profile_tool/most_used_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import ssg.components

from .most_used_rules import _sorted_dict_by_num_value
from .common import generate_output, merge_dicts
from .common import generate_output, merge_dicts, remove_zero_counts

PYTHON_2 = sys.version_info[0] < 3

Expand All @@ -18,19 +18,22 @@


def _count_rules_components(component_name, rules, used_rules_of_components_out):
used_rules = defaultdict(int)
if component_name in used_rules_of_components_out:
used_rules = used_rules_of_components_out[component_name]
used_rules = used_rules_of_components_out[component_name]
for rule in rules:
used_rules[rule] += 1
used_rules_of_components_out[component_name] = used_rules


def _count_components(components, rules_list, components_out, used_rules_of_components_out):
for component_name, component in components.items():
intersection = set(component.rules).intersection(set(rules_list))
components_out[component_name] += 0
if len(intersection) > 0:
components_out[component_name] += 1
if component_name not in used_rules_of_components_out:
used_rules_of_components_out[component_name] = {
rule_id: 0 for rule_id in component.rules
}

_count_rules_components(component_name, intersection, used_rules_of_components_out)


Expand Down Expand Up @@ -66,15 +69,26 @@ def _sort_rules_of_components(used_rules_of_components):
return out


def _remove_zero_counts_of(used_rules_of_components):
return {
component_name: remove_zero_counts(rules_dict)
for component_name, rules_dict in used_rules_of_components.items()
}


def command_most_used_components(args):
components = defaultdict(int)
used_rules_of_components = {}

_process_all_products_from_controls(components, used_rules_of_components, args.products)

if not args.all:
components = remove_zero_counts(components)
used_rules_of_components = _remove_zero_counts_of(used_rules_of_components)

sorted_components = _sorted_dict_by_num_value(components)
csv_header = "component_name,count_of_profiles"
if args.used_rules:
if args.rules:
csv_header = "component_name,count_of_profiles,used_rules:count_of_profiles"
delim = " "
if args.format == "csv":
Expand Down

0 comments on commit 56d38b9

Please sign in to comment.