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的文件格式,在指定位置添加我们想添加的东西就可以了。
修改文件就是智者见智了,我修改的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
- python 修改vs工程属性
- VS工程配置属性设置
- VS工程生成工程程序权限修改
- VS工程属性配置 /MDd /MLd /MTd
- VS工程的属性设置总结
- 使用Opencv时VS工程属性设置
- VS修改MFC工程的程序图标
- Python 实例属性VS类属性1
- vs将普通工程修改为com组件工程
- 修改VS属性页中的Inherited values
- 修改VS属性页中的Inherited values
- python pandas修改列属性
- VS 属性页修改当前项目和修改所有项目
- VS 工程属性中“编译事件”(Compile Event)疑难解答
- vs编译器工程属性的宏的意思
- VS工程属性配置 /MDd /MLd /MTd之二(进阶)
- VS的.props工程属性文件的使用
- vs如何将工程配置,保存到属性表
- assertThat用法
- 【Java学习笔记】继承和多态
- Python 全栈开发 -- 开发环境篇
- idea 常用快捷键
- 自定义View关于measure流程的基本思路整理
- python 修改vs工程属性
- py2exe将py打包成exe程序
- 成员函数重载运算符和友元函数重载运算符的比较
- 解析C++中不能重载为友元函数的四个运算符
- 运用smali自动注入技术分析android应用程序行为
- Android 判断 PendingIntent 是否存在
- Hibernate的Query和Session对象的示例
- R语言中的回归诊断
- 用到的命令