Visual Studio MFC程序中自动修改版本号
通过VS的生成事件+Python脚本+TortoiseSVN来实现版本号自增的方法
机器上需要安装Python和TortoiseSVN
工程属性-》生成前事件-》命令行增加1
2subwcrev.exe ./ "version.temp.h" "version.h"
python.exe version.py "version.h" "$(RESOURCE_FILE)"
version.temp.h1
2
3
version.py1
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139#!/usr/bin/python
# -*- coding: UTF-8 -*-
import sys
import re
import os
def open_file(name, flag):
try:
f = open(name, flag)
return f
except BaseException:
return None
return None
# 提取版本号
def extract_version(version):
# 提取版本号
ver_str = None
ver_num = None
for line in version.readlines(): # 读取文件的每一行
if -1 != line.find('VERSION_STR'):
ver_str = re.findall('\"\d+\.\d+\.\d+\.\d+\"', line)
if -1 != line.find('VERSION_NUM'):
ver_num = re.findall('\d+\,\d+\,\d+\,\d+', line)
if not ver_str:
return None, None
if not ver_num:
return None, None
ver_str = ver_str[0]
ver_num = ver_num[0]
print('make rc tmp ok!')
return ver_str, ver_num
# 打开文件
def open_all_file(version_name, rc_name, rc1_name):
version = open_file(version_name, 'r')
if not version:
print('not open {}'.format(version_name))
return None, None, None
rc = open_file(rc_name, 'r')
if not rc:
print('not open {}'.format(rc_name))
version.close()
return None, None, None
rc1 = open_file(rc1_name, 'w+')
if not rc1:
rc.close()
version.close()
print('not open {}'.format(rc1_name))
return None, None, None
# print('open file ok!')
return version, rc, rc1
def close_all_file(version, rc, rc1):
version.close()
rc.close()
rc1.close()
# print('close all file')
def make_new_rc(rc, rc1, ver_str, ver_num):
flag = 0
for line in rc.readlines(): # 读取文件的每一行
re_str = None
re_num = None
if -1 != line.find('FileVersion'):
re_str = re.findall('\"FileVersion\"\,\s*\"\d+\.\d+\.\d+\.\d+\"', line)
line = line.replace(re_str[0], '\"FileVersion\", {}'.format(ver_str))
flag = flag | 1
if -1 != line.find('FILEVERSION'):
re_num = re.findall('FILEVERSION\s*\d+\,\d+\,\d+\,\d+', line)
line = line.replace(re_num[0], 'FILEVERSION {}'.format(ver_num))
flag = flag | 2
rc1.write(line)
if flag != 3:
return False
return True
# 运行1
def run_x(version, rc, rc1):
# 提取版本号
ver_str, ver_num = extract_version(version)
if not ver_str or not ver_num:
return False
# 生成rc.rc
res = make_new_rc(rc, rc1, ver_str, ver_num)
return res
#
def replace_rc(rc_name, rc1_name):
try:
rc_back = rc_name + '.back'
if os.path.isfile(rc_back):
os.remove(rc_back)
os.rename(rc_name, rc_back)
os.rename(rc1_name, rc_name)
print('replace rc ok')
return True
except BaseException:
return False
def run():
argc = len(sys.argv)
print(sys.argv)
if argc == 3:
version_name = sys.argv[1]
rc_name = sys.argv[2]
rc1_name = rc_name + '.rc'
print('version name = {}\nrc name = {}'.format(version_name, rc_name))
else:
version_name = 'version.h' # sys.argv[1]
rc_name = 'DataVaultClient.rc' # sys.argv[2]
rc1_name = rc_name + '.rc'
return False
# 打开文件
version, rc, rc1 = open_all_file(version_name, rc_name, rc1_name)
if not version or not rc or not rc1:
return False
# 替换
res = run_x(version, rc, rc1)
# 关文件
close_all_file(version, rc, rc1)
if res:
res = replace_rc(rc_name, rc1_name)
return res
if __name__ == "__main__":
if run():
print('make done!')
else:
print('make rc error!')
使用python脚本替换rc文件中的版本号来实现版本号自增。