欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

删除无用文件脚本

程序员文章站 2023-11-01 13:58:46
import os# 赵鹏 删除项目中无用文件 有风险,例如#mContext.getResources().getIdentifier("rooms_third_guard_diamond_" + bean.getGuardLevel(), "drawable", mContext.getPackageName()) 过滤掉这种情况# style.xml中的情况未处理,需手动处理rootPath = "D:\code\Shiliu_sixrooms_main"javaDirSet =....
import os

# 赵鹏  删除项目中无用文件  有风险,例如
#mContext.getResources().getIdentifier("rooms_third_guard_diamond_" + bean.getGuardLevel(), "drawable", mContext.getPackageName())  过滤掉这种情况
# style.xml中的情况未处理,需手动处理
rootPath = "D:\code\Shiliu_sixrooms_main"

javaDirSet = {""}#java目录的集合
resDirSet = {""}#res目录的集合
javaFileSet = {""}#.java   .kt  文件的集合
resFileSet = {""}#drawable  layout 目录下文件的集合
styleFileSet = {""}
def getFolderPath(rootPath):

    if isinstance(rootPath, str):
        if os.path.isdir(rootPath):
            dirList = os.listdir(rootPath)
            for path in dirList:
                totalPath = os.path.join(rootPath, path)
                if os.path.isdir(totalPath):

                    if "build" in path:
                        continue

                    if "java" == path:
                        # print("java -- folder -- " + totalPath)
                        javaDirSet.add(totalPath)
                        continue

                    if "res" == path:
                        # print("res -- folder -- " + totalPath)
                        resDirSet.add(totalPath)
                        continue

                    if os.path.isdir(totalPath):
                        getFolderPath(totalPath)


def getFilePath(folderPath):
    if isinstance(folderPath, str):
        if os.path.isdir(folderPath):
            pathList = os.listdir(folderPath)
            for path in pathList:
                getFilePath(os.path.join(folderPath, path))

        elif os.path.isfile(folderPath):
            # print(folderPath )
            if ".java" in folderPath or ".kt" in folderPath:
                # print(folderPath)
                javaFileSet.add(folderPath)

            if "drawable" in folderPath or "layout" in folderPath:
                resFileSet.add(folderPath)

            if "styles.xml" in folderPath:
                styleFileSet.add(folderPath)

getFolderPath(rootPath)

print("javaDirSet -- " + str(len(javaDirSet)))
for path in javaDirSet:
    getFilePath(path)

print("resDirSet -- " + str(len(resDirSet)))
for path in resDirSet:
    getFilePath(path)

print("resFileSet -- " + str(len(resFileSet)))
print("javaFileSet -- " + str(len(javaFileSet)))
# for path in javaFileSet:
#     print(path)
totalNum = len(resFileSet)
curNum = 0;
for resFilePath in resFileSet:
    curNum  = curNum + 1
    print("剩余条数 ------------ " + str(totalNum - curNum))
    if os.path.exists(resFilePath):
        # print("resFilePath -- " + resFilePath)
        name = os.path.basename(resFilePath)
        name = name[:name.index(".")]#截取文件名
        isFileUsed = False;
        # print(name)
        # print("javaFileSet -- " + str(len(javaFileSet)))
        # 查询java和kotlin文件是否使用
        for path in javaFileSet:
            # print("path -- " + path)
            if len(path) > 0:
                # print("path -- " + path)
                if os.path.exists(path):
                    try:
                        with open(path, encoding= 'utf-8') as foo:
                            for line in foo.readlines():
                                if name in line:
                                    isFileUsed = True
                                    break
                    except FileNotFoundError as e:
                        e.filename

            if isFileUsed:
                break

        # 查询drawable 和layout 中的.xml文件是否使用
        if isFileUsed == False:
            for resPath in resFileSet:
                if len(resPath) > 0 and ".xml" in resPath:
                    # print("path -- " + path)
                    if os.path.exists(resPath):
                        try:
                            with open(resPath, encoding= 'utf-8') as foo:
                                for line in foo.readlines():
                                    if name in line:
                                        isFileUsed = True
                                        break
                        except FileNotFoundError as e:
                            e.filename

                if isFileUsed:
                    break

        #查询styles.xml文件是否使用
        if isFileUsed == False:
            for stylePath in styleFileSet:
                if len(stylePath) > 0 and ".xml" in stylePath:
                    # print("path -- " + path)
                    if os.path.exists(stylePath):
                        try:
                            with open(stylePath, encoding= 'utf-8') as foo:
                                for line in foo.readlines():
                                    if name in line:
                                        isFileUsed = True
                                        break
                        except FileNotFoundError as e:
                            e.filename

                if isFileUsed:
                    break


        if isFileUsed == False:
            print("remove -- " + resFilePath)
            try:
                # mContext.getResources().getIdentifier("rooms_third_guard_diamond_" + bean.getGuardLevel(), "drawable", mContext.getPackageName())  过滤掉这种情况
                name = os.path.basename(resFilePath)
                name = name[:name.index(".")]  # 截取文件名
                numStr = name[name.rindex("_") + 1:]
                # print("numstr ------- " + numStr)
                if len(numStr) > 0  and numStr.isdigit():
                    print("------------ 不删除 ----------------")
                else:
                    print("------------ 删除 ----------------")
                    os.remove(resFilePath)
            except ValueError as e:
                e.args



本文地址:https://blog.csdn.net/qingjiaowodiyiming/article/details/107367047