2019-07-16 21:19:01 +02:00
|
|
|
# coding: utf-8
|
|
|
|
# Copyright: (c) 2019, Ansible Project
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
|
|
|
# Make coding more python3-ish
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2017-03-23 06:11:40 +01:00
|
|
|
|
2019-04-23 20:54:39 +02:00
|
|
|
import argparse
|
2019-07-16 21:19:01 +02:00
|
|
|
import os.path
|
|
|
|
import pathlib
|
2017-03-23 06:11:40 +01:00
|
|
|
import sys
|
|
|
|
|
|
|
|
from jinja2 import Environment, FileSystemLoader
|
|
|
|
|
2017-03-30 17:31:09 +02:00
|
|
|
from ansible.module_utils._text import to_bytes
|
|
|
|
|
2019-07-16 21:19:01 +02:00
|
|
|
# Pylint doesn't understand Python3 namespace modules.
|
|
|
|
from ..change_detection import update_file_if_different # pylint: disable=relative-beyond-top-level
|
|
|
|
from ..commands import Command # pylint: disable=relative-beyond-top-level
|
2017-03-24 17:38:16 +01:00
|
|
|
|
2017-09-07 21:44:20 +02:00
|
|
|
|
2019-07-16 21:19:01 +02:00
|
|
|
DEFAULT_TEMPLATE_FILE = pathlib.Path(__file__).parents[4] / 'docs/templates/man.j2'
|
2017-09-07 21:44:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
# from https://www.python.org/dev/peps/pep-0257/
|
|
|
|
def trim_docstring(docstring):
|
|
|
|
if not docstring:
|
|
|
|
return ''
|
|
|
|
# Convert tabs to spaces (following the normal Python rules)
|
|
|
|
# and split into a list of lines:
|
|
|
|
lines = docstring.expandtabs().splitlines()
|
|
|
|
# Determine minimum indentation (first line doesn't count):
|
Fix build on Python 3.x by using sys.maxsize. (#30424)
As outlined in https://docs.python.org/3.1/whatsnew/3.0.html#integers,
sys.maxint doesn't exist anymore in Python 3.x because there is no maximum
value for integers in Python 3.x. sys.maxsize is present in all
versions of Python that are currently supported by Ansible, so use that
instead as an arbitrarily large index value.
Fixes the following build error when building with Python 3.x:
make -j1 docs
mkdir -p ./docs/man/man1/ ; \
PYTHONPATH=./lib docs/bin/generate_man.py --template-file=docs/templates/man.j2 --output-dir=docs/man/man1/ --output-format man lib/ansible/cli/*.py
Traceback (most recent call last):
File "docs/bin/generate_man.py", line 253, in <module>
allvars[cli_name] = opts_docs(cli_class_name, cli_name)
File "docs/bin/generate_man.py", line 119, in opts_docs
'long_desc': trim_docstring(cli.__doc__),
File "docs/bin/generate_man.py", line 34, in trim_docstring
indent = sys.maxint
AttributeError: module 'sys' has no attribute 'maxint'
make: *** [Makefile:347: generate_asciidoc] Error 1
2017-09-19 22:23:16 +02:00
|
|
|
indent = sys.maxsize
|
2017-09-07 21:44:20 +02:00
|
|
|
for line in lines[1:]:
|
|
|
|
stripped = line.lstrip()
|
|
|
|
if stripped:
|
|
|
|
indent = min(indent, len(line) - len(stripped))
|
|
|
|
# Remove indentation (first line is special):
|
|
|
|
trimmed = [lines[0].strip()]
|
Fix build on Python 3.x by using sys.maxsize. (#30424)
As outlined in https://docs.python.org/3.1/whatsnew/3.0.html#integers,
sys.maxint doesn't exist anymore in Python 3.x because there is no maximum
value for integers in Python 3.x. sys.maxsize is present in all
versions of Python that are currently supported by Ansible, so use that
instead as an arbitrarily large index value.
Fixes the following build error when building with Python 3.x:
make -j1 docs
mkdir -p ./docs/man/man1/ ; \
PYTHONPATH=./lib docs/bin/generate_man.py --template-file=docs/templates/man.j2 --output-dir=docs/man/man1/ --output-format man lib/ansible/cli/*.py
Traceback (most recent call last):
File "docs/bin/generate_man.py", line 253, in <module>
allvars[cli_name] = opts_docs(cli_class_name, cli_name)
File "docs/bin/generate_man.py", line 119, in opts_docs
'long_desc': trim_docstring(cli.__doc__),
File "docs/bin/generate_man.py", line 34, in trim_docstring
indent = sys.maxint
AttributeError: module 'sys' has no attribute 'maxint'
make: *** [Makefile:347: generate_asciidoc] Error 1
2017-09-19 22:23:16 +02:00
|
|
|
if indent < sys.maxsize:
|
2017-09-07 21:44:20 +02:00
|
|
|
for line in lines[1:]:
|
|
|
|
trimmed.append(line[indent:].rstrip())
|
|
|
|
# Strip off trailing and leading blank lines:
|
|
|
|
while trimmed and not trimmed[-1]:
|
|
|
|
trimmed.pop()
|
|
|
|
while trimmed and not trimmed[0]:
|
|
|
|
trimmed.pop(0)
|
|
|
|
# Return a single string:
|
|
|
|
return '\n'.join(trimmed)
|
|
|
|
|
|
|
|
|
2017-03-23 06:11:40 +01:00
|
|
|
def get_options(optlist):
|
|
|
|
''' get actual options '''
|
|
|
|
|
|
|
|
opts = []
|
|
|
|
for opt in optlist:
|
|
|
|
res = {
|
|
|
|
'desc': opt.help,
|
2019-04-23 20:54:39 +02:00
|
|
|
'options': opt.option_strings
|
2017-03-23 06:11:40 +01:00
|
|
|
}
|
2019-04-23 20:54:39 +02:00
|
|
|
if isinstance(opt, argparse._StoreAction):
|
2017-03-23 06:11:40 +01:00
|
|
|
res['arg'] = opt.dest.upper()
|
2019-04-23 20:54:39 +02:00
|
|
|
elif not res['options']:
|
|
|
|
continue
|
2017-03-23 06:11:40 +01:00
|
|
|
opts.append(res)
|
|
|
|
|
|
|
|
return opts
|
|
|
|
|
2017-03-24 17:38:16 +01:00
|
|
|
|
2019-04-23 20:54:39 +02:00
|
|
|
def dedupe_groups(parser):
|
|
|
|
action_groups = []
|
|
|
|
for action_group in parser._action_groups:
|
|
|
|
found = False
|
|
|
|
for a in action_groups:
|
|
|
|
if a._actions == action_group._actions:
|
|
|
|
found = True
|
|
|
|
break
|
|
|
|
if not found:
|
|
|
|
action_groups.append(action_group)
|
|
|
|
return action_groups
|
|
|
|
|
|
|
|
|
2017-09-07 21:44:20 +02:00
|
|
|
def get_option_groups(option_parser):
|
|
|
|
groups = []
|
2019-04-23 20:54:39 +02:00
|
|
|
for action_group in dedupe_groups(option_parser)[1:]:
|
2017-09-07 21:44:20 +02:00
|
|
|
group_info = {}
|
2019-04-23 20:54:39 +02:00
|
|
|
group_info['desc'] = action_group.description
|
|
|
|
group_info['options'] = action_group._actions
|
|
|
|
group_info['group_obj'] = action_group
|
2017-09-07 21:44:20 +02:00
|
|
|
groups.append(group_info)
|
|
|
|
return groups
|
|
|
|
|
|
|
|
|
2019-04-23 20:54:39 +02:00
|
|
|
def opt_doc_list(parser):
|
2017-03-23 06:11:40 +01:00
|
|
|
''' iterate over options lists '''
|
|
|
|
|
|
|
|
results = []
|
2019-04-23 20:54:39 +02:00
|
|
|
for option_group in dedupe_groups(parser)[1:]:
|
|
|
|
results.extend(get_options(option_group._actions))
|
2017-03-23 06:11:40 +01:00
|
|
|
|
2019-04-23 20:54:39 +02:00
|
|
|
results.extend(get_options(parser._actions))
|
2017-03-23 06:11:40 +01:00
|
|
|
|
|
|
|
return results
|
|
|
|
|
2017-03-24 17:38:16 +01:00
|
|
|
|
2017-09-07 21:44:20 +02:00
|
|
|
# def opts_docs(cli, name):
|
|
|
|
def opts_docs(cli_class_name, cli_module_name):
|
2017-03-23 06:11:40 +01:00
|
|
|
''' generate doc structure from options '''
|
|
|
|
|
2017-09-07 21:44:20 +02:00
|
|
|
cli_name = 'ansible-%s' % cli_module_name
|
|
|
|
if cli_module_name == 'adhoc':
|
|
|
|
cli_name = 'ansible'
|
|
|
|
|
|
|
|
# WIth no action/subcommand
|
|
|
|
# shared opts set
|
|
|
|
# instantiate each cli and ask its options
|
|
|
|
cli_klass = getattr(__import__("ansible.cli.%s" % cli_module_name,
|
|
|
|
fromlist=[cli_class_name]), cli_class_name)
|
2019-04-29 22:38:31 +02:00
|
|
|
cli = cli_klass([cli_name])
|
2017-09-07 21:44:20 +02:00
|
|
|
|
|
|
|
# parse the common options
|
|
|
|
try:
|
2019-04-23 20:54:39 +02:00
|
|
|
cli.init_parser()
|
2018-09-08 02:59:46 +02:00
|
|
|
except Exception:
|
2017-09-07 21:44:20 +02:00
|
|
|
pass
|
2017-03-23 06:11:40 +01:00
|
|
|
|
2017-09-07 21:44:20 +02:00
|
|
|
# base/common cli info
|
2017-03-23 06:11:40 +01:00
|
|
|
docs = {
|
2017-09-07 21:44:20 +02:00
|
|
|
'cli': cli_module_name,
|
|
|
|
'cli_name': cli_name,
|
2019-04-23 20:54:39 +02:00
|
|
|
'usage': cli.parser.format_usage(),
|
2017-03-23 06:11:40 +01:00
|
|
|
'short_desc': cli.parser.description,
|
2017-09-07 21:44:20 +02:00
|
|
|
'long_desc': trim_docstring(cli.__doc__),
|
|
|
|
'actions': {},
|
2019-07-09 21:47:25 +02:00
|
|
|
'content_depth': 2,
|
2017-03-23 06:11:40 +01:00
|
|
|
}
|
2017-09-07 21:44:20 +02:00
|
|
|
option_info = {'option_names': [],
|
|
|
|
'options': [],
|
|
|
|
'groups': []}
|
|
|
|
|
|
|
|
for extras in ('ARGUMENTS'):
|
|
|
|
if hasattr(cli, extras):
|
|
|
|
docs[extras.lower()] = getattr(cli, extras)
|
|
|
|
|
2019-04-23 20:54:39 +02:00
|
|
|
common_opts = opt_doc_list(cli.parser)
|
2017-09-07 21:44:20 +02:00
|
|
|
groups_info = get_option_groups(cli.parser)
|
|
|
|
shared_opt_names = []
|
|
|
|
for opt in common_opts:
|
|
|
|
shared_opt_names.extend(opt.get('options', []))
|
|
|
|
|
|
|
|
option_info['options'] = common_opts
|
|
|
|
option_info['option_names'] = shared_opt_names
|
|
|
|
|
|
|
|
option_info['groups'].extend(groups_info)
|
|
|
|
|
|
|
|
docs.update(option_info)
|
2017-03-23 06:11:40 +01:00
|
|
|
|
2017-09-07 21:44:20 +02:00
|
|
|
# now for each action/subcommand
|
2017-03-23 06:11:40 +01:00
|
|
|
# force populate parser with per action options
|
2017-09-07 21:44:20 +02:00
|
|
|
|
2019-07-09 21:47:25 +02:00
|
|
|
def get_actions(parser, docs):
|
|
|
|
# use class attrs not the attrs on a instance (not that it matters here...)
|
|
|
|
try:
|
|
|
|
subparser = parser._subparsers._group_actions[0].choices
|
|
|
|
except AttributeError:
|
|
|
|
subparser = {}
|
|
|
|
|
|
|
|
depth = 0
|
|
|
|
|
|
|
|
for action, parser in subparser.items():
|
|
|
|
action_info = {'option_names': [],
|
|
|
|
'options': [],
|
|
|
|
'actions': {}}
|
|
|
|
# docs['actions'][action] = {}
|
|
|
|
# docs['actions'][action]['name'] = action
|
|
|
|
action_info['name'] = action
|
|
|
|
action_info['desc'] = trim_docstring(getattr(cli, 'execute_%s' % action).__doc__)
|
|
|
|
|
|
|
|
# docs['actions'][action]['desc'] = getattr(cli, 'execute_%s' % action).__doc__.strip()
|
|
|
|
action_doc_list = opt_doc_list(parser)
|
|
|
|
|
|
|
|
uncommon_options = []
|
|
|
|
for action_doc in action_doc_list:
|
|
|
|
# uncommon_options = []
|
|
|
|
|
|
|
|
option_aliases = action_doc.get('options', [])
|
|
|
|
for option_alias in option_aliases:
|
2017-09-07 21:44:20 +02:00
|
|
|
|
2019-07-09 21:47:25 +02:00
|
|
|
if option_alias in shared_opt_names:
|
|
|
|
continue
|
2017-09-07 21:44:20 +02:00
|
|
|
|
2019-07-09 21:47:25 +02:00
|
|
|
# TODO: use set
|
|
|
|
if option_alias not in action_info['option_names']:
|
|
|
|
action_info['option_names'].append(option_alias)
|
2017-03-23 06:11:40 +01:00
|
|
|
|
2019-07-09 21:47:25 +02:00
|
|
|
if action_doc in action_info['options']:
|
|
|
|
continue
|
2017-09-07 21:44:20 +02:00
|
|
|
|
2019-07-09 21:47:25 +02:00
|
|
|
uncommon_options.append(action_doc)
|
2017-09-07 21:44:20 +02:00
|
|
|
|
2019-07-09 21:47:25 +02:00
|
|
|
action_info['options'] = uncommon_options
|
2017-09-07 21:44:20 +02:00
|
|
|
|
2019-07-09 21:47:25 +02:00
|
|
|
depth = 1 + get_actions(parser, action_info)
|
2017-09-07 21:44:20 +02:00
|
|
|
|
2019-07-09 21:47:25 +02:00
|
|
|
docs['actions'][action] = action_info
|
2017-09-07 21:44:20 +02:00
|
|
|
|
2019-07-09 21:47:25 +02:00
|
|
|
return depth
|
2017-09-07 21:44:20 +02:00
|
|
|
|
2019-07-09 21:47:25 +02:00
|
|
|
action_depth = get_actions(cli.parser, docs)
|
|
|
|
docs['content_depth'] = action_depth + 1
|
2017-09-07 21:44:20 +02:00
|
|
|
|
2019-04-23 20:54:39 +02:00
|
|
|
docs['options'] = opt_doc_list(cli.parser)
|
2017-03-23 06:11:40 +01:00
|
|
|
return docs
|
|
|
|
|
2017-09-07 21:44:20 +02:00
|
|
|
|
2019-07-16 21:19:01 +02:00
|
|
|
class GenerateMan(Command):
|
|
|
|
name = 'generate-man'
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def init_parser(cls, add_parser):
|
|
|
|
parser = add_parser(name=cls.name,
|
|
|
|
description='Generate cli documentation from cli docstrings')
|
|
|
|
|
|
|
|
parser.add_argument("-t", "--template-file", action="store", dest="template_file",
|
|
|
|
default=DEFAULT_TEMPLATE_FILE, help="path to jinja2 template")
|
|
|
|
parser.add_argument("-o", "--output-dir", action="store", dest="output_dir",
|
|
|
|
default='/tmp/', help="Output directory for rst files")
|
|
|
|
parser.add_argument("-f", "--output-format", action="store", dest="output_format",
|
|
|
|
default='man',
|
|
|
|
help="Output format for docs (the default 'man' or 'rst')")
|
|
|
|
parser.add_argument('cli_modules', help='CLI module name(s)', metavar='MODULE_NAME', nargs='*')
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def main(args):
|
|
|
|
template_file = args.template_file
|
|
|
|
template_path = os.path.expanduser(template_file)
|
|
|
|
template_dir = os.path.abspath(os.path.dirname(template_path))
|
|
|
|
template_basename = os.path.basename(template_file)
|
|
|
|
|
|
|
|
output_dir = os.path.abspath(args.output_dir)
|
|
|
|
output_format = args.output_format
|
|
|
|
|
|
|
|
cli_modules = args.cli_modules
|
|
|
|
|
|
|
|
# various cli parsing things checks sys.argv if the 'args' that are passed in are []
|
|
|
|
# so just remove any args so the cli modules dont try to parse them resulting in warnings
|
|
|
|
sys.argv = [sys.argv[0]]
|
|
|
|
|
|
|
|
allvars = {}
|
|
|
|
output = {}
|
|
|
|
cli_list = []
|
|
|
|
cli_bin_name_list = []
|
|
|
|
|
|
|
|
# for binary in os.listdir('../../lib/ansible/cli'):
|
|
|
|
for cli_module_name in cli_modules:
|
|
|
|
binary = os.path.basename(os.path.expanduser(cli_module_name))
|
|
|
|
|
|
|
|
if not binary.endswith('.py'):
|
|
|
|
continue
|
|
|
|
elif binary == '__init__.py':
|
|
|
|
continue
|
|
|
|
|
|
|
|
cli_name = os.path.splitext(binary)[0]
|
|
|
|
|
|
|
|
if cli_name == 'adhoc':
|
|
|
|
cli_class_name = 'AdHocCLI'
|
|
|
|
# myclass = 'AdHocCLI'
|
|
|
|
output[cli_name] = 'ansible.1.rst.in'
|
|
|
|
cli_bin_name = 'ansible'
|
|
|
|
else:
|
|
|
|
# myclass = "%sCLI" % libname.capitalize()
|
|
|
|
cli_class_name = "%sCLI" % cli_name.capitalize()
|
|
|
|
output[cli_name] = 'ansible-%s.1.rst.in' % cli_name
|
|
|
|
cli_bin_name = 'ansible-%s' % cli_name
|
|
|
|
|
|
|
|
# FIXME:
|
|
|
|
allvars[cli_name] = opts_docs(cli_class_name, cli_name)
|
|
|
|
cli_bin_name_list.append(cli_bin_name)
|
|
|
|
|
|
|
|
cli_list = allvars.keys()
|
|
|
|
|
|
|
|
doc_name_formats = {'man': '%s.1.rst.in',
|
|
|
|
'rst': '%s.rst'}
|
|
|
|
|
|
|
|
for cli_name in cli_list:
|
|
|
|
|
|
|
|
# template it!
|
|
|
|
env = Environment(loader=FileSystemLoader(template_dir))
|
|
|
|
template = env.get_template(template_basename)
|
|
|
|
|
|
|
|
# add rest to vars
|
|
|
|
tvars = allvars[cli_name]
|
|
|
|
tvars['cli_list'] = cli_list
|
|
|
|
tvars['cli_bin_name_list'] = cli_bin_name_list
|
|
|
|
tvars['cli'] = cli_name
|
|
|
|
if '-i' in tvars['options']:
|
|
|
|
print('uses inventory')
|
|
|
|
|
|
|
|
manpage = template.render(tvars)
|
|
|
|
filename = os.path.join(output_dir, doc_name_formats[output_format] % tvars['cli_name'])
|
|
|
|
update_file_if_different(filename, to_bytes(manpage))
|