本文適用於使用Subversion版本控制的讀者.
前一段時間研究了一下TortoiseSVN的原始碼 , 發現它使用 Nant+SubWCRev.exe 來自動產生版本號碼的. SubWCRev.exe 是TortoiseSVN下的一個命令列工具, 安裝了TortoiseSVN 就找到它. 這裡補充一下TortoiseSVN的版本號碼命名方式, 它的版本號碼是: 主要版本號(MajorVersion).從版本號碼(MinorVersion).小版本號碼(MicroVersion) build svn程式碼程式庫的修訂版本號碼, 其中主要版本號(MajorVersion), 從版本號碼(MinorVersion), 小版本號碼(MicroVersion) 是通過設定檔固定的.
在前一個項目中, 我用的就是TortoiseSVN的這種方式來自動產生版本號碼. 後來我想把它也移植到linux下, 剛開始時遇到了問題, 就改用python + pysvn 來實現這個功能. 以下是代碼:
MakeVersion.py 是實現代碼
#--*-- coding:cp936 --*--
"""
@file: MakeVersion.py
自動產生版本號碼
@author: riag
@contract: riag@163.com, http://blog.csdn.net/riag
@version: 0.12
@attention: 需要安裝pysvn模組
"""
import pysvn
import sys
import os
import time
from string import Template
import traceback
import VersionConf
VersionText = """
#define FILEVER ${MajorVersion},${MinorVersion},${MicroVersion},${WCREV}
#define PRODUCTVER ${MajorVersion},${MinorVersion},${MicroVersion},${WCREV}
#define STRFILEVER "${MajorVersion}, ${MinorVersion}, ${MicroVersion}, ${WCREV}/0"
#define STRPRODUCTVER "${MajorVersion}, ${MinorVersion}, ${MicroVersion}, ${WCREV}/0"
#define VERMAJOR ${MajorVersion}
#define VERMINOR ${MinorVersion}
#define VERMICRO ${MicroVersion}
#define VERBUILD ${WCREV}
#define BUILDNUMBER "${MajorVersion}.${MinorVersion}.${MicroVersion}, build ${WCREV}"
#define VERDATE "${WCDATE}"
#define BUILDTIME ${BuildTime}
"""
if __name__=="__main__":
if len(sys.argv) == 2 :
SVNReposPath = sys.argv[1]
else: SVNReposPath = os.getcwd() ;
try:
MajorVersion = VersionConf.MajorVersion
MinorVersion = VersionConf.MinorVersion
MicroVersion = VersionConf.MicroVersion
client = pysvn.Client()
LogList = client.log(SVNReposPath, limit=1 )
ReposInfo = LogList[0]
ReposRevNumber = ReposInfo.revision.number
ReposComitTime = time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(ReposInfo.date)) ;
BuildTime = """ + time.strftime("%Y/%m/%d %H:%M:%S", time.localtime()) + """ ;
try:
os.remove("version.h") ;
except: pass
try:
fread = open("VersionFormat.txt", "r") ;
VersionText = "" ;
line = fread.readline()
while line:
VersionText += line ;
line = fread.readline()
fread.close() ;
except:
pass
VersionValue = {"MajorVersion":MajorVersion, "MinorVersion":MinorVersion, "MicroVersion":MicroVersion}
VersionValue.update({'WCREV':ReposRevNumber, 'WCDATE':ReposComitTime, 'BuildTime':BuildTime})
t = Template(VersionText)
fwrite = open("version.h", "w")
fwrite.write(t.safe_substitute(VersionValue))
fwrite.close() ;
print "成功產生版本號碼"
except:
traceback.print_exc(file = sys.stdout)
print "無法產生版本號碼"
os.system("pause")
VersionConf.py 是設定檔:
#--*-- coding:cp936 --*--
MajorVersion = "1"
MinorVersion = "2"
MicroVersion = "0"
還可以有VersionFormat.txt, 這個用來描述version.h的格式, 如果沒有, 就採用預設格式產生version.h
#define FILEVER ${MajorVersion},${MinorVersion},${MicroVersion},${WCREV}
#define PRODUCTVER ${MajorVersion},${MinorVersion},${MicroVersion},${WCREV}
#define STRFILEVER "${MajorVersion}, ${MinorVersion}, ${MicroVersion}, ${WCREV}/0"
#define STRPRODUCTVER "${MajorVersion}, ${MinorVersion}, ${MicroVersion}, ${WCREV}/0"
#define VERMAJOR ${MajorVersion}
#define VERMINOR ${MinorVersion}
#define VERMICRO ${MicroVersion}
#define VERBUILD ${WCREV}
#define BUILDNUMBER "${MajorVersion}.${MinorVersion}.${MicroVersion}, build ${WCREV}"
#define VERDATE "${WCDATE}"
#define BUILDTIME ${BuildTime}
用法: 把這幾個檔案放到svn程式碼程式庫的一個工作拷貝下, 直接運行;
或 python MakeVersion.py Path, MakeVersion.py 後可以加一個路徑
代碼可在這裡下載:http://download.csdn.net/source/186366