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.
 
 
 
 

191 lines
5.6 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. # This marker is needed for Infra publishing and needs to go into the
  14. # root directory of each translated manual as file ".root-marker".
  15. MARKER_TEXT="Project: $ZUUL_PROJECT Ref: $ZUUL_BRANCH Build: $ZUUL_UUID Revision: $ZUUL_NEWREF"
  16. function build_rst {
  17. language=$1
  18. book=$2
  19. local ret
  20. # First build all the single po files
  21. # Note that we need to run inside a venv since the venv we are run in
  22. # uses SitePackages=True and we have to install Sphinx in the venv
  23. # together with openstackdocstheme. With SitePackages, the global Sphinx
  24. # is used and that will not work with a local openstackdocstheme installed.
  25. COMMON="common"
  26. LOCALE_DIR="${DOC_DIR}${book}/source/locale/"
  27. COMMON_DIR="${DOC_DIR}${COMMON}/source/locale/"
  28. tox -evenv -- sphinx-build -q -E -W -b gettext \
  29. ${DOC_DIR}${book}/source/ ${LOCALE_DIR}
  30. # Merge the common po file
  31. if [[ -e ${COMMON_DIR}${language}/LC_MESSAGES/${COMMON}.po ]] ; then
  32. msgcat --use-first -o ${LOCALE_DIR}${language}/${book}.po \
  33. ${LOCALE_DIR}${language}/LC_MESSAGES/${book}.po \
  34. ${COMMON_DIR}${language}/LC_MESSAGES/${COMMON}.po
  35. mv -f ${LOCALE_DIR}${language}/${book}.po \
  36. ${LOCALE_DIR}${language}/LC_MESSAGES/${book}.po
  37. fi
  38. # Now run msgmerge on all files
  39. for f in ${LOCALE_DIR}*.pot ; do
  40. # Skip the master file
  41. if [ $f = "${LOCALE_DIR}${book}.pot" ] ; then
  42. continue
  43. fi
  44. bf=$(basename $f)
  45. # Remove .pot
  46. bfname=${bf%.pot}
  47. msgmerge --silent \
  48. -o ${LOCALE_DIR}${language}/LC_MESSAGES/${bfname}.po \
  49. ${LOCALE_DIR}${language}/LC_MESSAGES/${book}.po \
  50. ${LOCALE_DIR}${bf}
  51. msgfmt ${LOCALE_DIR}${language}/LC_MESSAGES/${bfname}.po \
  52. -o ${LOCALE_DIR}${language}/LC_MESSAGES/${bfname}.mo
  53. done
  54. # Set the bug project to I18n project
  55. set +e
  56. grep 'bug_project' ${DOC_DIR}${book}/source/conf.py > /dev/null
  57. ret=$?
  58. set -e
  59. if [ "$ret" -eq 0 ] ; then
  60. # Replace the existing "bug_project" html context
  61. sed -i -e \
  62. 's/"bug_project" *: *[^ ,}]*/"bug_project": "openstack-i18n"/' \
  63. ${DOC_DIR}${book}/source/conf.py
  64. else
  65. # Add the "bug_project" html context
  66. sed -i -e \
  67. 's/html_context *= *{/html_context = { \
  68. "bug_project": "openstack-i18n", /' \
  69. ${DOC_DIR}${book}/source/conf.py
  70. fi
  71. # Build book
  72. BUILD_DIR="${DOC_DIR}${book}/build/html"
  73. DOCTREES="${BUILD_DIR}.doctrees"
  74. tox -evenv -- sphinx-build \
  75. -q -E -D language=${language} \
  76. -d ${DOCTREES} \
  77. ${DOC_DIR}${book}/source/ \
  78. ${BUILD_DIR}
  79. PUBLISH_DIR=publish-docs/html/${language}/${book}/
  80. mkdir -p ${PUBLISH_DIR}
  81. rsync -a ${DOC_DIR}${book}/build/html/ ${PUBLISH_DIR}
  82. echo $MARKER_TEXT > ${PUBLISH_DIR}/.root-marker
  83. # Remove newly created files
  84. git clean -f -q ${LOCALE_DIR}${language}/LC_MESSAGES/*.po
  85. git clean -f -x -q ${LOCALE_DIR}${language}/LC_MESSAGES/*.mo
  86. git clean -f -q ${LOCALE_DIR}*.pot
  87. # Revert changes to po file
  88. git reset -q ${LOCALE_DIR}${language}/LC_MESSAGES/${book}.po
  89. git checkout -- ${LOCALE_DIR}${language}/LC_MESSAGES/${book}.po
  90. # Revert changes to conf.py
  91. git reset -q ${DOC_DIR}${book}/source/conf.py
  92. git checkout -- ${DOC_DIR}${book}/source/conf.py
  93. }
  94. function test_language {
  95. language=$1
  96. echo
  97. echo "Building for language $language"
  98. echo
  99. for book in ${BOOKS["$language"]}; do
  100. if [ ${SPECIAL_BOOKS[$book]+_} ] ; then
  101. if [ ${SPECIAL_BOOKS[$book]} = "RST" ] ; then
  102. echo "Building translated RST book $book for $language"
  103. build_rst $language $book
  104. continue
  105. fi
  106. fi
  107. done
  108. }
  109. function usage {
  110. echo "usage: $0 CONF_FILE PURPOSE LANGUAGE1 LANGUAGE2 ..."
  111. echo
  112. echo "CONF_FILE is the path to the configuration file."
  113. echo
  114. echo "PURPOSE is 'test' or 'publish' (is ignored)."
  115. echo
  116. echo "LANGUAGE is either 'all' or 'LANG'."
  117. echo "LANG is a language code like 'fr' or 'ja'."
  118. }
  119. # Declare in case it's not in the file
  120. declare -A SPECIAL_BOOKS
  121. declare -A DRAFTS
  122. CONF_FILE=$1
  123. shift
  124. if [[ -z $CONF_FILE ]]; then
  125. usage
  126. exit 1
  127. fi
  128. if [[ ! -e $CONF_FILE ]]; then
  129. echo "Error: the configuration file '$CONF_FILE' does not exist"
  130. exit 1
  131. fi
  132. source $CONF_FILE
  133. if [[ -z $(declare -p BOOKS 2> /dev/null | grep 'declare -A BOOKS') || \
  134. -z $DOC_DIR ]]; then
  135. echo "Error: the configuration file '$CONF_FILE' is invalid"
  136. exit 1
  137. fi
  138. case "$1" in
  139. test|publish)
  140. PURPOSE=$1
  141. shift
  142. ;;
  143. *)
  144. usage
  145. exit 1
  146. ;;
  147. esac
  148. for language in "$@" ; do
  149. case "$language" in
  150. all)
  151. for language in "${!BOOKS[@]}"; do
  152. test_language $language
  153. done
  154. ;;
  155. *)
  156. if [[ -n ${BOOKS[$language]} ]]; then
  157. test_language $language
  158. else
  159. echo "Error: language $language not handled"
  160. fi
  161. ;;
  162. esac
  163. done
  164. exit 0