由于修改IOS工程名称十分繁琐,所以写了Python脚本来进行直接修改,同时也适用于其他项目工程的名称修改,直接看代码.
import os
import sys
import platform
import random
import re
import time
import shutil
import chardet
#旧的工程目录名称
g_old_project_name="XXXX"
def get_dirs_files_list(rootdir,pathlist,dirlist):
list = os.listdir(rootdir)
for i in range(0, len(list)):
path = os.path.join(rootdir, list[i])
if os.path.isfile(path):
ext=os.path.splitext(path)[1]
if(1|(".cpp" == ext)|(".hpp" == ext)|(".h" == ext)|(".c" == ext)):
#print (path)
pathlist.append(path)
else:
#过滤不要的文件夹
if((".\\_xxx_filter_xxx" == path)):
continue;
#print(path)
dirlist.append(path)
get_dirs_files_list(path,pathlist,dirlist)
def new_project(prj_name):
new_prj_main_dir=".\\"+prj_name;
src_path_list=[];
src_dir_list=[];
get_dirs_files_list(".\\"+g_old_project_name,src_path_list,src_dir_list)
if(os.path.exists(new_prj_main_dir)):
shutil.rmtree(new_prj_main_dir)
build_new_directory_structure(prj_name,src_dir_list);
filespath_out=build_new_files_structure(prj_name,src_path_list);
reset_prjname_in_files(prj_name,filespath_out);
print("项目创建完毕:"+prj_name)
def build_new_directory_structure(prj_name,src_dir_list):
for dirpath in src_dir_list:
dirpath=dirpath.replace(g_old_project_name,prj_name);
print("正在创建目录:"+dirpath)
os.makedirs(dirpath)
return;
def build_new_files_structure(prj_name,src_path_list):
filespath_out=[]
for filespath in src_path_list:
newpath=filespath.replace(g_old_project_name,prj_name);
shutil.copyfile(filespath,newpath)
print("正在创建文件:"+newpath)
filespath_out.append(newpath);
return filespath_out;
def check_codes(rpath):
f = open(rpath,'rb')
data = f.read()
rt=chardet.detect(data)
return rt['encoding'];
def replace_file_string(path,search_string,to_string):
#print("正在分析编码:"+path)
ext=os.path.splitext(path)[1]
#过滤无需编码分析的文件
if((""==ext)|(".jpg" in ext)|(".png" in ext)|(".ttf" in ext)|(".Bin" in ext)|(".bin" in ext)):
return;
sig=check_codes(path)
#print("编码:"+sig,"ex:",ext)
if((sig==None)|(sig=="None")|(sig=="Windows-1254")|(sig=="Windows-1252")):
return;
print("正在处理文件:"+path+" encoding:"+sig)
f=open(path,'r',encoding=sig)
alllines=f.readlines()
f.close()
f=open(path,'w+',encoding=sig)
for eachline in alllines:
a=eachline.replace(search_string,to_string)
f.writelines(a)
f.close()
#print("正在处理文件:"+str(len(alllines)))
def reset_prjname_in_files(prj_name,filespath_out):
for ipath in filespath_out:
replace_file_string(ipath,g_old_project_name,prj_name)
#此.py文件应与旧的工程目录同级下执行
if __name__ == '__main__':
#user_input = input("project name:")
#新的工程目录名称
user_input ='new_prj';
if(user_input==g_old_project_name):
print("工程名不允许为:"+g_old_project_name)
else:
new_project(user_input);