Autoenv for activating virtualenv when cd into project.
authorpatrick nsukami <ndkpatt@gmail.com>
Sat, 20 Jun 2015 23:57:14 +0000 (23:57 +0000)
committerpatrick nsukami <ndkpatt@gmail.com>
Sat, 20 Jun 2015 23:57:14 +0000 (23:57 +0000)
.autoenv/.travis.yml [new file with mode: 0644]
.autoenv/LICENSE [new file with mode: 0644]
.autoenv/Makefile [new file with mode: 0644]
.autoenv/README.rst [new file with mode: 0644]
.autoenv/activate.sh [new file with mode: 0755]
.autoenv/setup.py [new file with mode: 0644]
.autoenv/tests/simple_comment_test.sh [new file with mode: 0644]
.bashrc

diff --git a/.autoenv/.travis.yml b/.autoenv/.travis.yml
new file mode 100644 (file)
index 0000000..b4f9bcb
--- /dev/null
@@ -0,0 +1,7 @@
+language: ruby
+rvm:
+  - 1.9.3
+install: true
+before_install:
+  - gem install dtf --version 0.1.2
+script: 'dtf test/*'
diff --git a/.autoenv/LICENSE b/.autoenv/LICENSE
new file mode 100644 (file)
index 0000000..ac7b3ae
--- /dev/null
@@ -0,0 +1,7 @@
+Copyright (C) 2012 Kenneth Reitz
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/.autoenv/Makefile b/.autoenv/Makefile
new file mode 100644 (file)
index 0000000..53cf295
--- /dev/null
@@ -0,0 +1,22 @@
+.DEFAULT_GOAL := init
+
+clean:
+       rm -rf *.egg-info dist
+
+develop:
+       python setup.py develop
+
+init:
+       gem install dtf --version 0.1.2
+
+install:
+       python setup.py install
+
+publish: clean
+       python setup.py register sdist upload
+
+test:
+       dtf tests/*
+
+uninstall:
+       pip uninstall autoenv
diff --git a/.autoenv/README.rst b/.autoenv/README.rst
new file mode 100644 (file)
index 0000000..b4de83b
--- /dev/null
@@ -0,0 +1,88 @@
+Autoenv: Directory-based Environments
+======================================
+
+Magic per-project shell environments. Very pretentious.
+
+
+What is it?
+-----------
+
+If a directory contains a ``.env`` file, it will automatically be executed
+when you ``cd`` into it.
+
+This is great for...
+
+- auto-activating virtualenvs
+- project-specific environment variables
+- making millions
+
+`Foreman <https://github.com/ddollar/foreman>`_ env files are completely compatible.
+
+You can also nest envs within eachother. How awesome is that!?
+
+Usage
+-----
+
+Follow the white rabbit::
+
+    $ touch project/.env
+    $ echo "echo 'woah'" > project/.env
+    $ cd project
+    woah
+
+
+.. image:: http://media.tumblr.com/tumblr_ltuzjvbQ6L1qzgpx9.gif
+
+
+Install
+-------
+
+Install it easily:
+
+Mac OS X Using Homebrew
+~~~~~~~~~~~~~~~~~~~~~~~
+
+::
+
+    $ brew install autoenv
+    $ echo 'source /usr/local/opt/autoenv/activate.sh' >> ~/.bash_profile
+
+
+Using pip
+~~~~~~~~~
+
+::
+
+    $ pip install autoenv
+
+
+Using git
+~~~~~~~~~
+
+::
+
+    $ git clone git://github.com/kennethreitz/autoenv.git ~/.autoenv
+    $ echo 'source ~/.autoenv/activate.sh' >> ~/.bashrc
+
+
+Disclaimer
+----------
+
+Autoenv overrides ``cd``. If you already do this, invoke ``autoenv_init`` within your custom ``cd`` after sourcing ``activate.sh``.
+
+
+Testing
+-------
+
+Install the test runner::
+
+    $ make
+    gem install dtf --version 0.1.2
+    Successfully installed dtf-0.1.2
+
+Test::
+
+    $ make test
+    dtf tests/*
+    ............
+    ##### Processed commands 14 of 14, success tests 12 of 12.
diff --git a/.autoenv/activate.sh b/.autoenv/activate.sh
new file mode 100755 (executable)
index 0000000..bc49d8c
--- /dev/null
@@ -0,0 +1,147 @@
+#!/usr/bin/env bash
+AUTOENV_AUTH_FILE=~/.autoenv_authorized
+if [ -z "$AUTOENV_ENV_FILENAME" ]; then
+    AUTOENV_ENV_FILENAME=.env
+fi
+
+if [[ -n "${ZSH_VERSION}" ]]
+then __array_offset=0
+else __array_offset=1
+fi
+
+autoenv_init()
+{
+  defIFS=$IFS
+  IFS=$(echo -en "\n\b")
+
+  typeset target home _file
+  typeset -a _files
+  target=$1
+  home="$(dirname $HOME)"
+
+  _files=( $(
+    while [[ "$PWD" != "/" && "$PWD" != "$home" ]]
+    do
+      _file="$PWD/$AUTOENV_ENV_FILENAME"
+      if [[ -e "${_file}" ]]
+      then echo "${_file}"
+      fi
+      builtin cd .. &>/dev/null
+    done
+  ) )
+
+  _file=${#_files[@]}
+  while (( _file > 0 ))
+  do
+    envfile=${_files[_file-__array_offset]}
+    autoenv_check_authz_and_run "$envfile"
+    : $(( _file -= 1 ))
+  done
+
+  IFS=$defIFS
+}
+
+autoenv_run() {
+  typeset _file
+  _file="$(realpath "$1")"
+  autoenv_check_authz_and_run "${_file}"
+}
+
+autoenv_env() {
+  builtin echo "autoenv:" "$@"
+}
+
+autoenv_printf() {
+  builtin printf "autoenv: "
+  builtin printf "$@"
+}
+
+autoenv_indent() {
+  sed 's/.*/autoenv:     &/' $@
+}
+
+autoenv_hashline()
+{
+  typeset envfile hash
+  envfile=$1
+  if which shasum &> /dev/null
+  then hash=$(shasum "$envfile" | cut -d' ' -f 1)
+  else hash=$(sha1sum "$envfile" | cut -d' ' -f 1)
+  fi
+  echo "$envfile:$hash"
+}
+
+autoenv_check_authz()
+{
+  typeset envfile hash
+  envfile=$1
+  hash=$(autoenv_hashline "$envfile")
+  touch $AUTOENV_AUTH_FILE
+  \grep -Gq "$hash" $AUTOENV_AUTH_FILE
+}
+
+autoenv_check_authz_and_run()
+{
+  typeset envfile
+  envfile=$1
+  if autoenv_check_authz "$envfile"; then
+    autoenv_source "$envfile"
+    return 0
+  fi
+  if [[ -z $MC_SID ]]; then #make sure mc is not running
+    autoenv_env
+    autoenv_env "WARNING:"
+    autoenv_env "This is the first time you are about to source $envfile":
+    autoenv_env
+    autoenv_env "    --- (begin contents) ---------------------------------------"
+    autoenv_indent "$envfile"
+    autoenv_env
+    autoenv_env "    --- (end contents) -----------------------------------------"
+    autoenv_env
+    autoenv_printf "Are you sure you want to allow this? (y/N) "
+    read answer
+    if [[ "$answer" == "y" ]]; then
+      autoenv_authorize_env "$envfile"
+      autoenv_source "$envfile"
+    fi
+  fi
+}
+
+autoenv_deauthorize_env() {
+  typeset envfile
+  envfile=$1
+  \cp "$AUTOENV_AUTH_FILE" "$AUTOENV_AUTH_FILE.tmp"
+  \grep -Gv "$envfile:" "$AUTOENV_AUTH_FILE.tmp" > $AUTOENV_AUTH_FILE
+}
+
+autoenv_authorize_env() {
+  typeset envfile
+  envfile=$1
+  autoenv_deauthorize_env "$envfile"
+  autoenv_hashline "$envfile" >> $AUTOENV_AUTH_FILE
+}
+
+autoenv_source() {
+  typeset allexport
+  allexport=$(set +o | grep allexport)
+  set -a
+  source "$1"
+  eval "$allexport"
+}
+
+autoenv_cd()
+{
+  if builtin cd "$@"
+  then
+    autoenv_init
+    return 0
+  else
+    return $?
+  fi
+}
+
+cd() {
+  autoenv_cd "$@"
+}
+
+cd .
diff --git a/.autoenv/setup.py b/.autoenv/setup.py
new file mode 100644 (file)
index 0000000..35d7574
--- /dev/null
@@ -0,0 +1,17 @@
+#!/usr/bin/env python
+"""Pythonic Setup for autoenv."""
+
+
+import setuptools
+
+
+setuptools.setup(
+    version='1.0.0',
+    name='autoenv',
+    description='Directory-based environments.',
+    author='Kenneth Reitz',
+    author_email='_@kennethreitz.com',
+    license='See LICENSE.',
+    url='https://github.com/kennethreitz/autoenv',
+    scripts=['activate.sh'],
+)
diff --git a/.autoenv/tests/simple_comment_test.sh b/.autoenv/tests/simple_comment_test.sh
new file mode 100644 (file)
index 0000000..c424108
--- /dev/null
@@ -0,0 +1,22 @@
+## setup
+source activate.sh
+
+test_dir="${TMPDIR:=/tmp}/_autoenv_$$"
+
+mkdir -p "$test_dir/a/b"
+echo "echo -a/b-" > "$test_dir/a/b/.env"
+echo "echo -a-" > "$test_dir/a/.env"
+mkdir -p "$test_dir/c/d"
+echo "echo -c/d-" > "$test_dir/c/d/.env"
+
+cd $test_dir
+
+## test
+( cd a   ) # match=/-a-/ ; match!=/-a/b-/ ; status=0
+( cd a/b ) # match=/-a-\n-a/b-/ ; status=0
+( cd c   ) # match=/$^/ ; status=0
+( cd c/d ) # match!=/-c-/ ; match=/-c/d-/ ; status=0
+( cd e   ) # match!=/^$/ ; status!=0
+
+## teardown
+rm -rf "$test_dir"
diff --git a/.bashrc b/.bashrc
index 66d69ac..9de5560 100644 (file)
--- a/.bashrc
+++ b/.bashrc
@@ -55,7 +55,7 @@ if [ "$color_prompt" = yes ]; then
     if [ $USER='nsukami' ]; then
         #PS1='${debian_chroot:+($debian_chroot)}5@\[\033[31m\]\h\[\033[00m\]:\w \[\033[1;33m\]$(vcprompt -f %b%a%m%u)\[\033[0m\] > '
         #PS1='${debian_chroot:+($debian_chroot)}5@\[\033[31m\]k\[\033[00m\]:\w \[\033[1;33m\]$(vcprompt -f %b%a%m%u)\[\033[0m\] \n > '
