| 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 |
||
| 329 | def show_profile_stats(self, profile, options): |
||
| 330 | """Displays statistics for specific profile""" |
||
| 331 | |||
| 332 | profile_stats = self.get_profile_stats(profile) |
||
| 333 | rules_count = profile_stats['rules_count'] |
||
| 334 | impl_ovals_count = len(profile_stats['implemented_ovals']) |
||
| 335 | impl_bash_fixes_count = len(profile_stats['implemented_bash_fixes']) |
||
| 336 | impl_ansible_fixes_count = len(profile_stats['implemented_ansible_fixes']) |
||
| 337 | impl_ignition_fixes_count = len(profile_stats['implemented_ignition_fixes']) |
||
| 338 | impl_kubernetes_fixes_count = len(profile_stats['implemented_kubernetes_fixes']) |
||
| 339 | impl_puppet_fixes_count = len(profile_stats['implemented_puppet_fixes']) |
||
| 340 | impl_anaconda_fixes_count = len(profile_stats['implemented_anaconda_fixes']) |
||
| 341 | missing_stig_ids_count = len(profile_stats['missing_stig_ids']) |
||
| 342 | missing_cis_refs_count = len(profile_stats['missing_cis_refs']) |
||
| 343 | missing_hipaa_refs_count = len(profile_stats['missing_hipaa_refs']) |
||
| 344 | missing_anssi_refs_count = len(profile_stats['missing_anssi_refs']) |
||
| 345 | missing_ospp_refs_count = len(profile_stats['missing_ospp_refs']) |
||
| 346 | missing_cui_refs_count = len(profile_stats['missing_cui_refs']) |
||
| 347 | impl_cces_count = len(profile_stats['assigned_cces']) |
||
| 348 | |||
| 349 | if options.format == "plain": |
||
| 350 | if not options.skip_overall_stats: |
||
| 351 | print("\nProfile %s:" % profile) |
||
| 352 | print("* rules: %d" % rules_count) |
||
| 353 | print("* checks (OVAL): %d\t[%d%% complete]" % |
||
| 354 | (impl_ovals_count, |
||
| 355 | profile_stats['implemented_ovals_pct'])) |
||
| 356 | |||
| 357 | print("* fixes (bash): %d\t[%d%% complete]" % |
||
| 358 | (impl_bash_fixes_count, |
||
| 359 | profile_stats['implemented_bash_fixes_pct'])) |
||
| 360 | print("* fixes (ansible): %d\t[%d%% complete]" % |
||
| 361 | (impl_ansible_fixes_count, |
||
| 362 | profile_stats['implemented_ansible_fixes_pct'])) |
||
| 363 | print("* fixes (ignition): %d\t[%d%% complete]" % |
||
| 364 | (impl_ignition_fixes_count, |
||
| 365 | profile_stats['implemented_ignition_fixes_pct'])) |
||
| 366 | print("* fixes (kubernetes): %d\t[%d%% complete]" % |
||
| 367 | (impl_kubernetes_fixes_count, |
||
| 368 | profile_stats['implemented_kubernetes_fixes_pct'])) |
||
| 369 | print("* fixes (puppet): %d\t[%d%% complete]" % |
||
| 370 | (impl_puppet_fixes_count, |
||
| 371 | profile_stats['implemented_puppet_fixes_pct'])) |
||
| 372 | print("* fixes (anaconda): %d\t[%d%% complete]" % |
||
| 373 | (impl_anaconda_fixes_count, |
||
| 374 | profile_stats['implemented_anaconda_fixes_pct'])) |
||
| 375 | |||
| 376 | print("* CCEs: %d\t[%d%% complete]" % |
||
| 377 | (impl_cces_count, |
||
| 378 | profile_stats['assigned_cces_pct'])) |
||
| 379 | |||
| 380 | if options.implemented_ovals and \ |
||
| 381 | profile_stats['implemented_ovals']: |
||
| 382 | print("** Rules of '%s' " % profile + |
||
| 383 | "profile having OVAL check: %d of %d [%d%% complete]" % |
||
| 384 | (impl_ovals_count, rules_count, |
||
| 385 | profile_stats['implemented_ovals_pct'])) |
||
| 386 | self.console_print(profile_stats['implemented_ovals'], |
||
| 387 | console_width) |
||
| 388 | |||
| 389 | if options.implemented_fixes: |
||
| 390 | if profile_stats['implemented_bash_fixes']: |
||
| 391 | print("*** Rules of '%s' profile having " |
||
| 392 | "a bash fix script: %d of %d [%d%% complete]" |
||
| 393 | % (profile, impl_bash_fixes_count, rules_count, |
||
| 394 | profile_stats['implemented_bash_fixes_pct'])) |
||
| 395 | self.console_print(profile_stats['implemented_bash_fixes'], |
||
| 396 | console_width) |
||
| 397 | |||
| 398 | if profile_stats['implemented_ansible_fixes']: |
||
| 399 | print("*** Rules of '%s' profile having " |
||
| 400 | "a ansible fix script: %d of %d [%d%% complete]" |
||
| 401 | % (profile, impl_ansible_fixes_count, rules_count, |
||
| 402 | profile_stats['implemented_ansible_fixes_pct'])) |
||
| 403 | self.console_print( |
||
| 404 | profile_stats['implemented_ansible_fixes'], |
||
| 405 | console_width) |
||
| 406 | |||
| 407 | if profile_stats['implemented_ignition_fixes']: |
||
| 408 | print("*** Rules of '%s' profile having " |
||
| 409 | "a ignition fix script: %d of %d [%d%% complete]" |
||
| 410 | % (profile, impl_ignition_fixes_count, rules_count, |
||
| 411 | profile_stats['implemented_ignition_fixes_pct'])) |
||
| 412 | self.console_print( |
||
| 413 | profile_stats['implemented_ignition_fixes'], |
||
| 414 | console_width) |
||
| 415 | |||
| 416 | if profile_stats['implemented_kubernetes_fixes']: |
||
| 417 | print("*** Rules of '%s' profile having " |
||
| 418 | "a kubernetes fix script: %d of %d [%d%% complete]" |
||
| 419 | % (profile, impl_kubernetes_fixes_count, rules_count, |
||
| 420 | profile_stats['implemented_kubernetes_fixes_pct'])) |
||
| 421 | self.console_print( |
||
| 422 | profile_stats['implemented_kubernetes_fixes'], |
||
| 423 | console_width) |
||
| 424 | |||
| 425 | if profile_stats['implemented_puppet_fixes']: |
||
| 426 | print("*** Rules of '%s' profile having " |
||
| 427 | "a puppet fix script: %d of %d [%d%% complete]" |
||
| 428 | % (profile, impl_puppet_fixes_count, rules_count, |
||
| 429 | profile_stats['implemented_puppet_fixes_pct'])) |
||
| 430 | self.console_print( |
||
| 431 | profile_stats['implemented_puppet_fixes'], |
||
| 432 | console_width) |
||
| 433 | |||
| 434 | if profile_stats['implemented_anaconda_fixes']: |
||
| 435 | print("*** Rules of '%s' profile having " |
||
| 436 | "a anaconda fix script: %d of %d [%d%% complete]" |
||
| 437 | % (profile, impl_anaconda_fixes_count, rules_count, |
||
| 438 | profile_stats['implemented_anaconda_fixes_pct'])) |
||
| 439 | self.console_print( |
||
| 440 | profile_stats['implemented_anaconda_fixes'], |
||
| 441 | console_width) |
||
| 442 | |||
| 443 | if options.assigned_cces and \ |
||
| 444 | profile_stats['assigned_cces']: |
||
| 445 | print("*** Rules of '%s' " % profile + |
||
| 446 | "profile having CCE assigned: %d of %d [%d%% complete]" % |
||
| 447 | (impl_cces_count, rules_count, |
||
| 448 | profile_stats['assigned_cces_pct'])) |
||
| 449 | self.console_print(profile_stats['assigned_cces'], |
||
| 450 | console_width) |
||
| 451 | |||
| 452 | if options.missing_ovals and profile_stats['missing_ovals']: |
||
| 453 | print("*** Rules of '%s' " % profile + "profile missing " + |
||
| 454 | "OVAL: %d of %d [%d%% complete]" % |
||
| 455 | (rules_count - impl_ovals_count, rules_count, |
||
| 456 | profile_stats['implemented_ovals_pct'])) |
||
| 457 | self.console_print(profile_stats['missing_ovals'], |
||
| 458 | console_width) |
||
| 459 | |||
| 460 | if options.missing_fixes: |
||
| 461 | if profile_stats['missing_bash_fixes']: |
||
| 462 | print("*** rules of '%s' profile missing " |
||
| 463 | "a bash fix script: %d of %d [%d%% complete]" |
||
| 464 | % (profile, rules_count - impl_bash_fixes_count, |
||
| 465 | rules_count, |
||
| 466 | profile_stats['implemented_bash_fixes_pct'])) |
||
| 467 | self.console_print(profile_stats['missing_bash_fixes'], |
||
| 468 | console_width) |
||
| 469 | |||
| 470 | if profile_stats['missing_ansible_fixes']: |
||
| 471 | print("*** rules of '%s' profile missing " |
||
| 472 | "a ansible fix script: %d of %d [%d%% complete]" |
||
| 473 | % (profile, rules_count - impl_ansible_fixes_count, |
||
| 474 | rules_count, |
||
| 475 | profile_stats['implemented_ansible_fixes_pct'])) |
||
| 476 | self.console_print(profile_stats['missing_ansible_fixes'], |
||
| 477 | console_width) |
||
| 478 | |||
| 479 | if profile_stats['missing_ignition_fixes']: |
||
| 480 | print("*** rules of '%s' profile missing " |
||
| 481 | "a ignition fix script: %d of %d [%d%% complete]" |
||
| 482 | % (profile, rules_count - impl_ignition_fixes_count, |
||
| 483 | rules_count, |
||
| 484 | profile_stats['implemented_ignition_fixes_pct'])) |
||
| 485 | self.console_print(profile_stats['missing_ignition_fixes'], |
||
| 486 | console_width) |
||
| 487 | |||
| 488 | if profile_stats['missing_kubernetes_fixes']: |
||
| 489 | print("*** rules of '%s' profile missing " |
||
| 490 | "a kubernetes fix script: %d of %d [%d%% complete]" |
||
| 491 | % (profile, rules_count - impl_kubernetes_fixes_count, |
||
| 492 | rules_count, |
||
| 493 | profile_stats['implemented_kubernetes_fixes_pct'])) |
||
| 494 | self.console_print(profile_stats['missing_kubernetes_fixes'], |
||
| 495 | console_width) |
||
| 496 | |||
| 497 | if profile_stats['missing_puppet_fixes']: |
||
| 498 | print("*** rules of '%s' profile missing " |
||
| 499 | "a puppet fix script: %d of %d [%d%% complete]" |
||
| 500 | % (profile, rules_count - impl_puppet_fixes_count, |
||
| 501 | rules_count, |
||
| 502 | profile_stats['implemented_puppet_fixes_pct'])) |
||
| 503 | self.console_print(profile_stats['missing_puppet_fixes'], |
||
| 504 | console_width) |
||
| 505 | |||
| 506 | if profile_stats['missing_anaconda_fixes']: |
||
| 507 | print("*** rules of '%s' profile missing " |
||
| 508 | "a anaconda fix script: %d of %d [%d%% complete]" |
||
| 509 | % (profile, rules_count - impl_anaconda_fixes_count, |
||
| 510 | rules_count, |
||
| 511 | profile_stats['implemented_anaconda_fixes_pct'])) |
||
| 512 | self.console_print(profile_stats['missing_anaconda_fixes'], |
||
| 513 | console_width) |
||
| 514 | |||
| 515 | if options.missing_stig_ids and profile_stats['missing_stig_ids']: |
||
| 516 | print("*** rules of '%s' profile missing " |
||
| 517 | "STIG IDs: %d of %d have them [%d%% missing]" |
||
| 518 | % (profile, rules_count - missing_stig_ids_count, |
||
| 519 | rules_count, |
||
| 520 | (100.0 * missing_stig_ids_count / rules_count))) |
||
| 521 | self.console_print(profile_stats['missing_stig_ids'], |
||
| 522 | console_width) |
||
| 523 | |||
| 524 | if options.missing_cis_refs and profile_stats['missing_cis_refs']: |
||
| 525 | print("*** rules of '%s' profile missing " |
||
| 526 | "CIS Refs: %d of %d have them [%d%% missing]" |
||
| 527 | % (profile, rules_count - missing_cis_refs_count, |
||
| 528 | rules_count, |
||
| 529 | (100.0 * missing_cis_refs_count / rules_count))) |
||
| 530 | self.console_print(profile_stats['missing_cis_refs'], |
||
| 531 | console_width) |
||
| 532 | |||
| 533 | if options.missing_hipaa_refs and profile_stats['missing_hipaa_refs']: |
||
| 534 | print("*** rules of '%s' profile missing " |
||
| 535 | "HIPAA Refs: %d of %d have them [%d%% missing]" |
||
| 536 | % (profile, rules_count - missing_hipaa_refs_count, |
||
| 537 | rules_count, |
||
| 538 | (100.0 * missing_hipaa_refs_count / rules_count))) |
||
| 539 | self.console_print(profile_stats['missing_hipaa_refs'], |
||
| 540 | console_width) |
||
| 541 | |||
| 542 | if options.missing_anssi_refs and profile_stats['missing_anssi_refs']: |
||
| 543 | print("*** rules of '%s' profile missing " |
||
| 544 | "ANSSI Refs: %d of %d have them [%d%% missing]" |
||
| 545 | % (profile, rules_count - missing_anssi_refs_count, |
||
| 546 | rules_count, |
||
| 547 | (100.0 * missing_anssi_refs_count / rules_count))) |
||
| 548 | self.console_print(profile_stats['missing_anssi_refs'], |
||
| 549 | console_width) |
||
| 550 | |||
| 551 | if options.missing_ospp_refs and profile_stats['missing_ospp_refs']: |
||
| 552 | print("*** rules of '%s' profile missing " |
||
| 553 | "OSPP Refs: %d of %d have them [%d%% missing]" |
||
| 554 | % (profile, rules_count - missing_ospp_refs_count, |
||
| 555 | rules_count, |
||
| 556 | (100.0 * missing_ospp_refs_count / rules_count))) |
||
| 557 | self.console_print(profile_stats['missing_ospp_refs'], |
||
| 558 | console_width) |
||
| 559 | |||
| 560 | if options.missing_cui_refs and profile_stats['missing_cui_refs']: |
||
| 561 | print("*** rules of '%s' profile missing " |
||
| 562 | "CUI Refs: %d of %d have them [%d%% missing]" |
||
| 563 | % (profile, rules_count - missing_cui_refs_count, |
||
| 564 | rules_count, |
||
| 565 | (100.0 * missing_cui_refs_count / rules_count))) |
||
| 566 | self.console_print(profile_stats['missing_cui_refs'], |
||
| 567 | console_width) |
||
| 568 | |||
| 569 | if options.missing_cces and profile_stats['missing_cces']: |
||
| 570 | print("***Rules of '%s' " % profile + "profile missing " + |
||
| 571 | "CCE identifier: %d of %d [%d%% complete]" % |
||
| 572 | (rules_count - impl_cces_count, rules_count, |
||
| 573 | profile_stats['assigned_cces_pct'])) |
||
| 574 | self.console_print(profile_stats['missing_cces'], |
||
| 575 | console_width) |
||
| 576 | |||
| 577 | if options.ansible_parity: |
||
| 578 | print("*** rules of '%s' profile with bash fix that implement " |
||
| 579 | "ansible fix scripts: %d out of %d [%d%% complete]" |
||
| 580 | % (profile, impl_bash_fixes_count - len(profile_stats['ansible_parity']), |
||
| 581 | impl_bash_fixes_count, |
||
| 582 | profile_stats['ansible_parity_pct'])) |
||
| 583 | self.console_print(profile_stats['ansible_parity'], |
||
| 584 | console_width) |
||
| 585 | |||
| 586 | elif options.format == "html": |
||
| 587 | del profile_stats['implemented_ovals'] |
||
| 588 | del profile_stats['implemented_bash_fixes'] |
||
| 589 | del profile_stats['implemented_ansible_fixes'] |
||
| 590 | del profile_stats['implemented_ignition_fixes'] |
||
| 591 | del profile_stats['implemented_kubernetes_fixes'] |
||
| 592 | del profile_stats['implemented_puppet_fixes'] |
||
| 593 | del profile_stats['implemented_anaconda_fixes'] |
||
| 594 | del profile_stats['assigned_cces'] |
||
| 595 | |||
| 596 | profile_stats['missing_stig_ids_count'] = missing_stig_ids_count |
||
| 597 | profile_stats['missing_cis_refs_count'] = missing_cis_refs_count |
||
| 598 | profile_stats['missing_hipaa_refs_count'] = missing_hipaa_refs_count |
||
| 599 | profile_stats['missing_anssi_refs_count'] = missing_anssi_refs_count |
||
| 600 | profile_stats['missing_ospp_refs_count'] = missing_ospp_refs_count |
||
| 601 | profile_stats['missing_cui_refs_count'] = missing_cui_refs_count |
||
| 602 | profile_stats['missing_ovals_count'] = len(profile_stats['missing_ovals']) |
||
| 603 | profile_stats['missing_bash_fixes_count'] = len(profile_stats['missing_bash_fixes']) |
||
| 604 | profile_stats['missing_ansible_fixes_count'] = len(profile_stats['missing_ansible_fixes']) |
||
| 605 | profile_stats['missing_ignition_fixes_count'] = len(profile_stats['missing_ignition_fixes']) |
||
| 606 | profile_stats['missing_kubernetes_fixes_count'] = \ |
||
| 607 | len(profile_stats['missing_kubernetes_fixes']) |
||
| 608 | profile_stats['missing_puppet_fixes_count'] = len(profile_stats['missing_puppet_fixes']) |
||
| 609 | profile_stats['missing_anaconda_fixes_count'] = len(profile_stats['missing_anaconda_fixes']) |
||
| 610 | profile_stats['missing_cces_count'] = len(profile_stats['missing_cces']) |
||
| 611 | |||
| 612 | del profile_stats['implemented_ovals_pct'] |
||
| 613 | del profile_stats['implemented_bash_fixes_pct'] |
||
| 614 | del profile_stats['implemented_ansible_fixes_pct'] |
||
| 615 | del profile_stats['implemented_ignition_fixes_pct'] |
||
| 616 | del profile_stats['implemented_kubernetes_fixes_pct'] |
||
| 617 | del profile_stats['implemented_puppet_fixes_pct'] |
||
| 618 | del profile_stats['implemented_anaconda_fixes_pct'] |
||
| 619 | del profile_stats['assigned_cces_pct'] |
||
| 620 | del profile_stats['ssg_version'] |
||
| 621 | del profile_stats['ansible_parity_pct'] |
||
| 622 | |||
| 623 | return profile_stats |
||
| 624 | else: |
||
| 625 | # First delete the not requested information |
||
| 626 | if not options.missing_ovals: |
||
| 627 | del profile_stats['missing_ovals'] |
||
| 628 | if not options.missing_fixes: |
||
| 629 | del profile_stats['missing_bash_fixes'] |
||
| 630 | del profile_stats['missing_ansible_fixes'] |
||
| 631 | del profile_stats['missing_ignition_fixes'] |
||
| 632 | del profile_stats['missing_kubernetes_fixes'] |
||
| 633 | del profile_stats['missing_puppet_fixes'] |
||
| 634 | del profile_stats['missing_anaconda_fixes'] |
||
| 635 | del profile_stats['missing_stig_ids'] |
||
| 636 | del profile_stats['missing_cis_refs'] |
||
| 637 | del profile_stats['missing_hipaa_refs'] |
||
| 638 | del profile_stats['missing_anssi_refs'] |
||
| 639 | del profile_stats['missing_ospp_refs'] |
||
| 640 | del profile_stats['missing_cui_refs'] |
||
| 641 | if not options.missing_cces: |
||
| 642 | del profile_stats['missing_cces'] |
||
| 643 | if not options.implemented_ovals: |
||
| 644 | del profile_stats['implemented_ovals'] |
||
| 645 | if not options.implemented_fixes: |
||
| 646 | del profile_stats['implemented_bash_fixes'] |
||
| 647 | del profile_stats['implemented_ansible_fixes'] |
||
| 648 | del profile_stats['implemented_ignition_fixes'] |
||
| 649 | del profile_stats['implemented_kubernetes_fixes'] |
||
| 650 | del profile_stats['implemented_puppet_fixes'] |
||
| 651 | del profile_stats['implemented_anaconda_fixes'] |
||
| 652 | if not options.assigned_cces: |
||
| 653 | del profile_stats['assigned_cces'] |
||
| 654 | |||
| 655 | del profile_stats['rules'] |
||
| 656 | |||
| 657 | return profile_stats |
||
| 658 | |||
| 672 |