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
Respect Requires-Python
in PEXEnvironment
.
#923
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# coding=utf-8 | ||
# Copyright 2020 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
from __future__ import absolute_import | ||
|
||
import email | ||
|
||
from pex.third_party.packaging.specifiers import SpecifierSet | ||
from pex.third_party.pkg_resources import DistInfoDistribution | ||
|
||
|
||
def requires_python(dist): | ||
"""Examines dist for `Python-Requires` metadata and returns version constraints if any. | ||
|
||
See: https://www.python.org/dev/peps/pep-0345/#requires-python | ||
|
||
:param dist: A distribution to check for `Python-Requires` metadata. | ||
:type dist: :class:`pkg_resources.Distribution` | ||
:return: The required python version specifiers. | ||
:rtype: :class:`packaging.specifiers.SpecifierSet` or None | ||
""" | ||
if not dist.has_metadata(DistInfoDistribution.PKG_INFO): | ||
return None | ||
|
||
metadata = dist.get_metadata(DistInfoDistribution.PKG_INFO) | ||
pkg_info = email.parser.Parser().parsestr(metadata) | ||
python_requirement = pkg_info.get('Requires-Python') | ||
if not python_requirement: | ||
return None | ||
return SpecifierSet(python_requirement) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Requirement.parse() chokes on requirements that aren't of the form foo==x.y.z, e.g., URLs. So this cannot support URLs. And I know that is not a consequence of this change, it was true before, since the method that calls this one also calls Requirement.parse(), and WorkingSet.resolve() takes parsed Requirement objects.
So this comment is actually a question for my own education - do URL requirements work today? If so, how? Is this a build time/run time distinction? I have a feeling I have used them successfully since the switch to using pip.
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, this is a build time / run time distinction.
These are requirements stored in a built PEX's metadata, and all of those requirements are of the form
[key]==[version](; [markers])
. They are this way since they're buit from the results of a resolve. That resolve does accept the myriad forms of requirement strings, but it plops out concrete dists. Those concrete dists are used to create the requirements in the PEX metadata used here to boot up at PEX runtime:https://github.com/pantsbuild/pex/blob/a07954a1bce1cf0c023c67f36e4e37f078eb23ba/pex/resolver.py#L555-L579
Which uses:
https://github.com/pantsbuild/pex/blob/a07954a1bce1cf0c023c67f36e4e37f078eb23ba/pex/resolver.py#L96-L97
Which ues:
https://github.com/pantsbuild/pex/blob/a07954a1bce1cf0c023c67f36e4e37f078eb23ba/pex/vendor/_vendored/setuptools/pkg_resources/__init__.py#L2870-L2877