python自动化-日志处理(mac电脑)

1.判断文件夹是否存在

def isExists(folder):
    if os.path.exists(folder):  # 判断文件夹是否存在
        pass
    else:
        os.system('mkdir -pv '+folder)  # 创建文件夹以及子文件夹

2.获取iOS手机日志(udid为iOS手机)

def open_log(log_name):
    '''获取iOS手机日志'''
    times = time.strftime("%Y-%m-%d_%H_%M_%S", time.localtime())    # 获取时间
    log_path = path     # 获取文件夹路径
    filename = log_name + times + ".log"    # 定义文件名称及格式
    isExists(log_path)  # 判断文件夹是否存在
    logs = os.path.join(log_path, filename)     # 拼接在一起
    print(r"idevicesyslog >> %s &" % logs)  # 输出看一下有没有问题
    os.system(r"idevicesyslog -u " + udid + r" >> %s &" % logs)     # 执行命令行

def kill_log():
    '''杀进程(停止日志获取)'''
    os.system("pkill idevicesyslog")

3.挂载沙盒(ifuse)

def install_Sandbox():
    isExists(log_path)
    # 创建文件夹   mkdir -pv /Users/ceshi/samdbox/Xiaomi/myapp_log
    sandbox = 'ifuse --documents '+packname+' '+log_path
    os.system(sandbox)
    # 挂在沙河    ifuse --documents com.wondertek.hecmccmobile.prd.demo.222 /Users/ceshi/samdbox/Xiaomi/myapp_log
    rm_log = 'rm '+log_path+'/MGHealthNSLog/性能数据日志.txt'
    time.sleep(10)
    os.system(rm_log)
    # 删除日志    rm /Users/ceshi/samdbox/Xiaomi/myapp_log/MGHealthNSLog/性能数据日志.txt


def cp_box_log():
    file1 = log_path + '/MGHealthNSLog/性能数据日志.txt'
    file2 = path + '性能数据日志2.txt'
    cp_log ='cp ' + file1 + ' ' + file2
    os.system(cp_log)
    # 复制日志到本地 cp /Users/ceshi/Dqqqwww1/Xiaomi1/myapp221/MGHealthNSLog/性能数据日志.txt /Users/ceshi/Dqqqwww1/性能数据日志.txt


def uninstall_Sandbox():
    un_box = 'umount '+log_path
    os.system(un_box)
    # 卸载挂载    umount /Users/ceshi/samdbox/Xiaomi/myapp_log

4.获取操作并写入日志

def action_log(action):
    # 获取操作并写入日志
    action_path = path+'actionlog'
    isExists(action_path)
    action_path = action_path+'/iOS操作日志.txt'
    current_time = int(time.time())
    localtime = time.localtime(current_time)
    dt = time.strftime('%Y-%m-%d %H:%M:%S', localtime)
    dq = dt + ',' + action
    f = open(action_path, "a")
    f.write(str(dq) + '\n')
    f.close()


def de_action_log():
    # 删除操作日志
    action_path = path+'actionlog'
    action_path = action_path+'/iOS操作日志.txt'
    os.system('rm '+action_path)

5.日志处理成excel


