From 064a2c91721a285cf03dcd0505e3c01a8af39256 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Wed, 23 Sep 2015 14:59:57 +0100 Subject: [PATCH] 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`. --- scripts/gendoc.py | 91 +++++++++++++++++++++++++++++------------------ 1 file changed, 56 insertions(+), 35 deletions(-) diff --git a/scripts/gendoc.py b/scripts/gendoc.py index 364b8e65..d2b0d75e 100755 --- a/scripts/gendoc.py +++ b/scripts/gendoc.py @@ -1,5 +1,6 @@ #! /usr/bin/env python +from argparse import ArgumentParser from docutils.core import publish_file import copy import fileinput @@ -17,6 +18,7 @@ stylesheets = { "stylesheet_path": ["basic.css", "nature.css", "codehighlight.css"] } +VERBOSE = False """ 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: file_offset = line_title_level if file_offset != 0: - print (" WARNING: %s starts with a title style of '%s' but '%s' " + - "is preferable.") % (filename, line_title_style, title_styles[0]) + logv((" WARNING: %s starts with a title style of '%s' but '%s' " + + "is preferable.") % (filename, line_title_style, title_styles[0])) # Sanity checks: Make sure that this file is obeying the title levels # specified and bail if it isn't. @@ -101,12 +103,11 @@ def load_with_adjusted_titles(filename, file_stream, title_level, title_styles): continue # Adjusting line levels - # print ( - # "File: %s Adjusting %s to %s because file_offset=%s title_offset=%s" % - # (filename, line_title_style, - # title_styles[adjusted_level], - # file_offset, title_level) - # ) + logv( + "File: %s Adjusting %s to %s because file_offset=%s title_offset=%s" % + (filename, line_title_style, title_styles[adjusted_level], + file_offset, title_level) + ) rst_lines.append(line.replace( line_title_style, 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): # string are file paths to RST blobs 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: rst = None if adjust_titles: @@ -244,15 +245,21 @@ def run_through_template(input): tmpfile = './tmp/output' try: with open(tmpfile, 'w') as out: - print subprocess.check_output( - [ - 'python', 'build.py', "-v", - "-i", "matrix_templates", - "-o", "../scripts/tmp", - "../scripts/"+input - ], - stderr=out, - cwd="../templating", + args = [ + 'python', 'build.py', + "-i", "matrix_templates", + "-o", "../scripts/tmp", + "../scripts/"+input + ] + if VERBOSE: + args.insert(2, "-v") + log(" ==== build.py output ==== ") + log( + subprocess.check_output( + args, + stderr=out, + cwd="../templating" + ) ) except subprocess.CalledProcessError as e: with open(tmpfile, 'r') as f: @@ -347,6 +354,13 @@ def get_build_target(targets_listing, target_name): build_target["files"] = resolved_files return build_target +def log(line): + print "gendoc: %s" % line + +def logv(line): + if VERBOSE: + print "gendoc:V: %s" % line + def prepare_env(): try: @@ -363,9 +377,9 @@ def cleanup_env(): shutil.rmtree("./tmp") -def main(target_name): +def main(target_name, keep_intermediates): 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) build_spec(target=target, out_filename="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") rst2html("tmp/full_spec.rst", "gen/specification.html") rst2html("tmp/howto.rst", "gen/howtos.html") - if "--nodelete" not in sys.argv: + if not keep_intermediates: cleanup_env() if __name__ == '__main__': - if len(sys.argv) > 1 and sys.argv[1:] != ["--nodelete"]: - # we accept almost no args, so they don't know what they're doing! - print "gendoc.py - Generate the Matrix specification as HTML." - print "Usage:" - print " python gendoc.py [--nodelete]" - print "" - print "The specification can then be found in the gen/ folder." - print ("If --nodelete was specified, intermediate files will be " - "present in the tmp/ folder.") - print "" - print "Requirements:" - print " - This script requires Jinja2 and rst2html (docutils)." - sys.exit(0) - main("main") + parser = ArgumentParser( + "gendoc.py - Generate the Matrix specification as HTML to the gen/ folder." + ) + parser.add_argument( + "--nodelete", "-n", action="store_true", + help="Do not delete intermediate files. They will be found in tmp/" + ) + parser.add_argument( + "--target", "-t", default="main", + help="Specify the build target to build from specification/targets.yaml" + ) + parser.add_argument( + "--verbose", "-v", action="store_true", + 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)