Docs: Support out-of-source reference-API builds
[blender.git] / doc / python_api / sphinx_doc_gen.sh
1 #!/bin/sh
2 # run from the blender source dir
3 #   bash doc/python_api/sphinx_doc_gen.sh
4 # ssh upload means you need an account on the server
5
6 # ----------------------------------------------------------------------------
7 # Upload vars
8
9 # disable for testing
10 DO_UPLOAD=true
11 DO_EXE_BLENDER=true
12 DO_OUT_HTML=true
13 DO_OUT_HTML_ZIP=true
14 DO_OUT_PDF=false
15
16 if [ -z $BLENDER_BIN ] ; then
17         BLENDER_BIN="./blender.bin"
18 fi
19
20 if [ "$1" == "" ] ; then
21         echo "Expected a single argument for the username on blender.org, skipping upload step!"
22         DO_UPLOAD=false
23 else
24         SSH_USER=$1
25         SSH_HOST=$SSH_USER"@blender.org"
26         SSH_UPLOAD="/data/www/vhosts/www.blender.org/api" # blender_python_api_VERSION, added after
27 fi
28
29
30 # ----------------------------------------------------------------------------
31 # Blender Version & Info
32
33 # 'Blender 2.53 (sub 1) Build' --> '2_53_1' as a shell script.
34 # "_".join(str(v) for v in bpy.app.version)
35 # custom blender vars
36 blender_srcdir=$(dirname -- $0)/../..
37 blender_version_header="$blender_srcdir/source/blender/blenkernel/BKE_blender_version.h"
38 blender_version=$(grep "BLENDER_VERSION\s" "$blender_version_header" | awk '{print $3}')
39 blender_version_char=$(grep "BLENDER_VERSION_CHAR\s" "$blender_version_header" | awk '{print $3}')
40 blender_version_cycle=$(grep "BLENDER_VERSION_CYCLE\s" "$blender_version_header" | awk '{print $3}')
41 blender_subversion=$(grep "BLENDER_SUBVERSION\s" "$blender_version_header" | awk '{print $3}')
42 unset blender_version_header
43
44 if [ "$blender_version_cycle" = "release" ] ; then
45         BLENDER_VERSION=$(expr $blender_version / 100)_$(expr $blender_version % 100)$blender_version_char"_release"
46 else
47         BLENDER_VERSION=$(expr $blender_version / 100)_$(expr $blender_version % 100)_$blender_subversion
48 fi
49
50 SSH_UPLOAD_FULL=$SSH_UPLOAD/"blender_python_api_"$BLENDER_VERSION
51
52 SPHINXBASE=doc/python_api
53
54 SPHINX_WORKDIR="$(mktemp --directory --suffix=.sphinx)"
55
56
57 # ----------------------------------------------------------------------------
58 # Generate reStructuredText (blender/python only)
59
60 if $DO_EXE_BLENDER ; then
61         # dont delete existing docs, now partial updates are used for quick builds.
62         $BLENDER_BIN \
63                 --background \
64                 -noaudio \
65                 --factory-startup \
66                 --python-exit-code 1 \
67                 --python $SPHINXBASE/sphinx_doc_gen.py \
68                 -- \
69                 --output=$SPHINX_WORKDIR
70
71
72         if (($? != 0)) ; then
73                 echo "Generating documentation failed, aborting"
74                 exit 1
75         fi
76 fi
77
78 # ----------------------------------------------------------------------------
79 # Generate HTML (sphinx)
80
81 if $DO_OUT_HTML ; then
82         # sphinx-build -n -b html $SPHINX_WORKDIR/sphinx-in $SPHINX_WORKDIR/sphinx-out
83
84         # annoying bug in sphinx makes it very slow unless we do this. should report.
85         cd $SPHINX_WORKDIR
86         sphinx-build -b html sphinx-in sphinx-out
87
88         # XXX, saves space on upload and zip, should move HTML outside
89         # and zip up there, for now this is OK
90         rm -rf sphinx-out/.doctrees
91
92         # incase we have a zip already
93         rm -f blender_python_reference_$BLENDER_VERSION.zip
94
95         # ------------------------------------------------------------------------
96         # ZIP the HTML dir for upload
97
98         if $DO_OUT_HTML_ZIP ; then
99                 # lame, temp rename dir
100                 mv sphinx-out blender_python_reference_$BLENDER_VERSION
101                 zip -r -9 blender_python_reference_$BLENDER_VERSION.zip blender_python_reference_$BLENDER_VERSION
102                 mv blender_python_reference_$BLENDER_VERSION sphinx-out
103         fi
104
105         cd -
106 fi
107
108
109 # ----------------------------------------------------------------------------
110 # Generate PDF (sphinx/laytex)
111
112 if $DO_OUT_PDF ; then
113         cd $SPHINX_WORKDIR
114         sphinx-build -n -b latex $SPHINX_WORKDIR/sphinx-in $SPHINX_WORKDIR/sphinx-out
115         make -C $SPHINX_WORKDIR/sphinx-out
116         mv $SPHINX_WORKDIR/sphinx-out/contents.pdf \
117            $SPHINX_WORKDIR/sphinx-out/blender_python_reference_$BLENDER_VERSION.pdf
118 fi
119
120 # ----------------------------------------------------------------------------
121 # Upload to blender servers, comment this section for testing
122
123 if $DO_UPLOAD ; then
124
125         cp $SPHINX_WORKDIR/sphinx-out/contents.html $SPHINX_WORKDIR/sphinx-out/index.html
126         ssh $SSH_USER@blender.org 'rm -rf '$SSH_UPLOAD_FULL'/*'
127         rsync --progress -ave "ssh -p 22" $SPHINX_WORKDIR/sphinx-out/* $SSH_HOST:$SSH_UPLOAD_FULL/
128
129         ## symlink the dir to a static URL
130         #ssh $SSH_USER@blender.org 'rm '$SSH_UPLOAD'/250PythonDoc && ln -s '$SSH_UPLOAD_FULL' '$SSH_UPLOAD'/250PythonDoc'
131         if [ "$blender_version_cycle" = "release" ] ; then
132                 ssh $SSH_USER@blender.org 'rm '$SSH_UPLOAD'/blender_python_api_current && ln -s '$SSH_UPLOAD_FULL' '$SSH_UPLOAD'/blender_python_api_current'
133         fi
134
135         # better redirect
136         ssh $SSH_USER@blender.org 'echo "<html><head><title>Redirecting...</title><meta http-equiv=\"REFRESH\" content=\"0;url=../blender_python_api_'$BLENDER_VERSION'/\"></head><body>Redirecting...</body></html>" > '$SSH_UPLOAD'/250PythonDoc/index.html'
137
138         # redirect for release only so wiki can point here
139         if [ "$blender_version_cycle" = "release" ] ; then
140                 ssh $SSH_USER@blender.org 'echo "<html><head><title>Redirecting...</title><meta http-equiv=\"REFRESH\" content=\"0;url=../blender_python_api_'$BLENDER_VERSION'/\"></head><body>Redirecting...</body></html>" > '$SSH_UPLOAD'/blender_python_api/index.html'
141         fi
142
143         if $DO_OUT_PDF ; then
144                 # rename so local PDF has matching name.
145                 rsync --progress -ave "ssh -p 22" \
146                       $SPHINX_WORKDIR/sphinx-out/blender_python_reference_$BLENDER_VERSION.pdf \
147                       $SSH_HOST:$SSH_UPLOAD_FULL/blender_python_reference_$BLENDER_VERSION.pdf
148         fi
149
150         if $DO_OUT_HTML_ZIP ; then
151                 rsync --progress -ave "ssh -p 22" \
152                       $SPHINX_WORKDIR/blender_python_reference_$BLENDER_VERSION.zip \
153                       $SSH_HOST:$SSH_UPLOAD_FULL/blender_python_reference_$BLENDER_VERSION.zip
154         fi
155
156 fi
157
158
159 # ----------------------------------------------------------------------------
160 # Print some useful text
161
162 echo ""
163 echo "Finished! view the docs from: "
164 if $DO_OUT_HTML ; then echo "  html:" $SPHINX_WORKDIR/sphinx-out/contents.html ; fi
165 if $DO_OUT_PDF ; then  echo "   pdf:" $SPHINX_WORKDIR/sphinx-out/blender_python_reference_$BLENDER_VERSION.pdf ; fi