#!/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)
|