Skip to content

Commit ed6aa87

Browse files
Change from pkg_resources to importlib metadata (#329)
* Change from pkg_resources to importlib metadata
1 parent f8c5063 commit ed6aa87

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- Fixes:
1313
- Fix unexpected 'No active span' IllegalStateError (#311)
1414
- **Tentative**: Set upper bound <=5.9.5 for psutil package due to test failure. (#326)
15+
- Remove `DeprecationWarning` from `pkg_resources` by replace it with `importlib_metadata` (#329)
1516

1617
### 1.0.1
1718

skywalking/plugins/__init__.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,22 @@
2020
import re
2121
import traceback
2222

23-
import pkg_resources
23+
import sys
24+
25+
if sys.version_info < (3, 8):
26+
import pkg_resources
27+
PackageNotFoundException = pkg_resources.DistributionNotFound
28+
29+
def get_pkg_version(pkg_name):
30+
return pkg_resources.get_distribution(pkg_name).version
31+
32+
else:
33+
import importlib.metadata
34+
PackageNotFoundException = importlib.metadata.PackageNotFoundError
35+
36+
def get_pkg_version(pkg_name):
37+
return importlib.metadata.version(pkg_name)
38+
2439
from packaging import version
2540

2641
import skywalking
@@ -78,8 +93,8 @@ def pkg_version_check(plugin):
7893
rules = plugin.version_rule.get('rules')
7994

8095
try:
81-
current_pkg_version = pkg_resources.get_distribution(pkg_name).version
82-
except pkg_resources.DistributionNotFound:
96+
current_pkg_version = get_pkg_version(pkg_name)
97+
except PackageNotFoundException:
8398
# when failed to get the version, we consider it as supported.
8499
return supported
85100

tools/grpc_code_gen.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,18 @@
1818
import os
1919
import re
2020

21-
import pkg_resources
2221
from grpc_tools import protoc
2322
from packaging import version
23+
import sys
24+
25+
if sys.version_info < (3, 8):
26+
import pkg_resources
27+
grpc_tools_version = pkg_resources.get_distribution('grpcio-tools').version
28+
29+
else:
30+
import importlib.metadata
31+
grpc_tools_version = importlib.metadata.version('grpcio-tools')
2432

25-
grpc_tools_version = pkg_resources.get_distribution('grpcio-tools').version
2633
dest_dir = 'skywalking/protocol'
2734
src_dir = 'protocol'
2835

0 commit comments

Comments
 (0)