-
Notifications
You must be signed in to change notification settings - Fork 621
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
Fix auto-instrumentation dependency conflict detection #530
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,12 +1,16 @@ | ||
from logging import getLogger | ||
from typing import Collection, Optional | ||
|
||
from pkg_resources import ( | ||
Distribution, | ||
DistributionNotFound, | ||
RequirementParseError, | ||
VersionConflict, | ||
get_distribution, | ||
) | ||
|
||
logger = getLogger(__file__) | ||
|
||
|
||
class DependencyConflict: | ||
required: str = None | ||
|
@@ -25,22 +29,36 @@ def __str__(self): | |
def get_dist_dependency_conflicts( | ||
dist: Distribution, | ||
) -> Optional[DependencyConflict]: | ||
deps = [ | ||
dep | ||
for dep in dist.requires(("instruments",)) | ||
if dep not in dist.requires() | ||
] | ||
return get_dependency_conflicts(deps) | ||
main_deps = dist.requires() | ||
instrumentation_deps = [] | ||
for dep in dist.requires(("instruments",)): | ||
if dep not in main_deps: | ||
# we set marker to none so string representation of the dependency looks like | ||
# requests ~= 1.0 | ||
# instead of | ||
# requests ~= 1.0; extra = "instruments" | ||
# which does not work with `get_distribution()` | ||
dep.marker = None | ||
instrumentation_deps.append(str(dep)) | ||
|
||
return get_dependency_conflicts(instrumentation_deps) | ||
|
||
|
||
def get_dependency_conflicts( | ||
deps: Collection[str], | ||
) -> Optional[DependencyConflict]: | ||
for dep in deps: | ||
try: | ||
get_distribution(str(dep)) | ||
get_distribution(dep) | ||
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. Is it worth adding a catch all here to prevent any other exceptions from causing the script to exit unexpectedly? 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 could but I don't think there is anything too special about this and I expect it to work always unless in truly exceptional cases like the filesystem misbehaving or something like that. 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. Added an additional RequirementParseError that should catch most issues with invalid requirement strings being passed to this func. 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. What was the original issue behind this? 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. It cannot parse requirement strings that might also contain markers (extra requires section name).
|
||
except VersionConflict as exc: | ||
return DependencyConflict(dep, exc.dist) | ||
except DistributionNotFound: | ||
return DependencyConflict(dep) | ||
except RequirementParseError as exc: | ||
logger.warning( | ||
'error parsing dependency, reporting as a conflict: "%s" - %s', | ||
dep, | ||
exc, | ||
) | ||
return DependencyConflict(dep) | ||
return None |
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.
Just to clarify, this would generate the
Requirements
for whatever is inextra_requires["instruments"]
IN ADDITION TO the regularinstall_requires
correct? In this case, the logic underneath will only parse through the ones that are not in the regualrinstall_requires
?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.
Correct. pkg_resource does not have an API to fetch only a specific extra require group. We could have used something like
dist.requires(only_extra=('instruments',))
but it doesn't exist :)