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