def count(self): """ Counts the number of tokens in an Ansible script """ keys = len(utils.all_keys(self.playbook)) values = utils.all_values(self.playbook) return keys + sum( len(re.split(r'\s+', str(value).strip())) for value in values)
def count(self): """Return the number of included variables. Example ------- .. highlight:: python .. code-block:: python from ansiblemetrics.general.num_included_vars import NumIncludedVars playbook = ''' - name: Include a play after another play include_vars: myvars.yaml ''' NumIncludedVars(playbook).count() >> 1 Returns ------- int number of included variables """ script = self.playbook keys = utils.all_keys(script) return sum(1 for i in keys if i == 'include_vars')
def count(self): """Return the number of imported roles. Example ------- .. highlight:: python .. code-block:: python from ansiblemetrics.general.num_import_roles import NumImportedRoles playbook = ''' - import_role: name: myrole - name: Run tasks/other.yaml instead of "main" import_role: name: myrole tasks_from: other ''' NumImportedRoles(playbook).count() >> 2 Returns ------- int number of imported roles """ script = self.playbook keys = utils.all_keys(script) return sum(1 for i in keys if i == 'import_role')
def count(self): """Return the number of tokens. Example ------- .. highlight:: python .. code-block:: python from ansiblemetrics.general.num_tokens import NumTokens playbook = ''' - hosts: all tasks: - debug: msg: "Hello World" ''' NumTokens(playbook).count() >> 7 Returns ------- int Number of tokens """ keys = len(utils.all_keys(self.playbook)) values = utils.all_values(self.playbook) return keys + sum(len(re.split(r'\s+', str(value).strip())) for value in values)
def count(self): """Return the number of keys of the dictionary representing the playbook. Example ------- .. highlight:: python .. code-block:: python from ansiblemetrics.general.num_keys import NumKeys playbook = ''' - hosts: all tasks: - debug: msg: "Hello World" ''' NumKeys(playbook).count() >> 4 Returns ------- int Number of keys """ return len(utils.all_keys(self.playbook))
def count(self): """Return the number of imported tasks. Example ------- .. highlight:: python .. code-block:: python from ansiblemetrics.general.num_imported_tasks import NumImportedTasks playbook = ''' - name: Include task list in play import_tasks: stuff.yaml ''' NumImportedTasks(playbook).count() >> 1 Returns ------- int number of imported tasks """ script = self.playbook keys = utils.all_keys(script) return sum(1 for i in keys if i == 'import_tasks')
def count(self): """Return the number of included roles Example ------- .. highlight:: python .. code-block:: python from ansiblemetrics.general.num_included_roles import NumIncludedRoles playbook = ''' - name: Include task list in play include_role: role.yaml ''' NumIncludedRoles(playbook).count() >> 1 Returns ------- int number of included roles """ script = self.playbook keys = utils.all_keys(script) return sum(1 for i in keys if i == 'include_role')
def count(self): """Return the number of imported playbooks. Example ------- .. highlight:: python .. code-block:: python from ansiblemetrics.general.num_imported_playbooks import NumImportedPlaybooks playbook = ''' - name: Include a play after another play import_playbook: otherplays.yml - name: This fails because I'm inside a play already import_playbook: stuff.yaml ''' NumImportedPlaybooks(playbook).count() >> 2 Returns ------- int number of imported playbooks """ script = self.playbook keys = utils.all_keys(script) return sum(1 for i in keys if i == 'import_playbook')
def count(self): """ Returns the number of deprecated keywords in the script """ deprecated = [] keys = utils.all_keys(self.playbook) for k in keys: if k in DEPRECATED_KEYWORDS: deprecated.append(k) return len(deprecated)
def test_all_keys(): d = { "keyA": { "keyB": "valueB" }, "keyC": ["valueC1", "valueC2"], "keyD": "hello world" } keys = utils.all_keys(d) assert len(keys) == 4 assert "keyA" in keys assert "keyB" in keys assert "keyC" in keys assert "keyD" in keys
def count(self): """Return the playbook's text entropy. Example ------- .. highlight:: python .. code-block:: python from ansiblemetrics.general.text_entropy import TextEntropy playbook = ''' --- - hosts: all roles: - common - hosts: dbservers roles: - db - web ''' TextEntropy(playbook).count() >> 4.89 Returns ------- float Text entropy """ keys = utils.all_keys(self.playbook) values = utils.all_values(self.playbook) words = keys for v in values: words.extend(str(v).split()) words_set = set(words) freq = {word: words.count(word) for word in words_set} entropy = 0 for word in words_set: p = freq[word] / len(words) entropy -= p * log2(p) return round(entropy, 2)
def count(self): keys = utils.all_keys(self.playbook) values = utils.all_values(self.playbook) words = keys for v in values: words.extend(str(v).split()) words_set = set(words) freq = {word: words.count(word) for word in words_set} entropy = 0 for word in words_set: p = freq[word] / len(words) entropy -= p * log2(p) return round(entropy, 2)
def count(self): """Return the number of loops. Example ------- .. highlight:: python .. code-block:: python from ansiblemetrics.general.num_loops import NumLoops playbook = ''' --- - name: with_list debug: msg: "{{ item }}" with_list: # 1st loop - one - two - name: with_list -> loop debug: msg: "{{ item }}" loop: # 2nd loop - one - two ''' NumLoops(playbook).count() >> 2 Returns ------- int number of loops """ return sum(1 for key in utils.all_keys(self.playbook) if key == 'loop' or str(key).startswith('with_'))
def count(self): """Return the number of Ansible deprecated keywords. Example ------- .. highlight:: python .. code-block:: python from ansiblemetrics.general.num_deprecated_keywords import NumDeprecatedKeywords playbook = ''' - hosts: localhost tasks: - name: Hello, Ansible! action: rust_helloworld args: # Deprecated keyword name: Ansible ''' NumDeprecatedKeywords(playbook).count() >> 1 Returns ------- int Number of deprecated keywords """ deprecated = [] keys = utils.all_keys(self.playbook) for k in keys: if k in DEPRECATED_KEYWORDS: deprecated.append(k) return len(deprecated)
def count(self): script = self.playbook keys = utils.all_keys(script) return sum(1 for i in keys if i == 'import_tasks')
def count(self): """ Return the number of standard loops. """ return sum(1 for key in utils.all_keys(self.playbook) if key == 'loop' or str(key).startswith('with_'))
def count(self): return len(utils.all_keys(self.playbook))
def count(self): script = self.playbook keys = utils.all_keys(script) return sum(1 for i in keys if i == 'include')