<commit_before>""" report test results in HTML format
author: <NAME>
Based on initial code from <NAME> and mozilla selenium report generating.
"""

import pkg_resources
import os
import time
import datetime
import cgi
import shutil
import re

import py

from py.xml import html
from py.xml import raw


def mangle_testnames(names):
    names = [x.replace(".py", "") for x in names if x != '()']
    names[0] = names[0].replace("/", '.')
    #print("names %s" %names)
    return names


class LogHTML(object):
    def __init__(self, logfile, prefix):
        logfile = os.path.expanduser(os.path.expandvars(logfile))
        self.logfile = os.path.normpath(os.path.abspath(logfile))
        # TODO will not work if shishito runner is not used
        self.project_root = os.getcwd()
        self.screenshot_path = os.path.join(os.path.dirname(self.logfile), 'screenshots')
        self.debug_event_path = os.path.join(os.path.dirname(self.logfile), 'debug_events')
        self.used_screens = []
        self.used_debug_events = []
        self.prefix = prefix
        self.tests = []
        self.passed = self.skipped = 0
        self.failed = self.errors = 0
        self.test_logs = []
        self.errors = self.failed = 0
        self.passed = self.skipped = 0
        self.xfailed = self.xpassed = 0
        self.resources = ('style.css', 'jquery.js', 'main.js')

    def _make_report_dir(self):
        logfile_dirname = os.path.dirname(self.logfile)
        if logfile_dirname and not os.path.exists(logfile_dirname):
            os.makedirs(logfile_dirname)
        # copy across the static resources
        for file in self.resources:
            shutil.copyfile(
                pkg_resources.resource_filename(
                    __name__, os.path.sep.join(['resources', file])),
                os.path.abspath(os.path.join(logfile_dirname, file)))
        return logfile_dirname

    def _write_captured_output(self, report):
        sec = dict(report.sections)
        output = ""
        for name in ('out', 'err'):
            content = sec.get("Captured std%s setup" % name)
            if content:
                output = output + content
        return output

    def append_screenshot(self, name, log):
        name = re.sub('[^A-Za-z0-9_.]+', '_', name)
        if not os.path.exists(self.screenshot_path):
            os.makedirs(self.screenshot_path)
        source = os.path.join(self.project_root, 'screenshots', name + '.png')
        self.used_screens.append(source)
        log.append(html.img(src='screenshots/' + name + '.png'))

    def append_link_to_debug_event(self, name, log):
        name = re.sub('[^A-Za-z0-9_. ]+', '', name)
        if not os.path.exists(self.debug_event_path):
            os.makedirs(self.debug_event_path)
        source = os.path.join(self.project_root, 'debug_events', name + '.json')
        self.used_debug_events.append(source)
        log.append(html.h3('DebugEvent log'))
        log.append(html.a(name + '.json', href='debug_events/' + name + '.json'))

    def process_screenshot_files(self):
        for screen in self.used_screens:<commit_msg>Update report processing debug events<commit_after>""" report test results in HTML format
author: <NAME>
Based on initial code from <NAME> and mozilla selenium report generating.
"""

import pkg_resources
import os
import time
import datetime
import cgi
import shutil
import re

import py

from py.xml import html
from py.xml import raw


def mangle_testnames(names):
    names = [x.replace(".py", "") for x in names if x != '()']
    names[0] = names[0].replace("/", '.')
    #print("names %s" %names)
    return names


class LogHTML(object):
    def __init__(self, logfile, prefix):
        logfile = os.path.expanduser(os.path.expandvars(logfile))
        self.logfile = os.path.normpath(os.path.abspath(logfile))
        # TODO will not work if shishito runner is not used
        self.project_root = os.getcwd()
        self.screenshot_path = os.path.join(os.path.dirname(self.logfile), 'screenshots')
        self.debug_event_path = os.path.join(os.path.dirname(self.logfile), 'debug_events')
        self.used_screens = []
        self.used_debug_events = []
        self.prefix = prefix
        self.tests = []
        self.passed = self.skipped = 0
        self.failed = self.errors = 0
        self.test_logs = []
        self.errors = self.failed = 0
        self.passed = self.skipped = 0
        self.xfailed = self.xpassed = 0
        self.resources = ('style.css', 'jquery.js', 'main.js')

    def _make_report_dir(self):
        logfile_dirname = os.path.dirname(self.logfile)
        if logfile_dirname and not os.path.exists(logfile_dirname):
            os.makedirs(logfile_dirname)
        # copy across the static resources
        for file in self.resources:
            shutil.copyfile(
                pkg_resources.resource_filename(
                    __name__, os.path.sep.join(['resources', file])),
                os.path.abspath(os.path.join(logfile_dirname, file)))
        return logfile_dirname

    def _write_captured_output(self, report):
        sec = dict(report.sections)
        output = ""
        for name in ('out', 'err'):
            content = sec.get("Captured std%s setup" % name)
            if content:
                output = output + content
        return output

    def append_screenshot(self, name, log):
        name = re.sub('[^A-Za-z0-9_.]+', '_', name)
        if not os.path.exists(self.screenshot_path):
            os.makedirs(self.screenshot_path)
        source = os.path.join(self.project_root, 'screenshots', name + '.png')
        self.used_screens.append(source)
        log.append(html.img(src='screenshots/' + name + '.png'))

    def append_link_to_debug_event(self, name, log):
        name = re.sub('[^A-Za-z0-9_.]+', '_', name)
        if not os.path.exists(self.debug_event_path):
            os.makedirs(self.debug_event_path)
        source = os.path.join(self.project_root, 'debug_events', name + '.json')
        self.used_debug_events.append(source)
        log.append(html.h3('DebugEvent log'))
        log.append(html.a(name + '.json', href='debug_events/' + name + '.json'))

    def process_screenshot_files(self):
        for screen in self.used_screens:
{'fraction_non_alphanumeric': 0.12807409773235387, 'fraction_numerical': 0.0038326413286489938, 'mean_word_length': 2.5585227272727273, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '904436', 'n_tokens_mistral': 2015, 'n_tokens_neox': 1952, 'n_words': 433}