示例#1
0
def report_as_html(coverage, directory, exclude_patterns, files_list):
    """
    Write an HTML report on all of the files, plus a summary.
    """

    ### now, output.

    keys = coverage.keys()
    info_dict = {}
    for pyfile in filter_files(keys, exclude_patterns, files_list):

        try:
            fp = open(pyfile, 'rU')
            lines = figleaf.get_lines(fp)
        except KeyboardInterrupt:
            raise
        except IOError:
            logger.error('CANNOT OPEN: %s' % (pyfile,))
            continue
        except Exception, e:
            logger.error('ERROR: file %s, exception %s' % (pyfile, str(e)))
            continue

        #
        # ok, we want to annotate this file.  now annotate file ==> html.
        #

        # initialize
        covered = coverage.get(pyfile, set())

        # rewind
        fp.seek(0)

        # annotate
        output, n_covered, n_lines, percent = annotate_file(fp, lines, covered)

        # summarize
        info_dict[pyfile] = (n_lines, n_covered, percent)

        # write out the file
        html_outfile = make_html_filename(pyfile)
        html_outfile = os.path.join(directory, html_outfile)
        html_outfp = open(html_outfile, 'w')
        
        html_outfp.write('source file: <b>%s</b><br>\n' % (pyfile,))
        html_outfp.write('''

file stats: <b>%d lines, %d executed: %.1f%% covered</b>
<pre>
%s
</pre>

''' % (n_lines, n_covered, percent, "\n".join(output)))
            
        html_outfp.close()

        logger.info('reported on %s' % (pyfile,))
示例#2
0
def main():
    import sys
    import logging
    from optparse import OptionParser
    ###

    usage = "usage: %prog [options] [coverage files ... ]"
    option_parser = OptionParser(usage=usage)

    option_parser.add_option('-x', '--exclude-patterns', action="store",
                             dest="exclude_patterns_file",
        help="file containing regexp patterns of files to exclude from report")

    option_parser.add_option('-f', '--files-list', action="store",
                             dest="files_list",
                             help="file containing filenames to report on")

    option_parser.add_option('-d', '--output-directory', action='store',
                             dest="output_dir",
                             default = "html",
                             help="directory for HTML output")

    option_parser.add_option('-q', '--quiet', action='store_true',
                             dest='quiet',
                             help='Suppress all but error messages')
    
    option_parser.add_option('-D', '--debug', action='store_true',
                             dest='debug',
                             help='Show all debugging messages')

    (options, args) = option_parser.parse_args()

    if options.quiet:
        logging.disable(logging.DEBUG)
        
    if options.debug:
        logger.setLevel(logging.DEBUG)

    ### load/combine

    if not args:
        args = ['.figleaf']

    coverage = {}
    for filename in args:
        logger.debug("loading coverage info from '%s'\n" % (filename,))
        try:
            d = figleaf.read_coverage(filename)
            coverage = figleaf.combine_coverage(coverage, d)
        except IOError:
            logger.error("cannot open filename '%s'\n" % (filename,))

    if not coverage:
        logger.warning('EXITING -- no coverage info!\n')
        sys.exit(-1)

    exclude = []
    if options.exclude_patterns_file:
        exclude = read_exclude_patterns(options.exclude_patterns_file)

    files_list = {}
    if options.files_list:
        files_list = read_files_list(options.files_list)

    ### make directory
    prepare_reportdir(options.output_dir)
    report_as_html(coverage, options.output_dir, exclude, files_list)

    print 'figleaf: HTML output written to %s' % (options.output_dir,)