mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Moving now from getopt to optparse
Let me know if we can move to another library instead ;-)
This commit is contained in:
parent
d6b3f40cb7
commit
2786149bdc
1 changed files with 86 additions and 97 deletions
|
@ -25,7 +25,7 @@ import json
|
||||||
import ast
|
import ast
|
||||||
from jinja2 import Environment, FileSystemLoader
|
from jinja2 import Environment, FileSystemLoader
|
||||||
import re
|
import re
|
||||||
import getopt
|
import optparse
|
||||||
import time
|
import time
|
||||||
import datetime
|
import datetime
|
||||||
import subprocess
|
import subprocess
|
||||||
|
@ -169,9 +169,9 @@ def get_docstring(filename, verbose=False):
|
||||||
return doc
|
return doc
|
||||||
|
|
||||||
|
|
||||||
def return_data(text, args, outputname, module):
|
def return_data(text, options, outputname, module):
|
||||||
if args.output_dir is not None:
|
if options.output_dir is not None:
|
||||||
f = open(os.path.join(args.output_dir, outputname % module), 'w')
|
f = open(os.path.join(options.output_dir, outputname % module), 'w')
|
||||||
f.write(text)
|
f.write(text)
|
||||||
f.close()
|
f.close()
|
||||||
else:
|
else:
|
||||||
|
@ -179,91 +179,80 @@ def return_data(text, args, outputname, module):
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
class Object(object):
|
|
||||||
pass
|
|
||||||
|
|
||||||
type_choices = ['html', 'latex', 'man', 'rst', 'json']
|
p = optparse.OptionParser(
|
||||||
|
version='%prog 1.0',
|
||||||
|
usage='usage: %prog [options] arg1 arg2',
|
||||||
|
description='Convert Ansible module DOCUMENTATION strings to other formats',
|
||||||
|
)
|
||||||
|
|
||||||
args = Object()
|
p.add_option("-A", "--ansible-version",
|
||||||
args.ansible_version = 'unknown'
|
action="store",
|
||||||
args.module_dir = MODULEDIR
|
dest="ansible_version",
|
||||||
args.template_dir = 'hacking/templates'
|
default="unknown",
|
||||||
args.type = 'latex'
|
help="Ansible version number")
|
||||||
args.module_list = []
|
p.add_option("-M", "--module-dir",
|
||||||
args.verbose = False
|
action="store",
|
||||||
args.output_dir = None
|
dest="module_dir",
|
||||||
args.includes_file = None
|
default=MODULEDIR,
|
||||||
args.do_boilerplate = False
|
help="Ansible modules/ directory")
|
||||||
|
p.add_option("-T", "--template-dir",
|
||||||
|
action="store",
|
||||||
|
dest="template_dir",
|
||||||
|
default="hacking/templates",
|
||||||
|
help="directory containing Jinja2 templates")
|
||||||
|
p.add_option("-t", "--type",
|
||||||
|
action='store',
|
||||||
|
dest='type',
|
||||||
|
choices=['html', 'latex', 'man', 'rst', 'json'],
|
||||||
|
default='latex',
|
||||||
|
help="Output type")
|
||||||
|
p.add_option("-m", "--module",
|
||||||
|
action='append',
|
||||||
|
default=[],
|
||||||
|
dest='module_list',
|
||||||
|
help="Add modules to process in module_dir")
|
||||||
|
p.add_option("-v", "--verbose",
|
||||||
|
action='store_true',
|
||||||
|
default=False,
|
||||||
|
help="Verbose")
|
||||||
|
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")
|
||||||
|
p.add_option("-G", "--generate",
|
||||||
|
action="store_true",
|
||||||
|
dest="do_boilerplate",
|
||||||
|
default=False,
|
||||||
|
help="generate boilerplate DOCUMENTATION to stdout")
|
||||||
|
p.add_option('-V', action='version')
|
||||||
|
|
||||||
try:
|
(options, args) = p.parse_args()
|
||||||
opts, arguments = getopt.getopt(sys.argv[1:], 'A:M:T:t:m:vo:I:GVh',
|
|
||||||
[ 'ansible-version=', 'module-dir=', 'template-dir=', 'type=',
|
|
||||||
'module=', 'verbose', 'output-dir=', 'includes-file=',
|
|
||||||
'generate', 'version', 'help', ])
|
|
||||||
except getopt.error, e:
|
|
||||||
print >>sys.stderr, 'ERROR: %s'% str(e)
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
for opt, arg in opts:
|
# print "M: %s" % options.module_dir
|
||||||
if opt in ('-A', '--ansible-version'):
|
# print "t: %s" % options.type
|
||||||
args.ansible_version = arg
|
# print "m: %s" % options.module_list
|
||||||
elif opt in ('-M', '--module-dir'):
|
# print "v: %s" % options.verbose
|
||||||
args.module_dir = arg
|
|
||||||
elif opt in ('-T', '--template-dir'):
|
|
||||||
args.template_dir = arg
|
|
||||||
elif opt in ('-t', '--type'):
|
|
||||||
args.type = arg
|
|
||||||
if args.type not in type_choices:
|
|
||||||
print >>sys.stderr, 'ERROR: Type %s not in possible types %s.' % (args.type, type_choices)
|
|
||||||
sys.exit(1)
|
|
||||||
elif opt in ('-m', '--module'):
|
|
||||||
args.module_list.append(arg)
|
|
||||||
elif opt in ('-v', '--verbose'):
|
|
||||||
args.verbose = True
|
|
||||||
elif opt in ('-o', '--output-dir'):
|
|
||||||
args.output_dir = arg
|
|
||||||
elif opt in ('-I', '--includes-file'):
|
|
||||||
args.includes_file = arg
|
|
||||||
elif opt in ('-G', '--generate'):
|
|
||||||
args.do_boilerplate = True
|
|
||||||
elif opt in ('-V', '--version'):
|
|
||||||
print >>sys.stderr, '%(prog)s 1.0'
|
|
||||||
elif opt in ('-h', '--help'):
|
|
||||||
print >>sys.stderr, '''Convert Ansible module DOCUMENTATION strings to other formats
|
|
||||||
|
|
||||||
-A, --ansible-version= Ansible version number
|
if options.do_boilerplate:
|
||||||
-M, --module-dir= Ansible modules/ directory
|
|
||||||
-T, --template-dir= Directory containing Jinja2 templates
|
|
||||||
-t, --type= Output type
|
|
||||||
-m, --module= Add modules to process in module_dir
|
|
||||||
-v, --verbose Verbose
|
|
||||||
-o, --output-dir= Output directory for module files
|
|
||||||
-I, --includes-file= Create a file containing list of processed modules
|
|
||||||
-G, --generate Generate boilerplate DOCUMENTATION to stdout
|
|
||||||
'''
|
|
||||||
sys.exit(0)
|
|
||||||
else:
|
|
||||||
print >>sys.stderr, 'ERROR: Option %s unknown to getopt' % opt
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
# print "M: %s" % args.module_dir
|
|
||||||
# print "t: %s" % args.type
|
|
||||||
# print "m: %s" % args.module_list
|
|
||||||
# print "v: %s" % args.verbose
|
|
||||||
|
|
||||||
if args.do_boilerplate:
|
|
||||||
boilerplate()
|
boilerplate()
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
if not args.module_dir:
|
if not options.module_dir:
|
||||||
print "Need module_dir"
|
print "Need module_dir"
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
if not args.template_dir:
|
if not options.template_dir:
|
||||||
print "Need template_dir"
|
print "Need template_dir"
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
env = Environment(loader=FileSystemLoader(args.template_dir),
|
env = Environment(loader=FileSystemLoader(options.template_dir),
|
||||||
variable_start_string="@{",
|
variable_start_string="@{",
|
||||||
variable_end_string="}@",
|
variable_end_string="}@",
|
||||||
trim_blocks=True,
|
trim_blocks=True,
|
||||||
|
@ -271,25 +260,25 @@ def main():
|
||||||
|
|
||||||
env.globals['xline'] = rst_xline
|
env.globals['xline'] = rst_xline
|
||||||
|
|
||||||
if args.type == 'latex':
|
if options.type == 'latex':
|
||||||
env.filters['jpfunc'] = latex_ify
|
env.filters['jpfunc'] = latex_ify
|
||||||
template = env.get_template('latex.j2')
|
template = env.get_template('latex.j2')
|
||||||
outputname = "%s.tex"
|
outputname = "%s.tex"
|
||||||
includecmt = "% generated code\n"
|
includecmt = "% generated code\n"
|
||||||
includefmt = "\\input %s\n"
|
includefmt = "\\input %s\n"
|
||||||
if args.type == 'html':
|
if options.type == 'html':
|
||||||
env.filters['jpfunc'] = html_ify
|
env.filters['jpfunc'] = html_ify
|
||||||
template = env.get_template('html.j2')
|
template = env.get_template('html.j2')
|
||||||
outputname = "%s.html"
|
outputname = "%s.html"
|
||||||
includecmt = ""
|
includecmt = ""
|
||||||
includefmt = ""
|
includefmt = ""
|
||||||
if args.type == 'man':
|
if options.type == 'man':
|
||||||
env.filters['jpfunc'] = man_ify
|
env.filters['jpfunc'] = man_ify
|
||||||
template = env.get_template('man.j2')
|
template = env.get_template('man.j2')
|
||||||
outputname = "ansible.%s.3"
|
outputname = "ansible.%s.3"
|
||||||
includecmt = ""
|
includecmt = ""
|
||||||
includefmt = ""
|
includefmt = ""
|
||||||
if args.type == 'rst':
|
if options.type == 'rst':
|
||||||
env.filters['jpfunc'] = rst_ify
|
env.filters['jpfunc'] = rst_ify
|
||||||
env.filters['html_ify'] = html_ify
|
env.filters['html_ify'] = html_ify
|
||||||
env.filters['fmt'] = rst_fmt
|
env.filters['fmt'] = rst_fmt
|
||||||
|
@ -298,28 +287,28 @@ def main():
|
||||||
outputname = "%s.rst"
|
outputname = "%s.rst"
|
||||||
includecmt = ".. Generated by module_formatter\n"
|
includecmt = ".. Generated by module_formatter\n"
|
||||||
includefmt = ".. include:: modules/%s.rst\n"
|
includefmt = ".. include:: modules/%s.rst\n"
|
||||||
if args.type == 'json':
|
if options.type == 'json':
|
||||||
env.filters['jpfunc'] = json_ify
|
env.filters['jpfunc'] = json_ify
|
||||||
outputname = "%s.json"
|
outputname = "%s.json"
|
||||||
includecmt = ""
|
includecmt = ""
|
||||||
includefmt = ""
|
includefmt = ""
|
||||||
if args.type == 'js':
|
if options.type == 'js':
|
||||||
env.filters['jpfunc'] = js_ify
|
env.filters['jpfunc'] = js_ify
|
||||||
template = env.get_template('js.j2')
|
template = env.get_template('js.j2')
|
||||||
outputname = "%s.js"
|
outputname = "%s.js"
|
||||||
|
|
||||||
if args.includes_file is not None and includefmt != "":
|
if options.includes_file is not None and includefmt != "":
|
||||||
incfile = open(args.includes_file, "w")
|
incfile = open(options.includes_file, "w")
|
||||||
incfile.write(includecmt)
|
incfile.write(includecmt)
|
||||||
|
|
||||||
# Temporary variable required to genrate aggregated content in 'js' format.
|
# Temporary variable required to genrate aggregated content in 'js' format.
|
||||||
js_data = []
|
js_data = []
|
||||||
for module in sorted(os.listdir(args.module_dir)):
|
for module in sorted(os.listdir(options.module_dir)):
|
||||||
if len(args.module_list):
|
if len(options.module_list):
|
||||||
if not module in args.module_list:
|
if not module in options.module_list:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
fname = os.path.join(args.module_dir, module)
|
fname = os.path.join(options.module_dir, module)
|
||||||
extra = os.path.join("inc", "%s.tex" % module)
|
extra = os.path.join("inc", "%s.tex" % module)
|
||||||
|
|
||||||
if fname.endswith(".swp"):
|
if fname.endswith(".swp"):
|
||||||
|
@ -327,7 +316,7 @@ def main():
|
||||||
|
|
||||||
print " processing module source ---> %s" % fname
|
print " processing module source ---> %s" % fname
|
||||||
|
|
||||||
if args.type == 'js':
|
if options.type == 'js':
|
||||||
if fname.endswith(".json"):
|
if fname.endswith(".json"):
|
||||||
f = open(fname)
|
f = open(fname)
|
||||||
j = json.load(f)
|
j = json.load(f)
|
||||||
|
@ -335,7 +324,7 @@ def main():
|
||||||
js_data.append(j)
|
js_data.append(j)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
doc = get_docstring(fname, verbose=args.verbose)
|
doc = get_docstring(fname, verbose=options.verbose)
|
||||||
|
|
||||||
if doc is None and module not in BLACKLIST_MODULES:
|
if doc is None and module not in BLACKLIST_MODULES:
|
||||||
sys.stderr.write("*** ERROR: CORE MODULE MISSING DOCUMENTATION: %s ***\n" % module)
|
sys.stderr.write("*** ERROR: CORE MODULE MISSING DOCUMENTATION: %s ***\n" % module)
|
||||||
|
@ -346,34 +335,34 @@ def main():
|
||||||
doc['filename'] = fname
|
doc['filename'] = fname
|
||||||
doc['docuri'] = doc['module'].replace('_', '-')
|
doc['docuri'] = doc['module'].replace('_', '-')
|
||||||
doc['now_date'] = datetime.date.today().strftime('%Y-%m-%d')
|
doc['now_date'] = datetime.date.today().strftime('%Y-%m-%d')
|
||||||
doc['ansible_version'] = args.ansible_version
|
doc['ansible_version'] = options.ansible_version
|
||||||
|
|
||||||
if args.includes_file is not None and includefmt != "":
|
if options.includes_file is not None and includefmt != "":
|
||||||
incfile.write(includefmt % module)
|
incfile.write(includefmt % module)
|
||||||
|
|
||||||
if args.verbose:
|
if options.verbose:
|
||||||
print json.dumps(doc, indent=4)
|
print json.dumps(doc, indent=4)
|
||||||
|
|
||||||
|
|
||||||
if args.type == 'latex':
|
if options.type == 'latex':
|
||||||
if os.path.exists(extra):
|
if os.path.exists(extra):
|
||||||
f = open(extra)
|
f = open(extra)
|
||||||
extradata = f.read()
|
extradata = f.read()
|
||||||
f.close()
|
f.close()
|
||||||
doc['extradata'] = extradata
|
doc['extradata'] = extradata
|
||||||
|
|
||||||
if args.type == 'json':
|
if options.type == 'json':
|
||||||
text = json.dumps(doc, indent=2)
|
text = json.dumps(doc, indent=2)
|
||||||
else:
|
else:
|
||||||
text = template.render(doc)
|
text = template.render(doc)
|
||||||
|
|
||||||
return_data(text, args, outputname, module)
|
return_data(text, options, outputname, module)
|
||||||
|
|
||||||
if args.type == 'js':
|
if options.type == 'js':
|
||||||
docs = {}
|
docs = {}
|
||||||
docs['json'] = json.dumps(js_data, indent=2)
|
docs['json'] = json.dumps(js_data, indent=2)
|
||||||
text = template.render(docs)
|
text = template.render(docs)
|
||||||
return_data(text, args, outputname, 'modules')
|
return_data(text, options, outputname, 'modules')
|
||||||
|
|
||||||
#def boilerplate():
|
#def boilerplate():
|
||||||
#
|
#
|
||||||
|
|
Loading…
Reference in a new issue