利用pysvn使用代码与SVN进行交互
一、从远端仓库检出到本地
import time
import pysvn
import datetime
def get_login(realm
, username
, may_save
):
retcode
= True
username
= 'python'
password
= '123456'
save
= False
return retcode
, username
, password
, save
def ssl_server_trust_prompt(trust_dict
):
return (True
, trust_dict
["failures"]
, True)
client
= pysvn
.Client
()
client
.callback_ssl_server_trust_prompt
= get_login
client
.callback_ssl_server_trust_prompt
= ssl_server_trust_prompt
client
.update
(r
'C:\Users\yueguang\Desktop\project_A')
entry
= client
.info
(r
'C:\Users\yueguang\Desktop\project_A')
print(u
'SVN路径:', entry
.url
)
print(u
'最新版本:', entry
.commit_revision
.number
)
print(u
'提交人员:', entry
.commit_author
)
print(u
'更新日期:', datetime
.datetime
.fromtimestamp
(entry
.commit_time
))
t
= time
.localtime
(entry
.commit_time
)
print('最新提交的时间:', time
.strftime
('%Y-%m-%d %H:%M:%S', t
))
print("-------------ok")
执行结果:
二、获取远端仓库的版本等信息,不用检出
import pysvn
def get_login(realm
, username
, may_save
):
retcode
= True
username
= 'python'
password
= '123456'
save
= False
return retcode
, username
, password
, save
def ssl_server_trust_prompt(trust_dict
):
return (True
, trust_dict
["failures"]
, True)
client
= pysvn
.Client
()
client
.callback_ssl_server_trust_prompt
= get_login
client
.callback_ssl_server_trust_prompt
= ssl_server_trust_prompt
entries_list
= client
.ls
('https://yueguang.jiean.net/svn/test/project_A')
print('-'*90)
print(entries_list
)
for en
in entries_list
:
print()
print('文件的仓库路径 -----> ',en
.name
)
print('文件大小 -----> ',en
.size
)
print('文件更新时间 -----> ', en
.time
)
print('最新的提交者 -----> ',en
.last_author
)
print('文件的revision 版本 -----> ', en
.created_rev
)
print('文件类型 -----> ', en
.kind
)
print('en.time 类型 ----------:', type(en
.time
))
print('en.created_rev 类型 ----------:', type(en
.created_rev
))
print()
print('-'*90)
执行结果:
三、检测状态,获取各种新增、删除、修改、冲突、未版本化的状态
import pysvn
def get_login(realm
, username
, may_save
):
retcode
= True
username
= 'python'
password
= '123456'
save
= False
return retcode
, username
, password
, save
def ssl_server_trust_prompt(trust_dict
):
return (True
, trust_dict
["failures"]
, True)
client
= pysvn
.Client
()
client
.callback_ssl_server_trust_prompt
= get_login
client
.callback_ssl_server_trust_prompt
= ssl_server_trust_prompt
"""
changes ---> 是一个列表,显示的是本地仓库文件 、文件夹的路径信息
"""
changes
= client
.status
(r
'C:\Users\yueguang\Desktop\project_A')
print(changes
)
print()
for f
in changes
:
if f
.text_status
== pysvn
.wc_status_kind
.added
:
print(f
.path
, 'A')
elif f
.text_status
== pysvn
.wc_status_kind
.deleted
:
print(f
.path
, 'D')
elif f
.text_status
== pysvn
.wc_status_kind
.modified
:
print(f
.path
, 'M')
elif f
.text_status
== pysvn
.wc_status_kind
.conflicted
:
print(f
.path
, 'C')
elif f
.text_status
== pysvn
.wc_status_kind
.unversioned
:
print(f
.path
, 'U')
执行结果:
四、相关资源
1、Windows10环境下使用VisualSVN server搭建SVN服务器:https://blog.csdn.net/qq_32786873/article/details/80535567 2、pysvn安装包下载:链接一 、链接二 3、python学习-SVN常用命令:https://blog.csdn.net/mjx91282041/article/details/11630643 4、SVN-钩子祥解与配置:https://www.cnblogs.com/gaohj/p/3154448.html 5、Svn提交后触发Jenkins自动构建:http://pysvn.tigris.org/project_downloads.html