1. python 脚本实现特定string 的拷贝添加

source/vaues-xxx/string.xml -> aim/values-xxx/strings.xml


#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import re

#source and aim path,must be right
source_dir = "./Email_UnifiedEmail_svn/CP_G11N_SVN/SmartPhone_StringResource_Folder/res/_NEW/"
aim_dir = "./out/"

#source string names
res_names = ["account_setup_options_sync_tasks_label","account_settings_sync_tasks_enable","account_settings_sync_tasks_summary"]
end_tag = "</resources>"

#comments between sources strings
comments = ["    // add for task begin\n","    // add for task end\n"]

def getSubFile(dirpath):
#遍历filepath下所有文件,包括子目录
    files = os.listdir(dirpath)
    for file in files:
        #print file
        fi_d = os.path.join(dirpath,file)
        if os.path.isdir(fi_d):
            getSubFile(fi_d)
        else:
            cp_res(fi_d)

def cp_res(filepath):
    #print filepath
    #print filepath.split("/")[-1]
    #print filepath.split("/")[-2]
    aim_lines = []
    aim_lines_end = []
    aim_path = aim_dir + filepath.split("/")[-2] + "/" + filepath.split("/")[-1]
    has_end = False
    #read aim file
    print "parser the aim file start ..."
    file = open(aim_path)
    while 1:
        line = file.readline()
        if not line:
            break
        if end_tag in line:
            has_end = True
        if has_end:
            aim_lines_end.append(line)
        else:
            aim_lines.append(line)
    file.close()
    #print len(aim_lines)

    print "merge the source file start ..."
    aim_lines.append(comments[0])
    #read souce file
    file = open(filepath)
    while 1:
        line = file.readline()
        if not line:
            break
        for res_name in res_names:
            if res_name in line:
                aim_lines.append(line)
    file.close()
    aim_lines.append(comments[1])
    #write to aim file
    print "write to aim file start ..."
    fo = open(aim_path, "wb")
    for line in aim_lines:
        fo.writelines(line)
    for line in aim_lines_end:
        fo.writelines(line)
    fo.close()


def main():
    getSubFile(source_dir)

if __name__ == '__main__':
    main()


Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