| 1 |
""" |
| 2 |
click-man - Generate man pages for click application |
| 3 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 4 |
|
| 5 |
This module provides a click CLI command to |
| 6 |
generate man pages from a click application. |
| 7 |
|
| 8 |
:copyright: (c) 2016 by Timo Furrer. |
| 9 |
:license: MIT, see LICENSE for more details. |
| 10 |
""" |
| 11 |
|
| 12 |
from datetime import datetime |
| 13 |
import importlib.metadata |
| 14 |
import os |
| 15 |
import sys |
| 16 |
from typing import Optional |
| 17 |
|
| 18 |
import click |
| 19 |
|
| 20 |
from click_man.core import write_man_pages |
| 21 |
|
| 22 |
|
| 23 |
def _get_entry_point(name: str) -> Optional[importlib.metadata.EntryPoint]: |
| 24 |
entry_points = importlib.metadata.entry_points() |
| 25 |
if sys.version_info >= (3, 10): |
| 26 |
console_scripts = entry_points.select( |
| 27 |
group='console_scripts', name=name |
| 28 |
) |
| 29 |
else: |
| 30 |
console_scripts = [ |
| 31 |
ep |
| 32 |
for ep in entry_points.get('console_scripts', []) |
| 33 |
if ep.name == name |
| 34 |
] |
| 35 |
|
| 36 |
if len(console_scripts) < 1: |
| 37 |
return None |
| 38 |
|
| 39 |
# Only generate man pages for first console script |
| 40 |
return tuple(console_scripts)[0] |
| 41 |
|
| 42 |
|
| 43 |
@click.command(context_settings={'help_option_names': ['-h', '--help']}) |
| 44 |
@click.option( |
| 45 |
'--target', |
| 46 |
'-t', |
| 47 |
default=os.path.join(os.getcwd(), 'man'), |
| 48 |
type=click.Path(file_okay=False, dir_okay=True, resolve_path=True), |
| 49 |
help='Target location for the man pages', |
| 50 |
) |
| 51 |
@click.option('--man-version', help='Version to use in generated man page(s)') |
| 52 |
@click.option('--man-date', help='Date to use in generated man page(s)') |
| 53 |
@click.version_option( |
| 54 |
importlib.metadata.version('click-man'), '-V', '--version' |
| 55 |
) |
| 56 |
@click.argument('name') |
| 57 |
def cli(target, name, man_version, man_date): |
| 58 |
""" |
| 59 |
Generate man pages for the scripts defined in the ``console_scripts`` entry |
| 60 |
point. |
| 61 |
|
| 62 |
The cli application is gathered from entry points of installed packages. |
| 63 |
|
| 64 |
The generated man pages are written to files in the directory given |
| 65 |
by ``--target``. |
| 66 |
""" |
| 67 |
entry_point = _get_entry_point(name) |
| 68 |
if not entry_point: |
| 69 |
raise click.ClickException( |
| 70 |
'"{0}" is not an installed console script.'.format(name) |
| 71 |
) |
| 72 |
|
| 73 |
# create target directory if it does not exist yet |
| 74 |
try: |
| 75 |
os.makedirs(target) |
| 76 |
except OSError: |
| 77 |
pass |
| 78 |
|
| 79 |
if not man_version: |
| 80 |
man_version = entry_point.version |
| 81 |
|
| 82 |
if man_date: |
| 83 |
try: |
| 84 |
datetime.strptime(man_date, '%Y-%m-%d') |
| 85 |
except ValueError: |
| 86 |
raise click.ClickException( |
| 87 |
'"{0}" is not a valid date.'.format(man_date) |
| 88 |
) |
| 89 |
|
| 90 |
click.echo('Load entry point {0}'.format(name)) |
| 91 |
cli = entry_point.load() |
| 92 |
|
| 93 |
# If the entry point isn't a click.Command object, try to find it in the |
| 94 |
# module |
| 95 |
if not isinstance(cli, click.Command): |
| 96 |
from importlib import import_module |
| 97 |
from inspect import getmembers |
| 98 |
|
| 99 |
if not entry_point.module: |
| 100 |
raise click.ClickException( |
| 101 |
'Could not find module name for "{0}".'.format(name) |
| 102 |
) |
| 103 |
|
| 104 |
ep_module = import_module(entry_point.module) |
| 105 |
ep_members = getmembers( |
| 106 |
ep_module, |
| 107 |
lambda x: isinstance(x, click.Command), |
| 108 |
) |
| 109 |
|
| 110 |
if len(ep_members) < 1: |
| 111 |
raise click.ClickException( |
| 112 |
'Could not find click.Command object for "{0}".'.format(name) |
| 113 |
) |
| 114 |
|
| 115 |
ep_name, cli = ep_members[0] |
| 116 |
click.echo( |
| 117 |
'Found alternate entry point {0} in {1}'.format(ep_name, name) |
| 118 |
) |
| 119 |
|
| 120 |
click.echo('Generate man pages for {0} in {1}'.format(name, target)) |
| 121 |
write_man_pages( |
| 122 |
name, |
| 123 |
cli, |
| 124 |
version=man_version, |
| 125 |
target_dir=target, |
| 126 |
date=man_date, |
| 127 |
) |