Skip to content

Commit

Permalink
initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
Dusty Phillips committed Mar 20, 2012
0 parents commit 77e0a7e
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
35 changes: 35 additions & 0 deletions README.md
@@ -0,0 +1,35 @@
re-env
======

This is a simple bash function that solves the problem of activating a Python
virtualenv that is in the current directory or a parent directory. It
recursively searches for a directory named venv and activates the bin/activate
script inside that directory.

Rather than storing all virtualenvs in a single directory, as required by the
workon script included with virtualenv-wrappers, this script assumes that the
virtualenv for a given codebase is in the same directory as the code it is
being maintained for. For example, my code looks something like this:

/home/dusty/code/
+ project1
| + src
| | + pkg1
| | + pkg2
| + doc
| + venv
+ project2
| + src
| + venv27
| + venvpypy
+ project3
| + venv

If I am in the `code/project1/src/pkg1` directory, I can run the `v` function and it will
find and activate the virtualenv script in `code/project1/venv/bin/activate`. If I am in
the `project2/src` directory and I run the `v venvpypy` command, it will find and activate
the virtualenv in `code/project2/venvpypy/bin/activate`.

The easiest way to install is to copy the function in your ~/.bashrc.

Note that I am a Python programmer, this may not be the prettiest bash you've seen.
49 changes: 49 additions & 0 deletions re-env.sh
@@ -0,0 +1,49 @@
# Copyright (c) <year> <copyright holders>

# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

# put this in your bashrc

function v {
# activate a virtualenv
# usage: v
# activate the virtualenv named venv
# usage: v venvname
# activate the virtualenv named venvname
name=venv
if [ $1 ] ; then
name=$1
fi
olddir=$(pwd)
quit=0
deactivate &>/dev/null
cwd=$(pwd)
while [ $quit -eq 0 ]
do
cd $cwd
if [ $cwd == '/' ] ; then
quit=1
fi
if [ -e $name ] ; then
source "$name/bin/activate"
quit=1
fi
cwd=$(readlink -f $(dirname $cwd))
done
cd $olddir
}

0 comments on commit 77e0a7e

Please sign in to comment.