美文网首页
iOS 删除工程无效的图片,gif, mp3

iOS 删除工程无效的图片,gif, mp3

作者: Maj_sunshine | 来源:发表于2020-12-15 14:34 被阅读0次

通过脚本遍历查找,删除 (脚本根据自己的工程编写的,有问题修改)

import os

meariPath = "/Users/maj/Desktop/cloudedge-arenti"
xcodeprojPath = "/Users/maj/Desktop/cloudedge-arenti/Meari.xcodeproj/project.pbxproj"

# 文件做递归, 拿到有用的.h, .m 文件
path1 = []
for i in os.listdir(meariPath):
    tempPath1 = meariPath + "/" + i
    if "." not in os.path.splitext(i)[0] and "SDK" not in os.path.splitext(i)[0] or "pch" in os.path.splitext(i)[0] or "lock" in os.path.splitext(i)[0]:
        path1.append(tempPath1)

buildFileList = []  # 所有的.h .m xib, storyboard文件
imageFileList = [] # 图片文件列表
mp3FileList = [] # mp3文件列表
gifFileList = [] # gif文件列表
ignoreList = ["img_camera_connect_wifi_strn", "img_camera_connect_wifi_goclever"]; # 忽略的图片列表

# 获取buildFileList 和 imageFileList列表
def findLastFilePathList(file: list):
    for i in file:
        if os.path.isfile(i):
            if "Pods" in i:
                return ;
            if ".h" == os.path.splitext(i)[1] or ".m" == os.path.splitext(i)[1] or ".xib" == os.path.splitext(i)[1] or ".storyboard" == os.path.splitext(i)[1] or ".pch" == os.path.splitext(i)[1]:
                # if (os.path.splitext(i)[0].split("/")[-1].startswith(oldKey)) :
                buildFileList.append(i)
            if ".png" == os.path.splitext(i)[1] or ".jpg" == os.path.splitext(i)[1]:
                # APP.xcassets 文件不被删除
                if "APP.xcassets" not in i:
                    imageFileList.append(i)
            if ".mp3" == os.path.splitext(i)[1]:
                # APP.xcassets 文件不被删除
                if "APP.xcassets" not in i:
                    mp3FileList.append(i)
            if ".gif" == os.path.splitext(i)[1]:
                # APP.xcassets 文件不被删除
                if "APP.xcassets" not in i:
                    gifFileList.append(i)
        else:
            tempPath = []
            for j in os.listdir(i):
                tempPath.append(i + "/" + j)
            findLastFilePathList(tempPath)

findLastFilePathList(path1)

# 获取引用文件内容
with open(xcodeprojPath, "r") as file:
    lines = file.readlines()
    allContents = "".join(lines)

# 如果.h .m .xib ... 不在引用文件内, 则该文件为无效文件
for str in buildFileList:
    baseName = os.path.basename(str)
    if baseName not in allContents:
        # print(baseName)
        buildFileList.remove(str)

# 打印一下
# for i in buildFileList:
#     print(i)

# 删除忽略图片的内容
tempList = imageFileList[:]
for ignoreImage in ignoreList:
    for i in tempList:
        if ignoreImage in i :
            imageFileList.remove(i);

# 设置默认是否使用为False
imageExistDic = {};
mp3ExistDic = {};
gifExistDic = {};
for imageStr in imageFileList:
    imageExistDic[imageStr] = False;
for mp3Str in mp3FileList:
    mp3ExistDic[mp3Str] = False;
for gifStr in gifFileList:
    gifExistDic[gifStr] = False;

# 遍历文件, 更新图片使用
for buildFile in buildFileList:
    # print(buildFile)
    with open(buildFile, "r") as file:
         # 获取文件的代码每一
        lineList = file.readlines();
        fileContent = "".join(lineList);

        for imageStr in imageFileList:
            imageName = os.path.basename(imageStr).split(".")[0].split("@")[0];
            # print(imageStr)
            if imageName in fileContent:
                imageExistDic[imageStr] = True;

        for mp3Str in mp3FileList:
            last = os.path.basename(mp3Str).split("_")[-1];
            mp3Name = os.path.basename(mp3Str).replace("_" + last, "");
            if mp3Name in fileContent:
                mp3ExistDic[mp3Str] = True;

        for gifStr in gifFileList:
            gifName = os.path.basename(gifStr).split(".")[0]
            if gifName in fileContent:
                    gifExistDic[gifStr] = True;


# # 打印一下
for (key, value) in gifExistDic.items():
    # print(key, value)
    if(value == False):
       print(key)

# 删除无效资源
unablePath = []
for (key, value) in imageExistDic.items():
    if(value == False):
        if os.path.exists(key):
            unablePath.append(os.path.basename(key))
            os.remove(key)

for (key, value) in mp3ExistDic.items():
    if(value == False):
        if os.path.exists(key):
            unablePath.append(os.path.basename(key))
            os.remove(key)

for (key, value) in gifExistDic.items():
    if(value == False):
        if os.path.exists(key):
            unablePath.append(os.path.basename(key))
            os.remove(key)

# 获取引用文件内容
with open(xcodeprojPath, "r") as file:
    lines = file.readlines()
newContent = lines[:]
for (index, line) in enumerate(newContent):
    for name in unablePath:
        if name in line:
            print(name, line)
            lines.remove(line)
with open(xcodeprojPath, "w") as file:
    file.write("".join(lines))


# 文件夹如果没有图片, 删除图片文件夹
for key in imageExistDic.keys():
    dirpath = os.path.dirname(key);
    if os.path.exists(dirpath):
        file_list = os.listdir(dirpath)
        if len(file_list) == 1:
            if file_list[0] == "Contents.json":
                os.remove(dirpath + "/" + "Contents.json")
                os.removedirs(dirpath)

相关文章

网友评论

      本文标题:iOS 删除工程无效的图片,gif, mp3

      本文链接:https://www.haomeiwen.com/subject/ttrwgktx.html