Use argparse and log functions for gendoc.py

gendoc.py has become more complex such that we actually want to pass things
to it like `--verbose`, `--nodelete`, `--target`, so use `argparse` to do this
like we have `build.py`. Pass through `-v` flags to `build.py`.
pull/977/head
Kegan Dougal 9 years ago
parent 14e77b09ab
commit 064a2c9172

@ -1,5 +1,6 @@
#! /usr/bin/env python #! /usr/bin/env python
from argparse import ArgumentParser
from docutils.core import publish_file from docutils.core import publish_file
import copy import copy
import fileinput import fileinput
@ -17,6 +18,7 @@ stylesheets = {
"stylesheet_path": ["basic.css", "nature.css", "codehighlight.css"] "stylesheet_path": ["basic.css", "nature.css", "codehighlight.css"]
} }
VERBOSE = False
""" """
Read a RST file and replace titles with a different title level if required. Read a RST file and replace titles with a different title level if required.
@ -64,8 +66,8 @@ def load_with_adjusted_titles(filename, file_stream, title_level, title_styles):
if file_offset is None: if file_offset is None:
file_offset = line_title_level file_offset = line_title_level
if file_offset != 0: if file_offset != 0:
print (" WARNING: %s starts with a title style of '%s' but '%s' " + logv((" WARNING: %s starts with a title style of '%s' but '%s' " +
"is preferable.") % (filename, line_title_style, title_styles[0]) "is preferable.") % (filename, line_title_style, title_styles[0]))
# Sanity checks: Make sure that this file is obeying the title levels # Sanity checks: Make sure that this file is obeying the title levels
# specified and bail if it isn't. # specified and bail if it isn't.
@ -101,12 +103,11 @@ def load_with_adjusted_titles(filename, file_stream, title_level, title_styles):
continue continue
# Adjusting line levels # Adjusting line levels
# print ( logv(
# "File: %s Adjusting %s to %s because file_offset=%s title_offset=%s" % "File: %s Adjusting %s to %s because file_offset=%s title_offset=%s" %
# (filename, line_title_style, (filename, line_title_style, title_styles[adjusted_level],
# title_styles[adjusted_level], file_offset, title_level)
# file_offset, title_level) )
# )
rst_lines.append(line.replace( rst_lines.append(line.replace(
line_title_style, line_title_style,
title_styles[adjusted_level] title_styles[adjusted_level]
@ -118,7 +119,7 @@ def load_with_adjusted_titles(filename, file_stream, title_level, title_styles):
def get_rst(file_info, title_level, title_styles, spec_dir, adjust_titles): def get_rst(file_info, title_level, title_styles, spec_dir, adjust_titles):
# string are file paths to RST blobs # string are file paths to RST blobs
if isinstance(file_info, basestring): if isinstance(file_info, basestring):
print "%s %s" % (">" * (1 + title_level), file_info) log("%s %s" % (">" * (1 + title_level), file_info))
with open(os.path.join(spec_dir, file_info), "r") as f: with open(os.path.join(spec_dir, file_info), "r") as f:
rst = None rst = None
if adjust_titles: if adjust_titles:
@ -244,15 +245,21 @@ def run_through_template(input):
tmpfile = './tmp/output' tmpfile = './tmp/output'
try: try:
with open(tmpfile, 'w') as out: with open(tmpfile, 'w') as out:
print subprocess.check_output( args = [
[ 'python', 'build.py',
'python', 'build.py', "-v", "-i", "matrix_templates",
"-i", "matrix_templates", "-o", "../scripts/tmp",
"-o", "../scripts/tmp", "../scripts/"+input
"../scripts/"+input ]
], if VERBOSE:
stderr=out, args.insert(2, "-v")
cwd="../templating", log(" ==== build.py output ==== ")
log(
subprocess.check_output(
args,
stderr=out,
cwd="../templating"
)
) )
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
with open(tmpfile, 'r') as f: with open(tmpfile, 'r') as f:
@ -347,6 +354,13 @@ def get_build_target(targets_listing, target_name):
build_target["files"] = resolved_files build_target["files"] = resolved_files
return build_target return build_target
def log(line):
print "gendoc: %s" % line
def logv(line):
if VERBOSE:
print "gendoc:V: %s" % line
def prepare_env(): def prepare_env():
try: try:
@ -363,9 +377,9 @@ def cleanup_env():
shutil.rmtree("./tmp") shutil.rmtree("./tmp")
def main(target_name): def main(target_name, keep_intermediates):
prepare_env() prepare_env()
print "Building spec [target=%s]" % target_name log("Building spec [target=%s]" % target_name)
target = get_build_target("../specification/targets.yaml", target_name) target = get_build_target("../specification/targets.yaml", target_name)
build_spec(target=target, out_filename="tmp/templated_spec.rst") build_spec(target=target, out_filename="tmp/templated_spec.rst")
run_through_template("tmp/templated_spec.rst") run_through_template("tmp/templated_spec.rst")
@ -377,22 +391,29 @@ def main(target_name):
run_through_template("tmp/howto.rst") run_through_template("tmp/howto.rst")
rst2html("tmp/full_spec.rst", "gen/specification.html") rst2html("tmp/full_spec.rst", "gen/specification.html")
rst2html("tmp/howto.rst", "gen/howtos.html") rst2html("tmp/howto.rst", "gen/howtos.html")
if "--nodelete" not in sys.argv: if not keep_intermediates:
cleanup_env() cleanup_env()
if __name__ == '__main__': if __name__ == '__main__':
if len(sys.argv) > 1 and sys.argv[1:] != ["--nodelete"]: parser = ArgumentParser(
# we accept almost no args, so they don't know what they're doing! "gendoc.py - Generate the Matrix specification as HTML to the gen/ folder."
print "gendoc.py - Generate the Matrix specification as HTML." )
print "Usage:" parser.add_argument(
print " python gendoc.py [--nodelete]" "--nodelete", "-n", action="store_true",
print "" help="Do not delete intermediate files. They will be found in tmp/"
print "The specification can then be found in the gen/ folder." )
print ("If --nodelete was specified, intermediate files will be " parser.add_argument(
"present in the tmp/ folder.") "--target", "-t", default="main",
print "" help="Specify the build target to build from specification/targets.yaml"
print "Requirements:" )
print " - This script requires Jinja2 and rst2html (docutils)." parser.add_argument(
sys.exit(0) "--verbose", "-v", action="store_true",
main("main") help="Turn on verbose mode."
)
args = parser.parse_args()
if not args.target:
parser.print_help()
sys.exit(1)
VERBOSE = args.verbose
main(args.target, args.nodelete)

Loading…
Cancel
Save