Pysvn 使用指南
這是一篇關(guān)于pysvn模塊的指南.
完整和詳細(xì)的API請(qǐng)參考 http://pysvn.tigris.org/docs/pysvn_prog_ref.html
pysvn是操作Subversion版本控制的Python接口模塊. 這個(gè)API接口可以管理一個(gè)工作副本, 查詢(xún)檔案庫(kù), 和同步兩個(gè).
該API不能創(chuàng)建新的倉(cāng)庫(kù); 只能作用在現(xiàn)有倉(cāng)庫(kù)上. 如果你需要?jiǎng)?chuàng)建一個(gè)倉(cāng)庫(kù), 請(qǐng)使用Subversion的svnadmin命令.
使用這個(gè)API, 你可以check out一份工作拷貝, 添加, 編輯, 和刪除工作文件, 和check in, 比較, 或者放棄更改. 倉(cāng)庫(kù)屬性, 如關(guān)鍵字?jǐn)U展, 行字符結(jié)束, 或者忽略的列表也可以檢查和控制.
Subversion 模型
Subversion是一個(gè)更新-編輯-提交的模型. 首先在本地建立一個(gè)工作副本. 在工作副本上進(jìn)行修改, 最后提交到中央倉(cāng)庫(kù) (可以是本地或者遠(yuǎn)程).
這個(gè)模型允許多人偶爾會(huì)同時(shí)修改同一個(gè)文件. 大多情況下. Subversion不會(huì)干預(yù)合并這些不同修改, 如果一個(gè)提交失敗, 用戶(hù)或者應(yīng)用則要重新檢查和修改然后再次提交.
常見(jiàn)任務(wù)
本節(jié)給出一些使用pysvn接口的常用例子. 業(yè)務(wù)可以會(huì)遞歸的處理目錄. 添加參數(shù)recurse=False以防止這種行為; 例如, 你可以需要添加內(nèi)容沒(méi)有增加一個(gè)目錄.
check out一份工作副本
import pysvn
client = pysvn.Client()
#check out the current version of the pysvn project
client.checkout('http://localhost/example/trunk',
'./examples/pysvn')
#check out revision 11 of the pysvn project
client.checkout('http://localhost/example/trunk',
'./examples/pysvn-11',
revision=pysvn.Revision(pysvn.opt_revision_kind.number, 11))
這是一個(gè)建立example測(cè)試項(xiàng)目的例子,目錄是examples/pysvn. 這個(gè)項(xiàng)目是用在剩下的例子.
添加一個(gè)文件或者目錄到倉(cāng)庫(kù)
import pysvn
# write a file foo.txt
f = file('./examples/pysvn/foo.txt', 'w')
f.write('Sample versioned file via pithon\n')
f.close()
client = pysvn.Client()
#schedule the addition;
# the working copy will now track the file as a scheduled change
client.add('./examples/pysvn/foo.txt')
#committing the change actually adds the file to the repository
client.checkin(['./examples/pysvn/foo.txt'], 'Adding a sample file')
這個(gè)例子是在工作副本中創(chuàng)建了'foo.txt'文件, 然后添加到倉(cāng)庫(kù). 請(qǐng)注意Client.import_()命令會(huì)同時(shí)增加和提交. 大多數(shù)應(yīng)用, 會(huì)在許多修改后再提交.
更新工作副本
import pysvn
client = pysvn.Client()
client.update('./examples/pysvn')
從倉(cāng)庫(kù)中更新其他用戶(hù)修改并保存到本地副本. 大多數(shù)應(yīng)用應(yīng)該經(jīng)常這樣做以防止沖突.
提交更新到倉(cāng)庫(kù)
import pysvn
# edit the file foo.txt
f = open('./examples/pysvn/foo.txt', 'w')
f.write('Sample versioned file via python\n')
f.close()
# checkin the change with a log message
client = pysvn.Client()
client.checkin(['./examples/pysvn'], 'Corrected spelling of python in foo.txt')
提交到Subversion是原子的. 要么所有修改都成功提交, 要么提交失敗. 大部分應(yīng)用會(huì)提交工作副本所有修改, 如本例所示, 或者通過(guò)個(gè)別文件或者目錄, 但必須是同一單位.
放棄工作副本修改
import pysvn
# edit the file foo.txt
f = file('./examples/pysvn/foo.txt', 'w')
f.write('This change will never be seen\n')
f.close()
#discard the edits
client = pysvn.Client()
client.revert('./examples/pysvn/foo.txt')
這丟棄在工作拷貝和恢復(fù)的文件或目錄的任何未提交的未經(jīng)編輯的狀態(tài)變化.
正在計(jì)劃增加或移除留無(wú)版本或恢復(fù)到工作拷貝.
重命名或者移動(dòng)文件
import pysvn
client = pysvn.Client()
#rename the file client side
client.move('./examples/pysvn/foo.txt', './examples/pysvn/foo2.txt')
#checkin the change removes the file from the repository
client.checkin(['./examples/pysvn/foo.txt', './examples/pysvn/foo2.txt'], 'Foo has become Foo2')
移動(dòng)或重命名文件刪除舊路徑或名稱(chēng)的文件, 并增加了在新的位置, 同時(shí)保留以前的版本有關(guān)的信息.
在這個(gè)例子里, 我們通過(guò)文件名Client.checkin()傳遞父目錄也將是有效的.
轉(zhuǎn)移和合并可以在服務(wù)器端單步完成; 可以參見(jiàn)倉(cāng)庫(kù)任務(wù)的那節(jié)例子.
從倉(cāng)庫(kù)中刪除文件或目錄
import pysvn
client = pysvn.Client()
#schedule the removal;
# the file will be removed from the working copy
client.remove('./examples/pysvn/foo2.txt')
#committing the change removes the file from the repository
client.checkin(['./examples/pysvn/foo2.txt'], 'Removing sample file')
有些人把刪除的文件, 或是用完全清除存儲(chǔ)庫(kù)目錄. 該文件仍然存在于以前的版本, 可以通過(guò)檢查或以其他方式進(jìn)行審查以前修訂的內(nèi)容檢索.
確定等待變動(dòng)
import pysvn
client = pysvn.Client()
changes = client.status('./examples/pysvn')
print 'files to be added:'
print [f.path for f in changes if f.text_status == pysvn.wc_status_kind.added]
print 'files to be removed:'
print [f.path for f in changes if f.text_status == pysvn.wc_status_kind.deleted]
print 'files that have changed:'
print [f.path for f in changes if f.text_status == pysvn.wc_status_kind.modified]
print 'files with merge conflicts:'
print [f.path for f in changes if f.text_status == pysvn.wc_status_kind.conflicted]
print 'unversioned files:'
print [f.path for f in changes if f.text_status == pysvn.wc_status_kind.unversioned]
生成差異或補(bǔ)丁
import pysvn
client = pysvn.Client()
diff_text = client.diff('./tmp-file-prefix-', '.')
獲取倉(cāng)庫(kù)URL
import pysvn
client = pysvn.Client()
entry = client.info('.')
print 'Url:',entry.url
倉(cāng)庫(kù)任務(wù)
本節(jié)說(shuō)明任務(wù)的例子, 操縱或檢查倉(cāng)庫(kù).雖然共同任務(wù), 通過(guò)本地工作副本時(shí)間的變化, 這些任務(wù)直接影響到庫(kù)
獲取倉(cāng)庫(kù)目錄的清單
import pysvn
client = pysvn.Client()
entry_list = client.ls('.')
從倉(cāng)庫(kù)獲取文件內(nèi)容
import pysvn
client = pysvn.Client()
file_content = client.cat('file.txt')
創(chuàng)建一個(gè)標(biāo)記或分支
import pysvn
client = pysvn.Client()
log_message = "reason for change"
def get_log_message():
return True, log_message
client.callback_get_log_message = get_log_message
client.copy('http://svnrepo.com/svn/trunk', 'http://svnrepo.com/svn/tag/%s' % tag_name )
從倉(cāng)庫(kù)中轉(zhuǎn)移或者重命名
import pysvn client = pysvn.Client() client.move( 'file_old.txt', 'file_new.txt' )
鎖定文件
import pysvn client = pysvn.Client() client.lock( 'file.txt', 'reason for locking' )
鎖定文件并鎖定其他用戶(hù)或者工作副本
import pysvn client = pysvn.Client() client.lock( 'file.txt', 'reason for locking', force=True )
解鎖
import pysvn client = pysvn.Client() client.unlock( 'file.txt' )
解鎖文件并鎖定其他用戶(hù)或工作副本
import pysvn client = pysvn.Client() client.unlock( 'file.txt', force=True )
測(cè)試鎖定文件
Method 1:
all_entries = self.client.info2( path, recurse=False )
for path, info in all_entries:
if info['lock']:
if info['lock']['token'] != '':
print '%s is locked' % path
print info['lock']['comment']
Method 2:
all_status = self.client.status( path, recurse=False, update=True )
for status in all_status:
if status.entry is not None:
if status.entry.lock_token is not None:
print '%s is locked' % status.path到此這篇關(guān)于Pysvn 使用指南的文章就介紹到這了,更多相關(guān)Pysvn 使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一張圖帶我們?nèi)腴T(mén)Python基礎(chǔ)教程
啄木鳥(niǎo)社區(qū)上原始翻譯后繪制的,最早這個(gè)圖是出現(xiàn)在,這個(gè)圖太棒了,有編程基礎(chǔ)的人一下子就了解 Python 的用法了。真正的 30 分鐘上手,需要的朋友可以參考下2017-02-02
Python數(shù)據(jù)分析之Matplotlib數(shù)據(jù)可視化
這篇文章主要介紹了Python數(shù)據(jù)分析之Matplotlib數(shù)據(jù)可視化,Matplotlib?是?Python?中常用的?2D?繪圖庫(kù),它能輕松地將數(shù)據(jù)進(jìn)行可視化,作出精美的圖表2022-08-08
Python實(shí)現(xiàn)簡(jiǎn)易計(jì)算器的示例代碼
Tkinter作為 Python GUI 開(kāi)發(fā)工具之一,它具有 GUI 軟件包的必備的常用功能。本文就將利用Tkinter編寫(xiě)簡(jiǎn)易的計(jì)算器,感興趣的可以了解一下2022-11-11
利用Django提供的ModelForm增刪改數(shù)據(jù)的方法
這篇文章主要介紹了利用Django提供的ModelForm增刪改數(shù)據(jù),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
python?requests.post請(qǐng)求404問(wèn)題及解決方法
這篇文章主要介紹了python?requests.post請(qǐng)求404問(wèn)題,這里需要根據(jù)自己實(shí)際情況來(lái)分析當(dāng)前接口接收數(shù)據(jù)時(shí)使用的是什么格式,但目前一般的網(wǎng)站都開(kāi)始采用application/jsond的數(shù)據(jù)格式,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09
python3.4 將16進(jìn)制轉(zhuǎn)成字符串的實(shí)例
今天小編就為大家分享一篇python3.4 將16進(jìn)制轉(zhuǎn)成字符串的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06
Python 經(jīng)典貪心算法之Prim算法案例詳解
這篇文章主要介紹了Python 經(jīng)典貪心算法之Prim算法案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09

