Python Subversion Binding


Here are some ongoing notes on using the Python bindings to Subversion.

Note that I'm using Subversion 1.3 which handles pools for you. For this reason, what I say below won't work with versions earlier than 1.3.

from svn import fs, repos, core assert (core.SVN_VER_MAJOR, core.SVN_VER_MINOR) >= (1, 3), "Subversion 1.3 or later required"

General Repository Access

To get a repository object:

repository = repos.open(root_path)

where root_path is the location of the repository on your local filesystem.

The fs module functions take an fs_ptr so you'll want to grab one:

fs_ptr = repos.fs(repository)

You can now get the latest (i.e. youngest) revision number:

youngest_revision_number = fs.youngest_rev(fs_ptr)

It's also possible to get revision-level properties:

property_value = fs.revision_prop(fs_ptr, revision_number, property_name)

Valid property names include "svn:log", "svn:date", "svn:author", etc.

Node Access

To get a node (i.e. file or directory), you first need to get the root of the revision:

root = fs.revision_root(fs_ptr, revision_number)

Then you can get a file stream with:

stream = fs.file_contents(root, path)

where path is the path of the file you want within the repository.

This stream can then be read with:

core.svn_stream_read(stream, length)

and then closed with

core.svn_stream_close(stream)

Alternatively, the stream can be wrapped as a Python file-like object:

core.Stream(stream)

To get a node's properties:

property_dict = fs.node_proplist(root, path)

To get a specific property:

property_value = fs.node_prop(root, path, property_name)

Transactions

To begin a transaction:

txn = fs.begin_txn(fs_ptr, revision)

where revision is the revision to base the changes off.

You then get the root of the transaction:

txn_root = fs.txn_root(txn)

This can then be read and modified as if it were a revision root (see below).

To abort the transaction:

fs.abort_txn(txn)

To commit the transaction:

fs.commit_txn(txn)

This will return a pair of values, the second of which will be the new revision number (or None if the commit failed)

Modifications

Note these all use transaction roots not revision roots.

To create a new (empty) file node:

fs.make_file(txn_root, path)

To delete a node:

fs.delete(txn_root, path)

To change a node's properties:

fs.change_node_prop(txn_root, path, property_name, property_value)

To change a file's content:

stream = fs.apply_text(txt_root, path, None) core.svn_stream_write(stream, "hello world!\n") core.svn_stream_close(stream)

Note that this can be done after an fs.make_file to provide the content for a new file.

Unfortunately, the Stream wrapper object doesn't have a close() method which renders it useless for writing.

The original post was in the categories: python subversion but I'm still in the process of migrating categories over.

The original post had 7 comments I'm in the process of migrating over.