def count(self): """Return the number of Example ------- .. highlight:: python .. code-block:: python from ansiblemetrics.general.num_prompts import NumPrompts playbook = ''' - hosts: all remote_user: root vars_prompt: - name: "name" prompt: "what is your name?" - name: "quest" prompt: "what is your quest?" - name: "favcolor" prompt: "what is your favorite color?" ''' NumPrompts(playbook).count() >> 3 Returns ------- int number of """ return sum(1 for k, v in key_value_list(self.playbook) if k == 'prompt')
def count(self): """Return the number of lookups. Example ------- .. highlight:: python .. code-block:: python from ansiblemetrics.general.num_lookups import NumLookups playbook = ''' - hosts: all vars: contents: "{{ lookup('file', '/etc/foo.txt') }}" ''' NumLookups(playbook).count() >> 1 Returns ------- int number of lookups """ lookups = 0 for item in key_value_list(self.playbook): v = item[1] if re.search(r'\{\{\s*lookup\(.*\)\s*\}\}', str(v)) is not None: lookups += 1 return lookups
def count(self): """ Return the number of lookups""" lookups = 0 for item in key_value_list(self.playbook): v = item[1] if re.search(r'\{\{\s*lookup\(.*\)\s*\}\}', str(v)) is not None: lookups += 1 return lookups
def count(self): """ Return the number of filters. """ filters = 0 for item in key_value_list(self.playbook): v = item[1] if v is not None and re.search(r'\{\{.*\}\}', str( v)) is not None: # check for statements with brackets, such as: {{ foobar }} filters += len(filter_regex.findall(str(v))) return filters
def count(self): """Return the number of plays and tasks with a unique name. Example ------- .. highlight:: python .. code-block:: python from ansiblemetrics.general.num_unique_names import NumUniqueNames playbook = ''' --- - name: demo the logic # unique name hosts: localhost gather_facts: false vars: num1: 10 num3: 10 tasks: - name: logic and comparison # duplicate debug: msg: "Can you read me?" when: num1 >= num3 and num1 is even and num2 is not defined - name: logic and comparison # duplicate debug: msg: "Can you read me again?" when: num3 >= num1 ''' NumUniqueNames(playbook).count() >> 1 Returns ------- int number of plays and tasks with a unique name """ names = [] for item in key_value_list(self.playbook): # [(key, value)] if item[0] == 'name': item = re.sub(r'\s+', '', str(item[1])) names.append(item.strip()) frequencies = Counter(names).values() # counts the elements' frequency unique = sum(1 for v in frequencies if v == 1) return unique
def count(self): conditions = 0 for k, v in key_value_list(self.playbook): if k not in ('when', 'changed_when', 'failed_when') or v is None: continue if type(v) == bool: conditions += 1 else: conditions += len(COMPARISON_OPERATORS.findall(str(v))) return conditions
def count(self, relative=False): """ Counts the number of names using variables in the script. relative: boolean - if True returns the relative number, in the interval [0-1], of names using variables in the script. Default is False. """ names_with_vars = 0 for k, v in key_value_list(self.playbook): if 'name' == k: if re.search(r'\{{2,2}\s*([\w\W]+)\s*\}{2,2}', str(v)) is not None: names_with_vars += 1 return names_with_vars
def count(self): """ Return the number of unique names in a Ansible script. """ names = [] for item in key_value_list(self.playbook): # [(key, value)] if item[0] == 'name': item = re.sub(r'\s+', '', str(item[1])) names.append(item.strip()) frequencies = Counter(names).values() # counts the elements' frequency unique = sum(1 for v in frequencies if v == 1) return unique
def count(self): """Return the number of conditions. Example ------- .. highlight:: python .. code-block:: python from ansiblemetrics.general.num_conditions import NumConditions playbook = ''' - hosts: all vars: - hello_msg: "Hello World" tasks: - debug: msg: "Equals" when: - "Hello" in hello_msg # 1st condition - "World" in hello_msg # 2nd condition ''' NumConditions(playbook).count() >> 2 Returns ------- int Number of conditions """ conditions = 0 for k, v in key_value_list(self.playbook): if k not in ('when', 'changed_when', 'failed_when') or v is None: continue if type(v) == bool: conditions += 1 else: conditions += len(COMPARISON_OPERATORS.findall(str(v))) return conditions
def count(self): """Return the number of plays and tasks with names referencing variables. Example ------- .. highlight:: python .. code-block:: python from ansiblemetrics.general.num_name_with_vars import NumNameWithVars playbook = ''' - name: play with a {{ var_name }} # uses variable hosts: localhost vars: var_name: not-mastery tasks: - name: set a variable # does not use variable set_fact: task_var_name: "defined variable" ''' NumNameWithVars(playbook).count() >> 1 Returns ------- int number of plays and tasks with names referencing variables """ names_with_vars = 0 for k, v in key_value_list(self.playbook): if 'name' == k: if re.search(r'\{{2,2}\s*([\w\W]+)\s*\}{2,2}', str(v)) is not None: names_with_vars += 1 return names_with_vars
def count(self): """Return the number of mathematical operations. Example ------- .. highlight:: python .. code-block:: python from ansiblemetrics.general.num_math_operations import NumMathOperations playbook = ''' - hosts: localhost tasks: - debug: msg: "addition: {{ 4 + 3 }}" # 1st operation - debug: msg: "subtraction: {{ 4 - 3 }}" # 2nd operation - debug: msg: "multiplication: {{ 4 * 3 }}" # 3rd operation - debug: msg: "Modulo operation: {{ 7 % 4}}" # 4th operation - debug: msg: "floating division: {{ 4 / 3}}" # 5th operation ''' NumMathOperations(playbook).count() >> 5 Returns ------- int Number of math operations """ return sum( len(re.findall(OPERANDS, str(v))) for k, v in key_value_list(self.playbook))
def count(self): """Return the number of filters. Example ------- .. highlight:: python .. code-block:: python from ansiblemetrics.general.num_filters import NumFilters playbook = ''' - shell: cat /some/path/to/multidoc-file.yaml register: result - debug: msg: '{{ item }}' loop: '{{ result.stdout | from_yaml_all | list }}' # 2 filters ''' NumFilters(playbook).count() >> 2 Returns ------- int number of filters """ filters = 0 for item in key_value_list(self.playbook): v = item[1] if v is not None and re.search( r'\{\{.*\}\}', str(v) ) is not None: # check for statements with brackets, such as: {{ foobar }} filters += len(filter_regex.findall(str(v))) return filters
def count(self): """ Return the number of prompts in a playbook. """ return sum(1 for k, v in key_value_list(self.playbook) if k == 'prompt')
def count(self): return sum( len(re.findall(OPERANDS, str(v))) for k, v in key_value_list(self.playbook))