virtualenvwrapper.el-0.2.0/0000755000175000017500000000000013434316244015546 5ustar dogslegdogslegvirtualenvwrapper.el-0.2.0/LICENSE0000644000175000017500000000204413434316244016553 0ustar dogslegdogsleg Copyright (C) 2013 James J. Porter 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. virtualenvwrapper.el-0.2.0/README.md0000644000175000017500000003753413434316244017041 0ustar dogslegdogsleg# virtualenvwrapper.el [![Build Status](https://travis-ci.org/porterjamesj/virtualenvwrapper.el.svg?branch=master)](https://travis-ci.org/porterjamesj/virtualenvwrapper.el) [![MELPA](http://melpa.org/packages/virtualenvwrapper-badge.svg)](http://melpa.org/#/virtualenvwrapper) A featureful virtualenv tool for Emacs. Emulates much of the functionality of Doug Hellmann's [virtualenvwrapper](https://bitbucket.org/dhellmann/virtualenvwrapper/). ## Features * Works with the new [python.el](https://github.com/fgallina/python.el), which is the default on Emacs 24.3 and up. Does not support the older python modes. * Python shells, interactive shells, eshell, and any other subprocesses can be made aware of your virtualenvs. * Implements a large subset of the functionality of virtualenvwrapper. ## Basic Usage * Obviously make sure you have [virtualenv](http://www.virtualenv.org/en/latest/) installed. You don't actually need virtualenvwrapper.sh, this is a reimplementation in Emacs lisp. * Install from MELPA (`M-x package-install virtualenvwrapper`), or just put `virtualenvwrapper.el` on your load path somewhere. * Put ```lisp (require 'virtualenvwrapper) (venv-initialize-interactive-shells) ;; if you want interactive shell support (venv-initialize-eshell) ;; if you want eshell support ;; note that setting `venv-location` is not necessary if you ;; use the default location (`~/.virtualenvs`), or if the ;; the environment variable `WORKON_HOME` points to the right place (setq venv-location "/path/to/your/virtualenvs/") ``` in your config somewhere. * Use `M-x venv-workon` to activate virtualenvs and `M-x venv-deactivate` deactivate them. * If you have your virtualenvs spread around the filesystem rather than in one directory, just set venv-location to be a list of paths to each virtualenv. For example: ```lisp (setq venv-location '("/path/to/project1-env/" "/path/to/ptoject2-env/")) ``` Notice that the final directory of each path has a different name. The mode uses this fact to disambiguate virtualenvs from each other, so for now it is required. * You can also change easily the virtual environment location with `M-x venv-set-location`. This is particularly useful when working with tools such as [tox](https://testrun.org/tox/latest/) that generate virtual environments dynamically. ## What do activating and deactivating actually do? Many virtual environment support tools describe their functionality as "it just works" or "it's so simple". This is not descriptive enough to figure out what's wrong when something inevitably breaks, so here I will describe *exactly* what happens when you activate a virtualenv: 1. `python-shell-virtualenv-path` is set to the virtualenv's directory so that when you open a new python shell, it is aware of the virtual environment's installed packages and modules. 2. The virtualenv's `bin` directory is prepended to the `PATH` environment variable so that when a process is launched from Emacs it is aware of any executables installed in the virtualenv (such as `nosetests`, `pep8`, etc.). This comes in handy because you can do `M-! nosetests` to run your tests, for example. 3. The `VIRTUAL_ENV` environment variable is set to the virtualenv's directory so that any tools that depend on this variable function correctly (one such tool is [jedi](http://tkf.github.io/emacs-jedi/)). 4. The virtualenv's `bin` directory is added to the `exec-path`, so that Emacs itself can find the environment's installed executables. This is useful, for example, if you want to have Emacs spawn a subprocess running an executable installed in a virtualenv. 5. [`gud`](https://www.gnu.org/software/emacs/manual/html_node/emacs/Debuggers.html#Debuggers) is configured to run pdb as `python -m pdb` rather than the default `pdb`, which is necessary for virtualenvs to be detected. When you deactivate, all these things are undone. You can safely modify your `PATH` and `exec-path` while a virtualenv is active and expect the changes not to be destroyed by deactivating. This covers everything except interactive shells, which are covered in the next section. ## Shells This thing supports two types of interactive shells, the [eshell](https://www.gnu.org/software/emacs/manual/html_mono/eshell.html) and the [interactive subshell](https://www.gnu.org/software/emacs/manual/html_node/emacs/Interactive-Shell.html) (what you get when you do `M-x shell`). ### Interactive shell Support for interactive shell is turned on by calling `venv-initialize-interactive-shell`. After this is done, whenever you call `shell`, the shell will start in the correct virtualenv. This detects whether or not you have virtualenvwrapper.sh installed and does the right thing in either case. Note that changing the virtualenv in Emacs will not affect any running shells and vice-versa; they are independent processes. #### WARNINGS This feature is a pretty big hack and works by [advising](https://www.gnu.org/software/emacs/manual/html_node/elisp/Advising-Functions.html) the `shell` function. This works fine if you haven't otherwise tricked out or advised it, but if this is the case it may break. Please file an issue if you encounter any bugs with this functionality, I am interested to see how robust it is. ### Eshell support for eshell is turned on by calling `venv-initialize-eshell`. After doing this, any new eshells you launch will be in the correct virtualenv and have access to installed executables, etc. The mode also provides a variety of virtualenvwrapper commands that work identically to their bash/zsh counterparts (described in detail below). Note that in contrast to how interactive shells work, Eshell shares an environment with Emacs, so if you activate or deactivate in one, the other is affected as well. Note that this requires the variable `eshell-modify-global-environment` to be set to true. Running `venv-initialize-eshell` causes this to occur. If this doesn't work for you, open an issue! It's technically possible to separate the two, but it requires some hacking around with the different namespaces that I won't bother to do unless someone really needs it. ## Command Reference The commands this mode provides are prefixed with `venv-` All commands can be called interactively using `M-x`. All of these comamnds have also been aliased without prefixes as eshell functions, so you can call them on the eshell just as you would in bash or zsh. For example: ``` eshell> workon myenv eshell> deactivate eshell> cpvirtualenv env copy eshell> mkvirtualenv newenv ``` All will do what would expect. #### `venv-workon` Prompts for the name of a virtualenv and activates it as described above. Can also be called noninteractively as `(venv-workon "name")`. When called, it sets `gud-pdb-command-name` to `python -m pdb` so that `M-x pdb` can be used inside the virtual environment. #### `venv-deactivate` Deactivates your current virtualenv, undoing everything that `venv-workon` did. This can also be called noninteractively as `(venv-deactivate)`. When called, it sets `gud-pdb-command-name` to its default value (usually `pdb`). #### `venv-mkvirtualenv` Prompt for a name and create a new virtualenv. If your virtualenvs are all kept in the same directory (i.e. `venv-location` is a string), then the new virtualenv will be created in that directory. If you keep your virtualenvs in different places (i.e. `venv-location` is a list), then the new virtualenv will be created in the current default directory. Also callable noninteractively as `(venv-mkvirtualenv "name")`. #### `venv-mkvirtualenv-using` Supplying a prefix command (`C-u`) to `venv-mkvirtualenv` will prompt for a Python interpreter to use. You can use this function to specify the interpreter noninteractively. #### `venv-rmvirtualenv` Prompt for the name of a virutalenv and delete it. Also callable noninteractively as `(venv-rmvirtualenv "name")`. #### `venv-lsvirtualenv` Display all available virtualenvs in a help buffer. Also callable noninteractively as `(venv-list-virtualenvs)`. #### `venv-cdvirtualenv` Change the current default directory to the current virtualenv's directory. If called noninteractively, you can optionally provide an argument, which is interpreted as a subdirectory. For example, to go to the `bin` directory of the currently active virtualenv, call `(venv-cdvirtualenv "bin")`. #### `venv-cpvirtualenv` Makes a new virtualenv that is a copy of an existing one. Prompts for the names of both. *WARNING* This comes with the same caveat as the corresponding command in the original virtualenvwrapper, which is that some packages hardcode their locations when being installed, so creating new virtualenvs in this manner may cause them to break. Use with caution. ## Useful Macros There is a `venv-with-virtualenv` macro, which takes the name of a virtualenv and then any number of forms and executes those forms with that virtualenv active, in that virtualenv's directory. For example: ```lisp (venv-with-virtualenv "myenv" (message default-directory)) ``` Will message the path of `myenv`'s directory. There's also a `venv-all-virtualenv` macro, which takes a series of forms, activates each virtualenv in turn, moves to its directory, and executes the given forms. Since it's common to want to execute shell commands, there are convenience macros, `venv-with-virtualenv-shell-command` and `venv-allvirtualenv-shell-command`, which take a string, interpreted as a shell command, and do exactly what you'd expect. So for example, you can do `(venv-allvirtualenv-shell-command "pip install pep8")` to install `pep8` in all virtualenvs. `venv-allvirtualenv-shell-command` can also be called interactively and will prompt for a command to run if so. The eshell supports using this command just like in bash or zsh, so at an eshell prompt, you can just do: ``` eshell> allvirtualenv pip install pep8 ``` And it will do what you expect. ## Extras This mode doesn't screw with things you probably have customized yourself, such as your mode line, keybindings, mode-hooks, etc. in order to provide stuff like automatically turning on virtualenvs in certain projects, show the virtualenv on the mode line, etc. Instead, you can do all these things pretty easily using tools already provided by Emacs. How to do some of them are described below. ### Keybindings This mode doesn't provide any. I don't presume to know how you want your keybindings, you can bind them to whatever you want! Go crazy! ### Hooks Virtualenvwrapper lets you write shell scripts that run as hooks after you take certain actions, such as creating or deleting a virtualenv. This package provides Emacs [hooks](https://www.gnu.org/software/emacs/manual/html_node/emacs/Hooks.html), to achieve the same thing. The complete list of hooks is: ``` venv-premkvirtualenv-hook venv-postmkvirtualenv-hook venv-prermvirtualenv-hook venv-postrmvirtualenv-hook venv-preactivate-hook venv-postactivate-hook venv-predeactivate-hook venv-postdeactivate-hook ``` each of which is run when you would expect based on the name. For example, to install commonly used packages when a new virtualenv is created you could modify the `venv-postmkvirtualenv-hook` as follows: ```lisp (add-hook 'venv-postmkvirtualenv-hook (lambda () (shell-command "pip install nose flake8 jedi"))) ``` ### Automatically activating a virtualenv in a particular project It's also common to want to have a virtualenv automatically activated when you open a file in a certain project. This mode provides no special way to do this because once again Emacs has already done it in the form of [per-directory local variables](https://www.gnu.org/software/emacs/manual/html_node/emacs/Directory-Variables.html). In order to have a virtualenv automatically activated when you open a python file in a particular project, you could put a `.dir-locals.el` in the project's root directory with something like: ```lisp ((python-mode . ((eval . (venv-workon "myproject-env"))))) ``` ### Automatically activating a virtualenv when using projectile If you're using [projectile](https://github.com/bbatsov/projectile) there's an easier way to automatically activate a virtualenv when entering a project. You just have to call `(venv-projectile-auto-workon)` after switching projects, this can be achieved hooking the call to the action `projectile-switch-project-action` like this: ```lisp (setq projectile-switch-project-action 'venv-projectile-auto-workon) ``` If you relay on another use for this action, then just add a `lambda` instead: ```lisp (setq projectile-switch-project-action '(lambda () (venv-projectile-auto-workon) (projectile-find-file))) ``` As long as a virtualenv is found in the `projectile-project-root` and whose name is in the list `venv-dirlookup-names` it will be automatically activated. By default, it's value is `'(".venv", "venv")'`, but you can set if however you like to match your naming conventions: ```lisp (setq venv-dirlookup-names '(".venv" "pyenv" ".virtual")) ``` You can add as many names you need, the first one found in the current project will be activated. ### Displaying the currently active virtualenv on the mode line The name of the currently active virtualenv is stored in the variable `venv-current-name`. If you want to have it displayed on your custom mode line you can just add `(:exec (list venv-current-name)))` somewhere in your `mode-line-format`. If you don't customize your mode line and just want to have the current virtualenv displayed, you can do: ```lisp (setq-default mode-line-format (cons '(:exec venv-current-name) mode-line-format)) ``` ### Eshell prompt customization You also might want to have the name of your current virtualenv appear on the eshell prompt. You can do this by a pretty similar mechanism, just include `venv-current-name` in your `eshell-prompt-function` somewhere. Here is a simple example of a prompt that includes the current virtualenv name followed by a dollar sign: ```lisp (setq eshell-prompt-function (lambda () (concat venv-current-name " $ "))) ``` Make sure you also adjust your `eshell-prompt-regexp` if you do this. More about customizing the eshell prompt [on the EmacsWiki](http://www.emacswiki.org/emacs/EshellPrompt). ### Bugs / Comments / Contributions Open an issue or a PR! I'm happy to pull in contributions or take suggestions for improvements. ### Hacking I use [Cask](http://cask.readthedocs.io/en/latest/) to manage dependencies and [ert-runner](https://github.com/rejeep/ert-runner.el) for testing. To get started: 1. [install cask](http://cask.readthedocs.io/en/latest/guide/installation.html) 2. Install dependacies with `cask install --dev` 3. Verify that the tests pass with `cask exec ert-runner` The tests are pretty rudimentary integration tests but they verify that all the basic functionality works. If you're planning on submitting a PR, please make sure that the tests pass before you do so. Thanks! ### License Copyright (C) 2013 James J. Porter 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. virtualenvwrapper.el-0.2.0/.travis.yml0000644000175000017500000000126313434316244017661 0ustar dogslegdogsleglanguage: generic sudo: false dist: trusty addons: apt: packages: - python-pip env: global: - PATH=~/.evm/bin:~/.cask/bin:$PATH matrix: - EVM_EMACS=emacs-24.5-travis - EVM_EMACS=emacs-25.1-travis - EVM_EMACS=emacs-25.2-travis - EVM_EMACS=emacs-25.3-travis # - EVM_EMACS=emacs-git-snapshot-travis before_install: - pip install -U --user virtualenv - curl -fsSkL https://raw.github.com/rejeep/evm/master/go | bash - evm config path /tmp - evm install $EVM_EMACS - evm use $EVM_EMACS - hash -r - curl -fsSkL https://raw.github.com/cask/cask/master/go | python - cask exec emacs --version - cask install script: - cask exec ert-runner virtualenvwrapper.el-0.2.0/contrib/0000755000175000017500000000000013434316244017206 5ustar dogslegdogslegvirtualenvwrapper.el-0.2.0/contrib/helm-virtualenvwrapper.el0000644000175000017500000000144413434316244024256 0ustar dogslegdogsleg;;; helm-virtualenvwrapper.el --- A helm-source for virtualenvwrapper.el ;; Copyright (C) 2014 Javier Olaechea ;;; Commentary: ;; To start using define a python-local keybinding or a global one such as: ;; ;; (define-key python-mode-map (kbd "C-c v") 'helm-venv-workon) ;; (global-set-key (kbd "C-c v") 'helm-venv-workon) ;; ;; Then C-c v away ;;;###autoload (defun helm-venv-workon () "Like venv-work, for helm." (interactive) (helm :sources '(helm-source-venv))) (defvar helm-source-venv `((name . "Virtual env completion") (candidates . ,(cl-loop for venv in (venv-get-candidates) collect (cons venv venv))) (action . (("activate" . venv-workon))) (persistent-action . venv-workon) (persistent-help . "Activate the virtualenv."))) virtualenvwrapper.el-0.2.0/Cask0000644000175000017500000000041313434316244016350 0ustar dogslegdogsleg(source gnu) (source melpa) (package "virtualenvwrapper" "20140315" "a featureful virtualenv tool for Emacs") (depends-on "s" "1.6.1") (depends-on "dash" "1.5.0") (development (depends-on "ert-runner") (depends-on "noflet") (depends-on "with-simulated-input")) virtualenvwrapper.el-0.2.0/test/0000755000175000017500000000000013434316244016525 5ustar dogslegdogslegvirtualenvwrapper.el-0.2.0/test/virtualenvwrapper-test.el0000644000175000017500000002047413434316244023633 0ustar dogslegdogsleg;; Rudimentary test suite for virtualenvwrapper.el (load (expand-file-name "virtualenvwrapper.el" default-directory)) (require 's) (require 'noflet) (require 'with-simulated-input) ;; unclear why this is required, we get `(void-function string-trim)' ;; errors without, probably has something to do with byte-compiling (require 'subr-x) (setq venv-tmp-env "emacs-venvwrapper-test") (defmacro with-temp-location (&rest forms) `(let ((venv-location temporary-file-directory)) (unwind-protect (progn ,@forms)))) (defmacro with-temp-env (name &rest forms) `(let ((venv-location temporary-file-directory)) (unwind-protect (progn (venv-mkvirtualenv ,name) ,@forms) (venv-rmvirtualenv ,name)))) (defmacro with-temp-dir (&rest forms) `(let ((temp-dir (file-name-as-directory (make-temp-file nil t)))) (unwind-protect (progn ,@forms) (delete-directory temp-dir t)))) (defun assert-venv-activated () "Runs various assertions to check if a venv is activated." ;; M-x pdb should ask to run "python -m pdb" (should (equal gud-pdb-command-name "python -m pdb")) ;; we store the name correctly (should (s-contains? venv-tmp-env venv-current-name)) ;; assert that the current dir exists and is asbolute (should (file-name-absolute-p venv-current-dir)) (should (file-directory-p venv-current-dir)) ;; we change the path for python mode (should (s-contains? venv-tmp-env python-shell-virtualenv-path)) ;; we set PATH for shell and subprocesses (should (s-contains? venv-tmp-env (getenv "PATH"))) ;; we set VIRTUAL_ENV for jedi and whoever else needs it (should (s-contains? venv-tmp-env (getenv "VIRTUAL_ENV"))) ;; we add our dir to exec-path (should (s-contains? venv-tmp-env (car exec-path)))) (ert-deftest venv-mkvirtualenv-works () (with-temp-location (venv-mkvirtualenv venv-tmp-env) (should (equal venv-current-name venv-tmp-env)) (venv-deactivate) (venv-rmvirtualenv venv-tmp-env))) (ert-deftest venv-rmvirtualenv-works () (let ((venv-location temporary-file-directory)) (venv-mkvirtualenv venv-tmp-env) (venv-deactivate) (venv-rmvirtualenv venv-tmp-env) (should-error (venv-workon venv-tmp-env)))) (ert-deftest venv-mkvirtualenv-select-default-interpreter () (with-temp-location (let ((current-prefix-arg '(4))) (with-simulated-input "RET" (venv-mkvirtualenv venv-tmp-env)) (should (equal venv-current-name venv-tmp-env)) (venv-deactivate) (venv-rmvirtualenv venv-tmp-env)))) (ert-deftest venv-mkvirtualenv-select-different-interpreter () (with-temp-location (let ((current-prefix-arg '(4))) (with-simulated-input (concat (executable-find "python") " RET") (venv-mkvirtualenv venv-tmp-env)) (should (equal venv-current-name venv-tmp-env)) (venv-deactivate) (venv-rmvirtualenv venv-tmp-env)))) (ert-deftest venv-mkvirtualenv-using-default-interpreter-works () (with-temp-location (venv-mkvirtualenv-using nil venv-tmp-env) (should (equal venv-current-name venv-tmp-env)) (venv-deactivate) (venv-rmvirtualenv venv-tmp-env))) (ert-deftest venv-mkvirtualenv-using-different-interpreter-works () (with-temp-location (venv-mkvirtualenv-using (executable-find "python") venv-tmp-env) (should (equal venv-current-name venv-tmp-env)) (venv-deactivate) (venv-rmvirtualenv venv-tmp-env))) (ert-deftest venv-mkvirtualenv-using-select-default-interpreter () (with-temp-location (with-simulated-input "RET" (let ((current-prefix-arg '(4))) (venv-mkvirtualenv-using "some invalid interpreter" venv-tmp-env))) (should (equal venv-current-name venv-tmp-env)) (venv-deactivate) (venv-rmvirtualenv venv-tmp-env))) (ert-deftest venv-mkvirtualenv-using-select-different-interpreter () (with-temp-location (with-simulated-input (concat (executable-find "python") " RET") (let ((current-prefix-arg '(4))) (venv-mkvirtualenv-using "some invalid interpreter" venv-tmp-env))) (should (equal venv-current-name venv-tmp-env)) (venv-deactivate) (venv-rmvirtualenv venv-tmp-env))) (ert-deftest venv-workon-works () (with-temp-env venv-tmp-env (venv-deactivate) (venv-workon venv-tmp-env) (assert-venv-activated))) (ert-deftest venv-deactivate-works () (with-temp-env venv-tmp-env (venv-deactivate) ;; M-x pdb should ask to run "pdb" (should (equal gud-pdb-command-name "pdb")) ;; we remove the name correctly (should (equal venv-current-name nil)) ;; we change the python path back (should (equal python-shell-virtualenv-path nil))) ;; we reset the PATH correctly (should (not (s-contains? venv-tmp-env (getenv "PATH")))) ;; we reset VIRTUAL_ENV (should (equal nil (getenv "VIRTUAL_ENV"))) ;; we remove out dir to exec-path (should (not (s-contains? venv-tmp-env (car exec-path))))) (ert-deftest venv-workon-errors-for-nonexistence () (should-error (venv-workon "i-hopefully-do-not-exist"))) (ert-deftest venv-list-virtualenvs-works () (with-temp-env venv-tmp-env (should (s-contains? venv-tmp-env (venv-list-virtualenvs))))) (ert-deftest venv-cdvirtualenv-works () (with-temp-env venv-tmp-env (let ((old-wd default-directory)) (unwind-protect (progn (venv-cdvirtualenv) (should (s-contains? venv-tmp-env default-directory))) (cd old-wd))))) (ert-deftest venv-cpvirtualenv-works () (with-temp-env venv-tmp-env (unwind-protect (progn (venv-cpvirtualenv venv-tmp-env "copy-of-tmp-env") (should (s-contains? "copy-of-tmp-env" (venv-list-virtualenvs)))) (venv-rmvirtualenv "copy-of-tmp-env")))) ;; tests for hooks (ert-deftest venv-activate-hooks () (let ((preactivate nil) (postactivate nil) (venv-preactivate-hook '((lambda () (setq preactivate "yes")))) (venv-postactivate-hook '((lambda () (setq postactivate "yes"))))) (with-temp-env venv-tmp-env (should (equal preactivate "yes")) (should (equal postactivate "yes"))))) (ert-deftest venv-mkvenv-hooks () (let ((venv-premkvirtualenv-hook '((lambda () (setq preactivated "yes")))) (venv-postmkvirtualenv-hook '((lambda () (setq postactivated "yes") (setq name venv-current-name))))) (with-temp-env venv-tmp-env (venv-deactivate) (should (equal preactivated "yes")) (should (equal postactivated "yes")) (should (equal name venv-tmp-env))))) (ert-deftest venv-set-location-works () (let ((expected-venv-location "test location") (original-venv-location venv-location)) (venv-set-location expected-venv-location) (should (equal venv-location expected-venv-location)) (setq venv-location original-venv-location))) (ert-deftest venv-projectile-auto-workon-works () (with-temp-env venv-tmp-env ;; the reason for setting a bogus venv-location here is that the ;; venv-location shouldn't matter, projectile-auto-workon should happen ;; indepedent of it's being set or not (let ((venv-location "bogus")) (noflet ((projectile-project-root () temporary-file-directory)) (setq venv-dirlookup-names (list venv-tmp-env)) (venv-deactivate) (venv-projectile-auto-workon) (assert-venv-activated))))) (ert-deftest venv-test-auto-cd-to-project-dir-works () (with-temp-env venv-tmp-env (with-temp-dir (venv-deactivate) (should (not (equal default-directory temp-dir))) ;; set the project dir to be `temp-dir' (append-to-file temp-dir nil (s-concat (venv-name-to-dir venv-tmp-env) ".project")) (venv-workon venv-tmp-env) ;; TODO should probably set these up to reset current-directory ;; when done for hygeine purposes (should (equal default-directory temp-dir))))) (ert-deftest venv-test-workon-does-not-cd-to-project-when-disabled () (with-temp-env venv-tmp-env (with-temp-dir (let ((venv-workon-cd nil)) (venv-deactivate) (should (not (equal default-directory temp-dir))) ;; set the project dir to be `temp-dir' (append-to-file temp-dir nil (s-concat (venv-name-to-dir venv-tmp-env) ".project")) (venv-workon venv-tmp-env) (should (not (equal default-directory temp-dir))))))) virtualenvwrapper.el-0.2.0/virtualenvwrapper.el0000644000175000017500000005216113434316244021675 0ustar dogslegdogsleg;;; virtualenvwrapper.el --- a featureful virtualenv tool for Emacs ;; Copyright (C) 2013 - 2015 James J Porter and [contributors](https://github.com/porterjamesj/virtualenvwrapper.el/graphs/contributors) ;; Author: James J Porter ;; URL: http://github.com/porterjamesj/virtualenvwrapper.el ;; Version: 20151123 ;; Keywords: python, virtualenv, virtualenvwrapper ;; Package-Requires: ((dash "1.5.0") (s "1.6.1")) ;;; Commentary: ;; A featureful virtualenv tool for Emacs. Emulates much of the ;; functionality of Doug Hellmann's ;; [virtualenvwrapper](https://bitbucket.org/dhellmann/virtualenvwrapper/) ;; See documentation at ;; https://github.com/porterjamesj/virtualenvwrapper.el for more details. ;;; Code: (require 'dash) (require 's) ;; needed to set gud-pdb-command-name (require 'gud) ;; customizable variables (defgroup virtualenvwrapper nil "Virtualenvwrapper for Emacs." :group 'python) (defcustom venv-virtualenv-command "virtualenv" "The command to use to run virtualenv." :type '(string) :group 'virtualenvwrapper) (defcustom venv-location (expand-file-name (or (getenv "WORKON_HOME") "~/.virtualenvs/")) "The location(s) of your virtualenvs. This can be either a string, which indicates a single directory in which you keep all your virtualenvs, or a list of strings, in which case it specifies disparate locations in which all your virtualenvs are kept. The default location is ~/.virtualenvs/, which is where your virtualenvs are stored if you use virtualenvwrapper in the shell." :group 'virtualenvwrapper) (defcustom venv-dirlookup-names '(".venv" "venv") "Virtualenvs to search in the projectile-project-root to activate when one of them is found." :type '(repeat file) :group 'virtualenvwrapper) (defcustom venv-workon-cd t "If set to t, cd to the virtualenv's project root when activating it. Analgous to the VIRTUALENVWRAPPER_WORKON_CD enviornment variable in the original virtualenvwrapper" :type '(boolean) :group 'virtualenvwrapper) ;; hooks (defvar venv-premkvirtualenv-hook nil "Hook run before creating a new virtualenv.") (defvar venv-postmkvirtualenv-hook nil "Hook run after creating a new virtualenv.") (defvar venv-prermvirtualenv-hook nil "Hook run before deleting a virtualenv.") (defvar venv-postrmvirtualenv-hook nil "Hook run after deleting a virtualenv.") (defvar venv-preactivate-hook nil "Hook run before a virtualenv is activated.") (defvar venv-postactivate-hook nil "Hook run after a virtualenv is activated.") (defvar venv-predeactivate-hook nil "Hook run before a virtualenv is deactivated.") (defvar venv-postdeactivate-hook nil "Hook run after a virtualenv is deactivated.") ;; internal variables that you probably shouldn't mess with (defvar venv-history nil "The history of venvs we have worked on.") (defvar venv-current-name nil "Name of current virtualenv.") (defvar venv-current-dir nil "Directory of current virtualenv.") (defvar venv-system-gud-pdb-command-name gud-pdb-command-name "Whatever `gud-pdb-command-name' is (usually \\[pdb]).") ;; copy from virtualenv.el (defvar venv-executables-dir (if (eq system-type 'windows-nt) "Scripts" "bin") "The name of the directory containing executables. It is system dependent.") ;;;###autoload (defun venv-projectile-auto-workon () "If a venv in the projetile root exists, activates it. Set your common venvs names in `venv-dirlookup-names'" (let ((path (--first (file-exists-p it) (--map (concat (projectile-project-root) it) venv-dirlookup-names)))) (when path (setq venv-current-name path) ;; there's really nothing that feels good to do here ;_; (venv--activate-dir path)))) ;; internal utility functions (defun venv--set-venv-gud-pdb-command-name () "When in a virtual env, call pdb as \\[python -m pdb]." (setq gud-pdb-command-name "python -m pdb")) (defun venv--set-system-gud-pdb-command-name () "Set the system \\[pdb] command." (setq gud-pdb-command-name venv-system-gud-pdb-command-name)) (defun venv-clear-history () (setq venv-history nil)) (defun venv-dir-to-name (dir) "Extract the name of a virtualenv from a path." (car (last (--filter (not (s-blank? it)) (s-split "/" dir))))) (defun venv-name-to-dir (name) "Given the name of a virtualenv, translate it to the directory where that virtualenv is located." (file-name-as-directory (let ((potential-dir (if (stringp venv-location) (concat (file-name-as-directory (expand-file-name venv-location)) name) (car (-filter (lambda (d) (s-equals? name (venv-dir-to-name d))) venv-location))))) (if (and potential-dir (file-exists-p (concat (file-name-as-directory (expand-file-name potential-dir)) venv-executables-dir))) (file-name-as-directory (expand-file-name potential-dir)) (error (concat "No such virtualenv: " name)))))) (defun venv-get-candidates () "Wrapper to call get-candidates-list or get-candidates-string depending on which is appropriate for how venv-location is specified." (let ((candidates (if (stringp venv-location) (venv-get-candidates-dir venv-location) (venv-get-candidates-list venv-location)))) (when (not (eq (length (-distinct candidates)) (length candidates))) (error "Some virtualenvs have the same name!")) candidates)) (defun venv-get-candidates-list (list) "Given LIST of virtualenv directories, return a list of names that can be used in, e.g. a completing read. This trusts the caller to only pass directories with are actually virtualenvs." (-map (lambda (dir) (car (last (-filter (lambda (s) (not (s-blank? s))) (s-split "/" dir))))) (-filter (lambda (s) (car (file-attributes (concat (file-name-as-directory (expand-file-name s)) venv-executables-dir)))) list))) (defun venv-get-candidates-dir (dir) "Given a directory DIR containing virtualenvs, return a list of names that can be used in the completing read." (let ((proper-dir (file-name-as-directory (expand-file-name dir)))) (-filter (lambda (s) (let ((subdir (concat proper-dir s))) (car (file-attributes (concat (file-name-as-directory subdir) venv-executables-dir))))) (directory-files proper-dir nil "^[^.]")))) (defun venv-get-stripped-path (path) "Return what the PATH would look like if we weren't in a virtualenv. PATH should be a list of strings specifiying directories." (-filter (lambda (s) (not (s-equals? s (concat venv-current-dir venv-executables-dir)))) path)) (defun venv--purge-history (candidates) "Remove history candidates that are not present in the list CANDIDATES" (setq venv-history (-filter (lambda (s) (-contains? candidates s)) venv-history))) (defun venv-is-valid (name) "Test if NAME is a valid virtualenv specifier" (-contains? (venv-get-candidates) name)) (defun venv-read-name (prompt) "Do a completing read to get the name of a candidate, prompting the user with the string PROMPT" (let ((candidates (venv-get-candidates))) ;; purge history of no longer existant candidates first (venv--purge-history candidates) (completing-read prompt candidates nil t nil 'venv-history (or (car venv-history) (car candidates))))) (defun venv-list-virtualenvs () (s-join "\n" (venv-get-candidates))) (defun venv--activate-dir (dir) "Given a directory corresponding to a virtualenv, activate it" (run-hooks 'venv-preactivate-hook) (setq venv-current-dir (file-name-as-directory dir)) ;; setup the python shell (setq python-shell-virtualenv-path venv-current-dir) ;; setup emacs exec-path (add-to-list 'exec-path (concat venv-current-dir venv-executables-dir)) ;; setup the environment for subprocesses (let ((path (concat venv-current-dir venv-executables-dir path-separator (getenv "PATH")))) (setenv "PATH" path) ;; keep eshell path in sync (setq eshell-path-env path)) (setenv "VIRTUAL_ENV" venv-current-dir) (if venv-workon-cd (venv--switch-to-project-dir)) (venv--set-venv-gud-pdb-command-name) (run-hooks 'venv-postactivate-hook)) (defun venv--switch-to-project-dir () "If we find the project file, cd into that directory" (let ((proj-file (expand-file-name ".project" venv-current-dir))) (when (file-exists-p proj-file) (cd (with-temp-buffer (insert-file-contents proj-file) (string-trim (buffer-string))))))) ;; potentially interactive user-exposed functions ;;;###autoload (defun venv-deactivate () "Deactivate the current venv." (interactive) (run-hooks 'venv-predeactivate-hook) (setq python-shell-virtualenv-path nil) (setq exec-path (venv-get-stripped-path exec-path)) (setenv "PATH" (s-join path-separator (venv-get-stripped-path (s-split path-separator (getenv "PATH"))))) (setenv "VIRTUAL_ENV" nil) (setq venv-current-name nil) (setq venv-current-dir nil) (setq eshell-path-env (getenv "PATH")) (venv--set-system-gud-pdb-command-name) (run-hooks 'venv-postdeactivate-hook) (when (called-interactively-p 'interactive) (message "virtualenv deactivated"))) ;;;###autoload (defun venv-set-location (&optional location) "Set where to look for virtual environments to LOCATION. This is useful e.g. when using tox." (interactive) (when (not location) (setq location (read-directory-name "New virtualenv location: " venv-location))) (venv-deactivate) (setq venv-location location) (when (called-interactively-p 'interactive) (message (concat "Virtualenv location: " location)))) ;;;###autoload (defun venv-workon (&optional name) "Interactively switch to virtualenv NAME. Prompts for name if called interactively." (interactive) ;; if without argument, read from user (unless name (setq name (venv-read-name (if venv-current-name (format "Choose a virtualenv (currently %s): " venv-current-name) "Choose a virtualenv: ")))) ;; validate name (when (not (venv-is-valid name)) (error (format "Invalid virtualenv %s specified!" name))) ;; then deactivate (venv-deactivate) ;; then switch (setq venv-current-name name) ;; push it onto the history (add-to-list 'venv-history venv-current-name) ;; actually activate it (venv--activate-dir (venv-name-to-dir venv-current-name)) (when (called-interactively-p 'interactive) (message (concat "Switched to virtualenv: " venv-current-name)))) ;; for hilarious reasons to do with bytecompiling, this has to be here ;; instead of below (defmacro venv-with-virtualenv (name &rest forms) "Evaluate FORMS with venv NAME active. NAME must be a string identifying a virtualenv." `(progn (let ((prev-dir default-directory) (prev-env venv-current-name)) (venv-workon ,name) ;; switch it up (cd venv-current-dir) (unwind-protect (progn ,@forms) ;; evalulate forms (if prev-env ;; switch back (venv-workon prev-env) (venv-deactivate)) (cd prev-dir))))) (defun venv--check-executable () "Verify that there is a virtualenv executable available, throwing an error if not" (unless (executable-find venv-virtualenv-command) (error "There doesn't appear to be a virtualenv executable on your exec path. Ensure that you have virtualenv installed and that the exec-path variable is set such that virtualenv can be found. A common cause of problems like this is GUI Emacs not having environment variables set up like the shell. Check out https://github.com/purcell/exec-path-from-shell for a robust solution to this problem."))) (defun venv-get-python-executable () "Do a completing read for a python executable to use in mkvirtualenv" ) ;;;###autoload (defun venv-mkvirtualenv-using (interpreter &rest names) "Create new virtualenvs NAMES using INTERPRETER. If venv-location is a single directory, the new virtualenvs are made there; if it is a list of directories, the new virtualenvs are made in the current `default-directory'." (interactive) (venv--check-executable) (let* ((foo (if current-prefix-arg (read-string "Python executable: ") interpreter)) (parent-dir (if (stringp venv-location) (file-name-as-directory (expand-file-name venv-location)) default-directory)) (python-exe-arg (when foo (concat "--python=" foo))) (names (if names names (list (read-from-minibuffer "New virtualenv: "))))) ;; map over all the envs we want to make (--each names ;; error if this env already exists (when (-contains? (venv-get-candidates) it) (error "A virtualenv with this name already exists!")) (run-hooks 'venv-premkvirtualenv-hook) (shell-command (concat venv-virtualenv-command " " python-exe-arg " " parent-dir it)) (when (listp venv-location) (add-to-list 'venv-location (concat parent-dir it))) (venv-with-virtualenv it (run-hooks 'venv-postmkvirtualenv-hook)) (when (called-interactively-p 'interactive) (message (concat "Created virtualenv: " it)))) ;; workon the last venv we made (venv-workon (car (last names))))) ;;;###autoload (defun venv-mkvirtualenv (&rest names) "Create new virtualenvs NAMES. If venv-location is a single directory, the new virtualenvs are made there; if it is a list of directories, the new virtualenvs are made in the current `default-directory'." (interactive) (apply #'venv-mkvirtualenv-using nil names)) ;;;###autoload (defun venv-rmvirtualenv (&rest names) "Delete virtualenvs NAMES." (interactive) ;; deactivate first (venv-deactivate) ;; check validity and read names if necessary (if names (--map (when (not (venv-is-valid it)) (error "Invalid virtualenv specified!")) names) (setq names (list (venv-read-name "Virtualenv to delete: ")))) ;; map over names, deleting the appropriate directory (--each names (run-hooks 'venv-prermvirtualenv-hook) (delete-directory (venv-name-to-dir it) t) ;; get it out of the history so it doesn't show up in completing reads (setq venv-history (-filter (lambda (s) (not (s-equals? s it))) venv-history)) ;; if location is a list, delete it from the list (when (listp venv-location) (setq venv-location (-filter (lambda (locs) (not (s-equals? it (venv-dir-to-name locs)))) venv-location))) (run-hooks 'venv-postrmvirtualenv-hook) (when (called-interactively-p) (message (concat "Deleted virtualenv: " it))))) ;;;###autoload (defun venv-lsvirtualenv () "List all available virtualenvs in a temp buffer." (interactive) (with-output-to-temp-buffer "*Virtualenvs*" (princ (venv-list-virtualenvs)))) ;;;###autoload (defun venv-cdvirtualenv (&optional subdir) "Change to the directory of current virtualenv. If SUBDIR is passed, append that to the path such that we are immediately in that directory." (interactive) (if venv-current-dir (let ((going-to (concat (file-name-as-directory (expand-file-name venv-current-dir)) subdir))) (cd going-to) (when (called-interactively-p 'interactive) (message (concat "Now in directory: " going-to)))) (error "No virtualenv is currently active."))) ;;;###autoload (defun venv-cpvirtualenv (&optional name newname) "Copy virtualenv NAME to NEWNAME. Any arguments not passed will be prompted for This comes with the same caveat as cpvirtualenv in the original virtualenvwrapper, which is that is far from guarenteed to work well. Many packages hardcode absolute paths in various places an will break if moved to a new location. Use with caution. If used with a single virtualenv directory, behaves just like cpvirtualenv in virtualenvwrapper.sh. If used with virtualenvs spread around the filesystem, creates the new virtualenv in the current default directory." (interactive) (let ((parent-dir (if (stringp venv-location) (file-name-as-directory (expand-file-name venv-location)) default-directory))) (when (not name) (setq name (venv-read-name "Virtualenv to copy from: "))) (when (not newname) (setq newname (read-from-minibuffer "Virtualenv to copy to: "))) ;; throw an error if newname already exists (when (file-exists-p (concat parent-dir newname)) (error "A virtualenv with the proposed name already exists!")) ;; make the copy (copy-directory (venv-name-to-dir name) (concat parent-dir newname)) ;; if the location specifier is a list, add to it. (when (listp venv-location) (add-to-list 'venv-location (concat parent-dir newname))) (when (called-interactively-p 'interactive) (message (format "Copied virtualenv %s to %s" name newname))) (venv-workon newname))) ;; macros and functions supporting executing elisp or ;; shell commands in a particular venv (defmacro venv-allvirtualenv (&rest forms) "For each virtualenv, activate it, switch to its directory, and then evaluate FORMS." `(progn (--each (venv-get-candidates) (venv-with-virtualenv it ,@forms)))) (defun venv-with-virtualenv-shell-command (name command) "Execute the string COMMAND in virtualenv NAME." (venv-with-virtualenv name (shell-command command))) (defun venv-allvirtualenv-shell-command (&optional command) "Just like venv-allvirtulenv, but executes a shell command (COMMAND) rather than elisp forms." (interactive) (when (not command) (setq command (read-from-minibuffer "Shell command to execute: "))) (-map (lambda (name) (venv-with-virtualenv-shell-command name command)) (venv-get-candidates)) (message (concat "Executed " command " in all virtualenvs"))) ;; Code for setting up interactive shell and eshell ;; interactive shell ;;;###autoload (defun venv-shell-init (process) "Activate the current virtualenv in a newly opened shell." (comint-send-string process (concat "if command -v workon >/dev/null 2>&1; then workon " venv-current-name "; else source " venv-current-dir venv-executables-dir "/activate; fi \n"))) ;;;###autoload (defun venv-initialize-interactive-shells () "Configure interactive shells for use with virtualenvwrapper.el." (defadvice shell (around strip-env ()) "Use the environment without the venv to start up a new shell." (let* ((buffer-name (or buffer "*shell*")) (buffer-exists-already (get-buffer buffer-name))) (if (or buffer-exists-already (not venv-current-name)) ad-do-it (progn (setenv "PATH" (s-join path-separator (venv-get-stripped-path (s-split path-separator (getenv "PATH"))))) (setenv "VIRTUAL_ENV" nil) ad-do-it (venv-shell-init buffer-name) (setenv "PATH" (concat venv-current-dir venv-executables-dir path-separator (getenv "PATH"))) (setenv "VIRTUAL_ENV" venv-current-dir))))) (ad-activate 'shell)) ;; eshell (eval-and-compile (defun venv--gen-fun (command) `(defun ,(intern (format "pcomplete/eshell-mode/%s" command)) () (pcomplete-here* (venv-get-candidates))))) (defmacro venv--make-pcompletions (commands) `(progn ,@(-map #'venv--gen-fun commands))) ;;;###autoload (defun venv-initialize-eshell () "Configure eshell for use with virtualenvwrapper.el." ;; make emacs and eshell share an environment (setq eshell-modify-global-environment t) ;; set eshell path (setq eshell-path-env (getenv "PATH")) ;; alias functions (defun eshell/workon (arg) (venv-workon arg)) (defun eshell/deactivate () (venv-deactivate)) (defun eshell/rmvirtualenv (&rest args) (apply #'venv-rmvirtualenv args)) (defun eshell/mkvirtualenv (&rest args) (apply #'venv-mkvirtualenv args)) (defun eshell/cpvirtualenv (&rest args) (apply #'venv-cpvirtualenv args)) (defun eshell/cdvirtualenv (&optional arg) (venv-cdvirtualenv arg)) (defun eshell/lsvirtualenv () (venv-list-virtualenvs)) (defun eshell/allvirtualenv (&rest command) (venv-allvirtualenv-shell-command (s-join " " (eshell-stringify-list command)))) ;; make completions work (venv--make-pcompletions ("workon" "rmvirtualenv" "cdvirtualenv" "cpvirtualenv")) (message "Eshell virtualenv support initialized.")) (provide 'virtualenvwrapper) ;;; virtualenvwrapper.el ends here