Making textmate virtualenv aware

So I am using textmate for my python development, but I wanted it to pick up any virtualenv configured in a project. Here’s how to hack the python bundle…

First off, the run script command needs to be aware of the virtualenv stuff. So open up the bundle editor, and replace this:

is_test_script = ENV["TM_FILEPATH"] =~ /(?:\b|_)(?:test)(?:\b|_)/ or
File.read(ENV["TM_FILEPATH"]) =~ /\bimport\b.+(?:unittest)/

TextMate::Executor.run(ENV["TM_PYTHON"] || "python", "-u", ENV["TM_FILEPATH"]) do |str, type|

with:

is_test_script = ENV["TM_FILEPATH"] =~ /(?:\b|_)(?:test)(?:\b|_)/ or
File.read(ENV["TM_FILEPATH"]) =~ /\bimport\b.+(?:unittest)/

# default python
python = ENV["TM_PYTHON"] || "python"

# try for virtualenv if it exists
if ENV.has_key?("TM_PROJECT_DIRECTORY")
virtualenv_python = ENV["TM_PROJECT_DIRECTORY"] + "/bin/python"
if FileTest.exists?(virtualenv_python)
python = virtualenv_python
end
end

TextMate::Executor.run(python, "-u", ENV["TM_FILEPATH"]) do |str, type|

Now, we also want the unit tests to pick up that environment as well, so you need to do the same with the Run Project Unit Tests command. I am using nose to collect tests, and the nosexml plugin to format the results. You should install them if you need to. Replace:

# Find all files that end with "Test.py" and run
# them.

find . -name "*Test.py" -exec "${TM_PYTHON:-python}" '{}' \;|pre

with:

cd $TM_PROJECT_DIRECTORY
if [ -f bin/activate ]
then
source bin/activate
fi
nosetests --xml --xml-formatter=nosexml.TextMateFormatter

PS, it seems like the python bundle needs some love, anyone know the maintainer?

No Trackbacks

You can leave a trackback using this URL: http://www.machine-envy.com/blog/2008/12/16/making-textmate-virtualenv-aware/trackback/

One Comment

  1. james

    You can always branch the bundle and register it with something like “Get Bundles” which I think just about everyone uses.

    Posted June 26, 2009 at 1:49 am | Permalink

Post a Comment

Your email is never shared. Required fields are marked *

*
*