Tools used by OpenStack Documentation
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

219 lines
6.8 KiB

  1. #!/bin/bash -xe
  2. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  3. # not use this file except in compliance with the License. You may obtain
  4. # a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software
  9. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  10. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  11. # License for the specific language governing permissions and limitations
  12. # under the License.
  13. FIRSTAPP_TAGS="libcloud dotnet fog openstacksdk pkgcloud shade"
  14. # This marker is needed for Infra publishing and needs to go into the
  15. # root directory of each translated manual as file ".root-marker".
  16. MARKER_TEXT="Project: $ZUUL_PROJECT Ref: $ZUUL_BRANCH Build: $ZUUL_UUID Revision: $ZUUL_NEWREF"
  17. function build_rst {
  18. language=$1
  19. book=$2
  20. local ret
  21. # First build all the single po files
  22. # Note that we need to run inside a venv since the venv we are run in
  23. # uses SitePackages=True and we have to install Sphinx in the venv
  24. # together with openstackdocstheme. With SitePackages, the global Sphinx
  25. # is used and that will not work with a local openstackdocstheme installed.
  26. TAG=""
  27. # We need to extract all strings, so add all supported tags
  28. if [ ${book} = "firstapp" ] ; then
  29. TAG="-t libcloud -t fog -t dotnet -t openstacksdk -t pkgcloud -t shade"
  30. fi
  31. COMMON="common"
  32. LOCALE_DIR="${DOC_DIR}${book}/source/locale/"
  33. COMMON_DIR="${DOC_DIR}${COMMON}/source/locale/"
  34. tox -evenv -- sphinx-build -q -E -W -b gettext $TAG \
  35. ${DOC_DIR}${book}/source/ ${LOCALE_DIR}
  36. # Merge the common po file
  37. if [[ -e ${COMMON_DIR}${language}/LC_MESSAGES/${COMMON}.po ]] ; then
  38. msgcat --use-first -o ${LOCALE_DIR}${language}/${book}.po \
  39. ${LOCALE_DIR}${language}/LC_MESSAGES/${book}.po \
  40. ${COMMON_DIR}${language}/LC_MESSAGES/${COMMON}.po
  41. mv -f ${LOCALE_DIR}${language}/${book}.po \
  42. ${LOCALE_DIR}${language}/LC_MESSAGES/${book}.po
  43. fi
  44. # Now run msgmerge on all files
  45. for f in ${LOCALE_DIR}*.pot ; do
  46. # Skip the master file
  47. if [ $f = "${LOCALE_DIR}${book}.pot" ] ; then
  48. continue
  49. fi
  50. bf=$(basename $f)
  51. # Remove .pot
  52. bfname=${bf%.pot}
  53. msgmerge --silent \
  54. -o ${LOCALE_DIR}${language}/LC_MESSAGES/${bfname}.po \
  55. ${LOCALE_DIR}${language}/LC_MESSAGES/${book}.po \
  56. ${LOCALE_DIR}${bf}
  57. msgfmt ${LOCALE_DIR}${language}/LC_MESSAGES/${bfname}.po \
  58. -o ${LOCALE_DIR}${language}/LC_MESSAGES/${bfname}.mo
  59. done
  60. # Set the bug project to I18n project
  61. set +e
  62. grep 'bug_project' ${DOC_DIR}${book}/source/conf.py > /dev/null
  63. ret=$?
  64. set -e
  65. if [ "$ret" -eq 0 ] ; then
  66. # Replace the existing "bug_project" html context
  67. sed -i -e \
  68. 's/"bug_project" *: *[^ ,}]*/"bug_project": "openstack-i18n"/' \
  69. ${DOC_DIR}${book}/source/conf.py
  70. else
  71. # Add the "bug_project" html context
  72. sed -i -e \
  73. 's/html_context *= *{/html_context = { \
  74. "bug_project": "openstack-i18n", /' \
  75. ${DOC_DIR}${book}/source/conf.py
  76. fi
  77. # Build all books
  78. if [ ${book} = "firstapp" ] ; then
  79. # Firstapp has several variations, build all of them
  80. for tag in $FIRSTAPP_TAGS ; do
  81. BUILD_DIR="${DOC_DIR}${book}/build-${tag}/html"
  82. DOCTREES="${BUILD_DIR}.doctrees"
  83. tox -evenv -- sphinx-build -q -E \
  84. -t $tag -D language=${language} \
  85. -d ${DOCTREES} \
  86. ${DOC_DIR}${book}/source/ \
  87. ${BUILD_DIR}
  88. PUBLISH_DIR=publish-docs/html/${language}/${book}-${tag}
  89. mkdir -p ${PUBLISH_DIR}
  90. rsync -a ${DOC_DIR}${book}/build-${tag}/html/ ${PUBLISH_DIR}
  91. echo $MARKER_TEXT > ${PUBLISH_DIR}/.root-marker
  92. done
  93. else
  94. BUILD_DIR="${DOC_DIR}${book}/build/html"
  95. DOCTREES="${BUILD_DIR}.doctrees"
  96. tox -evenv -- sphinx-build \
  97. -q -E -D language=${language} \
  98. -d ${DOCTREES} \
  99. ${DOC_DIR}${book}/source/ \
  100. ${BUILD_DIR}
  101. PUBLISH_DIR=publish-docs/html/${language}/${book}/
  102. mkdir -p ${PUBLISH_DIR}
  103. rsync -a ${DOC_DIR}${book}/build/html/ ${PUBLISH_DIR}
  104. echo $MARKER_TEXT > ${PUBLISH_DIR}/.root-marker
  105. fi
  106. # Remove newly created files
  107. git clean -f -q ${LOCALE_DIR}${language}/LC_MESSAGES/*.po
  108. git clean -f -x -q ${LOCALE_DIR}${language}/LC_MESSAGES/*.mo
  109. git clean -f -q ${LOCALE_DIR}*.pot
  110. # Revert changes to po file
  111. git reset -q ${LOCALE_DIR}${language}/LC_MESSAGES/${book}.po
  112. git checkout -- ${LOCALE_DIR}${language}/LC_MESSAGES/${book}.po
  113. # Revert changes to conf.py
  114. git reset -q ${DOC_DIR}${book}/source/conf.py
  115. git checkout -- ${DOC_DIR}${book}/source/conf.py
  116. }
  117. function test_language {
  118. language=$1
  119. echo
  120. echo "Building for language $language"
  121. echo
  122. args=("-v")
  123. if [[ $PURPOSE -eq "publish" ]]; then
  124. args+=("--publish")
  125. fi
  126. args+=("--check-build" "-l $language")
  127. for book in ${BOOKS["$language"]}; do
  128. if [ ${SPECIAL_BOOKS[$book]+_} ] ; then
  129. if [ ${SPECIAL_BOOKS[$book]} = "RST" ] ; then
  130. echo "Building translated RST book $book for $language"
  131. build_rst $language $book
  132. continue
  133. fi
  134. fi
  135. done
  136. }
  137. function usage {
  138. echo "usage: $0 CONF_FILE PURPOSE LANGUAGE1 LANGUAGE2 ..."
  139. echo
  140. echo "CONF_FILE is the path to the configuration file."
  141. echo
  142. echo "PURPOSE is either 'test' or 'publish'."
  143. echo
  144. echo "LANGUAGE is either 'all' or 'LANG'."
  145. echo "LANG is a language code like 'fr' or 'ja'."
  146. }
  147. # Declare in case it's not in the file
  148. declare -A SPECIAL_BOOKS
  149. declare -A DRAFTS
  150. CONF_FILE=$1
  151. shift
  152. if [[ -z $CONF_FILE ]]; then
  153. usage
  154. exit 1
  155. fi
  156. if [[ ! -e $CONF_FILE ]]; then
  157. echo "Error: the configuration file '$CONF_FILE' does not exist"
  158. exit 1
  159. fi
  160. source $CONF_FILE
  161. if [[ -z $(declare -p BOOKS 2> /dev/null | grep 'declare -A BOOKS') || \
  162. -z $DOC_DIR ]]; then
  163. echo "Error: the configuration file '$CONF_FILE' is invalid"
  164. exit 1
  165. fi
  166. case "$1" in
  167. test|publish)
  168. PURPOSE=$1
  169. shift
  170. ;;
  171. *)
  172. usage
  173. exit 1
  174. ;;
  175. esac
  176. for language in "$@" ; do
  177. case "$language" in
  178. all)
  179. for language in "${!BOOKS[@]}"; do
  180. test_language $language
  181. done
  182. ;;
  183. *)
  184. if [[ -n ${BOOKS[$language]} ]]; then
  185. test_language $language
  186. else
  187. echo "Error: language $language not handled"
  188. fi
  189. ;;
  190. esac
  191. done
  192. exit 0