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 ##############################################################################
 
  13 DOCS_DIR=${DOCS_DIR:-docs}
 
  14 INDEX_RST=${INDEX_RST:-index.rst}
 
  15 BUILD_DIR=${BUILD_DIR:-docs_build}
 
  16 OUTPUT_DIR=${OUTPUT_DIR:-docs_output}
 
  17 SRC_DIR=${SRC_DIR:-$BUILD_DIR/_src}
 
  18 VENV_DIR=${VENV_DIR:-$BUILD_DIR/_venv}
 
  19 OPNFVDOCS_DIR=${OPNFVDOCS_DIR:-$BUILD_DIR/_opnfvdocs}
 
  20 GERRIT_COMMENT=${GERRIT_COMMENT:-}
 
  22 revision="$(git rev-parse --short HEAD)"
 
  23 rev_full="$(git rev-parse HEAD)"
 
  24 version="$(git tag | tail -1)"
 
  25 project="$(basename $(git rev-parse --show-toplevel))"
 
  26 html_notes="    Revision: $rev_full\n    Build date: $(date -u +'%Y-%m-%d')"
 
  27 opnfv_logo="$OPNFVDOCS_DIR/etc/opnfv-logo.png"
 
  28 copyright="$(date +%Y), OPNFV."
 
  29 copyrightlong="$(date +%Y), OPNFV. Licensed under CC BY 4.0."
 
  32 function set_error() {
 
  33     # TODO(yujunz) log detail errors
 
  34     error_count=$((error_count + 1))
 
  37 if [ "$(uname)" == "Darwin" ]; then
 
  38   # Override system $SED/$FIND with gnu $SED and gnu $FIND
 
  39   # If not found, install with
 
  40   # $ brew install gnu-sed findutils
 
  41   echo "macOS detected."
 
  49 function check_rst_doc() {
 
  52     # Note: This check may fail in many jobs for building project docs, since
 
  53     #       the old sample has lines more than 120. We ignore failures on this
 
  54     #       check right now, but these have to be fixed before OPNFV B release.
 
  55     _out=$(doc8 --max-line-length 240 --ignore D000 "$_src") || {
 
  56         _msg='Warning: rst validation (doc8) has failed, please fix the following error(s).'
 
  57         _errs=$(echo "$_out" | $SED -n -e "/^$_src/s/^/    /p")
 
  59         echo -e "$_msg\n$_errs"
 
  61         if [ -n "$GERRIT_COMMENT" ]; then
 
  62             echo -e "$_msg\n$_errs" >> "$GERRIT_COMMENT"
 
  67 function add_html_notes() {
 
  70     $FIND "$_src" -name '*.rst' | while read file
 
  72         if grep -q -e ' _sha1_' "$file" ; then
 
  73             # TODO: remove this, once old templates were removed from all repos.
 
  75             echo "Warn: '_sha1_' was found in [$file], use the latest document template."
 
  76             echo "      See http://artifacts.opnfv.org/opnfvdocs/docs/how-to-use-docs ."
 
  78             $SED -i "s/ _sha1_/ $git_sha1/g" "$file"
 
  80         $SED -i -e "\$a\\\n..\n$html_notes" "$file"
 
  84 function prepare_src_files() {
 
  85     mkdir -p "$(dirname $SRC_DIR)"
 
  87     if [ -e "$SRC_DIR" ]; then
 
  90     cp -r "$DOCS_DIR" "$SRC_DIR"
 
  91     add_html_notes "$SRC_DIR"
 
  94 function add_config() {
 
  99     if ! grep -q -e "^$_param = " "$_conf" ; then
 
 100         echo "Adding '$_param' into $_conf ..."
 
 101         echo "$_param = $_val" >> "$_conf"
 
 105 # Note: User can customize config for specific document by creating 'conf.py'
 
 106 #       in the taeget document dir (e.g. docs/abc/conf.py). This config file does
 
 107 #       not need to cover all config parameter, as all missing parameter will be
 
 108 #       automatically filled by this function.
 
 109 function prepare_config() {
 
 112     _conf="$_src/conf.py"
 
 117     # Note: If you want to add a new sphinx extention here, you may need python
 
 118     #       package for it (e.g. python package 'sphinxcontrib-httpdomain' is
 
 119     #       required by 'sphinxcontrib.httpdomain'). If you need such python
 
 120     #       package, add the name of the python package into the requirement
 
 121     #       list 'docs/etc/requirements.txt' .
 
 122     add_config "$_conf" 'extensions' \
 
 123     "['sphinxcontrib.httpdomain', 'sphinx.ext.autodoc', 'sphinx.ext.viewcode', 'sphinx.ext.napoleon']"
 
 124     add_config "$_conf" 'needs_sphinx' "'1.3'"
 
 125     add_config "$_conf" 'numfig' "True"
 
 126     add_config "$_conf" 'master_doc' "'index'"
 
 127     add_config "$_conf" 'pygments_style' "'sphinx'"
 
 128     add_config "$_conf" 'html_use_index' "False"
 
 129     add_config "$_conf" 'html_last_updated_fmt' "'%b %d, %Y'"
 
 130     add_config "$_conf" 'html_logo' "'opnfv-logo.png'"
 
 131     add_config "$_conf" 'html_sidebars' \
 
 132                         "{'**': ['globaltoc.html',
 
 133                                  '$(cd $OPNFVDOCS_DIR; pwd)/etc/pagemenu.html',
 
 137     add_config "$_conf" 'release' "u'$version'"
 
 138     add_config "$_conf" 'version' "u'$version'"
 
 139     add_config "$_conf" 'project' "u'$project'"
 
 140     add_config "$_conf" 'copyright' "u'$copyrightlong'"
 
 141     add_config "$_conf" 'rst_epilog' "u'$html_notes'"
 
 143     echo "sphinx config to be used:"
 
 145     $SED -e "s/^/    /" "$_conf"
 
 149 function is_top_dir() {
 
 150     [[ "$1" == "$DOCS_DIR" ]]
 
 153 function generate_name_for_top_dir() {
 
 154     for suffix in '' '.top' '.all' '.master' '_' '__' '___'
 
 156         _name="$(basename $DOCS_DIR)$suffix"
 
 157         [[ -e "$DOCS_DIR/$_name" ]] && continue
 
 162     echo "Error: cannot find name for top directory [$DOCS_DIR]"
 
 166 function generate_name() {
 
 169     if is_top_dir "$_dir" ; then
 
 170         _name=$(generate_name_for_top_dir $DOCS_DIR)
 
 172         _name="${_dir#$DOCS_DIR/}"
 
 179 if [[ ! -d "$OPNFVDOCS_DIR" ]] ; then
 
 180     echo "Error: $OPNFVDOCS_DIR dir not found."
 
 181     echo "See http://artifacts.opnfv.org/opnfvdocs/docs/how-to-use-docs ."
 
 185 if ! which virtualenv > /dev/null ; then
 
 186     echo "Error: 'virtualenv' not found. Exec 'sudo pip install virtualenv' first."
 
 190 # workaround for doc8 error in python2.6
 
 191 if [[ $(python -V 2>&1) == Python\ 2.6.* ]] && [ -e /usr/bin/python2.7 ]; then
 
 192     echo "creating venv with Python 2.7 instead of Python 2.6.x ..."
 
 193     virtualenv "$VENV_DIR" --python=/usr/bin/python2.7
 
 195     virtualenv "$VENV_DIR"
 
 198 source "$VENV_DIR/bin/activate"
 
 199 pip install -r "$OPNFVDOCS_DIR/etc/requirements.txt"
 
 201 check_rst_doc $DOCS_DIR
 
 205 if [ -e "$DOCS_DIR/pre-hook.sh" ]; then
 
 206     source "$DOCS_DIR/pre-hook.sh"
 
 209 $FIND $DOCS_DIR -name $INDEX_RST -printf '%h\n' | while read dir
 
 211     name=$(generate_name $dir)
 
 212     if is_top_dir "$dir" ; then
 
 215         src="$SRC_DIR/${dir#$DOCS_DIR/}"
 
 217     build="$BUILD_DIR/$name"
 
 218     output="$OUTPUT_DIR/$name"
 
 221     echo "#################${dir//?/#}"
 
 222     echo "Building DOCS in $dir"
 
 223     echo "#################${dir//?/#}"
 
 226     prepare_config "$src" "$name"
 
 227     cp -f $opnfv_logo "$src/opnfv-logo.png"
 
 232         sphinx-build -b singlehtml -t singlehtml -E "$src" "$output"
 
 234         msg="Error: HTML creation for $dir has failed."
 
 238         if [ -n "$GERRIT_COMMENT" ]; then
 
 239             echo "$msg" >> "$GERRIT_COMMENT"
 
 243     if is_top_dir "$dir" ; then
 
 244         # NOTE: Having top level document (docs/index.rst) is not recommended.
 
 245         #       It may cause conflicts with other docs (mostly with HTML
 
 246         #       folders for contents in top level docs and other document
 
 247         #       folders). But, let's try merge of those contents into the top
 
 251             $FIND . -type d -print | xargs -I d mkdir -p ../d
 
 252             $FIND . -type f -print | xargs -I f mv -b f ../f