-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
bpo-42856: Add --with-wheel-pkg-dir=PATH configure option #24210
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -1,27 +1,82 @@ | ||||||||
| import collections | ||||||||
| import os | ||||||||
| import os.path | ||||||||
| import subprocess | ||||||||
| import sys | ||||||||
| import runpy | ||||||||
| import sysconfig | ||||||||
| import tempfile | ||||||||
| import subprocess | ||||||||
| from importlib import resources | ||||||||
|
|
||||||||
| from . import _bundled | ||||||||
|
|
||||||||
|
|
||||||||
|
|
||||||||
| __all__ = ["version", "bootstrap"] | ||||||||
|
|
||||||||
|
|
||||||||
| _PACKAGE_NAMES = ('setuptools', 'pip') | ||||||||
| _SETUPTOOLS_VERSION = "47.1.0" | ||||||||
|
|
||||||||
| _PIP_VERSION = "20.2.3" | ||||||||
|
|
||||||||
| _PROJECTS = [ | ||||||||
| ("setuptools", _SETUPTOOLS_VERSION, "py3"), | ||||||||
| ("pip", _PIP_VERSION, "py2.py3"), | ||||||||
| ] | ||||||||
|
|
||||||||
| # Packages bundled in ensurepip._bundled have wheel_name set. | ||||||||
| # Packages from WHEEL_PKG_DIR have wheel_path set. | ||||||||
| _Package = collections.namedtuple('Package', | ||||||||
| ('version', 'wheel_name', 'wheel_path')) | ||||||||
|
|
||||||||
| # Directory of system wheel packages. Some Linux distribution packaging | ||||||||
| # policies recommend against bundling dependencies. For example, Fedora | ||||||||
| # installs wheel packages in the /usr/share/python-wheels/ directory and don't | ||||||||
| # install the ensurepip._bundled package. | ||||||||
| _WHEEL_PKG_DIR = sysconfig.get_config_var('WHEEL_PKG_DIR') | ||||||||
|
|
||||||||
|
|
||||||||
| def _find_packages(path): | ||||||||
| packages = {} | ||||||||
| try: | ||||||||
| filenames = os.listdir(path) | ||||||||
| except OSError: | ||||||||
| # Ignore: path doesn't exist or permission error | ||||||||
| filenames = () | ||||||||
| # Make the code deterministic if a directory contains multiple wheel files | ||||||||
| # of the same package, but don't attempt to implement correct version | ||||||||
| # comparison since this case should not happen. | ||||||||
| filenames = sorted(filenames) | ||||||||
| for filename in filenames: | ||||||||
| # filename is like 'pip-20.2.3-py2.py3-none-any.whl' | ||||||||
| if not filename.endswith(".whl"): | ||||||||
| continue | ||||||||
| for name in _PACKAGE_NAMES: | ||||||||
| prefix = name + '-' | ||||||||
| if filename.startswith(prefix): | ||||||||
| break | ||||||||
| else: | ||||||||
| continue | ||||||||
|
|
||||||||
| # Extract '20.2.2' from 'pip-20.2.2-py2.py3-none-any.whl' | ||||||||
| version = filename.removeprefix(prefix).partition('-')[0] | ||||||||
| wheel_path = os.path.join(path, filename) | ||||||||
| packages[name] = _Package(version, None, wheel_path) | ||||||||
| return packages | ||||||||
|
|
||||||||
|
|
||||||||
| def _get_packages(): | ||||||||
| global _PACKAGES, _WHEEL_PKG_DIR | ||||||||
| if _PACKAGES is not None: | ||||||||
| return _PACKAGES | ||||||||
|
|
||||||||
| packages = {} | ||||||||
| for name, version, py_tag in _PROJECTS: | ||||||||
| wheel_name = f"{name}-{version}-{py_tag}-none-any.whl" | ||||||||
| packages[name] = _Package(version, wheel_name, None) | ||||||||
| if _WHEEL_PKG_DIR: | ||||||||
| dir_packages = _find_packages(_WHEEL_PKG_DIR) | ||||||||
| # only used the wheel package directory if all packages are found there | ||||||||
| if all(name in dir_packages for name in _PACKAGE_NAMES): | ||||||||
| packages = dir_packages | ||||||||
| _PACKAGES = packages | ||||||||
| return packages | ||||||||
| _PACKAGES = None | ||||||||
|
|
||||||||
|
|
||||||||
| def _run_pip(args, additional_paths=None): | ||||||||
| # Run the bootstraping in a subprocess to avoid leaking any state that happens | ||||||||
|
|
@@ -42,7 +97,8 @@ def version(): | |||||||
| """ | ||||||||
| Returns a string specifying the bundled version of pip. | ||||||||
| """ | ||||||||
| return _PIP_VERSION | ||||||||
| return _get_packages()['pip'].version | ||||||||
|
|
||||||||
|
|
||||||||
| def _disable_pip_configuration_settings(): | ||||||||
| # We deliberately ignore all pip environment variables | ||||||||
|
|
@@ -104,16 +160,23 @@ def _bootstrap(*, root=None, upgrade=False, user=False, | |||||||
| # Put our bundled wheels into a temporary directory and construct the | ||||||||
| # additional paths that need added to sys.path | ||||||||
| additional_paths = [] | ||||||||
| for project, version, py_tag in _PROJECTS: | ||||||||
| wheel_name = "{}-{}-{}-none-any.whl".format(project, version, py_tag) | ||||||||
| whl = resources.read_binary( | ||||||||
| _bundled, | ||||||||
| wheel_name, | ||||||||
| ) | ||||||||
| with open(os.path.join(tmpdir, wheel_name), "wb") as fp: | ||||||||
| for name, package in _get_packages().items(): | ||||||||
| if package.wheel_name: | ||||||||
|
||||||||
| if package.wheel_name: | |
| # bundled wheels have names: | |
| if package.wheel_name: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added comments inside, is it better?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,19 +1,68 @@ | ||||||
| import unittest | ||||||
| import unittest.mock | ||||||
| import test.support | ||||||
| import contextlib | ||||||
| import os | ||||||
| import os.path | ||||||
| import contextlib | ||||||
| import sys | ||||||
| import tempfile | ||||||
| import test.support | ||||||
| import unittest | ||||||
| import unittest.mock | ||||||
|
|
||||||
| import ensurepip | ||||||
| import ensurepip._uninstall | ||||||
|
|
||||||
|
|
||||||
| class TestEnsurePipVersion(unittest.TestCase): | ||||||
| class TestPackages(unittest.TestCase): | ||||||
| def touch(self, directory, filename): | ||||||
| fullname = os.path.join(directory, filename) | ||||||
| open(fullname, "wb").close() | ||||||
|
|
||||||
| def test_version(self): | ||||||
| # Test version() | ||||||
| with tempfile.TemporaryDirectory() as tmpdir: | ||||||
| self.touch(tmpdir, "pip-1.2.3b1-py2.py3-none-any.whl") | ||||||
|
||||||
| self.touch(tmpdir, "pip-1.2.3b1-py2.py3-none-any.whl") | |
| pathlib.Path(tmpdir, "pip-1.2.3b1-py2.py3-none-any.whl").touch() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer to avoid testing pathlib in test_ensurepip :-) I like low-level code :-D
vstinner marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| Add ``--with-wheel-pkg-dir=PATH`` option to the ``./configure`` script. If | ||
| specified, the :mod:`ensurepip` module looks for ``setuptools`` and ``pip`` | ||
| wheel packages in this directory: if both are present, these wheel packages are | ||
| used instead of ensurepip bundled wheel packages. | ||
|
|
||
| Some Linux distribution packaging policies recommend against bundling | ||
| dependencies. For example, Fedora installs wheel packages in the | ||
| ``/usr/share/python-wheels/`` directory and don't install the | ||
| ``ensurepip._bundled`` package. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not move this to other constants?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not a constant but a cache for _get_packages(). I prefer to keep it close to the function definition.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your call. The missing empty line looks weird thou.