|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +# Documentation build script for AWS Sphinx documentation on GitHub. |
| 5 | +# |
| 6 | +# Author: Eron Hennessey ([email protected]) |
| 7 | +# |
| 8 | +# Copyright © 2016, Amazon Web Services, Inc. or its affiliates. All rights reserved. |
| 9 | + |
| 10 | +import sys, os |
| 11 | +import subprocess |
| 12 | +import shutil |
| 13 | + |
| 14 | +SPHINX_MISSING = """ |
| 15 | +You must have Sphinx installed to use this script! |
| 16 | +
|
| 17 | +Go to http://www.sphinx-doc.org for information about installing and using |
| 18 | +Sphinx. |
| 19 | +""" |
| 20 | + |
| 21 | +FAILED_CHECKOUT = """ |
| 22 | +Couldn't clone repository. Please make sure that you have 'git' installed and |
| 23 | +that you can access GitHub repositories using SSH. |
| 24 | +""" |
| 25 | + |
| 26 | +AWS_SHARED_REPO = '[email protected]:awsdocs/aws-doc-shared-content.git' |
| 27 | +BUILD_DIR = 'build' |
| 28 | +OUTPUT_DIR = 'output' |
| 29 | +SHARED_DIR = 'shared_content' |
| 30 | +SHARED_SUBDIR = 'sphinx_shared' |
| 31 | +SOURCE_DIR = 'doc_source' |
| 32 | + |
| 33 | + |
| 34 | +def check_and_remove_dir(dir_name): |
| 35 | + """Check to see if the named directory exists. If it does, then remove it. |
| 36 | + Throw an exception if the directory can't be removed.""" |
| 37 | + |
| 38 | + if os.path.exists(dir_name): |
| 39 | + print("Removing directory: " + dir_name) |
| 40 | + try: |
| 41 | + shutil.rmtree(dir_name) |
| 42 | + except: |
| 43 | + print("Couldn't remove " + dir_name) |
| 44 | + print("Remove this directory before building!") |
| 45 | + sys.exit(1) |
| 46 | + |
| 47 | + |
| 48 | +def copy_dir_contents_with_overwrite(input_dir_name, output_dir_name): |
| 49 | + """Copy the contents of a directory into another, overwriting files if they |
| 50 | + exist.""" |
| 51 | + |
| 52 | + # if output_dir_name isn't a location, make it so. |
| 53 | + if not os.path.exists(output_dir_name): |
| 54 | + os.makedirs(output_dir_name) |
| 55 | + |
| 56 | + dir_entries = os.listdir(input_dir_name) |
| 57 | + |
| 58 | + for dir_entry in dir_entries: |
| 59 | + input_path = os.path.join(input_dir_name, dir_entry) |
| 60 | + output_path = os.path.join(output_dir_name, dir_entry) |
| 61 | + |
| 62 | + if os.path.isdir(input_path): |
| 63 | + copy_dir_contents_with_overwrite(input_path, output_path) |
| 64 | + else: |
| 65 | + shutil.copyfile(input_path, output_path) |
| 66 | + |
| 67 | + |
| 68 | +def run(): |
| 69 | + # try to import requirements, and complain if they can't be found. |
| 70 | + try: |
| 71 | + from sphinx import version_info as sphinx_version |
| 72 | + print("Using Sphinx version %s.%s.%s" % sphinx_version[0:3]) |
| 73 | + except: |
| 74 | + print(SPHINX_MISSING) |
| 75 | + sys.exit(1) |
| 76 | + |
| 77 | + build_target = 'html' # by default |
| 78 | + cmd_switches = [] |
| 79 | + |
| 80 | + for arg in sys.argv[1:]: |
| 81 | + if arg.startswith('--'): |
| 82 | + cmd_switches.append(arg) |
| 83 | + else: |
| 84 | + # the only non-switch argument is the output format. |
| 85 | + build_target = arg |
| 86 | + |
| 87 | + print("Building '%s' target." % build_target) |
| 88 | + |
| 89 | + # |
| 90 | + # Step 0: empty the build dir if it's there. |
| 91 | + # |
| 92 | + check_and_remove_dir(BUILD_DIR) |
| 93 | + check_and_remove_dir(SHARED_DIR) |
| 94 | + check_and_remove_dir(OUTPUT_DIR) |
| 95 | + |
| 96 | + # |
| 97 | + # Step 1: grab the shared content and copy it into BUILD_DIR. |
| 98 | + # |
| 99 | + print("Getting shared content from " + AWS_SHARED_REPO) |
| 100 | + try: |
| 101 | + subprocess.check_call(['git', 'clone', '--depth', '1', AWS_SHARED_REPO, |
| 102 | + SHARED_DIR]) |
| 103 | + except: |
| 104 | + print(FAILED_CHECKOUT) |
| 105 | + sys.exit(1) |
| 106 | + |
| 107 | + shared_input_dir = os.path.join(SHARED_DIR, SHARED_SUBDIR) |
| 108 | + print("Copying shared content from %s to %s" % (shared_input_dir, |
| 109 | + BUILD_DIR)) |
| 110 | + copy_dir_contents_with_overwrite(shared_input_dir, BUILD_DIR) |
| 111 | + |
| 112 | + # |
| 113 | + # Step 2: copy the contents of SOURCE_DIR into the BUILD_DIR. |
| 114 | + # |
| 115 | + print("Copying doc sources from %s to %s" % (SOURCE_DIR, BUILD_DIR)) |
| 116 | + copy_dir_contents_with_overwrite(SOURCE_DIR, BUILD_DIR) |
| 117 | + |
| 118 | + # |
| 119 | + # Append the contents of any files in the 'build/_conf' directory to the |
| 120 | + # project's conf.py file (so that shared content can add commonly-used |
| 121 | + # extlinks, etc.). |
| 122 | + # |
| 123 | + conf_py_path = os.path.join(BUILD_DIR, 'conf.py') |
| 124 | + extra_conf_path = os.path.join(BUILD_DIR, '_conf') |
| 125 | + # first, open the conf.py file for appending... |
| 126 | + with open(conf_py_path, 'a') as conf_file: |
| 127 | + # now, add the contents of each file in alpha-sorted order. |
| 128 | + for filename in sorted(os.listdir(extra_conf_path)): |
| 129 | + print(" - %s" % filename) |
| 130 | + conf_file.write('# ** added by extra conf file: %s **\n' % filename) |
| 131 | + with open(os.path.join(extra_conf_path, filename), 'r') as extra_conf_file: |
| 132 | + conf_file.write(extra_conf_file.read()) |
| 133 | + conf_file.write('# ** end of content from %s **\n' % filename) |
| 134 | + |
| 135 | + # |
| 136 | + # Step 3: build the docs |
| 137 | + # |
| 138 | + print("Building documentation.") |
| 139 | + try: |
| 140 | + subprocess.check_call(['sphinx-build', '-b', build_target, BUILD_DIR, |
| 141 | + OUTPUT_DIR]) |
| 142 | + except: |
| 143 | + print(FAILED_CHECKOUT) |
| 144 | + sys.exit(1) |
| 145 | + |
| 146 | + # |
| 147 | + # Step 4: Clean up the build dir and shared content. |
| 148 | + # |
| 149 | + if '--noclean' not in cmd_switches: |
| 150 | + print("Cleaning up.") |
| 151 | + check_and_remove_dir(BUILD_DIR) |
| 152 | + check_and_remove_dir(SHARED_DIR) |
| 153 | + |
| 154 | + print("Finished! You'll find the built docs in the '%s' directory." % |
| 155 | + OUTPUT_DIR) |
| 156 | + |
| 157 | + |
| 158 | +# If run from the command-line... |
| 159 | +if __name__ == '__main__': |
| 160 | + run() |
| 161 | + |
0 commit comments