分析iOS Framework 架构以及大小

608 阅读1分钟
import os
import csv


def searchFrameworks(path):
    path = path.strip()
    if len(path) < 1:
        return ""
    searchPath = "find " + path + " -name *.framework"

    resStr = os.popen(searchPath).read()
    return resStr


def splitFrameworksStr():
    exeOutput = []
    pathArr = []
    fatArchArr = []
    filekbs = []
    path = "xxx_ios-3.0"
    outputStr = searchFrameworks(path)
    splitedArr = outputStr.split("\n")
    if len(splitedArr) <= 0:
        return 0
    for item in splitedArr:
        itemPath = item.strip()
        filePath = itemPath.split("/")[-1]
        fileName = filePath.split(".")[0]
        commandStr = "lipo -info " + itemPath + "/" + fileName
        # print(" %s" % commandStr)
        # 查看架构信息
        commandResult = os.popen(commandStr).read()
        # print("执行结果 %s" % commandResult)
        exeOutput.append(commandResult)
        pathArr.append(itemPath)
        archStr = commandResult.split(":")[-1]
        fatArchArr.append(archStr.strip())

        # 查看framework 文件大小
        file_space_command = "du -sh " + itemPath
        space_result_str = os.popen(file_space_command).read()
        first_slash_index = space_result_str.find("/")
        space_kb = space_result_str[0:first_slash_index]
        # print("%s result = %s" % (space_result_str, space_kb))
        filekbs.append(space_kb.strip())

    output_file(pathArr, fatArchArr, filekbs)



def output_file(paths, archs, filekbs):
    with open("frameworks.csv", "w", newline="") as datacsv:
        field_name = ["path", "arch"]
        writer = csv.writer(datacsv, dialect=("excel"))

        for index, item in enumerate(paths):
            writer.writerow([item, archs[index], filekbs[index]])