55cb9f652e32d3e96b8131635e206ea38535f3cb
[releng.git] / utils / docs-build.sh
1 #!/bin/bash -e
2
3 export PATH=$PATH:/usr/local/bin/
4
5
6 DOCS_DIR=${DOCS_DIR:-docs}
7 INDEX_RST=${INDEX_RST:-index.rst}
8 BUILD_DIR=${BUILD_DIR:-docs_build}
9 OUTPUT_DIR=${OUTPUT_DIR:-docs_output}
10 SRC_DIR=${SRC_DIR:-$BUILD_DIR/_src}
11 RELENG_DIR=${RELENG_DIR:-releng}
12 GERRIT_COMMENT=${GERRIT_COMMENT:-}
13
14 get_title_script="
15 import os
16 from docutils import core, nodes
17
18 try:
19     with open('index.rst', 'r') as file:
20         data = file.read()
21     doctree = core.publish_doctree(data,
22         settings_overrides={'report_level': 5,
23                             'halt_level': 5})
24     if isinstance(doctree[0], nodes.title):
25         title = doctree[0]
26     else:
27         for c in doctree.children:
28             if isinstance(c, nodes.section):
29                 title = c[0]
30                 break
31     print title.astext()
32 except:
33     print 'None'"
34 revision="$(git rev-parse --short HEAD)"
35 rev_full="$(git rev-parse HEAD)"
36 version="$(git describe --abbrev=0 2> /dev/null || echo draft) ($revision)"
37 project="$(basename $(git rev-parse --show-toplevel))"
38 html_notes="\n    Revision: $rev_full\n\n    Build date: |today|"
39 default_conf='releng/docs/etc/conf.py'
40 opnfv_logo='releng/docs/etc/opnfv-logo.png'
41
42 function check_rst_doc() {
43     _src="$1"
44
45     if ! which doc8 > /dev/null ; then
46         echo "Error: 'doc8' not found. Exec 'sudo pip install doc8' first."
47         exit 1
48     fi
49     # Note: This check may fail in many jobs for building project docs, since
50     #       the old sample has lines more than 120. We ignore failures on this
51     #       check right now, but these have to be fixed before OPNFV B release.
52     _out=$(doc8 --max-line-length 120 --ignore D000 "$_src") || {
53         _msg='Error: rst validatino (doc8) has failed, please fix the following error(s).'
54         _errs=$(echo "$_out" | sed -n -e "/^$_src/s/^/    /p")
55         echo
56         echo -e "$_msg\n$_errs"
57         echo
58         [[ -n "$GERRIT_COMMENT" ]] && echo -e "$_msg\n$_errs" >> "$GERRIT_COMMENT"
59     }
60 }
61
62 function add_html_notes() {
63     _src="$1"
64
65     find "$_src" -name '*.rst' | while read file
66     do
67         if grep -q -e ' _sha1_' "$file" ; then
68             # TODO: remove this, once old templates were removed from all repos.
69             echo
70             echo "Warn: '_sha1_' was found in [$file], use the latest document template."
71             echo "      See https://wiki.opnfv.org/documentation/tools ."
72             echo
73             sed -i "s/ _sha1_/ $git_sha1/g" "$file"
74         fi
75         sed -i -e "\$a\\\n.. only:: html\n$html_notes" "$file"
76     done
77 }
78
79 function prepare_src_files() {
80     mkdir -p "$(dirname $SRC_DIR)"
81
82     [[ -e "$SRC_DIR" ]] && rm -rf "$SRC_DIR"
83     cp -r "$DOCS_DIR" "$SRC_DIR"
84     add_html_notes "$SRC_DIR"
85 }
86
87 function add_config() {
88     _conf="$1"
89     _param="$2"
90     _val="$3"
91
92     if ! grep -q -e "^$_param = " "$_conf" ; then
93         echo "Adding '$_param' into $_conf ..."
94         echo "$_param = $_val" >> "$_conf"
95     fi
96 }
97
98 function is_top_dir() {
99     [[ "$1" == "$DOCS_DIR" ]]
100 }
101
102 function generate_name_for_top_dir() {
103     for suffix in '' '.top' '.all' '.master' '_' '__' '___'
104     do
105         _name="$(basename $DOCS_DIR)$suffix"
106         [[ -e "$DOCS_DIR/$_name" ]] && continue
107         echo "$_name"
108         return
109     done
110
111     echo "Error: cannot find name for top directory [$DOCS_DIR]"
112     exit 1
113 }
114
115 function generate_name() {
116     _dir=$1
117
118     if is_top_dir "$_dir" ; then
119         _name=$(generate_name_for_top_dir $DOCS_DIR)
120     else
121         _name="${_dir#$DOCS_DIR/}"
122     fi
123     # Replace '/' by '_'
124     echo "${_name////_}"
125 }
126
127
128 check_rst_doc $DOCS_DIR
129
130 if [[ ! -d "$RELENG_DIR" ]] ; then
131     echo "Error: $RELENG_DIR dir not found. See https://wiki.opnfv.org/documentation/tools ."
132     exit 1
133 fi
134
135 prepare_src_files
136
137 find $DOCS_DIR -name $INDEX_RST -printf '%h\n' | while read dir
138 do
139     name=$(generate_name $dir)
140     src="$SRC_DIR/${dir#$DOCS_DIR/}"
141     build="$BUILD_DIR/$name"
142     output="$OUTPUT_DIR/$name"
143     conf="$src/conf.py"
144
145     echo
146     echo "#################${dir//?/#}"
147     echo "Building DOCS in $dir"
148     echo "#################${dir//?/#}"
149     echo
150
151     [[ ! -f "$conf" ]] && cp "$default_conf" "$conf"
152     title=$(cd $src; python -c "$get_title_script")
153     latex_conf="[('index', '$name.tex', \"$title\", 'OPNFV', 'manual'),]"
154     add_config "$conf" 'latex_documents' "$latex_conf"
155     add_config "$conf" 'release' "u'$version'"
156     add_config "$conf" 'version' "u'$version'"
157     add_config "$conf" 'project' "u'$project'"
158     add_config "$conf" 'copyright' "u'$(date +%Y), OPNFV'"
159     cp -f $opnfv_logo "$src/opnfv-logo.png"
160
161     mkdir -p "$output"
162
163     sphinx-build -b html -t html -E "$src" "$output"
164
165     # Note: PDF creation may fail in project doc builds.
166     #       We allow this build job to be marked as succeeded with
167     #       failure in PDF creation, but leave message to fix it.
168     #       Any failure has to be fixed before OPNFV B release.
169     {
170         sphinx-build -b latex -t pdf -E "$src" "$build" && \
171             make -C "$build" LATEXOPTS='--interaction=nonstopmode' all-pdf
172     } && {
173         mv "$build/$name.pdf" "$output"
174     } || {
175         msg="Error: PDF creation for $dir has failed, please fix source rst file(s)."
176         echo
177         echo "$msg"
178         echo
179         [[ -n "$GERRIT_COMMENT" ]] && echo "$msg" >> "$GERRIT_COMMENT"
180     }
181
182     # TODO: failures in ODT creation should be handled error and
183     #       cause 'exit 1' before OPNFV B release.
184     tex=$(find $build -name '*.tex' | head -1)
185     odt=$(basename "${tex%.tex}.odt")
186     if [[ -e $tex ]] && which pandoc > /dev/null ; then
187         pandoc $tex -o $output/$odt || {
188             msg="Error: ODT creation for $dir has failed."
189             echo
190             echo "$msg"
191             echo
192         }
193     else
194         echo "Warn: tex file and/or 'pandoc' are not found, skip ODT creation."
195     fi
196
197     if is_top_dir "$dir" ; then
198         # NOTE: Having top level document (docs/index.rst) is not recommended.
199         #       It may cause conflicts with other docs (mostly with HTML
200         #       folders for contents in top level docs and other document
201         #       folders). But, let's try merge of those contents into the top
202         #       docs directory.
203         (
204             cd $output
205             find . -type d -print | xargs -I d mkdir -p ../d
206             find . -type f -print | xargs -I f mv -b f ../f
207         )
208         rm -rf "$output"
209     fi
210
211 done