lib_sxng_weblate.sh 7.1 KB

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