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