Copy a remote branch to your computer

#!/usr/bin/env python

# [SNIPPET_NAME: Copy a remote branch to your computer]
# [SNIPPET_CATEGORIES: bzrlib]
# [SNIPPET_DESCRIPTION: Copy a branch to your local computer]
# [SNIPPET_AUTHOR: Jono Bacon <[email protected]>]
# [SNIPPET_LICENSE: GPL]

import os

from bzrlib.branch import Branch
import bzrlib.directory_service
import bzrlib.plugin
bzrlib.plugin.load_plugins()

# first grab the home directory 
home = os.getenv("HOME")

# this helps us determine the full address of the remote branch
branchname = bzrlib.directory_service.directories.dereference('lp:python-snippets')

# let's now connect to the remote branch
remote_branch = Branch.open(branchname)

# download the branch to the user's home directory and call it python-snippets (e.g. /home/jono/python-snippets)
local_branch = remote_branch.bzrdir.sprout(home + '/python-snippets').open_branch()