Skip to content

Commit

Permalink
Merge pull request #295 from gotmax23/goodbye_pkg_resources
Browse files Browse the repository at this point in the history
Remove pkg_resources usage
  • Loading branch information
TomasTomecek authored Mar 14, 2023
2 parents 692618c + f933f0f commit 6d63962
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
12 changes: 8 additions & 4 deletions ansible_bender/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
$ ab build ./playbook.yaml fedora:28 my-custom-image
"""
from pkg_resources import get_distribution, DistributionNotFound
try:
__version__ = get_distribution(__name__).version
except DistributionNotFound:
from importlib.metadata import version, PackageNotFoundError
except ImportError:
from importlib_metadata import version, PackageNotFoundError

try:
__version__ = version(__name__)
except PackageNotFoundError:
# package is not installed
pass
__version__ = None
8 changes: 4 additions & 4 deletions ansible_bender/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import json
import sys
import subprocess
import pkg_resources
import yaml
from tabulate import tabulate

from ansible_bender import __version__
from ansible_bender.api import Application
from ansible_bender.constants import ANNOTATIONS_KEY, PLAYBOOK_TEMPLATE
from ansible_bender.core import AnsibleVarsParser
Expand Down Expand Up @@ -421,9 +421,9 @@ def run(self):
subcommand = getattr(self.args, "subcommand", "nope")
try:
if self.args.version:
try:
print(pkg_resources.get_distribution("ansible-bender").version)
except pkg_resources.DistributionNotFound:
if __version__ is not None:
print(__version__)
else:
print("Ansible-bender is not installed, therefore we can't print the version.")
print("Please run `python3 setup.py --version` instead.")
return 18
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ dependencies = [
"tabulate",
"jsonschema",
"setuptools",
# https://peps.python.org/pep-0631/
"importlib_metadata; python_version < '3.8'",
]
dynamic = ["version"]

Expand Down

0 comments on commit 6d63962

Please sign in to comment.