mdk中在文件中加入工程标识

#!/usr/bin/env python2
# -*- coding:utf-8 -*-

import re,glob
import os,sys
import ipdb
import shutil

fn_a = "Main.c"
patt1="proj_name"
patt2=r'(\").*(\")'
p1 = re.compile(patt1)

flist=["Main.c", "app.c", "APP.c", "main.c"]
def check_pattern(fn):
    file = open(fn).read()
    pos = 0
    match_pos = 0
    for line in file.splitlines():
        match = re.search(patt1, line)
        if match:
            match_pos = pos + match.start() #exact pos
            print match_pos
            print file.count('\n', 0, match_pos) #lineno
            return True
        pos = pos + len(line)
    return False



def scan_files(flist):
    for fn in flist:
        if(os.path.exists(fn)):
            if(check_pattern(fn)):
                return fn
        return ""
def create_backup(fn,subs):
    global ds1,ds2
    f1 = open(fn).readlines()
    subs_need = 1
    for line in f1:
        if(p1.search(line)):
            s1=repr(subs)
            s2 = s1[1:len(s1)-2]
            if(re.search(re.escape(s2),line)):
                subs_need = 0
    if(subs_need == 0):
        print "substitue is not need",
        return ""
    f_bak = fn+".bak"
    print f_bak
    shutil.copyfile(fn,f_bak)
    return f_bak

def getFiles(path):
    answer = []
    endPaths = glob.glob(path + "*.uvproj")
    answer += endPaths
    #if len(glob.glob(path + "*ear/")) > 0:
    #        answer += getFiles(path + "*ear/")

    return answer

def get_string(fn,subs):

    def exec_subs(matched):
        #ipdb.set_trace()
        print matched
        s1 = matched.groups()[0]
        s2 = matched.groups()[1]
        s3 = repr(subs)
        return s1+s3[1:len(s3)-2]+s2

    f1 = open(fn).readlines()
    i=0;
    for line in f1:
        if(p1.search(line)):
            print line,
            line = re.sub(patt2,exec_subs,line)
            print line
            f1[i] = line
        i = i+1
    f2 = open(fn_a, 'w')
    for line in f1:
        f2.write(line)
    f2.close()

if __name__ == "__main__":
    print scan_files(flist)
    #sys.exit()
    fns = getFiles("./")
    if(len(fns)):
        pv =  (os.path.realpath(fns[0])).replace(r'\\', r'\\\\');
        print "----",repr(pv)
    f_bak = create_backup(fn_a,pv)
    if(f_bak != ""):
        get_string(f_bak,pv)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值