python 修改vs工程属性

来源:互联网 发布:一年级毕业季网络直播 编辑:程序博客网 时间:2024/05/22 17:10

有时候,在学习新技术的时候,或者在基于测试用例的开发新应用的时候,需要写很多工程,这些工程的属性配置基本都是一样,如,include头文件路径,lib路径,lib的input名字。新建一个工程,需要重写非常麻烦,vs能配置一个通用的,但是其他工程中又不需要这些的环境,所以突发想法写个脚本批处理vs这些属性。这里使用python修改当前目录下所有.vcvproj工程属性。

使用.notepad打开vcxproj工程文件,可以发现这是一个.xml文件,第一行声明了其文件编码格式为utf-8。但是这里需要注意的是这个文件的格式是utf-8-BOM,而在用python文件读取vcFile = open(fileName, encoding="utf-8"),在后面使用python的print或者read函数会出错,所以我们需要去除bom头文件最开始的3个字节。

def removeBom(file):    BOM = b'\xef\xbb\xbf'    existBom = lambda s: True if s==BOM else False    f = open(file, 'rb')    if existBom( f.read(3) ):        fbody = f.read()        #f.close()        with open(file, 'wb') as f:            f.write(fbody)

其次的就是递归查询当前目录下的.vcxproj文件。文件名分隔格式为os.path.splitext(filename)[1] == '.vcxproj',会把文件分为两半。找到的文件名添加到list中,后面的for循环就可以处理相应路径下的文件。

fileList = []for dirpath, dirnames, filenames in os.walk("."):    for filename in filenames:        #print(filename)        if os.path.splitext(filename)[1] == '.vcxproj':            filepath = os.path.join(dirpath, filename)            #print("file:" + filepath)            fileList.append(filepath)print(fileList)

接下来观察.vcxproj的文件格式,在指定位置添加我们想添加的东西就可以了。
vcxproj属性部分

修改文件就是智者见智了,我修改的openscenegraph的属性样例,把相应的文件属性改成自己所需就好。

# coding=utf-8import osimport sysimport codecsdef removeBom(file):    BOM = b'\xef\xbb\xbf'    existBom = lambda s: True if s==BOM else False    f = open(file, 'rb')    if existBom( f.read(3) ):        fbody = f.read()        #f.close()        with open(file, 'wb') as f:            f.write(fbody)fileList = []for dirpath, dirnames, filenames in os.walk("."):    for filename in filenames:        #print(filename)        if os.path.splitext(filename)[1] == '.vcxproj':            filepath = os.path.join(dirpath, filename)            #print("file:" + filepath)            fileList.append(filepath)print(fileList)for i in range(len(fileList)):    #traverse file list    fileName = fileList[i]    print(fileName)    removeBom(fileName)       vcFile = open(fileName, encoding="utf-8")    fileContent = ""    Count = 1    while True:        textLines = vcFile.readline()        if not textLines:            break;        #print(textLines)    # debug infomation        debugStr = "</PreprocessorDefinitions>"        debugPos = textLines.find(debugStr)    #add debug include info            if (debugPos != -1):            debugPos += len(debugStr)            addIncludeContent= "\n      <AdditionalIncludeDirectories>$(OSG_ROOT)\include;</AdditionalIncludeDirectories>"            textLines = textLines[:debugPos] + addIncludeContent + textLines[debugPos:]                nextText = vcFile.readline()            if nextText.find("AdditionalIncludeDirectories") == -1:                                   fileContent += textLines;                     fileContent += nextText;                print("exceed to adding include")                continue;            else:                fileContent += textLines                print("exceed to adding include")                               continue;    #add debug lib and input info        libStr = "<GenerateDebugInformation>true</GenerateDebugInformation>"        libPos = textLines.find(libStr);        if (libPos != -1):            libPos += len(libStr)            addLibContent = "\n      <AdditionalLibraryDirectories>$(OSG_ROOT)/lib2012;</AdditionalLibraryDirectories>\n      <AdditionalDependencies>osgd.lib;osgDBd.lib;osgViewerd.lib;osgUtild.lib;osgGAd.lib;</AdditionalDependencies>"            addLibContentRea = "\n      <AdditionalLibraryDirectories>$(OSG_ROOT)/lib2012;</AdditionalLibraryDirectories>\n      <AdditionalDependencies>osg.lib;osgDB.lib;osgViewer.lib;osgUtil.lib;osgGA.lib;</AdditionalDependencies>"            if Count == 1:                textLines = textLines[:libPos] + addLibContent + textLines[libPos:]             else:                textLines = textLines[:libPos] + addLibContentRea + textLines[libPos:]             nextLibText = vcFile.readline()            nextInputText = vcFile.readline()                               print(nextLibText)            print(nextInputText)            if nextLibText.find("AdditionalLibraryDirectories") != -1:                if nextInputText.find("AdditionalDependencies") == -1:                    fileContent += textLines                    fileContent += nextInputText                elif nextInputText.find("AdditionalDependencies") != -1:                    fileContent += textLines                    else:                if nextLibText.find("AdditionalDependencies") != -1:                    fileContent += textLines                    fileContent += nextInputText                elif nextInputText.find("AdditionalDependencies") == -1:                    fileContent += textLines                    fileContent += nextLibText                    fileContent += nextInputText                                print("exceed to add lib and input")            Count += 1            continue;        fileContent += textLines;    newFile = open(fileName, "w+", encoding="utf-8")    newFile.write(fileContent)      newFile.close()        vcFile.close;os.system("pause")

【参考资料】:
【1】http://www.runoob.com/python/python-functions.html
【2】http://www.ymsky.net/views/34601.shtml
【3】http://www.jb51.net/article/63247.htm

0 0
原创粉丝点击