Removing excess License info.
[opnfvdocs.git] / scripts / docs-build.sh
1 #!/bin/bash
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
11 DOCS_DIR=${DOCS_DIR:-docs}
12 INDEX_RST=${INDEX_RST:-index.rst}
13 BUILD_DIR=${BUILD_DIR:-docs_build}
14 OUTPUT_DIR=${OUTPUT_DIR:-docs_output}
15 SRC_DIR=${SRC_DIR:-$BUILD_DIR/_src}
16 VENV_DIR=${VENV_DIR:-$BUILD_DIR/_venv}
17 OPNFVDOCS_DIR=${OPNFVDOCS_DIR:-$BUILD_DIR/_opnfvdocs}
18 GERRIT_COMMENT=${GERRIT_COMMENT:-}
19
20 revision="$(git rev-parse --short HEAD)"
21 rev_full="$(git rev-parse HEAD)"
22 version="$(git describe --abbrev=0 2> /dev/null || echo draft) ($revision)"
23 project="$(basename $(git rev-parse --show-toplevel))"
24 html_notes="    Revision: $rev_full\n    Build date: $(date -u +'%Y-%m-%d')"
25 opnfv_logo="$OPNFVDOCS_DIR/etc/opnfv-logo.png"
26 copyright="$(date +%Y), OPNFV."
27 copyrightlong="$(date +%Y), OPNFV. Licenced under CC BY 4.0."
28
29 function check_rst_doc() {
30     _src="$1"
31
32     # Note: This check may fail in many jobs for building project docs, since
33     #       the old sample has lines more than 120. We ignore failures on this
34     #       check right now, but these have to be fixed before OPNFV B release.
35     _out=$(doc8 --max-line-length 240 --ignore D000 "$_src") || {
36         _msg='Warning: rst validation (doc8) has failed, please fix the following error(s).'
37         _errs=$(echo "$_out" | sed -n -e "/^$_src/s/^/    /p")
38         echo
39         echo -e "$_msg\n$_errs"
40         echo
41         if [ -n "$GERRIT_COMMENT" ]; then
42             echo -e "$_msg\n$_errs" >> "$GERRIT_COMMENT"
43         fi
44     }
45 }
46
47 function add_html_notes() {
48     _src="$1"
49
50     find "$_src" -name '*.rst' | while read file
51     do
52         if grep -q -e ' _sha1_' "$file" ; then
53             # TODO: remove this, once old templates were removed from all repos.
54             echo
55             echo "Warn: '_sha1_' was found in [$file], use the latest document template."
56             echo "      See http://artifacts.opnfv.org/opnfvdocs/docs/how-to-use-docs ."
57             echo
58             sed -i "s/ _sha1_/ $git_sha1/g" "$file"
59         fi
60         sed -i -e "\$a\\\n..\n$html_notes" "$file"
61     done
62 }
63
64 function prepare_src_files() {
65     mkdir -p "$(dirname $SRC_DIR)"
66
67     if [ -e "$SRC_DIR" ]; then
68         rm -rf "$SRC_DIR"
69     fi
70     cp -r "$DOCS_DIR" "$SRC_DIR"
71     add_html_notes "$SRC_DIR"
72 }
73
74 function add_config() {
75     _conf="$1"
76     _param="$2"
77     _val="$3"
78
79     if ! grep -q -e "^$_param = " "$_conf" ; then
80         echo "Adding '$_param' into $_conf ..."
81         echo "$_param = $_val" >> "$_conf"
82     fi
83 }
84
85 # Note: User can customize config for specific document by creating 'conf.py'
86 #       in the taeget document dir (e.g. docs/abc/conf.py). This config file does
87 #       not need to cover all config parameter, as all missing parameter will be
88 #       automatically filled by this function.
89 function prepare_config() {
90     _src="$1"
91     _name="$2"
92     _conf="$_src/conf.py"
93
94     touch "$_conf"
95
96     # default params
97     # Note: If you want to add a new sphinx extention here, you may need python
98     #       package for it (e.g. python package 'sphinxcontrib-httpdomain' is
99     #       required by 'sphinxcontrib.httpdomain'). If you need such python
100     #       package, add the name of the python package into the requirement
101     #       list 'docs/etc/requirements.txt' .
102     add_config "$_conf" 'extensions' \
103     "['sphinxcontrib.httpdomain', 'sphinx.ext.autodoc', 'sphinx.ext.viewcode', 'sphinx.ext.napoleon']"
104     add_config "$_conf" 'needs_sphinx' "'1.3'"
105     add_config "$_conf" 'numfig' "True"
106     add_config "$_conf" 'master_doc' "'index'"
107     add_config "$_conf" 'pygments_style' "'sphinx'"
108     add_config "$_conf" 'html_use_index' "False"
109     add_config "$_conf" 'html_last_updated_fmt' "'%b %d, %Y'"
110     add_config "$_conf" 'html_logo' "'opnfv-logo.png'"
111     add_config "$_conf" 'html_sidebars' \
112                         "{'**': ['globaltoc.html',
113                                  '$(cd $OPNFVDOCS_DIR; pwd)/etc/pagemenu.html',
114                                  'searchbox.html']}"
115
116     # genarated params
117     add_config "$_conf" 'release' "u'$version'"
118     add_config "$_conf" 'version' "u'$version'"
119     add_config "$_conf" 'project' "u'$project'"
120     add_config "$_conf" 'copyright' "u'$copyrightlong'"
121     add_config "$_conf" 'rst_epilog' "u'$html_notes'"
122
123     echo "sphinx config to be used:"
124     echo
125     sed -e "s/^/    /" "$_conf"
126     echo
127 }
128
129 function is_top_dir() {
130     [[ "$1" == "$DOCS_DIR" ]]
131 }
132
133 function generate_name_for_top_dir() {
134     for suffix in '' '.top' '.all' '.master' '_' '__' '___'
135     do
136         _name="$(basename $DOCS_DIR)$suffix"
137         [[ -e "$DOCS_DIR/$_name" ]] && continue
138         echo "$_name"
139         return
140     done
141
142     echo "Error: cannot find name for top directory [$DOCS_DIR]"
143     exit 1
144 }
145
146 function generate_name() {
147     _dir=$1
148
149     if is_top_dir "$_dir" ; then
150         _name=$(generate_name_for_top_dir $DOCS_DIR)
151     else
152         _name="${_dir#$DOCS_DIR/}"
153     fi
154     # Replace '/' by '_'
155     echo "${_name////_}"
156 }
157
158
159 if [[ ! -d "$OPNFVDOCS_DIR" ]] ; then
160     echo "Error: $OPNFVDOCS_DIR dir not found."
161     echo "See http://artifacts.opnfv.org/opnfvdocs/docs/how-to-use-docs ."
162     exit 1
163 fi
164
165 if ! which virtualenv > /dev/null ; then
166     echo "Error: 'virtualenv' not found. Exec 'sudo pip install virtualenv' first."
167     exit 1
168 fi
169
170 # workaround for doc8 error in python2.6
171 if [[ $(python -V 2>&1) == Python\ 2.6.* ]] && [ -e /usr/bin/python2.7 ]; then
172     echo "creating venv with Python 2.7 instead of Python 2.6.x ..."
173     virtualenv "$VENV_DIR" --python=/usr/bin/python2.7
174 else
175     virtualenv "$VENV_DIR"
176 fi
177
178 source "$VENV_DIR/bin/activate"
179 pip install -r "$OPNFVDOCS_DIR/etc/requirements.txt"
180
181 check_rst_doc $DOCS_DIR
182
183 prepare_src_files
184
185 if [ -e "$DOCS_DIR/pre-hook.sh" ]; then
186     source "$DOCS_DIR/pre-hook.sh"
187 fi
188
189 find $DOCS_DIR -name $INDEX_RST -printf '%h\n' | while read dir
190 do
191     name=$(generate_name $dir)
192     if is_top_dir "$dir" ; then
193         src="$SRC_DIR"
194     else
195         src="$SRC_DIR/${dir#$DOCS_DIR/}"
196     fi
197     build="$BUILD_DIR/$name"
198     output="$OUTPUT_DIR/$name"
199
200     echo
201     echo "#################${dir//?/#}"
202     echo "Building DOCS in $dir"
203     echo "#################${dir//?/#}"
204     echo
205
206     prepare_config "$src" "$name"
207     cp -f $opnfv_logo "$src/opnfv-logo.png"
208
209     mkdir -p "$output"
210
211     sphinx-build -b html -t html -E "$src" "$output"
212
213     {
214         sphinx-build -b singlehtml -t singlehtml -E "$src" "${output}-single"
215     } || {
216         msg="Error: Single HTML creation for $dir has failed."
217         echo
218         echo "$msg"
219         echo
220         if [ -n "$GERRIT_COMMENT" ]; then
221             echo "$msg" >> "$GERRIT_COMMENT"
222         fi
223     }
224
225     if is_top_dir "$dir" ; then
226         # NOTE: Having top level document (docs/index.rst) is not recommended.
227         #       It may cause conflicts with other docs (mostly with HTML
228         #       folders for contents in top level docs and other document
229         #       folders). But, let's try merge of those contents into the top
230         #       docs directory.
231         (
232             cd $output
233             find . -type d -print | xargs -I d mkdir -p ../d
234             find . -type f -print | xargs -I f mv -b f ../f
235         )
236         rm -rf "$output"
237     fi
238
239 done
240
241 deactivate