3 export PATH=$PATH:/usr/local/bin/
6 DOCS_DIR=${DOCS_DIR:-docs}
7 INDEX_RST=${INDEX_RST:-index.rst}
8 BUILD_DIR=${BUILD_DIR:-docs_build}
9 OUTPUT_DIR=${OUTPUT_DIR:-docs_output}
10 SRC_DIR=${SRC_DIR:-$BUILD_DIR/_src}
11 RELENG_DIR=${RELENG_DIR:-releng}
12 GERRIT_COMMENT=${GERRIT_COMMENT:-}
16 from docutils import core, nodes
19 with open('index.rst', 'r') as file:
21 doctree = core.publish_doctree(data,
22 settings_overrides={'report_level': 5,
24 if isinstance(doctree[0], nodes.title):
27 for c in doctree.children:
28 if isinstance(c, nodes.section):
34 revision="$(git rev-parse --short HEAD)"
35 rev_full="$(git rev-parse HEAD)"
36 version="$(git describe --abbrev=0 2> /dev/null || echo draft) ($revision)"
37 project="$(basename $(git rev-parse --show-toplevel))"
38 html_notes="\n Revision: $rev_full\n\n Build date: |today|"
39 default_conf='releng/docs/etc/conf.py'
40 opnfv_logo='releng/docs/etc/opnfv-logo.png'
42 function check_rst_doc() {
45 if ! which doc8 > /dev/null ; then
46 echo "Error: 'doc8' not found. Exec 'sudo pip install doc8' first."
49 # Note: This check may fail in many jobs for building project docs, since
50 # the old sample has lines more than 120. We ignore failures on this
51 # check right now, but these have to be fixed before OPNFV B release.
52 _out=$(doc8 --max-line-length 120 --ignore D000 "$_src") || {
53 _msg='Error: rst validatino (doc8) has failed, please fix the following error(s).'
54 _errs=$(echo "$_out" | sed -n -e "/^$_src/s/^/ /p")
56 echo -e "$_msg\n$_errs"
58 [[ -n "$GERRIT_COMMENT" ]] && echo -e "$_msg\n$_errs" >> "$GERRIT_COMMENT"
62 function add_html_notes() {
65 find "$_src" -name '*.rst' | while read file
67 if grep -q -e ' _sha1_' "$file" ; then
68 # TODO: remove this, once old templates were removed from all repos.
70 echo "Warn: '_sha1_' was found in [$file], use the latest document template."
71 echo " See https://wiki.opnfv.org/documentation/tools ."
73 sed -i "s/ _sha1_/ $git_sha1/g" "$file"
75 sed -i -e "\$a\\\n.. only:: html\n$html_notes" "$file"
79 function prepare_src_files() {
80 mkdir -p "$(dirname $SRC_DIR)"
82 [[ -e "$SRC_DIR" ]] && rm -rf "$SRC_DIR"
83 cp -r "$DOCS_DIR" "$SRC_DIR"
84 add_html_notes "$SRC_DIR"
87 function add_config() {
92 if ! grep -q -e "^$_param = " "$_conf" ; then
93 echo "Adding '$_param' into $_conf ..."
94 echo "$_param = $_val" >> "$_conf"
98 function is_top_dir() {
99 [[ "$1" == "$DOCS_DIR" ]]
102 function generate_name_for_top_dir() {
103 for suffix in '' '.top' '.all' '.master' '_' '__' '___'
105 _name="$(basename $DOCS_DIR)$suffix"
106 [[ -e "$DOCS_DIR/$_name" ]] && continue
111 echo "Error: cannot find name for top directory [$DOCS_DIR]"
115 function generate_name() {
118 if is_top_dir "$_dir" ; then
119 _name=$(generate_name_for_top_dir $DOCS_DIR)
121 _name="${_dir#$DOCS_DIR/}"
128 check_rst_doc $DOCS_DIR
130 if [[ ! -d "$RELENG_DIR" ]] ; then
131 echo "Error: $RELENG_DIR dir not found. See https://wiki.opnfv.org/documentation/tools ."
137 find $DOCS_DIR -name $INDEX_RST -printf '%h\n' | while read dir
139 name=$(generate_name $dir)
140 src="$SRC_DIR/${dir#$DOCS_DIR/}"
141 build="$BUILD_DIR/$name"
142 output="$OUTPUT_DIR/$name"
146 echo "#################${dir//?/#}"
147 echo "Building DOCS in $dir"
148 echo "#################${dir//?/#}"
151 [[ ! -f "$conf" ]] && cp "$default_conf" "$conf"
152 title=$(cd $src; python -c "$get_title_script")
153 latex_conf="[('index', '$name.tex', \"$title\", 'OPNFV', 'manual'),]"
154 add_config "$conf" 'latex_documents' "$latex_conf"
155 add_config "$conf" 'release' "u'$version'"
156 add_config "$conf" 'version' "u'$version'"
157 add_config "$conf" 'project' "u'$project'"
158 add_config "$conf" 'copyright' "u'$(date +%Y), OPNFV'"
159 cp -f $opnfv_logo "$src/opnfv-logo.png"
163 sphinx-build -b html -t html -E "$src" "$output"
165 # Note: PDF creation may fail in project doc builds.
166 # We allow this build job to be marked as succeeded with
167 # failure in PDF creation, but leave message to fix it.
168 # Any failure has to be fixed before OPNFV B release.
170 sphinx-build -b latex -t pdf -E "$src" "$build" && \
171 make -C "$build" LATEXOPTS='--interaction=nonstopmode' all-pdf
173 mv "$build/$name.pdf" "$output"
175 msg="Error: PDF creation for $dir has failed, please fix source rst file(s)."
179 [[ -n "$GERRIT_COMMENT" ]] && echo "$msg" >> "$GERRIT_COMMENT"
182 # TODO: failures in ODT creation should be handled error and
183 # cause 'exit 1' before OPNFV B release.
184 tex=$(find $build -name '*.tex' | head -1)
185 odt="${tex%.tex}.odt"
186 if [[ -e $tex ]] && which pandoc > /dev/null ; then
189 pandoc $(basename $tex) -o $(basename $odt)
193 msg="Error: ODT creation for $dir has failed."
199 echo "Warn: tex file and/or 'pandoc' are not found, skip ODT creation."
202 if is_top_dir "$dir" ; then
203 # NOTE: Having top level document (docs/index.rst) is not recommended.
204 # It may cause conflicts with other docs (mostly with HTML
205 # folders for contents in top level docs and other document
206 # folders). But, let's try merge of those contents into the top
210 find . -type d -print | xargs -I d mkdir -p ../d
211 find . -type f -print | xargs -I f mv -b f ../f