docs: fix docs-build.sh for top level document
[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     if is_top_dir "$dir" ; then
141         src="$SRC_DIR"
142     else
143         src="$SRC_DIR/${dir#$DOCS_DIR/}"
144     fi
145     build="$BUILD_DIR/$name"
146     output="$OUTPUT_DIR/$name"
147     conf="$src/conf.py"
148
149     echo
150     echo "#################${dir//?/#}"
151     echo "Building DOCS in $dir"
152     echo "#################${dir//?/#}"
153     echo
154
155     [[ ! -f "$conf" ]] && cp "$default_conf" "$conf"
156     title=$(cd $src; python -c "$get_title_script")
157     latex_conf="[('index', '$name.tex', \"$title\", 'OPNFV', 'manual'),]"
158     add_config "$conf" 'latex_documents' "$latex_conf"
159     add_config "$conf" 'release' "u'$version'"
160     add_config "$conf" 'version' "u'$version'"
161     add_config "$conf" 'project' "u'$project'"
162     add_config "$conf" 'copyright' "u'$(date +%Y), OPNFV'"
163     cp -f $opnfv_logo "$src/opnfv-logo.png"
164
165     mkdir -p "$output"
166
167     sphinx-build -b html -t html -E "$src" "$output"
168
169     # Note: PDF creation may fail in project doc builds.
170     #       We allow this build job to be marked as succeeded with
171     #       failure in PDF creation, but leave message to fix it.
172     #       Any failure has to be fixed before OPNFV B release.
173     {
174         sphinx-build -b latex -t pdf -E "$src" "$build" && \
175             make -C "$build" LATEXOPTS='--interaction=nonstopmode' all-pdf
176     } && {
177         mv "$build/$name.pdf" "$output"
178     } || {
179         msg="Error: PDF creation for $dir has failed, please fix source rst file(s)."
180         echo
181         echo "$msg"
182         echo
183         [[ -n "$GERRIT_COMMENT" ]] && echo "$msg" >> "$GERRIT_COMMENT"
184     }
185
186     # TODO: failures in ODT creation should be handled error and
187     #       cause 'exit 1' before OPNFV B release.
188     tex=$(find $build -name '*.tex' | head -1)
189     odt="${tex%.tex}.odt"
190     if [[ -e $tex ]] && which pandoc > /dev/null ; then
191         (
192             cd $(dirname $tex)
193             pandoc $(basename $tex) -o $(basename $odt)
194         ) && {
195             mv $odt $output/
196         }|| {
197             msg="Error: ODT creation for $dir has failed."
198             echo
199             echo "$msg"
200             echo
201         }
202     else
203         echo "Warn: tex file and/or 'pandoc' are not found, skip ODT creation."
204     fi
205
206     if is_top_dir "$dir" ; then
207         # NOTE: Having top level document (docs/index.rst) is not recommended.
208         #       It may cause conflicts with other docs (mostly with HTML
209         #       folders for contents in top level docs and other document
210         #       folders). But, let's try merge of those contents into the top
211         #       docs directory.
212         (
213             cd $output
214             find . -type d -print | xargs -I d mkdir -p ../d
215             find . -type f -print | xargs -I f mv -b f ../f
216         )
217         rm -rf "$output"
218     fi
219
220 done