Package: tosca-metrics
The static source code measurement tool for ToscaToscaMetricsis 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 installInstallation is made simple by thePyPI repository.
Download the tool and install it with:pip install tosca-metricsor, alternatively from the source code project directory:pip install -r requirements.txt
pip install .How to useCommand-lineRuntosca-metrics --helpfor 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 exitAssume that the following example is namedblueprint1.tosca:tosca_definitions_version:tosca_simple_yaml_1_0imports:-indigo_custom_types:https://raw.githubusercontent.com/indigo-dc/tosca-types/master/custom_types.yamldescription:>TOSCA example for launching the generic_deepaas mesos jobtopology_template:node_templates:marathon-job:type:tosca.nodes.indigo.Container.Application.Docker.Marathonproperties:uris:[]command:'deepaas-run--listen-ip0.0.0.0'labels:HAPROXY_GROUP:externalartifacts:image:file:deephdc/deep-oc-plant-classification-theanotype:tosca.artifacts.Deployment.Image.Container.Dockerrequirements:-host:docker_runtimedocker_runtime:type:tosca.nodes.indigo.Container.Runtime.Dockercapabilities:host:properties:num_cpus:1.0mem_size:1024 MBpublish_ports:-protocol:tcpsource:5000outputs: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 folderblueprintsas follows:blueprints|- blueprint1.yml|- blueprint2.yml|- blueprint3.ymlAlso, assume the user's working directory is theblueprintsfolder.
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.jsonFor this example, thereport.jsonwill 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
    }
]PythonAnsibleMetricscurrently supports up to 46 source code metrics, implemented in Python.
To extract the value for a given metric follow this pattern:fromtoscametrics.<general|blueprint>.metricimportMetricscript='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 thegeneraland theblueprintmodules is that theblueprintmodule contains metrics specific
to blueprints (for example, the number of node types), while thegeneralmodule 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:fromtoscametrics.general.lines_codeimportLinesCodeblueprint="""tosca_definitions_version: tosca_simple_yaml_1_0imports:- indigo_custom_types: https://raw.githubusercontent.com/indigo-dc/tosca-types/master/custom_types.yamldescription: >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 thetoscametrics.metrics_extractorpackage and call the
methodextract_all()(in this case the return value will be a json object):fromtoscametrics.metrics_extractorimportextract_allblueprint="""tosca_definitions_version: tosca_simple_yaml_1_0imports:- indigo_custom_types: https://raw.githubusercontent.com/indigo-dc/tosca-types/master/custom_types.yamldescription: >TOSCA example for launching the generic_deepaas mesos job"""metrics=extract_all(blueprint)print('Lines of executable code:',metrics['lines_code'])