e5d6b4a7fd50c1256d32bdd5db84c00dd7467e0e
[releng.git] / utils / docs-build.sh
1 #!/bin/bash -e
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/
11
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:-}
19
20 get_title_script="
21 import os
22 from docutils import core, nodes
23
24 try:
25     with open('index.rst', 'r') as file:
26         data = file.read()
27     doctree = core.publish_doctree(data,
28         settings_overrides={'report_level': 5,
29                             'halt_level': 5})
30     if isinstance(doctree[0], nodes.title):
31         title = doctree[0]
32     else:
33         for c in doctree.children:
34             if isinstance(c, nodes.section):
35                 title = c[0]
36                 break
37     print title.astext()
38 except:
39     print 'None'"
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'
47
48 function check_rst_doc() {
49     _src="$1"
50
51     if ! which doc8 > /dev/null ; then
52         echo "Error: 'doc8' not found. Exec 'sudo pip install doc8' first."
53         exit 1
54     fi
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")
61         echo
62         echo -e "$_msg\n$_errs"
63         echo
64         [[ -n "$GERRIT_COMMENT" ]] && echo -e "$_msg\n$_errs" >> "$GERRIT_COMMENT"
65     }
66 }
67
68 function add_html_notes() {
69     _src="$1"
70
71     find "$_src" -name '*.rst' | while read file
72     do
73         if grep -q -e ' _sha1_' "$file" ; then
74             # TODO: remove this, once old templates were removed from all repos.
75             echo
76             echo "Warn: '_sha1_' was found in [$file], use the latest document template."
77             echo "      See https://wiki.opnfv.org/documentation/tools ."
78             echo
79             sed -i "s/ _sha1_/ $git_sha1/g" "$file"
80         fi
81         sed -i -e "\$a\\\n..\n$html_notes" "$file"
82     done
83 }
84
85 function prepare_src_files() {
86     mkdir -p "$(dirname $SRC_DIR)"
87
88     [[ -e "$SRC_DIR" ]] && rm -rf "$SRC_DIR"
89     cp -r "$DOCS_DIR" "$SRC_DIR"
90     add_html_notes "$SRC_DIR"
91 }
92
93 function add_config() {
94     _conf="$1"
95     _param="$2"
96     _val="$3"
97
98     if ! grep -q -e "^$_param = " "$_conf" ; then
99         echo "Adding '$_param' into $_conf ..."
100         echo "$_param = $_val" >> "$_conf"
101     fi
102 }
103
104 function is_top_dir() {
105     [[ "$1" == "$DOCS_DIR" ]]
106 }
107
108 function generate_name_for_top_dir() {
109     for suffix in '' '.top' '.all' '.master' '_' '__' '___'
110     do
111         _name="$(basename $DOCS_DIR)$suffix"
112         [[ -e "$DOCS_DIR/$_name" ]] && continue
113         echo "$_name"
114         return
115     done
116
117     echo "Error: cannot find name for top directory [$DOCS_DIR]"
118     exit 1
119 }
120
121 function generate_name() {
122     _dir=$1
123
124     if is_top_dir "$_dir" ; then
125         _name=$(generate_name_for_top_dir $DOCS_DIR)
126     else
127         _name="${_dir#$DOCS_DIR/}"
128     fi
129     # Replace '/' by '_'
130     echo "${_name////_}"
131 }
132
133
134 check_rst_doc $DOCS_DIR
135
136 if [[ ! -d "$RELENG_DIR" ]] ; then
137     echo "Error: $RELENG_DIR dir not found. See https://wiki.opnfv.org/documentation/tools ."
138     exit 1
139 fi
140
141 prepare_src_files
142
143 find $DOCS_DIR -name $INDEX_RST -printf '%h\n' | while read dir
144 do
145     name=$(generate_name $dir)
146     if is_top_dir "$dir" ; then
147         src="$SRC_DIR"
148     else
149         src="$SRC_DIR/${dir#$DOCS_DIR/}"
150     fi
151     build="$BUILD_DIR/$name"
152     output="$OUTPUT_DIR/$name"
153     conf="$src/conf.py"
154
155     echo
156     echo "#################${dir//?/#}"
157     echo "Building DOCS in $dir"
158     echo "#################${dir//?/#}"
159     echo
160
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"
170
171     mkdir -p "$output"
172
173     sphinx-build -b html -t html -E "$src" "$output"
174
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.
179     {
180         sphinx-build -b latex -t pdf -E "$src" "$build" && \
181             make -C "$build" LATEXOPTS='--interaction=nonstopmode' all-pdf
182     } && {
183         mv "$build/$name.pdf" "$output"
184     } || {
185         msg="Error: PDF creation for $dir has failed, please fix source rst file(s)."
186         echo
187         echo "$msg"
188         echo
189         [[ -n "$GERRIT_COMMENT" ]] && echo "$msg" >> "$GERRIT_COMMENT"
190     }
191
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
197         (
198             cd $(dirname $tex)
199             pandoc $(basename $tex) -o $(basename $odt)
200         ) && {
201             mv $odt $output/
202         }|| {
203             msg="Error: ODT creation for $dir has failed."
204             echo
205             echo "$msg"
206             echo
207         }
208     else
209         echo "Warn: tex file and/or 'pandoc' are not found, skip ODT creation."
210     fi
211
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
217         #       docs directory.
218         (
219             cd $output
220             find . -type d -print | xargs -I d mkdir -p ../d
221             find . -type f -print | xargs -I f mv -b f ../f
222         )
223         rm -rf "$output"
224     fi
225
226 done