136 Commits

Author SHA1 Message Date
Yamashita Yuu
ea18a1b283 v20140615 (fixes #26) 2014-06-15 20:20:29 +09:00
Yamashita Yuu
dcb5371f07 Merge branch 'workaround-for-pyenv-which-ext' 2014-06-15 20:16:25 +09:00
Yamashita Yuu
56a3225d6c Merge branch 'issue26' 2014-06-15 20:16:09 +09:00
Yamashita Yuu
dc12a345d8 Workaround for the issue with pyenv-which-ext (refs #26) 2014-06-15 18:36:21 +09:00
Yamashita Yuu
d9e3adc108 Performe as same as before v20140614 if pyenv virtualenv-init is not configured (#26) 2014-06-15 16:43:50 +09:00
Yamashita Yuu
4a6ef11349 v20140614 2014-06-14 14:45:57 +09:00
Yamashita Yuu
21e999cc7c Fix deactivation issue with system version
$ pyenv global system
    $ pyenv shell venv
    (venv) $ pyenv shell --unset
    (venv) $ pyenv version
    system (set by /home/yamashita/.pyenv/version)
    (venv) $ pyenv which python
    /home/yamashita/.pyenv/versions/venv/bin/python # <-- should be `/usr/bin/python` (system)
2014-06-13 19:30:37 +09:00
Yamashita Yuu
e853f74b26 Fix test cases around create_symlinks 2014-06-12 20:35:46 +09:00
Yamashita Yuu
fe5e69846e Create symlinks for executables with version suffix 2014-06-12 20:35:44 +09:00
Yamashita Yuu
c17e4ec9a4 style 2014-06-05 19:31:10 +09:00
Yamashita Yuu
7828a15066 Use pyenv-virtualenv-init to show caveats 2014-06-05 00:17:06 +09:00
Yamashita Yuu
55a2a9e482 Read shell profile from $TMP during tests 2014-06-05 00:16:41 +09:00
Yamashita Yuu
b258bfa907 Display notice about virtualenv-init in activate 2014-06-05 00:08:21 +09:00
Yamashita, Yuu
8838bae3bc Merge pull request #24 from yyuu/automatic-activation
Automatic activation
2014-06-04 20:51:05 +09:00
Yamashita Yuu
b7535395e8 Update README 2014-06-04 20:48:22 +09:00
Yamashita Yuu
6761ed09cd Add test for pyenv-virtualenv-init 2014-06-04 20:48:22 +09:00
Yamashita Yuu
8a837758dc activate: invoke pyenv shell only if the arguments passed 2014-06-04 15:24:36 +09:00
Yamashita Yuu
ea120da0c5 fish: Suppress annoying stderr outputs from pyenv-sh-activate 2014-06-04 15:24:34 +09:00
Yamashita Yuu
af6c157a79 Add eval "$(pyenv virtualenv-init -)" to setup _precmd_ for user's shell 2014-06-04 15:24:12 +09:00
Yamashita Yuu
c9043920db v20140602 2014-06-02 21:42:27 +09:00
Yamashita Yuu
26f2004abb Add semicolon for the deactivate scripts for non-fish 2014-06-02 16:02:39 +09:00
Yamashita, Yuu
b9e2fc6acb Merge pull request #23 from fgimian/fish-activation-fix
Repaired virtualenv activation and deactivation for the fish shell
2014-05-31 14:19:02 +09:00
Fotis Gimian
21a8745bcf Updated tests to correctly pass for fish shell bug fix 2014-05-31 15:15:45 +10:00
Fotis Gimian
f7da0c5808 Repaired virtualenv activation and deactivation for the fish shell 2014-05-31 12:29:19 +10:00
Yamashita Yuu
3f5171e2fc Use new style GH raw url to avoid redirects (raw.github.com -> raw.githubusercontent.com) 2014-04-30 11:52:15 +09:00
Yamashita Yuu
579f4784c8 v20140421 (fixes #19) 2014-04-21 20:24:25 +09:00
Yamashita Yuu
a8940e2f85 Merge branch 'pyvenv-with-p' 2014-04-20 15:27:41 +09:00
Yamashita Yuu
2cab533652 Use virtualenv instead of pyvenv if -p has given (yyuu/pyenv#158) 2014-04-20 15:24:40 +09:00
Yamashita Yuu
5033c3ba0d Update README about pyenv activate --unset (#15) 2014-02-03 16:13:41 +09:00
Yamashita Yuu
a958640776 Fix completion of pyenv activate (fixes #15) 2014-02-03 14:49:47 +09:00
Yamashita Yuu
503a51564e Display error if pyenv activate was invoked as a command 2014-01-25 19:14:58 +09:00
Yamashita Yuu
1db565b67c Update README.
Restart shell after installation of the plugin to enable `pyenv-sh-*`
commands.
2014-01-25 18:39:29 +09:00
Yamashita Yuu
1d291c7c4c v20140123 2014-01-23 19:09:40 +09:00
Yamashita Yuu
f8e1d9c57b Add tests for pyenv-virtualenvs 2014-01-23 15:14:01 +09:00
Yamashita Yuu
4feb64e8d3 Display the version of pyvenv if USE_PYVENV is set 2014-01-23 14:51:56 +09:00
Yamashita Yuu
39ce4e76fd Update change log 2014-01-22 22:01:27 +09:00
Yamashita Yuu
60ddcd8628 Prefer https than git when pulling from GH 2014-01-22 20:16:51 +09:00
Yamashita Yuu
b5b0cd6c72 Merge branch 'unset-pip-require-venv' 2014-01-22 20:10:01 +09:00
Yamashita Yuu
826a7e1a37 Unset PIP_REQUIRE_VENV before executing pip install. (#10) 2014-01-22 20:08:11 +09:00
Yamashita Yuu
a40cbc8acd Add tests for pyenv-virtualenv-prefix 2014-01-21 01:27:03 +09:00
Yamashita Yuu
d0685addc1 Add tests for activate and deactivate 2014-01-21 00:37:33 +09:00
Yamashita Yuu
802cd6b2fa Add .gitignore 2014-01-21 00:04:48 +09:00
Yamashita Yuu
e0012d8aa5 Add build status badge 2014-01-18 18:22:51 +09:00
Yamashita Yuu
87b2f7c6a2 Merge branch 'unittest' 2014-01-18 18:21:11 +09:00
Yamashita Yuu
5e36ec7b51 Add .travis.yml 2014-01-18 18:21:01 +09:00
Yamashita Yuu
dcd3b12a97 Add unittest 2014-01-18 18:19:53 +09:00
Yamashita Yuu
2aa207ede0 Add prepare_upgrade and upgrade 2014-01-18 18:04:59 +09:00
Yamashita Yuu
1cbcee3fc8 Add HAS_VIRTUALENV, HAS_PYVENV and USE_PYVENV 2014-01-18 18:04:45 +09:00
Yamashita Yuu
e7cc57acb8 Preserve original IFS 2014-01-18 18:01:34 +09:00
Yamashita Yuu
dc7b6abfde Display messages only on verbose mode 2014-01-18 18:00:59 +09:00
Yamashita Yuu
88c9633415 Merge branch 'activate' (fixes #9) 2014-01-17 23:24:03 +09:00
Yamashita Yuu
64b2545d5e Hide useless output from pyenv-virtualenv-prefix 2014-01-17 22:43:03 +09:00
Yamashita Yuu
f9d3984909 Hide output from declare -f deactivate 2014-01-17 22:39:37 +09:00
Yamashita Yuu
08951ea22d update README about pyenv activate 2014-01-17 20:26:49 +09:00
Yamashita Yuu
85959c735d Add sh commands to activate/deactivate virtualenv into current shell 2014-01-17 20:17:15 +09:00
Yamashita Yuu
4e61656e9d Modify error messages 2014-01-17 20:17:05 +09:00
Yamashita Yuu
c7da5d53bb Should not exit with error if virtualenv_is_pyvenv returns with non-zero (fixes #8) 2014-01-17 11:39:26 +09:00
Yamashita Yuu
0c2cf9f439 Merge branch 'ensurepip' 2014-01-16 18:33:39 +09:00
Yamashita Yuu
fc01a3d475 Use ensurepip if available 2014-01-16 18:33:02 +09:00
Yamashita Yuu
6e0798ea10 Merge branch 'release-20140110.1' 2014-01-10 15:36:57 +09:00
Yamashita Yuu
6b712a95cb v20140110.1 2014-01-10 15:36:42 +09:00
Yamashita Yuu
95cc31ba6b Import changes from install script of ruby-build v20131225.1 2014-01-10 15:35:10 +09:00
Yamashita Yuu
cebd326821 Merge branch 'release-20140110' 2014-01-10 11:36:08 +09:00
Yamashita Yuu
fa9950aa5b v20140110 2014-01-10 11:35:19 +09:00
Yamashita Yuu
124645e84c Use ez_setup.py and get_pip.py on filesystem if exist 2014-01-09 18:46:44 +09:00
Yamashita Yuu
ec60d5dfcb Support a short option of virtualenv: -p 2014-01-09 11:19:39 +09:00
Yamashita Yuu
d96a00ece3 v20131216 2013-12-16 13:58:10 +09:00
Yamashita, Yuu
20e31ed587 Merge pull request #3 from eikonomega/patch-1
Update README.md
2013-09-03 19:22:37 -07:00
Michael Dunn
32bfce2e41 Update 'Using pyenv virtualenv with pyenv' section.
Explicitly define the parameters that you pass into 'pyenv virtualenv' and what they stand for.
2013-09-03 17:15:53 -04:00
Michael Dunn
99de2882e3 Update README.md
Make it explicit that the 'installing as a plugin' section is assuming that you've installed pyenv in the standard location and what to do if you've done something else.  Clarify the language a little bit about checking out a specific release vs. pulling the current development version.
2013-09-03 17:10:23 -04:00
Yamashita Yuu
39bac37090 Add new installation method. pyenv-virtualenv is now a part of Homebrew! 2013-08-06 22:58:18 +09:00
Yamashita Yuu
a4ce544798 Remove embeded version string of setuptools/pip. 2013-07-17 16:03:31 +09:00
Yamashita Yuu
16a4a8d37e Update setuptools (0.8 -> 0.9.5) 2013-07-17 15:52:32 +09:00
Yamashita Yuu
8d4b901250 update setuptools (0.7.7 -> 0.8) 2013-07-08 16:30:07 +09:00
Yamashita Yuu
8363a4f0e0 update setuptools (0.7.4 -> 0.7.7) 2013-07-05 11:16:02 +09:00
Yamashita Yuu
79c4e62886 v20130622 2013-06-22 10:30:00 +09:00
Yamashita Yuu
eee0e2bea8 update README 2013-06-21 19:49:36 +09:00
Yamashita Yuu
2f20c56b1d remove redundant checks for pyenv-prefix 2013-06-21 19:42:46 +09:00
Yamashita Yuu
77f9c1502d like pyenv versions, show "*" on the start of line if the environment
is enabled
2013-06-21 19:40:59 +09:00
Yamashita Yuu
327b8b7615 do not create python executable in pyenv-virtualenv
it will be created by `virtualenv` or `pyvenv`
2013-06-20 17:53:45 +09:00
Yamashita Yuu
8eecec0d7d Merge branch 'pyvenv' 2013-06-20 17:47:52 +09:00
Yamashita Yuu
23a115c12a set STATUS as non-zero even if the setuptools or pip have failed to
install into pyvenv
2013-06-20 16:25:54 +09:00
Yamashita Yuu
f3c5a64281 renamed virtualenv() to venv() 2013-06-20 14:56:24 +09:00
Yamashita Yuu
384fab86ef add EZ_SETUP_URL and GET_PIP_URL 2013-06-20 14:50:20 +09:00
Yamashita Yuu
f0ca1b27d0 add SETUPTOOLS_VERSION and PIP_VERSION 2013-06-20 13:48:01 +09:00
Yamashita Yuu
2e5db090a9 install setuptools and pip into pyvenv 2013-06-20 12:23:46 +09:00
Yamashita Yuu
11792cbaf9 Remove --pyvenv option.
Use pyvenv only if virtualenv is not installed and there is pyvenv.
2013-06-20 11:59:54 +09:00
Yamashita Yuu
6e7945a626 prefer pyvenv rather than virtualenv if --pyvenv is given 2013-06-19 23:57:45 +09:00
Yamashita Yuu
1db9840620 check existence of ./bin/activate to detect virtualenvs to support pyvenv 2013-06-19 23:39:26 +09:00
Yamashita Yuu
212021c322 update README 2013-06-18 23:55:48 +09:00
Yamashita Yuu
7507cb1e41 apply $QUIET and $VERBOSE both pip and virtualenv 2013-06-18 23:39:43 +09:00
Yamashita Yuu
7a54a83392 fix --help option 2013-06-18 23:34:04 +09:00
Yamashita Yuu
551956f017 improve arguments handling 2013-06-18 23:26:30 +09:00
Yamashita Yuu
1ad6788e84 Merge branch 'remove-bundled-virtualenv.py' 2013-06-18 19:34:27 +09:00
Yamashita Yuu
f78a393864 fixed broken reference for "${PYTHON_BIN}" 2013-06-18 14:54:11 +09:00
Yamashita Yuu
a4de1d7e89 Removed bundled virtualenv.py script. Now pyenv-virtualenv installs
`virtualenv` package into source version and then use it.
2013-06-18 12:21:16 +09:00
Yamashita Yuu
f2bd87fc3e support virtualenv's short option of "-q" 2013-06-15 12:42:30 +09:00
Yamashita Yuu
2a5a483c5d v20130614 2013-06-14 21:31:24 +09:00
Yamashita Yuu
a2c878f284 remove --distribute from example.
it will be replaced by new setuptools in near future.
2013-06-04 20:32:22 +09:00
Yamashita Yuu
b7c498ce03 add pyenv-virtualenv-prefix 2013-06-04 18:56:58 +09:00
Yamashita Yuu
6915cd0320 no need to import print_function in this case 2013-06-04 17:52:10 +09:00
Yamashita Yuu
4263783195 add pyenv virtualenvs command 2013-06-04 17:07:48 +09:00
Yamashita Yuu
65b58770a7 display package list on unsuccessful upgrade 2013-05-28 21:07:35 +09:00
Yamashita Yuu
66546cea12 ignore stderr from pyenv-exec 2013-05-28 16:54:25 +09:00
Yamashita Yuu
7562795c42 test the existence of ${VIRTUALENV_PIP_BIN} before invoking it 2013-05-28 16:30:39 +09:00
Yamashita Yuu
72592fde6b Merge branch 'upgrade-virtualenv' 2013-05-28 13:44:37 +09:00
Yamashita Yuu
8ac27a7a77 add --upgrade option to upgrade existing virtualenv 2013-05-28 13:43:17 +09:00
Yamashita Yuu
f9532f6c4e load hooks just before creating virtualenv 2013-05-28 13:37:32 +09:00
Yamashita Yuu
67d6cfc0d2 handle --force option expectedly 2013-05-27 23:50:13 +09:00
Yamashita Yuu
695c2fd618 v20130527 2013-05-27 20:26:42 +09:00
Yamashita Yuu
bc433be75c update README 2013-05-27 20:25:57 +09:00
Yamashita Yuu
16470ec39c Unset environment variables which starts with VIRTUALENV_. These variables are reserved for virtualenv. 2013-05-08 15:51:09 +09:00
Yamashita Yuu
c8b9969619 (1) fix handling of hooks
(2) remove $VIRTUALENV_PATH after failed creation
2013-05-08 14:28:56 +09:00
Yamashita Yuu
3b7be75458 restore missing VIRTUALENV_PYTHON_BIN 2013-05-08 14:08:26 +09:00
Yamashita Yuu
404918c7d9 Merge branch 'virtualenv-install' 2013-05-07 22:56:32 +09:00
Yamashita Yuu
20e78d8ffa update README 2013-05-07 22:54:43 +09:00
Yamashita Yuu
db37bb9643 Download virtualenv.py if specified version has not been installed. 2013-05-07 22:31:12 +09:00
Yamashita Yuu
a9903fab6e make installation path of virtualenv.py configurable 2013-05-07 21:51:12 +09:00
Yamashita Yuu
b594b99a18 s/VIRTUALENV_CACHE_PATH/PYENV_&/g 2013-05-07 21:26:28 +09:00
Yamashita Yuu
9e438ee3e2 Change the installation path of the virtualenv.py script. 2013-05-07 21:22:51 +09:00
Yamashita Yuu
1389d840d1 Remove python-virtualenv which was no longer used. 2013-05-07 21:15:07 +09:00
Yamashita Yuu
78af7ec9d4 release v20130507 2013-05-07 19:55:31 +09:00
Yamashita Yuu
5b357d2862 display virtualenv's help message on '--help' 2013-05-01 13:07:49 +09:00
Yamashita Yuu
5baa5084dc don't capture '-v' option in $VERBOSE to pass it to virtualenv 2013-05-01 13:05:32 +09:00
Yamashita Yuu
0f8477fcf5 store distribute/setuptools archive in $PYTHON_BUILD_CACHE_PATH to reuse
them between invocations.
2013-04-30 19:55:49 +09:00
Yamashita Yuu
aec759a0e0 ignore error on checking virtualenv version 2013-04-26 13:03:35 +09:00
Yamashita Yuu
15608701ae show virtualenv version in pyenv virtualenv --version 2013-04-25 22:10:29 +09:00
Yamashita Yuu
d927b2e137 update virtualenv version (1.8.4 -> 1.9.1) 2013-04-25 21:45:44 +09:00
Yamashita Yuu
ebeb3dcb71 fix wrong path for libexec 2013-03-27 15:15:44 +09:00
Yamashita Yuu
8b1ae9f19a update README. add note about pyenv-virtualenvwrapper. 2013-03-22 15:38:08 +09:00
Yamashita Yuu
003ea01b97 release v20130307 2013-03-07 13:12:17 +09:00
Yamashita Yuu
1a2d06bc05 update copyright 2013-02-23 19:09:49 +09:00
Yamashita Yuu
5ed1988b34 update README 2013-02-23 16:46:09 +09:00
Yamashita Yuu
c65e8b266b update README 2013-02-20 18:43:07 +09:00
Yamashita Yuu
6ee4bf1e5c Update virtualenv version; 1.8.2 -> 1.8.4 2013-02-18 13:16:10 +09:00
Yamashita Yuu
2910f87cb6 Preparing for renaming project; s/python-virtualenv/pyenv-virtualenv/g 2013-02-18 13:14:42 +09:00
28 changed files with 2086 additions and 2696 deletions

8
.gitignore vendored
View File

@@ -1,6 +1,6 @@
/libexec/*.class
/libexec/*.pyc
/libexec/*.pyo
/libexec/__pycache__
/libexec/pyenv-virtualenv/*/*.class
/libexec/pyenv-virtualenv/*/*.pyc
/libexec/pyenv-virtualenv/*/*.pyo
/libexec/pyenv-virtualenv/*/__pycache__
*.swo
*.swp

6
.travis.yml Normal file
View File

@@ -0,0 +1,6 @@
install: git clone https://github.com/sstephenson/bats.git
script: bats/bin/bats --tap test
language: c
notifications:
email:
on_success: never

View File

@@ -1,5 +1,4 @@
Copyright (c) 2012 Yamashita, Yuu
Copyright (c) 2011 Sam Stephenson
Copyright (c) 2013 Yamashita, Yuu
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the

209
README.md
View File

@@ -1,61 +1,203 @@
# python-virtualenv
# pyenv-virtualenv
python-virtualenv is a [pyenv](https://github.com/yyuu/pyenv) plugin
that provides an `pyenv virtualenv` command to create virtualenv for Python
[![Build Status](https://travis-ci.org/yyuu/pyenv-virtualenv.png)](https://travis-ci.org/yyuu/pyenv-virtualenv)
pyenv-virtualenv is a [pyenv](https://github.com/yyuu/pyenv) plugin
that provides a `pyenv virtualenv` command to create virtualenv for Python
on UNIX-like systems.
(NOTICE: If you are an existing user of [virtualenvwrapper](http://pypi.python.org/pypi/virtualenvwrapper)
and you love it, [pyenv-virtualenvwrapper](https://github.com/yyuu/pyenv-virtualenvwrapper) may help you
to manage your virtualenvs.)
## Installation
### Installing as a pyenv plugin (recommended)
### Installing as a pyenv plugin
Installing python-virtualenv as a pyenv plugin will give you access to the
`pyenv virtualenv` command.
This will install the latest development version of pyenv-virtualenv into
the `~/.pyenv/plugins/pyenv-virtualenv` directory.
$ mkdir -p ~/.pyenv/plugins
$ cd ~/.pyenv/plugins
$ git clone git://github.com/yyuu/python-virtualenv.git
**Important note:** If you installed pyenv into a non-standard directory, make sure that you clone this
repo into the 'plugins' directory of wherever you installed into.
This will install the latest development version of python-virtualenv into
the `~/.pyenv/plugins/python-virtualenv` directory. From that directory, you
can check out a specific release tag. To update python-virtualenv, run `git
pull` to download the latest changes.
From inside that directory you can:
- Check out a specific release tag.
- Get the latest development release by running `git pull` to download the latest changes.
### Installing as a standalone program (advanced)
1. **Check out pyenv-virtualenv into plugin directory**
Installing python-virtualenv as a standalone program will give you access to
the `python-virtualenv` command for precise control over Python version
installation. If you have rbenv installed, you will also be able to
use the `rbenv install` command.
$ git clone https://github.com/yyuu/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv
$ git clone git://github.com/yyuu/python-virtualenv.git
$ cd python-virtualenv
$ ./install.sh
2. **Add `pyenv virtualenv-init` to your shell** to enable activation of virtualenv
This will install python-virtualenv into `/usr/local`. If you do not have
write permission to `/usr/local`, you will need to run `sudo
./install.sh` instead. You can install to a different prefix by
setting the `PREFIX` environment variable.
$ echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bash_profile
**Zsh note**: Modify your `~/.zshenv` file instead of `~/.bash_profile`.
3. **Restart your shell to the enable pyenv-virtualenv**
$ exec "$SHELL"
### Installing with Homebrew (for OS X users)
Mac OS X users can install pyenv-virtualenv with the
[Homebrew](http://brew.sh) package manager. This
will give you access to the `pyenv-virtualenv` command. If you have pyenv
installed, you will also be able to use the `pyenv virtualenv` command.
*This is recommended method of installation if you installed pyenv
with Homebrew.*
```
$ brew install pyenv-virtualenv
```
Or, if you would like to install the latest development release:
```
$ brew install --HEAD pyenv-virtualenv
```
After installation, you'll still need to add `eval "$(pyenv virtualenv-init -)"` to your
profile (as stated in the caveats). You'll only ever have to do this once.
To update python-virtualenv after it has been installed, run `git pull` in
your cloned copy of the repository, then re-run the install script.
## Usage
### Using `pyenv virtualenv` with pyenv
To create a virtualenv for the Python version use with pyenv, run
`pyenv virtualenv` with tha exact name of the version you want to create
virtualenv. For example,
`pyenv virtualenv`, specifying the Python version you want and the name
of the virtualenv directory. For example,
$ pyenv virtualenv --distribute 2.7.3 venv27
```
$ pyenv virtualenv 2.7.7 my-virtual-env-2.7.7
```
virtualenvs will be created into a directory of the same name
under `~/.pyenv/versions`.
will create a virtualenv based on Python 2.7.7
under `~/.pyenv/versions` in a folder called `my-virtual-env-2.7.7`.
### Create virtualenv from current version
If there is only one argument is given to `pyenv virtualenv`,
virtualenv will be created with given name based on current
version.
```
$ pyenv version
3.4.1 (set by /home/yyuu/.pyenv/version)
$ pyenv virtualenv venv34
```
### List existing virtualenvs
`pyenv virtualenvs` shows you the list of existing virtualenvs.
```
$ pyenv shell venv27
$ pyenv virtualenvs
* venv27 (created from /home/yyuu/.pyenv/versions/2.7.7)
venv34 (created from /home/yyuu/.pyenv/versions/3.4.1)
```
### Activate virtualenv
Some external tools (e.g. [jedi](https://github.com/davidhalter/jedi)) might require you to `activate` the virtualenv.
The `pyenv-virtualenv` will automatically activate/deactivate the virtualenv if the `eval "$(pyenv virtualenv-init -)"` is properly configured in your shell.
### Special environment variables
You can set certain environment variables to control the pyenv-virtualenv.
* `PYENV_VIRTUALENV_CACHE_PATH`, if set, specifies a directory to use for
caching downloaded package files.
* `VIRTUALENV_VERSION`, if set, forces pyenv-virtualenv to install desired
version of virtualenv. If the virtualenv has not been installed,
pyenv-virtualenv will try to install the given version of virtualenv.
* `EZ_SETUP` and `GET_PIP`, if set and pyvenv is preferred than virtualenv,
use `ez_setup.py` and `get_pip.py` at specified location.
* `EZ_SETUP_URL` and `GET_PIP_URL`, if set and pyvenv is preferred
than virtualenv, download `ez_setup.py` and `get_pip.py` from specified URL.
* `SETUPTOOLS_VERSION` and `PIP_VERSION`, if set and pyvenv is preferred
than virtualenv, install specified version of setuptools and pip.
## Version History
#### 20140615
* Fix incompatibility issue of `pyenv activate` and `pyenv deactivate` (#26)
* Workaround for the issue with pyenv-which-ext (#26)
#### 20140614
* Add `pyenv virtualenv-init` to enable auto-activation feature (#24)
* Create symlinks for executables with version suffix (yyuu/pyenv#182)
#### 20140602
* Use new style GH raw url to avoid redirects (raw.github.com -> raw.githubusercontent.com)
* Repaired virtualenv activation and deactivation for the fish shell (#23)
#### 20140421
* Display error if `pyenv activate` was invoked as a command
* Fix completion of `pyenv activate` (#15)
* Use `virtualenv` instead of `pyvenv` if `-p` has given (yyuu/pyenv#158)
#### 20140123
* Add `activate` and `deactivate` to make `pyenv-virtualenv` work with [jedi](https://github.com/davidhalter/jedi) (#9)
* Use `ensurepip` to install `pip` if it is available
* Unset `PIP_REQUIRE_VENV` to avoid problem on the installation of `virtualenv` (#10)
* Add tests
#### 20140110.1
* Fix install script
#### 20140110
* Support environment variables of `EZ_SETUP` and `GET_PIP`.
* Support a short option `-p` of `virtualenv`.
#### 20131216
* Use latest release of setuptools and pip if the version not given via environment variables.
#### 20130622
* Removed bundled `virtualenv.py` script. Now pyenv-virtualenv installs `virtualenv` package into source version and then use it.
* On Python 3.3+, use `pyvenv` as virtualenv command if `virtualenv` is not available.
* Install setuptools and pip into environments created by `pyvenv`.
#### 20130614
* Add `pyenv virtualenvs` to list all virtualenv versions.
* *EXPERIMENTAL*: Add `--upgrade` option to re-create virtualenv with migrating packages
#### 20130527
* Remove `python-virtualenv` which was no longer used.
* Change the installation path of the `virtualenv.py` script. (`./libexec` -> `./libexec/pyenv-virtualenv/${VIRTUALENV_VERSION}`)
* Download `virtualenv.py` if desired version has not been installed.
#### 20130507
* Display virtualenv information in `--help` and `--version`
* Update virtualenv version; 1.8.4 -> 1.9.1
#### 20130307
* Rename the project; `s/python-virtualenv/pyenv-virtualenv/g`
* The `pyenv-virtualenv` script is not depending on `python-virtualenv` now.
`python-virtualenv` will left for compatibility and will not continue for future releases.
* Update virtualenv version; 1.8.2 -> 1.8.4
#### 20130218
* Add pyenv 0.2.x (rbenv 0.4.x) style help messages.
@@ -75,8 +217,7 @@ under `~/.pyenv/versions`.
(The MIT License)
* Copyright (c) 2012 Yamashita, Yuu
* Copyright (c) 2011 Sam Stephenson
* Copyright (c) 2013 Yamashita, Yuu
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the

32
bin/pyenv-activate Executable file
View File

@@ -0,0 +1,32 @@
#!/usr/bin/env bash
#
# Summary: Activate virtual environment
#
# Usage: pyenv activate <virtualenv>
# pyenv activate --unset
#
# Activate a Python virtualenv environment in current shell.
# This acts almost as same as `pyenv shell`, but this invokes the `activate`
# script in your shell.
#
# <virtualenv> should be a string matching a Python version known to pyenv.
set -e
[ -n "$PYENV_DEBUG" ] && set -x
# Provide pyenv completions
if [ "$1" = "--complete" ]; then
echo --unset
exec pyenv-virtualenvs --bare
fi
{ printf "\x1B[31;1m"
echo
echo "Failed to activate virtualenv."
echo
echo "Perhaps pyenv-virtualenv has not been loaded into your shell properly."
echo "Please restart current shell and try again."
echo
printf "\x1B[0m"
} 1>&2
exit 1

21
bin/pyenv-deactivate Executable file
View File

@@ -0,0 +1,21 @@
#!/usr/bin/env bash
#
# Summary: Deactivate virtual environment
#
# Usage: pyenv deactivate
#
# Deactivate a Python virtual environment.
set -e
[ -n "$PYENV_DEBUG" ] && set -x
{ printf "\x1B[31;1m"
echo
echo "Failed to deactivate virtualenv."
echo
echo "Perhaps pyenv-virtualenv has not been loaded into your shell properly."
echo "Please restart current shell and try again."
echo
printf "\x1B[0m"
} 1>&2
exit 1

80
bin/pyenv-sh-activate Executable file
View File

@@ -0,0 +1,80 @@
#!/usr/bin/env bash
#
# Summary: Activate virtual environment
#
# Usage: pyenv activate <virtualenv>
# pyenv activate --unset
#
# Activate a Python virtualenv environment in current shell.
# This acts almost as same as `pyenv shell`, but this invokes the `activate`
# script in your shell.
#
# <virtualenv> should be a string matching a Python version known to pyenv.
set -e
[ -n "$PYENV_DEBUG" ] && set -x
# Provide pyenv completions
if [ "$1" = "--complete" ]; then
echo --unset
exec pyenv-virtualenvs --bare
fi
if [ "$1" = "--unset" ]; then
echo "pyenv deactivate"
exit
fi
versions=("$@")
if [ -z "$versions" ]; then
no_shell=1
OLDIFS="$IFS"
IFS=: versions=($(pyenv-version-name))
IFS="$OLDIFS"
fi
if [ -z "${PYENV_VIRTUALENV_INIT}" ]; then
# Backward compatibility issue
# https://github.com/yyuu/pyenv-virtualenv/issues/26
no_shell=
fi
if [ "${#versions[@]}" -gt 1 ]; then
echo "pyenv-virtualenv: cannot activate multiple versions at once: ${versions[@]}" 1>&2
exit 1
fi
pyenv-virtualenv-prefix "${versions}" 1>/dev/null
shell="$(basename "${PYENV_SHELL:-$SHELL}")"
case "$shell" in
bash )
profile="$HOME/.bash_profile"
;;
zsh )
profile="$HOME/.zshrc"
;;
ksh )
profile="$HOME/.profile"
;;
fish )
profile="$HOME/.config/fish/config.fish"
;;
* )
profile="$HOME/.profile"
;;
esac
# Display setup instruction if 'pyenv virtualenv-init -' is not found in "$profile"
if [ -f "$profile" ] && grep -q 'pyenv init -' "$profile" && ! grep -q 'pyenv virtualenv-init -' "$profile"; then
pyenv-virtualenv-init >&2 || true
fi
if [ -z "$no_shell" ]; then
echo "pyenv shell \"${versions}\";"
fi
case "$shell" in
fish ) echo ". \"$(pyenv-prefix "${versions}")/bin/activate.fish\"" ;;
* ) echo "source \"$(pyenv-prefix "${versions}")/bin/activate\"" ;;
esac

22
bin/pyenv-sh-deactivate Executable file
View File

@@ -0,0 +1,22 @@
#!/usr/bin/env bash
#
# Summary: Deactivate virtual environment
#
# Usage: pyenv deactivate
#
# Deactivate a Python virtual environment.
set -e
[ -n "$PYENV_DEBUG" ] && set -x
shell="$(basename "${PYENV_SHELL:-$SHELL}")"
case "$shell" in
fish ) echo "functions -q deactivate; and deactivate;";;
* ) echo "declare -f deactivate 1>/dev/null 2>&1 && deactivate;";;
esac
if [ -z "${PYENV_VIRTUALENV_INIT}" ]; then
# Backward compatibility issue
# https://github.com/yyuu/pyenv-virtualenv/issues/26
echo "pyenv shell --unset"
fi

View File

@@ -1,11 +1,18 @@
#!/usr/bin/env bash
#
# Summary: Create a Python virtualenv using the python-virtualenv plugin
# Summary: Create a Python virtualenv using the pyenv-virtualenv plugin
#
# Usage: pyenv virtualenv [-v|--verbose] [VIRTUALENV_OPTIONS] <version> <virtualenv-name>
# Usage: pyenv virtualenv [-f|--force] [-u|--upgrade] [VIRTUALENV_OPTIONS] <version> <virtualenv-name>
# pyenv virtualenv --version
# pyenv virtualenv --help
#
# -v/--verbose Verbose mode: print compilation status to stdout
# -u/--upgrade Upgrade existing virtualenv to use new version of Python,
# assuming Python has been upgraded in-place.
# -f/--force Install even if the version appears to be installed already
#
PYENV_VIRTUALENV_VERSION="20140615"
set -e
[ -n "$PYENV_DEBUG" ] && set -x
@@ -18,69 +25,428 @@ if [ -z "$PYENV_ROOT" ]; then
PYENV_ROOT="${HOME}/.pyenv"
fi
# Load shared library functions
eval "$(python-virtualenv --lib)"
unset PIP_REQUIRE_VENV
unset PIP_REQUIRE_VIRTUALENV
# Define library functions
parse_options() {
OPTIONS=()
ARGUMENTS=()
local arg option index
for arg in "$@"; do
if [ "${arg:0:1}" = "-" ]; then
if [ "${arg:1:1}" = "-" ]; then
OPTIONS[${#OPTIONS[*]}]="${arg:2}"
else
index=1
while option="${arg:$index:1}"; do
[ -n "$option" ] || break
OPTIONS[${#OPTIONS[*]}]="$option"
index=$(($index+1))
done
fi
else
ARGUMENTS[${#ARGUMENTS[*]}]="$arg"
fi
done
}
resolve_link() {
$(type -p greadlink readlink | head -1) "$1"
}
abs_dirname() {
local cwd="$(pwd)"
local path="$1"
while [ -n "$path" ]; do
cd "${path%/*}"
local name="${path##*/}"
path="$(resolve_link "$name" || true)"
done
pwd
cd "$cwd"
}
http() {
local method="$1"
local url="$2"
local file="$3"
[ -n "$url" ] || return 1
if type curl &>/dev/null; then
"http_${method}_curl" "$url" "$file"
elif type wget &>/dev/null; then
"http_${method}_wget" "$url" "$file"
else
echo "error: please install \`curl\` or \`wget\` and try again" >&2
exit 1
fi
}
http_head_curl() {
curl -qsILf "$1" >&4 2>&1
}
http_get_curl() {
curl -C - -o "${2:--}" -qsSLf "$1"
}
http_head_wget() {
wget -q --spider "$1" >&4 2>&1
}
http_get_wget() {
wget -nv -c -O "${2:--}" "$1"
}
version() {
detect_venv
local version
if [ -n "$USE_PYVENV" ]; then
version="$(pyenv-which pyvenv 2>/dev/null || true)"
version="${version#$(pyenv-root)/versions/}"
version="${version%/bin/pyvenv}"
echo "pyenv-virtualenv ${PYENV_VIRTUALENV_VERSION} (pyvenv ${version:-unknown})"
else
version="$(venv --version 2>/dev/null || true)"
echo "pyenv-virtualenv ${PYENV_VIRTUALENV_VERSION} (virtualenv ${version:-unknown})"
fi
}
usage() {
# We can remove the sed fallback once pyenv 0.2.0 is widely available.
pyenv-help virtualenv 2>/dev/null || sed -ne '/^#/!q;s/.//;s/.//;1,4d;p' < "$0"
venv --help 2>/dev/null || true
[ -z "$1" ] || exit "$1"
}
unset VERBOSE
detect_venv() {
# Check the existence of executables as a workaround for the issue with pyenv-which-ext
# https://github.com/yyuu/pyenv-virtualenv/issues/26
if [ -x "$(pyenv-prefix)/bin/virtualenv" ]; then
HAS_VIRTUALENV=1
fi
if [ -x "$(pyenv-prefix)/bin/pyvenv" ]; then
HAS_PYVENV=1
fi
# Use pyvenv only if there is pyvenv, virtualenv is not installed, and `-p` not given
if [ -n "${HAS_PYVENV}" ] && [ -z "${HAS_VIRTUALENV}" ] && [ -z "${VIRTUALENV_PYTHON}" ]; then
USE_PYVENV=1
fi
}
venv() {
local args=("$@")
if [ -n "${USE_PYVENV}" ]; then
pyenv-exec pyvenv "${args[@]}"
local last="${args[${#args[@]}-1]}"
ensurepip "${last##*/}"
else
pyenv-exec virtualenv "${args[@]}"
fi
}
install_setuptools() {
local version="$1"
{ if [ "${EZ_SETUP+defined}" ] && [ -f "${EZ_SETUP}" ]; then
if [ -n "$VERBOSE" ]; then
echo "Installing setuptools from ${EZ_SETUP}..." 1>&2
fi
cat "${EZ_SETUP}"
else
[ -n "${EZ_SETUP_URL}" ] || {
if [ -n "${SETUPTOOLS_VERSION}" ]; then
EZ_SETUP_URL="https://bitbucket.org/pypa/setuptools/raw/${SETUPTOOLS_VERSION}/ez_setup.py"
else
EZ_SETUP_URL="https://bitbucket.org/pypa/setuptools/downloads/ez_setup.py"
fi
}
if [ -n "$VERBOSE" ]; then
echo "Installing setuptools from ${EZ_SETUP_URL}..." 1>&2
fi
http get "${EZ_SETUP_URL}"
fi
} | PYENV_VERSION="${version}" pyenv-exec python
}
install_pip() {
local version="$1"
{ if [ "${GET_PIP+defined}" ] && [ -f "${GET_PIP}" ]; then
if [ -n "$VERBOSE" ]; then
echo "Installing pip from ${GET_PIP}..." 1>&2
fi
cat "${GET_PIP}"
else
[ -n "${GET_PIP_URL}" ] || {
if [ -n "${PIP_VERSION}" ]; then
GET_PIP_URL="https://raw.githubusercontent.com/pypa/pip/${PIP_VERSION}/contrib/get-pip.py"
else
GET_PIP_URL="https://raw.githubusercontent.com/pypa/pip/master/contrib/get-pip.py"
fi
}
if [ -n "$VERBOSE" ]; then
echo "Installing pip from ${GET_PIP_URL}..." 1>&2
fi
http get "${GET_PIP_URL}"
fi
} | PYENV_VERSION="${version}" pyenv-exec python
}
install_virtualenv() {
local version="$1"
VIRTUALENV_VERSION="==${VIRTUALENV_VERSION}"
PYENV_VERSION="${version}" pyenv-exec pip install $QUIET $VERBOSE "virtualenv${VIRTUALENV_VERSION%==}"
}
ensurepip() {
local version="$1"
PYENV_VERSION="${version}" pyenv-exec python -m ensurepip 2>/dev/null || {
PYENV_VERSION="${version}" pyenv-exec python -c "import setuptools" 1>/dev/null 2>&1 || {
install_setuptools "${version}"
} 1>&2
PYENV_VERSION="${version}" pyenv-which pip 1>/dev/null 2>&1 || {
install_pip "${version}"
} 1>&2
}
}
create_symlinks() {
local version="$1"
local suffix="$(PYENV_VERSION="${version}" pyenv-exec python -c 'import sys;v=sys.version_info;sys.stdout.write("%d.%d"%(v[0],v[1]))')"
local file link
shopt -s nullglob
for file in "${VIRTUALENV_PATH}/bin"/*; do
if [[ "${file##*/}" == *"${suffix}" ]]; then
if [[ "${file}" == *"-${suffix}" ]]; then
link="${file%%-${suffix}}"
else
link="${file%%${suffix}}"
fi
if [ ! -e "${link}" ]; then
( cd "${file%/*}" && ln -fs "${file##*/}" "${link##*/}" )
fi
fi
done
shopt -u nullglob
}
prepare_upgrade() {
local version="$1"
local prefix="$2"
PYENV_VERSION="${version}" pyenv-exec pip freeze >"${UPGRADE_LIST}"
mv -f "${prefix}" "${UPGRADE_PATH}"
}
upgrade() {
local version="$1"
local prefix="$2"
## Migrate previously installed packages from requirements.txt
if ! PYENV_VERSION="${version}" pyenv-exec pip install $QUIET $VERBOSE --requirement "${UPGRADE_LIST}"; then
{ echo
echo "UPGRADE FAILED"
echo
echo "Inspect or clean up the original tree at ${UPGRADE_PATH}"
echo
echo "Package list:"
cat "${UPGRADE_LIST}"
} 1>&2
return 1
fi
rm -f "${UPGRADE_LIST}"
rm -fr "${UPGRADE_PATH}"
}
PYENV_VIRTUALENV_ROOT="$(abs_dirname "$0")/.."
if [ -z "${PYENV_VIRTUALENV_CACHE_PATH}" ]; then
PYENV_VIRTUALENV_CACHE_PATH="${PYTHON_BUILD_CACHE_PATH:-${PYENV_ROOT}/cache}"
fi
VIRTUALENV_OPTIONS=()
unset FORCE
unset VIRTUALENV_PYTHON
unset QUIET
unset UPGRADE
unset VERBOSE
parse_options "$@"
for option in "${OPTIONS[@]}"; do
case "$option" in
"f" | "force" )
FORCE=true
;;
"h" | "help" )
usage 0
;;
"p" | "python" )
VIRTUALENV_PYTHON="${ARGUMENTS[0]}"
ARGUMENTS=("${ARGUMENTS[@]:1}") # shift 1
;;
"q" | "quiet" )
QUIET="--quiet"
;;
"u" | "upgrade" )
UPGRADE=true
;;
"v" | "verbose" )
VERBOSE="-v"
VERBOSE="--verbose"
;;
"version" )
exec python-virtualenv --version
version
exit 0
;;
* )
VIRTUALENV_OPTIONS[${#VIRTUALENV_OPTIONS[*]}]="--$option"
* ) # virtualenv long options
if [[ "$option" == "python="* ]]; then
VIRTUALENV_PYTHON="${option#python=}"
else
VIRTUALENV_OPTIONS[${#VIRTUALENV_OPTIONS[*]}]="--$option"
fi
;;
esac
done
VERSION_NAME="${ARGUMENTS[0]}"
[ -n "$VERSION_NAME" ] || usage 1
for script in $(pyenv-hooks virtualenv); do
source "$script"
done
PYTHON_BIN=$(PYENV_VERSION="${VERSION_NAME}" pyenv-which python)
if [ ! -x "${PYTHON_BIN}" ]; then
echo "pyenv-virtualenv: could not obtain python executable: ${PYTHON_BIN}" >&2
if [[ "${#ARGUMENTS[@]}" == 0 ]]; then
echo "pyenv-virtualenv: no virtualenv name given." 1>&2
exit 1
fi
# find canonical name of python executable.
# virtualenv will create "bin/python" executable as same name as its bootstraped python.
if [ -L "${PYTHON_BIN}" ]; then
while [ -L "${PYTHON_BIN}" ]; do # retrieve symlinks
PYTHON_BIN="$(dirname "${PYTHON_BIN}")/$(resolve_link "${PYTHON_BIN}")"
done
elif [[ "${#ARGUMENTS[@]}" == 1 ]]; then
# If only one argument given, use current version as source version
VERSION_NAME="$(pyenv-version-name)"
VIRTUALENV_NAME="${ARGUMENTS[0]}"
else
# python 2.6 and older don't have "bin/python" as symlink.
# so we must traverse files like "bin/python*" to obtain canonical name.
for python in ${PYENV_ROOT}/versions/${VERSION_NAME}/bin/python*; do
if ( basename "$python" | grep '^python[0-9][0-9]*\.[0-9][0-9]*$' && cmp "$PYTHON_BIN" "$python" ) >/dev/null; then
PYTHON_BIN="${python}"
break
fi
done
# Otherwise, use former as source version, and latter as virtualenv version
VERSION_NAME="${ARGUMENTS[0]}"
VIRTUALENV_NAME="${ARGUMENTS[1]##*/}"
fi
VIRTUALENV_NAME="${ARGUMENTS[1]##*/}"
if [ -z "${VERSION_NAME}" ] || [ -z "${VIRTUALENV_NAME}" ]; then
usage 1
fi
# Set VERSION_NAME as default version in this script
export PYENV_VERSION="${VERSION_NAME}"
# Source version must exists before creating virtualenv
pyenv-prefix 1>/dev/null 2>&1 || usage 1
if [ -z "$TMPDIR" ]; then
TMP="/tmp"
else
TMP="${TMPDIR%/}"
fi
SEED="$(date "+%Y%m%d%H%M%S").$$"
UPGRADE_PATH="${TMP}/pyenv-virtualenv.${SEED}"
UPGRADE_LIST="${TMP}/pyenv-virtualenv.${SEED}.txt"
VIRTUALENV_PATH="${PYENV_ROOT}/versions/${VIRTUALENV_NAME}"
python-virtualenv $VERBOSE "${VIRTUALENV_OPTIONS[@]}" "$PYTHON_BIN" "$VIRTUALENV_PATH"
pyenv rehash
unset HAS_VIRTUALENV
unset HAS_PYVENV
unset USE_PYVENV
detect_venv
if [ -n "${USE_PYVENV}" ]; then
# Unset some arguments not supported by pyvenv
unset QUIET
unset VERBOSE
if [ -n "$UPGRADE" ]; then
unset UPGRADE
VIRTUALENV_OPTIONS[${#VIRTUALENV_OPTIONS[*]}]="--upgrade"
fi
else
if [ -n "${VIRTUALENV_PYTHON}" ]; then
VIRTUALENV_OPTIONS[${#VIRTUALENV_OPTIONS[*]}]="--python=${VIRTUALENV_PYTHON}"
fi
if [ -z "${HAS_VIRTUALENV}" ]; then
install_virtualenv "${PYENV_VERSION}"
HAS_VIRTUALENV=1
fi
fi
# Unset environment variables which starts with `VIRTUALENV_`.
# These variables are reserved for virtualenv.
unset VIRTUALENV_VERSION
# Define `before_virtualenv` and `after_virtualenv` functions that allow
# plugin hooks to register a string of code for execution before or
# after the installation process.
declare -a before_hooks after_hooks
before_virtualenv() {
local hook="$1"
before_hooks["${#before_hooks[@]}"]="$hook"
}
after_virtualenv() {
local hook="$1"
after_hooks["${#after_hooks[@]}"]="$hook"
}
# Load plugin hooks.
OLDIFS="$IFS"
IFS=$'\n' scripts=(`pyenv-hooks virtualenv`)
IFS="$OLDIFS"
for script in "${scripts[@]}"; do source "$script"; done
[ -d "${VIRTUALENV_PATH}" ] && PREFIX_EXISTS=1
# If the virtualenv exists, prompt for confirmation unless
# the --force option was specified.
if [ -d "${VIRTUALENV_PATH}/bin" ]; then
if [ -z "$FORCE" ]; then
echo "pyenv-virtualenv: ${VIRTUALENV_PATH} already exists" 1>&2
read -p "continue with installation? (y/N) "
case "$REPLY" in
y* | Y* ) ;;
* ) exit 1 ;;
esac
fi
if [ -n "$UPGRADE" ]; then
prepare_upgrade "${VIRTUALENV_NAME}" "${VIRTUALENV_PATH}"
fi
fi
# Execute `before_virtualenv` hooks.
for hook in "${before_hooks[@]}"; do eval "$hook"; done
# Plan cleanup on unsuccessful installation.
cleanup() {
[ -z "${PREFIX_EXISTS}" ] && rm -rf "$VIRTUALENV_PATH"
}
trap cleanup SIGINT
# Invoke virtualenv and record exit status in $STATUS.
STATUS=0
# virtualenv may download distribute/setuptools in current directory.
# change to cache directory to reuse them between invocation.
mkdir -p "${PYENV_VIRTUALENV_CACHE_PATH}"
cd "${PYENV_VIRTUALENV_CACHE_PATH}"
venv $QUIET $VERBOSE "${VIRTUALENV_OPTIONS[@]}" "${VIRTUALENV_PATH}" || STATUS="$?"
## Migrate previously installed packages from requirements.txt
if [ -n "$UPGRADE" ]; then
upgrade "${VIRTUALENV_NAME}" "${VIRTUALENV_PATH}" || STATUS="$?"
fi
create_symlinks "${VIRTUALENV_NAME}"
# Execute `after_virtualenv` hooks
for hook in "${after_hooks[@]}"; do eval "$hook"; done
# Run `pyenv-rehash` after a successful installation.
if [ "$STATUS" == "0" ]; then
pyenv-rehash
else
cleanup
fi
exit "$STATUS"

115
bin/pyenv-virtualenv-init Executable file
View File

@@ -0,0 +1,115 @@
#!/usr/bin/env bash
# Summary: Configure the shell environment for pyenv-virtualenv
# Usage: eval "$(pyenv virtualenv-init - [<shell>])"
#
# Automatically activates a Python virtualenv environment based on current
# pyenv version.
#
set -e
[ -n "$PYENV_DEBUG" ] && set -x
print=""
for args in "$@"
do
if [ "$args" = "-" ]; then
print=1
shift
fi
done
shell="$1"
if [ -z "$shell" ]; then
shell="$(ps c -p "$PPID" -o 'ucomm=' 2>/dev/null || true)"
shell="${shell##-}"
shell="${shell%% *}"
shell="$(basename "${shell:-$SHELL}")"
fi
if [ -z "$print" ]; then
case "$shell" in
bash )
profile='~/.bash_profile'
;;
zsh )
profile='~/.zshrc'
;;
ksh )
profile='~/.profile'
;;
fish )
profile='~/.config/fish/config.fish'
;;
* )
profile='your profile'
;;
esac
{ echo "# Load pyenv-virtualenv automatically by adding"
echo "# the following to ${profile}:"
echo
case "$shell" in
fish )
echo 'status --is-interactive; and . (pyenv virtualenv-init -|psub)'
;;
* )
echo 'eval "$(pyenv virtualenv-init -)"'
;;
esac
echo
} >&2
exit 1
fi
case "$shell" in
bash )
cat <<EOS
export PYENV_VIRTUALENV_INIT=1
_pyenv_virtualenv_hook() {
if [[ "\$(pyenv version-name)" == "system" ]]; then
pyenv deactivate || true;
elif [[ "\$VIRTUAL_ENV" != "\$(pyenv prefix)" ]]; then
pyenv deactivate || true;
pyenv activate 2>/dev/null || true
fi
};
if ! [[ "\$PROMPT_COMMAND" =~ _pyenv_virtualenv_hook ]]; then
PROMPT_COMMAND="_pyenv_virtualenv_hook;\$PROMPT_COMMAND";
fi
EOS
;;
fish )
cat <<EOS
setenv PYENV_VIRTUALENV_INIT=1;
function _pyenv_virtualenv_hook --on-event fish_prompt;
if [ (pyenv version-name) = "system" ]
eval (pyenv sh-deactivate); or true
else if [ "\$VIRTUAL_ENV" != (pyenv prefix) ]
eval (pyenv sh-deactivate); or true
eval (pyenv sh-activate 2>/dev/null); or true
end
end
EOS
;;
zsh )
cat <<EOS
export PYENV_VIRTUALENV_INIT=1
_pyenv_virtualenv_hook() {
if [[ "\$(pyenv version-name)" == "system" ]]; then
pyenv deactivate || true
elif [[ "\$VIRTUAL_ENV" != "\$(pyenv prefix)" ]]; then
pyenv deactivate || true
pyenv activate 2>/dev/null || true
fi
}
typeset -a precmd_functions
if [[ -z \$precmd_functions[(r)_pyenv_virtualenv_hook] ]]; then
precmd_functions+=_pyenv_virtualenv_hook;
fi
EOS
;;
* )
# FIXME: what should i do here??
;;
esac

