Add PDF creation in docu-build-new.sh
[releng.git] / utils / docu-build-new.sh
1 #!/bin/bash
2 set -e
3 set -o pipefail
4
5 export PATH=$PATH:/usr/local/bin/
6 git_sha1="$(git rev-parse HEAD)"
7
8 clean() {{
9 if [[ -d docs/output ]]; then
10 rm -rf docs/output
11 echo "cleaning up output directory"
12 fi
13 }}
14
15 trap clean EXIT TERM INT SIGTERM SIGHUP
16
17 #set git_sha1
18 files=()
19 while read -r -d ''; do
20   files+=("$REPLY")
21 done < <(find docs/ -type f -iname '*.rst' -print0)
22 for file in "${{files[@]}}"; do
23   sed -i "s/_sha1_/$git_sha1/g" $file
24 done
25
26 directories=()
27 while read -d $'\n'; do
28   directories+=("$REPLY")
29 done < <(find docs/ -name 'index.rst' -printf '%h\n' | sort -u )
30
31 for dir in "${{directories[@]}}"; do
32   _name="${{dir##*/}}"
33   _build="${{dir}}/build"
34   _output="docs/output/${{_name}}"
35   echo
36   echo "#################${{_name//?/#}}"
37   echo "Building DOCS in ${{_name}}"
38   echo "#################${{_name//?/#}}"
39   echo
40
41   mkdir -p "${{_output}}"
42
43   sphinx-build -b html -E -c docs/etc "${{dir}}" "${{_output}}"
44
45   sphinx-build -b latex -E -c docs/etc "${{dir}}" "${{_build}}"
46   make -C "${{_build}}" LATEXOPTS='--interaction=nonstopmode' all-pdf
47   mv "${{_build}}"/*.pdf "${{_output}}"
48
49 done
50
51 # NOTE: make sure source parameters for GS paths are not empty.
52 [[ $GERRIT_CHANGE_NUMBER =~ .+ ]]
53 [[ $GERRIT_PROJECT =~ .+ ]]
54 [[ $GERRIT_BRANCH =~ .+ ]]
55
56 gs_path_review="artifacts.opnfv.org/review/$GERRIT_CHANGE_NUMBER"
57
58 if [[ $GERRIT_BRANCH = "master" ]] ; then
59   gs_path_branch="artifacts.opnfv.org/$GERRIT_PROJECT"
60 else
61   gs_path_branch="artifacts.opnfv.org/$GERRIT_PROJECT/${{GERRIT_BRANCH##*/}}"
62 fi
63
64 for dir in "${{directories[@]}}"; do
65   echo
66   echo "#############################"
67   echo "UPLOADING DOCS in ${{dir##*/}}"
68   echo "#############################"
69   echo
70
71
72   if [[ $JOB_NAME =~ "verify" ]] ; then
73
74     #upload artifacts for verify job
75     gsutil cp -r docs/output/"${{dir##*/}}/" "gs://$gs_path_review/"
76
77     # post link to gerrit as comment
78     gerrit_comment="$(echo '"Document is available at 'http://$gs_path_review/"${{dir##*/}}"/index.html' for review"')"
79     echo "$gerrit_comment"
80     ssh -p 29418 gerrit.opnfv.org gerrit review -p $GERRIT_PROJECT -m \
81     "$gerrit_comment" $GERRIT_PATCHSET_REVISION
82
83     #set cache to 0
84     for x in $(gsutil ls gs://$gs_path_review/"${{dir##*/}}" | grep html);
85     do
86       gsutil setmeta -h "Content-Type:text/html" \
87       -h "Cache-Control:private, max-age=0, no-transform" \
88       "$x"
89     done
90
91   else
92
93     #upload artifacts for merge job
94     gsutil cp -r docs/output/"${{dir##*/}}" "gs://$gs_path_branch/docs/"
95     echo "Latest document is available at http://$gs_path_branch/docs/"${{dir##*/}}"/index.html"
96
97     #set cache to 0
98     for x in $(gsutil ls gs://$gs_path_branch/"${{dir}}" | grep html);
99     do
100       gsutil setmeta -h "Content-Type:text/html" \
101       -h "Cache-Control:private, max-age=0, no-transform" \
102       "$x"
103     done
104
105     #Clean up review when merging
106     if gsutil ls "gs://$gs_path_review" > /dev/null 2>&1 ; then
107       echo
108       echo "Deleting Out-of-dated Documents..."
109       gsutil rm -r "gs://$gs_path_review"
110     fi
111
112   fi
113
114 done