Commits

Doug Hellmann committed 2977761

start implementing hook loader and a couple of sample hooks

Comments (0)

Files changed (8)

                                      ),
 
     entry_points = {
-#         'console_scripts': [ 'ical2org = ical2org.app:main' ],
+        #'console_scripts': [ 'venvw_hook = virtualenvwrapper.hook_loader:main' ],
+        'virtualenvwrapper.pre_initialize_source': [
+            'user_scripts = virtualenvwrapper.user_scripts:pre_initialize_source',
+            'make_hooks = virtualenvwrapper.make_hooks:pre_initialize_source',
+            ],
         },
 
     zip_safe=False,

virtualenvwrapper.sh

 #     (mkdir $HOME/.virtualenvs).
 #  2. Add a line like "export WORKON_HOME=$HOME/.virtualenvs"
 #     to your .bashrc.
-#  3. Add a line like "source /path/to/this/file/virtualenvwrapper_bashrc"
+#  3. Add a line like "source /path/to/this/file/virtualenvwrapper.sh"
 #     to your .bashrc.
 #  4. Run: source ~/.bashrc
 #  5. Run: workon

virtualenvwrapper/__init__.py

 """virtualenvwrapper module
 """
 
-import os, warnings
-docs_root = os.path.join(os.path.dirname(__file__), 'docs', 'index.html')
-warnings.warn('Use virtualenvwrapper_bashrc to set up your shell environment.  See %s for details.' % docs_root)
+__import__('pkg_resources').declare_namespace(__name__)

virtualenvwrapper/hook_loader.py

+#!/usr/bin/env python
+# encoding: utf-8
+#
+# Copyright (c) 2010 Doug Hellmann.  All rights reserved.
+#
+"""Load hooks for virtualenvwrapper.
+"""
+
+import optparse
+
+import pkg_resources
+
+def main():
+    parser = optparse.OptionParser(
+        usage='usage: %prog [options] <hook> [options]',
+        prog='virtualenvwrapper.hook_loader',
+        description='Manage hooks for virtualenvwrapper',
+        )
+    options, args = parser.parse_args()
+
+    hook = args[0]
+    print hook
+
+    for ep in pkg_resources.iter_entry_points('virtualenvwrapper.%s' % hook):
+        plugin = ep.load()
+        print plugin()
+    return 0
+
+if __name__ == '__main__':
+    main()

virtualenvwrapper/make_hooks.py

+#!/usr/bin/env python
+# encoding: utf-8
+#
+# Copyright (c) 2010 Doug Hellmann.  All rights reserved.
+#
+"""Plugin to create hooks during initialization phase.
+"""
+
+import pkg_resources
+
+def pre_initialize_source():
+    return pkg_resources.resource_string(__name__, 'make_hooks_pre_initialize.sh')
+

virtualenvwrapper/make_hooks_pre_initialize.sh

+#
+# Create the default hooks, if they do not exist on startup.
+#
+
+# Create a hook script
+#
+# Usage: virtualenvwrapper_make_hook filename comment
+#
+function virtualenvwrapper_make_hook () {
+    filename="$1"
+    comment="$2"
+    if [ ! -f "$filename" ]
+    then
+        #echo "Creating $filename"
+        cat - > "$filename" <<EOF
+#!/bin/sh
+# $comment
+
+EOF
+    fi
+    if [ ! -x "$filename" ]
+    then
+        chmod +x "$filename"
+    fi
+}
+
+# initialize
+virtualenvwrapper_make_hook "$WORKON_HOME/preinitialize" \
+    "This hook is run early in the startup phase when loading virtualenvwrapper.sh."
+virtualenvwrapper_make_hook "$WORKON_HOME/postinitialize" \
+    "This hook is run later in the startup phase when loading virtualenvwrapper.sh."
+# mkvirtualenv
+virtualenvwrapper_make_hook "$WORKON_HOME/premkvirtualenv" \
+    "This hook is run after a new virtualenv is created and before it is activated."
+virtualenvwrapper_make_hook "$WORKON_HOME/postmkvirtualenv" \
+    "This hook is run after a new virtualenv is activated."
+# rmvirtualenv
+virtualenvwrapper_make_hook "$WORKON_HOME/prermvirtualenv" \
+    "This hook is run before a virtualenv is deleted."
+virtualenvwrapper_make_hook "$WORKON_HOME/postrmvirtualenv" \
+    "This hook is run after a virtualenv is deleted."
+# deactivate
+virtualenvwrapper_make_hook "$WORKON_HOME/predeactivate" \
+    "This hook is run before every virtualenv is deactivated."
+virtualenvwrapper_make_hook "$WORKON_HOME/postdeactivate" \
+    "This hook is run after every virtualenv is deactivated."
+# activate
+virtualenvwrapper_make_hook "$WORKON_HOME/preactivate" \
+    "This hook is run before every virtualenv is activated."
+virtualenvwrapper_make_hook "$WORKON_HOME/postactivate" \
+    "This hook is run after every virtualenv is activated."

virtualenvwrapper/user_scripts.py

+#!/usr/bin/env python
+# encoding: utf-8
+#
+# Copyright (c) 2010 Doug Hellmann.  All rights reserved.
+#
+"""Plugin to handle hooks in user-defined scripts.
+"""
+
+import pkg_resources
+
+def pre_initialize_source():
+    return pkg_resources.resource_string(__name__, 'user_scripts_pre_initialize.sh')

virtualenvwrapper/user_scripts_pre_initialize.sh

+#
+# Run user-provided initialization scripts
+#
+global_script="$WORKON_HOME/preinitialize"
+[ -f "$global_script" ] && source "$global_script"
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.