Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1157836
  • 博文数量: 241
  • 博客积分: 4385
  • 博客等级: 上校
  • 技术积分: 2383
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-07 23:13
文章分类

全部博文(241)

文章存档

2013年(1)

2012年(8)

2011年(62)

2010年(109)

2009年(61)

分类: Python/Ruby

2011-01-24 10:37:33

#!/usr/bin/env python

import os, os.path, sys

search_file = "CMakeLists.sub"
search_path = "./"
control_flag = "-c"

args = len(sys.argv)
if args == 3:
    search_path = sys.argv[1]
    control_flag = sys.argv[2]
else:
    print "Usage: %s [directory] [-c, -o]" % sys.argv[0]
    
def del_comment(src_file, des_file_object, line):
    if "D__USE_CTSLIB" in line:
        des_file_object.write(line.lstrip('#'))
    elif "lcts" in line:
        des_file_object.write(line.lstrip('#'))
    elif "lhighgui" in line and "lcts" not in line:
        des_file_object.write(line.replace("SET(SLP_LD_FLAGS \"-lcv -lcvaux -lcxcore -lhighgui\")", "#SET(SLP_LD_FLAGS \"-lcv -lcvaux -lcxcore -lhighgui\")"))
    elif "lSoundTouch" in line and "lcts" not in line:
        des_file_object.write(line.replace("SET(SLP_LD_FLAGS \"-lSoundTouch\")", "#SET(SLP_LD_FLAGS \"-lSoundTouch\")"))
    else:
        des_file_object.write(line)
        
def add_comment(src_file, des_file_object, line):
    if "D__USE_CTSLIB" in line:
        des_file_object.write(line.replace("ADD_DEFINITIONS(\"-D__USE_CTSLIB__\")", "#ADD_DEFINITIONS(\"-D__USE_CTSLIB__\")"))
    elif "lcts" in line:
        if "talking" in src_file:
            des_file_object.write(line.replace("SET(SLP_LD_FLAGS \"-lSoundTouch -lcts\")", "#SET(SLP_LD_FLAGS \"-lSoundTouch -lcts\")"))
        elif "face" in src_file:
            des_file_object.write(line.replace("SET(SLP_LD_FLAGS \"-lcv -lcvaux -lcxcore -lhighgui -lcts\")", "#SET(SLP_LD_FLAGS \"-lcv -lcvaux -lcxcore -lhighgui -lcts\")"))
        else:
            des_file_object.write(line.replace("SET(SLP_LD_FLAGS \"-lcts\")", "#SET(SLP_LD_FLAGS \"-lcts\")"))
    elif "lhighgui" in line and "lcts" not in line:
        des_file_object.write(line.lstrip('#'))
    elif "lSoundTouch" in line and "lcts" not in line:
        des_file_object.write(line.lstrip('#'))
    else:
        des_file_object.write(line)

def control_comment(file, flag):
    src_file = file
    des_file = "./CMakeLists.sub.tmp"
    
    src_file_object = open(src_file, "r")
    des_file_object = open(des_file, "a")
    
    for each_line in src_file_object:
        if flag == "-c":
            add_comment(src_file, des_file_object, each_line)
        elif flag == "-o":
            del_comment(src_file, des_file_object, each_line)
            
    mv_cmd = "mv"+" "+des_file+" "+src_file
    os.system(mv_cmd)
    
def find_files(path, file):
    result = []
    for root, dirs, files in os.walk(path):
        for item in files:
            if file in item:
                result.append(os.path.join(root, file))
    return result

files = find_files(search_path, search_file)
for file in files:
    print file
    control_comment(file, control_flag)    


阅读(833) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~