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 VENV_DIR=${VENV_DIR:-$BUILD_DIR/_venv}
18 RELENG_DIR=${RELENG_DIR:-releng}
19 GERRIT_COMMENT=${GERRIT_COMMENT:-}
23 from docutils import core, nodes
26 with open('index.rst', 'r') as file:
28 doctree = core.publish_doctree(data,
29 settings_overrides={'report_level': 5,
31 if isinstance(doctree[0], nodes.title):
34 for c in doctree.children:
35 if isinstance(c, nodes.section):
41 revision="$(git rev-parse --short HEAD)"
42 rev_full="$(git rev-parse HEAD)"
43 version="$(git describe --abbrev=0 2> /dev/null || echo draft) ($revision)"
44 project="$(basename $(git rev-parse --show-toplevel))"
45 html_notes=" Revision: $rev_full\n Build date: $(date -u +'%Y-%m-%d')"
46 default_conf='releng/docs/etc/conf.py'
47 opnfv_logo='releng/docs/etc/opnfv-logo.png'
49 function check_rst_doc() {
52 # Note: This check may fail in many jobs for building project docs, since
53 # the old sample has lines more than 120. We ignore failures on this
54 # check right now, but these have to be fixed before OPNFV B release.
55 _out=$(doc8 --max-line-length 240 --ignore D000 "$_src") || {
56 _msg='Warning: rst validation (doc8) has failed, please fix the following error(s).'
57 _errs=$(echo "$_out" | sed -n -e "/^$_src/s/^/ /p")
59 echo -e "$_msg\n$_errs"
61 [[ -n "$GERRIT_COMMENT" ]] && echo -e "$_msg\n$_errs" >> "$GERRIT_COMMENT"
65 function add_html_notes() {
68 find "$_src" -name '*.rst' | while read file
70 if grep -q -e ' _sha1_' "$file" ; then
71 # TODO: remove this, once old templates were removed from all repos.
73 echo "Warn: '_sha1_' was found in [$file], use the latest document template."
74 echo " See https://wiki.opnfv.org/documentation/tools ."
76 sed -i "s/ _sha1_/ $git_sha1/g" "$file"
78 sed -i -e "\$a\\\n..\n$html_notes" "$file"
82 function prepare_src_files() {
83 mkdir -p "$(dirname $SRC_DIR)"
85 [[ -e "$SRC_DIR" ]] && rm -rf "$SRC_DIR"
86 cp -r "$DOCS_DIR" "$SRC_DIR"
87 add_html_notes "$SRC_DIR"
90 function add_config() {
95 if ! grep -q -e "^$_param = " "$_conf" ; then
96 echo "Adding '$_param' into $_conf ..."
97 echo "$_param = $_val" >> "$_conf"
101 function is_top_dir() {
102 [[ "$1" == "$DOCS_DIR" ]]
105 function generate_name_for_top_dir() {
106 for suffix in '' '.top' '.all' '.master' '_' '__' '___'
108 _name="$(basename $DOCS_DIR)$suffix"
109 [[ -e "$DOCS_DIR/$_name" ]] && continue
114 echo "Error: cannot find name for top directory [$DOCS_DIR]"
118 function generate_name() {
121 if is_top_dir "$_dir" ; then
122 _name=$(generate_name_for_top_dir $DOCS_DIR)
124 _name="${_dir#$DOCS_DIR/}"
131 check_rst_doc $DOCS_DIR
133 if [[ ! -d "$RELENG_DIR" ]] ; then
134 echo "Error: $RELENG_DIR dir not found. See https://wiki.opnfv.org/documentation/tools ."
140 if ! which virtualenv > /dev/null ; then
141 echo "Error: 'virtualenv' not found. Exec 'sudo pip install virtualenv' first."
145 virtualenv "$VENV_DIR"
146 source "$VENV_DIR/bin/activate"
147 pip install -r "$RELENG_DIR/docs/etc/requirements.txt"
149 find $DOCS_DIR -name $INDEX_RST -printf '%h\n' | while read dir
151 name=$(generate_name $dir)
152 if is_top_dir "$dir" ; then
155 src="$SRC_DIR/${dir#$DOCS_DIR/}"
157 build="$BUILD_DIR/$name"
158 output="$OUTPUT_DIR/$name"
162 echo "#################${dir//?/#}"
163 echo "Building DOCS in $dir"
164 echo "#################${dir//?/#}"
167 [[ ! -f "$conf" ]] && cp "$default_conf" "$conf"
168 title=$(cd $src; python -c "$get_title_script")
169 latex_conf="[('index', '$name.tex', \"$title\", 'OPNFV', 'manual'),]"
170 add_config "$conf" 'latex_documents' "$latex_conf"
171 add_config "$conf" 'release' "u'$version'"
172 add_config "$conf" 'version' "u'$version'"
173 add_config "$conf" 'project' "u'$project'"
174 add_config "$conf" 'copyright' "u'$(date +%Y), OPNFV'"
175 cp -f $opnfv_logo "$src/opnfv-logo.png"
179 sphinx-build -b html -t html -E "$src" "$output"
181 # Note: PDF creation may fail in project doc builds.
182 # We allow this build job to be marked as succeeded with
183 # failure in PDF creation, but leave message to fix it.
184 # Any failure has to be fixed before OPNFV B release.
186 sphinx-build -b latex -t pdf -E "$src" "$build" && \
187 make -C "$build" LATEXOPTS='--interaction=nonstopmode' all-pdf
189 mv "$build/$name.pdf" "$output"
191 msg="Error: PDF creation for $dir has failed, please fix source rst file(s)."
195 [[ -n "$GERRIT_COMMENT" ]] && echo "$msg" >> "$GERRIT_COMMENT"
198 # TODO: failures in ODT creation should be handled error and
199 # cause 'exit 1' before OPNFV B release.
200 tex=$(find $build -name '*.tex' | head -1)
201 odt="${tex%.tex}.odt"
202 if [[ -e $tex ]] && which pandoc > /dev/null ; then
205 pandoc $(basename $tex) -o $(basename $odt)
209 msg="Error: ODT creation for $dir has failed."
215 echo "Warn: tex file and/or 'pandoc' are not found, skip ODT creation."
218 if is_top_dir "$dir" ; then
219 # NOTE: Having top level document (docs/index.rst) is not recommended.
220 # It may cause conflicts with other docs (mostly with HTML
221 # folders for contents in top level docs and other document
222 # folders). But, let's try merge of those contents into the top
226 find . -type d -print | xargs -I d mkdir -p ../d
227 find . -type f -print | xargs -I f mv -b f ../f