Source

pycabehtml / pycabehtml.py

#!/usr/bin/env python

#####################################################
# File Name : pycabehtml.py
# Purpose : Parse pymetrics results
# Created By : Jordi Carrillo
# GPLv3 License
# Copyright Jordi Carrillo 2011

# vim: ai ts=4 sts=4 et sw=4
#####################################################

"""This small script parses the output given by the command line program
pymetrics. It is designed to be run on Jenkins Continuous Integration Server.


pymetrics `find pathtofiles -iname "*.py"` > output.txt

pycabehtml parses output.txt and gives an Html file with all those methods and
classes where McCabe index is beyond 10. This Html file can be used by Jenkins
CI Html plugin.

Note: No method in this module goes beyond index 10 ;)
"""

import os
from os.path import dirname, join as pjoin
import sys
from optparse import OptionParser
from pycabe.parser import (parse_complexity,
        ReportStorage,
        DataStorage,
        DataRestorer,
        ComplexityHTML,
        ToHtml,
        get_new_complex_methods,
        parse_cmdline)


def store_data(data, fh):
    data_store = DataStorage(data, fh)
    data_store.save()


class ExitStatus(object):

    def __init__(self, data_diff):
        self._data_diff = data_diff

    def exit(self):
        if self._data_diff:
            # Makes Jenkins fail to raise concern.
            print "Code complexity has increased, please check report"
            sys.exit(-1)


def complex_differentials(data, by_complexity, pickle_data_file,
        pickle_complexity_file):
    """Returns the diffs in complexity between builds.

    :param data: current dict holding complexity data
    :param by_complexity: list of tuples complexity index, module
    :param pickle_data_file: filename pickle previous data
    :param pickle_complexity_file: filename pickle previous by_complexity
    :return: Tuple data, by_complexity diffs.
    """
    data_fh = open(pickle_data_file)
    restorer = DataRestorer(data_fh)
    prev_data = restorer.restore()
    complexity_fh = open(pickle_complexity_file)
    restorer = DataRestorer(complexity_fh)
    prev_by_complexity = restorer.restore()
    data_diff, by_complexity_diff = get_new_complex_methods(data,
            by_complexity, prev_data, prev_by_complexity)
    return data_diff, by_complexity_diff


def report_complexity(options):
    outputfilename = os.path.basename(options.output_graph)
    # get current complexity data
    data, by_complexity = parse_complexity(options.input_file)
    base_dir = dirname(options.past_complexity)
    data_pickle_file = pjoin(base_dir, ".pycabehtml_data.pck")
    complexity_pickle_file = pjoin(base_dir, ".pycabehtml_complexity.pck")
    try:
        # Get the diffs
        data_diff, by_complexity_diff = complex_differentials(data,
                by_complexity, data_pickle_file, complexity_pickle_file)
    except IOError:
        data_diff = {}
        by_complexity_diff = []
    fh_complexity_acc = open(options.past_complexity, 'a')
    store = ReportStorage(data, by_complexity, fh_complexity_acc)
    store.save()
    data_pickle_fh = open(data_pickle_file, 'w')
    complexity_pickle_fh = open(complexity_pickle_file, 'w')
    store_data(data, data_pickle_fh)
    store_data(by_complexity, complexity_pickle_fh)
    from pycabe.complexity_charts import complexity_bar_chart
    complexity_bar_chart(open(options.past_complexity), options.output_graph)
    fh = open(options.output_html, 'w')
    ToHtml(data, by_complexity, data_diff, by_complexity_diff,
            ComplexityHTML(), outputfilename).print_tohtml(fh)
    return data_diff


def main():
    data_diff = report_complexity(parse_cmdline(OptionParser(), sys.argv)[0])
    exit_status = ExitStatus(data_diff)
    exit_status.exit()


if __name__ == '__main__':
    main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.