美文网首页ARN-MJiOS 收藏篇
iOS批量修改类和类别的名字

iOS批量修改类和类别的名字

作者: T_aa | 来源:发表于2019-04-24 16:19 被阅读0次

最近在做马甲包
真的难上
现在是 修改项目类文件名称的python代码
复制保存成 xxx.py文件 放到工程目录下

# -*- coding: utf-8 -*-
import os
import time
import random

print "\n=======================================================\n"

#xiugai leiming
def replaceName( dirpath, oldName, newName ):
    oldFile = os.path.join(dirpath,filename)
    newFile = os.path.join(dirpath,filename.replace(oldName,newName,1))
    print newFile
    inFile = open(oldFile)
    outFile = open(newFile, 'w')
    replacements = {oldName:newName}
    for line in inFile:
        for src, target in replacements.iteritems():
            line = line.replace(src, target)
        outFile.write(line)
    inFile.close()
    outFile.close()
    os.remove(oldFile)
    

pathHead = './YYBuYu'#工程下 文件的主目录
str1 = 'TTBYDJ'#要修改以这个开头类 或者类别
str2 = 'ZBSYDJ'#替换成

#类名替换
for dirpath, _, filenames in os.walk('.'):
    
    if dirpath.startswith(pathHead):

        for filename in filenames:
            if filename.startswith(str1):
                replaceName(dirpath, str1, str2)

            elif filename.find('+YH') > 0 :
                replaceName(dirpath, '+YH', '+'+str2)

            elif filename.find('+'+str1) > 0 :
                replaceName(dirpath, '+'+str1, '+'+str2)




#类内容替换
def replaceStr(filePath, oldstr, newstr):
    with open(filePath) as f:
        file_str = f.read()

    replacements = {oldstr:newstr}
    for src, target in replacements.iteritems():
            file_str = file_str.replace(src, target)

    with open(filePath, 'w') as f:
        f.write(file_str)

    


for dirpath, _, filenames in os.walk('.'):

    if dirpath.startswith(pathHead):
        
        for filename in filenames:
            filepath = os.path.join(dirpath,filename)
            replaceStr(filepath,'(YH)','(%s)' % str2)
            # replaceStr(filepath,'(%s)' % str1,'(%s)' % str2)
            replaceStr(filepath,str1,str2)

            replaceStr(filepath,'+YH','+%s' % str2)
            replaceStr(filepath,'yh_','%s_' % str2.lower())
            replaceStr(filepath,'Yh_','%s_' % str2.capitalize())
            replaceStr(filepath,'%s_' % str1.lower(),'%s_' % str2.lower())
            replaceStr(filepath,'%s_' % str1.capitalize(),'%s_' % str2.capitalize())





# 修改文件的创建时间 和 编辑时间 
dictName = {}#保证相同类的 创建时间一致
for dirpath, _, filenames in os.walk('.'):

    if dirpath.startswith(pathHead):

        for fName in filenames:

            randomDay = random.randint(1,60)
            atime = 0;
            mtime = 0;

            fileKey = fName[0:-2]
            if fileKey in dictName:
                timeTemp = int(dictName[fileKey])
                mtime = timeTemp
                tempNow = int(time.time())
                atime = mtime + random.randint(0,tempNow-timeTemp)
            else :
                timeTemp = int(time.time())
                mtime = timeTemp - random.randint(3600,86400*randomDay)
                dictName[fileKey] = mtime
                randomATime = random.randint(1,randomDay)
                atime = mtime + random.randint(10,86400*randomATime)
                

            # change timestamp of file
            filepath = os.path.join(dirpath,fName)
            os.utime(filepath, (atime, mtime))



for dirpath, _, filenames in os.walk('.'):

    if dirpath.startswith(pathHead):

        for fName in filenames:

            atime = 0;
            mtime = 0;

            fileKey = fName[0:-2]
            if fileKey in dictName:
                timeTemp = int(dictName[fileKey])
                tempNow = int(time.time())
                mtime = timeTemp + random.randint(0,tempNow-timeTemp)
                atime = mtime
                
                filepath = os.path.join(dirpath,fName)
                os.utime(filepath, (atime, mtime))


相关文章

网友评论

    本文标题:iOS批量修改类和类别的名字

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