Skip to content

Commit c72f40e

Browse files
authored
[FLINK-39052][python] Remove usage of pkg_resources (#27556)
1 parent db1cf97 commit c72f40e

1 file changed

Lines changed: 10 additions & 9 deletions

File tree

flink-python/pyflink/gen_protos.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030
import time
3131
import warnings
3232

33-
import pkg_resources
33+
from importlib import metadata as importlib_metadata
34+
from importlib import resources as importlib_resources
35+
from packaging.version import parse as parse_version
3436

3537
GRPC_TOOLS = 'grpcio-tools>=1.29.0,<=1.71.0'
3638
PROTO_PATHS = ['proto']
@@ -87,7 +89,9 @@ def generate_proto_files(force=True, output_dir=DEFAULT_PYTHON_OUTPUT_PATH):
8789
else:
8890
_check_grpcio_tools_version()
8991
logging.info('Regenerating out-of-date Python proto definitions.')
90-
builtin_protos = pkg_resources.resource_filename('grpc_tools', '_proto')
92+
# Get the grpc_tools _proto directory path
93+
grpc_tools_files = importlib_resources.files('grpc_tools')
94+
builtin_protos = str(grpc_tools_files.joinpath('_proto'))
9195
args = (
9296
[sys.executable] + # expecting to be called from command line
9397
['--proto_path=%s' % builtin_protos] +
@@ -122,8 +126,7 @@ def _install_grpcio_tools_and_generate_proto_files(force, output_dir):
122126
start = time.time()
123127
# since '--prefix' option is only supported for pip 8.0+, so here we fallback to
124128
# use '--install-option' when the pip version is lower than 8.0.0.
125-
pip_version = pkg_resources.get_distribution("pip").version
126-
from pkg_resources import parse_version
129+
pip_version = importlib_metadata.version("pip")
127130
if parse_version(pip_version) >= parse_version('8.0.0'):
128131
subprocess.check_call(
129132
[sys.executable, '-m', 'pip', 'install',
@@ -144,10 +147,8 @@ def _install_grpcio_tools_and_generate_proto_files(force, output_dir):
144147
sys.stderr.flush()
145148
shutil.rmtree(build_path, ignore_errors=True)
146149
sys.path.append(install_obj.install_purelib)
147-
pkg_resources.working_set.add_entry(install_obj.install_purelib)
148150
if install_obj.install_purelib != install_obj.install_platlib:
149151
sys.path.append(install_obj.install_platlib)
150-
pkg_resources.working_set.add_entry(install_obj.install_platlib)
151152
try:
152153
generate_proto_files(force, output_dir)
153154
finally:
@@ -185,11 +186,11 @@ def _add_license_header(dir, file_name):
185186

186187

187188
def _check_grpcio_tools_version():
188-
version = pkg_resources.get_distribution("grpcio-tools").parsed_version
189-
from pkg_resources import parse_version
189+
version_str = importlib_metadata.version("grpcio-tools")
190+
version = parse_version(version_str)
190191
if version < parse_version('1.29.0') or version > parse_version('1.71.0'):
191192
raise RuntimeError(
192-
"Version of grpcio-tools must be between 1.29.0 and 1.71.0, got %s" % version)
193+
"Version of grpcio-tools must be between 1.29.0 and 1.71.0, got %s" % version_str)
193194

194195

195196
if __name__ == '__main__':

0 commit comments

Comments
 (0)