-        PS1='${debian_chroot:+($debian_chroot)}\[\033[31m\]\[\033[00m\]:\w \[\033[1;33m\]$(vcprompt -f %b%a%m%u)\[\033[0m\] \n '
+        PS1='${debian_chroot:+($debian_chroot)}\[\033[31m\]\[\033[00m\]:\w \[\033[1;33m\]$(vcprompt -f %b%a%m%u)\[\033[0m\] \n '
     else
         PS1='${debian_chroot:+($debian_chroot)}\u@\[\033[31m\]\h\[\033[00m\]:\w \[\033[1;33m\]$(vcprompt -f %b%a%m%u)\[\033[0m\] \n > '
     fi
@@ -152,12 +152,18 @@ eval `gpg-agent --daemon`
 [[ -f ~/.bash_memo ]] && . ~/.bash_memo
 
 # custom bindings
-
 [[ -f ~/.bash_bind ]] && . ~/.bash_bind
 
 # custom filters
-
 [[ -f ~/.bash_filters ]] && . ~/.bash_filters
 
+# if autoenv is present
+# usage:
+# $ touch project/.env
+# $ echo "echo 'woah'" > project/.env
+# $ cd project
+# woah
+[[ -f ~/.autoenv/activate.sh ]] && . ~/.autoenv/activate.sh
+
 # print available tmux sessions if available
 tmux list-sessions 2> /dev/null