-
Notifications
You must be signed in to change notification settings - Fork 2
Improve the package compatibility check #340
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0d21042
Use new `is_package_compatible` function
bitterpanda63 12c4a34
Update is_package_compatible function
bitterpanda63 ac1d398
Add packagesStore and improve psycopg2 writing
bitterpanda63 31e0855
Update aikido_zen/background_process/packages.py
bitterpanda63 67030b4
Update aikido_zen/background_process/packages.py
bitterpanda63 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
@@ -1,37 +1,77 @@ | ||
"""Helper functions for packages""" | ||
|
||
import importlib.metadata as metadata | ||
import importlib.metadata as importlib_metadata | ||
|
||
from packaging.version import Version | ||
from aikido_zen.helpers.logging import logger | ||
import aikido_zen.background_process.comms as comms | ||
|
||
MAX_REPORT_TRIES = 5 | ||
|
||
# If any version is supported, this constant can be used | ||
ANY_VERSION = "0.0.0" | ||
|
||
|
||
def pkg_compat_check(pkg_name, required_version): | ||
"""Reports a newly wrapped package to the bg process""" | ||
def is_package_compatible(package=None, required_version=ANY_VERSION, packages=None): | ||
bitterpanda63 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Checks for compatibility of one or multiple package names (in the case of psycopg, you need to check multiple names)""" | ||
# Fetch package version : | ||
if package is not None: | ||
packages = [package] | ||
if packages is None: | ||
return False # no package names provided, return false. | ||
try: | ||
pkg_version = metadata.version(pkg_name) | ||
except metadata.PackageNotFoundError: | ||
logger.info( | ||
"Version for %s is undetermined. Zen is unable to protect this module.", | ||
pkg_name, | ||
) | ||
return False # We don't support it since we are not sure what it is. | ||
|
||
# Check if the package version is supported : | ||
version_supported = is_version_supported(pkg_version, required_version) | ||
if version_supported: | ||
logger.debug("Instrumentation for %s=%s supported", pkg_name, pkg_version) | ||
else: | ||
logger.info("Zen does not support %s=%s", pkg_name, pkg_version) | ||
return version_supported | ||
|
||
|
||
def is_version_supported(pkg_verion, required_version): | ||
for package in packages: | ||
# Checks if we already looked up the package : | ||
if PackagesStore.get_package(package) is not None: | ||
bitterpanda63 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return PackagesStore.get_package(package)["supported"] | ||
|
||
# Safely get the package version, with an exception for when the package was not found | ||
try: | ||
package_version = importlib_metadata.version(package) | ||
except importlib_metadata.PackageNotFoundError: | ||
continue | ||
|
||
# Check support and store package for later | ||
supported = is_version_supported(package_version, required_version) | ||
PackagesStore.add_package(package, package_version, supported) | ||
|
||
if supported: | ||
logger.debug( | ||
"Instrumentation for %s=%s supported", package, package_version | ||
) | ||
return True | ||
|
||
# No match found | ||
logger.info("Zen does not support %s", packages) | ||
return False | ||
except Exception as e: | ||
logger.debug("Exception occurred in is_package_compatible: %s", e) | ||
return False # We don't support it, since something unexpected happened in checking compatibility. | ||
|
||
|
||
def is_version_supported(version, required_version): | ||
"""Checks if the package version is supported""" | ||
return Version(pkg_verion) >= Version(required_version) | ||
return Version(version) >= Version(required_version) | ||
|
||
|
||
# packages store, uses python's built in GlobalInterpreterLock (GIL) | ||
packages = dict() | ||
|
||
|
||
class PackagesStore: | ||
@staticmethod | ||
def get_packages(): | ||
global packages | ||
return packages | ||
|
||
@staticmethod | ||
def add_package(package, version, supported): | ||
global packages | ||
packages[package] = { | ||
"version": version, | ||
"supported": bool(supported), | ||
} | ||
|
||
@staticmethod | ||
def get_package(package_name): | ||
global packages | ||
if package_name in packages: | ||
return packages[package_name] | ||
return None |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -1,11 +1,11 @@ | ||
"""Gunicorn Module, report if module was found""" | ||
|
||
import aikido_zen.importhook as importhook | ||
from aikido_zen.background_process.packages import pkg_compat_check, ANY_VERSION | ||
from aikido_zen.background_process.packages import is_package_compatible, ANY_VERSION | ||
|
||
|
||
@importhook.on_import("gunicorn") | ||
def on_gunicorn_import(gunicorn): | ||
"""Report to the core when gunicorn gets imported""" | ||
pkg_compat_check("gunicorn", required_version=ANY_VERSION) | ||
is_package_compatible("gunicorn", required_version=ANY_VERSION) | ||
return gunicorn |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -1,11 +1,11 @@ | ||
"""UWSGI Module, report if module was found""" | ||
|
||
import aikido_zen.importhook as importhook | ||
from aikido_zen.background_process.packages import pkg_compat_check, ANY_VERSION | ||
from aikido_zen.background_process.packages import is_package_compatible, ANY_VERSION | ||
|
||
|
||
@importhook.on_import("uwsgi") | ||
def on_uwsgi_import(uwsgi): | ||
"""Report to the core when uwsgi gets imported""" | ||
pkg_compat_check("uwsgi", required_version=ANY_VERSION) | ||
is_package_compatible("uwsgi", required_version=ANY_VERSION) | ||
return uwsgi |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.