update-pot.sh 6.69 KB
Newer Older
1
2
#!/bin/bash

3
4
5
6
7
8
9
10
11
12
13
####################################################
# This script pulls the latest English language strings from the Mahara core project,
# converts them into .pot files, and uploads those into the branches for the
# mahara-lang project, so that they can be translated through Launchpad.
#
# NOTE: This script contains a hard-coded list of branch names to operate on.
# Whenever there is a new major release of Mahara, these lists need to be
# manually updated. The list has been tagged with this comment:
#
#     # @UPDATE when there is a new series
#
14

15
# this is expected to define DATA, SCRIPTS, DOCROOT
16
17
. /etc/mahara-langpacks.conf

18
19
20
echo -n "Checking for changes to strings "
date "+%Y-%m-%d %H:%M:%S"

21
22
23
24
25
if [ ! -w ${DATA} ]; then
    echo "${DATA} not writable"
    exit 1
fi

26
27
28
29
30
if [ ! -w ${DOCROOT} ]; then
    echo "${DOCROOT} not writable"
    exit 1
fi

31
# Lock the script to prevent running in parallel
32
if ! mkdir ${DATA}/update-pot-lock; then
33
34
35
36
    echo "The script is running" >&2
    exit 0
fi

37
38
39
40
41
42
WORK=${DATA}/templates
GITDIR=${WORK}/git
TEMP=${WORK}/temp

[ ! -d ${WORK} ] && mkdir ${WORK}
[ ! -d ${TEMP} ] && mkdir ${TEMP}
43
[ ! -d ${DOCROOT}/pot ] && mkdir ${DOCROOT}/pot
44

45
mahararemote='https://git.mahara.org/mahara/mahara.git'
46
47
# mahararemote='git@git.mahara.org/mahara/mahara.git'
# mahararemote='git@github.com:MaharaProject/mahara.git'
48
49

if [ ! -d ${GITDIR} ]; then
50
51
    echo "git clone ${mahararemote} ${GITDIR}"
    git clone --quiet ${mahararemote} ${GITDIR}
52
53
fi

54
bzr launchpad-login dev-mahara
55
56
57
58
[ ! -d "${WORK}/mahara-lang-bzr" ] && bzr init-repo ${WORK}/mahara-lang-bzr

BZR=${WORK}/mahara-lang-bzr

59
60
61
cd ${GITDIR}
git fetch --quiet origin

62
# @UPDATE when there is a new series
63
branches="17.04_STABLE 17.10_STABLE 18.04_STABLE 18.10_STABLE 19.04_STABLE 19.10_STABLE master"
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

for branch in ${branches} ; do
    branchexists=`git branch | grep "${branch}$"`
    if [ -z "${branchexists}" ]; then
        git checkout -b ${branch} origin/${branch}
    else
        git checkout --quiet ${branch}
        git reset --hard -q origin/${branch}
    fi

    remotecommit=`git log --pretty=format:"%H" | head -1`

    last=${WORK}/${branch}.last
    lastruncommit=z
    if [ -f ${last} ]; then
        lastruncommit=`cat ${last}`
    fi

    if [ "$remotecommit" = "$lastruncommit" ] ; then
        echo "${branch} is up to date"
    else
        echo "New commits on ${branch}"

        if [ ! -d ${GITDIR}/htdocs ] ; then
            echo "No htdocs directory in branch ${branch}; skipping."
            continue
        fi

        find htdocs -type f -path "*lang/en.utf8*" | xargs tar zcf ${TEMP}/${branch}.tar.gz

        if [ ! -f ${TEMP}/${branch}.tar.gz ] ; then
            echo "Missing archive ${TEMP}/${branch}.tar.gz; skipping."
            continue
        fi

99
100
101
        # While we have an up-to-date en tarball handy, dump a copy of it in the main tarball directory
        cp ${TEMP}/${branch}.tar.gz ${DOCROOT}/en-${branch}.tar.gz

102
103
104
105
106
107
108
109
110
111
112
113
        langpack=${TEMP}/${branch}
        [ ! -d ${langpack} ] && mkdir ${langpack}

        cd ${langpack}
        tar zxf ../${branch}.tar.gz
        cd ${GITDIR}

        if [ ! -d ${langpack}/htdocs ] ; then
            echo "No htdocs directory in langpack ${langpack}; skipping."
            continue
        fi

114
        # Output into a copy of the launchpad mahara-lang repo
