From 20563db49fb823ef331822e6795849d01ce0df3b Mon Sep 17 00:00:00 2001 From: Jack Lloyd Date: Sun, 3 Dec 2017 13:20:30 -0500 Subject: Add a script for generating the documentation --- src/scripts/build_docs.py | 145 ++++++++++++++++++++++++++++++++++++++++++++++ src/scripts/ci_build.py | 1 + src/scripts/cleanup.py | 14 +++-- 3 files changed, 155 insertions(+), 5 deletions(-) create mode 100755 src/scripts/build_docs.py (limited to 'src/scripts') diff --git a/src/scripts/build_docs.py b/src/scripts/build_docs.py new file mode 100755 index 000000000..b30e3d3e1 --- /dev/null +++ b/src/scripts/build_docs.py @@ -0,0 +1,145 @@ +#!/usr/bin/env python + +""" +Botan doc generation script + +(C) 2014,2015,2017 Jack Lloyd + +Botan is released under the Simplified BSD License (see license.txt) +""" + +import sys +import optparse # pylint: disable=deprecated-module +import subprocess +import shutil +import logging +import json +import os + +def get_concurrency(): + """ + Get default concurrency level of build + """ + def_concurrency = 2 + + try: + import multiprocessing + return max(def_concurrency, multiprocessing.cpu_count()) + except ImportError: + return def_concurrency + +def touch(fname): + try: + os.utime(fname, None) + except OSError: + open(fname, 'a').close() + +def copy_files(src_dir, dest_dir): + for f in os.listdir(src_dir): + src_file = os.path.join(src_dir, f) + dest_file = os.path.join(dest_dir, f) + logging.debug("Copying from %s to %s", src_file, dest_file) + shutil.copyfile(src_file, dest_file) + +def run_and_check(cmd_line): + + logging.debug("Executing %s", ' '.join(cmd_line)) + + proc = subprocess.Popen(cmd_line, + close_fds=True) + + (stdout, stderr) = proc.communicate() + + if proc.returncode != 0: + logging.error("Error running %s", ' '.join(cmd_line)) + sys.exit(1) + + +def parse_options(args): + parser = optparse.OptionParser() + + parser.add_option('--verbose', action='store_true', default=False, + help='Show debug messages') + parser.add_option('--quiet', action='store_true', default=False, + help='Show only warnings and errors') + + parser.add_option('--build-dir', metavar='DIR', default='build', + help='Location of build output (default \'%default\')') + parser.add_option('--dry-run', default=False, action='store_true', + help='Just display what would be done') + + (options, args) = parser.parse_args(args) + + if len(args) > 1: + logging.error("Unknown arguments") + return None + + def log_level(): + if options.verbose: + return logging.DEBUG + if options.quiet: + return logging.WARNING + return logging.INFO + + logging.getLogger().setLevel(log_level()) + + return options + + +def main(args=None): + if args is None: + args = sys.argv + + logging.basicConfig(stream=sys.stdout, + format='%(levelname) 7s: %(message)s') + + options = parse_options(args) + + if options is None: + return 1 + + with open(os.path.join(options.build_dir, 'build_config.json')) as f: + cfg = json.load(f) + + with_docs = bool(cfg['with_documentation']) + with_sphinx = bool(cfg['with_sphinx']) + with_doxygen = bool(cfg['with_doxygen']) + + doc_stamp_file = cfg['doc_stamp_file'] + + manual_src = os.path.join(cfg['doc_dir'], 'manual') + manual_output = os.path.join(cfg['doc_output_dir'], 'manual') + + if with_docs is False: + logging.debug('Documentation build disabled') + return 0 + + cmds = [] + + if with_doxygen: + cmds.append(['doxygen', os.path.join(cfg['build_dir'], 'botan.doxy')]) + + if with_sphinx: + cmds.append(['sphinx-build', '-q', '-b', 'html', '-c', cfg['sphinx_config_dir'], + '-j', str(get_concurrency()), manual_src, manual_output]) + else: + # otherwise just copy it + cmds.append(['cp', manual_src, manual_output]) + + cmds.append(['touch', doc_stamp_file]) + + for cmd in cmds: + if options.dry_run: + print(' '.join(cmd)) + else: + if cmd[0] == 'cp': + assert len(cmd) == 3 + copy_files(cmd[1], cmd[2]) + elif cmd[0] == 'touch': + assert len(cmd) == 2 + touch(cmd[1]) + else: + run_and_check(cmd) + +if __name__ == '__main__': + sys.exit(main()) diff --git a/src/scripts/ci_build.py b/src/scripts/ci_build.py index 74c6a7bfb..c81c86795 100755 --- a/src/scripts/ci_build.py +++ b/src/scripts/ci_build.py @@ -364,6 +364,7 @@ def main(args=None): 'src/scripts/ci_build.py', 'src/scripts/install.py', 'src/scripts/cleanup.py', + 'src/scripts/build_docs.py', 'src/scripts/website.py', 'src/scripts/python_unittests.py', 'src/scripts/python_unittests_unix.py'] diff --git a/src/scripts/cleanup.py b/src/scripts/cleanup.py index d246f766c..71c59609e 100755 --- a/src/scripts/cleanup.py +++ b/src/scripts/cleanup.py @@ -69,8 +69,9 @@ def main(args=None): build_dir = options.build_dir if os.access(build_dir, os.X_OK) != True: - logging.error("Unable to access build directory") - return 1 + logging.debug('No build directory found') + # No build dir: clean enough! + return 0 build_config_path = os.path.join(build_dir, 'build_config.json') build_config_str = None @@ -86,8 +87,6 @@ def main(args=None): build_config = json.loads(build_config_str) - #print(json.dumps(build_config, sort_keys=True, indent=3)) - if options.distclean: build_dir = build_config['build_dir'] remove_file(build_config['makefile_path']) @@ -97,7 +96,12 @@ def main(args=None): dir_path = build_config[dir_type] remove_all_in_dir(dir_path) - shutil.rmtree(build_config['doc_output_dir']) + try: + shutil.rmtree(build_config['doc_output_dir']) + except OSError as e: + pass + + #remove_file(build_config['doc_stamp_file']) remove_file(build_config['cli_exe']) remove_file(build_config['test_exe']) -- cgit v1.2.3 From 44a570bab8cb15f104687affa12e2bc85e8cdf9a Mon Sep 17 00:00:00 2001 From: Jack Lloyd Date: Mon, 4 Dec 2017 07:17:47 -0500 Subject: Fix lint error [ci skip] --- src/scripts/cleanup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/scripts') diff --git a/src/scripts/cleanup.py b/src/scripts/cleanup.py index 71c59609e..f14f6d9c1 100755 --- a/src/scripts/cleanup.py +++ b/src/scripts/cleanup.py @@ -98,7 +98,7 @@ def main(args=None): try: shutil.rmtree(build_config['doc_output_dir']) - except OSError as e: + except OSError: pass #remove_file(build_config['doc_stamp_file']) -- cgit v1.2.3 From 6713f6363033ed45d7ced49c2e8cb6240334ea7a Mon Sep 17 00:00:00 2001 From: Jack Lloyd Date: Mon, 4 Dec 2017 10:59:50 -0500 Subject: Lint fixes --- src/scripts/build_docs.py | 2 +- src/scripts/cleanup.py | 22 ++++++++++++---------- 2 files changed, 13 insertions(+), 11 deletions(-) (limited to 'src/scripts') diff --git a/src/scripts/build_docs.py b/src/scripts/build_docs.py index b30e3d3e1..c6b8e9f67 100755 --- a/src/scripts/build_docs.py +++ b/src/scripts/build_docs.py @@ -48,7 +48,7 @@ def run_and_check(cmd_line): proc = subprocess.Popen(cmd_line, close_fds=True) - (stdout, stderr) = proc.communicate() + proc.communicate() if proc.returncode != 0: logging.error("Error running %s", ' '.join(cmd_line)) diff --git a/src/scripts/cleanup.py b/src/scripts/cleanup.py index f14f6d9c1..ae2f588f5 100755 --- a/src/scripts/cleanup.py +++ b/src/scripts/cleanup.py @@ -43,10 +43,7 @@ def remove_all_in_dir(d): for f in os.listdir(d): remove_file(os.path.join(d, f)) -def main(args=None): - if args is None: - args = sys.argv - +def parse_options(args): parser = optparse.OptionParser() parser.add_option('--build-dir', default='build', metavar='DIR', help='specify build dir to clean (default %default)') @@ -58,14 +55,21 @@ def main(args=None): (options, args) = parser.parse_args(args) + if len(args) > 1: + raise Exception("Unknown arguments") + + return options + +def main(args=None): + if args is None: + args = sys.argv + + options = parse_options(args) + logging.basicConfig(stream=sys.stderr, format='%(levelname) 7s: %(message)s', level=logging.DEBUG if options.verbose else logging.INFO) - if len(args) > 1: - logging.error("Unknown arguments") - return 1 - build_dir = options.build_dir if os.access(build_dir, os.X_OK) != True: @@ -101,8 +105,6 @@ def main(args=None): except OSError: pass - #remove_file(build_config['doc_stamp_file']) - remove_file(build_config['cli_exe']) remove_file(build_config['test_exe']) -- cgit v1.2.3