Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always use virtualenv directly, not "pew new" #2518

Merged
merged 8 commits into from
Jul 5, 2018
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 44 additions & 39 deletions pipenv/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 and os.path.exists(location), "virtualenv not created"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably stop using asserts in our codebase as tests, since a lot of production machines disable the debug flag

if not allow_global:
if os.name == "nt":
p = find_windows_executable(os.path.join(location, "Scripts"), command)
Expand Down Expand Up @@ -871,35 +875,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
Expand All @@ -912,14 +897,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",
"--prompt=({0})".format(project.name),
"--python={0}".format(python),
project.get_location_for_virtualenv(),
]

# 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}. "
Expand All @@ -933,14 +939,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)

Expand Down
28 changes: 9 additions & 19 deletions pipenv/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -282,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

Expand Down Expand Up @@ -315,18 +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

# 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"])
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):
Expand Down
53 changes: 0 additions & 53 deletions tests/pytest-pypi/pytest_pypi.egg-info/PKG-INFO

This file was deleted.

17 changes: 0 additions & 17 deletions tests/pytest-pypi/pytest_pypi.egg-info/SOURCES.txt

This file was deleted.

This file was deleted.

3 changes: 0 additions & 3 deletions tests/pytest-pypi/pytest_pypi.egg-info/entry_points.txt

This file was deleted.

2 changes: 0 additions & 2 deletions tests/pytest-pypi/pytest_pypi.egg-info/requires.txt

This file was deleted.

1 change: 0 additions & 1 deletion tests/pytest-pypi/pytest_pypi.egg-info/top_level.txt

This file was deleted.