Skip to content
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 package_version_utils.py logic #689

Merged
merged 11 commits into from
Nov 16, 2022
50 changes: 45 additions & 5 deletions newrelic/common/package_version_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,52 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# To use:
lrafeei marked this conversation as resolved.
Show resolved Hide resolved
# Input: name of library as a string.
# example:
# get_package_version("botocore")
# Output: version of library as a string

import sys

# Need to account for 4 possible variations of version declaration specified in (rejected) PEP 396
VERSION_ATTRS = ("__version__", "version", "__version_tuple__", "version_tuple") # nosec
NULL_VERSIONS = frozenset((None, "", "0", "0.0", "0.0.0", "0.0.0.0", (0,), (0, 0), (0, 0, 0), (0, 0, 0, 0))) # nosec


def get_package_version(name):
# importlib was introduced into the standard library starting in Python3.8.
if "importlib" in sys.modules and hasattr(sys.modules["importlib"], "metadata"):
return sys.modules["importlib"].metadata.version(name) # pylint: disable=E1101
elif "pkg_resources" in sys.modules:
return sys.modules["pkg_resources"].get_distribution(name).version
def _get_package_version(name):
module = sys.modules.get(name, None)
version = None
for attr in VERSION_ATTRS:
try:
version = getattr(module, attr, None)
if version not in NULL_VERSIONS:
return version
except Exception:
pass

# importlib was introduced into the standard library starting in Python3.8.
if "importlib" in sys.modules and hasattr(sys.modules["importlib"], "metadata"):
try:
version = sys.modules["importlib"].metadata.version(name) # pylint: disable=E1101
if version not in NULL_VERSIONS:
return version
except Exception:
pass

if "pkg_resources" in sys.modules:
try:
version = sys.modules["pkg_resources"].get_distribution(name).version
if version not in NULL_VERSIONS:
return version
except Exception:
pass

version = _get_package_version(name)

# Coerce iterables into a string
if isinstance(version, (tuple, list)):
lrafeei marked this conversation as resolved.
Show resolved Hide resolved
version = ".".join(str(v) for v in version)

return version
14 changes: 14 additions & 0 deletions tests/agent_unittests/_test_package_version_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def test___version__():
lrafeei marked this conversation as resolved.
Show resolved Hide resolved
pass


def test__version():
pass


def test___version():
pass


def module():
pass
72 changes: 72 additions & 0 deletions tests/agent_unittests/test_package_version_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Copyright 2010 New Relic, Inc.
#
# Licensed 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.

import sys

# Empty module for running tests against
import _test_package_version_utils as module
import pytest
from testing_support.validators.validate_function_called import validate_function_called

from newrelic.common.package_version_utils import (
NULL_VERSIONS,
VERSION_ATTRS,
get_package_version,
)

IS_PY38_PLUS = sys.version_info[:2] >= (3, 8)
SKIP_IF_NOT_IMPORTLIB_METADATA = pytest.mark.skipif(not IS_PY38_PLUS, reason="importlib.metadata is not supported.")
SKIP_IF_IMPORTLIB_METADATA = pytest.mark.skipif(
IS_PY38_PLUS, reason="importlib.metadata is preferred over pkg_resources."
)


@pytest.fixture(scope="function", autouse=True)
def patched_pytest_module(monkeypatch):
for attr in VERSION_ATTRS:
if hasattr(pytest, attr):
monkeypatch.delattr(pytest, attr)

yield pytest


@pytest.mark.parametrize(
"attr,value,expected_value",
(
("version", "1.2.3.4", "1.2.3.4"),
("__version__", "1.3.5rc2", "1.3.5rc2"),
("__version_tuple__", (3, 5, 8), "3.5.8"),
("version_tuple", (3, 1, "0b2"), "3.1.0b2"),
lrafeei marked this conversation as resolved.
Show resolved Hide resolved
),
)
def test_get_package_version(attr, value, expected_value):
lrafeei marked this conversation as resolved.
Show resolved Hide resolved
setattr(module, attr, value)
version = get_package_version(module.__name__)
delattr(module, attr)

assert version == expected_value


@SKIP_IF_NOT_IMPORTLIB_METADATA
@validate_function_called("importlib.metadata", "version")
def test_importlib_metadata():
version = get_package_version("pytest")
assert version not in NULL_VERSIONS, version


@SKIP_IF_IMPORTLIB_METADATA
@validate_function_called("pkg_resources", "get_distribution")
def test_pkg_resources_metadata():
version = get_package_version("pytest")
assert version not in NULL_VERSIONS, version