-
Notifications
You must be signed in to change notification settings - Fork 416
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test importlibs instead of pkg_resources
- Loading branch information
Showing
5 changed files
with
62 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import typing | ||
|
||
|
||
Distribution = typing.NamedTuple("Distribution", [("name", str), ("version", str)]) | ||
|
||
_DISTRIBUTIONS = None | ||
|
||
|
||
def get_distributions(): | ||
# type: () -> typing.List[Distribution] | ||
global _DISTRIBUTIONS | ||
if _DISTRIBUTIONS is not None: | ||
return _DISTRIBUTIONS | ||
|
||
try: | ||
import importlib.metadata as importlib_metadata | ||
except ImportError: | ||
import importlib_metadata | ||
|
||
pkgs = [] | ||
pkg_names = set() | ||
for dist in importlib_metadata.distributions(): | ||
metadata = dist.metadata | ||
name = metadata["version"] | ||
version = metadata["version"] | ||
if name and version and name not in pkg_names: | ||
pkgs.append(Distribution(name, version)) | ||
# Avoids adding duplicates | ||
pkg_names.add(name) | ||
|
||
_DISTRIBUTIONS = pkgs | ||
return _DISTRIBUTIONS |
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from ddtrace.internal.packages import Distribution | ||
from ddtrace.internal.packages import get_distributions | ||
|
||
|
||
def test_get_distributions(): | ||
import pkg_resources | ||
|
||
pkg_resources_ws = [Distribution(pkg.project_name, pkg.version) for pkg in pkg_resources.working_set] | ||
importlib_pkg = get_distributions() | ||
|
||
assert pkg_resources_ws.sort(key=lambda x: x[0]) == importlib_pkg.sort(key=lambda x: x[0]) |
This file contains 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