2SVN目录间合并文件脚本

之前写了一个SVN提交的脚本, 现在做一个脚本,是把开发环境的代码,合并到要测试的SVN目录中

#!/usr/bin/env python
#coding:utf-8
"""
Author:	rikugun
Site: http://rikugun.iteye.com/

拷贝文件,发布到正式环境TEST_CRM

"""
import optparse
import os
import re
import subprocess
				
def main():
	"""主函数"""
	def getInput(msg):
		"""获取用户输入,默认是NO"""
		return opts.quite or raw_input(msg) not in ['n','no']

	def sysexec(cmd):
		"""执行命令幷返回结果"""
		msg = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE).communicate()
		#[0].decode('utf-8')
		if msg[1] is not None:
			print u'=============== 错误信息====================='
			print msg[1].decode('utf-8')
		if opts.debug:
			print u'================执行命令==================='
			print cmd
			print u'================我是分割线==================='
			print msg[0].decode('utf-8')
			print u'================命令完成==================='
		return msg
	opt = optparse.OptionParser("dsvn [-m comments] [-L listfile] filepath [other files]")
	opt.add_option("-m","--comments",help=u"提交的注释",default="commit to compile")
	opt.add_option("-L","--listfile",help=u"提交的文件列表")
	opt.add_option("-c","--usecopy",action="store_true",help=u"使用拷贝而不是合并")
	opt.add_option("-M","--mergetool",help=u"指定合并工具")
	opt.add_option("-q","--quite",action="store_true",help=u"安静模式,所有提示都选择Y")
	opt.add_option("-d","--debug",action="store_true",help=u"调试模式")
	opts,files = opt.parse_args()
	if len(files) == 0 and opts.listfile is None:
		print u'请输入要提交文件,或者文件列表'
		print opt.print_usage()
		print __doc__
		exit(2)
	if not files and opts.listfile:
		files = [line for line in open(opts.listfile,'r').readlines() if not line.startswith('#')]	
	#获取环境变量
	# 开发环境
	dev_dir =os.getenv('CRM')
	# 本地编译目录
	test_dir = os.getenv('TEST_CRM')+os.sep+"TEST_"
	#源文件路径
	src_file_list =[]
	#目标文件路径
	dst_file_list = []
	
	add_dst_file_list = []
	
	reg = re.compile(dev_dir+os.sep+'(.+)$')
	
	#设置合并工具
	if opts.usecopy:
		if os.name =='nt':
			merge_cmd = 'copy /Y'
		else:	
			merge_cmd='cp -arf'
	else:		
		if opts.mergetool is not None:
			merge_cmd = opts.mergetool
		else:
			mergetool = "opendiff"	
	#svn 提交命令
	svn_cmd = ['svn ci -m "%s"' % opts.comments,]
		
	#过滤文件名 如 CRM_TRANS/src/share/EssErrorTrans.txt	
	fileset = set([reg.match(os.path.realpath(os.getcwd()+os.sep+it)).group(1).strip() for it in files])	
	file_list = '\n'.join(fileset)
			
	# 传输文件,提交到TEST_CRM
	#if	opts.quite or raw_input('commit such files [\n%s\n] to TEST SVN ? (y)es/no :' % file_list) not in ['n','no'] :
	if	getInput('commit such files [\n%s\n] to TEST SVN ? (y)es/no :' % file_list):
		for	x in fileset:
			src_path = dev_dir+os.sep+x
			dst_path = test_dir+x
			src_file_list.append(src_path)
			dst_file_list.append(dst_path)
			sysexec(' '.join([merge_cmd,src_path,dst_path]))
			print dst_path
			#过滤新加的文件,需要额外执行svn add命令
			if not os.path.exists(dst_path):
				add_dst_file_list.append(dst_path)
				
		sysexec(' '.join(['svn up --force']+dst_file_list))
		if len(add_dst_file_list):
			sysexec(' '.join(['svn add']+add_dst_file_list))
		sysexec(' '.join(svn_cmd+dst_file_list))		

	print u'脚本执行成功'		
	
			
if __name__ == "__main__":
	main()
	

你可能感兴趣的:(python,SVN,OS,脚本,UP)