Pysvn 使用指南
這是一篇關(guān)于pysvn模塊的指南.
完整和詳細(xì)的API請(qǐng)參考 http://pysvn.tigris.org/docs/pysvn_prog_ref.html
pysvn是操作Subversion版本控制的Python接口模塊. 這個(gè)API接口可以管理一個(gè)工作副本, 查詢檔案庫, 和同步兩個(gè).
該API不能創(chuàng)建新的倉庫; 只能作用在現(xiàn)有倉庫上. 如果你需要?jiǎng)?chuàng)建一個(gè)倉庫, 請(qǐng)使用Subversion的svnadmin命令.
使用這個(gè)API, 你可以check out一份工作拷貝, 添加, 編輯, 和刪除工作文件, 和check in, 比較, 或者放棄更改. 倉庫屬性, 如關(guān)鍵字?jǐn)U展, 行字符結(jié)束, 或者忽略的列表也可以檢查和控制.
Subversion 模型
Subversion是一個(gè)更新-編輯-提交的模型. 首先在本地建立一個(gè)工作副本. 在工作副本上進(jìn)行修改, 最后提交到中央倉庫 (可以是本地或者遠(yuǎn)程).
這個(gè)模型允許多人偶爾會(huì)同時(shí)修改同一個(gè)文件. 大多情況下. Subversion不會(huì)干預(yù)合并這些不同修改, 如果一個(gè)提交失敗, 用戶或者應(yīng)用則要重新檢查和修改然后再次提交.
常見任務(wù)
本節(jié)給出一些使用pysvn接口的常用例子. 業(yè)務(wù)可以會(huì)遞歸的處理目錄. 添加參數(shù)recurse=False以防止這種行為; 例如, 你可以需要添加內(nè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測試項(xiàng)目的例子,目錄是examples/pysvn. 這個(gè)項(xiàng)目是用在剩下的例子.
添加一個(gè)文件或者目錄到倉庫
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'文件, 然后添加到倉庫. 請(qǐng)注意Client.import_()命令會(huì)同時(shí)增加和提交. 大多數(shù)應(yīng)用, 會(huì)在許多修改后再提交.
更新工作副本
import pysvn client = pysvn.Client() client.update('./examples/pysvn')
從倉庫中更新其他用戶修改并保存到本地副本. 大多數(shù)應(yīng)用應(yīng)該經(jīng)常這樣做以防止沖突.
提交更新到倉庫
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ì)提交工作副本所有修改, 如本例所示, 或者通過個(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ì)劃增加或移除留無版本或恢復(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)或重命名文件刪除舊路徑或名稱的文件, 并增加了在新的位置, 同時(shí)保留以前的版本有關(guān)的信息.
在這個(gè)例子里, 我們通過文件名Client.checkin()傳遞父目錄也將是有效的.
轉(zhuǎn)移和合并可以在服務(wù)器端單步完成; 可以參見倉庫任務(wù)的那節(jié)例子.
從倉庫中刪除文件或目錄
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ǔ)庫目錄. 該文件仍然存在于以前的版本, 可以通過檢查或以其他方式進(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-', '.')
獲取倉庫URL
import pysvn client = pysvn.Client() entry = client.info('.') print 'Url:',entry.url
倉庫任務(wù)
本節(jié)說明任務(wù)的例子, 操縱或檢查倉庫.雖然共同任務(wù), 通過本地工作副本時(shí)間的變化, 這些任務(wù)直接影響到庫
獲取倉庫目錄的清單
import pysvn client = pysvn.Client() entry_list = client.ls('.')
從倉庫獲取文件內(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 )
從倉庫中轉(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' )
鎖定文件并鎖定其他用戶或者工作副本
import pysvn client = pysvn.Client() client.lock( 'file.txt', 'reason for locking', force=True )
解鎖
import pysvn client = pysvn.Client() client.unlock( 'file.txt' )
解鎖文件并鎖定其他用戶或工作副本
import pysvn client = pysvn.Client() client.unlock( 'file.txt', force=True )
測試鎖定文件
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)文章
python 辦公自動(dòng)化——基于pyqt5和openpyxl統(tǒng)計(jì)符合要求的名單
前幾天接到的一個(gè)需求,因?yàn)閷W(xué)校給的名單是青年大學(xué)習(xí)已學(xué)習(xí)的名單,然而要知道未學(xué)習(xí)的名單只能從所有團(tuán)員中再排查一次,過程相當(dāng)麻煩。剛好我也學(xué)過一些操作辦公軟件的基礎(chǔ),再加上最近在學(xué)pyqt5,所以我決定用python寫個(gè)自動(dòng)操作文件的腳本給她用用。2021-05-05Python實(shí)現(xiàn)線程池之線程安全隊(duì)列
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)線程池之線程安全隊(duì)列,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05Python使用PyMuPDF實(shí)現(xiàn)添加PDF水印
在日常工作中,我們經(jīng)常需要對(duì)PDF文件進(jìn)行處理,其中一項(xiàng)常見的需求是向PDF文件添加水印,本文將介紹如何使用Python編程語言和PyMuPDF庫在PDF文件中添加水印,感興趣的可以了解一下2023-08-08python學(xué)習(xí)之可迭代對(duì)象、迭代器、生成器
這篇文章主要介紹了python學(xué)習(xí)之可迭代對(duì)象、迭代器、生成器,需要的朋友可以參考下2021-04-04pandas讀取CSV文件時(shí)查看修改各列的數(shù)據(jù)類型格式
這篇文章主要介紹了pandas讀取CSV文件時(shí)查看修改各列的數(shù)據(jù)類型格式,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07python pandas 對(duì)時(shí)間序列文件處理的實(shí)例
今天小編就為大家分享一篇python pandas 對(duì)時(shí)間序列文件處理的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-06-06