115
116
117
118
119
120
121
122
        if [ ! -d ${BZR}/${branch} ]; then
            bzr branch lp:~mahara-lang/mahara-lang/${branch} ${BZR}/${branch}
        else
            cd ${BZR}/${branch}
            bzr pull
            cd ${GITDIR}
        fi

123
        outputdir=${BZR}/${branch}/mahara
124
125
126
        [ ! -d ${outputdir} ] && mkdir ${outputdir}
        outputfile=${outputdir}/mahara.pot

127
128
129
        echo "Updating ${outputfile}"

        [ -f ${outputfile} ] && rm ${outputfile}
130
        /usr/bin/php ${SCRIPTS}/php-po.php ${langpack}/htdocs ${langpack}/htdocs ${outputfile}
131

132
        if [ -f ${outputfile} ]; then
133
134
            cd ${BZR}/${branch}

Richard Mansfield's avatar
Richard Mansfield committed
135
            diffs=`bzr diff mahara/mahara.pot | grep "[+-]msg"`
136

Richard Mansfield's avatar
Richard Mansfield committed
137
138
            if [ -z "${diffs}" ]; then
                bzr revert
139

Richard Mansfield's avatar
Richard Mansfield committed
140
            else
141
142
143
                # Update copy of template in webroot
                tar zcf ${DOCROOT}/pot/${branch}.tar.gz mahara/mahara.pot

144
                # Update template
145
146
147
                bzr add mahara/mahara.pot
                bzr commit -m "Update template to ${remotecommit}"

148
149
150
151
152
153
154
155
156
157
158
159
                if [ $branch = 'master' ] ; then
                    # Update all the .po files from the export repo to avoid unnecessary invalidation
                    # of existing translations

                    exportbranch=${branch}-export
                    if [ ! -d ${BZR}/${exportbranch} ]; then
                        bzr branch lp:~mahara-lang/mahara-lang/${exportbranch} ${BZR}/${exportbranch}
                    else
                        cd ${BZR}/${exportbranch}
                        bzr pull
                    fi

Richard Mansfield's avatar
Richard Mansfield committed
160
161
                    cd ${BZR}/${branch}

162
                    for po in `ls ${BZR}/${exportbranch}/mahara/*.po`; do
163
164
                        pobase=${po##*/}
                        /usr/bin/perl ${SCRIPTS}/update-po-from-pot.pl $po mahara/mahara.pot mahara/$pobase
Richard Mansfield's avatar
Richard Mansfield committed
165
166
167
168
169
170

                        status=`bzr status -S mahara/$pobase | grep ?`
                        if [ ! -z "$status" ]; then
                            # New file
                            bzr add mahara/$pobase
                        else
171
172
                            # There are always a few changes in the po header, but we don't care about them, so just
                            # check if anything's changed after the "X-Generator:" or "X-Launchpad-Export-Date:" lines
Son Nguyen's avatar
Son Nguyen committed
173
                            podiffs=`bzr diff mahara/${pobase} | awk '/(X-Generator: Launchpad|X-Launchpad-Export-Date:)/ {p+=1;next}; p>1 {print}' | grep "^[+-]"`
Richard Mansfield's avatar
Richard Mansfield committed
174
175
176
177
178
                            if [ -z "$podiffs" ] ; then
                                # Nothing worth committing
                                bzr revert mahara/$pobase
                            fi
                        fi
179
180
181
182
183
184
185
                    done

                    podiffs=`bzr diff mahara`
                    if [ ! -z "$podiffs" ] ; then
                        bzr add mahara
                        bzr commit -m "Update translations to ${remotecommit}"
                    fi
Richard Mansfield's avatar
Richard Mansfield committed
186

187
188
                fi

189
190
191
192
193
194
                # Push everything to lp:mahara-lang, if this is the prod instance.
                if [ "${WWWROOT}" = 'http://langpacks.mahara.org' ]; then
                    bzr push lp:~mahara-lang/mahara-lang/${branch}
                else
                    echo "Not pushing to lp:~mahara-lang/mahara-lang/${branch}"
                fi
195
196
            fi

197
            cd ${GITDIR}
198
199
        fi

200
201
        echo "${remotecommit}" > ${last}
    fi
Son Nguyen's avatar
Son Nguyen committed
202
done
203
204

# Unlock the script
205
rmdir ${DATA}/update-pot-lock