2 # SPDX-license-identifier: Apache-2.0
3 ##############################################################################
4 # Copyright (c) 2016 NEC and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10 export PATH=$PATH:/usr/local/bin/
12 DOCS_DIR=${DOCS_DIR:-docs}
13 INDEX_RST=${INDEX_RST:-index.rst}
14 BUILD_DIR=${BUILD_DIR:-docs_build}
15 OUTPUT_DIR=${OUTPUT_DIR:-docs_output}
16 SRC_DIR=${SRC_DIR:-$BUILD_DIR/_src}
17 RELENG_DIR=${RELENG_DIR:-releng}
18 GERRIT_COMMENT=${GERRIT_COMMENT:-}
22 from docutils import core, nodes
25 with open('index.rst', 'r') as file:
27 doctree = core.publish_doctree(data,
28 settings_overrides={'report_level': 5,
30 if isinstance(doctree[0], nodes.title):
33 for c in doctree.children:
34 if isinstance(c, nodes.section):
40 revision="$(git rev-parse --short HEAD)"
41 rev_full="$(git rev-parse HEAD)"
42 version="$(git describe --abbrev=0 2> /dev/null || echo draft) ($revision)"
43 project="$(basename $(git rev-parse --show-toplevel))"
44 html_notes="\n Revision: $rev_full\n\n Build date: |today|"
45 default_conf='releng/docs/etc/conf.py'
46 opnfv_logo='releng/docs/etc/opnfv-logo.png'
48 function check_rst_doc() {
51 if ! which doc8 > /dev/null ; then
52 echo "Error: 'doc8' not found. Exec 'sudo pip install doc8' first."
55 # Note: This check may fail in many jobs for building project docs, since
56 # the old sample has lines more than 120. We ignore failures on this
57 # check right now, but these have to be fixed before OPNFV B release.
58 _out=$(doc8 --max-line-length 240 --ignore D000 "$_src") || {
59 _msg='Warning: rst validation (doc8) has failed, please fix the following error(s).'
60 _errs=$(echo "$_out" | sed -n -e "/^$_src/s/^/ /p")
62 echo -e "$_msg\n$_errs"
64 [[ -n "$GERRIT_COMMENT" ]] && echo -e "$_msg\n$_errs" >> "$GERRIT_COMMENT"
68 function add_html_notes() {
71 find "$_src" -name '*.rst' | while read file
73 if grep -q -e ' _sha1_' "$file" ; then
74 # TODO: remove this, once old templates were removed from all repos.
76 echo "Warn: '_sha1_' was found in [$file], use the latest document template."
77 echo " See https://wiki.opnfv.org/documentation/tools ."
79 sed -i "s/ _sha1_/ $git_sha1/g" "$file"
81 sed -i -e "\$a\\\n.. only:: html\n$html_notes" "$file"
85 function prepare_src_files() {
86 mkdir -p "$(dirname $SRC_DIR)"
88 [[ -e "$SRC_DIR" ]] && rm -rf "$SRC_DIR"
89 cp -r "$DOCS_DIR" "$SRC_DIR"
90 add_html_notes "$SRC_DIR"
93 function add_config() {
98 if ! grep -q -e "^$_param = " "$_conf" ; then
99 echo "Adding '$_param' into $_conf ..."
100 echo "$_param = $_val" >> "$_conf"
104 function is_top_dir() {
105 [[ "$1" == "$DOCS_DIR" ]]
108 function generate_name_for_top_dir() {
109 for suffix in '' '.top' '.all' '.master' '_' '__' '___'
111 _name="$(basename $DOCS_DIR)$suffix"
112 [[ -e "$DOCS_DIR/$_name" ]] && continue
117 echo "Error: cannot find name for top directory [$DOCS_DIR]"
121 function generate_name() {
124 if is_top_dir "$_dir" ; then
125 _name=$(generate_name_for_top_dir $DOCS_DIR)
127 _name="${_dir#$DOCS_DIR/}"
134 check_rst_doc $DOCS_DIR
136 if [[ ! -d "$RELENG_DIR" ]] ; then
137 echo "Error: $RELENG_DIR dir not found. See https://wiki.opnfv.org/documentation/tools ."
143 find $DOCS_DIR -name $INDEX_RST -printf '%h\n' | while read dir
145 name=$(generate_name $dir)
146 if is_top_dir "$dir" ; then
149 src="$SRC_DIR/${dir#$DOCS_DIR/}"
151 build="$BUILD_DIR/$name"
152 output="$OUTPUT_DIR/$name"
156 echo "#################${dir//?/#}"
157 echo "Building DOCS in $dir"
158 echo "#################${dir//?/#}"
161 [[ ! -f "$conf" ]] && cp "$default_conf" "$conf"
162 title=$(cd $src; python -c "$get_title_script")
163 latex_conf="[('index', '$name.tex', \"$title\", 'OPNFV', 'manual'),]"
164 add_config "$conf" 'latex_documents' "$latex_conf"
165 add_config "$conf" 'release' "u'$version'"
166 add_config "$conf" 'version' "u'$version'"
167 add_config "$conf" 'project' "u'$project'"
168 add_config "$conf" 'copyright' "u'$(date +%Y), OPNFV'"
169 cp -f $opnfv_logo "$src/opnfv-logo.png"
173 sphinx-build -b html -t html -E "$src" "$output"
175 # Note: PDF creation may fail in project doc builds.
176 # We allow this build job to be marked as succeeded with
177 # failure in PDF creation, but leave message to fix it.
178 # Any failure has to be fixed before OPNFV B release.
180 sphinx-build -b latex -t pdf -E "$src" "$build" && \
181 make -C "$build" LATEXOPTS='--interaction=nonstopmode' all-pdf
183 mv "$build/$name.pdf" "$output"
185 msg="Error: PDF creation for $dir has failed, please fix source rst file(s)."
189 [[ -n "$GERRIT_COMMENT" ]] && echo "$msg" >> "$GERRIT_COMMENT"
192 # TODO: failures in ODT creation should be handled error and
193 # cause 'exit 1' before OPNFV B release.
194 tex=$(find $build -name '*.tex' | head -1)
195 odt="${tex%.tex}.odt"
196 if [[ -e $tex ]] && which pandoc > /dev/null ; then
199 pandoc $(basename $tex) -o $(basename $odt)
203 msg="Error: ODT creation for $dir has failed."
209 echo "Warn: tex file and/or 'pandoc' are not found, skip ODT creation."
212 if is_top_dir "$dir" ; then
213 # NOTE: Having top level document (docs/index.rst) is not recommended.
214 # It may cause conflicts with other docs (mostly with HTML
215 # folders for contents in top level docs and other document
216 # folders). But, let's try merge of those contents into the top
220 find . -type d -print | xargs -I d mkdir -p ../d
221 find . -type f -print | xargs -I f mv -b f ../f