Permalink
Browse files

Add python script that is responsible for resolving a the project pat…

…h and finding the activation file. I have also added a shell wrapper that make it easy to call in from the command line.
  • Loading branch information...
1 parent a1b5e87 commit 0e96738eda9546638db6b149bb8b6fb789928466 @yml committed May 21, 2011
Showing with 112 additions and 22 deletions.
  1. +1 −1 _vim/bundle/minibufexpl
  2. +1 −1 _vim/bundle/pydoc
  3. +1 −1 _vim/snippets/python.snippets
  4. +57 −0 _workon.py
  5. +52 −0 _workon.sh
  6. +0 −19 virtualenv_vimrc.sample
Submodule pydoc updated 2 files
+2 −0 README
+0 −0 ftplugin/python_pydoc.vim
@@ -1,5 +1,5 @@
snippet pdb
import pdb; pdb.set_trace()
snippet ipdb$
->---import ipdb; ipdb.set_trace()$
+ import ipdb; ipdb.set_trace()$
View
@@ -0,0 +1,57 @@
+#!/usr/bin/env python
+import os
+import sys
+from fabric.api import local
+
+PROJECT_ROOTS = {
+ 'webapp': os.path.abspath('/opt/webapps'),
+ 'sandbox': os.path.abspath('/home/yml/workdir/sandbox')
+ }
+
+
+class ProjectNotFoundException(Exception):
+ pass
+
+
+class WorkonProject(object):
+ """
+ This class is responsible for all work to
+ find and activate the virtualenv in a project.
+ """
+ def __init__(self, project):
+ self.project = project
+ self.project_dir = self._project_lookup()
+ self.activation_file = self._activation_file() or None
+
+ def _project_lookup(self):
+ """This function lookup for the project you
+ want to work on.
+ It return the complete path to the project
+ """
+ if os.path.isdir(self.project):
+ return os.path.abspath(self.project)
+ else:
+ for project_root in PROJECT_ROOTS.values():
+ if os.path.isdir(os.path.join(project_root, self.project)):
+ return os.path.join(project_root, self.project)
+ raise ProjectNotFoundException
+
+ def _activation_file(self):
+ """
+ This method return the path to the file that can activate the env
+ if it exists
+
+ @param project_dir is the path of the project containning the venv
+ """
+ activate_file = os.path.join(self.project_dir, 'bin', 'activate')
+ if os.path.isfile(activate_file):
+ return activate_file
+
+
+def main():
+ project = sys.argv[1]
+ wp = WorkonProject(project)
+ print('%s,%s' % (wp.project_dir, wp.activation_file))
+
+if __name__ == '__main__':
+ main()
View
@@ -0,0 +1,52 @@
+#!/bin/bash
+
+workon_usage () {
+ echo "Usage: workon <project lookup>"
+ echo "Project lookup can be a project name or a path."
+}
+
+# Test if an argument is given else print the usage help text
+if [ -z $1 ]
+ then
+ workon_usage
+ return 1
+fi
+
+echo "Deactivating the current virtual env"
+# Deactivate any current environment "destructively"
+# before switching so we use our override function,
+# if it exists.
+type deactivate >/dev/null 2>&1
+if [ $? -eq 0 ]
+ then
+ deactivate
+ unset -f deactivate >/dev/null 2>&1
+fi
+
+echo "Looking for project $1"
+OUTPUT=`python ~/.workon.py $1`
+
+OIFS=$IFS
+set -- $OUTPUT
+IFS=","; declare -a Array=($*)
+IFS=$OIFS
+
+echo "output $OUTPUT"
+echo "${Array[@]}"
+
+PROJECT_DIR=${Array[0]}
+ACTIVATION_FILE=${Array[1]}
+
+echo "Project dir $PROJECT_DIR"
+echo "activation file $ACTIVATION_FILE"
+
+if [ -d $PROJECT_DIR ]
+ then
+ cd $PROJECT_DIR
+fi
+
+if [ -f $ACTIVATION_FILE ]
+ then
+ source $ACTIVATION_FILE
+fi
+
View
@@ -1,19 +0,0 @@
-py << EOF
-import os.path
-import sys
-import vim
-
-# SET THIS MANUALLY
-# =================
-DJANGO_SETTINGS_MODULE='tekblog_sample.settings'
-
-project_base_dir = os.environ['VIRTUAL_ENV']
-
-sys.path.insert(0, project_base_dir)
-
-activate_this = os.path.join(project_base_dir, 'bin/activate_this.py')
-execfile(activate_this, dict(__file__=activate_this))
-
-os.environ['DJANGO_SETTINGS_MODULE'] = DJANGO_SETTINGS_MODULE
-EOF
-

0 comments on commit 0e96738

Please sign in to comment.