2017-03-23 06:11:40 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from jinja2 import Environment, FileSystemLoader
|
|
|
|
|
2017-03-30 17:31:09 +02:00
|
|
|
from ansible.module_utils._text import to_bytes
|
|
|
|
|
2017-03-24 17:38:16 +01:00
|
|
|
|
2017-03-23 06:11:40 +01:00
|
|
|
def get_options(optlist):
|
|
|
|
''' get actual options '''
|
|
|
|
|
|
|
|
opts = []
|
|
|
|
for opt in optlist:
|
|
|
|
res = {
|
|
|
|
'desc': opt.help,
|
|
|
|
'options': opt._short_opts + opt._long_opts
|
|
|
|
}
|
|
|
|
if opt.action == 'store':
|
|
|
|
res['arg'] = opt.dest.upper()
|
|
|
|
opts.append(res)
|
|
|
|
|
|
|
|
return opts
|
|
|
|
|
2017-03-24 17:38:16 +01:00
|
|
|
|
2017-03-23 06:11:40 +01:00
|
|
|
def opt_doc_list(cli):
|
|
|
|
''' iterate over options lists '''
|
|
|
|
|
|
|
|
results = []
|
|
|
|
for optg in cli.parser.option_groups:
|
|
|
|
results.extend(get_options(optg.option_list))
|
|
|
|
|
|
|
|
results.extend(get_options(cli.parser.option_list))
|
|
|
|
|
|
|
|
return results
|
|
|
|
|
2017-03-24 17:38:16 +01:00
|
|
|
|
2017-03-23 06:11:40 +01:00
|
|
|
def opts_docs(cli, name):
|
|
|
|
''' generate doc structure from options '''
|
|
|
|
|
|
|
|
# cli name
|
|
|
|
if '-' in name:
|
|
|
|
name = name.split('-')[1]
|
|
|
|
else:
|
2017-03-24 17:38:16 +01:00
|
|
|
name = 'adhoc'
|
2017-03-23 06:11:40 +01:00
|
|
|
|
|
|
|
# cli info
|
|
|
|
docs = {
|
|
|
|
'cli': name,
|
|
|
|
'usage': cli.parser.usage,
|
|
|
|
'short_desc': cli.parser.description,
|
|
|
|
'long_desc': cli.__doc__,
|
|
|
|
}
|
|
|
|
|
|
|
|
# force populate parser with per action options
|
|
|
|
if cli.VALID_ACTIONS:
|
|
|
|
docs['actions'] = {}
|
|
|
|
# avoid dupe errors
|
|
|
|
cli.parser.set_conflict_handler('resolve')
|
|
|
|
for action in cli.VALID_ACTIONS:
|
|
|
|
cli.args.append(action)
|
|
|
|
cli.set_action()
|
|
|
|
docs['actions'][action] = getattr(cli, 'execute_%s' % action).__doc__
|
|
|
|
|
|
|
|
docs['options'] = opt_doc_list(cli)
|
|
|
|
|
|
|
|
return docs
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
|
|
template_file = 'man.j2'
|
|
|
|
|
|
|
|
# need to be in right dir
|
|
|
|
os.chdir(os.path.dirname(__file__))
|
|
|
|
|
|
|
|
allvars = {}
|
|
|
|
output = {}
|
|
|
|
cli_list = []
|
|
|
|
for binary in os.listdir('../../lib/ansible/cli'):
|
|
|
|
|
|
|
|
if not binary.endswith('.py'):
|
|
|
|
continue
|
|
|
|
elif binary == '__init__.py':
|
|
|
|
continue
|
|
|
|
|
|
|
|
libname = os.path.splitext(binary)[0]
|
|
|
|
print("Found CLI %s" % libname)
|
|
|
|
|
2017-03-24 17:38:16 +01:00
|
|
|
if libname == 'adhoc':
|
2017-03-23 06:11:40 +01:00
|
|
|
myclass = 'AdHocCLI'
|
|
|
|
output[libname] = 'ansible.1.asciidoc.in'
|
|
|
|
else:
|
|
|
|
myclass = "%sCLI" % libname.capitalize()
|
|
|
|
output[libname] = 'ansible-%s.1.asciidoc.in' % libname
|
|
|
|
|
2017-06-12 08:55:19 +02:00
|
|
|
# instantiate each cli and ask its options
|
2017-03-23 06:11:40 +01:00
|
|
|
mycli = getattr(__import__("ansible.cli.%s" % libname, fromlist=[myclass]), myclass)
|
|
|
|
cli_object = mycli([])
|
|
|
|
try:
|
|
|
|
cli_object.parse()
|
|
|
|
except:
|
|
|
|
# no options passed, we expect errors
|
|
|
|
pass
|
|
|
|
|
|
|
|
allvars[libname] = opts_docs(cli_object, libname)
|
|
|
|
|
|
|
|
for extras in ('ARGUMENTS'):
|
|
|
|
if hasattr(cli_object, extras):
|
|
|
|
allvars[libname][extras.lower()] = getattr(cli_object, extras)
|
|
|
|
|
|
|
|
cli_list = allvars.keys()
|
|
|
|
for libname in cli_list:
|
|
|
|
|
|
|
|
# template it!
|
|
|
|
env = Environment(loader=FileSystemLoader('../templates'))
|
|
|
|
template = env.get_template('man.j2')
|
|
|
|
|
|
|
|
# add rest to vars
|
|
|
|
tvars = allvars[libname]
|
|
|
|
tvars['cli_list'] = cli_list
|
|
|
|
tvars['cli'] = libname
|
|
|
|
if '-i' in tvars['options']:
|
|
|
|
print('uses inventory')
|
|
|
|
|
|
|
|
manpage = template.render(tvars)
|
|
|
|
filename = '../man/man1/%s' % output[libname]
|
2017-03-30 17:31:09 +02:00
|
|
|
with open(filename, 'wb') as f:
|
|
|
|
f.write(to_bytes(manpage))
|
2017-03-23 06:11:40 +01:00
|
|
|
print("Wrote man docs to %s" % filename)
|