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

Remove the use of the deprecated pkg_resources package #13842

Merged
merged 4 commits into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions LICENSE-3rdparty.csv
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ futures,PyPI,PSF,Copyright (c) 2015 Brian Quinlan
gearman,PyPI,Apache-2.0,Copyright 2010 Yelp <[email protected]>
gssapi,PyPI,ISC,"Copyright (c) 2014, The Python GSSAPI Team"
immutables,PyPI,Apache-2.0,Copyright 2018-present Contributors to the immutables project.
importlib-metadata,PyPI,Apache-2.0,Copyright Jason R. Coombs
in-toto,PyPI,Apache-2.0,Copyright 2018 New York University
ipaddress,PyPI,PSF,Copyright (c) 2013 Philipp Hagemeister
jellyfish,PyPI,BSD-3-Clause,"Copyright (c) 2015, James Turk"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ gearman==2.0.2; sys_platform != 'win32' and python_version < '3.0'
gssapi==1.6.1; python_version < '3.0'
gssapi==1.8.2; python_version > '3.0'
immutables==0.19; python_version > '3.0'
importlib-metadata==2.1.3; python_version < '3.8'
in-toto==1.0.1; python_version > '3.0'
ipaddress==1.0.23; python_version < '3.0'
jaydebeapi==1.2.3
Expand Down
16 changes: 10 additions & 6 deletions datadog_checks_base/datadog_checks/base/utils/agent/packages.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
# (C) Datadog, Inc. 2019-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
import sys

import pkg_resources
if sys.version_info >= (3, 8):
from importlib.metadata import distributions
else:
from importlib_metadata import distributions

DATADOG_CHECK_PREFIX = "datadog-"
DATADOG_CHECK_PREFIX = 'datadog-'


def get_datadog_wheels():
packages = []
dist = list(pkg_resources.working_set)
for package in dist:
if package.project_name.startswith(DATADOG_CHECK_PREFIX):
name = package.project_name[len(DATADOG_CHECK_PREFIX) :].replace('-', '_')
for package in distributions():
project_name = package.metadata['Name']
if project_name.startswith(DATADOG_CHECK_PREFIX):
name = project_name[len(DATADOG_CHECK_PREFIX) :].replace('-', '_')
packages.append(name)

return sorted(packages)[::-1]
1 change: 1 addition & 0 deletions datadog_checks_base/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ deps = [
"ddtrace==0.32.2; sys_platform == 'win32' and python_version < '3.0'",
"ddtrace==0.53.2; sys_platform != 'win32' or python_version > '3.0'",
"enum34==1.1.10; python_version < '3.0'",
"importlib-metadata==2.1.3; python_version < '3.8'",
"immutables==0.19; python_version > '3.0'",
"ipaddress==1.0.23; python_version < '3.0'",
"jellyfish==0.9.0; python_version > '3.0'",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ def get_wheel_relpath(self, standard_distribution_name, version=None, ignore_pyt
raise MissingVersions(standard_distribution_name)

if not version:
# https://setuptools.readthedocs.io/en/latest/pkg_resources.html#parsing-utilities
# https://packaging.pypa.io/en/latest/version.html
version = str(max(parse_version(v) for v in wheels.keys()))

python_tags = wheels[version]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"""
import os.path

from pkg_resources import safe_name
from packaging.utils import canonicalize_name

from .exceptions import NonDatadogPackage

Expand All @@ -28,7 +28,7 @@ def substitute(target_relpath):
if not wheel_distribution_name.startswith('datadog_'):
raise NonDatadogPackage(wheel_distribution_name)

standard_distribution_name = safe_name(wheel_distribution_name)
standard_distribution_name = canonicalize_name(wheel_distribution_name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ofek have you checked that this function does the same thing? I assume you have, but just double-checking.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For our use case, yes


# These names are the exceptions. In this case, the wheel distribution name
# matches exactly the directory name of the check on GitHub.
Expand Down
5 changes: 0 additions & 5 deletions mcache/datadog_checks/mcache/mcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from __future__ import division

import bmemcached
import pkg_resources
from six import iteritems, itervalues

from datadog_checks.base import AgentCheck, ConfigurationError
Expand Down Expand Up @@ -105,10 +104,6 @@ class Memcache(AgentCheck):

SERVICE_CHECK = 'memcache.can_connect'

@classmethod
def get_library_versions(cls):
return {"memcache": pkg_resources.get_distribution("python-binary-memcached").version}
Comment on lines -109 to -110
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is entirely removed, is there no alternative to using pkg_resources to implement this function?

Copy link
Contributor

@fridex fridex Jan 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each entry in importlib.metadata.distributions() iterator should have version attribute (docs). See also version function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was not being used by anything so I simply removed it


def _process_response(self, response):
"""
Examine the response and raise an error is something is off
Expand Down