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