|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# |
| 4 | +# Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 5 | +# |
| 6 | +# This file is licensed under the Apache License, Version 2.0 (the "License"). |
| 7 | +# You may not use this file except in compliance with the License. A copy of the |
| 8 | +# License is located at |
| 9 | +# |
| 10 | +# http://aws.amazon.com/apache2.0/ |
| 11 | +# |
| 12 | +# This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS |
| 13 | +# OF ANY KIND, either express or implied. See the License for the specific |
| 14 | +# language governing permissions and limitations under the License. |
| 15 | +# |
| 16 | +# ------------------------------------------------------------------ |
| 17 | +# Documentation build script for AWS Sphinx documentation on GitHub. |
| 18 | +# ------------------------------------------------------------------ |
| 19 | + |
| 20 | +import sys, os |
| 21 | +import subprocess |
| 22 | +import shutil |
| 23 | + |
| 24 | +SPHINX_MISSING = """ |
| 25 | +You must have Sphinx installed to use this script! |
| 26 | +
|
| 27 | +Go to http://www.sphinx-doc.org for information about installing and using |
| 28 | +Sphinx. |
| 29 | +""" |
| 30 | + |
| 31 | +FAILED_CHECKOUT = """ |
| 32 | +Couldn't clone repository. Please make sure that you have 'git' installed and |
| 33 | +that you can access GitHub repositories using SSH. |
| 34 | +""" |
| 35 | + |
| 36 | +# The file to load extra dependencies from. |
| 37 | +DEPENDENCIES_FILE = 'dependencies.txt' |
| 38 | +DEPENDENCIES_DIR = 'build_dependencies' |
| 39 | + |
| 40 | +BUILD_DIR = 'doc_build' |
| 41 | +OUTPUT_DIR = 'doc_output' |
| 42 | +SOURCE_DIR = 'doc_source' |
| 43 | + |
| 44 | + |
| 45 | +def check_and_remove_dir(dir_name): |
| 46 | + """Check to see if the named directory exists. If it does, then remove it. |
| 47 | + Throw an exception if the directory can't be removed.""" |
| 48 | + |
| 49 | + if os.path.exists(dir_name): |
| 50 | + print("Removing directory: " + dir_name) |
| 51 | + try: |
| 52 | + shutil.rmtree(dir_name) |
| 53 | + except: |
| 54 | + print("Couldn't remove " + dir_name) |
| 55 | + print("Remove this directory before building!") |
| 56 | + sys.exit(1) |
| 57 | + |
| 58 | + |
| 59 | +def copy_dir_contents_with_overwrite(input_dir_name, output_dir_name): |
| 60 | + """Copy the contents of a directory into another, overwriting files if they |
| 61 | + exist.""" |
| 62 | + |
| 63 | + # if output_dir_name isn't a location, make it so. |
| 64 | + if not os.path.exists(output_dir_name): |
| 65 | + os.makedirs(output_dir_name) |
| 66 | + |
| 67 | + dir_entries = os.listdir(input_dir_name) |
| 68 | + |
| 69 | + for dir_entry in dir_entries: |
| 70 | + input_path = os.path.join(input_dir_name, dir_entry) |
| 71 | + output_path = os.path.join(output_dir_name, dir_entry) |
| 72 | + |
| 73 | + if os.path.isdir(input_path): |
| 74 | + copy_dir_contents_with_overwrite(input_path, output_path) |
| 75 | + else: |
| 76 | + shutil.copyfile(input_path, output_path) |
| 77 | + |
| 78 | + |
| 79 | +def clone_and_copy_dependency(git_url, dep, subdir = ''): |
| 80 | + print("Getting content from dependency: " + git_url) |
| 81 | + try: |
| 82 | + subprocess.check_call(['git', 'clone', '--depth', '1', git_url, |
| 83 | + os.path.join(DEPENDENCIES_DIR, dep)]) |
| 84 | + except: |
| 85 | + print(FAILED_CHECKOUT) |
| 86 | + sys.exit(1) |
| 87 | + |
| 88 | + # copy the contents of the subdir into the build directory. |
| 89 | + path_to_copy = os.path.join(DEPENDENCIES_DIR, dep, subdir) |
| 90 | + copy_dir_contents_with_overwrite(path_to_copy, BUILD_DIR) |
| 91 | + |
| 92 | + |
| 93 | +def run(): |
| 94 | + # try to import requirements, and complain if they can't be found. |
| 95 | + try: |
| 96 | + from sphinx import version_info as sphinx_version |
| 97 | + print("Using Sphinx version %s.%s.%s" % sphinx_version[0:3]) |
| 98 | + except: |
| 99 | + print(SPHINX_MISSING) |
| 100 | + sys.exit(1) |
| 101 | + |
| 102 | + build_target = 'html' # by default |
| 103 | + cmd_switches = [] |
| 104 | + |
| 105 | + for arg in sys.argv[1:]: |
| 106 | + if arg.startswith('--'): |
| 107 | + cmd_switches.append(arg) |
| 108 | + else: |
| 109 | + # the only non-switch argument is the output format. |
| 110 | + build_target = arg |
| 111 | + |
| 112 | + print("Building '%s' target." % build_target) |
| 113 | + |
| 114 | + # |
| 115 | + # Step 0: empty the build dir if it's there. |
| 116 | + # |
| 117 | + check_and_remove_dir(BUILD_DIR) |
| 118 | + check_and_remove_dir(DEPENDENCIES_DIR) |
| 119 | + check_and_remove_dir(OUTPUT_DIR) |
| 120 | + |
| 121 | + # Step 1: Load any dependencies. Dependencies consist of a tuple: a git repository and a |
| 122 | + # subdirectory to copy. |
| 123 | + dependencies = [] |
| 124 | + if os.path.exists(DEPENDENCIES_FILE): |
| 125 | + os.mkdir(DEPENDENCIES_DIR) |
| 126 | + dep_num = 0 |
| 127 | + dep_file_contents = open(DEPENDENCIES_FILE, 'r').readlines() |
| 128 | + for line in dep_file_contents: |
| 129 | + # ignore comments |
| 130 | + bare_line = line.strip() |
| 131 | + if bare_line == '': |
| 132 | + continue |
| 133 | + elif not bare_line.startswith('#'): |
| 134 | + (git_url, subdir) = line.split() |
| 135 | + if subdir == None: |
| 136 | + subdir = '' |
| 137 | + clone_and_copy_dependency(git_url, str(dep_num), subdir) |
| 138 | + dep_num += 1 |
| 139 | + |
| 140 | + # |
| 141 | + # Step 2: copy the contents of SOURCE_DIR into the BUILD_DIR. |
| 142 | + # |
| 143 | + print("Copying doc sources from %s to %s" % (SOURCE_DIR, BUILD_DIR)) |
| 144 | + copy_dir_contents_with_overwrite(SOURCE_DIR, BUILD_DIR) |
| 145 | + |
| 146 | + # |
| 147 | + # Append the contents of any files in the 'build/_conf' directory to the |
| 148 | + # project's conf.py file (so that shared content can add commonly-used |
| 149 | + # extlinks, etc.). |
| 150 | + # |
| 151 | + conf_py_path = os.path.join(BUILD_DIR, 'conf.py') |
| 152 | + extra_conf_path = os.path.join(BUILD_DIR, '_conf') |
| 153 | + # first, open the conf.py file for appending... |
| 154 | + with open(conf_py_path, 'a') as conf_file: |
| 155 | + # now, add the contents of each file in alpha-sorted order. |
| 156 | + for filename in sorted(os.listdir(extra_conf_path)): |
| 157 | + print(" - %s" % filename) |
| 158 | + conf_file.write('# ** added by extra conf file: %s **\n' % filename) |
| 159 | + with open(os.path.join(extra_conf_path, filename), 'r') as extra_conf_file: |
| 160 | + conf_file.write(extra_conf_file.read()) |
| 161 | + conf_file.write('# ** end of content from %s **\n' % filename) |
| 162 | + |
| 163 | + # |
| 164 | + # Step 3: build the docs |
| 165 | + # |
| 166 | + print("Building documentation.") |
| 167 | + try: |
| 168 | + subprocess.check_call(['sphinx-build', '-b', build_target, BUILD_DIR, |
| 169 | + OUTPUT_DIR]) |
| 170 | + except: |
| 171 | + print(FAILED_CHECKOUT) |
| 172 | + sys.exit(1) |
| 173 | + |
| 174 | + # |
| 175 | + # Step 4: Clean up the build dir and shared content. |
| 176 | + # |
| 177 | + if '--noclean' not in cmd_switches: |
| 178 | + print("Cleaning up.") |
| 179 | + check_and_remove_dir(BUILD_DIR) |
| 180 | + check_and_remove_dir(DEPENDENCIES_DIR) |
| 181 | + |
| 182 | + print("Finished! You'll find the built docs in the '%s' directory." % |
| 183 | + OUTPUT_DIR) |
| 184 | + |
| 185 | + |
| 186 | +# If run from the command-line... |
| 187 | +if __name__ == '__main__': |
| 188 | + run() |
| 189 | + |
0 commit comments