Python 一键commit文件、目录到SVN服务器
一键commit文件、目录到SVN服务器
by:授客 QQ:1033553122
实现功能
一键提交文件、目录到svn
测试环境
Win7 64位
Python 3.3.2
TortoiseSVN 1.9.6-64 Bit
代码show
#!/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不是内部或外部命令,也不是可运行的程序


Python 一键commit文件、目录到SVN服务器的更多相关文章
- 取消本地文件夹与SVN服务器的关联
我们在开发项目中用SVN作为版本管理工具时,从服务器下载到本地的项目是有.svn文件夹的,这个代表是与svn服务器代码相关联的,如果我们想取消本地文件夹与svn服务器的关联,那么有多种方法,这里介绍导 ...
- 解除单个文件的与svn服务器的关联
有些文件和个人开发环境有关不需要和svn服务器做同步,可以取消其和svn服务的关联. 右键选中要取消关联的文件,右键菜单 Tortoise SVN ---> unversion and a ...
- linux下svn导入新目录到svn服务器特定地址
svn import transplant-apps/ svn://xx.xx.xx.90/ -m "changelog:add transplant-apps to 90-svn" ...
- 使用C#代码追加和提交文件到SVN服务器
windows系统下使用svn的命令需要安装一个插件,下载地址:http://sourceforge.net/projects/win32svn/?source=typ_redirect 安装后程序会 ...
- 从svn服务器自动同步到另一台服务器
需求场景 A commit B post-commit C (workstation) --------------> (svn server) ---------------------> ...
- CentOS7 配置SVN服务器
也可以参考这里:https://jingyan.baidu.com/article/148a1921d84be34d71c3b18f.html 1.安装svn yum install -y subve ...
- IntelliJ IDEA之如何提交代码到SVN服务器
一.准备 参照<IntelliJ IDEA教程之如何配置SVN>这篇文章配置SVN插件.提交代码到SVN之前,记得要先创建版本库,请参照<<SVN如何建立版本库>> ...
- Windows下SVN服务器及客户端的使用
原文地址:windows下配置VisualSVN Server服务器 作者:Deem_passion 下载安装文件: 服务端安装文件:VisualSVN-Server-1.6.2 客户端安装文件:To ...
- SVN服务器和客户端使用教程总结
一.SVN简介 Subversion是什么? 它是一个自由/开源的版本控制系统,一组文件存放在中心版本库,记录每一次文件和目录的修改,Subversion允许把数据恢复到早期版本,或是检查数据修改的历 ...
随机推荐
- ubuntu 16.04 python版本切换(python2和python3)
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100 sudo update-alternati ...
- Lock wait timeout exceeded
MySQL事务锁问题-Lock wait timeout exceeded问题: 一次ios在请求接口响应时间超长,耗时几十秒才返回错误提示,后台日志中出现Lock wait timeout exce ...
- HBase——HMaster启动之一(HMaster的构建)
首先,让我们来到HMaster的main方法.我们今天的流程就从这里开始. 我们需要注意,下图所示的tool的类型就是HMasterCommandLine. 接下来,让我们来到HMasterComma ...
- tensorflow 1.0 学习:模型的保存与恢复(Saver)
将训练好的模型参数保存起来,以便以后进行验证或测试,这是我们经常要做的事情.tf里面提供模型保存的是tf.train.Saver()模块. 模型保存,先要创建一个Saver对象:如 saver=tf. ...
- vscode mysql v0.3插件 连接不了
环境: centos7.6 x64 python 3.6 插件mysql v0.3 解决办法:回滚插件版本v0.2.3 使用方法注意: https://www.cnblogs.com/-admin- ...
- 【python】版本35 正则-非库-爬虫-读写xlw文件
#交代:代码凌乱,新手一个,论坛都是高手,我也是鼓了很大勇气,发出来就是被批评和进步的 #需求:需要对某网站的某id子标签批量爬取,每个网页的id在xlw里,爬取完,再批量存取到这xlw里的第6行 ...
- HDU 1006 Tick and Tick 时钟指针问题
Tick and Tick Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- javaweb之Filter过滤器详解
快速入门 1.新建一个类,实现Filter接口 2.实现doFilter()方法,打印一句话,来证明能够进行拦截 3.在web.xml中进行配置(参照Servlet配置) 4.访问一个页面,看看能不能 ...
- Unity中控制天空盒移动的解决办法
为了使天空盒更真实,需要控制天空盒动态旋转. 网上找到的方法是: float num = RenderSettings.skybox.GetFloat("_Rotation"); ...
- ES6 系列之私有变量的实现
前言 在阅读 <ECMAScript 6 入门>的时候,零散的看到有私有变量的实现,所以在此总结一篇. 1. 约定 实现 class Example { constructor() { t ...