Skip to content

Commit

Permalink
cli, rpmbuild, common: replace deprecated pkg_resources with importli…
Browse files Browse the repository at this point in the history
…b.metadata

Fix fedora-copr#2674
Fix fedora-copr#3349
  • Loading branch information
FrostyX committed Aug 19, 2024
1 parent 04d375b commit 69957fc
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 12 deletions.
5 changes: 2 additions & 3 deletions cli/copr_cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from collections import defaultdict

import six
import pkg_resources
import requests

try:
Expand All @@ -36,7 +35,7 @@
from copr_cli.helpers import cli_use_output_format, print_project_info
from copr_cli.monitor import cli_monitor_parser
from copr_cli.printers import cli_get_output_printer as get_printer
from copr_cli.util import get_progress_callback, serializable
from copr_cli.util import get_progress_callback, serializable, package_version
from .build_config import MockProfile


Expand Down Expand Up @@ -1065,7 +1064,7 @@ def setup_parser():
help="Path to an alternative configuration file")

parser.add_argument("--version", action="version",
version="%(prog)s version " + pkg_resources.require('copr-cli')[0].version)
version="%(prog)s version " + package_version("copr-cli"))

subparsers = parser.add_subparsers(title="actions")

Expand Down
23 changes: 23 additions & 0 deletions cli/copr_cli/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,26 @@ def serializable(result):

def json_dumps(result):
return json.dumps(serializable(result), indent=4, sort_keys=True)


def package_version(name):
"""
Return version of a given Python package
The `importlib.metadata` module was introduced in Python 3.8 while
EPEL 8 has Python 3.6. At the same time, `pkg_resources` is deprecated
since Python 3.12 (Fedora 40):
"""
# pylint: disable=import-outside-toplevel
try:
from importlib.metadata import distribution, PackageNotFoundError
try:
return distribution(name).version
except PackageNotFoundError:
return "git"
except ImportError:
import pkg_resources
try:
return pkg_resources.require(name)[0].version
except pkg_resources.DistributionNotFound:
return "git"
27 changes: 27 additions & 0 deletions common/copr_common/compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""
A compatibility layer between already deprecated things from Rawhide and not
yet available replacements in old EPELs
"""


def package_version(name):
"""
Return version of a given Python package
The `importlib.metadata` module was introduced in Python 3.8 while
EPEL 8 has Python 3.6. At the same time, `pkg_resources` is deprecated
since Python 3.12 (Fedora 40):
"""
# pylint: disable=import-outside-toplevel
try:
from importlib.metadata import distribution, PackageNotFoundError
try:
return distribution(name).version
except PackageNotFoundError:
return "git"
except ImportError:
import pkg_resources
try:
return pkg_resources.require(name)[0].version
except pkg_resources.DistributionNotFound:
return "git"
4 changes: 2 additions & 2 deletions common/copr_common/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

import json
import time
import pkg_resources
from requests import get, post, put, RequestException
from copr_common.compat import package_version


class SafeRequest:
Expand All @@ -23,7 +23,7 @@ class SafeRequest:
package_name = 'copr-common'
user_agent = {
'name': package_name,
'version': pkg_resources.require(package_name)[0].version
'version': package_version(package_name),
}

def __init__(self, auth=None, log=None, try_indefinitely=False, timeout=2 * 60):
Expand Down
23 changes: 23 additions & 0 deletions rpmbuild/copr_rpmbuild/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,3 +446,26 @@ def safe_attr(self, name):
return getattr(self.tags, name).expanded_value
except AttributeError:
return ""


def package_version(name):
"""
Return version of a given Python package
The `importlib.metadata` module was introduced in Python 3.8 while
EPEL 8 has Python 3.6. At the same time, `pkg_resources` is deprecated
since Python 3.12 (Fedora 40):
"""
# pylint: disable=import-outside-toplevel
try:
from importlib.metadata import distribution, PackageNotFoundError
try:
return distribution(name).version
except PackageNotFoundError:
return "git"
except ImportError:
import pkg_resources
try:
return pkg_resources.require(name)[0].version
except pkg_resources.DistributionNotFound:
return "git"
17 changes: 10 additions & 7 deletions rpmbuild/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,27 @@
import shutil
import pprint
import shlex
import pkg_resources

from copr_common.request import SafeRequest, RequestError
from copr_rpmbuild import providers
from copr_rpmbuild.builders.mock import MockBuilder
from copr_rpmbuild.automation import run_automation_tools
from copr_rpmbuild.helpers import read_config, \
parse_copr_name, dump_live_log, copr_chroot_to_task_id, macros_for_task, locate_srpm
from copr_rpmbuild.helpers import (
read_config,
parse_copr_name,
dump_live_log,
copr_chroot_to_task_id,
macros_for_task,
locate_srpm,
package_version,
)
from six.moves.urllib.parse import urlparse, urljoin, urlencode

log = logging.getLogger(__name__)
log.setLevel(logging.INFO)
log.addHandler(logging.StreamHandler(sys.stdout))

try:
VERSION = pkg_resources.require('copr-rpmbuild')[0].version
except pkg_resources.DistributionNotFound:
VERSION = 'git'
VERSION = package_version("copr-rpmbuild")

def daemonize():
try:
Expand Down

0 comments on commit 69957fc

Please sign in to comment.