Skip to content

Commit

Permalink
pip.py falls back to virtualenv pip/python
Browse files Browse the repository at this point in the history
  • Loading branch information
Joseph Yu authored and j0yu committed Apr 18, 2020
1 parent 6b82114 commit 5e180d9
Showing 1 changed file with 56 additions and 19 deletions.
75 changes: 56 additions & 19 deletions src/rez/pip.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from rez.vendor.distlib.markers import interpret
from rez.vendor.distlib.util import parse_name_and_version
from rez.vendor.enum.enum import Enum
from rez.vendor.packaging.version import Version as PackagingVersion
from rez.vendor.packaging.specifiers import Specifier
from rez.vendor.six.six import StringIO
from rez.resolved_context import ResolvedContext
from rez.utils.execution import Popen
Expand All @@ -31,6 +33,9 @@
from textwrap import dedent


PIP_SPECIFIER = Specifier(">=19") # rez pip only compatible with pip>=19


class InstallMode(Enum):
# don't install dependencies. Build may fail, for example the package may
# need to compile against a dependency. Will work for pure python though.
Expand Down Expand Up @@ -81,31 +86,35 @@ def find_pip(pip_version=None, python_version=None):
"""
py_exe = None
context = None
found_pip_version = None

py_exe, pip_version, context = find_pip_from_context(
python_version,
pip_version=pip_version
)

if not py_exe:
py_exe, pip_version, context = find_pip_from_context(
python_version,
pip_version=pip_version or "latest"
)
for version in [pip_version, pip_version or "latest"]:
try:
py_exe, found_pip_version, context = find_pip_from_context(
python_version,
pip_version=version
)
if _check_found(py_exe, found_pip_version):
break
except BuildError as error:
print_warning(str(error))

if not py_exe:
else: # No break from: for version in [pip_version,...]
import pip
pip_version = pip.__version__
found_pip_version = pip.__version__
py_exe = sys.executable
print_warning(
"Found no pip in python and pip package; "
"falling back to pip installed in rez own virtualenv (version %s)",
pip_version
print_warning("Found no pip in any python and/or pip packages!")
print_warning("Falling back to pip installed in rez own virtualenv:")
logging_arguments = (
("pip", found_pip_version, pip.__file__),
("python", ".".join(map(str, sys.version_info[:3])), py_exe),
)
for warn_args in logging_arguments:
print_warning("%10s: %s (%s)", *warn_args)

pip_major = pip_version.split('.')[0]
if int(pip_major) < 19:
raise RezSystemError("pip >= 19 is required! Please update your pip.")
if not _check_found(py_exe, found_pip_version, log_invalid=False):
message = "pip{specifier} is required! Please update your pip."
raise RezSystemError(message.format(specifier=PIP_SPECIFIER))

return py_exe, context

Expand Down Expand Up @@ -559,6 +568,34 @@ def _cmd(context, command):
raise BuildError("Failed to download source with pip: %s" % cmd_str)


def _check_found(py_exe, version_text, log_invalid=True):
"""Check the Python and pip version text found.
Args:
py_exe (str or None): Python executable path found, if any.
version_text (str or None): Pip version found, if any.
log_invalid (bool): Whether to log messages if found invalid.
Returns:
bool: Python is OK and pip version fits against ``PIP_SPECIFIER``.
"""
is_valid = True
message = "Needs pip%s, but found '%s' for Python '%s'"

if version_text is None or not py_exe:
is_valid = False
if log_invalid:
print_debug(message, PIP_SPECIFIER, version_text, py_exe)


elif PackagingVersion(version_text) not in PIP_SPECIFIER:
is_valid = False
if log_invalid:
print_warning(message, PIP_SPECIFIER, version_text, py_exe)

return is_valid


_verbose = config.debug("package_release")


Expand Down

0 comments on commit 5e180d9

Please sign in to comment.