<commit_before>from openpyxl import Workbook
from openpyxl.styles import Border, Side, Font, Alignment, NamedStyle
import os

class MonkeyLog(object):

    log_path = "/home/blue/python/AlgorithmPython/Files/25179；25187/monkeysys.log"
    excel_path = "/home/blue/python/AlgorithmPython/Files/log.xlsx"

    info_total = []
    info_final = []

    def __init__(self):
        if os.path.exists(self.excel_path):
            os.remove(self.excel_path)

    def get_info(self):
        name = []
        detail = []
        with open(self.log_path, 'r', encoding='utf-8') as data:
            for x in data:
                if x.startswith('// CRASH: '):
                    name.append(x.split(' ')[2])
                if x.startswith('// Long Msg:'):
                    detail.append(' '.join(x.split(' ')[3:]).replace('\n', ''))
        if len(name) != len(detail):
            print("Wrong message !!!!!!!")

        for i in range(len(name)):
            temp = [name[i], detail[i]]
            self.info_total.append(temp)

    def analyze_info(self):
        self.info_total.sort(key=lambda x: x[0])
        each_bug_num = self.count_bug(self.info_total)
        each_app_num = self.count_app(self.info_total)
        for i in range(len(each_app_num)):
            self.info_total[i].insert(0, each_app_num[i])
        for i in range(len(each_bug_num)):
            self.info_total[i].append(each_bug_num[i])
        for x in self.info_total:
            if x not in self.info_final:
                self.info_final.append(x)

    def write_excel(self):
        wb = Workbook()
        ws = wb.active
        ws.title = "monkey_log"
        ws.append(['总数', '进程名', '错误信息', '复现次数'])

        merge_line_num = self.count_merge(self.info_final)
        length = len(merge_line_num)
        for i in range(length):
            if length == 1:
                ws.merge_cells('A%s:A%s' % (2, merge_line_num[-1]))
                ws.merge_cells('B%s:B%s' % (2, merge_line_num[-1]))
            elif i < length - 1:
                if merge_line_num[i + 1] - merge_line_num[i] != 1:
                    ws.merge_cells('A%s:A%s' % (merge_line_num[i] + 1, merge_line_num[i + 1]))
                    ws.merge_cells('B%s:B%s' % (merge_line_num[i] + 1, merge_line_num[i + 1]))

        for x in self.info_final:
            ws.append(x)
        self.format_excel(ws)
        wb.save(self.excel_path)

    def count_app(self, info):
        temp_name = []
        temp_num = []
        for x in info:
            temp_name.append(x[0])
        for x in temp_name:
            temp_num.append(temp_name.count(x))
        return temp_num

    def count_bug(self, info):
        temp_num = []
        for x in info:
            temp_num.append(self.info_total.count(x))
        return temp_num

    def count_merge(self, info):
        temp_num = []
        length = len(info)
        for i in range(length):
            if i < length - 1:
                if info[i][1] != info[i + 1][1]:
                    temp_num.append(i + 2)
            else:
                temp_num.append(length + 1)
        return temp_num

    def format_excel(self, ws):
        ws.column_dimensions['A'].width = 8
        ws.column_dimensions['B'].width = 30
        ws.column_dimensions['C'].width = 100
        ws.column_dimensions['D'].width = 10
        for i in range(ws.max_row):
            ws.row_dimensions[i + 1].height = 30
        left, right, top, bottom = [Side(style='thin', color='000000')] * 4
        title = NamedStyle(name="title")
        title.font = Font(name=u'宋体', size=11)
        title.alignment = Alignment(horizontal='center', vertical='center', wrap_text=True)
        title.border = Border(left=left, right=right, top=top, bottom=bottom)
        content = NamedStyle(name="content")
        content.font = Font(name=u'宋体', size=11)
        content.alignment = Alignment(horizontal='center', vertical='center', wrap_text=True)
        content.border = Border(left=left, right=right, top=top, bottom=bottom)
        content_long = NamedStyle(name="content_long")
        content_long.font = Font(name=u'宋体', size=11)
        content_long.border = Border(left=left, right=right, top=top, bottom=bottom)
        content_long.alignment = Alignment(horizontal='left', vertical='center', wrap_text=True)
        for x in ws[1]:
            x.style = title
        for x in ws['A:B']:
            for y in x:
                y.style = content
        for x in ws['C'][1:]:
            x.style = content_long
        for x in ws['D'][1:]:
            x.style = content

if __name__ == '__main__':
    log = MonkeyLog()
    log.get_info()
    log.analyze_info()
    log.write_excel()<commit_msg>update log.py, add anr log<commit_after>from openpyxl import Workbook
from openpyxl.styles import Border, Side, Font, Alignment, NamedStyle
import os