def processing_log():
    time1 = []
    Memory_Size = []
    FPS = []
    CPU = []
    Descriptor = []

    path = r'/Users/ceshi/PycharmProjects/mgsp_iOS_quality_two/log/性能数据日志2.txt'
    with open(path, encoding='utf-8') as file:
        content = file.read()
        ###逐行读取数据
        content1 = content.split('\n')
        print(len(content1))
    for line in content1[0:(len(content1)-1)]:
        line2 = line.split(',')
        print(line2)
        print(type(line2))
        time1.append(line2[0])

        l1 = line2[1].split(':')
        Memory_Size.append(l1[1].strip(' '))

        l3 = line2[3].split(':')
        FPS.append(l3[1].strip(' '))

        l4 = line2[4].split(':')
        CPU.append(l4[1].strip(' '))

        l5 = line2[5].split(':')
        Descriptor.append(l5[1].strip(' '))

    print(time1)
    print(len(time1))
    print(Memory_Size)
    print(len(Memory_Size))
    print(FPS)
    print(len(FPS))
    print(CPU)
    print(len(CPU))
    print(Descriptor)
    print(len(Descriptor))
    # 读取性能日志数据完成

    xlsxname_path = r'/Users/ceshi/PycharmProjects/mgsp_iOS_quality_two/log/性能数据日志2.xls'
    workbook = xlsxwriter.Workbook(xlsxname_path)
    sheet = workbook.add_worksheet('DATA')
    sheet_pic = workbook.add_worksheet('pic')
    sheet_tmp = workbook.add_worksheet('tmp_data')
    title = ['time', 'Memory_Size', 'FPS', 'CPU', 'Descriptor']
    j = 0
    for i in title:
        sheet.write(0, j, i)
        j += 1

    j = 1
    for i in time1:
        sheet.write(j, 0, i)
        j += 1

    j = 1
    for i in Memory_Size:
        sheet.write(j, 1, i)
        j += 1

    j = 1
    for i in FPS:
        sheet.write(j, 2, i)
        j += 1

    j = 1
    for i in CPU:
        sheet.write(j, 3, i)
        j += 1

    j = 1
    for i in Descriptor:
        sheet.write(j, 4, i)
        j += 1

    # 在DATA中写入完成


    path2 = r'/Users/ceshi/PycharmProjects/mgsp_iOS_quality_two/log/actionlog/iOS操作日志.txt'
    with open(path2, encoding='utf-8') as file:
        content = file.read()
        ###逐行读取数据
        content1 = content.split('\n')
        print(len(content1))

    time2 = []
    action = []

    for line in content1[0:(len(content1)-1)]:
        line2 = line.split(',')
        print(line2)
        time2.append(line2[0])
        action.append(line2[1])

    print(time2)
    print(len(time2))
    print(action)
    print(len(action))

    title = ['time', 'Memory_Size', 'FPS', 'CPU(%)', 'Descriptor']

    j = 0
    for i in title:
        sheet_tmp.write(0, j, i)
        j += 1

    i = 1
    z = 0
    for x in time2:
        for y in time1:
            if x == y:
                j = time1.index(y)
                w1 = time1[j][11:16]
                w2 = str(w1) + "(" + str(action[z]) + ')'
                sheet_tmp.write(i, 0, w2)
                sheet_tmp.write(i, 1, int(Memory_Size[j]))
                sheet_tmp.write(i, 2, int(FPS[j]))
                sheet_tmp.write(i, 3, int(CPU[j]))
                sheet_tmp.write(i, 4, int(Descriptor[j]))
                i += 1
                break
        z += 1

    # 向tmp_data写数据

    # 插入性能分析折线图
    print("开始插入性能折线图...")
    chart = workbook.add_chart({'type': 'line'})
    maxcount = i+1


    chart.add_series({
        'name':       '=tmp_data!$B$1',
        'categories': '=tmp_data!$A$2:$A$%d'%int(maxcount),
        'values':     '=tmp_data!$B$2:$B$%d'%int(maxcount)
    })

    chart.add_series({
        'name':       '=tmp_data!$C$1',
        'categories': '=tmp_data!$A$2:$A$%d'%int(maxcount),
        'values':     '=tmp_data!$C$2:$C$%d'%int(maxcount)
    })

    chart.add_series({
        'name':       '=tmp_data!$D$1',
        'categories': '=tmp_data!$A$2:$A$%d'%int(maxcount),
        'values':     '=tmp_data!$D$2:$D$%d'%int(maxcount)
    })

    chart.add_series({
        'name':       '=tmp_data!$E$1',
        'categories': '=tmp_data!$A$2:$A$%d'%int(maxcount),
        'values':     '=tmp_data!$E$2:$E$%d'%int(maxcount)
    })


    # 设置图表的title 和 x,y轴信息
    chart.set_title({'name': '性能数据统计'})
    chart.set_x_axis({'name': 'time'})

    # 设置图表显示
    sheet_pic.insert_chart('C4', chart, {'x_scale': 5.25, 'y_scale': 1.8})

    workbook.close()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值