Posted By

nerdfiles on 12/10/11


Tagged

Bash python workon virtualenv


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

wliment


work


 / Published in: Bash
 

URL: http://webjournal.nerdfiles.net/2012/11/14/work-that-is-workon-in-python/

"work" with a Python virtual environment of the current dir.

  1. function work() {
  2.  
  3. :<<FUNC_WORK
  4.  
  5. "work" with a Python virtual environment of the current dir.
  6.  
  7. @assumes: Basic shared name between environment and project folder; so, e.g.:
  8.  
  9. #File .bashrc contains:
  10.  
  11. $WORKON_HOME=$HOME/.virtualenvs/shared_name
  12. $PROJECT_HOME=$HOME/Projects/shared_name
  13.  
  14. @usage:
  15.  
  16. $ cd /path/to/dir
  17. $ work
  18.  
  19. $ cd /path/to/dir
  20. $ work scss #see http://snipplr.com/view/68643/a-work-file-example/
  21. $ git status #e.g. (compass will continue crunching, just ctrl+c after mods to SCSS files)
  22.  
  23. FUNC_WORK
  24.  
  25. # Development
  26.  
  27. debug=0
  28.  
  29. if [ "$1" == "scss" ]; then
  30. workon_scss=$1
  31. if [[ -f ".work" ]]; then
  32. source .work
  33. fi
  34. fi
  35.  
  36. # Get the current directory.
  37. # Underscores and periods are dealt with as per, what it seems,
  38. # virtualenv's naming conventions. I have no fully tested this.
  39. old="$PWD"
  40. cur="$PWD"
  41.  
  42. # Store IFS
  43. OLDIFS=$IFS
  44.  
  45. set -- "$cur"
  46. IFS="/"; declare -a pwdarray=($*)
  47. len=${#pwdarray[@]}
  48. len=($len-1)
  49. proj=${pwdarray[$len]}
  50. proj=${proj/"-"/"_"}
  51. proj=${proj/"."/""}
  52.  
  53. # Get the virtual env by a shared name with the project folder.
  54.  
  55. env_proj="$(find "$WORKON_HOME" -type d -maxdepth 1 -iname '*'$proj'*' -print)"
  56. set -- "$env_proj"
  57. IFS="/"; declare -a envarray=($*)
  58. lenw=${#envarray[@]}
  59. lenw=($len)
  60. proj_env=${envarray[$lenw]}
  61.  
  62. # I'm assuming one has set their markdown editor, like Byword.
  63.  
  64. if [[ -f "$PWD/readme.markdown" ]]; then
  65. open readme.markdown
  66. elif [[ -f "$PWD/README.md" ]]; then
  67. open README.md
  68. fi
  69.  
  70. # assuming python; that is #ideas
  71.  
  72. workon $proj_env
  73.  
  74. # Reset IFS
  75. IFS=$OLDIFS
  76.  
  77. # Display
  78.  
  79. printf "\nWorking on [$proj_env]...\n---"
  80. printf "\nEnvironment: $(py?)"
  81. printf "\nParking at yr repo...\n"
  82. git status
  83.  
  84. }

Report this snippet  

You need to login to post a comment.