6

I'm python developer and most frequently I use buildout for managing my projects. In this case I dont ever need to run any command to activate my dependencies environment.

However, sometime I use virtualenv when buildout is to complicated for this particular case.

Recently I started playing with ruby. And noticed very useful feature. Enviourement is changing automatically when I cd in to the project folder. It is somehow related to rvm nad .rvmrc file.

I'm just wondering if there are ways to hook some script on different bash commands. So than I can workon environment_name automatically when cd into to project folder.

So the logic as simple as:

When you cd in the project with folder_name, than script should run workon folder_name

Pol
  • 24,517
  • 28
  • 74
  • 95
  • possible duplicate of [Bash: custom PS1 with nice working directory path](http://stackoverflow.com/questions/5687446/bash-custom-ps1-with-nice-working-directory-path) – tripleee Jul 12 '13 at 17:55
  • @tripleee Sorry, but this is very different question. – Pol Jul 12 '13 at 18:00
  • @Pol: Well, if your shell supports evaluating arbitrary expressions in PS1, you _could_ write a function that detects whether you've changed directories since last prompt, and whether the new directory is a venv directory, and if so activates it. But this would be very, very silly. :) – abarnert Jul 12 '13 at 18:27

3 Answers3

15

One feature of Unix shells is that they let you create shell functions, which are much like functions in other languages; they are essentially named groups of commands. For example, you can write a function named mycd that first runs cd, and then runs other commands:

function mycd () {
    cd "$@"
    if ... ; then
        workon environment
    fi
}

(The "$@" expands to the arguments that you passed to mycd; so mycd /path/to/dir will call cd /path/to/dir.)

As a special case, a shell function actually supersedes a like-named builtin command; so if you name your function cd, it will be run instead of the cd builtin whenever you run cd. In that case, in order for the function to call the builtin cd to perform the actual directory-change (instead of calling itself, causing infinite recursion), it can use Bash's builtin builtin to call a specified builtin command. So:

function cd () {
    builtin cd "$@"    # perform the actual cd
    if ... ; then
        workon environment
    fi
}

(Note: I don't know what your logic is for recognizing a project directory, so I left that as ... for you to fill in. If you describe your logic in a comment, I'll edit accordingly.)

ruakh
  • 175,680
  • 26
  • 273
  • 307
  • Thank you. This is pretty obvious solution, though. I dont think that this is the ruby guys way. – Pol Jul 12 '13 at 17:47
  • @Pol: I don't know about the Ruby feature that you mention, but I don't see what's wrong with an "obvious solution"! – ruakh Jul 12 '13 at 17:49
  • dont get me wrong. I did not say that it is not appropriate. I would say it advanced way. I just feel it a bit to much to recompile cd command for this kind of stuff. I believe that there suppose to be easier ways. – Pol Jul 12 '13 at 17:53
  • By the way I added the logic. – Pol Jul 12 '13 at 17:55
  • @Pol: Sorry, I think you've *completely* misunderstood this answer. This is actually a very easy way, and does not require any sort of "compiling". I guess you're not familiar with shell functions? I'll edit my answer to add more information . . . – ruakh Jul 12 '13 at 17:57
3

I think you're looking for one of two things.

autoenv is a relatively simple tool that creates the relevant bash functions for you. It's essentially doing what ruakh suggested, but you can use it without having to know how the shell works.

virtualenvwrapper is full of tools that make it easier to build smarter versions of the bash functions—e.g., switch to the venv even if you cd into one of its subdirectories instead of the base, or track venvs stored in git or hg, or … See the Tips and Tricks page.

The Cookbook for autoenv, shows some nifty ways ways to use the two together.

abarnert
  • 354,177
  • 51
  • 601
  • 671
0

Just found in the description of virtualenvwraper this topic

It describes exactly what I need.

Pol
  • 24,517
  • 28
  • 74
  • 95