Skip to content

Commit

Permalink
Migrate virtualenv detection to PEP 405
Browse files Browse the repository at this point in the history
  • Loading branch information
uranusjr committed Feb 11, 2020
1 parent 719d19f commit 3764555
Showing 1 changed file with 2 additions and 48 deletions.
50 changes: 2 additions & 48 deletions src/pip/_internal/utils/virtualenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import logging
import os
import re
import site
import sys

from pip._internal.utils.typing import MYPY_CHECK_RUNNING
Expand All @@ -17,7 +16,7 @@
)


def _running_under_venv():
def running_under_virtualenv():
# type: () -> bool
"""Checks if sys.base_prefix and sys.prefix match.
Expand All @@ -26,23 +25,6 @@ def _running_under_venv():
return sys.prefix != getattr(sys, "base_prefix", sys.prefix)


def _running_under_regular_virtualenv():
# type: () -> bool
"""Checks if sys.real_prefix is set.
This handles virtual environments created with pypa's virtualenv.
"""
# pypa/virtualenv case
return hasattr(sys, 'real_prefix')


def running_under_virtualenv():
# type: () -> bool
"""Return True if we're running inside a virtualenv, False otherwise.
"""
return _running_under_venv() or _running_under_regular_virtualenv()


def _get_pyvenv_cfg_lines():
# type: () -> Optional[List[str]]
"""Reads {sys.prefix}/pyvenv.cfg and returns its contents as list of lines
Expand All @@ -57,7 +39,7 @@ def _get_pyvenv_cfg_lines():
return None


def _no_global_under_venv():
def virtualenv_no_global():
# type: () -> bool
"""Check `{sys.prefix}/pyvenv.cfg` for system site-packages inclusion
Expand Down Expand Up @@ -85,31 +67,3 @@ def _no_global_under_venv():
if match is not None and match.group('value') == 'false':
return True
return False


def _no_global_under_regular_virtualenv():
# type: () -> bool
"""Check if "no-global-site-packages.txt" exists beside site.py
This mirrors logic in pypa/virtualenv for determining whether system
site-packages are visible in the virtual environment.
"""
site_mod_dir = os.path.dirname(os.path.abspath(site.__file__))
no_global_site_packages_file = os.path.join(
site_mod_dir, 'no-global-site-packages.txt',
)
return os.path.exists(no_global_site_packages_file)


def virtualenv_no_global():
# type: () -> bool
"""Returns a boolean, whether running in venv with no system site-packages.
"""

if _running_under_regular_virtualenv():
return _no_global_under_regular_virtualenv()

if _running_under_venv():
return _no_global_under_venv()

return False

0 comments on commit 3764555

Please sign in to comment.