Skip to content

Commit

Permalink
Stop importing pip, use subprocesses instead. Will close #710
Browse files Browse the repository at this point in the history
  • Loading branch information
dwpaley committed Oct 4, 2022
1 parent b11cbf2 commit dc000ad
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 24 deletions.
3 changes: 3 additions & 0 deletions libtbx/auto_build/conda_envs/cctbx.devenv.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ dependencies:
- dials-data
- pytest-mock
- pytest-xdist

# extra
- packaging
1 change: 1 addition & 0 deletions libtbx/auto_build/conda_envs/cctbxlite.devenv.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ dependencies:
# extra
- libsvm_py
- pytest
- packaging

# docs
- docutils
Expand Down
39 changes: 15 additions & 24 deletions libtbx/pkg_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,36 @@
import itertools
import os
import sys
import subprocess

import libtbx.load_env

try:
import pip
pip_version_cmd = ['python', '-c', 'import pip; print(pip.__version__)']
pip_version_str = subprocess.check_output(pip_version_cmd).decode().strip()
import pkg_resources

# Don't run if pip version < 9.0.0.
# There is no technical reason for this, the code should work still.
# But in the interest of not upsetting legacy build systems let's be cautious.
if not all(symbol in dir(pkg_resources) for symbol in
('parse_version', 'require', 'DistributionNotFound', 'VersionConflict')) \
or pkg_resources.parse_version(pip.__version__) < pkg_resources.parse_version('9.0.0'):
pip = None
or pkg_resources.parse_version(pip_version_str) < pkg_resources.parse_version('9.0.0'):
use_pip = False
pkg_resources = None
if pip:
if pkg_resources.parse_version(pip.__version__) >= pkg_resources.parse_version('19.3.0'):
import pip._internal.main
pip_main = pip._internal.main.main
elif pkg_resources.parse_version(pip.__version__) >= pkg_resources.parse_version('10.0.0'):
import pip._internal
pip_main = pip._internal.main
else:
pip_main = pip.main
except ImportError:
pip = None
use_pip = False
pkg_resources = None

# Try to find packaging. This is normally(?) installed by setuptools but
# otherwise pip keeps a copy.
# Try to find packaging. As of Oct 2022 this should be provided explicitly when
# the conda env is created. Not safe to import the vendored version from pip;
# see https://github.com/pypa/setuptools/issues/3297
try:
import packaging
from packaging.requirements import Requirement
except ImportError:
try:
import pip._vendor.packaging as packaging
from pip._vendor.packaging.requirements import Requirement
except ImportError:
# If all else fails then we need the symbol to check
Requirement = None
packaging = None
Requirement = None
packaging = None

try:
import setuptools
Expand Down Expand Up @@ -77,7 +66,7 @@ def require(pkgname, version=None):
eg. '<2', or both, eg. '>=4.5,<4.6'.
:return: True when the requirement is met, False otherwise.'''

if not pip:
if not use_pip:
_notice(" WARNING: Can not verify python package requirements - pip/setuptools out of date",
" Please update pip and setuptools by running:", "",
" libtbx.python -m pip install pip setuptools --upgrade", "",
Expand Down Expand Up @@ -157,7 +146,9 @@ def require(pkgname, version=None):

print("attempting {action} of {package}...".format(action=action, package=pkgname))
has_req_tracker = os.environ.get('PIP_REQ_TRACKER')
exit_code = pip_main(['install', requirestring])
pip_install_cmd = ['pip', 'install', requirestring]
pip_install_result = subprocess.run(pip_install_cmd)
exit_code = pip_install_result.returncode
if not has_req_tracker:
# clean up environment after pip call for next invocation
os.environ.pop('PIP_REQ_TRACKER', None)
Expand Down

0 comments on commit dc000ad

Please sign in to comment.