45
bin/pyenv-virtualenv-prefix Executable file
View File

@@ -0,0 +1,45 @@
#!/usr/bin/env bash
#
# Summary: Display real_prefix for a Python virtualenv version
# Usage: pyenv virtualenv-prefix [<virtualenv>]
#
set -e
[ -n "$PYENV_DEBUG" ] && set -x
if [ -z "$PYENV_ROOT" ]; then
PYENV_ROOT="${HOME}/.pyenv"
fi
if [ -n "$1" ]; then
versions=($@)
IFS=: PYENV_VERSION="${versions[*]}"
export PYENV_VERSION
else
IFS=: versions=($(pyenv-version-name))
fi
real_prefix() { # virtualenv
local version="$1"
PYENV_VERSION="${version}" pyenv-exec python -c 'import sys;print(sys.real_prefix)' 2>/dev/null
}
base_prefix() { # pyvenv
# FIXME: non-pyvenv versions also have sys.base_prefix
local version="$1"
PYENV_VERSION="${version}" pyenv-exec python -c 'import sys;print(sys.base_prefix)' 2>/dev/null
}
VIRTUALENV_PREFIX_PATHS=()
for version in "${versions[@]}"; do
PREFIX="$(pyenv-prefix "${version}")"
if [ -f "${PREFIX}/bin/activate" ]; then
VIRTUALENV_PREFIX_PATH="$(real_prefix "${version}" || base_prefix "${version}" || true)"
VIRTUALENV_PREFIX_PATHS=("${VIRTUALENV_PREFIX_PATHS[@]}" "$VIRTUALENV_PREFIX_PATH")
else
echo "pyenv-virtualenv: version \`${version}' is not a virtualenv" 1>&2
exit 1
fi
done
IFS=: echo "${VIRTUALENV_PREFIX_PATHS[*]}"

