lib_sxng_weblate.sh 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #!/usr/bin/env bash
  2. # SPDX-License-Identifier: AGPL-3.0-or-later
  3. weblate.help() {
  4. cat <<EOF
  5. weblate.:
  6. push.translations: push translation changes from SearXNG to Weblate's counterpart
  7. to.translations: Update 'translations' branch with last additions from Weblate.
  8. EOF
  9. }
  10. TRANSLATIONS_WORKTREE="$CACHE/translations"
  11. weblate.translations.worktree() {
  12. # Create git worktree ${TRANSLATIONS_WORKTREE} and checkout branch
  13. # 'translations' from Weblate's counterpart (weblate) of the SearXNG
  14. # (origin).
  15. #
  16. # remote weblate https://translate.codeberg.org/git/searxng/searxng/
  17. (
  18. set -e
  19. if ! git remote get-url weblate 2>/dev/null; then
  20. git remote add weblate https://translate.codeberg.org/git/searxng/searxng/
  21. fi
  22. if [ -d "${TRANSLATIONS_WORKTREE}" ]; then
  23. pushd .
  24. cd "${TRANSLATIONS_WORKTREE}"
  25. git reset --hard HEAD
  26. git pull origin translations
  27. popd
  28. else
  29. mkdir -p "${TRANSLATIONS_WORKTREE}"
  30. git worktree add "${TRANSLATIONS_WORKTREE}" translations
  31. fi
  32. )
  33. }
  34. weblate.to.translations() {
  35. # Update 'translations' branch of SearXNG (origin) with last additions from
  36. # Weblate.
  37. # 1. Check if Weblate is locked, if not die with error message
  38. # 2. On Weblate's counterpart (weblate), pull master and translations branch
  39. # from SearXNG (origin).
  40. # 3. Commit changes made in a Weblate object on Weblate's counterpart
  41. # (weblate).
  42. # 4. In translations worktree, merge changes of branch 'translations' from
  43. # remote 'weblate' and push it on branch 'translations' of 'origin'
  44. (
  45. set -e
  46. pyenv.activate
  47. if [ "$(wlc lock-status)" != "locked: True" ]; then
  48. die 1 "weblate must be locked, currently: $(wlc lock-status)"
  49. fi
  50. # weblate: commit pending changes
  51. wlc pull
  52. wlc commit
  53. # get the translations in a worktree
  54. weblate.translations.worktree
  55. pushd "${TRANSLATIONS_WORKTREE}"
  56. git remote update weblate
  57. git merge weblate/translations
  58. git push
  59. popd
  60. )
  61. dump_return $?
  62. }
  63. weblate.translations.commit() {
  64. # Update 'translations' branch of SearXNG (origin) with last additions from
  65. # Weblate. Copy the changes to the master branch, compile translations and
  66. # create a commit in the local branch (master)
  67. local existing_commit_hash commit_body commit_message exitcode
  68. (
  69. set -e
  70. pyenv.activate
  71. # lock change on weblate
  72. wlc lock
  73. # get translations branch in git worktree (TRANSLATIONS_WORKTREE)
  74. weblate.translations.worktree
  75. existing_commit_hash=$(
  76. cd "${TRANSLATIONS_WORKTREE}"
  77. git log -n1 --pretty=format:'%h'
  78. )
  79. # pull weblate commits
  80. weblate.to.translations
  81. # copy the changes to the master branch
  82. cp -rv --preserve=mode,timestamps "${TRANSLATIONS_WORKTREE}/searx/translations" "searx"
  83. # compile translations
  84. build_msg BABEL 'compile translation catalogs into binary MO files'
  85. pybabel compile --statistics \
  86. -d "searx/translations"
  87. # update searx/data/translation_labels.json
  88. data.locales
  89. # git add/commit (no push)
  90. commit_body=$(
  91. cd "${TRANSLATIONS_WORKTREE}"
  92. git log --pretty=format:'%h - %as - %aN <%ae>' "${existing_commit_hash}..HEAD"
  93. )
  94. commit_message=$(echo -e "[l10n] update translations from Weblate\n\n${commit_body}")
  95. git add searx/translations
  96. git add searx/data/locales.json
  97. git commit -m "${commit_message}"
  98. )
  99. exitcode=$?
  100. ( # make sure to always unlock weblate
  101. set -e
  102. pyenv.cmd wlc unlock
  103. )
  104. dump_return $exitcode
  105. }
  106. weblate.push.translations() {
  107. # Push *translation changes* from SearXNG (origin) to Weblate's counterpart
  108. # (weblate).
  109. # In branch master of SearXNG (origin) check for meaningful changes in
  110. # folder 'searx/translations', commit changes on branch 'translations' and
  111. # at least, pull updated branches on Weblate's counterpart (weblate).
  112. # 1. Create git worktree ${TRANSLATIONS_WORKTREE} and checkout branch
  113. # 'translations' from remote 'weblate'.
  114. # 2. Stop if there is no meaningful change in the 'master' branch (origin),
  115. # compared to the 'translations' branch (weblate), otherwise ...
  116. # 3. Update 'translations' branch of SearXNG (origin) with last additions
  117. # from Weblate.
  118. # 5. Notify Weblate to pull updated 'master' & 'translations' branch.
  119. local messages_pot diff_messages_pot last_commit_hash last_commit_detail \
  120. exitcode
  121. messages_pot="${TRANSLATIONS_WORKTREE}/searx/translations/messages.pot"
  122. (
  123. set -e
  124. pyenv.activate
  125. # get translations branch in git worktree (TRANSLATIONS_WORKTREE)
  126. weblate.translations.worktree
  127. # update messages.pot in the master branch
  128. build_msg BABEL 'extract messages from source files and generate POT file'
  129. pybabel extract -F babel.cfg --project="SearXNG" --version="-" \
  130. -o "${messages_pot}" \
  131. "searx/"
  132. # stop if there is no meaningful change in the master branch
  133. diff_messages_pot=$(
  134. cd "${TRANSLATIONS_WORKTREE}"
  135. git diff -- "searx/translations/messages.pot"
  136. )
  137. if ! echo "$diff_messages_pot" | grep -qE "[\+\-](msgid|msgstr)"; then
  138. build_msg BABEL 'no changes detected, exiting'
  139. return 42
  140. fi
  141. return 0
  142. )
  143. exitcode=$?
  144. if [ "$exitcode" -eq 42 ]; then
  145. return 0
  146. fi
  147. if [ "$exitcode" -gt 0 ]; then
  148. return $exitcode
  149. fi
  150. (
  151. set -e
  152. pyenv.activate
  153. # lock change on weblate
  154. # weblate may add commit(s) since the call to "weblate.translations.worktree".
  155. # this is not a problem because after this line, "weblate.to.translations"
  156. # calls again "weblate.translations.worktree" which calls "git pull"
  157. wlc lock
  158. # save messages.pot in the translations branch for later
  159. pushd "${TRANSLATIONS_WORKTREE}"
  160. git stash push
  161. popd
  162. # merge weblate commits into the translations branch
  163. weblate.to.translations
  164. # restore messages.pot in the translations branch
  165. pushd "${TRANSLATIONS_WORKTREE}"
  166. git stash pop
  167. popd
  168. # update messages.po files in the master branch
  169. build_msg BABEL 'update existing message catalogs from POT file'
  170. pybabel update -N \
  171. -i "${messages_pot}" \
  172. -d "${TRANSLATIONS_WORKTREE}/searx/translations"
  173. # git add/commit/push
  174. last_commit_hash=$(git log -n1 --pretty=format:'%h')
  175. last_commit_detail=$(git log -n1 --pretty=format:'%h - %as - %aN <%ae>' "${last_commit_hash}")
  176. pushd "${TRANSLATIONS_WORKTREE}"
  177. git add searx/translations
  178. git commit \
  179. -m "[translations] update messages.pot and messages.po files" \
  180. -m "From ${last_commit_detail}"
  181. git push
  182. popd
  183. # notify weblate to pull updated master & translations branch
  184. wlc pull
  185. )
  186. exitcode=$?
  187. ( # make sure to always unlock weblate
  188. set -e
  189. pyenv.activate
  190. wlc unlock
  191. )
  192. dump_return $exitcode
  193. }