-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Fix ensurepip bundled pip upgrade for Python 3.12+ #38765
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
base: master
Are you sure you want to change the base?
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 |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| # | ||
| # Licensed to the Apache Software Foundation (ASF) under one or more | ||
| # contributor license agreements. See the NOTICE file distributed with | ||
| # this work for additional information regarding copyright ownership. | ||
| # The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| # (the "License"); you may not use this file except in compliance with | ||
| # the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
|
|
||
| """ | ||
| Upgrade the pip wheel bundled in ensurepip for Python 3.12+. | ||
|
|
||
| The script is executed within Docker after the image pip has been upgraded. | ||
| upgrade_ensurepip expects setuptools to be bundled as well, but CPython 3.12+ | ||
| only ships pip in ensurepip/_bundled. | ||
| """ | ||
|
|
||
| import subprocess | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| import ensurepip | ||
|
|
||
|
|
||
| def main(): | ||
| ep_path = Path(ensurepip.__file__) | ||
| wheel_dir = ep_path.parent / '_bundled' | ||
| pip_version = subprocess.check_output( | ||
| [sys.executable, '-m', 'pip', '--version'], | ||
| text=True).split()[1] | ||
| subprocess.check_call([ | ||
| sys.executable, | ||
| '-m', | ||
| 'pip', | ||
| 'download', | ||
| 'pip=={}'.format(pip_version), | ||
| '-d', | ||
| str(wheel_dir), | ||
| '--no-deps', | ||
| ]) | ||
| lines = ep_path.read_text().splitlines() | ||
| pip_line = None | ||
| for idx, line in enumerate(lines): | ||
| if line.startswith('_PIP_VERSION = '): | ||
| pip_line = idx | ||
| break | ||
| if pip_line is None: | ||
| sys.exit('ensurepip _PIP_VERSION not found') | ||
| org = ep_path.with_suffix('.py.org') | ||
| if not org.exists(): | ||
| ep_path.rename(org) | ||
| lines[pip_line] = '_PIP_VERSION = "{}"'.format(pip_version) | ||
| ep_path.write_text('\n'.join(lines) + '\n') | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| main() | ||
|
Comment on lines
+33
to
+65
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can enhance def main():
ep_path = Path(ensurepip.__file__)
wheel_dir = ep_path.parent / '_bundled'
# Get current pip version
pip_version = subprocess.check_output(
[sys.executable, '-m', 'pip', '--version'],
text=True).split()[1]
# Download new pip wheel
subprocess.check_call([
sys.executable,
'-m',
'pip',
'download',
f'pip=={pip_version}',
'-d',
str(wheel_dir),
'--no-deps',
])
org = ep_path.with_suffix('.py.org')
if org.exists():
lines = org.read_text().splitlines()
else:
lines = ep_path.read_text().splitlines()
ep_path.rename(org)
# Update pip version in ensurepip/__init__.py
pip_line = None
for idx, line in enumerate(lines):
if line.startswith('_PIP_VERSION = '):
pip_line = idx
break
if pip_line is None:
sys.exit('ensurepip _PIP_VERSION not found')
lines[pip_line] = f'_PIP_VERSION = "{pip_version}"'
# Check if setuptools is also bundled (Python < 3.12)
setuptools_line = None
setuptools_version = None
for idx, line in enumerate(lines):
if line.startswith('_SETUPTOOLS_VERSION = '):
setuptools_line = idx
break
if setuptools_line is not None:
try:
setuptools_version = subprocess.check_output(
[sys.executable, '-c', 'import setuptools; print(setuptools.__version__)'],
text=True).strip()
subprocess.check_call([
sys.executable,
'-m',
'pip',
'download',
f'setuptools=={setuptools_version}',
'-d',
str(wheel_dir),
'--no-deps',
])
lines[setuptools_line] = f'_SETUPTOOLS_VERSION = "{setuptools_version}"'
except Exception as e:
print(f"Failed to upgrade bundled setuptools: {e}", file=sys.stderr)
# Write modified ensurepip/__init__.py
ep_path.write_text('\n'.join(lines) + '\n')
# Clean up old wheels in _bundled
for path in wheel_dir.glob('pip-*.whl'):
if pip_version not in path.name:
path.unlink()
if setuptools_version is not None:
for path in wheel_dir.glob('setuptools-*.whl'):
if setuptools_version not in path.name:
path.unlink()
if __name__ == '__main__':
main() |
||
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.
Instead of maintaining separate logic for Python 3.10/3.11 (using the external
upgrade_ensurepippackage) and Python 3.12+ (using a custom script), we can unify both cases by extendingupgrade_bundled_pip.pyto handlesetuptoolswhen it is present. This completely eliminates the externalupgrade_ensurepipdependency, avoids complex shell-basedfind/ls -vcleanup commands, and simplifies the Dockerfile significantly.