Skip to content

Commit

Permalink
Remove use of pkg_resources
Browse files Browse the repository at this point in the history
This allows us to remove setuptools as a dependency.

Signed-off-by: Stephen Finucane <[email protected]>
  • Loading branch information
stephenfin committed Dec 10, 2024
1 parent cbd7625 commit 62e97df
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
20 changes: 11 additions & 9 deletions click_man/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
:license: MIT, see LICENSE for more details.
"""

import importlib.metadata
import os

import click
from pkg_resources import iter_entry_points, get_distribution

from click_man.core import write_man_pages

Expand All @@ -20,7 +21,7 @@
@click.option('--target', '-t', default=os.path.join(os.getcwd(), 'man'),
type=click.Path(file_okay=False, dir_okay=True, resolve_path=True),
help='Target location for the man pages')
@click.version_option(get_distribution('click-man').version, '-V', '--version')
@click.version_option(importlib.metadata.version('click-man'), '-V', '--version')
@click.argument('name')
def cli(target, name):
"""
Expand All @@ -31,29 +32,30 @@ def cli(target, name):
The generated man pages are written to files in the directory given
by ``--target``.
"""
console_scripts = [ep for ep in iter_entry_points('console_scripts', name=name)]
entry_points = importlib.metadata.entry_points()
console_scripts = entry_points.select(group='console_scripts', name=name)
if len(console_scripts) < 1:
raise click.ClickException('"{0}" is not an installed console script.'.format(name))
# Only generate man pages for first console script
entry_point = console_scripts[0]
(entry_point,) = console_scripts

# create target directory if it does not exist yet
try:
os.makedirs(target)
except OSError:
pass

click.echo('Load entry point {0}'.format(name))
cli = entry_point.resolve()
cli = entry_point.load()

# If the entry point isn't a click.Command object, try to find it in the module
if not isinstance(cli, click.Command):
from importlib import import_module
from inspect import getmembers

if not entry_point.module_name:
if not entry_point.module:
raise click.ClickException('Could not find module name for "{0}".'.format(name))
ep_module = import_module(entry_point.module_name)
ep_module = import_module(entry_point.module)
ep_members = getmembers(ep_module, lambda x: isinstance(x, click.Command))

if len(ep_members) < 1:
Expand All @@ -62,7 +64,7 @@ def cli(target, name):
click.echo('Found alternate entry point {0} in {1}'.format(ep_name, name))

click.echo('Generate man pages for {0} in {1}'.format(name, target))
write_man_pages(name, cli, version=entry_point.dist.version, target_dir=target)
write_man_pages(name, cli, version=entry_point.version, target_dir=target)


if __name__ == '__main__':
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def read(fname):
author_email='[email protected]',
install_requires=[
'click',
'setuptools',
],
packages=find_packages(exclude=('tests', )),
entry_points={
Expand Down

0 comments on commit 62e97df

Please sign in to comment.