summaryrefslogtreecommitdiff
path: root/utils/lib_sxng_weblate.sh
blob: f52b75d7c714145646f65483e3bac2e83ed79916 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env bash
# SPDX-License-Identifier: AGPL-3.0-or-later

weblate.help(){
    cat <<EOF
weblate.:
  push.translations: push translation changes from SearXNG to Weblate's counterpart
  to.translations: Update 'translations' branch with last additions from Weblate.
EOF
}

TRANSLATIONS_WORKTREE="$CACHE/translations"

weblate.translations.worktree() {

    # Create git worktree ${TRANSLATIONS_WORKTREE} and checkout branch
    # 'translations' from Weblate's counterpart (weblate) of the SearXNG
    # (origin).
    #
    #     remote weblate https://translate.codeberg.org/git/searxng/searxng/

    (   set -e
        if ! git remote get-url weblate 2> /dev/null; then
            git remote add weblate https://translate.codeberg.org/git/searxng/searxng/
        fi
        if [ -d "${TRANSLATIONS_WORKTREE}" ]; then
            pushd .
            cd "${TRANSLATIONS_WORKTREE}"
            git reset --hard HEAD
            git pull origin translations
            popd
        else
            mkdir -p "${TRANSLATIONS_WORKTREE}"
            git worktree add "${TRANSLATIONS_WORKTREE}" translations
        fi
    )
}

weblate.to.translations() {

    # Update 'translations' branch of SearXNG (origin) with last additions from
    # Weblate.

    # 1. Check if Weblate is locked, if not die with error message
    # 2. On Weblate's counterpart (weblate), pull master and translations branch
    #    from SearXNG (origin).
    # 3. Commit changes made in a Weblate object on Weblate's counterpart
    #    (weblate).
    # 4. In translations worktree, merge changes of branch 'translations' from
    #    remote 'weblate' and push it on branch 'translations' of 'origin'

    (   set -e
        pyenv.activate
        if [ "$(wlc lock-status)" != "locked: True" ]; then
            die 1 "weblate must be locked, currently: $(wlc lock-status)"
        fi
        # weblate: commit pending changes
        wlc pull
        wlc commit

        # get the translations in a worktree
        weblate.translations.worktree

        pushd "${TRANSLATIONS_WORKTREE}"
        git remote update weblate
        git merge weblate/translations
        git push
        popd
    )
    dump_return $?
}

weblate.translations.commit() {

    # Update 'translations' branch of SearXNG (origin) with last additions from
    # Weblate.  Copy the changes to the master branch, compile translations and
    # create a commit in the local branch (master)

    local existing_commit_hash commit_body commit_message exitcode
    (   set -e
        pyenv.activate
        # lock change on weblate
        wlc lock

        # get translations branch in git worktree (TRANSLATIONS_WORKTREE)
        weblate.translations.worktree
        existing_commit_hash=$(cd "${TRANSLATIONS_WORKTREE}"; git log -n1  --pretty=format:'%h')

        # pull weblate commits
        weblate.to.translations

        # copy the changes to the master branch
        cp -rv --preserve=mode,timestamps "${TRANSLATIONS_WORKTREE}/searx/translations" "searx"

        # compile translations
        build_msg BABEL 'compile translation catalogs into binary MO files'
        pybabel compile --statistics \
                -d "searx/translations"
        # git add/commit (no push)
        commit_body=$(cd "${TRANSLATIONS_WORKTREE}"; git log --pretty=format:'%h - %as - %aN <%ae>' "${existing_commit_hash}..HEAD")
        commit_message=$(echo -e "[translations] update from Weblate\n\n${commit_body}")
        git add searx/translations
        git commit -m "${commit_message}"
    )
    exitcode=$?
    (   # make sure to always unlock weblate
        set -e
        pyenv.cmd wlc unlock
    )
    dump_return $exitcode
}

weblate.push.translations() {

    # Push *translation changes* from SearXNG (origin) to Weblate's counterpart
    # (weblate).

    # In branch master of SearXNG (origin) check for meaningful changes in
    # folder 'searx/translations', commit changes on branch 'translations' and
    # at least, pull updated branches on Weblate's counterpart (weblate).

    # 1. Create git worktree ${TRANSLATIONS_WORKTREE} and checkout branch
    #    'translations' from remote 'weblate'.
    # 2. Stop if there is no meaningful change in the 'master' branch (origin),
    #    compared to the 'translations' branch (weblate), otherwise ...
    # 3. Update 'translations' branch of SearXNG (origin) with last additions
    #    from Weblate.
    # 5. Notify Weblate to pull updated 'master' & 'translations' branch.

    local messages_pot diff_messages_pot last_commit_hash last_commit_detail \
          exitcode
    messages_pot="${TRANSLATIONS_WORKTREE}/searx/translations/messages.pot"
    (   set -e
        pyenv.activate
        # get translations branch in git worktree (TRANSLATIONS_WORKTREE)
        weblate.translations.worktree

        # update messages.pot in the master branch
        build_msg BABEL 'extract messages from source files and generate POT file'
        pybabel extract -F babel.cfg \
                -o "${messages_pot}" \
                "searx/"

        # stop if there is no meaningful change in the master branch
        diff_messages_pot=$(cd "${TRANSLATIONS_WORKTREE}";\
                            git diff -- "searx/translations/messages.pot")
        if ! echo "$diff_messages_pot" | grep -qE "[\+\-](msgid|msgstr)"; then
            build_msg BABEL 'no changes detected, exiting'
            return 42
        fi
        return 0
    )
    exitcode=$?
    if [ "$exitcode" -eq 42 ]; then
        return 0
    fi
    if [ "$exitcode" -gt 0 ]; then
       return $exitcode
    fi
    (
        set -e
        pyenv.activate

        # lock change on weblate
        # weblate may add commit(s) since the call to "weblate.translations.worktree".
        # this is not a problem because after this line, "weblate.to.translations"
        # calls again "weblate.translations.worktree" which calls "git pull"
        wlc lock

        # save messages.pot in the translations branch for later
        pushd "${TRANSLATIONS_WORKTREE}"
        git stash push
        popd

        # merge weblate commits into the translations branch
        weblate.to.translations

        # restore messages.pot in the translations branch
        pushd "${TRANSLATIONS_WORKTREE}"
        git stash pop
        popd

        # update messages.po files in the master branch
        build_msg BABEL 'update existing message catalogs from POT file'
        pybabel update -N \
            -i "${messages_pot}" \
            -d "${TRANSLATIONS_WORKTREE}/searx/translations"

        # git add/commit/push
        last_commit_hash=$(git log -n1  --pretty=format:'%h')
        last_commit_detail=$(git log -n1 --pretty=format:'%h - %as - %aN <%ae>' "${last_commit_hash}")

        pushd "${TRANSLATIONS_WORKTREE}"
        git add searx/translations
        git commit \
            -m "[translations] update messages.pot and messages.po files" \
            -m "From ${last_commit_detail}"
        git push
        popd

        # notify weblate to pull updated master & translations branch
        wlc pull
    )
    exitcode=$?
    (   # make sure to always unlock weblate
        set -e
        pyenv.activate
        wlc unlock
    )
    dump_return $exitcode
}