Allow doc-build.sh to ignore D000 of doc8
[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 check_rst_doc() {
42     _src="$1"
43
44     if ! which doc8 > /dev/null ; then
45         echo "Error: 'doc8' not found. Exec 'sudo pip install doc8' first."
46         exit 1
47     fi
48     # Note: This check may fail in many jobs for building project docs, since
49     #       the old sample has lines more than 120. We ignore failures on this
50     #       check right now, but these have to be fixed before OPNFV B release.
51     _out=$(doc8 --max-line-length 120 --ignore D000 "$_src") || {
52         _msg='Error: rst validatino (doc8) has failed, please fix the following error(s).'
53         _errs=$(echo "$_out" | sed -n -e "/^$_src/s/^/    /p")
54         echo
55         echo -e "$_msg\n$_errs"
56         echo
57         [[ -n "$GERRIT_COMMENT" ]] && echo -e "$_msg\n$_errs" >> "$GERRIT_COMMENT"
58     }
59 }
60
61 function add_html_notes() {
62     _src="$1"
63     _dir="$2"
64
65     if grep -q -e ' _sha1_' "$_src"/*.rst ; then
66         # TODO: remove this, once old templates were removed from all repos.
67         echo
68         echo "Warn: '_sha1_' was found in $_dir , use the latest document template."
69         echo "      See https://wiki.opnfv.org/documentation/tools ."
70         echo
71         sed -i "s/ _sha1_/ $git_sha1/g" "$_src"/*.rst
72     fi
73     sed -i -e "\$a\\\n.. only:: html\n$html_notes" "$_src"/*.rst
74 }
75
76 function add_config() {
77     _conf="$1"
78     _param="$2"
79     _val="$3"
80
81     if ! grep -q -e "^$_param = " "$_conf" ; then
82         echo "Adding '$_param' into $_conf ..."
83         echo "$_param = $_val" >> "$_conf"
84     fi
85 }
86
87 function is_top_dir() {
88     [[ "$1" == "$SRC_DIR" ]]
89 }
90
91 function generate_name_for_top_dir() {
92     for suffix in '' '.top' '.all' '.master' '_' '__' '___'
93     do
94         _name="$(basename $SRC_DIR)$suffix"
95         [[ -e "$SRC_DIR/$_name" ]] && continue
96         echo "$_name"
97         return
98     done
99
100     echo "Error: cannot find name for top directory [$SRC_DIR]"
101     exit 1
102 }
103
104 function generate_name() {
105     _dir=$1
106
107     if is_top_dir "$_dir" ; then
108         _name=$(generate_name_for_top_dir $SRC_DIR)
109     else
110         _name="${_dir#$SRC_DIR/}"
111     fi
112     # Replace '/' by '_'
113     echo "${_name////_}"
114 }
115
116 check_rst_doc $SRC_DIR
117
118 if [[ ! -d "$RELENG_DIR" ]] ; then
119     echo "Error: $RELENG_DIR dir not found. See https://wiki.opnfv.org/documentation/tools ."
120     exit 1
121 fi
122
123 find $SRC_DIR -name $INDEX_RST -printf '%h\n' | while read dir
124 do
125     name=$(generate_name $dir)
126     src="$BUILD_DIR/src/$name"
127     build="$BUILD_DIR/$name"
128     output="$OUTPUT_DIR/$name"
129     conf="$src/conf.py"
130
131     echo
132     echo "#################${dir//?/#}"
133     echo "Building DOCS in $dir"
134     echo "#################${dir//?/#}"
135     echo
136
137     mkdir -p "$BUILD_DIR/src"
138     [[ -e "$src" ]] && rm -rf "$src"
139     cp -r "$dir" "$src"
140
141     add_html_notes "$src" "$dir"
142
143     [[ ! -f "$conf" ]] && cp "$default_conf" "$conf"
144     title=$(cd $src; python -c "$get_title_script")
145     latex_conf="[('index', '$name.tex', \"$title\", 'OPNFV', 'manual'),]"
146     add_config "$conf" 'latex_documents' "$latex_conf"
147     add_config "$conf" 'release' "u'$version'"
148     add_config "$conf" 'version' "u'$version'"
149     add_config "$conf" 'project' "u'$project'"
150     add_config "$conf" 'copyright' "u'$(date +%Y), OPNFV'"
151     cp -f $opnfv_logo "$src/opnfv-logo.png"
152
153     mkdir -p "$output"
154
155     sphinx-build -b html -t html -E "$src" "$output"
156
157     # Note: PDF creation may fail in project doc builds.
158     #       We allow this build job to be marked as succeeded with
159     #       failure in PDF creation, but leave message to fix it.
160     #       Any failure has to be fixed before OPNFV B release.
161     {
162         sphinx-build -b latex -t pdf -E "$src" "$build" && \
163             make -C "$build" LATEXOPTS='--interaction=nonstopmode' all-pdf
164     } && {
165         mv "$build/$name.pdf" "$output"
166     } || {
167         msg="Error: PDF creation for $dir has failed, please fix source rst file(s)."
168         echo
169         echo "$msg"
170         echo
171         [[ -n "$GERRIT_COMMENT" ]] && echo "$msg" >> "$GERRIT_COMMENT"
172     }
173
174     if is_top_dir "$dir" ; then
175         mv "$output"/* "$OUTPUT_DIR"/
176         rm -rf "$output"
177     fi
178
179 done