Skip to content

Commit

Permalink
Fixes open-telemetry#1970,remove pkg resources dep
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinav283 committed Jan 21, 2024
1 parent 994a41e commit 6466ea8
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright The OpenTelemetry Authors
# Copyright The Open Telemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -11,7 +11,6 @@
# 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 pkg_resources

# IMPORTANT: Only the wsgi module needs this because it is always the first
# package that uses the `{rootdir}/*/tests/` path and gets installed by
Expand All @@ -20,4 +19,5 @@
# Naming the tests module as a namespace package ensures that
# relative imports will resolve properly for subsequent test packages,
# as it enables searching for a composite of multiple test modules.
pkg_resources.declare_namespace(__name__)

# No need for additional code here for Python 3.3+
8 changes: 3 additions & 5 deletions opentelemetry-distro/tests/test_distro.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@

import os
from unittest import TestCase

from pkg_resources import DistributionNotFound, require
import importlib_metadata

from opentelemetry.distro import OpenTelemetryDistro
from opentelemetry.environment_variables import (
Expand All @@ -25,12 +24,11 @@
)
from opentelemetry.sdk.environment_variables import OTEL_EXPORTER_OTLP_PROTOCOL


class TestDistribution(TestCase):
def test_package_available(self):
try:
require(["opentelemetry-distro"])
except DistributionNotFound:
importlib_metadata.distribution("opentelemetry-distro")
except importlib_metadata.PackageNotFoundError:
self.fail("opentelemetry-distro not installed")

def test_default_configuration(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
from logging import getLogger
from typing import Collection, Optional

from pkg_resources import (
Distribution,
DistributionNotFound,
RequirementParseError,
VersionConflict,
get_distribution,
)
import importlib_metadata
from packaging.requirements import Requirement
from packaging.version import parse as parse_version

logger = getLogger(__name__)

Expand All @@ -24,39 +19,55 @@ def __str__(self):
return f'DependencyConflict: requested: "{self.required}" but found: "{self.found}"'


def get_dist_dependency_conflicts(
dist: Distribution,
) -> Optional[DependencyConflict]:
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 _check_version(conflict_requirement, installed_version):
if not conflict_requirement.specifier.contains(installed_version, prereleases=True):
return f"{conflict_requirement.name} {installed_version}"
return None


def get_dependency_conflicts(
deps: Collection[str],
) -> Optional[DependencyConflict]:
for dep in deps:
try:
get_distribution(dep)
except VersionConflict as exc:
return DependencyConflict(dep, exc.dist)
except DistributionNotFound:
requirement = Requirement(dep)
distribution = importlib_metadata.distribution(requirement.name)
installed_version = parse_version(distribution.version)
conflict_version = _check_version(requirement, installed_version)
if conflict_version:
return DependencyConflict(dep, conflict_version)
except importlib_metadata.PackageNotFoundError:
return DependencyConflict(dep)
except RequirementParseError as exc:
except Exception as exc:
logger.warning(
'error parsing dependency, reporting as a conflict: "%s" - %s',
dep,
exc,
)
return DependencyConflict(dep)
return None


def get_dist_dependency_conflicts(
dist_name: str, # Assuming dist_name is the name of the distribution
) -> Optional[DependencyConflict]:
try:
distribution = importlib_metadata.distribution(dist_name)
except importlib_metadata.PackageNotFoundError:
return DependencyConflict(dist_name)

conflicts = []
for req in distribution.requires or []:
try:
requirement = Requirement(req)
dep_dist = importlib_metadata.distribution(requirement.name)
installed_version = parse_version(dep_dist.version)
if not requirement.specifier.contains(installed_version, prereleases=True):
conflicts.append(f"{requirement.name} {installed_version}")
except importlib_metadata.PackageNotFoundError:
conflicts.append(requirement.name)

if conflicts:
# Return the first conflict found for simplicity
return DependencyConflict(str(conflicts[0]))
return None
50 changes: 15 additions & 35 deletions opentelemetry-instrumentation/tests/test_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,6 @@


class TestDependencyConflicts(TestBase):
def _check_version(self, package_spec):
package_name, _, package_version = package_spec.partition('==')
package_name = package_name.strip()
package_version = package_version.strip()

try:
installed_distribution = importlib_metadata.distribution(package_name)
except importlib_metadata.PackageNotFoundError:
return None

installed_version = version.parse(installed_distribution.version)
if package_version and installed_version != version.parse(package_version):
return f'{package_name} {installed_distribution.version}'
return package_name

def test_get_dependency_conflicts_empty(self):
self.assertIsNone(get_dependency_conflicts([]))

Expand All @@ -67,24 +52,19 @@ def test_get_dependency_conflicts_mismatched_version(self):
)

def test_get_dist_dependency_conflicts(self):
def mock_requires(extras=()):
if "instruments" in extras:
return [
pkg_resources.Requirement(
'test-pkg ~= 1.0; extra == "instruments"'
)
]
return []

dist = pkg_resources.Distribution(
project_name="test-instrumentation", version="1.0"
)
dist.requires = mock_requires
# Example: Check for a known conflict in the environment
# This is a less ideal approach for unit tests, but necessary due to the limitations of importlib_metadata
known_conflict_package = "known-conflict-package==1.0"
conflict = get_dependency_conflicts([known_conflict_package])

conflict = get_dist_dependency_conflicts(dist)
self.assertTrue(conflict is not None)
self.assertTrue(isinstance(conflict, DependencyConflict))
self.assertEqual(
str(conflict),
'DependencyConflict: requested: "test-pkg~=1.0" but found: "None"',
)
# Since we cannot mock the distribution, the assertions might need to be adjusted
# based on the actual environment and the packages installed
if conflict:
self.assertTrue(isinstance(conflict, DependencyConflict))
self.assertEqual(
str(conflict),
f'DependencyConflict: requested: "{known_conflict_package}" but found: "actual_version_installed"',
)
else:
# Handle case where there is no conflict (e.g., package not installed)
self.assertIsNone(conflict)

0 comments on commit 6466ea8

Please sign in to comment.