class MonkeyLog(object):

    log_path = "E:\worktest\python\PythonExercise\Files\monkeysys.log"
    excel_path = "E:\worktest\python\PythonExercise\Files\log.xlsx"

    info_total = []
    info_final = []

    def __init__(self):
        if os.path.exists(self.excel_path):
            os.remove(self.excel_path)


    def get_info(self, app_name, message, index):
        name = []
        detail = []
        with open(self.log_path, 'r', encoding='utf-8') as data:
            for x in data:
                if x.startswith(app_name):
                    name.append(x.split(' ')[2].replace('\n', ''))
                if x.startswith(message):
                    detail.append(' '.join(x.split(' ')[index:]).replace('\n', ''))
        if len(name) != len(detail):
            print("Wrong message !!!!!!!")
        for i in range(len(name)):
            temp = [name[i], detail[i]]
            self.info_total.append(temp)

    def analyze_info(self):
        self.info_total.sort(key=lambda x: x[0])
        each_bug_num = self.count_bug(self.info_total)
        each_app_num = self.count_app(self.info_total)
        for i in range(len(each_app_num)):
            self.info_total[i].insert(0, each_app_num[i])
        for i in range(len(each_bug_num)):
            self.info_total[i].append(each_bug_num[i])
        for x in self.info_total:
            if x not in self.info_final:
                self.info_final.append(x)

    def write_excel(self, ws, title_style, content_style, content_long_style):
        ws.append(['总数', '进程名', '错误信息', '复现次数'])
        merge_line_num = self.count_merge(self.info_final)
        length = len(merge_line_num)
        for i in range(length):
            if length == 1:
                ws.merge_cells('A%s:A%s' % (2, merge_line_num[-1]))
                ws.merge_cells('B%s:B%s' % (2, merge_line_num[-1]))
            elif i < length - 1:
                if merge_line_num[i + 1] - merge_line_num[i] != 1:
                    ws.merge_cells('A%s:A%s' % (merge_line_num[i] + 1, merge_line_num[i + 1]))
                    ws.merge_cells('B%s:B%s' % (merge_line_num[i] + 1, merge_line_num[i + 1]))
        for x in self.info_final:
            ws.append(x)
        self.format_excel(ws, title_style, content_style, content_long_style)
        wb.save(self.excel_path)
        self.info_final.clear()
        self.info_total.clear()

    def count_app(self, info):
        temp_name = []
        temp_num = []
        for x in info:
            temp_name.append(x[0])
        for x in temp_name:
            temp_num.append(temp_name.count(x))
        return temp_num

    def count_bug(self, info):
        temp_num = []
        for x in info:
            temp_num.append(self.info_total.count(x))
        return temp_num

    def count_merge(self, info):
        temp_num = []
        length = len(info)
        for i in range(length):
            if i < length - 1:
                if info[i][1] != info[i + 1][1]:
                    temp_num.append(i + 2)
            else:
                temp_num.append(length + 1)
        return temp_num

    def format_excel(self, ws, title_style, content_style, content_long_style):
        ws.column_dimensions['A'].width = 8
        ws.column_dimensions['B'].width = 30
        ws.column_dimensions['C'].width = 100
        ws.column_dimensions['D'].width = 10
        for i in range(ws.max_row):
            ws.row_dimensions[i + 1].height = 30
        for x in ws[1]:
            x.style = title_style
        for x in ws['A:B']:
            for y in x:
                y.style = content_style
        for x in ws['C'][1:]:
            x.style = content_long_style
        for x in ws['D'][1:]:
            x.style = content_style

if __name__ == '__main__':
    crash = MonkeyLog()
    anr = MonkeyLog()
    wb = Workbook()

    left, right, top, bottom = [Side(style='thin', color='000000')] * 4
    title = NamedStyle(name="title")
    title.font = Font(name=u'宋体', size=11)
    title.alignment = Alignment(horizontal='center', vertical='center', wrap_text=True)
    title.border = Border(left=left, right=right, top=top, bottom=bottom)
    content = NamedStyle(name="content")
    content.font = Font(name=u'宋体', size=11)
    content.alignment = Alignment(horizontal='center', vertical='center', wrap_text=True)
    content.border = Border(left=left, right=right, top=top, bottom=bottom)
    content_long = NamedStyle(name="content_long")
    content_long.font = Font(name=u'宋体', size=11)
    content_long.border = Border(left=left, right=right, top=top, bottom=bottom)
    content_long.alignment = Alignment(horizontal='left', vertical='center', wrap_text=True)

    crash.get_info('// CRASH: ', '// Long Msg:', 3)
    crash.analyze_info()
    ws_crash = wb.active
    ws_crash.title = "crash"
    crash.write_excel(ws_crash, title, content, content_long)

    anr.get_info('ANR in', 'Reason:', 1)
    anr.analyze_info()
    ws_anr = wb.create_sheet("anr")
    anr.write_excel(ws_anr, title, content, content_long)

{'fraction_non_alphanumeric': 0.13853648634933524, 'fraction_numerical': 0.011468588247234344, 'mean_word_length': 2.2329396325459316, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '3548088', 'n_tokens_mistral': 3473, 'n_tokens_neox': 3302, 'n_words': 649}