Skip to content

Commit b5942da

Browse files
authored
Replace pkg_resources to impotlib (#217)
1 parent 0722906 commit b5942da

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/fosslight_util/help.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
# Copyright (c) 2021 LG Electronics Inc.
44
# SPDX-License-Identifier: Apache-2.0
55
import sys
6-
import pkg_resources
6+
try:
7+
from importlib.metadata import version, PackageNotFoundError
8+
except ImportError:
9+
from importlib_metadata import version, PackageNotFoundError # Python <3.8
710

811
_HELP_MESSAGE_COMMON = """
912
_______ _______ _______ _______ ___ ___ __
@@ -50,7 +53,10 @@ def print_help_msg(self, exitopt: bool) -> None:
5053
def print_package_version(pkg_name: str, msg: str = "", exitopt: bool = True) -> str:
5154
if msg == "":
5255
msg = f"{pkg_name} Version:"
53-
cur_version = pkg_resources.get_distribution(pkg_name).version
56+
try:
57+
cur_version = version(pkg_name)
58+
except PackageNotFoundError:
59+
cur_version = "unknown"
5460

5561
if exitopt:
5662
print(f'{msg} {cur_version}')

src/fosslight_util/set_log.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import logging
77
import os
88
from pathlib import Path
9-
import pkg_resources
109
import sys
1110
import platform
1211
from . import constant as constant
@@ -15,6 +14,11 @@
1514
from typing import Tuple
1615
from logging import Logger
1716

17+
try:
18+
from importlib.metadata import version, PackageNotFoundError
19+
except ImportError:
20+
from importlib_metadata import version, PackageNotFoundError # Python <3.8
21+
1822

1923
def init_check_latest_version(pkg_version="", main_package_name=""):
2024

@@ -92,9 +96,11 @@ def init_log(log_file: str, create_file: bool = True, stream_log_level: int = lo
9296
if main_package_name != "":
9397
pkg_info = main_package_name
9498
try:
95-
pkg_version = pkg_resources.get_distribution(main_package_name).version
99+
pkg_version = version(main_package_name)
96100
init_check_latest_version(pkg_version, main_package_name)
97101
pkg_info = main_package_name + " v" + pkg_version
102+
except PackageNotFoundError:
103+
logger.debug('Cannot check the version: Package not found')
98104
except Exception as error:
99105
logger.debug('Cannot check the version:' + str(error))
100106
_result_log["Tool Info"] = pkg_info

0 commit comments

Comments
 (0)