56
bin/pyenv-virtualenvs Executable file
View File

@@ -0,0 +1,56 @@
#!/usr/bin/env bash
#
# Summary: List all Python virtualenvs found in `$PYENV_ROOT/versions/*'.
#
# Usage: pyenv virtualenvs [--bare]
#
set -e
[ -n "$PYENV_DEBUG" ] && set -x
if [ -z "$PYENV_ROOT" ]; then
PYENV_ROOT="${HOME}/.pyenv"
fi
if [ "$1" = "--bare" ]; then
hit_prefix=""
miss_prefix=""
current_versions=()
print_origin=""
else
hit_prefix="* "
miss_prefix=" "
current_versions=($(IFS=:; for version in $(pyenv-version-name); do echo "$version"; done))
print_origin="1"
fi
array_exists() {
local x car="$1"
shift
for x in "$@"; do
[ "${x}" = "${car}" ] && return 0
done
return 1
}
print_version() {
if [ -n "${print_origin}" ]; then
local version_origin="$2"
else
local version_origin=""
fi
if array_exists "$1" "${current_versions[@]}"; then
echo "${hit_prefix}${1}${version_origin}"
else
echo "${miss_prefix}${1}${version_origin}"
fi
}
for version in $(pyenv-versions --bare); do
if [[ "${version}" != "system" ]]; then
virtualenv_prefix="$(pyenv-virtualenv-prefix "${version}" 2>/dev/null || true)"
if [ -d "${virtualenv_prefix}" ]; then
print_version "${version}" " (created from ${virtualenv_prefix})"
fi
fi
done

View File

@@ -1,130 +0,0 @@
#!/usr/bin/env bash
PYTHON_VIRTUALENV_VERSION="20130218"
set -E
exec 3<&2 # preserve original stderr at fd 3
lib() {
parse_options() {
OPTIONS=()
ARGUMENTS=()
local arg option index
for arg in "$@"; do
if [ "${arg:0:1}" = "-" ]; then
if [ "${arg:1:1}" = "-" ]; then
OPTIONS[${#OPTIONS[*]}]="${arg:2}"
else
index=1
while option="${arg:$index:1}"; do
[ -n "$option" ] || break
OPTIONS[${#OPTIONS[*]}]="$option"
index=$(($index+1))
done
fi
else
ARGUMENTS[${#ARGUMENTS[*]}]="$arg"
fi
done
}
resolve_link() {
$(type -p greadlink readlink | head -1) "$1"
}
abs_dirname() {
local cwd="$(pwd)"
local path="$1"
while [ -n "$path" ]; do
cd "${path%/*}"
local name="${path##*/}"
path="$(resolve_link "$name" || true)"
done
pwd
cd "$cwd"
}
if [ "$1" == "--$FUNCNAME" ]; then
declare -f "$FUNCNAME"
echo "$FUNCNAME \"\$1\";"
exit
fi
}
lib "$1"
version() {
echo "python-virtualenv ${PYTHON_VIRTUALENV_VERSION}"
}
usage() {
{ version
echo "usage: python-virtualenv [-v|--verbose] [VIRTUALENV_OPTIONS] python_bin virtualenv_path"
} >&2
if [ -z "$1" ]; then
exit 1
fi
}
unset VERBOSE
PYTHON_VIRTUALENV_ROOT="$(abs_dirname "$0")/.."
VIRTUALENV_OPTIONS=()
parse_options "$@"
for option in "${OPTIONS[@]}"; do
case "$option" in
"h" | "help" )
usage without_exiting
{ echo
echo " -v/--verbose Verbose mode: print compilation status to stdout"
echo
} >&2
exit 0
;;
"v" | "verbose" )
VERBOSE=true
;;
"version" )
version
exit 0
;;
* )
VIRTUALENV_OPTIONS[${#VIRTUALENV_OPTIONS[*]}]="--$option"
;;
esac
done
PYTHON_BIN="${ARGUMENTS[0]}"
if [ -z "${PYTHON_BIN}" ]; then
usage
elif [ ! -x "${PYTHON_BIN}" ]; then
echo "python-virtualenv: given python is not an executable: ${PYTHON_BIN}" >&2
exit 1
fi
VIRTUALENV_PATH="${ARGUMENTS[1]}"
if [ -z "$VIRTUALENV_PATH" ]; then
usage
fi
VIRTUALENV_PYTHON_BIN="${VIRTUALENV_PATH}/bin/python"
# create virtualenv
VIRTUALENV="${PYTHON_VIRTUALENV_ROOT}/libexec/python-virtualenv/virtualenv.py"
[ -f "${VIRTUALENV}" ] || VIRTUALENV="${PYTHON_VIRTUALENV_ROOT}/libexec/virtualenv.py"
"${PYTHON_BIN}" "${VIRTUALENV}" "${VIRTUALENV_OPTIONS[@]}" "${VIRTUALENV_PATH}"
# create symlink of `python' bound for actual executable
if [ ! -f "$VIRTUALENV_PYTHON_BIN" ]; then
if [ -f "${VIRTUALENV_PATH}/bin/$(basename "${PYTHON_BIN}")" ]; then
{
cd ${VIRTUALENV_PATH}/bin
ln -fs "$(basename "${PYTHON_BIN}")" python
}
fi
fi

View File

@@ -1,23 +1,18 @@
#!/bin/sh
# Usage: PREFIX=/usr/local ./install.sh
#
# Installs pyenv-virtualenv under $PREFIX.
set -e
cd "$(dirname "$0")"
if [ -z "${PREFIX}" ]; then
PREFIX="/usr/local"
fi
BIN_PATH="${PREFIX}/bin"
LIBEXEC_PATH="${PREFIX}/libexec/python-virtualenv"
mkdir -p "${BIN_PATH}"
mkdir -p "${LIBEXEC_PATH}"
mkdir -p "$BIN_PATH"
for file in bin/*; do
cp "${file}" "${BIN_PATH}"
done
for file in libexec/*; do
cp "${file}" "${LIBEXEC_PATH}"
done
echo "Installed python-virtualenv at ${PREFIX}"
install -p bin/* "$BIN_PATH"

File diff suppressed because it is too large Load Diff

156
test/activate.bats Normal file
View File

@@ -0,0 +1,156 @@
#!/usr/bin/env bats
load test_helper
setup() {
export HOME="${TMP}"
export PYENV_ROOT="${TMP}/pyenv"
}
@test "activate virtualenv from current version" {
export PYENV_VIRTUALENV_INIT=1
stub pyenv-version-name "echo venv"
stub pyenv-virtualenv-prefix "venv : echo \"${PYENV_ROOT}/versions/venv\""
stub pyenv-prefix "venv : echo \"${PYENV_ROOT}/versions/venv\""
PYENV_SHELL="bash" PYENV_VERSION="venv" run pyenv-sh-activate
unstub pyenv-version-name
unstub pyenv-virtualenv-prefix
unstub pyenv-prefix
assert_success
assert_output <<EOS
source "${PYENV_ROOT}/versions/venv/bin/activate"
EOS
}
@test "activate virtualenv from current version (without pyenv-virtualenv-init)" {
export PYENV_VIRTUALENV_INIT=
stub pyenv-version-name "echo venv"
stub pyenv-virtualenv-prefix "venv : echo \"${PYENV_ROOT}/versions/venv\""
stub pyenv-prefix "venv : echo \"${PYENV_ROOT}/versions/venv\""
PYENV_SHELL="bash" PYENV_VERSION="venv" run pyenv-sh-activate
unstub pyenv-version-name
unstub pyenv-virtualenv-prefix
unstub pyenv-prefix
assert_success
assert_output <<EOS
pyenv shell "venv";
source "${PYENV_ROOT}/versions/venv/bin/activate"
EOS
}
@test "activate virtualenv from current version (fish)" {
export PYENV_VIRTUALENV_INIT=1
stub pyenv-version-name "echo venv"
stub pyenv-virtualenv-prefix "venv : echo \"${PYENV_ROOT}/versions/venv\""
stub pyenv-prefix "venv : echo \"${PYENV_ROOT}/versions/venv\""
PYENV_SHELL="fish" PYENV_VERSION="venv" run pyenv-sh-activate
unstub pyenv-version-name
unstub pyenv-virtualenv-prefix
unstub pyenv-prefix
assert_success
assert_output <<EOS
. "${PYENV_ROOT}/versions/venv/bin/activate.fish"
EOS
}
@test "activate virtualenv from current version (fish) (without pyenv-virtualenv-init)" {
export PYENV_VIRTUALENV_INIT=
stub pyenv-version-name "echo venv"
stub pyenv-virtualenv-prefix "venv : echo \"${PYENV_ROOT}/versions/venv\""
stub pyenv-prefix "venv : echo \"${PYENV_ROOT}/versions/venv\""
PYENV_SHELL="fish" PYENV_VERSION="venv" run pyenv-sh-activate
unstub pyenv-version-name
unstub pyenv-virtualenv-prefix
unstub pyenv-prefix
assert_success
assert_output <<EOS
pyenv shell "venv";
. "${PYENV_ROOT}/versions/venv/bin/activate.fish"
EOS
}
@test "activate virtualenv from command-line argument" {
export PYENV_VIRTUALENV_INIT=1
stub pyenv-virtualenv-prefix "venv27 : echo \"${PYENV_ROOT}/versions/venv27\""
stub pyenv-prefix "venv27 : echo \"${PYENV_ROOT}/versions/venv27\""
run pyenv-sh-activate "venv27"
unstub pyenv-virtualenv-prefix
unstub pyenv-prefix
assert_success
assert_output <<EOS
pyenv shell "venv27";
source "${PYENV_ROOT}/versions/venv27/bin/activate"
EOS
}
@test "activate virtualenv from command-line argument (without pyenv-virtualenv-init)" {
export PYENV_VIRTUALENV_INIT=
stub pyenv-virtualenv-prefix "venv27 : echo \"${PYENV_ROOT}/versions/venv27\""
stub pyenv-prefix "venv27 : echo \"${PYENV_ROOT}/versions/venv27\""
run pyenv-sh-activate "venv27"
unstub pyenv-virtualenv-prefix
unstub pyenv-prefix
assert_success
assert_output <<EOS
pyenv shell "venv27";
source "${PYENV_ROOT}/versions/venv27/bin/activate"
EOS
}
@test "unset invokes deactivate" {
run pyenv-sh-activate --unset
assert_success
assert_output <<EOS
pyenv deactivate
EOS
}
@test "should fail if the version is not a virtualenv" {
stub pyenv-virtualenv-prefix "3.3.3 : false"
run pyenv-sh-activate "3.3.3"
unstub pyenv-virtualenv-prefix
assert_failure
}
@test "should fail if there are multiple versions" {
run pyenv-sh-activate "venv" "venv27"
assert_failure
assert_output <<EOS
pyenv-virtualenv: cannot activate multiple versions at once: venv venv27
EOS
}
@test "should fail if activate is invoked as a command" {
run pyenv-activate
assert_failure
}

59
test/deactivate.bats Normal file
View File

@@ -0,0 +1,59 @@
#!/usr/bin/env bats
load test_helper
setup() {
export PYENV_ROOT="${TMP}/pyenv"
}
@test "deactivate virtualenv" {
export PYENV_VIRTUALENV_INIT=1
PYENV_SHELL="bash" run pyenv-sh-deactivate
assert_success
assert_output <<EOS
declare -f deactivate 1>/dev/null 2>&1 && deactivate;
EOS
}
@test "deactivate virtualenv (without pyenv-virtualenv-init)" {
export PYENV_VIRTUALENV_INIT=
PYENV_SHELL="bash" run pyenv-sh-deactivate
assert_success
assert_output <<EOS
declare -f deactivate 1>/dev/null 2>&1 && deactivate;
pyenv shell --unset
EOS
}
@test "deactivate virtualenv (fish)" {
export PYENV_VIRTUALENV_INIT=1
PYENV_SHELL="fish" run pyenv-sh-deactivate
assert_success
assert_output <<EOS
functions -q deactivate; and deactivate;
EOS
}
@test "deactivate virtualenv (fish) (without pyenv-virtualenv-init)" {
export PYENV_VIRTUALENV_INIT=
PYENV_SHELL="fish" run pyenv-sh-deactivate
assert_success
assert_output <<EOS
functions -q deactivate; and deactivate;
pyenv shell --unset
EOS
}
@test "should fail if deactivate is invoked as a command" {
run pyenv-deactivate
assert_failure
}

35
test/hooks.bats Normal file
View File

@@ -0,0 +1,35 @@
#!/usr/bin/env bats
load test_helper
setup() {
export PYENV_ROOT="${TMP}/pyenv"
export HOOK_PATH="${TMP}/i has hooks"
mkdir -p "$HOOK_PATH"
}
@test "pyenv-virtualenv hooks" {
cat > "${HOOK_PATH}/virtualenv.bash" <<OUT
before_virtualenv 'echo before: \$VIRTUALENV_PATH'
after_virtualenv 'echo after: \$STATUS'
OUT
stub pyenv-prefix "echo '${PYENV_ROOT}/versions/3.2.1'"
stub pyenv-prefix "echo '${PYENV_ROOT}/versions/3.2.1'"
stub pyenv-prefix "echo '${PYENV_ROOT}/versions/3.2.1'"
stub pyenv-hooks "virtualenv : echo '$HOOK_PATH'/virtualenv.bash"
stub pyenv-exec "echo PYENV_VERSION=3.2.1 \"\$@\""
stub pyenv-rehash "echo rehashed"
create_executable "3.2.1" "virtualenv"
remove_executable "3.2.1" "pyvenv"
run pyenv-virtualenv "3.2.1" venv
assert_success
assert_output <<-OUT
before: ${PYENV_ROOT}/versions/venv
PYENV_VERSION=3.2.1 virtualenv ${PYENV_ROOT}/versions/venv
after: 0
rehashed
OUT
}

44
test/init.bats Normal file
View File

@@ -0,0 +1,44 @@
#!/usr/bin/env bats
load test_helper
@test "detect parent shell" {
root="$(cd $BATS_TEST_DIRNAME/.. && pwd)"
SHELL=/bin/false run pyenv-virtualenv-init -
assert_success
assert_output_contains ' PROMPT_COMMAND="_pyenv_virtualenv_hook;$PROMPT_COMMAND";'
}
@test "sh-compatible instructions" {
run pyenv-virtualenv-init bash
assert [ "$status" -eq 1 ]
assert_output_contains 'eval "$(pyenv virtualenv-init -)"'
run pyenv-virtualenv-init zsh
assert [ "$status" -eq 1 ]
assert_output_contains 'eval "$(pyenv virtualenv-init -)"'
}
@test "fish instructions" {
run pyenv-virtualenv-init fish
assert [ "$status" -eq 1 ]
assert_output_contains 'status --is-interactive; and . (pyenv virtualenv-init -|psub)'
}
@test "outputs bash-specific syntax" {
run pyenv-virtualenv-init - bash
assert_success
assert_output_contains ' PROMPT_COMMAND="_pyenv_virtualenv_hook;$PROMPT_COMMAND";'
}
@test "outputs fish-specific syntax" {
run pyenv-virtualenv-init - fish
assert_success
assert_output_contains 'function _pyenv_virtualenv_hook --on-event fish_prompt;'
}
@test "outputs zsh-specific syntax" {
run pyenv-virtualenv-init - zsh
assert_success
assert_output_contains ' precmd_functions+=_pyenv_virtualenv_hook;'
}

48
test/installer.bats Normal file
View File

@@ -0,0 +1,48 @@
#!/usr/bin/env bats
load test_helper
@test "installs pyenv-virtualenv into PREFIX" {
cd "$TMP"
PREFIX="${PWD}/usr" run "${BATS_TEST_DIRNAME}/../install.sh"
assert_success ""
cd usr
assert [ -x bin/pyenv-activate ]
assert [ -x bin/pyenv-deactivate ]
assert [ -x bin/pyenv-sh-activate ]
assert [ -x bin/pyenv-sh-deactivate ]
assert [ -x bin/pyenv-virtualenv ]
assert [ -x bin/pyenv-virtualenv-init ]
assert [ -x bin/pyenv-virtualenv-prefix ]
assert [ -x bin/pyenv-virtualenvs ]
}
@test "overwrites old installation" {
cd "$TMP"
mkdir -p bin
touch bin/pyenv-virtualenv
PREFIX="$PWD" run "${BATS_TEST_DIRNAME}/../install.sh"
assert_success ""
assert [ -x bin/pyenv-virtualenv ]
run grep "virtualenv" bin/pyenv-virtualenv
assert_success
}
@test "unrelated files are untouched" {
cd "$TMP"
mkdir -p bin share/bananas
chmod g-w bin
touch bin/bananas
PREFIX="$PWD" run "${BATS_TEST_DIRNAME}/../install.sh"
assert_success ""
assert [ -e bin/bananas ]
run ls -ld bin
assert_equal "r-x" "${output:4:3}"
}

200
test/pyvenv.bats Normal file
View File

@@ -0,0 +1,200 @@
#!/usr/bin/env bats
load test_helper
setup() {
export PYENV_ROOT="${TMP}/pyenv"
}
stub_pyenv() {
export PYENV_VERSION="$1"
stub pyenv-version-name "echo \${PYENV_VERSION}"
stub pyenv-prefix " : echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'"
stub pyenv-hooks "virtualenv : echo"
stub pyenv-rehash " : echo rehashed"
}
unstub_pyenv() {
unset PYENV_VERSION
unstub pyenv-version-name
unstub pyenv-prefix
unstub pyenv-hooks
unstub pyenv-rehash
}
@test "use pyvenv if virtualenv is not available" {
stub_pyenv "3.4.1"
stub pyenv-prefix " : echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'"
stub pyenv-prefix " : echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'"
stub pyenv-exec "echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
stub pyenv-exec "bin=\"${PYENV_ROOT}/versions/venv/bin\";mkdir -p \"\$bin\";touch \"\$bin/pip3.4\";echo PYENV_VERSION=\${PYENV_VERSION} ensurepip"
stub pyenv-exec "echo 3.4"
remove_executable "3.4.1" "virtualenv"
create_executable "3.4.1" "pyvenv"
run pyenv-virtualenv venv
assert_success
assert_output <<OUT
PYENV_VERSION=3.4.1 pyvenv ${PYENV_ROOT}/versions/venv
PYENV_VERSION=venv ensurepip
rehashed
OUT
assert [ -e "${PYENV_ROOT}/versions/venv/bin/pip" ]
unstub_pyenv
unstub pyenv-exec
}
@test "not use pyvenv if virtualenv is available" {
stub_pyenv "3.4.1"
stub pyenv-prefix " : echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'"
stub pyenv-prefix " : echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'"
stub pyenv-exec "echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
stub pyenv-exec "echo 3.4"
create_executable "3.4.1" "virtualenv"
create_executable "3.4.1" "pyvenv"
run pyenv-virtualenv venv
assert_success
assert_output <<OUT
PYENV_VERSION=3.4.1 virtualenv ${PYENV_ROOT}/versions/venv
rehashed
OUT
unstub_pyenv
unstub pyenv-exec
}
@test "install virtualenv if pyvenv is not avaialble" {
stub_pyenv "3.2.1"
stub pyenv-prefix " : echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'"
stub pyenv-prefix " : echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'"
stub pyenv-exec "echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
stub pyenv-exec "echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
stub pyenv-exec "echo 3.2"
remove_executable "3.2.1" "virtualenv"
remove_executable "3.2.1" "pyvenv"
run pyenv-virtualenv venv
assert_success
assert_output <<OUT
PYENV_VERSION=3.2.1 pip install virtualenv
PYENV_VERSION=3.2.1 virtualenv ${PYENV_ROOT}/versions/venv
rehashed
OUT
unstub_pyenv
unstub pyenv-exec
}
@test "install virtualenv if -p has given" {
stub_pyenv "3.4.1"
stub pyenv-prefix " : echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'"
stub pyenv-prefix " : echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'"
stub pyenv-exec "echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
stub pyenv-exec "echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
stub pyenv-exec "echo 3.4"
remove_executable "3.4.1" "virtualenv"
create_executable "3.4.1" "pyvenv"
run pyenv-virtualenv -p python3 venv
assert_success
assert_output <<OUT
PYENV_VERSION=3.4.1 pip install virtualenv
PYENV_VERSION=3.4.1 virtualenv --python=python3 ${PYENV_ROOT}/versions/venv
rehashed
OUT
unstub_pyenv
unstub pyenv-exec
}
@test "install virtualenv if --python has given" {
stub_pyenv "3.4.1"
stub pyenv-prefix " : echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'"
stub pyenv-prefix " : echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'"
stub pyenv-exec "echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
stub pyenv-exec "echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
stub pyenv-exec "echo 3.4"
remove_executable "3.4.1" "virtualenv"
create_executable "3.4.1" "pyvenv"
run pyenv-virtualenv --python=python3 venv
assert_success
assert_output <<OUT
PYENV_VERSION=3.4.1 pip install virtualenv
PYENV_VERSION=3.4.1 virtualenv --python=python3 ${PYENV_ROOT}/versions/venv
rehashed
OUT
unstub_pyenv
unstub pyenv-exec
}
@test "install virtualenv with unsetting troublesome pip options" {
stub_pyenv "3.2.1"
stub pyenv-prefix " : echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'"
stub pyenv-prefix " : echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'"
stub pyenv-exec "echo PIP_REQUIRE_VENV=\${PIP_REQUIRE_VENV} PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
stub pyenv-exec "echo PIP_REQUIRE_VENV=\${PIP_REQUIRE_VENV} PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
stub pyenv-exec "echo 3.2"
remove_executable "3.2.1" "virtualenv"
remove_executable "3.2.1" "pyvenv"
PIP_REQUIRE_VENV="true" run pyenv-virtualenv venv
assert_success
assert_output <<OUT
PIP_REQUIRE_VENV= PYENV_VERSION=3.2.1 pip install virtualenv
PIP_REQUIRE_VENV= PYENV_VERSION=3.2.1 virtualenv ${PYENV_ROOT}/versions/venv
rehashed
OUT
unstub_pyenv
unstub pyenv-exec
}
@test "install pip without using ensurepip" {
stub_pyenv "3.3.5"
stub pyenv-prefix " : echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'"
stub pyenv-prefix " : echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'"
stub pyenv-which "pip : echo no pip; false"
stub pyenv-exec "echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
stub pyenv-exec "echo PYENV_VERSION=\${PYENV_VERSION} no ensurepip; false"
stub pyenv-exec "echo PYENV_VERSION=\${PYENV_VERSION} no setuptools; false"
stub pyenv-exec "echo PYENV_VERSION=\${PYENV_VERSION} setuptools"
stub pyenv-exec "bin=\"${PYENV_ROOT}/versions/venv/bin\";mkdir -p \"\$bin\";touch \"\$bin/pip\";echo PYENV_VERSION=\${PYENV_VERSION} pip"
stub pyenv-exec "echo 3.3"
stub curl "echo ez_setup.py"
stub curl "echo get_pip.py"
remove_executable "3.3.5" "virtualenv"
create_executable "3.3.5" "pyvenv"
run pyenv-virtualenv venv
assert_success
assert_output <<OUT
PYENV_VERSION=3.3.5 pyvenv ${PYENV_ROOT}/versions/venv
PYENV_VERSION=venv no ensurepip
PYENV_VERSION=venv setuptools
PYENV_VERSION=venv pip
rehashed
OUT
assert [ -e "${PYENV_ROOT}/versions/venv/bin/pip" ]
unstub_pyenv
unstub pyenv-which
unstub pyenv-exec
}

109
test/stubs/stub Executable file
View File

@@ -0,0 +1,109 @@
#!/usr/bin/env bash
set -e
status=0
program="${0##*/}"
PROGRAM="$(echo "$program" | tr a-z- A-Z_)"
[ -n "$TMPDIR" ] || TMPDIR="/tmp"
_STUB_PLAN="${PROGRAM}_STUB_PLAN"
_STUB_RUN="${PROGRAM}_STUB_RUN"
_STUB_INDEX="${PROGRAM}_STUB_INDEX"
_STUB_RESULT="${PROGRAM}_STUB_RESULT"
_STUB_END="${PROGRAM}_STUB_END"
_STUB_DEBUG="${PROGRAM}_STUB_DEBUG"
if [ -n "${!_STUB_DEBUG}" ]; then
echo "$program" "$@" >&${!_STUB_DEBUG}
fi
[ -e "${!_STUB_PLAN}" ] || exit 1
[ -n "${!_STUB_RUN}" ] || eval "${_STUB_RUN}"="${TMPDIR}/${program}-stub-run"
# Initialize or load the stub run information.
eval "${_STUB_INDEX}"=1
eval "${_STUB_RESULT}"=0
[ ! -e "${!_STUB_RUN}" ] || source "${!_STUB_RUN}"
# Loop over each line in the plan.
index=0
while IFS= read -r line; do
index=$(($index + 1))
if [ -z "${!_STUB_END}" ] && [ $index -eq "${!_STUB_INDEX}" ]; then
# We found the plan line we're interested in.
# Start off by assuming success.
result=0
# Split the line into an array of arguments to
# match and a command to run to produce output.
command=" $line"
if [ "$command" != "${command/ : }" ]; then
patterns="${command%% : *}"
command="${command#* : }"
fi
# Naively split patterns by whitespace for now.
# In the future, use a sed script to split while
# respecting quoting.
set -f
patterns=($patterns)
set +f
arguments=("$@")
# Match the expected argument patterns to actual
# arguments.
for (( i=0; i<${#patterns[@]}; i++ )); do
pattern="${patterns[$i]}"
argument="${arguments[$i]}"
case "$argument" in
$pattern ) ;;
* ) result=1 ;;
esac
done
# If the arguments matched, evaluate the command
# in a subshell. Otherwise, log the failure.
if [ $result -eq 0 ] ; then
set +e
( eval "$command" )
status="$?"
set -e
else
eval "${_STUB_RESULT}"=1
fi
fi
done < "${!_STUB_PLAN}"
if [ -n "${!_STUB_END}" ]; then
# Clean up the run file.
rm -f "${!_STUB_RUN}"
# If the number of lines in the plan is larger than
# the requested index, we failed.
if [ $index -ge "${!_STUB_INDEX}" ]; then
eval "${_STUB_RESULT}"=1
fi
# Return the result.
exit "${!_STUB_RESULT}"
else
# If the requested index is larger than the number
# of lines in the plan file, we failed.
if [ "${!_STUB_INDEX}" -gt $index ]; then
eval "${_STUB_RESULT}"=1
fi
# Write out the run information.
{ echo "${_STUB_INDEX}=$((${!_STUB_INDEX} + 1))"
echo "${_STUB_RESULT}=${!_STUB_RESULT}"
} > "${!_STUB_RUN}"
exit "$status"
fi

108
test/test_helper.bash Normal file
View File

@@ -0,0 +1,108 @@
export TMP="$BATS_TEST_DIRNAME/tmp"
PATH=/usr/bin:/usr/sbin:/bin/:/sbin
PATH="$BATS_TEST_DIRNAME/../bin:$PATH"
PATH="$TMP/bin:$PATH"
export PATH
teardown() {
rm -fr "$TMP"/*
}
stub() {
local program="$1"
local prefix="$(echo "$program" | tr a-z- A-Z_)"
shift
export "${prefix}_STUB_PLAN"="${TMP}/${program}-stub-plan"
export "${prefix}_STUB_RUN"="${TMP}/${program}-stub-run"
export "${prefix}_STUB_END"=
mkdir -p "${TMP}/bin"
ln -sf "${BATS_TEST_DIRNAME}/stubs/stub" "${TMP}/bin/${program}"
touch "${TMP}/${program}-stub-plan"
for arg in "$@"; do printf "%s\n" "$arg" >> "${TMP}/${program}-stub-plan"; done
}
unstub() {
local program="$1"
local prefix="$(echo "$program" | tr a-z- A-Z_)"
local path="${TMP}/bin/${program}"
export "${prefix}_STUB_END"=1
local STATUS=0
"$path" || STATUS="$?"
rm -f "$path"
rm -f "${TMP}/${program}-stub-plan" "${TMP}/${program}-stub-run"
return "$STATUS"
}
assert() {
if ! "$@"; then
flunk "failed: $@"
fi
}
flunk() {
{ if [ "$#" -eq 0 ]; then cat -
else echo "$@"
fi
} | sed "s:${TMP}:\${TMP}:g" >&2
return 1
}
assert_success() {
if [ "$status" -ne 0 ]; then
{ echo "command failed with exit status $status"
echo "output: $output"
} | flunk
elif [ "$#" -gt 0 ]; then
assert_output "$1"
fi
}
assert_failure() {
if [ "$status" -eq 0 ]; then
flunk "expected failed exit status"
elif [ "$#" -gt 0 ]; then
assert_output "$1"
fi
}
assert_equal() {
if [ "$1" != "$2" ]; then
{ echo "expected: $1"
echo "actual: $2"
} | flunk
fi
}
assert_output() {
local expected
if [ $# -eq 0 ]; then expected="$(cat -)"
else expected="$1"
fi
assert_equal "$expected" "$output"
}
assert_output_contains() {
local expected="$1"
echo "$output" | grep -F "$expected" >/dev/null || {
{ echo "expected output to contain $expected"
echo "actual: $output"
} | flunk
}
}
create_executable() {
mkdir -p "${PYENV_ROOT}/versions/$1/bin"
touch "${PYENV_ROOT}/versions/$1/bin/$2"
chmod +x "${PYENV_ROOT}/versions/$1/bin/$2"
}
remove_executable() {
rm -f "${PYENV_ROOT}/versions/$1/bin/$2"
}

1
test/tmp/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
*

43
test/version.bats Normal file
View File

@@ -0,0 +1,43 @@
#!/usr/bin/env bats
load test_helper
setup() {
export PYENV_ROOT="${TMP}/pyenv"
export PYENV_VIRTUALENV_VERSION="20140615"
}
@test "display virtualenv version" {
stub pyenv-prefix "echo '${PYENV_ROOT}/versions/2.7.7'"
stub pyenv-prefix "echo '${PYENV_ROOT}/versions/2.7.7'"
stub pyenv-exec "virtualenv --version : echo \"1.11\""
create_executable "2.7.7" "virtualenv"
remove_executable "2.7.7" "pyvenv"
run pyenv-virtualenv --version
assert_success
assert_output "pyenv-virtualenv ${PYENV_VIRTUALENV_VERSION} (virtualenv 1.11)"
unstub pyenv-prefix
unstub pyenv-exec
}
@test "display pyvenv version" {
stub pyenv-prefix "echo '${PYENV_ROOT}/versions/3.4.1'"
stub pyenv-prefix "echo '${PYENV_ROOT}/versions/3.4.1'"
stub pyenv-which "pyvenv : echo \"${PYENV_ROOT}/versions/3.4.1/bin/pyvenv\""
stub pyenv-root "echo \"${PYENV_ROOT}\""
remove_executable "3.4.1" "virtualenv"
create_executable "3.4.1" "pyvenv"
run pyenv-virtualenv --version
assert_success
assert_output "pyenv-virtualenv ${PYENV_VIRTUALENV_VERSION} (pyvenv 3.4.1)"
unstub pyenv-prefix
unstub pyenv-root
}

145
test/virtualenv-prefix.bats Normal file
View File

@@ -0,0 +1,145 @@
#!/usr/bin/env bats
load test_helper
setup() {
export PYENV_ROOT="${TMP}/pyenv"
}
create_virtualenv() {
mkdir -p "${PYENV_ROOT}/versions/$1/bin"
touch "${PYENV_ROOT}/versions/$1/bin/activate"
}
remove_virtualenv() {
rm -fr "${PYENV_ROOT}/versions/$1"
}
@test "display prefix with using sys.real_prefix" {
stub pyenv-version-name "echo venv27"
stub pyenv-prefix "venv27 : echo \"${PYENV_ROOT}/versions/venv27\""
stub pyenv-exec "echo \"${PYENV_ROOT}/versions/2.7.6\""
create_virtualenv "venv27"
PYENV_VERSION="venv27" run pyenv-virtualenv-prefix
unstub pyenv-version-name
unstub pyenv-prefix
unstub pyenv-exec
remove_virtualenv "venv27"
assert_success
assert_output <<OUT
${PYENV_ROOT}/versions/2.7.6
OUT
}
@test "display prefixes with using sys.real_prefix" {
stub pyenv-version-name "echo venv27:venv32"
stub pyenv-prefix "venv27 : echo \"${PYENV_ROOT}/versions/venv27\"" \
"venv32 : echo \"${PYENV_ROOT}/versions/venv32\""
stub pyenv-exec "echo \"${PYENV_ROOT}/versions/2.7.6\"" \
"echo \"${PYENV_ROOT}/versions/3.2.1\""
create_virtualenv "venv27"
create_virtualenv "venv32"
PYENV_VERSION="venv27:venv32" run pyenv-virtualenv-prefix
unstub pyenv-version-name
unstub pyenv-prefix
unstub pyenv-exec
remove_virtualenv "venv27"
remove_virtualenv "venv32"
assert_success
assert_output <<OUT
${PYENV_ROOT}/versions/2.7.6:${PYENV_ROOT}/versions/3.2.1
OUT
}
@test "display prefix with using sys.base_prefix" {
stub pyenv-version-name "echo venv33"
stub pyenv-prefix "venv33 : echo \"${PYENV_ROOT}/versions/venv33\""
stub pyenv-exec "false" \
"echo \"${PYENV_ROOT}/versions/3.3.3\""
create_virtualenv "venv33"
PYENV_VERSION="venv33" run pyenv-virtualenv-prefix
unstub pyenv-version-name
unstub pyenv-prefix
unstub pyenv-exec
remove_virtualenv "venv33"
assert_success
assert_output <<OUT
${PYENV_ROOT}/versions/3.3.3
OUT
}
@test "display prefixes with using sys.base_prefix" {
stub pyenv-version-name "echo venv33:venv34"
stub pyenv-prefix "venv33 : echo \"${PYENV_ROOT}/versions/venv33\"" \
"venv34 : echo \"${PYENV_ROOT}/versions/venv34\""
stub pyenv-exec "false" \
"echo \"${PYENV_ROOT}/versions/3.3.3\"" \
"false" \
"echo \"${PYENV_ROOT}/versions/3.4.0\""
create_virtualenv "venv33"
create_virtualenv "venv34"
PYENV_VERSION="venv33:venv34" run pyenv-virtualenv-prefix
unstub pyenv-version-name
unstub pyenv-prefix
unstub pyenv-exec
remove_virtualenv "venv33"
remove_virtualenv "venv34"
assert_success
assert_output <<OUT
${PYENV_ROOT}/versions/3.3.3:${PYENV_ROOT}/versions/3.4.0
OUT
}
@test "should fail if the version is not a virtualenv" {
stub pyenv-version-name "echo 3.4.0"
stub pyenv-prefix "3.4.0 : echo \"${PYENV_ROOT}/versions/3.4.0\""
mkdir -p "${PYENV_ROOT}/versions/3.4.0"
PYENV_VERSION="3.4.0" run pyenv-virtualenv-prefix
unstub pyenv-version-name
unstub pyenv-prefix
rmdir "${PYENV_ROOT}/versions/3.4.0"
assert_failure
assert_output <<OUT
pyenv-virtualenv: version \`3.4.0' is not a virtualenv
OUT
}
@test "should fail if one of the versions is not a virtualenv" {
stub pyenv-version-name "echo venv33:3.4.0"
stub pyenv-prefix "venv33 : echo \"${PYENV_ROOT}/versions/venv33\"" \
"3.4.0 : echo \"${PYENV_ROOT}/versions/3.4.0\""
stub pyenv-exec "false" \
"echo \"${PYENV_ROOT}/versions/3.3.3\""
create_virtualenv "venv33"
mkdir -p "${PYENV_ROOT}/versions/3.4.0"
PYENV_VERSION="venv33:3.4.0" run pyenv-virtualenv-prefix
unstub pyenv-version-name
unstub pyenv-prefix
unstub pyenv-exec
remove_virtualenv "venv33"
rmdir "${PYENV_ROOT}/versions/3.4.0"
assert_failure
assert_output <<OUT
pyenv-virtualenv: version \`3.4.0' is not a virtualenv
OUT
}

101
test/virtualenv.bats Normal file
View File

@@ -0,0 +1,101 @@
#!/usr/bin/env bats
load test_helper
setup() {
export PYENV_ROOT="${TMP}/pyenv"
}
stub_pyenv() {
export PYENV_VERSION="$1"
create_executable "${PYENV_VERSION}" "virtualenv"
remove_executable "${PYENV_VERSION}" "pyvenv"
stub pyenv-prefix "echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'"
stub pyenv-prefix "echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'"
stub pyenv-prefix "echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'"
stub pyenv-hooks "virtualenv : echo"
stub pyenv-rehash " : echo rehashed"
}
unstub_pyenv() {
unset PYENV_VERSION
unstub pyenv-prefix
unstub pyenv-hooks
unstub pyenv-rehash
}
@test "create virtualenv from given version" {
stub_pyenv "3.2.1"
stub pyenv-exec "echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
stub pyenv-exec "echo 3.2"
run pyenv-virtualenv "3.2.1" "venv"
assert_success
assert_output <<OUT
PYENV_VERSION=3.2.1 virtualenv ${PYENV_ROOT}/versions/venv
rehashed
OUT
unstub_pyenv
unstub pyenv-exec
}
@test "create virtualenv from current version" {
stub_pyenv "3.2.1"
stub pyenv-version-name "echo \${PYENV_VERSION}"
stub pyenv-exec "echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
stub pyenv-exec "echo 3.2"
run pyenv-virtualenv venv
assert_success
assert_output <<OUT
PYENV_VERSION=3.2.1 virtualenv ${PYENV_ROOT}/versions/venv
rehashed
OUT
unstub_pyenv
unstub pyenv-version-name
unstub pyenv-exec
}
@test "create virtualenv with short options" {
stub_pyenv "3.2.1"
stub pyenv-version-name "echo \${PYENV_VERSION}"
stub pyenv-exec "echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
stub pyenv-exec "echo 3.2"
run pyenv-virtualenv -v -p python venv
assert_success
assert_output <<OUT
PYENV_VERSION=3.2.1 virtualenv --verbose --python=python ${PYENV_ROOT}/versions/venv
rehashed
OUT
unstub_pyenv
unstub pyenv-version-name
unstub pyenv-exec
}
@test "create virtualenv with long options" {
stub_pyenv "3.2.1"
stub pyenv-version-name "echo \${PYENV_VERSION}"
stub pyenv-exec "echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
stub pyenv-exec "echo 3.2"
run pyenv-virtualenv --verbose --python=python venv
assert_success
assert_output <<OUT
PYENV_VERSION=3.2.1 virtualenv --verbose --python=python ${PYENV_ROOT}/versions/venv
rehashed
OUT
unstub_pyenv
unstub pyenv-version-name
unstub pyenv-exec
}

68
test/virtualenvs.bats Normal file
View File

@@ -0,0 +1,68 @@
#!/usr/bin/env bats
load test_helper
setup() {
export PYENV_ROOT="${TMP}/pyenv"
mkdir -p "${PYENV_ROOT}/versions/2.7.6"
mkdir -p "${PYENV_ROOT}/versions/3.3.3"
mkdir -p "${PYENV_ROOT}/versions/venv27"
mkdir -p "${PYENV_ROOT}/versions/venv33"
}
@test "list virtual environments only" {
stub pyenv-version-name ": echo system"
stub pyenv-versions "--bare : echo \"system\";echo \"2.7.6\";echo \"3.3.3\";echo \"venv27\";echo \"venv33\""
stub pyenv-virtualenv-prefix "2.7.6 : false"
stub pyenv-virtualenv-prefix "3.3.3 : false"
stub pyenv-virtualenv-prefix "venv27 : echo \"${PYENV_ROOT}/versions/2.7.6\""
stub pyenv-virtualenv-prefix "venv33 : echo \"${PYENV_ROOT}/versions/3.3.3\""
run pyenv-virtualenvs
unstub pyenv-version-name
unstub pyenv-versions
unstub pyenv-virtualenv-prefix
assert_success
assert_output <<OUT
venv27 (created from ${PYENV_ROOT}/versions/2.7.6)
venv33 (created from ${PYENV_ROOT}/versions/3.3.3)
OUT
}
@test "list virtual environments with hit prefix" {
stub pyenv-version-name ": echo venv33"
stub pyenv-versions "--bare : echo \"system\";echo \"venv27\";echo \"venv33\""
stub pyenv-virtualenv-prefix "venv27 : echo \"/usr\""
stub pyenv-virtualenv-prefix "venv33 : echo \"/usr\""
run pyenv-virtualenvs
unstub pyenv-version-name
unstub pyenv-versions
unstub pyenv-virtualenv-prefix
assert_success
assert_output <<OUT
venv27 (created from /usr)
* venv33 (created from /usr)
OUT
}
@test "list virtual environments with --bare" {
stub pyenv-versions "--bare : echo \"system\";echo \"venv27\";echo \"venv33\""
stub pyenv-virtualenv-prefix "venv27 : echo \"/usr\""
stub pyenv-virtualenv-prefix "venv33 : echo \"/usr\""
run pyenv-virtualenvs --bare
unstub pyenv-versions
unstub pyenv-virtualenv-prefix
assert_success
assert_output <<OUT
venv27
venv33
OUT
}