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
|
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/>.
|
|
|
|
#
|
|
|
|
|
2015-08-31 02:35:14 +02:00
|
|
|
from __future__ import print_function
|
2016-09-07 07:54:17 +02:00
|
|
|
__metaclass__ = type
|
2016-04-12 02:11:55 +02:00
|
|
|
|
2012-09-26 20:41:44 +02:00
|
|
|
import os
|
2013-04-28 21:03:45 +02:00
|
|
|
import glob
|
2012-09-26 20:41:44 +02:00
|
|
|
import sys
|
|
|
|
import yaml
|
|
|
|
import re
|
2012-10-11 18:11:33 +02:00
|
|
|
import optparse
|
2012-09-26 20:41:44 +02:00
|
|
|
import datetime
|
2013-06-14 20:27:59 +02:00
|
|
|
import cgi
|
2015-07-17 16:00:02 +02:00
|
|
|
import warnings
|
2016-04-12 02:11:55 +02:00
|
|
|
from collections import defaultdict
|
|
|
|
|
2013-12-25 18:35:41 +01:00
|
|
|
from jinja2 import Environment, FileSystemLoader
|
2015-09-03 08:23:27 +02:00
|
|
|
from six import iteritems
|
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
|
|
|
|
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
|
|
|
|
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
|
2012-10-13 01:21:41 +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\(([^)]+)\)")
|
|
|
|
_BOLD = re.compile(r"B\(([^)]+)\)")
|
|
|
|
_MODULE = re.compile(r"M\(([^)]+)\)")
|
|
|
|
_URL = re.compile(r"U\(([^)]+)\)")
|
|
|
|
_CONST = re.compile(r"C\(([^)]+)\)")
|
|
|
|
|
2017-05-13 17:04:30 +02:00
|
|
|
DEPRECATED = b" (D)"
|
2013-12-25 18:35:41 +01: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-12-25 18:35:41 +01:00
|
|
|
#####################################################################################
|
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
|
|
|
|
2013-06-14 20:27:59 +02:00
|
|
|
t = cgi.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
|
|
|
|
|
2013-12-25 18:35:41 +01:00
|
|
|
#####################################################################################
|
2012-09-26 20:41:44 +02:00
|
|
|
|
2013-12-25 19:23:58 +01:00
|
|
|
def write_data(text, options, outputname, module):
|
2013-12-25 18:35:41 +01:00
|
|
|
''' dumps module output to a file or the screen, as requested '''
|
|
|
|
|
2012-10-11 18:11:33 +02:00
|
|
|
if options.output_dir is not None:
|
2014-09-26 23:10:13 +02:00
|
|
|
fname = os.path.join(options.output_dir, outputname % module)
|
|
|
|
fname = fname.replace(".py","")
|
2017-05-13 17:04:30 +02:00
|
|
|
f = open(fname, 'wb')
|
|
|
|
f.write(to_bytes(text))
|
2012-10-09 22:04:55 +02:00
|
|
|
f.close()
|
|
|
|
else:
|
2015-08-31 02:35:14 +02:00
|
|
|
print(text)
|
2012-10-09 22:04:55 +02:00
|
|
|
|
2013-12-25 18:35:41 +01:00
|
|
|
#####################################################################################
|
|
|
|
|
2014-09-26 23:10:13 +02:00
|
|
|
|
2014-11-04 04:02:13 +01:00
|
|
|
def list_modules(module_dir, depth=0):
|
2013-12-25 18:35:41 +01:00
|
|
|
''' returns a hash of categories, each category being a hash of module names to file paths '''
|
|
|
|
|
2016-04-12 02:11:55 +02:00
|
|
|
categories = dict()
|
|
|
|
module_info = dict()
|
|
|
|
aliases = defaultdict(set)
|
|
|
|
|
|
|
|
# * 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:
|
|
|
|
if module_path.endswith('__init__.py'):
|
|
|
|
continue
|
|
|
|
category = categories
|
2016-12-06 20:27:14 +01:00
|
|
|
mod_path_only = module_path
|
2016-04-12 02:11:55 +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):])
|
|
|
|
|
2016-04-12 02:11:55 +02:00
|
|
|
# directories (core, extras)
|
|
|
|
for new_cat in mod_path_only.split('/')[1:]:
|
|
|
|
if new_cat not in category:
|
|
|
|
category[new_cat] = dict()
|
|
|
|
category = category[new_cat]
|
|
|
|
|
|
|
|
module = os.path.splitext(os.path.basename(module_path))[0]
|
2017-03-18 02:07:39 +01:00
|
|
|
if module in plugin_docs.BLACKLIST['MODULE']:
|
2016-04-12 02:11:55 +02:00
|
|
|
# Do not list blacklisted modules
|
|
|
|
continue
|
|
|
|
if module.startswith("_") and os.path.islink(module_path):
|
|
|
|
source = os.path.splitext(os.path.basename(os.path.realpath(module_path)))[0]
|
|
|
|
module = module.replace("_","",1)
|
|
|
|
aliases[source].add(module)
|
|
|
|
continue
|
|
|
|
|
|
|
|
category[module] = module_path
|
|
|
|
module_info[module] = module_path
|
|
|
|
|
|
|
|
# keep module tests out of becoming module docs
|
2015-10-28 00:59:12 +01:00
|
|
|
if 'test' in categories:
|
|
|
|
del categories['test']
|
|
|
|
|
2016-04-12 02:11:55 +02:00
|
|
|
return module_info, categories, aliases
|
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")
|
|
|
|
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")
|
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
|
|
|
|
2013-12-25 18:35:41 +01:00
|
|
|
#####################################################################################
|
2012-09-26 20:41:44 +02:00
|
|
|
|
2013-12-25 18:35:41 +01:00
|
|
|
def jinja2_environment(template_dir, typ):
|
2012-10-13 01:21:41 +02:00
|
|
|
|
2013-12-25 18:35:41 +01:00
|
|
|
env = Environment(loader=FileSystemLoader(template_dir),
|
2012-09-28 09:59:43 +02:00
|
|
|
variable_start_string="@{",
|
|
|
|
variable_end_string="}@",
|
2012-09-30 15:06:18 +02:00
|
|
|
trim_blocks=True,
|
2012-11-03 23:52:59 +01:00
|
|
|
)
|
2012-09-28 09:59:43 +02:00
|
|
|
env.globals['xline'] = rst_xline
|
2012-09-28 03:06:31 +02:00
|
|
|
|
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-03-23 06:13:23 +01:00
|
|
|
template = env.get_template('plugin.rst.j2')
|
2013-12-25 19:23:58 +01:00
|
|
|
outputname = "%s_module.rst"
|
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
|
|
|
|
2013-12-25 18:35:41 +01:00
|
|
|
return env, template, outputname
|
2012-09-26 20:41:44 +02:00
|
|
|
|
2013-12-25 18:35:41 +01: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
|
|
|
|
return (added_float < TO_OLD_TO_BE_NOTABLE)
|
2013-04-28 21:03:45 +02:00
|
|
|
|
2014-11-04 04:02:13 +01:00
|
|
|
def process_module(module, options, env, template, outputname, module_map, aliases):
|
2013-12-25 18:35:41 +01:00
|
|
|
|
|
|
|
fname = module_map[module]
|
2014-11-04 04:02:13 +01:00
|
|
|
if isinstance(fname, dict):
|
|
|
|
return "SKIPPED"
|
|
|
|
|
2014-10-30 18:29:54 +01:00
|
|
|
basename = os.path.basename(fname)
|
|
|
|
deprecated = False
|
2013-12-25 18:35:41 +01:00
|
|
|
|
|
|
|
# ignore files with extensions
|
2014-10-30 18:29:54 +01:00
|
|
|
if not basename.endswith(".py"):
|
2013-12-25 18:35:41 +01:00
|
|
|
return
|
2014-10-31 19:20:26 +01:00
|
|
|
elif module.startswith("_"):
|
|
|
|
if os.path.islink(fname):
|
|
|
|
return # ignore, its an alias
|
2014-10-30 18:29:54 +01:00
|
|
|
deprecated = True
|
2014-10-31 19:20:26 +01:00
|
|
|
module = module.replace("_","",1)
|
|
|
|
|
2015-08-31 02:35:14 +02:00
|
|
|
print("rendering: %s" % module)
|
2013-12-25 18:35:41 +01:00
|
|
|
|
|
|
|
# use ansible core library to parse out doc metadata YAML and plaintext examples
|
2017-03-18 02:07:39 +01:00
|
|
|
doc, examples, returndocs, metadata = plugin_docs.get_docstring(fname, verbose=options.verbose)
|
2013-12-25 18:35:41 +01:00
|
|
|
|
|
|
|
# crash if module is missing documentation and not explicitly hidden from docs index
|
|
|
|
if doc is None:
|
2017-01-20 01:01:58 +01:00
|
|
|
sys.exit("*** ERROR: MODULE MISSING DOCUMENTATION: %s, %s ***\n" % (fname, module))
|
2014-10-31 19:20:26 +01:00
|
|
|
|
2016-12-07 23:50:17 +01:00
|
|
|
if metadata is None:
|
2017-01-20 01:01:58 +01:00
|
|
|
sys.exit("*** ERROR: MODULE MISSING METADATA: %s, %s ***\n" % (fname, module))
|
2016-12-07 23:50:17 +01:00
|
|
|
|
2014-10-31 19:20:26 +01:00
|
|
|
if deprecated and 'deprecated' not in doc:
|
2017-01-20 01:01:58 +01:00
|
|
|
sys.exit("*** ERROR: DEPRECATED MODULE MISSING 'deprecated' DOCUMENTATION: %s, %s ***\n" % (fname, module))
|
2013-12-25 18:35:41 +01:00
|
|
|
|
2014-11-04 04:02:13 +01:00
|
|
|
if module in aliases:
|
|
|
|
doc['aliases'] = aliases[module]
|
2014-09-26 23:52:50 +02:00
|
|
|
|
2013-12-25 18:35:41 +01:00
|
|
|
all_keys = []
|
2013-04-28 21:03:45 +02:00
|
|
|
|
2013-12-25 18:35:41 +01:00
|
|
|
if not 'version_added' in doc:
|
2017-01-20 01:01:58 +01:00
|
|
|
sys.exit("*** ERROR: missing version_added in: %s ***\n" % module)
|
2013-12-25 18:35:41 +01:00
|
|
|
|
|
|
|
added = 0
|
|
|
|
if doc['version_added'] == 'historical':
|
|
|
|
del doc['version_added']
|
|
|
|
else:
|
|
|
|
added = doc['version_added']
|
|
|
|
|
|
|
|
# don't show version added information if it's too old to be called out
|
2015-07-17 16:00:02 +02:00
|
|
|
if too_old(added):
|
|
|
|
del doc['version_added']
|
2013-12-25 18:35:41 +01:00
|
|
|
|
2015-07-28 20:29:43 +02:00
|
|
|
if 'options' in doc and doc['options']:
|
2015-09-03 08:23:27 +02:00
|
|
|
for (k,v) in iteritems(doc['options']):
|
2015-07-17 16:00:02 +02:00
|
|
|
# don't show version added information if it's too old to be called out
|
|
|
|
if 'version_added' in doc['options'][k] and too_old(doc['options'][k]['version_added']):
|
|
|
|
del doc['options'][k]['version_added']
|
2016-02-08 18:33:54 +01:00
|
|
|
if not 'description' in doc['options'][k]:
|
|
|
|
raise AnsibleError("Missing required description for option %s in %s " % (k, module))
|
2016-05-19 01:57:36 +02:00
|
|
|
|
|
|
|
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))
|
2016-02-08 18:33:54 +01:00
|
|
|
if not isinstance(doc['options'][k]['description'],list):
|
|
|
|
doc['options'][k]['description'] = [doc['options'][k]['description']]
|
|
|
|
|
2015-05-23 15:42:17 +02:00
|
|
|
all_keys.append(k)
|
2014-10-31 19:20:26 +01:00
|
|
|
|
2013-12-25 18:35:41 +01:00
|
|
|
all_keys = sorted(all_keys)
|
|
|
|
|
2014-10-31 19:20:26 +01:00
|
|
|
doc['option_keys'] = all_keys
|
2013-12-25 18:35:41 +01:00
|
|
|
doc['filename'] = fname
|
|
|
|
doc['docuri'] = doc['module'].replace('_', '-')
|
|
|
|
doc['now_date'] = datetime.date.today().strftime('%Y-%m-%d')
|
|
|
|
doc['ansible_version'] = options.ansible_version
|
|
|
|
doc['plainexamples'] = examples #plain text
|
2016-12-07 23:50:17 +01:00
|
|
|
doc['metadata'] = metadata
|
|
|
|
|
2015-03-20 21:54:22 +01:00
|
|
|
if returndocs:
|
2015-10-12 16:19:48 +02:00
|
|
|
try:
|
|
|
|
doc['returndocs'] = yaml.safe_load(returndocs)
|
|
|
|
except:
|
|
|
|
print("could not load yaml: %s" % returndocs)
|
|
|
|
raise
|
2015-03-20 21:54:22 +01:00
|
|
|
else:
|
|
|
|
doc['returndocs'] = None
|
2013-12-25 18:35:41 +01:00
|
|
|
|
|
|
|
# here is where we build the table of contents...
|
|
|
|
|
2015-07-28 20:29:43 +02:00
|
|
|
try:
|
|
|
|
text = template.render(doc)
|
|
|
|
except Exception as e:
|
|
|
|
raise AnsibleError("Failed to render doc for %s: %s" % (fname, str(e)))
|
2013-12-25 19:23:58 +01:00
|
|
|
write_data(text, options, outputname, module)
|
2014-10-31 19:20:26 +01:00
|
|
|
return doc['short_description']
|
2013-04-28 21:03:45 +02:00
|
|
|
|
2013-12-25 18:35:41 +01:00
|
|
|
#####################################################################################
|
2013-08-06 16:53:56 +02:00
|
|
|
|
2016-12-07 23:50:17 +01:00
|
|
|
def print_modules(module, category_file, deprecated, options, env, template, outputname, module_map, aliases):
|
2014-11-04 04:02:13 +01:00
|
|
|
modstring = module
|
2016-04-12 02:11:55 +02:00
|
|
|
if modstring.startswith('_'):
|
|
|
|
modstring = module[1:]
|
|
|
|
modname = modstring
|
2014-11-04 04:02:13 +01:00
|
|
|
if module in deprecated:
|
2017-05-13 17:04:30 +02:00
|
|
|
modstring = to_bytes(modstring) + DEPRECATED
|
2014-11-04 04:02:13 +01:00
|
|
|
|
2017-05-13 17:04:30 +02:00
|
|
|
category_file.write(b" %s - %s <%s_module>\n" % (to_bytes(modstring), to_bytes(rst_ify(module_map[module][1])), to_bytes(modname)))
|
2014-11-04 04:02:13 +01:00
|
|
|
|
2013-12-25 18:35:41 +01:00
|
|
|
def process_category(category, categories, options, env, template, outputname):
|
2013-08-06 16:53:56 +02:00
|
|
|
|
2016-04-12 02:11:55 +02:00
|
|
|
### FIXME:
|
|
|
|
# We no longer conceptually deal with a mapping of category names to
|
|
|
|
# modules to file paths. Instead we want several different records:
|
|
|
|
# (1) Mapping of module names to file paths (what's presently used
|
|
|
|
# as categories['all']
|
|
|
|
# (2) Mapping of category names to lists of module names (what you'd
|
|
|
|
# presently get from categories[category_name][subcategory_name].keys()
|
|
|
|
# (3) aliases (what's presently in categories['_aliases']
|
|
|
|
#
|
|
|
|
# list_modules() now returns those. Need to refactor this function and
|
|
|
|
# main to work with them.
|
|
|
|
|
2013-12-25 18:35:41 +01:00
|
|
|
module_map = categories[category]
|
2016-04-12 02:11:55 +02:00
|
|
|
module_info = categories['all']
|
2013-04-28 21:03:45 +02:00
|
|
|
|
2014-11-04 04:02:13 +01:00
|
|
|
aliases = {}
|
|
|
|
if '_aliases' in categories:
|
|
|
|
aliases = categories['_aliases']
|
|
|
|
|
2013-12-25 19:23:58 +01:00
|
|
|
category_file_path = os.path.join(options.output_dir, "list_of_%s_modules.rst" % category)
|
2017-05-13 17:04:30 +02:00
|
|
|
category_file = open(category_file_path, "wb")
|
2015-08-31 02:35:14 +02:00
|
|
|
print("*** recording category %s in %s ***" % (category, category_file_path))
|
2013-12-25 19:23:58 +01:00
|
|
|
|
2015-07-17 16:00:02 +02:00
|
|
|
# start a new category file
|
2013-04-28 21:03:45 +02:00
|
|
|
|
2013-12-25 18:35:41 +01:00
|
|
|
category = category.replace("_"," ")
|
|
|
|
category = category.title()
|
|
|
|
|
2014-10-31 19:20:26 +01:00
|
|
|
modules = []
|
|
|
|
deprecated = []
|
|
|
|
for module in module_map.keys():
|
2014-11-04 05:14:22 +01:00
|
|
|
if isinstance(module_map[module], dict):
|
2016-04-12 02:11:55 +02:00
|
|
|
for mod in (m for m in module_map[module].keys() if m in module_info):
|
2014-11-04 05:14:22 +01:00
|
|
|
if mod.startswith("_"):
|
|
|
|
deprecated.append(mod)
|
|
|
|
else:
|
2016-04-12 02:11:55 +02:00
|
|
|
if module not in module_info:
|
|
|
|
continue
|
2014-11-04 05:14:22 +01:00
|
|
|
if module.startswith("_"):
|
|
|
|
deprecated.append(module)
|
2014-10-31 19:20:26 +01:00
|
|
|
modules.append(module)
|
|
|
|
|
2016-04-12 02:11:55 +02:00
|
|
|
modules.sort(key=lambda k: k[1:] if k.startswith('_') else k)
|
2013-12-25 18:35:41 +01:00
|
|
|
|
2017-05-13 17:04:30 +02:00
|
|
|
category_header = b"%s Modules" % (to_bytes(category.title()))
|
|
|
|
underscores = b"`" * len(category_header)
|
2013-12-25 19:23:58 +01:00
|
|
|
|
2017-05-13 17:04:30 +02:00
|
|
|
category_file.write(b"""\
|
2013-12-25 20:05:01 +01:00
|
|
|
%s
|
|
|
|
%s
|
|
|
|
|
2014-10-31 19:20:26 +01:00
|
|
|
.. toctree:: :maxdepth: 1
|
2013-12-25 20:05:01 +01:00
|
|
|
|
|
|
|
""" % (category_header, underscores))
|
2014-11-04 04:02:13 +01:00
|
|
|
sections = []
|
2013-12-25 18:35:41 +01:00
|
|
|
for module in modules:
|
2014-11-04 04:02:13 +01:00
|
|
|
if module in module_map and isinstance(module_map[module], dict):
|
|
|
|
sections.append(module)
|
|
|
|
continue
|
|
|
|
else:
|
2016-12-07 23:50:17 +01:00
|
|
|
print_modules(module, category_file, deprecated, options, env, template, outputname, module_info, aliases)
|
2013-12-25 19:23:58 +01:00
|
|
|
|
2014-11-04 05:14:22 +01:00
|
|
|
sections.sort()
|
2014-11-04 04:02:13 +01:00
|
|
|
for section in sections:
|
2017-05-13 17:04:30 +02:00
|
|
|
category_file.write(b"\n%s\n%s\n\n" % (to_bytes(section.replace("_"," ").title()), b'-' * len(section)))
|
|
|
|
category_file.write(b".. toctree:: :maxdepth: 1\n\n")
|
2014-10-31 19:20:26 +01:00
|
|
|
|
2017-05-13 17:04:30 +02:00
|
|
|
section_modules = list(module_map[section].keys())
|
2016-04-12 02:11:55 +02:00
|
|
|
section_modules.sort(key=lambda k: k[1:] if k.startswith('_') else k)
|
2014-11-04 05:14:22 +01:00
|
|
|
#for module in module_map[section]:
|
2016-04-12 02:11:55 +02:00
|
|
|
for module in (m for m in section_modules if m in module_info):
|
2016-12-07 23:50:17 +01:00
|
|
|
print_modules(module, category_file, deprecated, options, env, template, outputname, module_info, aliases)
|
2013-12-25 19:23:58 +01:00
|
|
|
|
2017-05-13 17:04:30 +02:00
|
|
|
category_file.write(b"""\n\n
|
2014-10-31 20:06:00 +01:00
|
|
|
.. note::
|
2017-02-07 16:49:55 +01:00
|
|
|
- %s: This marks a module as deprecated, which means a module is kept for backwards compatibility but usage is discouraged.
|
|
|
|
The module documentation details page may explain more about this rationale.
|
2016-12-07 23:50:17 +01:00
|
|
|
""" % DEPRECATED)
|
2013-12-25 19:23:58 +01:00
|
|
|
category_file.close()
|
2013-12-25 18:35:41 +01:00
|
|
|
|
|
|
|
# TODO: end a new category file
|
|
|
|
|
|
|
|
#####################################################################################
|
|
|
|
|
|
|
|
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
|
|
|
|
2013-12-25 18:35:41 +01: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
|
|
|
|
2013-12-25 18:35:41 +01:00
|
|
|
env, template, outputname = jinja2_environment(options.template_dir, options.type)
|
2012-10-09 22:04:55 +02:00
|
|
|
|
2016-04-12 02:11:55 +02:00
|
|
|
mod_info, categories, aliases = list_modules(options.module_dir)
|
|
|
|
categories['all'] = mod_info
|
|
|
|
categories['_aliases'] = aliases
|
|
|
|
category_names = [c for c in categories.keys() if not c.startswith('_')]
|
2013-12-25 18:35:41 +01:00
|
|
|
category_names.sort()
|
2013-12-25 19:38:40 +01:00
|
|
|
|
2016-04-12 02:11:55 +02:00
|
|
|
# Write master category list
|
2013-12-25 19:23:58 +01:00
|
|
|
category_list_path = os.path.join(options.output_dir, "modules_by_category.rst")
|
2017-05-13 17:04:30 +02:00
|
|
|
with open(category_list_path, "wb") as category_list_file:
|
|
|
|
category_list_file.write(b"Module Index\n")
|
|
|
|
category_list_file.write(b"============\n")
|
|
|
|
category_list_file.write(b"\n\n")
|
|
|
|
category_list_file.write(b".. toctree::\n")
|
|
|
|
category_list_file.write(b" :maxdepth: 1\n\n")
|
2016-04-12 02:11:55 +02:00
|
|
|
|
|
|
|
for category in category_names:
|
2017-05-13 17:04:30 +02:00
|
|
|
category_list_file.write(b" list_of_%s_modules\n" % to_bytes(category))
|
2016-04-12 02:11:55 +02:00
|
|
|
|
|
|
|
#
|
|
|
|
# Import all the docs into memory
|
|
|
|
#
|
|
|
|
module_map = mod_info.copy()
|
|
|
|
|
|
|
|
for modname in module_map:
|
|
|
|
result = process_module(modname, options, env, template, outputname, module_map, aliases)
|
|
|
|
if result == 'SKIPPED':
|
|
|
|
del categories['all'][modname]
|
|
|
|
else:
|
|
|
|
categories['all'][modname] = (categories['all'][modname], result)
|
2013-12-25 19:38:40 +01:00
|
|
|
|
2016-04-12 02:11:55 +02:00
|
|
|
#
|
|
|
|
# Render all the docs to rst via category pages
|
|
|
|
#
|
2013-12-25 18:35:41 +01:00
|
|
|
for category in category_names:
|
|
|
|
process_category(category, categories, options, env, template, outputname)
|
2012-09-26 20:41:44 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|