views:

100

answers:

2

How can developers set up their environment paths when working on different SVN branches of the same project so that they don't need to setup their paths each time they switch from one branch to another?

I am a Python developer and I find myself working on different branches of the same project and it's pretty ugly to export each time PYTHONPATH when I am switching from one branch to another.

That is if your IDE doesn't do this automatically for you (I am using vim).

A: 

So your workflow is to keep separate branches in separate working directories? That's fine, it's a pretty reasonable workflow with Subversion.

Personally I would change whatever Python programs you have in source control to look for their own libraries and modify sys.path before importing anything; that way they'll work no matter where they've been relocated to.

#!/usr/bin/env python
import os, sys
basedir = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(0, os.path.join(basedir, 'lib'))

If you don't do that, you could put a wrapper executable in your ~/bin or whatever, somewhere in $PATH,

#!/bin/bash
dir=$(pwd)
while [[ $dir != / ]]; do
    if [[ -d "$dir/lib" ]]; then
        export PYTHONPATH="$dir/lib${PYTHONPATH:+:$PYTHONPATH}"
        break
    fi
    dir="${dir%/*}"
done
if (($# > 0)); then exec "$@"; fi

which simply looks around for a lib directory to prepend to $PYTHONPATH before running whatever you want. Maybe name it "py".

$ cd dir1          # dir1/lib exists
$ py ./my-program
$ cd ../dir2       # dir2/lib exists
$ py ./my-program

It's also possible to have a workflow where you stay within a single working directory, using svn sw to change branches. Since your path never changes, setting PYTHONPATH once would stay valid even with branch changes.

(This is the common workflow in git, monotone, hg, etc.)

ephemient
+2  A: 

For another option, you could use a symlink, then have your environment variables point to it. Like:

branch1/
branch2/
current -> branch1/

Then if you want to work on branch2 just point 'current' to it. Your environment variables would point to 'current'.

Chris J