Archive for September 27th, 2011

Svn Recursive Checkout

Hi All,

Its a small helper function to checkout svn repo recursively .
[Python]
“””
Helper function for recursively checkout svn repo to a location.
“””
import os
import logging
import sys
logger = logging.getLogger(“svnRcout”)
logging.basicConfig()

#~ we need to make sure we have pysvn installed.
try:
import pysvn
except ImportError:
logger.exception(“svnRcout need pysvn, please make sure you have pysvn installed.”)
sys.exit()

from optparse import OptionParser as optparse

#~ creating pysvn client.
client = pysvn.Client()

def list_all_svn_childs(root_svn, checkout_loc,trunk_chk,recurse):
“””
Function for creating svn repo to local location.

:param root_svn: Svn root path for checkout_loc

:type root_svn: string

:param checkout_loc: local checkout location path

:type checkout_loc: string

:param trunk_chk: do we need to check only trunk or all including branch and tags.

:type trunk_chk: bool

:param recurse: do we need to checkout recursively or not

:type trunk_chk: bool

“””
entries_list = client.ls( root_svn, revision=pysvn.Revision( pysvn.opt_revision_kind.head ), \
recurse=False, peg_revision=pysvn.Revision\
( pysvn.opt_revision_kind.unspecified ) )
print “\n=” * 80
print “\t\tSVN Checkout starting for %s.\n” % (root_svn)
print “=” * 80
for each_svn in entries_list:
svn_url = each_svn.name

if trunk_chk:
svn_url = os.path.join(each_svn.name,”trunk”)

if not os.path.exists(os.path.join(checkout_loc, os.path.basename(each_svn.name))):
os.mkdir(os.path.join(checkout_loc, os.path.basename(each_svn.name)))

svn_check_out_loc = os.path.join(checkout_loc, os.path.basename(each_svn.name))
revision = client.checkout(svn_url, svn_check_out_loc, recurse=recurse, \
revision=pysvn.Revision( pysvn.opt_revision_kind.head ), \
peg_revision=pysvn.Revision( pysvn.opt_revision_kind.unspecified ), \
ignore_externals=False )
print ” \”%s\” checked out to %s with revision number of %s.” % ( os.path.basename(each_svn.name)\
, svn_check_out_loc, revision.number)

print “=\n” * 80

def main():
“””
Main function and more details can say be the help doc’s
“””
usage = “usage: svnRcout.py [options] arg1 arg2”
parser = optparse(description = “Tool for recursively checkout svn repo. “,
prog = “svnRcout”,
usage = usage,
version = “Version 0.0.1”)
parser.add_option(“-s”, “–svnroot”, action = “store”, type = “string”, \
help = “SVN Root folder path (required).”)
parser.add_option(“-o”, “–output”, action = “store”, type = “string”, \
help = “Checkout location (required).”)
parser.add_option(“-t”, “–trunk”, action = “store_true”, default = True, \
help = “Checkout only trunk, by default its true”)
parser.add_option(“-r”, “–recurse”, action = “store_true”, default = True, \
help = “Checkout recursively , by default its true.”)
(options, args) = parser.parse_args()

svn_root = options.svnroot
svn_checkout = options.output
trunk_chk = options.trunk
recurse = options.recurse

if not svn_root or not svn_checkout:
parser.print_help()
parser.error (“Insufficient arguments.”)

list_all_svn_childs(svn_root, svn_checkout, trunk_chk, recurse)

if __name__ == “__main__”:

main()

[/Python]