ComplianceAsCode/content

View on GitHub
utils/ansible_playbook_to_role.py

Summary

Maintainability
C
1 day
Test Coverage

File ansible_playbook_to_role.py has 487 lines of code (exceeds 400 allowed). Consider refactoring.
Open

#!/usr/bin/python3

from __future__ import print_function

from tempfile import mkdtemp
Severity: Minor
Found in utils/ansible_playbook_to_role.py - About 4 hrs to fix

    Function main has a Cognitive Complexity of 21 (exceeds 7 allowed). Consider refactoring.
    Open

    def main():
        args = parse_args()
    
        product_allowlist = set(PRODUCT_ALLOWLIST)
        profile_allowlist = set(PROFILE_ALLOWLIST)
    Severity: Minor
    Found in utils/ansible_playbook_to_role.py - About 2 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    PlaybookToRoleConverter has 23 functions (exceeds 20 allowed). Consider refactoring.
    Open

    class PlaybookToRoleConverter():
        PRODUCED_FILES = ('defaults/main.yml', 'meta/main.yml', 'tasks/main.yml', 'vars/main.yml',
                          'README.md')
    
        def __init__(self, local_playbook_filename):
    Severity: Minor
    Found in utils/ansible_playbook_to_role.py - About 2 hrs to fix

      Cyclomatic complexity is too high in function main. (14)
      Open

      def main():
          args = parse_args()
      
          product_allowlist = set(PRODUCT_ALLOWLIST)
          profile_allowlist = set(PROFILE_ALLOWLIST)
      Severity: Minor
      Found in utils/ansible_playbook_to_role.py by radon

      Cyclomatic Complexity

      Cyclomatic Complexity corresponds to the number of decisions a block of code contains plus 1. This number (also called McCabe number) is equal to the number of linearly independent paths through the code. This number can be used as a guide when testing conditional logic in blocks.

      Radon analyzes the AST tree of a Python program to compute Cyclomatic Complexity. Statements have the following effects on Cyclomatic Complexity:

      Construct Effect on CC Reasoning
      if +1 An if statement is a single decision.
      elif +1 The elif statement adds another decision.
      else +0 The else statement does not cause a new decision. The decision is at the if.
      for +1 There is a decision at the start of the loop.
      while +1 There is a decision at the while statement.
      except +1 Each except branch adds a new conditional path of execution.
      finally +0 The finally block is unconditionally executed.
      with +1 The with statement roughly corresponds to a try/except block (see PEP 343 for details).
      assert +1 The assert statement internally roughly equals a conditional statement.
      Comprehension +1 A list/set/dict comprehension of generator expression is equivalent to a for loop.
      Boolean Operator +1 Every boolean operator (and, or) adds a decision point.

      Source: http://radon.readthedocs.org/en/latest/intro.html

      Avoid too many return statements within this function.
      Open

                  return self._generate_meta_content()
      Severity: Major
      Found in utils/ansible_playbook_to_role.py - About 30 mins to fix

        Avoid too many return statements within this function.
        Open

                    return self._generate_defaults_content()
        Severity: Major
        Found in utils/ansible_playbook_to_role.py - About 30 mins to fix

          Function added_variables has a Cognitive Complexity of 8 (exceeds 7 allowed). Consider refactoring.
          Open

              def added_variables(self):
                  variables = set()
                  for task in self.tasks_data:
                      if "tags" not in task:
                          next
          Severity: Minor
          Found in utils/ansible_playbook_to_role.py - About 25 mins to fix

          Cognitive Complexity

          Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

          A method's cognitive complexity is based on a few simple rules:

          • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
          • Code is considered more complex for each "break in the linear flow of the code"
          • Code is considered more complex when "flow breaking structures are nested"

          Further reading

          Function file has a Cognitive Complexity of 8 (exceeds 7 allowed). Consider refactoring.
          Open

              def file(self, filepath):
                  if filepath == 'tasks/main.yml':
                      return self.tasks_local_content
                  elif filepath == 'vars/main.yml':
                      if len(self.vars_data) < 1:
          Severity: Minor
          Found in utils/ansible_playbook_to_role.py - About 25 mins to fix

          Cognitive Complexity

          Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

          A method's cognitive complexity is based on a few simple rules:

          • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
          • Code is considered more complex for each "break in the linear flow of the code"
          • Code is considered more complex when "flow breaking structures are nested"

          Further reading

          Refactor this function to reduce its Cognitive Complexity from 21 to the 15 allowed.
          Open

          def main():
          Severity: Critical
          Found in utils/ansible_playbook_to_role.py by sonar-python

          Cognitive Complexity is a measure of how hard the control flow of a function is to understand. Functions with high Cognitive Complexity will be difficult to maintain.

          See

          Either remove or fill this block of code.
          Open

                          pass

          Most of the time a block of code is empty when a piece of code is really missing. So such empty block must be either filled or removed.

          Noncompliant Code Example

          for i in range(3):
              pass
          

          Exceptions

          When a block contains a comment, this block is not considered to be empty.

          Line too long (140 > 99 characters)
          Open

                  print("Converting Ansible Playbook {} to Ansible Role {}".format(self._local_playbook_filename, os.path.join(directory, self.name)))
          Severity: Minor
          Found in utils/ansible_playbook_to_role.py by pep8

          Limit all lines to a maximum of 79 characters.

          There are still many devices around that are limited to 80 character
          lines; plus, limiting windows to 80 characters makes it possible to
          have several windows side-by-side.  The default wrapping on such
          devices looks ugly.  Therefore, please limit all lines to a maximum
          of 79 characters. For flowing long blocks of text (docstrings or
          comments), limiting the length to 72 characters is recommended.
          
          Reports error E501.

          Expected 2 blank lines after class or function definition, found 1
          Open

          _mapping_tag = yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG
          Severity: Minor
          Found in utils/ansible_playbook_to_role.py by pep8

          Separate top-level function and class definitions with two blank lines.

          Method definitions inside a class are separated by a single blank
          line.
          
          Extra blank lines may be used (sparingly) to separate groups of
          related functions.  Blank lines may be omitted between a bunch of
          related one-liners (e.g. a set of dummy implementations).
          
          Use blank lines in functions, sparingly, to indicate logical
          sections.
          
          Okay: def a():\n    pass\n\n\ndef b():\n    pass
          Okay: def a():\n    pass\n\n\nasync def b():\n    pass
          Okay: def a():\n    pass\n\n\n# Foo\n# Bar\n\ndef b():\n    pass
          Okay: default = 1\nfoo = 1
          Okay: classify = 1\nfoo = 1
          
          E301: class Foo:\n    b = 0\n    def bar():\n        pass
          E302: def a():\n    pass\n\ndef b(n):\n    pass
          E302: def a():\n    pass\n\nasync def b(n):\n    pass
          E303: def a():\n    pass\n\n\n\ndef b(n):\n    pass
          E303: def a():\n\n\n\n    pass
          E304: @decorator\n\ndef a():\n    pass
          E305: def a():\n    pass\na()
          E306: def a():\n    def b():\n        pass\n    def c():\n        pass

          Line too long (126 > 99 characters)
          Open

                  github_new_repos = sorted(list(set(map(str.lower, selected_roles.keys())) - set(map(str.lower, github_repositories))))
          Severity: Minor
          Found in utils/ansible_playbook_to_role.py by pep8

          Limit all lines to a maximum of 79 characters.

          There are still many devices around that are limited to 80 character
          lines; plus, limiting windows to 80 characters makes it possible to
          have several windows side-by-side.  The default wrapping on such
          devices looks ugly.  Therefore, please limit all lines to a maximum
          of 79 characters. For flowing long blocks of text (docstrings or
          comments), limiting the length to 72 characters is recommended.
          
          Reports error E501.

          There are no issues that match your filters.

          Category
          Status