2012-09-26 20:41:44 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# (c) 2012, Jan-Piet Mens <jpmens () gmail.com>
|
2014-09-26 23:10:13 +02:00
|
|
|
# (c) 2012-2014, Michael DeHaan <michael@ansible.com> and others
|
2017-08-14 04:12:43 +02:00
|
|
|
# (c) 2017 Ansible Project
|
2012-09-26 20:41:44 +02:00
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2017-08-14 04:12:43 +02:00
|
|
|
from __future__ import absolute_import, division, print_function
|
2016-09-07 07:54:17 +02:00
|
|
|
__metaclass__ = type
|
2016-04-12 02:11:55 +02:00
|
|
|
|
2017-08-14 04:12:43 +02:00
|
|
|
|
2017-05-30 19:08:25 +02:00
|
|
|
import datetime
|
2013-04-28 21:03:45 +02:00
|
|
|
import glob
|
2012-10-11 18:11:33 +02:00
|
|
|
import optparse
|
2017-05-30 19:08:25 +02:00
|
|
|
import os
|
2017-09-19 17:14:27 +02:00
|
|
|
import pprint
|
2017-05-30 19:08:25 +02:00
|
|
|
import re
|
|
|
|
import sys
|
2015-07-17 16:00:02 +02:00
|
|
|
import warnings
|
2017-05-30 19:08:25 +02:00
|
|
|
from collections import defaultdict
|
2017-08-14 04:12:43 +02:00
|
|
|
try:
|
|
|
|
from html import escape as html_escape
|
|
|
|
except ImportError:
|
|
|
|
# Python-3.2 or later
|
|
|
|
import cgi
|
|
|
|
|
|
|
|
def html_escape(text, quote=True):
|
|
|
|
return cgi.escape(text, quote)
|
|
|
|
|
|
|
|
import yaml
|
2013-12-25 18:35:41 +01:00
|
|
|
from jinja2 import Environment, FileSystemLoader
|
2017-09-19 17:14:27 +02:00
|
|
|
from six import iteritems, string_types
|
2013-12-25 18:35:41 +01:00
|
|
|
|
2016-09-07 07:54:17 +02:00
|
|
|
from ansible.errors import AnsibleError
|
|
|
|
from ansible.module_utils._text import to_bytes
|
2017-03-18 02:07:39 +01:00
|
|
|
from ansible.utils import plugin_docs
|
2012-10-01 03:10:07 +02:00
|
|
|
|
2017-08-14 04:12:43 +02:00
|
|
|
|
2013-12-25 18:35:41 +01:00
|
|
|
#####################################################################################
|
|
|
|
# constants and paths
|
|
|
|
|
2013-11-28 03:34:00 +01:00
|
|
|
# if a module is added in a version of Ansible older than this, don't print the version added information
|
|
|
|
# in the module documentation because everyone is assumed to be running something newer than this already.
|
2015-07-17 16:00:02 +02:00
|
|
|
TO_OLD_TO_BE_NOTABLE = 1.3
|
2013-11-28 03:34:00 +01:00
|
|
|
|
2012-10-13 01:21:41 +02:00
|
|
|
# Get parent directory of the directory this script lives in
|
2017-05-30 19:08:25 +02:00
|
|
|
MODULEDIR = os.path.abspath(os.path.join(
|
2014-09-26 23:10:13 +02:00
|
|
|
os.path.dirname(os.path.realpath(__file__)), os.pardir, 'lib', 'ansible', 'modules'
|
2013-12-25 17:45:27 +01:00
|
|
|
))
|
|
|
|
|
|
|
|
# The name of the DOCUMENTATION template
|
2017-05-30 19:08:25 +02:00
|
|
|
EXAMPLE_YAML = os.path.abspath(os.path.join(
|
2013-12-25 17:45:27 +01:00
|
|
|
os.path.dirname(os.path.realpath(__file__)), os.pardir, 'examples', 'DOCUMENTATION.yml'
|
|
|
|
))
|
2012-09-26 20:41:44 +02:00
|
|
|
|
|
|
|
_ITALIC = re.compile(r"I\(([^)]+)\)")
|
2017-05-30 19:08:25 +02:00
|
|
|
_BOLD = re.compile(r"B\(([^)]+)\)")
|
2012-09-26 20:41:44 +02:00
|
|
|
_MODULE = re.compile(r"M\(([^)]+)\)")
|
2017-05-30 19:08:25 +02:00
|
|
|
_URL = re.compile(r"U\(([^)]+)\)")
|
|
|
|
_CONST = re.compile(r"C\(([^)]+)\)")
|
2012-09-26 20:41:44 +02:00
|
|
|
|
2017-05-13 17:04:30 +02:00
|
|
|
DEPRECATED = b" (D)"
|
2017-05-30 19:08:25 +02:00
|
|
|
|
2012-09-26 20:41:44 +02:00
|
|
|
|
|
|
|
def rst_ify(text):
|
2013-12-25 18:35:41 +01:00
|
|
|
''' convert symbols like I(this is in italics) to valid restructured text '''
|
2012-09-26 20:41:44 +02:00
|
|
|
|
2015-07-28 20:29:43 +02:00
|
|
|
try:
|
|
|
|
t = _ITALIC.sub(r'*' + r"\1" + r"*", text)
|
|
|
|
t = _BOLD.sub(r'**' + r"\1" + r"**", t)
|
|
|
|
t = _MODULE.sub(r':ref:`' + r"\1 <\1>" + r"`", t)
|
|
|
|
t = _URL.sub(r"\1", t)
|
|
|
|
t = _CONST.sub(r'``' + r"\1" + r"``", t)
|
|
|
|
except Exception as e:
|
|
|
|
raise AnsibleError("Could not process (%s) : %s" % (str(text), str(e)))
|
2012-09-26 20:41:44 +02:00
|
|
|
|
|
|
|
return t
|
|
|
|
|
2013-06-14 20:27:59 +02:00
|
|
|
|
2013-12-25 18:35:41 +01:00
|
|
|
def html_ify(text):
|
|
|
|
''' convert symbols like I(this is in italics) to valid HTML '''
|
2012-10-18 07:34:17 +02:00
|
|
|
|
2017-08-14 04:12:43 +02:00
|
|
|
t = html_escape(text)
|
2013-12-25 18:35:41 +01:00
|
|
|
t = _ITALIC.sub("<em>" + r"\1" + "</em>", t)
|
|
|
|
t = _BOLD.sub("<b>" + r"\1" + "</b>", t)
|
|
|
|
t = _MODULE.sub("<span class='module'>" + r"\1" + "</span>", t)
|
|
|
|
t = _URL.sub("<a href='" + r"\1" + "'>" + r"\1" + "</a>", t)
|
|
|
|
t = _CONST.sub("<code>" + r"\1" + "</code>", t)
|
2012-10-18 07:34:17 +02:00
|
|
|
|
|
|
|
return t
|
|
|
|
|
2013-12-25 18:35:41 +01:00
|
|
|
|
2012-09-26 20:41:44 +02:00
|
|
|
def rst_fmt(text, fmt):
|
2013-12-25 18:35:41 +01:00
|
|
|
''' helper for Jinja2 to do format strings '''
|
|
|
|
|
2012-09-26 20:41:44 +02:00
|
|
|
return fmt % (text)
|
|
|
|
|
2013-12-25 18:35:41 +01:00
|
|
|
|
2012-09-26 20:41:44 +02:00
|
|
|
def rst_xline(width, char="="):
|
2013-12-25 18:35:41 +01:00
|
|
|
''' return a restructured text line of a given length '''
|
|
|
|
|
2012-09-26 20:41:44 +02:00
|
|
|
return char * width
|
|
|
|
|
|
|
|
|
2017-08-14 04:12:43 +02:00
|
|
|
def write_data(text, output_dir, outputname, module=None):
|
2013-12-25 18:35:41 +01:00
|
|
|
''' dumps module output to a file or the screen, as requested '''
|
|
|
|
|
2017-08-14 04:12:43 +02:00
|
|
|
if output_dir is not None:
|
|
|
|
if module:
|
|
|
|
outputname = outputname % module
|
2017-09-19 17:14:27 +02:00
|
|
|
|
|
|
|
if not os.path.exists(output_dir):
|
2017-09-21 03:09:55 +02:00
|
|
|
os.makedirs(output_dir)
|
2017-08-14 04:12:43 +02:00
|
|
|
fname = os.path.join(output_dir, outputname)
|
2017-05-30 19:08:25 +02:00
|
|
|
fname = fname.replace(".py", "")
|
2017-08-14 04:12:43 +02:00
|
|
|
with open(fname, 'wb') as f:
|
|
|
|
f.write(to_bytes(text))
|
2012-10-09 22:04:55 +02:00
|
|
|
else:
|
2015-08-31 02:35:14 +02:00
|
|
|
print(text)
|
2012-10-09 22:04:55 +02:00
|
|
|
|
2014-09-26 23:10:13 +02:00
|
|
|
|
2017-08-14 04:12:43 +02:00
|
|
|
def get_module_info(module_dir, limit_to_modules=None, verbose=False):
|
|
|
|
'''
|
|
|
|
Returns information about modules and the categories that they belong to
|
|
|
|
|
|
|
|
:arg module_dir: file system path to the top of the modules directory
|
|
|
|
:kwarg limit_to_modules: If given, this is a list of module names to
|
|
|
|
generate information for. All other modules will be ignored.
|
|
|
|
:returns: Tuple of two dicts containing module_info, categories, and
|
|
|
|
aliases and a set listing deprecated modules:
|
|
|
|
|
|
|
|
:module_info: mapping of module names to information about them. The fields of the dict are:
|
|
|
|
|
|
|
|
:path: filesystem path to the module
|
|
|
|
:deprecated: boolean. True means the module is deprecated otherwise not.
|
|
|
|
:aliases: set of aliases to this module name
|
|
|
|
:metadata: The modules metadata (as recorded in the module)
|
|
|
|
:doc: The documentation structure for the module
|
|
|
|
:examples: The module's examples
|
|
|
|
:returndocs: The module's returndocs
|
|
|
|
|
|
|
|
:categories: maps category names to a dict. The dict contains at
|
|
|
|
least one key, '_modules' which contains a list of module names in
|
|
|
|
that category. Any other keys in the dict are subcategories with
|
|
|
|
the same structure.
|
2017-09-19 17:14:27 +02:00
|
|
|
|
2017-08-14 04:12:43 +02:00
|
|
|
'''
|
2013-12-25 18:35:41 +01:00
|
|
|
|
2016-04-12 02:11:55 +02:00
|
|
|
categories = dict()
|
2017-08-14 04:12:43 +02:00
|
|
|
module_info = defaultdict(dict)
|
2016-04-12 02:11:55 +02:00
|
|
|
|
|
|
|
# * windows powershell modules have documentation stubs in python docstring
|
|
|
|
# format (they are not executed) so skip the ps1 format files
|
|
|
|
# * One glob level for every module level that we're going to traverse
|
2017-02-07 16:49:55 +01:00
|
|
|
files = (
|
|
|
|
glob.glob("%s/*.py" % module_dir) +
|
|
|
|
glob.glob("%s/*/*.py" % module_dir) +
|
|
|
|
glob.glob("%s/*/*/*.py" % module_dir) +
|
|
|
|
glob.glob("%s/*/*/*/*.py" % module_dir)
|
|
|
|
)
|
2016-04-12 02:11:55 +02:00
|
|
|
|
|
|
|
for module_path in files:
|
2017-08-14 04:12:43 +02:00
|
|
|
# Do not list __init__.py files
|
2016-04-12 02:11:55 +02:00
|
|
|
if module_path.endswith('__init__.py'):
|
|
|
|
continue
|
2017-08-14 04:12:43 +02:00
|
|
|
|
|
|
|
# Do not list blacklisted modules
|
|
|
|
module = os.path.splitext(os.path.basename(module_path))[0]
|
2017-09-23 05:19:50 +02:00
|
|
|
if module in plugin_docs.BLACKLIST['MODULE'] or module == 'base':
|
2017-08-14 04:12:43 +02:00
|
|
|
continue
|
|
|
|
|
|
|
|
# If requested, limit module documentation building only to passed-in
|
|
|
|
# modules.
|
2017-08-18 16:35:06 +02:00
|
|
|
if limit_to_modules is not None and module.lower() not in limit_to_modules:
|
2017-08-14 04:12:43 +02:00
|
|
|
continue
|
|
|
|
|
|
|
|
deprecated = False
|
|
|
|
if module.startswith("_"):
|
|
|
|
if os.path.islink(module_path):
|
|
|
|
# Handle aliases
|
|
|
|
source = os.path.splitext(os.path.basename(os.path.realpath(module_path)))[0]
|
|
|
|
module = module.replace("_", "", 1)
|
|
|
|
aliases = module_info[source].get('aliases', set())
|
|
|
|
aliases.add(module)
|
|
|
|
# In case we just created this via get()'s fallback
|
|
|
|
module_info[source]['aliases'] = aliases
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
# Handle deprecations
|
|
|
|
module = module.replace("_", "", 1)
|
|
|
|
deprecated = True
|
|
|
|
|
|
|
|
#
|
|
|
|
# Regular module to process
|
|
|
|
#
|
|
|
|
|
2016-04-12 02:11:55 +02:00
|
|
|
category = categories
|
2016-12-06 20:27:14 +01:00
|
|
|
|
2017-08-14 04:12:43 +02:00
|
|
|
# Start at the second directory because we don't want the "vendor"
|
2016-12-06 20:27:14 +01:00
|
|
|
mod_path_only = os.path.dirname(module_path[len(module_dir):])
|
|
|
|
|
2017-09-19 17:14:27 +02:00
|
|
|
module_categories = []
|
2017-08-14 04:12:43 +02:00
|
|
|
# build up the categories that this module belongs to
|
2016-04-12 02:11:55 +02:00
|
|
|
for new_cat in mod_path_only.split('/')[1:]:
|
|
|
|
if new_cat not in category:
|
|
|
|
category[new_cat] = dict()
|
2017-08-14 04:12:43 +02:00
|
|
|
category[new_cat]['_modules'] = []
|
2017-09-19 17:14:27 +02:00
|
|
|
module_categories.append(new_cat)
|
2016-04-12 02:11:55 +02:00
|
|
|
category = category[new_cat]
|
|
|
|
|
2017-08-14 04:12:43 +02:00
|
|
|
category['_modules'].append(module)
|
2016-04-12 02:11:55 +02:00
|
|
|
|
2017-09-19 17:14:27 +02:00
|
|
|
# the category we will use in links (so list_of_all_plugins can point to plugins/action_plugins/*'
|
|
|
|
if module_categories:
|
|
|
|
primary_category = module_categories[0]
|
|
|
|
|
2017-08-14 04:12:43 +02:00
|
|
|
# use ansible core library to parse out doc metadata YAML and plaintext examples
|
|
|
|
doc, examples, returndocs, metadata = plugin_docs.get_docstring(module_path, verbose=verbose)
|
|
|
|
|
|
|
|
# save all the information
|
|
|
|
module_info[module] = {'path': module_path,
|
|
|
|
'deprecated': deprecated,
|
|
|
|
'aliases': set(),
|
|
|
|
'metadata': metadata,
|
|
|
|
'doc': doc,
|
|
|
|
'examples': examples,
|
|
|
|
'returndocs': returndocs,
|
2017-09-19 17:14:27 +02:00
|
|
|
'categories': module_categories,
|
|
|
|
'primary_category': primary_category,
|
2017-08-14 04:12:43 +02:00
|
|
|
}
|
2016-04-12 02:11:55 +02:00
|
|
|
|
|
|
|
# keep module tests out of becoming module docs
|
2015-10-28 00:59:12 +01:00
|
|
|
if 'test' in categories:
|
|
|
|
del categories['test']
|
|
|
|
|
2017-08-14 04:12:43 +02:00
|
|
|
return module_info, categories
|
2012-10-09 22:04:55 +02:00
|
|
|
|
2013-12-25 18:35:41 +01:00
|
|
|
|
|
|
|
def generate_parser():
|
|
|
|
''' generate an optparse parser '''
|
2012-09-26 20:41:44 +02:00
|
|
|
|
2012-10-11 18:11:33 +02:00
|
|
|
p = optparse.OptionParser(
|
|
|
|
version='%prog 1.0',
|
|
|
|
usage='usage: %prog [options] arg1 arg2',
|
2013-12-25 18:35:41 +01:00
|
|
|
description='Generate module documentation from metadata',
|
2012-10-11 18:11:33 +02:00
|
|
|
)
|
|
|
|
|
2013-12-25 18:35:41 +01:00
|
|
|
p.add_option("-A", "--ansible-version", action="store", dest="ansible_version", default="unknown", help="Ansible version number")
|
|
|
|
p.add_option("-M", "--module-dir", action="store", dest="module_dir", default=MODULEDIR, help="Ansible library path")
|
2017-09-19 17:14:27 +02:00
|
|
|
p.add_option("-P", "--plugin-type", action="store", dest="plugin_type", default='modules', help="The type of plugin (plugins, modules)")
|
2013-12-25 18:35:41 +01:00
|
|
|
p.add_option("-T", "--template-dir", action="store", dest="template_dir", default="hacking/templates", help="directory containing Jinja2 templates")
|
2014-02-22 15:51:59 +01:00
|
|
|
p.add_option("-t", "--type", action='store', dest='type', choices=['rst'], default='rst', help="Document type")
|
2013-12-25 19:38:40 +01:00
|
|
|
p.add_option("-v", "--verbose", action='store_true', default=False, help="Verbose")
|
2013-12-25 18:35:41 +01:00
|
|
|
p.add_option("-o", "--output-dir", action="store", dest="output_dir", default=None, help="Output directory for module files")
|
|
|
|
p.add_option("-I", "--includes-file", action="store", dest="includes_file", default=None, help="Create a file containing list of processed modules")
|
2017-08-04 22:10:36 +02:00
|
|
|
p.add_option("-l", "--limit-to-modules", action="store", dest="limit_to_modules", default=None,
|
|
|
|
help="Limit building module documentation to comma-separated list of modules. Specify non-existing module name for no modules.")
|
2012-10-13 01:21:41 +02:00
|
|
|
p.add_option('-V', action='version', help='Show version number and exit')
|
2013-12-25 18:35:41 +01:00
|
|
|
return p
|
2012-10-11 18:11:33 +02:00
|
|
|
|
2012-09-26 20:41:44 +02:00
|
|
|
|
2017-09-19 17:14:27 +02:00
|
|
|
def jinja2_environment(template_dir, typ, plugin_type):
|
2012-10-13 01:21:41 +02:00
|
|
|
|
2013-12-25 18:35:41 +01:00
|
|
|
env = Environment(loader=FileSystemLoader(template_dir),
|
2017-05-30 19:08:25 +02:00
|
|
|
variable_start_string="@{",
|
|
|
|
variable_end_string="}@",
|
|
|
|
trim_blocks=True)
|
2012-09-28 09:59:43 +02:00
|
|
|
env.globals['xline'] = rst_xline
|
2012-09-28 03:06:31 +02:00
|
|
|
|
2017-08-14 04:12:43 +02:00
|
|
|
templates = {}
|
2013-12-25 18:35:41 +01:00
|
|
|
if typ == 'rst':
|
2013-12-25 17:44:01 +01:00
|
|
|
env.filters['convert_symbols_to_format'] = rst_ify
|
2012-09-28 03:06:31 +02:00
|
|
|
env.filters['html_ify'] = html_ify
|
2012-09-26 20:41:44 +02:00
|
|
|
env.filters['fmt'] = rst_fmt
|
|
|
|
env.filters['xline'] = rst_xline
|
2017-08-14 04:12:43 +02:00
|
|
|
templates['plugin'] = env.get_template('plugin.rst.j2')
|
2017-09-19 17:14:27 +02:00
|
|
|
templates['category_list'] = env.get_template('%s_by_category.rst.j2' % plugin_type)
|
|
|
|
templates['support_list'] = env.get_template('%s_by_support.rst.j2' % plugin_type)
|
|
|
|
templates['list_of_CATEGORY_modules'] = env.get_template('list_of_CATEGORY_%s.rst.j2' % plugin_type)
|
2013-12-25 18:35:41 +01:00
|
|
|
else:
|
|
|
|
raise Exception("unknown module format type: %s" % typ)
|
2012-09-30 19:07:40 +02:00
|
|
|
|
2017-09-19 17:14:27 +02:00
|
|
|
return templates
|
2012-09-26 20:41:44 +02:00
|
|
|
|
2017-05-30 19:08:25 +02:00
|
|
|
|
2015-07-17 16:00:02 +02:00
|
|
|
def too_old(added):
|
|
|
|
if not added:
|
|
|
|
return False
|
|
|
|
try:
|
|
|
|
added_tokens = str(added).split(".")
|
|
|
|
readded = added_tokens[0] + "." + added_tokens[1]
|
|
|
|
added_float = float(readded)
|
|
|
|
except ValueError as e:
|
|
|
|
warnings.warn("Could not parse %s: %s" % (added, str(e)))
|
|
|
|
return False
|
2017-08-14 04:12:43 +02:00
|
|
|
return added_float < TO_OLD_TO_BE_NOTABLE
|
2013-12-25 18:35:41 +01:00
|
|
|
|
2014-10-31 19:20:26 +01:00
|
|
|
|
2017-09-23 05:19:50 +02:00
|
|
|
def process_modules(module_map, templates, outputname, output_dir, ansible_version, plugin_type):
|
2017-08-14 04:12:43 +02:00
|
|
|
for module in module_map:
|
2017-09-19 17:14:27 +02:00
|
|
|
# print("rendering: %s" % module)
|
|
|
|
|
|
|
|
# pprint.pprint(('process_modules module:', module))
|
2013-12-25 18:35:41 +01:00
|
|
|
|
2017-08-14 04:12:43 +02:00
|
|
|
fname = module_map[module]['path']
|
2013-12-25 18:35:41 +01:00
|
|
|
|
2017-09-19 17:14:27 +02:00
|
|
|
# pprint.pprint(('process_modules module_info: ', module_map[module]))
|
|
|
|
|
|
|
|
module_categories = module_map[module].get('categories', [])
|
|
|
|
|
2017-08-14 04:12:43 +02:00
|
|
|
# crash if module is missing documentation and not explicitly hidden from docs index
|
|
|
|
if module_map[module]['doc'] is None:
|
2017-09-19 17:14:27 +02:00
|
|
|
print("%s: ERROR: MODULE MISSING DOCUMENTATION" % (fname,))
|
|
|
|
_doc = {'module': module,
|
|
|
|
'version_added': '2.4',
|
|
|
|
'filename': fname}
|
|
|
|
module_map[module]['doc'] = _doc
|
|
|
|
# continue
|
2014-10-31 19:20:26 +01:00
|
|
|
|
2017-08-14 04:12:43 +02:00
|
|
|
# Going to reference this heavily so make a short name to reference it by
|
|
|
|
doc = module_map[module]['doc']
|
2013-12-25 18:35:41 +01:00
|
|
|
|
2017-09-19 17:14:27 +02:00
|
|
|
# pprint.pprint(('process_modules doc: ', doc))
|
|
|
|
|
|
|
|
# add some defaults for plugins that dont have most of the info
|
|
|
|
doc['module'] = doc.get('module', module)
|
|
|
|
doc['version_added'] = doc.get('version_added', 'historical')
|
|
|
|
|
|
|
|
doc['plugin_type'] = plugin_type
|
|
|
|
|
2017-08-14 04:12:43 +02:00
|
|
|
if module_map[module]['deprecated'] and 'deprecated' not in doc:
|
2017-09-19 17:14:27 +02:00
|
|
|
print("%s: WARNING: MODULE MISSING DEPRECATION DOCUMENTATION: %s" % (fname, 'deprecated'))
|
|
|
|
|
|
|
|
required_fields = ('short_description',)
|
|
|
|
for field in required_fields:
|
|
|
|
if field not in doc:
|
|
|
|
print("%s: WARNING: MODULE MISSING field '%s'" % (fname, field))
|
|
|
|
|
|
|
|
not_nullable_fields = ('short_description',)
|
|
|
|
for field in not_nullable_fields:
|
|
|
|
if field in doc and doc[field] in (None, ''):
|
|
|
|
print("%s: WARNING: MODULE field '%s' DOCUMENTATION is null/empty value=%s" % (fname, field, doc[field]))
|
2014-09-26 23:52:50 +02:00
|
|
|
|
2017-08-14 04:12:43 +02:00
|
|
|
if 'version_added' not in doc:
|
2017-09-19 17:14:27 +02:00
|
|
|
pprint.pprint(doc)
|
|
|
|
# sys.exit("*** ERROR: missing version_added in: %s ***\n" % module)
|
2013-04-28 21:03:45 +02:00
|
|
|
|
2017-08-14 04:12:43 +02:00
|
|
|
#
|
|
|
|
# The present template gets everything from doc so we spend most of this
|
|
|
|
# function moving data into doc for the template to reference
|
|
|
|
#
|
2013-12-25 18:35:41 +01:00
|
|
|
|
2017-08-14 04:12:43 +02:00
|
|
|
if module_map[module]['aliases']:
|
|
|
|
doc['aliases'] = module_map[module]['aliases']
|
2013-12-25 18:35:41 +01:00
|
|
|
|
2017-08-14 04:12:43 +02:00
|
|
|
# don't show version added information if it's too old to be called out
|
|
|
|
added = 0
|
|
|
|
if doc['version_added'] == 'historical':
|
|
|
|
del doc['version_added']
|
2014-11-04 05:14:22 +01:00
|
|
|
else:
|
2017-08-14 04:12:43 +02:00
|
|
|
added = doc['version_added']
|
|
|
|
|
|
|
|
# Strip old version_added for the module
|
|
|
|
if too_old(added):
|
|
|
|
del doc['version_added']
|
|
|
|
|
|
|
|
option_names = []
|
|
|
|
|
|
|
|
if 'options' in doc and doc['options']:
|
|
|
|
for (k, v) in iteritems(doc['options']):
|
|
|
|
# Error out if there's no description
|
|
|
|
if 'description' not in doc['options'][k]:
|
|
|
|
raise AnsibleError("Missing required description for option %s in %s " % (k, module))
|
|
|
|
|
|
|
|
# Error out if required isn't a boolean (people have been putting
|
|
|
|
# information on when something is required in here. Those need
|
|
|
|
# to go in the description instead).
|
|
|
|
required_value = doc['options'][k].get('required', False)
|
|
|
|
if not isinstance(required_value, bool):
|
|
|
|
raise AnsibleError("Invalid required value '%s' for option '%s' in '%s' (must be truthy)" % (required_value, k, module))
|
|
|
|
|
|
|
|
# Strip old version_added information for options
|
|
|
|
if 'version_added' in doc['options'][k] and too_old(doc['options'][k]['version_added']):
|
|
|
|
del doc['options'][k]['version_added']
|
|
|
|
|
|
|
|
# Make sure description is a list of lines for later formatting
|
|
|
|
if not isinstance(doc['options'][k]['description'], list):
|
|
|
|
doc['options'][k]['description'] = [doc['options'][k]['description']]
|
|
|
|
|
|
|
|
option_names.append(k)
|
|
|
|
|
|
|
|
option_names.sort()
|
|
|
|
|
|
|
|
doc['option_keys'] = option_names
|
|
|
|
doc['filename'] = fname
|
|
|
|
doc['docuri'] = doc['module'].replace('_', '-')
|
|
|
|
doc['now_date'] = datetime.date.today().strftime('%Y-%m-%d')
|
|
|
|
doc['ansible_version'] = ansible_version
|
2017-09-19 17:14:27 +02:00
|
|
|
|
|
|
|
# check the 'deprecated' field in doc. We expect a dict potentially with 'why', 'version', and 'alternative' fields
|
|
|
|
# examples = module_map[module]['examples']
|
|
|
|
# print('\n\n%s: type of examples: %s\n' % (module, type(examples)))
|
|
|
|
# if examples and not isinstance(examples, (str, unicode, list)):
|
|
|
|
# raise TypeError('module %s examples is wrong type (%s): %s' % (module, type(examples), examples))
|
|
|
|
|
|
|
|
# use 'examples' for 'plainexamples' if 'examples' is a string
|
|
|
|
if isinstance(module_map[module]['examples'], string_types):
|
|
|
|
doc['plainexamples'] = module_map[module]['examples'] # plain text
|
|
|
|
else:
|
|
|
|
doc['plainexamples'] = ''
|
|
|
|
|
2017-08-14 04:12:43 +02:00
|
|
|
doc['metadata'] = module_map[module]['metadata']
|
|
|
|
|
2017-09-19 17:14:27 +02:00
|
|
|
# pprint.pprint(module_map[module]
|
2017-08-14 04:12:43 +02:00
|
|
|
if module_map[module]['returndocs']:
|
|
|
|
try:
|
|
|
|
doc['returndocs'] = yaml.safe_load(module_map[module]['returndocs'])
|
2017-09-19 17:14:27 +02:00
|
|
|
except Exception as e:
|
|
|
|
print("%s:%s:yaml error:%s:returndocs=%s" % (fname, module, e, module_map[module]['returndocs']))
|
|
|
|
doc['returndocs'] = None
|
2014-11-04 04:02:13 +01:00
|
|
|
else:
|
2017-08-14 04:12:43 +02:00
|
|
|
doc['returndocs'] = None
|
|
|
|
|
2017-09-19 17:14:27 +02:00
|
|
|
doc['author'] = doc.get('author', ['UNKNOWN'])
|
|
|
|
if isinstance(doc['author'], string_types):
|
|
|
|
doc['author'] = [doc['author']]
|
|
|
|
|
|
|
|
# print('about to template')
|
|
|
|
# pprint.pprint(doc)
|
2017-08-14 04:12:43 +02:00
|
|
|
text = templates['plugin'].render(doc)
|
|
|
|
|
2017-09-19 17:14:27 +02:00
|
|
|
# plugins get namespace dirs but modules do not
|
|
|
|
if plugin_type == 'plugins':
|
|
|
|
for module_category in module_categories:
|
|
|
|
category_output_dir = os.path.join(output_dir, 'plugins', '%s' % module_category)
|
|
|
|
write_data(text, category_output_dir, outputname, module)
|
|
|
|
else:
|
|
|
|
write_data(text, output_dir, outputname, module)
|
2017-08-14 04:12:43 +02:00
|
|
|
|
|
|
|
|
2017-09-19 17:14:27 +02:00
|
|
|
def process_categories(mod_info, categories, templates,
|
|
|
|
output_dir, output_name, plugin_type):
|
2017-08-14 04:12:43 +02:00
|
|
|
for category in sorted(categories.keys()):
|
2017-09-19 17:14:27 +02:00
|
|
|
if (plugin_type, category) == ('plugins', ''):
|
|
|
|
print('skipping unknown cat: %s' % category)
|
|
|
|
continue
|
2017-08-14 04:12:43 +02:00
|
|
|
module_map = categories[category]
|
|
|
|
category_filename = output_name % category
|
|
|
|
|
|
|
|
print("*** recording category %s in %s ***" % (category, category_filename))
|
|
|
|
|
|
|
|
# start a new category file
|
|
|
|
|
2017-09-19 17:14:27 +02:00
|
|
|
category_name = category.replace("_", " ")
|
|
|
|
category_title = category_name.title()
|
2017-08-14 04:12:43 +02:00
|
|
|
|
|
|
|
subcategories = dict((k, v) for k, v in module_map.items() if k != '_modules')
|
2017-09-19 17:14:27 +02:00
|
|
|
template_data = {'title': category_title,
|
|
|
|
'category_name': category_name,
|
2017-08-14 04:12:43 +02:00
|
|
|
'category': module_map,
|
|
|
|
'subcategories': subcategories,
|
|
|
|
'module_info': mod_info,
|
2017-09-19 17:14:27 +02:00
|
|
|
'plugin_type': plugin_type
|
2017-08-14 04:12:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
text = templates['list_of_CATEGORY_modules'].render(template_data)
|
|
|
|
write_data(text, output_dir, category_filename)
|
|
|
|
|
|
|
|
|
2017-09-19 17:14:27 +02:00
|
|
|
def process_support_levels(mod_info, templates, output_dir, plugin_type):
|
2017-08-14 04:12:43 +02:00
|
|
|
supported_by = {'Ansible Core Team': {'slug': 'core_supported',
|
|
|
|
'modules': [],
|
2017-08-16 16:05:14 +02:00
|
|
|
'output': 'core_maintained.rst',
|
|
|
|
'blurb': "These are :doc:`modules maintained by the"
|
|
|
|
" Ansible Core Team<core_maintained>` and will always ship"
|
|
|
|
" with Ansible itself."},
|
2017-08-14 04:12:43 +02:00
|
|
|
'Ansible Network Team': {'slug': 'network_supported',
|
|
|
|
'modules': [],
|
2017-08-16 16:05:14 +02:00
|
|
|
'output': 'network_maintained.rst',
|
|
|
|
'blurb': "These are :doc:`modules maintained by the"
|
|
|
|
" Ansible Network Team<network_maintained>` in"
|
|
|
|
" a relationship similar to how the Ansible Core Team"
|
|
|
|
" maintains the Core modules."},
|
2017-08-14 04:12:43 +02:00
|
|
|
'Ansible Partners': {'slug': 'partner_supported',
|
|
|
|
'modules': [],
|
2017-08-16 16:05:14 +02:00
|
|
|
'output': 'partner_maintained.rst',
|
|
|
|
'blurb': """
|
|
|
|
Some examples of :doc:`Certified Modules<partner_maintained>` are those submitted by other
|
|
|
|
companies. Maintainers of these types of modules must watch for any issues reported or pull requests
|
|
|
|
raised against the module.
|
|
|
|
|
|
|
|
The Ansible Core Team will review all modules becoming certified. Core committers will review
|
|
|
|
proposed changes to existing Certified Modules once the community maintainers of the module have
|
|
|
|
approved the changes. Core committers will also ensure that any issues that arise due to Ansible
|
|
|
|
engine changes will be remediated. Also, it is strongly recommended (but not presently required)
|
|
|
|
for these types of modules to have unit tests.
|
|
|
|
|
|
|
|
These modules are currently shipped with Ansible, but might be shipped separately in the future.
|
|
|
|
"""},
|
2017-08-14 04:12:43 +02:00
|
|
|
'Ansible Community': {'slug': 'community_supported',
|
|
|
|
'modules': [],
|
2017-08-16 16:05:14 +02:00
|
|
|
'output': 'community_maintained.rst',
|
|
|
|
'blurb': """
|
|
|
|
These are :doc:`modules maintained by the Ansible Community<community_maintained>`. They **are
|
|
|
|
not** supported by the Ansible Core Team or by companies/partners associated to the module.
|
|
|
|
|
|
|
|
They are still fully usable, but the response rate to issues is purely up to the community. Best
|
|
|
|
effort support will be provided but is not covered under any support contracts.
|
|
|
|
|
|
|
|
These modules are currently shipped with Ansible, but will most likely be shipped separately in the future.
|
|
|
|
"""},
|
2017-08-14 04:12:43 +02:00
|
|
|
}
|
|
|
|
|
2017-09-19 17:14:27 +02:00
|
|
|
# only gen support pages for modules for now, need to split and namespace templates and generated docs
|
|
|
|
if plugin_type == 'plugins':
|
|
|
|
return
|
2017-08-14 04:12:43 +02:00
|
|
|
# Separate the modules by support_level
|
|
|
|
for module, info in mod_info.items():
|
2017-09-19 17:14:27 +02:00
|
|
|
if not info.get('metadata', None):
|
|
|
|
print('no metadata for %s' % module)
|
|
|
|
continue
|
2017-08-14 04:12:43 +02:00
|
|
|
if info['metadata']['supported_by'] == 'core':
|
|
|
|
supported_by['Ansible Core Team']['modules'].append(module)
|
|
|
|
elif info['metadata']['supported_by'] == 'network':
|
|
|
|
supported_by['Ansible Network Team']['modules'].append(module)
|
|
|
|
elif info['metadata']['supported_by'] == 'certified':
|
|
|
|
supported_by['Ansible Partners']['modules'].append(module)
|
|
|
|
elif info['metadata']['supported_by'] == 'community':
|
|
|
|
supported_by['Ansible Community']['modules'].append(module)
|
|
|
|
else:
|
|
|
|
raise AnsibleError('Unknown supported_by value: %s' % info['metadata']['supported_by'])
|
2013-12-25 18:35:41 +01:00
|
|
|
|
2017-08-14 04:12:43 +02:00
|
|
|
# Render the module lists
|
|
|
|
for maintainers, data in supported_by.items():
|
|
|
|
template_data = {'maintainers': maintainers,
|
|
|
|
'modules': data['modules'],
|
|
|
|
'slug': data['slug'],
|
|
|
|
'module_info': mod_info,
|
|
|
|
}
|
|
|
|
text = templates['support_list'].render(template_data)
|
|
|
|
write_data(text, output_dir, data['output'])
|
2013-12-25 18:35:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
def validate_options(options):
|
|
|
|
''' validate option parser options '''
|
|
|
|
|
|
|
|
if not options.module_dir:
|
2017-01-20 01:01:58 +01:00
|
|
|
sys.exit("--module-dir is required", file=sys.stderr)
|
2013-12-25 18:35:41 +01:00
|
|
|
if not os.path.exists(options.module_dir):
|
2017-01-20 01:01:58 +01:00
|
|
|
sys.exit("--module-dir does not exist: %s" % options.module_dir, file=sys.stderr)
|
2013-12-25 18:35:41 +01:00
|
|
|
if not options.template_dir:
|
2017-01-20 01:01:58 +01:00
|
|
|
sys.exit("--template-dir must be specified")
|
2013-04-28 21:03:45 +02:00
|
|
|
|
2012-09-26 20:41:44 +02:00
|
|
|
|
2013-12-25 18:35:41 +01:00
|
|
|
def main():
|
2012-09-26 20:41:44 +02:00
|
|
|
|
2013-12-25 18:35:41 +01:00
|
|
|
p = generate_parser()
|
2012-09-28 03:34:28 +02:00
|
|
|
|
2013-12-25 18:35:41 +01:00
|
|
|
(options, args) = p.parse_args()
|
|
|
|
validate_options(options)
|
2012-09-26 20:41:44 +02:00
|
|
|
|
2017-09-19 17:14:27 +02:00
|
|
|
plugin_type = options.plugin_type
|
|
|
|
|
|
|
|
if plugin_type == 'modules':
|
2017-09-23 05:19:50 +02:00
|
|
|
templates = jinja2_environment(options.template_dir, options.type, plugin_type)
|
|
|
|
output_dir = options.output_dir
|
|
|
|
# trim trailing s off of plugin_type for plugin_type=='modules'. ie 'copy_module.rst'
|
2017-09-19 17:14:27 +02:00
|
|
|
outputname = '%s_' + '%s.rst' % plugin_type[:-1]
|
2017-09-23 05:19:50 +02:00
|
|
|
else:
|
|
|
|
templates = jinja2_environment(options.template_dir, options.type, 'plugins')
|
|
|
|
# for plugins, just use 'ssh.rst' vs 'ssh_module.rst'
|
|
|
|
outputname = '%s.rst'
|
|
|
|
output_dir = '%s/plugins/%s' % (options.output_dir, plugin_type)
|
2012-10-09 22:04:55 +02:00
|
|
|
|
2017-08-04 22:10:36 +02:00
|
|
|
# Convert passed-in limit_to_modules to None or list of modules.
|
|
|
|
if options.limit_to_modules is not None:
|
|
|
|
options.limit_to_modules = [s.lower() for s in options.limit_to_modules.split(",")]
|
|
|
|
|
2017-09-23 05:19:50 +02:00
|
|
|
mod_info, categories = get_module_info(options.module_dir, limit_to_modules=options.limit_to_modules, verbose=options.verbose)
|
2017-08-14 04:12:43 +02:00
|
|
|
|
|
|
|
categories['all'] = {'_modules': mod_info.keys()}
|
|
|
|
|
2017-09-19 17:14:27 +02:00
|
|
|
# pprint.pprint(categories)
|
|
|
|
# pprint.pprint(mod_info)
|
|
|
|
# pprint.pprint(dict(mod_info))
|
2017-08-14 04:12:43 +02:00
|
|
|
# Transform the data
|
|
|
|
if options.type == 'rst':
|
2017-09-19 17:14:27 +02:00
|
|
|
for key, record in mod_info.items():
|
|
|
|
# pprint.pprint(('record', record))
|
|
|
|
if record.get('doc', None):
|
|
|
|
short_desc = record['doc']['short_description']
|
|
|
|
if short_desc is None:
|
|
|
|
print('WARNING: short_description for %s is None' % key)
|
|
|
|
short_desc = ''
|
|
|
|
record['doc']['short_description'] = rst_ify(short_desc)
|
2013-12-25 19:38:40 +01:00
|
|
|
|
2017-09-23 05:19:50 +02:00
|
|
|
if plugin_type == 'module':
|
|
|
|
# Write master category list
|
|
|
|
category_list_text = templates['category_list'].render(categories=sorted(categories.keys()))
|
|
|
|
category_index_name = '%s_by_category.rst' % plugin_type
|
|
|
|
write_data(category_list_text, output_dir, category_index_name)
|
2017-08-14 04:12:43 +02:00
|
|
|
|
|
|
|
# Render all the individual module pages
|
2017-09-23 05:19:50 +02:00
|
|
|
process_modules(mod_info, templates, outputname, output_dir, options.ansible_version, plugin_type)
|
2017-08-14 04:12:43 +02:00
|
|
|
|
|
|
|
# Render all the categories for modules
|
2017-09-23 05:19:50 +02:00
|
|
|
if plugin_type == 'module':
|
|
|
|
category_list_name_template = 'list_of_%s_' + '%s.rst' % plugin_type
|
|
|
|
process_categories(mod_info, categories, templates, output_dir, category_list_name_template, plugin_type)
|
2013-12-25 19:38:40 +01:00
|
|
|
|
2017-09-23 05:19:50 +02:00
|
|
|
# Render all the categories for modules
|
|
|
|
process_support_levels(mod_info, templates, output_dir, plugin_type)
|
2012-09-26 20:41:44 +02:00
|
|
|
|
2017-05-30 19:08:25 +02:00
|
|
|
|
2012-09-26 20:41:44 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|