| Conditions | 45 |
| Total Lines | 329 |
| Code Lines | 274 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 2070 |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
Complex classes like ssg.build_profile.XCCDFBenchmark.show_profile_stats() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | from __future__ import absolute_import |
||
| 349 | def show_profile_stats(self, profile, options): |
||
| 350 | """Displays statistics for specific profile""" |
||
| 351 | |||
| 352 | profile_stats = self.get_profile_stats(profile) |
||
| 353 | rules_count = profile_stats['rules_count'] |
||
| 354 | impl_ovals_count = len(profile_stats['implemented_ovals']) |
||
| 355 | impl_bash_fixes_count = len(profile_stats['implemented_bash_fixes']) |
||
| 356 | impl_ansible_fixes_count = len(profile_stats['implemented_ansible_fixes']) |
||
| 357 | impl_ignition_fixes_count = len(profile_stats['implemented_ignition_fixes']) |
||
| 358 | impl_kubernetes_fixes_count = len(profile_stats['implemented_kubernetes_fixes']) |
||
| 359 | impl_puppet_fixes_count = len(profile_stats['implemented_puppet_fixes']) |
||
| 360 | impl_anaconda_fixes_count = len(profile_stats['implemented_anaconda_fixes']) |
||
| 361 | missing_stig_ids_count = len(profile_stats['missing_stig_ids']) |
||
| 362 | missing_cis_refs_count = len(profile_stats['missing_cis_refs']) |
||
| 363 | missing_hipaa_refs_count = len(profile_stats['missing_hipaa_refs']) |
||
| 364 | missing_anssi_refs_count = len(profile_stats['missing_anssi_refs']) |
||
| 365 | missing_ospp_refs_count = len(profile_stats['missing_ospp_refs']) |
||
| 366 | missing_cui_refs_count = len(profile_stats['missing_cui_refs']) |
||
| 367 | impl_cces_count = len(profile_stats['assigned_cces']) |
||
| 368 | |||
| 369 | if options.format == "plain": |
||
| 370 | if not options.skip_overall_stats: |
||
| 371 | print("\nProfile %s:" % profile) |
||
| 372 | print("* rules: %d" % rules_count) |
||
| 373 | print("* checks (OVAL): %d\t[%d%% complete]" % |
||
| 374 | (impl_ovals_count, |
||
| 375 | profile_stats['implemented_ovals_pct'])) |
||
| 376 | |||
| 377 | print("* fixes (bash): %d\t[%d%% complete]" % |
||
| 378 | (impl_bash_fixes_count, |
||
| 379 | profile_stats['implemented_bash_fixes_pct'])) |
||
| 380 | print("* fixes (ansible): %d\t[%d%% complete]" % |
||
| 381 | (impl_ansible_fixes_count, |
||
| 382 | profile_stats['implemented_ansible_fixes_pct'])) |
||
| 383 | print("* fixes (ignition): %d\t[%d%% complete]" % |
||
| 384 | (impl_ignition_fixes_count, |
||
| 385 | profile_stats['implemented_ignition_fixes_pct'])) |
||
| 386 | print("* fixes (kubernetes): %d\t[%d%% complete]" % |
||
| 387 | (impl_kubernetes_fixes_count, |
||
| 388 | profile_stats['implemented_kubernetes_fixes_pct'])) |
||
| 389 | print("* fixes (puppet): %d\t[%d%% complete]" % |
||
| 390 | (impl_puppet_fixes_count, |
||
| 391 | profile_stats['implemented_puppet_fixes_pct'])) |
||
| 392 | print("* fixes (anaconda): %d\t[%d%% complete]" % |
||
| 393 | (impl_anaconda_fixes_count, |
||
| 394 | profile_stats['implemented_anaconda_fixes_pct'])) |
||
| 395 | |||
| 396 | print("* CCEs: %d\t[%d%% complete]" % |
||
| 397 | (impl_cces_count, |
||
| 398 | profile_stats['assigned_cces_pct'])) |
||
| 399 | |||
| 400 | if options.implemented_ovals and \ |
||
| 401 | profile_stats['implemented_ovals']: |
||
| 402 | print("** Rules of '%s' " % profile + |
||
| 403 | "profile having OVAL check: %d of %d [%d%% complete]" % |
||
| 404 | (impl_ovals_count, rules_count, |
||
| 405 | profile_stats['implemented_ovals_pct'])) |
||
| 406 | self.console_print(profile_stats['implemented_ovals'], |
||
| 407 | console_width) |
||
| 408 | |||
| 409 | if options.implemented_fixes: |
||
| 410 | if profile_stats['implemented_bash_fixes']: |
||
| 411 | print("*** Rules of '%s' profile having " |
||
| 412 | "a bash fix script: %d of %d [%d%% complete]" |
||
| 413 | % (profile, impl_bash_fixes_count, rules_count, |
||
| 414 | profile_stats['implemented_bash_fixes_pct'])) |
||
| 415 | self.console_print(profile_stats['implemented_bash_fixes'], |
||
| 416 | console_width) |
||
| 417 | |||
| 418 | if profile_stats['implemented_ansible_fixes']: |
||
| 419 | print("*** Rules of '%s' profile having " |
||
| 420 | "a ansible fix script: %d of %d [%d%% complete]" |
||
| 421 | % (profile, impl_ansible_fixes_count, rules_count, |
||
| 422 | profile_stats['implemented_ansible_fixes_pct'])) |
||
| 423 | self.console_print( |
||
| 424 | profile_stats['implemented_ansible_fixes'], |
||
| 425 | console_width) |
||
| 426 | |||
| 427 | if profile_stats['implemented_ignition_fixes']: |
||
| 428 | print("*** Rules of '%s' profile having " |
||
| 429 | "a ignition fix script: %d of %d [%d%% complete]" |
||
| 430 | % (profile, impl_ignition_fixes_count, rules_count, |
||
| 431 | profile_stats['implemented_ignition_fixes_pct'])) |
||
| 432 | self.console_print( |
||
| 433 | profile_stats['implemented_ignition_fixes'], |
||
| 434 | console_width) |
||
| 435 | |||
| 436 | if profile_stats['implemented_kubernetes_fixes']: |
||
| 437 | print("*** Rules of '%s' profile having " |
||
| 438 | "a kubernetes fix script: %d of %d [%d%% complete]" |
||
| 439 | % (profile, impl_kubernetes_fixes_count, rules_count, |
||
| 440 | profile_stats['implemented_kubernetes_fixes_pct'])) |
||
| 441 | self.console_print( |
||
| 442 | profile_stats['implemented_kubernetes_fixes'], |
||
| 443 | console_width) |
||
| 444 | |||
| 445 | if profile_stats['implemented_puppet_fixes']: |
||
| 446 | print("*** Rules of '%s' profile having " |
||
| 447 | "a puppet fix script: %d of %d [%d%% complete]" |
||
| 448 | % (profile, impl_puppet_fixes_count, rules_count, |
||
| 449 | profile_stats['implemented_puppet_fixes_pct'])) |
||
| 450 | self.console_print( |
||
| 451 | profile_stats['implemented_puppet_fixes'], |
||
| 452 | console_width) |
||
| 453 | |||
| 454 | if profile_stats['implemented_anaconda_fixes']: |
||
| 455 | print("*** Rules of '%s' profile having " |
||
| 456 | "a anaconda fix script: %d of %d [%d%% complete]" |
||
| 457 | % (profile, impl_anaconda_fixes_count, rules_count, |
||
| 458 | profile_stats['implemented_anaconda_fixes_pct'])) |
||
| 459 | self.console_print( |
||
| 460 | profile_stats['implemented_anaconda_fixes'], |
||
| 461 | console_width) |
||
| 462 | |||
| 463 | if options.assigned_cces and \ |
||
| 464 | profile_stats['assigned_cces']: |
||
| 465 | print("*** Rules of '%s' " % profile + |
||
| 466 | "profile having CCE assigned: %d of %d [%d%% complete]" % |
||
| 467 | (impl_cces_count, rules_count, |
||
| 468 | profile_stats['assigned_cces_pct'])) |
||
| 469 | self.console_print(profile_stats['assigned_cces'], |
||
| 470 | console_width) |
||
| 471 | |||
| 472 | if options.missing_ovals and profile_stats['missing_ovals']: |
||
| 473 | print("*** Rules of '%s' " % profile + "profile missing " + |
||
| 474 | "OVAL: %d of %d [%d%% complete]" % |
||
| 475 | (rules_count - impl_ovals_count, rules_count, |
||
| 476 | profile_stats['implemented_ovals_pct'])) |
||
| 477 | self.console_print(profile_stats['missing_ovals'], |
||
| 478 | console_width) |
||
| 479 | |||
| 480 | if options.missing_fixes: |
||
| 481 | if profile_stats['missing_bash_fixes']: |
||
| 482 | print("*** rules of '%s' profile missing " |
||
| 483 | "a bash fix script: %d of %d [%d%% complete]" |
||
| 484 | % (profile, rules_count - impl_bash_fixes_count, |
||
| 485 | rules_count, |
||
| 486 | profile_stats['implemented_bash_fixes_pct'])) |
||
| 487 | self.console_print(profile_stats['missing_bash_fixes'], |
||
| 488 | console_width) |
||
| 489 | |||
| 490 | if profile_stats['missing_ansible_fixes']: |
||
| 491 | print("*** rules of '%s' profile missing " |
||
| 492 | "a ansible fix script: %d of %d [%d%% complete]" |
||
| 493 | % (profile, rules_count - impl_ansible_fixes_count, |
||
| 494 | rules_count, |
||
| 495 | profile_stats['implemented_ansible_fixes_pct'])) |
||
| 496 | self.console_print(profile_stats['missing_ansible_fixes'], |
||
| 497 | console_width) |
||
| 498 | |||
| 499 | if profile_stats['missing_ignition_fixes']: |
||
| 500 | print("*** rules of '%s' profile missing " |
||
| 501 | "a ignition fix script: %d of %d [%d%% complete]" |
||
| 502 | % (profile, rules_count - impl_ignition_fixes_count, |
||
| 503 | rules_count, |
||
| 504 | profile_stats['implemented_ignition_fixes_pct'])) |
||
| 505 | self.console_print(profile_stats['missing_ignition_fixes'], |
||
| 506 | console_width) |
||
| 507 | |||
| 508 | if profile_stats['missing_kubernetes_fixes']: |
||
| 509 | print("*** rules of '%s' profile missing " |
||
| 510 | "a kubernetes fix script: %d of %d [%d%% complete]" |
||
| 511 | % (profile, rules_count - impl_kubernetes_fixes_count, |
||
| 512 | rules_count, |
||
| 513 | profile_stats['implemented_kubernetes_fixes_pct'])) |
||
| 514 | self.console_print(profile_stats['missing_kubernetes_fixes'], |
||
| 515 | console_width) |
||
| 516 | |||
| 517 | if profile_stats['missing_puppet_fixes']: |
||
| 518 | print("*** rules of '%s' profile missing " |
||
| 519 | "a puppet fix script: %d of %d [%d%% complete]" |
||
| 520 | % (profile, rules_count - impl_puppet_fixes_count, |
||
| 521 | rules_count, |
||
| 522 | profile_stats['implemented_puppet_fixes_pct'])) |
||
| 523 | self.console_print(profile_stats['missing_puppet_fixes'], |
||
| 524 | console_width) |
||
| 525 | |||
| 526 | if profile_stats['missing_anaconda_fixes']: |
||
| 527 | print("*** rules of '%s' profile missing " |
||
| 528 | "a anaconda fix script: %d of %d [%d%% complete]" |
||
| 529 | % (profile, rules_count - impl_anaconda_fixes_count, |
||
| 530 | rules_count, |
||
| 531 | profile_stats['implemented_anaconda_fixes_pct'])) |
||
| 532 | self.console_print(profile_stats['missing_anaconda_fixes'], |
||
| 533 | console_width) |
||
| 534 | |||
| 535 | if options.missing_stig_ids and profile_stats['missing_stig_ids']: |
||
| 536 | print("*** rules of '%s' profile missing " |
||
| 537 | "STIG IDs: %d of %d have them [%d%% missing]" |
||
| 538 | % (profile, rules_count - missing_stig_ids_count, |
||
| 539 | rules_count, |
||
| 540 | (100.0 * missing_stig_ids_count / rules_count))) |
||
| 541 | self.console_print(profile_stats['missing_stig_ids'], |
||
| 542 | console_width) |
||
| 543 | |||
| 544 | if options.missing_cis_refs and profile_stats['missing_cis_refs']: |
||
| 545 | print("*** rules of '%s' profile missing " |
||
| 546 | "CIS Refs: %d of %d have them [%d%% missing]" |
||
| 547 | % (profile, rules_count - missing_cis_refs_count, |
||
| 548 | rules_count, |
||
| 549 | (100.0 * missing_cis_refs_count / rules_count))) |
||
| 550 | self.console_print(profile_stats['missing_cis_refs'], |
||
| 551 | console_width) |
||
| 552 | |||
| 553 | if options.missing_hipaa_refs and profile_stats['missing_hipaa_refs']: |
||
| 554 | print("*** rules of '%s' profile missing " |
||
| 555 | "HIPAA Refs: %d of %d have them [%d%% missing]" |
||
| 556 | % (profile, rules_count - missing_hipaa_refs_count, |
||
| 557 | rules_count, |
||
| 558 | (100.0 * missing_hipaa_refs_count / rules_count))) |
||
| 559 | self.console_print(profile_stats['missing_hipaa_refs'], |
||
| 560 | console_width) |
||
| 561 | |||
| 562 | if options.missing_anssi_refs and profile_stats['missing_anssi_refs']: |
||
| 563 | print("*** rules of '%s' profile missing " |
||
| 564 | "ANSSI Refs: %d of %d have them [%d%% missing]" |
||
| 565 | % (profile, rules_count - missing_anssi_refs_count, |
||
| 566 | rules_count, |
||
| 567 | (100.0 * missing_anssi_refs_count / rules_count))) |
||
| 568 | self.console_print(profile_stats['missing_anssi_refs'], |
||
| 569 | console_width) |
||
| 570 | |||
| 571 | if options.missing_ospp_refs and profile_stats['missing_ospp_refs']: |
||
| 572 | print("*** rules of '%s' profile missing " |
||
| 573 | "OSPP Refs: %d of %d have them [%d%% missing]" |
||
| 574 | % (profile, rules_count - missing_ospp_refs_count, |
||
| 575 | rules_count, |
||
| 576 | (100.0 * missing_ospp_refs_count / rules_count))) |
||
| 577 | self.console_print(profile_stats['missing_ospp_refs'], |
||
| 578 | console_width) |
||
| 579 | |||
| 580 | if options.missing_cui_refs and profile_stats['missing_cui_refs']: |
||
| 581 | print("*** rules of '%s' profile missing " |
||
| 582 | "CUI Refs: %d of %d have them [%d%% missing]" |
||
| 583 | % (profile, rules_count - missing_cui_refs_count, |
||
| 584 | rules_count, |
||
| 585 | (100.0 * missing_cui_refs_count / rules_count))) |
||
| 586 | self.console_print(profile_stats['missing_cui_refs'], |
||
| 587 | console_width) |
||
| 588 | |||
| 589 | if options.missing_cces and profile_stats['missing_cces']: |
||
| 590 | print("***Rules of '%s' " % profile + "profile missing " + |
||
| 591 | "CCE identifier: %d of %d [%d%% complete]" % |
||
| 592 | (rules_count - impl_cces_count, rules_count, |
||
| 593 | profile_stats['assigned_cces_pct'])) |
||
| 594 | self.console_print(profile_stats['missing_cces'], |
||
| 595 | console_width) |
||
| 596 | |||
| 597 | if options.ansible_parity: |
||
| 598 | print("*** rules of '%s' profile with bash fix that implement " |
||
| 599 | "ansible fix scripts: %d out of %d [%d%% complete]" |
||
| 600 | % (profile, impl_bash_fixes_count - len(profile_stats['ansible_parity']), |
||
| 601 | impl_bash_fixes_count, |
||
| 602 | profile_stats['ansible_parity_pct'])) |
||
| 603 | self.console_print(profile_stats['ansible_parity'], |
||
| 604 | console_width) |
||
| 605 | |||
| 606 | elif options.format == "html": |
||
| 607 | del profile_stats['implemented_ovals'] |
||
| 608 | del profile_stats['implemented_bash_fixes'] |
||
| 609 | del profile_stats['implemented_ansible_fixes'] |
||
| 610 | del profile_stats['implemented_ignition_fixes'] |
||
| 611 | del profile_stats['implemented_kubernetes_fixes'] |
||
| 612 | del profile_stats['implemented_puppet_fixes'] |
||
| 613 | del profile_stats['implemented_anaconda_fixes'] |
||
| 614 | del profile_stats['assigned_cces'] |
||
| 615 | |||
| 616 | profile_stats['missing_stig_ids_count'] = missing_stig_ids_count |
||
| 617 | profile_stats['missing_cis_refs_count'] = missing_cis_refs_count |
||
| 618 | profile_stats['missing_hipaa_refs_count'] = missing_hipaa_refs_count |
||
| 619 | profile_stats['missing_anssi_refs_count'] = missing_anssi_refs_count |
||
| 620 | profile_stats['missing_ospp_refs_count'] = missing_ospp_refs_count |
||
| 621 | profile_stats['missing_cui_refs_count'] = missing_cui_refs_count |
||
| 622 | profile_stats['missing_ovals_count'] = len(profile_stats['missing_ovals']) |
||
| 623 | profile_stats['missing_bash_fixes_count'] = len(profile_stats['missing_bash_fixes']) |
||
| 624 | profile_stats['missing_ansible_fixes_count'] = len(profile_stats['missing_ansible_fixes']) |
||
| 625 | profile_stats['missing_ignition_fixes_count'] = len(profile_stats['missing_ignition_fixes']) |
||
| 626 | profile_stats['missing_kubernetes_fixes_count'] = \ |
||
| 627 | len(profile_stats['missing_kubernetes_fixes']) |
||
| 628 | profile_stats['missing_puppet_fixes_count'] = len(profile_stats['missing_puppet_fixes']) |
||
| 629 | profile_stats['missing_anaconda_fixes_count'] = len(profile_stats['missing_anaconda_fixes']) |
||
| 630 | profile_stats['missing_cces_count'] = len(profile_stats['missing_cces']) |
||
| 631 | |||
| 632 | del profile_stats['implemented_ovals_pct'] |
||
| 633 | del profile_stats['implemented_bash_fixes_pct'] |
||
| 634 | del profile_stats['implemented_ansible_fixes_pct'] |
||
| 635 | del profile_stats['implemented_ignition_fixes_pct'] |
||
| 636 | del profile_stats['implemented_kubernetes_fixes_pct'] |
||
| 637 | del profile_stats['implemented_puppet_fixes_pct'] |
||
| 638 | del profile_stats['implemented_anaconda_fixes_pct'] |
||
| 639 | del profile_stats['assigned_cces_pct'] |
||
| 640 | del profile_stats['ssg_version'] |
||
| 641 | del profile_stats['ansible_parity_pct'] |
||
| 642 | |||
| 643 | return profile_stats |
||
| 644 | else: |
||
| 645 | # First delete the not requested information |
||
| 646 | if not options.missing_ovals: |
||
| 647 | del profile_stats['missing_ovals'] |
||
| 648 | if not options.missing_fixes: |
||
| 649 | del profile_stats['missing_bash_fixes'] |
||
| 650 | del profile_stats['missing_ansible_fixes'] |
||
| 651 | del profile_stats['missing_ignition_fixes'] |
||
| 652 | del profile_stats['missing_kubernetes_fixes'] |
||
| 653 | del profile_stats['missing_puppet_fixes'] |
||
| 654 | del profile_stats['missing_anaconda_fixes'] |
||
| 655 | del profile_stats['missing_stig_ids'] |
||
| 656 | del profile_stats['missing_cis_refs'] |
||
| 657 | del profile_stats['missing_hipaa_refs'] |
||
| 658 | del profile_stats['missing_anssi_refs'] |
||
| 659 | del profile_stats['missing_ospp_refs'] |
||
| 660 | del profile_stats['missing_cui_refs'] |
||
| 661 | if not options.missing_cces: |
||
| 662 | del profile_stats['missing_cces'] |
||
| 663 | if not options.implemented_ovals: |
||
| 664 | del profile_stats['implemented_ovals'] |
||
| 665 | if not options.implemented_fixes: |
||
| 666 | del profile_stats['implemented_bash_fixes'] |
||
| 667 | del profile_stats['implemented_ansible_fixes'] |
||
| 668 | del profile_stats['implemented_ignition_fixes'] |
||
| 669 | del profile_stats['implemented_kubernetes_fixes'] |
||
| 670 | del profile_stats['implemented_puppet_fixes'] |
||
| 671 | del profile_stats['implemented_anaconda_fixes'] |
||
| 672 | if not options.assigned_cces: |
||
| 673 | del profile_stats['assigned_cces'] |
||
| 674 | |||
| 675 | del profile_stats['rules'] |
||
| 676 | |||
| 677 | return profile_stats |
||
| 678 | |||
| 692 |