Adding coverage report to merge job
[releng.git] / utils / docs-build.sh
1 #!/bin/bash -e
2
3 export PATH=$PATH:/usr/local/bin/
4
5
6 SRC_DIR=${SRC_DIR:-docs}
7 INDEX_RST=${INDEX_RST:-index.rst}
8 BUILD_DIR=${BUILD_DIR:-build}
9 OUTPUT_DIR=${OUTPUT_DIR:-output}
10 RELENG_DIR=${RELENG_DIR:-releng}
11 GERRIT_COMMENT=${GERRIT_COMMENT:-}
12
13 get_title_script="
14 import os
15 from docutils import core, nodes
16
17 try:
18     with open('index.rst', 'r') as file:
19         data = file.read()
20     doctree = core.publish_doctree(data,
21         settings_overrides={'report_level': 5,
22                             'halt_level': 5})
23     if isinstance(doctree[0], nodes.title):
24         title = doctree[0]
25     else:
26         for c in doctree.children:
27             if isinstance(c, nodes.section):
28                 title = c[0]
29                 break
30     print title.astext()
31 except:
32     print 'None'"
33 revision="$(git rev-parse --short HEAD)"
34 rev_full="$(git rev-parse HEAD)"
35 version="$(git describe --abbrev=0 2> /dev/null || echo draft) ($revision)"
36 project="$(basename $(git rev-parse --show-toplevel))"
37 html_notes="\n    Revision: $rev_full\n\n    Build date: |today|"
38 default_conf='releng/docs/etc/conf.py'
39 opnfv_logo='releng/docs/etc/opnfv-logo.png'
40
41 function add_html_notes() {
42     _src="$1"
43     _dir="$2"
44
45     if grep -q -e ' _sha1_' "$_src"/*.rst ; then
46         # TODO: remove this, once old templates were removed from all repos.
47         echo
48         echo "Warn: '_sha1_' was found in $_dir , use the latest document template."
49         echo "      See https://wiki.opnfv.org/documentation/tools ."
50         echo
51         sed -i "s/ _sha1_/ $git_sha1/g" "$_src"/*.rst
52     fi
53     sed -i -e "\$a\\\n.. only:: html\n$html_notes" "$_src"/*.rst
54 }
55
56 function add_config() {
57     _conf="$1"
58     _param="$2"
59     _val="$3"
60
61     if ! grep -q -e "^$_param = " "$_conf" ; then
62         echo "Adding '$_param' into $_conf ..."
63         echo "$_param = $_val" >> "$_conf"
64     fi
65 }
66
67
68 if [[ ! -d "$RELENG_DIR" ]] ; then
69     echo "Error: $RELENG_DIR dir not found. See https://wiki.opnfv.org/documentation/tools ."
70     exit 1
71 fi
72
73 find $SRC_DIR -name $INDEX_RST -printf '%h\n' | while read dir
74 do
75     name="${dir##*/}"
76     src="$BUILD_DIR/src/$name"
77     build="$BUILD_DIR/$name"
78     output="$OUTPUT_DIR/$name"
79     conf="$src/conf.py"
80
81     echo
82     echo "#################${dir//?/#}"
83     echo "Building DOCS in $dir"
84     echo "#################${dir//?/#}"
85     echo
86
87     mkdir -p "$BUILD_DIR/src"
88     [[ -e "$src" ]] && rm -rf "$src"
89     cp -r "$dir" "$src"
90
91     add_html_notes "$src" "$dir"
92
93     [[ ! -f "$conf" ]] && cp "$default_conf" "$conf"
94     title=$(cd $src; python -c "$get_title_script")
95     latex_conf="[('index', '$name.tex', \"$title\", 'OPNFV', 'manual'),]"
96     add_config "$conf" 'latex_documents' "$latex_conf"
97     add_config "$conf" 'release' "u'$version'"
98     add_config "$conf" 'version' "u'$version'"
99     add_config "$conf" 'project' "u'$project'"
100     add_config "$conf" 'copyright' "u'$(date +%Y), OPNFV'"
101     cp -f $opnfv_logo "$src/opnfv-logo.png"
102
103     mkdir -p "$output"
104
105     sphinx-build -b html -t html -E "$src" "$output"
106
107     # Note: PDF creation may fail in project doc builds.
108     #       We allow this build job to be marked as succeeded with
109     #       failure in PDF creation, but leave message to fix it.
110     #       Any failure has to be fixed before OPNFV B release.
111     {
112         sphinx-build -b latex -t pdf -E "$src" "$build" && \
113             make -C "$build" LATEXOPTS='--interaction=nonstopmode' all-pdf
114     } && {
115         mv "$build/$name.pdf" "$output"
116     } || {
117         msg="Error: PDF creation for $dir has failed, please fix source rst file(s)."
118         echo
119         echo "$msg"
120         echo
121         [[ -n "$GERRIT_COMMENT" ]] && echo "$msg" >> "$GERRIT_COMMENT"
122     }
123
124 done