Python - 修改.vcxproj文件

工作需要,修改一些项目配置,重复、枯燥、乏味,所以用Python写了个自动化脚本,记录一下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python
# coding: utf-8
import os
import re
files = []
current_config = '' # e.g. Debug2010
def main():
global current_config
get_files()
for filepath in files:
print filepath
tmp_file = filepath + '.tmp'
try:
os.remove(os.path.abspath(tmp_file))
except:
pass
os.rename(filepath, tmp_file)
with open(tmp_file, 'r') as tmp_f:
with open(filepath, 'w') as f:
for line in tmp_f.readlines():
# 确定当前配置
m = re.search(r'''<PropertyGroup Condition=.*=='(.*)\|''', line)
if m is not None:
current_config = m.group(1)
f.write(line)
continue
m = re.search(r'''<ItemDefinitionGroup Condition=.*=='(.*)\|''', line)
if m is not None:
current_config = m.group(1)
f.write(line)
continue
# 替换
line = replace(line)
if line:
f.write(line)
os.remove(os.path.abspath(tmp_file))
def get_files():
for dirpath, dirnames, filenames in os.walk('.'):
for filename in filenames:
_, ext = os.path.splitext(filename)
if ext != '.vcxproj':
continue
filepath = os.path.join(dirpath, filename)
files.append(filepath)
def replace(line):
arx_version = ''
lib_suffix = ''
ret = line
is_debug = False
if current_config == 'Debug2010':
arx_version = '2013'
lib_suffix = '19'
is_debug = True
elif current_config == 'Release2010':
arx_version = '2013'
lib_suffix = '19'
elif current_config == 'Debug2012':
arx_version = '2015'
lib_suffix = '20'
is_debug = True
elif current_config == 'Release2012':
arx_version = '2015'
lib_suffix = '20'
if arx_version == '':
return ret
# 附加包含目录
regex_str = r'<AdditionalIncludeDirectories>.*OBJECTARX\\.+</AdditionalIncludeDirectories>'
m = re.search(regex_str, line)
if m is not None:
ret = re.sub(r'(OBJECTARX\\)([0-9]+?)(\\)', r'OBJECTARX\\%s\\' % arx_version, line)
return ret
# 附加依赖项
regex_str = r'<AdditionalDependencies>.+</AdditionalDependencies>'
m = re.search(regex_str, line)
if m is not None:
if is_debug:
ret = re.sub(r'ZW([a-zA-Z]+?)d\.lib', r'ZW\g<1>%sd.lib' % lib_suffix, line)
else:
ret = re.sub(r'ZW([a-zA-Z]+?)\.lib', r'ZW\g<1>%s.lib' % lib_suffix, line)
return ret
if __name__ == '__main__':
main()