一鍵commit文件、目錄到SVN服務器
by:授客 QQ:1033553122
一鍵提交文件、目錄到svn
Win7 64位
Python 3.3.2
TortoiseSVN 1.9.6-64 Bit
#!/usr/bin/env/ python
# -*- coding:utf-8 -*-
__author__ = 'shouke'
import subprocess
import os.path
class SVNClient:
def __init__(self):
self.svn_work_path = 'D:\svn\myfolder'
if not os.path.exists(self.svn_work_path):
print('svn工作路徑:%s 不存在,退出程序' % self.svn_work_path)
exit()
self.try_for_filure = 1 # 提交失敗,重試次數
def get_svn_work_path(self):
return self.svn_work_path
def set_svn_work_path(self, svn_work_path):
self.svn_work_path = svn_work_path
def update(self):
args = 'cd /d ' + self.svn_work_path + ' & svn update'
with subprocess.Popen(args, shell=True, universal_newlines = True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc:
output = proc.communicate()
print('執行svn update命令輸出:%s' % str(output))
if not output[1]:
print('svn update命令執行成功' )
return [True,'執行成功']
else:
print('svn update命令執行失敗:%s' % str(output))
return [False, str(output)]
def add(self, path):
args = 'cd /d ' + self.svn_work_path + ' & svn add ' + path
with subprocess.Popen(args, shell=True, universal_newlines = True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc:
output = proc.communicate()
print('執行svn add命令輸出:%s' % str(output))
if not output[1] or ( not str(output) and str(output).find('is already under version control') != -1):
print('svn add命令執行成功' )
return [True,'執行成功']
else:
print('svn add命令執行失敗:%s' % str(output))
return [False, 'svn add命令執行失敗:%s' % str(output)]
def commit(self, path):
args = 'cd /d ' + self.svn_work_path + ' & svn commit -m "添加版本文件"' + path
with subprocess.Popen(args, shell=True, universal_newlines = True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc:
output = proc.communicate()
print('執行svn commit命令輸出:%s' % str(output))
if not output[1]:
print('svn commit命令執行成功' )
return [True,'執行成功']
else:
print('svn commit命令執行失敗,正在重試:%s' % str(output))
if self.try_for_filure != 0:
self.commit(path)
self.try_for_filure = self.try_for_filure - 1
return [False, str(output)]
filepath_list = []
# 獲取目標目錄下的文件|子目錄|子文件路徑
def get_subdir_or_subfile_paths(dirpath, excludes):
global filepath_list
if not os.path.exists(dirpath):
print('路徑:%s 不存在,退出程序' % dirpath)
exit()
elif not os.path.isdir(dirpath):
print('路徑:%s 不為目錄' % dirpath)
return []
for name in os.listdir(dirpath):
for exclude in excludes.strip(',').split(','):
if not name.endswith(exclude):
full_path = os.path.join(dirpath, name)
filepath_list.append(full_path)
if os.path.isdir(full_path):
get_subdir_or_subfile_paths(full_path, exclude)
return filepath_list
if __name__ == '__main__':
svn_client = SVNClient()
svn_client.update()
dirpath = 'dirname' # 'D:\svn\myfolder\dirname'
if svn_client.add(dirpath)[0]:
svn_client.commit(dirpath)
dirpath = 'D:\svn\myfolder\dirname' # ''
# 傳遞每個文件、目錄的絕對路徑,確保重復執行時,給定目錄下新增的文件也可以被提交
paths = get_subdir_or_subfile_paths(dirpath, '.svn') # .svn文件需要被過濾掉,因為無法提交成功
for path in paths:
if svn_client.add(path)[0]:
svn_client.commit(dirpath)
filepath = 'myfile.txt' # 'D:\svn\myfolder\dirname\myfile.txt'
if svn_client.add(filepath)[0]:
svn_client.commit(filepath)
# 報錯
# dirpath = 'd:/svn/dir_out_of_svn_workpath'
# if svn_client.add(dirpath)[0]:
# svn_client.commit(dirpath)
注意:
例中,svn工作路徑為:'D:\svn\myfolder',即“執行checkout時選擇的目錄”
1、只能添加並提交位於svn工作目錄下的文件目錄,否則會報錯,如下:
if __name__ == '__main__':
svn_client = SVNClient()
svn_client.update()
dirpath = 'd:/svn/dir_out_of_svn_workpath'
if svn_client.add(dirpath)[0]:
svn_client.commit(dirpath)
2、如果未對給定目錄執行過add類函數,那么執行add函數后,執行commit函數,將會把該目錄下的文件、目錄及其下子文件、子目錄,一起提交svn;否則不會做任何提交操作;所以,給add傳遞參數,最好是通過遍歷的方式,傳遞每個文件、目錄的絕對路徑。
3、安裝svn時,第二項,必須選擇圖示紅色選框框選項,否則運行會報錯:svn不是內部或外部命令,也不是可運行的程序