<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>smallfish logs &#187; Pysvn</title>
	<atom:link href="http://chenxiaoyu.org/blog/archives/tag/pysvn/feed" rel="self" type="application/rss+xml" />
	<link>http://chenxiaoyu.org</link>
	<description>关注 Python &#38; Go &#38; PostgreSQL</description>
	<lastBuildDate>Sat, 07 Aug 2010 10:31:22 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Pysvn 程序员指南</title>
		<link>http://chenxiaoyu.org/blog/archives/75</link>
		<comments>http://chenxiaoyu.org/blog/archives/75#comments</comments>
		<pubDate>Fri, 20 Nov 2009 09:31:48 +0000</pubDate>
		<dc:creator>smallfish</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Pysvn]]></category>

		<guid isPermaLink="false">http://chenxiaoyu.org/blog/?p=75</guid>
		<description><![CDATA[这是一篇关于pysvn模块的指南.
完整和详细的API请参考 pysvn Programmer&#8217;s Reference.
pysvn是操作Subversion版本控制的Python接口模块. 这个API接口可以管理一个工作副本, 查询档案库, 和同步两个.
该API不能创建新的仓库; 只能作用在现有仓库上. 如果你需要创建一个仓库, 请使用Subversion的svnadmin命令.
使用这个API, 你可以check out一份工作拷贝, 添加, 编辑, 和删除工作文件, 和check in, 比较, 或者放弃更改. 仓库属性, 如关键字扩展, 行字符结束, 或者忽略的列表也可以检查和控制.
Subversion 模型
Subversion是一个更新-编辑-提交的模型. 首先在本地建立一个工作副本. 在工作副本上进行修改, 最后提交到中央仓库 (可以是本地或者远程).
这个模型允许多人偶尔会同时修改同一个文件. 大多情况下. Subversion不会干预合并这些不同修改, 如果一个提交失败, 用户或者应用则要重新检查和修改然后再次提交.
常见任务
本节给出一些使用pysvn接口的常用例子. 业务可以会递归的处理目录. 添加参数recurse=False以防止这种行为; 例如, 你可以需要添加内容没有增加一个目录.

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 [...]]]></description>
			<content:encoded><![CDATA[<p>这是一篇关于pysvn模块的指南.</p>
<p>完整和详细的API请参考 <a href="http://pysvn.tigris.org/docs/pysvn_prog_ref.html" target="_blank">pysvn Programmer&#8217;s Reference</a>.</p>
<p>pysvn是操作Subversion版本控制的Python接口模块. 这个API接口可以管理一个工作副本, 查询档案库, 和同步两个.</p>
<p>该API不能创建新的仓库; 只能作用在现有仓库上. 如果你需要创建一个仓库, 请使用Subversion的svnadmin命令.</p>
<p>使用这个API, 你可以check out一份工作拷贝, 添加, 编辑, 和删除工作文件, 和check in, 比较, 或者放弃更改. 仓库属性, 如关键字扩展, 行字符结束, 或者忽略的列表也可以检查和控制.</p>
<p><strong>Subversion 模型</strong></p>
<p>Subversion是一个更新-编辑-提交的模型. 首先在本地建立一个工作副本. 在工作副本上进行修改, 最后提交到中央仓库 (可以是本地或者远程).</p>
<p>这个模型允许多人偶尔会同时修改同一个文件. 大多情况下. Subversion不会干预合并这些不同修改, 如果一个提交失败, 用户或者应用则要重新检查和修改然后再次提交.</p>
<p><strong>常见任务</strong></p>
<p>本节给出一些使用pysvn接口的常用例子. 业务可以会递归的处理目录. 添加参数recurse=False以防止这种行为; 例如, 你可以需要添加内容没有增加一个目录.<br />
<strong><br />
check out一份工作副本</strong></p>
<pre>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))</pre>
<p>这是一个建立example测试项目的例子，目录是examples/pysvn. 这个项目是用在剩下的例子.</p>
<p><strong>添加一个文件或者目录到仓库</strong></p>
<pre>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')</pre>
<p>这个例子是在工作副本中创建了&#8217;foo.txt&#8217;文件, 然后添加到仓库. 请注意Client.import_()命令会同时增加和提交. 大多数应用, 会在许多修改后再提交.<br />
<strong><br />
更新工作副本</strong></p>
<pre>import pysvn
client = pysvn.Client()
client.update('./examples/pysvn')</pre>
<p>从仓库中更新其他用户修改并保存到本地副本. 大多数应用应该经常这样做以防止冲突.<br />
<strong><br />
提交更新到仓库</strong></p>
<pre>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')</pre>
<p>提交到Subversion是原子的. 要么所有修改都成功提交, 要么提交失败. 大部分应用会提交工作副本所有修改, 如本例所示, 或者通过个别文件或者目录, 但必须是同一单位.<br />
<strong><br />
放弃工作副本修改</strong></p>
<pre>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')</pre>
<p>这丢弃在工作拷贝和恢复的文件或目录的任何未提交的未经编辑的状态变化.</p>
<p>正在计划增加或移除留无版本或恢复到工作拷贝.<br />
<strong><br />
重命名或者移动文件</strong></p>
<pre>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')</pre>
<p>移动或重命名文件删除旧路径或名称的文件, 并增加了在新的位置, 同时保留以前的版本有关的信息.</p>
<p>在这个例子里, 我们通过文件名Client.checkin()传递父目录也将是有效的.</p>
<p>转移和合并可以在服务器端单步完成; 可以参见仓库任务的那节例子.<br />
<strong><br />
从仓库中删除文件或目录</strong></p>
<pre>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')</pre>
<p>有些人把删除的文件, 或是用完全清除存储库目录. 该文件仍然存在于以前的版本, 可以通过检查或以其他方式进行审查以前修订的内容检索.<br />
<strong><br />
确定等待变动</strong></p>
<pre>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]</pre>
<p><strong>生成差异或补丁</strong></p>
<pre>import pysvn
client = pysvn.Client()
diff_text = client.diff('./tmp-file-prefix-', '.')</pre>
<p><strong>获取仓库URL</strong></p>
<pre>import pysvn
client = pysvn.Client()
entry = client.info('.')
print 'Url:',entry.url</pre>
<p><strong>仓库任务</strong></p>
<p>本节说明任务的例子, 操纵或检查仓库.虽然共同任务, 通过本地工作副本时间的变化, 这些任务直接影响到库<br />
获取仓库目录的清单</p>
<pre>import pysvn
client = pysvn.Client()
entry_list = client.ls('.')</pre>
<p><strong>从仓库获取文件内容</strong></p>
<pre>import pysvn
client = pysvn.Client()
file_content = client.cat('file.txt')</pre>
<p><strong>创建一个标记或分支</strong></p>
<pre>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 )</pre>
<p><strong>从仓库中转移或者重命名</strong></p>
<pre>import pysvn
client = pysvn.Client()
client.move( 'file_old.txt', 'file_new.txt' )</pre>
<p><strong>锁定文件</strong></p>
<pre>import pysvn
client = pysvn.Client()
client.lock( 'file.txt', 'reason for locking' )</pre>
<p><strong>锁定文件并锁定其他用户或者工作副本</strong></p>
<pre>import pysvn
client = pysvn.Client()
client.lock( 'file.txt', 'reason for locking', force=True )</pre>
<p><strong>解锁</strong></p>
<pre>import pysvn
client = pysvn.Client()
client.unlock( 'file.txt' )</pre>
<p><strong>解锁文件并锁定其他用户或工作副本</strong></p>
<pre>import pysvn
client = pysvn.Client()
client.unlock( 'file.txt', force=True )</pre>
<p><strong>测试锁定文件</strong></p>
<p>Method 1:</p>
<pre>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']</pre>
<p>Method 2:</p>
<pre>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</pre>
<hr />
<p><small>© smallfish for <a href="http://chenxiaoyu.org">smallfish logs</a>, 2009. |
<a href="http://chenxiaoyu.org/blog/archives/75">Permalink</a> |
<a href="http://chenxiaoyu.org/blog/archives/75#comments">One comment</a> |
Add to
<a href="http://del.icio.us/post?url=http://chenxiaoyu.org/blog/archives/75&title=Pysvn 程序员指南">del.icio.us</a>
<br/>
Post tags: <a href="http://chenxiaoyu.org/blog/archives/tag/pysvn" rel="tag">Pysvn</a>, <a href="http://chenxiaoyu.org/blog/archives/tag/python" rel="tag">Python</a><br/>
</small></p>
<p>
<script type="text/javascript"><!--
google_ad_client = "pub-8914011260472945";
/* 468x60, 创建于 09-11-19 */
google_ad_slot = "7198645178";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</p>
<p><small>Feed enhanced by <a href='http://planetozh.com/blog/my-projects/wordpress-plugin-better-feed-rss/'>Better Feed</a> from  <a href='http://planetozh.com/blog/'>Ozh</a></small></p>
]]></content:encoded>
			<wfw:commentRss>http://chenxiaoyu.org/blog/archives/75/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
