Skip to main content

The static source code measurement tool for Tosca

Build Status LGTM Grade Codecov coverage pypi-version python-version

ToscaMetrics is a Python-based static source code measurement tool to characterize Infrastructure-as-Code. It helps quantify the characteristics of Tosca blueprints to support DevOps engineers when maintaining and evolving it. It currently supports 17 source code metrics, though other metrics can be derived by combining the implemented ones.


How to install

Installation is made simple by the PyPI repository. Download the tool and install it with:

pip install tosca-metrics

or, alternatively from the source code project directory:

pip install -r requirements.txt
pip install .

How to use

Command-line

Run tosca-metrics --help for instructions about the usage:

usage: tosca-metrics [-h] [--omit-zero-metrics] [-d DEST] [-o] [-v] src

Extract metrics from Ansible scripts.

positional arguments:
  src                   source file (Tosca blueprint) or directory

optional arguments:
  -h, --help            show this help message and exit
  --omit-zero-metrics   omit metrics with value equal 0
  -d DEST, --dest DEST  destination path to save results
  -o, --output          shows output
  -v, --version         show program's version number and exit

Assume that the following example is named blueprint1.tosca:

tosca_definitions_version: tosca_simple_yaml_1_0

imports:
  - indigo_custom_types: https://raw.githubusercontent.com/indigo-dc/tosca-types/master/custom_types.yaml

description: >
  TOSCA example for launching the generic_deepaas mesos job
topology_template:


  node_templates:

    marathon-job:
      type: tosca.nodes.indigo.Container.Application.Docker.Marathon
      properties:
        uris: []
        command: 'deepaas-run --listen-ip 0.0.0.0'
        labels:
          HAPROXY_GROUP: external
      artifacts:
        image:
          file: deephdc/deep-oc-plant-classification-theano
          type: tosca.artifacts.Deployment.Image.Container.Docker
      requirements:
        - host: docker_runtime

    docker_runtime:
      type: tosca.nodes.indigo.Container.Runtime.Docker
      capabilities:
        host:
          properties:
            num_cpus: 1.0
            mem_size: 1024 MB
            publish_ports:
               - protocol: tcp
                 source: 5000

  outputs:
    endpoint: 
      value: { concat: [ { get_attribute : [ marathon-job, load_balancer_ips, 0 ] }, ':', { get_attribute : [ docker_runtime, host, publish_ports, 0, target ] } ] }

and is located within the folder blueprints as follows:

blueprints
   |- blueprint1.yml
   |- blueprint2.yml
   |- blueprint3.yml

Also, assume the user's working directory is the blueprints folder. Then, it is possible to extract source code characteristics from that blueprint by running the following command:

tosca-metrics --omit-zero-metrics playbook1.yml --dest report.json

For this example, the report.json will result in

[
    {
        "filepath": "blueprint1.tosca",
        "lines_blank": 7,
        "lines_code": 33,
        "num_imports": 1,
        "num_keys": 35,
        "num_node_templates": 2,
        "num_properties": 6,
        "num_tokens": 68,
        "text_entropy": 5.76
    }
]

Python

AnsibleMetrics currently supports up to 46 source code metrics, implemented in Python. To extract the value for a given metric follow this pattern:

from toscametrics.<general|blueprint>.metric import Metric

script = 'a valid yaml script'
value = Metric(script).count()

where has to be replaced with the name of the desired metric module to compute the value of a specific metric.
The difference between the general and the blueprint modules is that the blueprint module contains metrics specific to blueprints (for example, the number of node types), while the general module contains metrics that can be generalized to other languages (for example, the lines of code).

For example, to count the number of lines of code:

from toscametrics.general.lines_code import LinesCode

blueprint = """
tosca_definitions_version: tosca_simple_yaml_1_0

imports:
  - indigo_custom_types: https://raw.githubusercontent.com/indigo-dc/tosca-types/master/custom_types.yaml

description: >
  TOSCA example for launching the generic_deepaas mesos job
"""

print('Lines of executable code:', LinesCode(blueprint).count())

To extract the value for all the metrics at once, import the toscametrics.metrics_extractor package and call the method extract_all() (in this case the return value will be a json object):

from toscametrics.metrics_extractor import extract_all

blueprint = """
tosca_definitions_version: tosca_simple_yaml_1_0

imports:
  - indigo_custom_types: https://raw.githubusercontent.com/indigo-dc/tosca-types/master/custom_types.yaml

description: >
  TOSCA example for launching the generic_deepaas mesos job
"""

metrics = extract_all(blueprint)
print('Lines of executable code:', metrics['lines_code'])

Metadata

Release files for tosca-metrics 0.0.11

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tosca-metrics 0.0.11
File Size Uploaded Provenance Actions
tosca-metrics-0.0.11.tar.gz 24.0 kB Without provenance Details for tosca-metrics-0.0.11.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for tosca-metrics 0.0.11
File Interpreter ABI Platform Actions
tosca_metrics-0.0.11-py3-none-any.whl Python 3 none any Details for tosca_metrics-0.0.11-py3-none-any.whl

Total release size: 69.0 kB

Release files / tosca-metrics-0.0.11.tar.gz

Download URL tosca-metrics-0.0.11.tar.gz
Size 24.0 kB
Tags Source
SHA-256 checksum
How to use checksums
b5bfc16835779e9b20924d393af2a499d6e1539c2c8d227ca7cd524e3113992f
BLAKE2b-256 checksum
How to use checksums
1e57aa9a529cdfb828fdfbfc9d6371aec477b05374558b07cb96f984a68402b1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.5

Release files / tosca_metrics-0.0.11-py3-none-any.whl

Download URL tosca_metrics-0.0.11-py3-none-any.whl
Size 45.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
67a07595804f3b6da8706143e142704b15a8b61189cf22d00d3758b254f06550
BLAKE2b-256 checksum
How to use checksums
8ad1d32150debc9c991a79b9d4f283d4bc04144159b47503274f2aa7babd97d7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.5

Security for tosca-metrics 0.0.11

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page