2012-11-28 09:39:26 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
# (c) 2012, Jan-Piet Mens <jpmens () gmail.com>
|
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
#
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import textwrap
|
|
|
|
import re
|
|
|
|
import optparse
|
|
|
|
import datetime
|
2013-10-11 19:21:29 +02:00
|
|
|
import subprocess
|
2012-11-28 09:39:26 +01:00
|
|
|
from ansible import utils
|
|
|
|
from ansible.utils import module_docs
|
|
|
|
import ansible.constants as C
|
2012-12-03 09:01:04 +01:00
|
|
|
from ansible.utils import version
|
2013-01-26 18:34:30 +01:00
|
|
|
import traceback
|
2012-11-28 09:39:26 +01:00
|
|
|
|
|
|
|
MODULEDIR = C.DEFAULT_MODULE_PATH
|
|
|
|
|
2014-10-26 15:46:08 +01:00
|
|
|
BLACKLIST_EXTS = ('.pyc', '.swp', '.bak', '~', '.rpm')
|
2012-12-03 09:01:04 +01:00
|
|
|
|
2012-11-28 09:39:26 +01: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\(([^)]+)\)")
|
2013-10-11 19:21:29 +02:00
|
|
|
PAGER = 'less'
|
|
|
|
LESS_OPTS = 'FRSX' # -F (quit-if-one-screen) -R (allow raw ansi control chars)
|
|
|
|
# -S (chop long lines) -X (disable termcap init and de-init)
|
|
|
|
|
|
|
|
def pager_print(text):
|
|
|
|
''' just print text '''
|
|
|
|
print text
|
|
|
|
|
|
|
|
def pager_pipe(text, cmd):
|
|
|
|
''' pipe text through a pager '''
|
|
|
|
if 'LESS' not in os.environ:
|
|
|
|
os.environ['LESS'] = LESS_OPTS
|
|
|
|
try:
|
|
|
|
cmd = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=sys.stdout)
|
|
|
|
cmd.communicate(input=text)
|
|
|
|
except IOError:
|
|
|
|
pass
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def pager(text):
|
|
|
|
''' find reasonable way to display text '''
|
|
|
|
# this is a much simpler form of what is in pydoc.py
|
|
|
|
if not sys.stdout.isatty():
|
|
|
|
pager_print(text)
|
|
|
|
elif 'PAGER' in os.environ:
|
|
|
|
if sys.platform == 'win32':
|
|
|
|
pager_print(text)
|
|
|
|
else:
|
|
|
|
pager_pipe(text, os.environ['PAGER'])
|
|
|
|
elif hasattr(os, 'system') and os.system('(less) 2> /dev/null') == 0:
|
|
|
|
pager_pipe(text, 'less')
|
|
|
|
else:
|
|
|
|
pager_print(text)
|
2012-11-28 09:39:26 +01:00
|
|
|
|
|
|
|
def tty_ify(text):
|
|
|
|
|
|
|
|
t = _ITALIC.sub("`" + r"\1" + "'", text) # I(word) => `word'
|
|
|
|
t = _BOLD.sub("*" + r"\1" + "*", t) # B(word) => *word*
|
|
|
|
t = _MODULE.sub("[" + r"\1" + "]", t) # M(word) => [word]
|
|
|
|
t = _URL.sub(r"\1", t) # U(word) => word
|
|
|
|
t = _CONST.sub("`" + r"\1" + "'", t) # C(word) => `word'
|
|
|
|
|
|
|
|
return t
|
|
|
|
|
2013-10-11 19:21:29 +02:00
|
|
|
def get_man_text(doc):
|
2012-11-28 09:39:26 +01:00
|
|
|
|
|
|
|
opt_indent=" "
|
2013-10-11 19:21:29 +02:00
|
|
|
text = []
|
|
|
|
text.append("> %s\n" % doc['module'].upper())
|
2012-11-28 09:39:26 +01:00
|
|
|
|
2014-05-20 21:41:22 +02:00
|
|
|
desc = " ".join(doc['description'])
|
2012-11-28 09:39:26 +01:00
|
|
|
|
2013-10-11 19:21:29 +02:00
|
|
|
text.append("%s\n" % textwrap.fill(tty_ify(desc), initial_indent=" ", subsequent_indent=" "))
|
2012-11-28 09:39:26 +01:00
|
|
|
|
2012-12-03 09:01:04 +01:00
|
|
|
if 'option_keys' in doc and len(doc['option_keys']) > 0:
|
2013-10-11 19:21:29 +02:00
|
|
|
text.append("Options (= is mandatory):\n")
|
2012-11-28 09:39:26 +01:00
|
|
|
|
2014-02-14 06:11:21 +01:00
|
|
|
for o in sorted(doc['option_keys']):
|
2012-11-28 09:39:26 +01:00
|
|
|
opt = doc['options'][o]
|
|
|
|
|
|
|
|
if opt.get('required', False):
|
|
|
|
opt_leadin = "="
|
|
|
|
else:
|
|
|
|
opt_leadin = "-"
|
|
|
|
|
2013-10-11 19:21:29 +02:00
|
|
|
text.append("%s %s" % (opt_leadin, o))
|
2013-01-26 18:58:12 +01:00
|
|
|
|
2014-05-20 21:41:22 +02:00
|
|
|
desc = " ".join(opt['description'])
|
2012-11-28 09:39:26 +01:00
|
|
|
|
|
|
|
if 'choices' in opt:
|
2012-12-03 09:01:04 +01:00
|
|
|
choices = ", ".join(str(i) for i in opt['choices'])
|
2012-11-28 09:39:26 +01:00
|
|
|
desc = desc + " (Choices: " + choices + ")"
|
2014-07-01 19:05:18 +02:00
|
|
|
if 'default' in opt:
|
|
|
|
default = str(opt['default'])
|
|
|
|
desc = desc + " [Default: " + default + "]"
|
2013-10-11 19:21:29 +02:00
|
|
|
text.append("%s\n" % textwrap.fill(tty_ify(desc), initial_indent=opt_indent,
|
|
|
|
subsequent_indent=opt_indent))
|
2012-11-28 09:39:26 +01:00
|
|
|
|
2012-12-11 08:26:12 +01:00
|
|
|
if 'notes' in doc and len(doc['notes']) > 0:
|
2014-05-20 21:41:22 +02:00
|
|
|
notes = " ".join(doc['notes'])
|
2013-10-11 19:21:29 +02:00
|
|
|
text.append("Notes:%s\n" % textwrap.fill(tty_ify(notes), initial_indent=" ",
|
|
|
|
subsequent_indent=opt_indent))
|
2012-11-28 09:39:26 +01:00
|
|
|
|
|
|
|
|
2013-05-16 17:13:18 +02:00
|
|
|
if 'requirements' in doc and doc['requirements'] is not None and len(doc['requirements']) > 0:
|
2013-05-11 16:31:47 +02:00
|
|
|
req = ", ".join(doc['requirements'])
|
2013-10-11 19:21:29 +02:00
|
|
|
text.append("Requirements:%s\n" % textwrap.fill(tty_ify(req), initial_indent=" ",
|
|
|
|
subsequent_indent=opt_indent))
|
2013-05-11 16:31:47 +02:00
|
|
|
|
2012-12-14 11:12:26 +01:00
|
|
|
if 'examples' in doc and len(doc['examples']) > 0:
|
2013-10-11 19:21:29 +02:00
|
|
|
text.append("Example%s:\n" % ('' if len(doc['examples']) < 2 else 's'))
|
2012-12-14 11:12:26 +01:00
|
|
|
for ex in doc['examples']:
|
2013-10-11 19:21:29 +02:00
|
|
|
text.append("%s\n" % (ex['code']))
|
2012-11-28 09:39:26 +01:00
|
|
|
|
2013-02-18 15:31:38 +01:00
|
|
|
if 'plainexamples' in doc and doc['plainexamples'] is not None:
|
2013-10-11 19:21:29 +02:00
|
|
|
text.append(doc['plainexamples'])
|
|
|
|
text.append('')
|
|
|
|
|
|
|
|
return "\n".join(text)
|
2013-02-18 15:31:38 +01:00
|
|
|
|
2013-05-11 16:31:47 +02:00
|
|
|
|
2013-10-11 19:21:29 +02:00
|
|
|
def get_snippet_text(doc):
|
2012-11-28 09:39:26 +01:00
|
|
|
|
2013-10-11 19:21:29 +02:00
|
|
|
text = []
|
2014-05-20 21:41:22 +02:00
|
|
|
desc = tty_ify(" ".join(doc['short_description']))
|
2013-10-11 19:21:29 +02:00
|
|
|
text.append("- name: %s" % (desc))
|
|
|
|
text.append(" action: %s" % (doc['module']))
|
2012-11-28 09:39:26 +01:00
|
|
|
|
2014-02-14 06:11:21 +01:00
|
|
|
for o in sorted(doc['options'].keys()):
|
2012-11-28 09:39:26 +01:00
|
|
|
opt = doc['options'][o]
|
2014-05-20 21:41:22 +02:00
|
|
|
desc = tty_ify(" ".join(opt['description']))
|
2014-02-14 06:12:29 +01:00
|
|
|
|
|
|
|
if opt.get('required', False):
|
|
|
|
s = o + "="
|
|
|
|
else:
|
|
|
|
s = o
|
|
|
|
|
2013-10-11 19:21:29 +02:00
|
|
|
text.append(" %-20s # %s" % (s, desc))
|
|
|
|
text.append('')
|
|
|
|
|
|
|
|
return "\n".join(text)
|
|
|
|
|
|
|
|
def get_module_list_text(module_list):
|
2014-10-30 03:33:31 +01:00
|
|
|
columns = max(60, int(os.popen('stty size', 'r').read().split()[1]))
|
|
|
|
displace = max(len(x) for x in module_list)
|
|
|
|
linelimit = columns - displace - 5
|
2013-10-11 19:21:29 +02:00
|
|
|
text = []
|
2014-10-30 03:33:31 +01:00
|
|
|
deprecated = []
|
2013-10-11 19:21:29 +02:00
|
|
|
for module in sorted(set(module_list)):
|
|
|
|
|
|
|
|
if module in module_docs.BLACKLIST_MODULES:
|
|
|
|
continue
|
|
|
|
|
|
|
|
filename = utils.plugins.module_finder.find_plugin(module)
|
2013-11-14 02:32:20 +01:00
|
|
|
|
|
|
|
if filename is None:
|
|
|
|
continue
|
2014-07-28 18:13:30 +02:00
|
|
|
if filename.endswith(".ps1"):
|
|
|
|
continue
|
2013-10-11 19:21:29 +02:00
|
|
|
if os.path.isdir(filename):
|
|
|
|
continue
|
2013-11-14 02:32:20 +01:00
|
|
|
|
2013-10-11 19:21:29 +02:00
|
|
|
try:
|
|
|
|
doc, plainexamples = module_docs.get_docstring(filename)
|
2014-10-30 03:33:31 +01:00
|
|
|
desc = tty_ify(doc.get('short_description', '?')).strip()
|
|
|
|
if len(desc) > linelimit:
|
|
|
|
desc = desc[:linelimit] + '...'
|
|
|
|
|
2014-10-30 18:50:34 +01:00
|
|
|
if module.startswith('_'): # Handle deprecated
|
2014-10-30 18:53:05 +01:00
|
|
|
deprecated.append("%-*s %-*.*s" % (displace, module[1:], linelimit, len(desc), desc))
|
2014-10-30 03:33:31 +01:00
|
|
|
else:
|
|
|
|
text.append("%-*s %-*.*s" % (displace, module, linelimit, len(desc), desc))
|
2013-10-11 19:21:29 +02:00
|
|
|
except:
|
|
|
|
traceback.print_exc()
|
|
|
|
sys.stderr.write("ERROR: module %s has a documentation error formatting or is missing documentation\n" % module)
|
2014-10-30 03:33:31 +01:00
|
|
|
|
|
|
|
if len(deprecated) > 0:
|
|
|
|
text.append("\nDEPRECATED:")
|
|
|
|
text.extend(deprecated)
|
2013-10-11 19:21:29 +02:00
|
|
|
return "\n".join(text)
|
2012-11-28 09:39:26 +01:00
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
|
|
p = optparse.OptionParser(
|
2012-12-03 09:01:04 +01:00
|
|
|
version=version("%prog"),
|
2012-11-28 09:39:26 +01:00
|
|
|
usage='usage: %prog [options] [module...]',
|
|
|
|
description='Show Ansible module documentation',
|
|
|
|
)
|
|
|
|
|
|
|
|
p.add_option("-M", "--module-path",
|
|
|
|
action="store",
|
|
|
|
dest="module_path",
|
|
|
|
default=MODULEDIR,
|
|
|
|
help="Ansible modules/ directory")
|
|
|
|
p.add_option("-l", "--list",
|
|
|
|
action="store_true",
|
|
|
|
default=False,
|
|
|
|
dest='list_dir',
|
|
|
|
help='List available modules')
|
|
|
|
p.add_option("-s", "--snippet",
|
|
|
|
action="store_true",
|
|
|
|
default=False,
|
|
|
|
dest='show_snippet',
|
|
|
|
help='Show playbook snippet for specified module(s)')
|
|
|
|
p.add_option('-v', action='version', help='Show version number and exit')
|
|
|
|
|
|
|
|
(options, args) = p.parse_args()
|
|
|
|
|
|
|
|
if options.module_path is not None:
|
2012-12-03 09:01:04 +01:00
|
|
|
for i in options.module_path.split(os.pathsep):
|
|
|
|
utils.plugins.module_finder.add_directory(i)
|
2012-11-28 09:39:26 +01:00
|
|
|
|
2014-10-30 18:40:34 +01:00
|
|
|
if options.list_dir:
|
2014-10-30 03:33:31 +01:00
|
|
|
# list modules
|
2012-11-28 09:39:26 +01:00
|
|
|
paths = utils.plugins.module_finder._get_paths()
|
|
|
|
module_list = []
|
|
|
|
for path in paths:
|
|
|
|
if os.path.isdir(path):
|
|
|
|
for module in os.listdir(path):
|
2014-10-30 03:33:31 +01:00
|
|
|
if any(module.endswith(x) for x in BLACKLIST_EXTS):
|
2012-12-03 09:01:04 +01:00
|
|
|
continue
|
2014-10-30 03:33:31 +01:00
|
|
|
elif module.startswith('__'):
|
|
|
|
continue
|
|
|
|
elif module.startswith('_'):
|
|
|
|
fullpath = '/'.join([path,module])
|
|
|
|
if os.path.islink(fullpath): # avoids aliases
|
|
|
|
continue
|
2012-11-28 09:39:26 +01:00
|
|
|
|
2014-10-30 03:33:31 +01:00
|
|
|
module = os.path.splitext(module)[0] # removes the extension
|
|
|
|
module_list.append(module)
|
|
|
|
|
2013-10-11 19:21:29 +02:00
|
|
|
pager(get_module_list_text(module_list))
|
2012-11-28 09:39:26 +01:00
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
if len(args) == 0:
|
|
|
|
p.print_help()
|
2013-04-20 18:31:14 +02:00
|
|
|
|
|
|
|
def print_paths(finder):
|
|
|
|
''' Returns a string suitable for printing of the search path '''
|
|
|
|
|
|
|
|
# Uses a list to get the order right
|
|
|
|
ret = []
|
|
|
|
for i in finder._get_paths():
|
|
|
|
if i not in ret:
|
|
|
|
ret.append(i)
|
|
|
|
return os.pathsep.join(ret)
|
|
|
|
|
2013-10-11 19:21:29 +02:00
|
|
|
text = ''
|
2012-11-28 09:39:26 +01:00
|
|
|
for module in args:
|
|
|
|
|
|
|
|
filename = utils.plugins.module_finder.find_plugin(module)
|
|
|
|
if filename is None:
|
|
|
|
sys.stderr.write("module %s not found in %s\n" % (module,
|
2013-04-20 18:31:14 +02:00
|
|
|
print_paths(utils.plugins.module_finder)))
|
2012-11-28 09:39:26 +01:00
|
|
|
continue
|
|
|
|
|
2012-12-03 09:01:04 +01:00
|
|
|
if any(filename.endswith(x) for x in BLACKLIST_EXTS):
|
2012-11-28 09:39:26 +01:00
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
2013-02-18 15:31:38 +01:00
|
|
|
doc, plainexamples = module_docs.get_docstring(filename)
|
2012-11-28 09:39:26 +01:00
|
|
|
except:
|
2013-01-26 18:34:30 +01:00
|
|
|
traceback.print_exc()
|
|
|
|
sys.stderr.write("ERROR: module %s has a documentation error formatting or is missing documentation\n" % module)
|
2012-11-28 09:39:26 +01:00
|
|
|
continue
|
|
|
|
|
2013-01-26 18:34:30 +01:00
|
|
|
if doc is not None:
|
2012-11-28 09:39:26 +01:00
|
|
|
|
|
|
|
all_keys = []
|
|
|
|
for (k,v) in doc['options'].iteritems():
|
|
|
|
all_keys.append(k)
|
|
|
|
all_keys = sorted(all_keys)
|
|
|
|
doc['option_keys'] = all_keys
|
|
|
|
|
|
|
|
doc['filename'] = filename
|
|
|
|
doc['docuri'] = doc['module'].replace('_', '-')
|
|
|
|
doc['now_date'] = datetime.date.today().strftime('%Y-%m-%d')
|
2013-02-18 15:31:38 +01:00
|
|
|
doc['plainexamples'] = plainexamples
|
2012-11-28 09:39:26 +01:00
|
|
|
|
|
|
|
if options.show_snippet:
|
2013-10-11 19:21:29 +02:00
|
|
|
text += get_snippet_text(doc)
|
2012-11-28 09:39:26 +01:00
|
|
|
else:
|
2013-10-11 19:21:29 +02:00
|
|
|
text += get_man_text(doc)
|
2012-11-28 09:39:26 +01:00
|
|
|
else:
|
2013-02-09 20:29:31 +01:00
|
|
|
# this typically means we couldn't even parse the docstring, not just that the YAML is busted,
|
|
|
|
# probably a quoting issue.
|
|
|
|
sys.stderr.write("ERROR: module %s missing documentation (or could not parse documentation)\n" % module)
|
2013-10-11 19:21:29 +02:00
|
|
|
pager(text)
|
2012-11-28 09:39:26 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|