Created
October 8, 2010 01:49
-
-
Save poke/616231 to your computer and use it in GitHub Desktop.
SvnDumpToolEdit: SVN Dump content editor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
""" | |
SvnDumpToolEdit: SVN Dump content editor | |
SvnDumpToolEdit is a small script that allows to change single file nodes | |
within an SVN dump file. It automatically updates the Content-length attribute | |
for nodes and generates a new MD5 checksum when a file is changed. This ensures | |
that the SVN dump will always remain a valid dump file. | |
This script is based on SvnDumpTool and the SvnDump package written by Martin | |
Furter. To run the script, the package needs to be installed. Visit his | |
website[1] for further details and instructions. | |
SvnDumpToolEdit is written by Patrick Westerhoff[2] and released in the public | |
domain. Feel free to modify, improve and redistribute the script. | |
[1] SvnDumpTool: http://svn.borg.ch/svndumptool | |
[2] poke on github: http://github.com/poke | |
Instructions | |
============ | |
To use this script, you will need to modify the `processFile` function below. | |
It gets the current revision ID, the full file path, the file name and a file | |
object. The first three values are just for identification, in case you need | |
it. | |
The file object is opened and positioned at the beginning. To modify the text, | |
you have to save the new text in the *same* file object. See the example below | |
to get an idea of how you can do this. | |
""" | |
import os, re, sys | |
from svndump.file import SvnDumpFile | |
# Temporary file name to use | |
__tmpFileName = '.svndump.nodetext' | |
# Edit this function to your needs. | |
def processFile ( revId, filePath, fileName, f ): | |
""" | |
Process the current file. | |
Arguments: | |
revId Current revision ID. | |
filePath Full file path. | |
fileName File name. | |
f Opened file object to be edited. | |
""" | |
# check if the file should be edited | |
if fileName != 'example.py': | |
return | |
# read the content, move to the start and truncate the file | |
text = f.read() | |
f.seek( 0 ) | |
f.truncate() | |
# edit the file content | |
text = re.sub( 'Foo', 'Bar', text ) | |
# save the content back | |
f.write( text ) | |
def editDump ( srcFile, dstFile ): | |
""" | |
Edit the source dump using the custom processFile function and store it at | |
the destination. | |
Arguments: | |
srcFile Filename of the source dump. | |
dstFile Filename of the destination dump. | |
""" | |
srcDmp = SvnDumpFile() | |
srcDmp.open( srcFile ) | |
dstDmp = None | |
# read source dump | |
while srcDmp.read_next_rev(): | |
revId = srcDmp.get_rev_nr() | |
if dstDmp is None: | |
dstDmp = SvnDumpFile() | |
dstDmp.create_like( dstFile, srcDmp ) | |
# add revision to destination dump | |
dstDmp.add_rev( srcDmp.get_rev_props() ) | |
# load nodes from current revision of source dump | |
for node in srcDmp.get_nodes_iter(): | |
if ( node.has_text() ): | |
with open( __tmpFileName, 'wb+' ) as f: | |
node.write_text_to_file( f ) | |
f.seek( 0 ) | |
processFile( revId, node.get_path(), node.get_name(), f ) | |
# update text | |
node.set_text_file( __tmpFileName ) | |
# add node to current revision of destination dump | |
dstDmp.add_node( node ) | |
srcDmp.close() | |
dstDmp.close() | |
os.unlink( __tmpFileName ) | |
## Module entry | |
if __name__ == '__main__': | |
if len( sys.argv ) > 2: | |
editDump( sys.argv[1], sys.argv[2] ) | |
else: | |
print( 'Usage: %s <source file> <destination file>' % sys.argv[0] ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment