From 1d3f46653c29527f123e1fa3038b83cb663eac4a Mon Sep 17 00:00:00 2001 From: Tzu-ping Chung Date: Thu, 5 Jul 2018 01:03:02 +0800 Subject: [PATCH 1/7] Always use virtualenv directly, not "pew new" This removed Pew references in virtualenv creation. --- pipenv/core.py | 77 +++++++++++++++++++++++++------------------------- 1 file changed, 39 insertions(+), 38 deletions(-) diff --git a/pipenv/core.py b/pipenv/core.py index aec0b3d98a..918f9066f8 100644 --- a/pipenv/core.py +++ b/pipenv/core.py @@ -871,35 +871,16 @@ def convert_three_to_python(three, python): def do_create_virtualenv(python=None, site_packages=False, pypi_mirror=None): """Creates a virtualenv.""" click.echo( - crayons.normal(u"Creating a virtualenv for this project…", bold=True), err=True + crayons.normal(u"Creating a virtualenv for this project…", bold=True), + err=True, ) click.echo( - u"Pipfile: {0}".format(crayons.red(project.pipfile_location, bold=True)), + u"Pipfile: {0}".format( + crayons.red(project.pipfile_location, bold=True), + ), err=True, ) - # The user wants the virtualenv in the project. - if project.is_venv_in_project(): - cmd = [ - sys.executable, - "-m", - "virtualenv", - project.virtualenv_location, - "--prompt=({0})".format(project.name), - ] - # Pass site-packages flag to virtualenv, if desired… - if site_packages: - cmd.append("--system-site-packages") - else: - # Default: use pew. - cmd = [ - sys.executable, - "-m", - "pipenv.pew", - "new", - "-d", - "-a", - project.project_directory, - ] + # Default to using sys.executable, if Python wasn't provided. if not python: python = sys.executable @@ -912,14 +893,35 @@ def do_create_virtualenv(python=None, site_packages=False, pypi_mirror=None): ), err=True, ) - cmd = cmd + ["-p", python] - if not project.is_venv_in_project(): - cmd = cmd + ["--", project.virtualenv_name] + + cmd = [ + sys.executable, + "-m", + "virtualenv", + project.virtualenv_location, + "--prompt=({0})".format(project.name), + "--python={0}".format(python), + ] + + # Pass site-packages flag to virtualenv, if desired… + if site_packages: + click.echo( + crayons.normal(u"Making site-packages available…", bold=True), + err=True, + ) + cmd.append("--system-site-packages") + + if pypi_mirror: + pip_config = {"PIP_INDEX_URL": fs_str(pypi_mirror)} + else: + pip_config = {} + # Actually create the virtualenv. with spinner(): try: - pip_config = {"PIP_INDEX_URL": fs_str(pypi_mirror)} if pypi_mirror else {} - c = delegator.run(cmd, block=False, timeout=PIPENV_TIMEOUT, env=pip_config) + c = delegator.run( + cmd, block=False, timeout=PIPENV_TIMEOUT, env=pip_config, + ) except OSError: click.echo( "{0}: it looks like {1} is not in your {2}. " @@ -933,14 +935,13 @@ def do_create_virtualenv(python=None, site_packages=False, pypi_mirror=None): ) sys.exit(1) click.echo(crayons.blue(c.out), err=True) - # Enable site-packages, if desired - if not project.is_venv_in_project() and site_packages: - click.echo( - crayons.normal(u"Making site-packages available…", bold=True), err=True - ) - os.environ["VIRTUAL_ENV"] = project.virtualenv_location - delegator.run("pipenv run pewtwo toggleglobalsitepackages") - del os.environ["VIRTUAL_ENV"] + + # Associate project directory with the environment. + # This mimics Pew's "setproject". + project_file_name = os.path.join(project.virtualenv_location, '.project') + with open(project_file_name, 'w') as f: + f.write(fs_str(project.project_directory)) + # Say where the virtualenv is. do_where(virtualenv=True, bare=False) From 76e7b3e63f3a3ca97af2961a04be3376e6c007b4 Mon Sep 17 00:00:00 2001 From: Tzu-ping Chung Date: Thu, 5 Jul 2018 01:35:34 +0800 Subject: [PATCH 2/7] More explicit failures --- pipenv/core.py | 6 +++++- pipenv/project.py | 4 +++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/pipenv/core.py b/pipenv/core.py index 918f9066f8..a14e1f6837 100644 --- a/pipenv/core.py +++ b/pipenv/core.py @@ -102,7 +102,11 @@ def spinner(): def which(command, location=None, allow_global=False): if not allow_global and location is None: - location = project.virtualenv_location or os.environ.get("VIRTUAL_ENV") + location = ( + project.virtualenv_location + or os.environ.get("VIRTUAL_ENV", "") + ) + assert location, "virtual environment not created" if not allow_global: if os.name == "nt": p = find_windows_executable(os.path.join(location, "Scripts"), command) diff --git a/pipenv/project.py b/pipenv/project.py index c13cf59798..6139112e02 100644 --- a/pipenv/project.py +++ b/pipenv/project.py @@ -319,12 +319,14 @@ def virtualenv_location(self): if self._virtualenv_location: return self._virtualenv_location + assert self.project_directory, "project not created" + # Default mode. if not self.is_venv_in_project(): loc = self._get_virtualenv_location(self.virtualenv_name) # The user wants the virtualenv in the project. else: - loc = os.sep.join(self.pipfile_location.split(os.sep)[:-1] + [".venv"]) + loc = os.path.join(self.project_directory, ".venv") self._virtualenv_location = loc return loc From 0023adea343f1466753f872c2122c1734272c97a Mon Sep 17 00:00:00 2001 From: Tzu-ping Chung Date: Thu, 5 Jul 2018 01:42:30 +0800 Subject: [PATCH 3/7] Ignore pytest_pypi.egg-info --- .../pytest-pypi/pytest_pypi.egg-info/PKG-INFO | 53 ------------------- .../pytest_pypi.egg-info/SOURCES.txt | 17 ------ .../pytest_pypi.egg-info/dependency_links.txt | 1 - .../pytest_pypi.egg-info/entry_points.txt | 3 -- .../pytest_pypi.egg-info/requires.txt | 2 - .../pytest_pypi.egg-info/top_level.txt | 1 - 6 files changed, 77 deletions(-) delete mode 100644 tests/pytest-pypi/pytest_pypi.egg-info/PKG-INFO delete mode 100644 tests/pytest-pypi/pytest_pypi.egg-info/SOURCES.txt delete mode 100644 tests/pytest-pypi/pytest_pypi.egg-info/dependency_links.txt delete mode 100644 tests/pytest-pypi/pytest_pypi.egg-info/entry_points.txt delete mode 100644 tests/pytest-pypi/pytest_pypi.egg-info/requires.txt delete mode 100644 tests/pytest-pypi/pytest_pypi.egg-info/top_level.txt diff --git a/tests/pytest-pypi/pytest_pypi.egg-info/PKG-INFO b/tests/pytest-pypi/pytest_pypi.egg-info/PKG-INFO deleted file mode 100644 index 50322734e4..0000000000 --- a/tests/pytest-pypi/pytest_pypi.egg-info/PKG-INFO +++ /dev/null @@ -1,53 +0,0 @@ -Metadata-Version: 1.1 -Name: pytest-pypi -Version: 0.1.1 -Summary: Easily test your HTTP library against a local copy of pypi -Home-page: https://github.com/kennethreitz/pytest-pypi -Author: Kenneth Reitz -Author-email: me@kennethreitz.org -License: MIT -Description: pytest-httpbin - ============== - - httpbin is an amazing web service for testing HTTP libraries. It has several - great endpoints that can test pretty much everything you need in a HTTP - library. The only problem is: maybe you don't want to wait for your tests to - travel across the Internet and back to make assertions against a remote web - service. - - Enter pytest-httpbin. Pytest-httpbin creates a pytest "fixture" that is - dependency-injected into your tests. It automatically starts up a HTTP server - in a separate thread running httpbin and provides your test with the URL in the - fixture. Check out this example: - - .. code-block:: python - - def test_that_my_library_works_kinda_ok(httpbin): - assert requests.get(httpbin.url + '/get/').status_code == 200 - - This replaces a test that might have looked like this before: - - .. code-block:: python - - def test_that_my_library_works_kinda_ok(): - assert requests.get('http://httpbin.org/get').status_code == 200 - - pytest-httpbin also supports https and includes its own CA cert you can use. - Check out `the full documentation`_ on the github page. - - .. _the full documentation: https://github.com/kevin1024/pytest-httpbin - -Keywords: pytest-pypi testing pytest pypi -Platform: UNKNOWN -Classifier: Development Status :: 3 - Alpha -Classifier: Intended Audience :: Developers -Classifier: Topic :: Software Development :: Testing -Classifier: Topic :: Software Development :: Libraries -Classifier: License :: OSI Approved :: MIT License -Classifier: Programming Language :: Python :: 2 -Classifier: Programming Language :: Python :: 2.6 -Classifier: Programming Language :: Python :: 2.7 -Classifier: Programming Language :: Python :: 3 -Classifier: Programming Language :: Python :: 3.4 -Classifier: Programming Language :: Python :: 3.5 -Classifier: Programming Language :: Python :: 3.6 diff --git a/tests/pytest-pypi/pytest_pypi.egg-info/SOURCES.txt b/tests/pytest-pypi/pytest_pypi.egg-info/SOURCES.txt deleted file mode 100644 index 6725abbc17..0000000000 --- a/tests/pytest-pypi/pytest_pypi.egg-info/SOURCES.txt +++ /dev/null @@ -1,17 +0,0 @@ -DESCRIPTION.rst -MANIFEST.in -README.md -setup.cfg -setup.py -pytest_pypi/__init__.py -pytest_pypi/app.py -pytest_pypi/certs.py -pytest_pypi/plugin.py -pytest_pypi/serve.py -pytest_pypi/version.py -pytest_pypi.egg-info/PKG-INFO -pytest_pypi.egg-info/SOURCES.txt -pytest_pypi.egg-info/dependency_links.txt -pytest_pypi.egg-info/entry_points.txt -pytest_pypi.egg-info/requires.txt -pytest_pypi.egg-info/top_level.txt \ No newline at end of file diff --git a/tests/pytest-pypi/pytest_pypi.egg-info/dependency_links.txt b/tests/pytest-pypi/pytest_pypi.egg-info/dependency_links.txt deleted file mode 100644 index 8b13789179..0000000000 --- a/tests/pytest-pypi/pytest_pypi.egg-info/dependency_links.txt +++ /dev/null @@ -1 +0,0 @@ - diff --git a/tests/pytest-pypi/pytest_pypi.egg-info/entry_points.txt b/tests/pytest-pypi/pytest_pypi.egg-info/entry_points.txt deleted file mode 100644 index d0bfd64f3b..0000000000 --- a/tests/pytest-pypi/pytest_pypi.egg-info/entry_points.txt +++ /dev/null @@ -1,3 +0,0 @@ -[pytest11] -pypi = pytest_pypi.plugin - diff --git a/tests/pytest-pypi/pytest_pypi.egg-info/requires.txt b/tests/pytest-pypi/pytest_pypi.egg-info/requires.txt deleted file mode 100644 index 53cec2f941..0000000000 --- a/tests/pytest-pypi/pytest_pypi.egg-info/requires.txt +++ /dev/null @@ -1,2 +0,0 @@ -Flask -six diff --git a/tests/pytest-pypi/pytest_pypi.egg-info/top_level.txt b/tests/pytest-pypi/pytest_pypi.egg-info/top_level.txt deleted file mode 100644 index 81f34cb52d..0000000000 --- a/tests/pytest-pypi/pytest_pypi.egg-info/top_level.txt +++ /dev/null @@ -1 +0,0 @@ -pytest_pypi From 96c2517f3b5edd8758119ac229e1be4cfee6454c Mon Sep 17 00:00:00 2001 From: Tzu-ping Chung Date: Thu, 5 Jul 2018 01:42:41 +0800 Subject: [PATCH 4/7] More explicit error --- pipenv/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pipenv/core.py b/pipenv/core.py index a14e1f6837..442227ff0a 100644 --- a/pipenv/core.py +++ b/pipenv/core.py @@ -106,7 +106,7 @@ def which(command, location=None, allow_global=False): project.virtualenv_location or os.environ.get("VIRTUAL_ENV", "") ) - assert location, "virtual environment not created" + assert location and os.path.exists(location), "virtualenv not created" if not allow_global: if os.name == "nt": p = find_windows_executable(os.path.join(location, "Scripts"), command) From 341297272a659db95b90db553e99d65ac73c15ce Mon Sep 17 00:00:00 2001 From: Tzu-ping Chung Date: Thu, 5 Jul 2018 01:56:05 +0800 Subject: [PATCH 5/7] Need to be able to get venv path before creating --- pipenv/core.py | 2 +- pipenv/project.py | 28 ++++++++-------------------- 2 files changed, 9 insertions(+), 21 deletions(-) diff --git a/pipenv/core.py b/pipenv/core.py index 442227ff0a..1a9bccce44 100644 --- a/pipenv/core.py +++ b/pipenv/core.py @@ -902,9 +902,9 @@ def do_create_virtualenv(python=None, site_packages=False, pypi_mirror=None): sys.executable, "-m", "virtualenv", - project.virtualenv_location, "--prompt=({0})".format(project.name), "--python={0}".format(python), + project.get_location_for_virtualenv(), ] # Pass site-packages flag to virtualenv, if desired… diff --git a/pipenv/project.py b/pipenv/project.py index 6139112e02..df6b41120d 100644 --- a/pipenv/project.py +++ b/pipenv/project.py @@ -237,12 +237,10 @@ def virtualenv_exists(self): return False - @classmethod - def _get_virtualenv_location(cls, name): - venv = get_workon_home() / name - if not venv.exists(): - return "" - return "{0}".format(venv) + def get_location_for_virtualenv(self): + if self.is_venv_in_project(): + return os.path.join(self.project_directory, ".venv") + return str(get_workon_home().joinpath(self.virtualenv_name)) @classmethod def _sanitize(cls, name): @@ -315,20 +313,10 @@ def virtualenv_location(self): if PIPENV_VIRTUALENV: return PIPENV_VIRTUALENV - # Use cached version, if available. - if self._virtualenv_location: - return self._virtualenv_location - - assert self.project_directory, "project not created" - - # Default mode. - if not self.is_venv_in_project(): - loc = self._get_virtualenv_location(self.virtualenv_name) - # The user wants the virtualenv in the project. - else: - loc = os.path.join(self.project_directory, ".venv") - self._virtualenv_location = loc - return loc + if not self._virtualenv_location: # Use cached version, if available. + assert self.project_directory, "project not created" + self._virtualenv_location = self.get_location_for_virtualenv() + return self._virtualenv_location @property def virtualenv_src_location(self): From 676d5a2f75b3e5e2116fc5b31d206581cdb923aa Mon Sep 17 00:00:00 2001 From: Tzu-ping Chung Date: Thu, 5 Jul 2018 02:59:12 +0800 Subject: [PATCH 6/7] Use exists() to check for virtualenv instead --- pipenv/project.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pipenv/project.py b/pipenv/project.py index df6b41120d..7f40576d99 100644 --- a/pipenv/project.py +++ b/pipenv/project.py @@ -280,7 +280,7 @@ def get_name(name, location): if ( fnmatch.fnmatch('A', 'a') or self.is_venv_in_project() - or self._get_virtualenv_location(venv_name) + or get_workon_home().joinpath(venv_name).exists() ): return clean_name, encoded_hash From 999761130e910bb211c75467d66be7db7547e7fe Mon Sep 17 00:00:00 2001 From: Tzu-ping Chung Date: Thu, 5 Jul 2018 15:37:33 +0800 Subject: [PATCH 7/7] Raise explicit exception for wrong which context --- pipenv/core.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pipenv/core.py b/pipenv/core.py index 1a9bccce44..aaec1bf9fa 100644 --- a/pipenv/core.py +++ b/pipenv/core.py @@ -106,7 +106,8 @@ def which(command, location=None, allow_global=False): project.virtualenv_location or os.environ.get("VIRTUAL_ENV", "") ) - assert location and os.path.exists(location), "virtualenv not created" + if not location and os.path.exists(location): + raise RuntimeError("virtualenv not created nor specified") if not allow_global: if os.name == "nt": p = find_windows_executable(os.path.join(location, "Scripts"), command)