Some tips for autoenv 2014-01-02

I've finally took the time to test autoenv by kenneth. It's a very nice piece of software that allow the automatic execution of the content of a .env file if present in the current directory or in a directory above. It is very usefull to auto activate virtualenv or those kind of similar actions.

It also comes with security mechanism (it asks you to accept a .env file if it's the first time you saw it and store an hash of it).

The installation instructions and usage informations are clear enough so I won't write about it, but I wanted to share 2 small tips:

Deadline with the "in above directory" case

Autovenv also work when the .env is located in a directory above, the problem is that the CWD passed to the script is the current one and not the one in which the .env file exists. Therefor, just dropping source ve/bin/activate (assuming your virtualenv is called ve) won't work everywhere.

Here is how to get the directory in which the file is in zsh:

${0%/*})

Therefor, you can write this in your .env file:

#!/bin/zsh
[ -e "$(/bin/readlink -f ${0%/*})/ve/bin/activate" ] && source $(/bin/readlink -f ${0%/*})/ve/bin/activate

Auto install for the lazy

You have several computers, your dotfiles are synced but you don't want to care about remembering of installing autoenv everywhere? Just drop this line in your ~/.zshrc (should be working for bash to):

[ -e $HOME/.local/bin/activate.sh ] || (pip install --user autoenv)

# if you don't have it already, put this line after
source $HOME/.local/bin/activate.sh

This will install autoenv, if it's not already installed, the next time you launch a shell.