|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# |
| 4 | +# Emulate 'tx pull --use-git-timestamps' by automating 'wlc download' |
| 5 | +# to download updated translations to the local workspace. |
| 6 | +# This is NOT the same as 'wlc pull' |
| 7 | +# |
| 8 | +# Usage: wlpull [component [language]] |
| 9 | +# |
| 10 | +# By the I2P project |
| 11 | +# Public Domain |
| 12 | +# |
| 13 | + |
| 14 | +# project |
| 15 | +# leave blank to get from .weblate |
| 16 | +P="" |
| 17 | +# component(s) |
| 18 | +# leave blank to get from .weblate |
| 19 | +# must specify here if multiple components |
| 20 | +C="android android-helper" |
| 21 | +# languages |
| 22 | +# leave blank to get from .weblate |
| 23 | +L="" |
| 24 | + |
| 25 | +TMP=/var/tmp/wlpull$$ |
| 26 | + |
| 27 | +if [ "$1" = "-h" -o "$1" = "-?" -o "$1" = "help" -o "$1" = "-help" -o "$1" = "--help" -o "$#" -gt 2 ] |
| 28 | +then |
| 29 | + echo "Usage: $0 [component [language]]" |
| 30 | + echo |
| 31 | + echo "Note that language must be the Weblate (language) identifier, not the ISO (language_code) identifier, which is sometimes different." |
| 32 | + |
| 33 | + exit 1 |
| 34 | +fi |
| 35 | + |
| 36 | +wlc help 2> /dev/null |
| 37 | +if [ $? -ne 2 ] |
| 38 | +then |
| 39 | + echo 'ERROR: wlc must be installed' |
| 40 | + exit 1 |
| 41 | +fi |
| 42 | + |
| 43 | + |
| 44 | +if [ ! -f .weblate -o ! -d .git ] |
| 45 | +then |
| 46 | + echo 'ERROR: Must run from base git project directory with a .weblate file' |
| 47 | + exit 1 |
| 48 | +fi |
| 49 | + |
| 50 | +X=`grep 'translation =' .weblate | cut -c14-` |
| 51 | +if [ "$X" = "" ] |
| 52 | +then |
| 53 | + echo 'ERROR: Must have a translation = project or translation = project/component line in the .weblate file' |
| 54 | + exit 1 |
| 55 | +fi |
| 56 | + |
| 57 | +# command line overrides |
| 58 | +if [ ! -z "$1" ] |
| 59 | +then |
| 60 | + C="$1" |
| 61 | +fi |
| 62 | +if [ ! -z "$2" ] |
| 63 | +then |
| 64 | + L="$2" |
| 65 | +fi |
| 66 | + |
| 67 | +if [ -z "$P" ] |
| 68 | +then |
| 69 | + P=`echo $X | cut -d '/' -f 1` |
| 70 | +fi |
| 71 | + |
| 72 | +if [ -z "$C" ] |
| 73 | +then |
| 74 | + C=`echo $X | cut -d '/' -f 2` |
| 75 | + if [ -z "$C" ] |
| 76 | + then |
| 77 | + echo "ERROR: Must have a project/component line in the .weblate file, or specify here in $0" |
| 78 | + exit 1 |
| 79 | + fi |
| 80 | +fi |
| 81 | + |
| 82 | +echo "Project: $P" |
| 83 | +echo "Components: $C" |
| 84 | +echo "Languages: ${L:-all}" |
| 85 | +echo |
| 86 | + |
| 87 | +RC=0 |
| 88 | + |
| 89 | +for CX in $C |
| 90 | +do |
| 91 | + # full component path |
| 92 | + W="$P/$CX" |
| 93 | + echo "Checking for updated translations in $W" |
| 94 | + |
| 95 | + wlc list-translations "$W" > "$TMP" |
| 96 | + if [ "$?" -ne 0 ] |
| 97 | + then |
| 98 | + echo "ERROR: wlc list-translations failed for $W - bad component name?" |
| 99 | + rm -f $TMP |
| 100 | + exit 1 |
| 101 | + fi |
| 102 | + # extract params for each language |
| 103 | + |
| 104 | + # files |
| 105 | + # skip filename for english |
| 106 | + FILES=`grep '^filename: ' "$TMP" | cut -c 11- | tail -n +2` |
| 107 | + FILES=`echo "$FILES" | tr '\n' ' '` |
| 108 | + if [ -z "$FILES" ] |
| 109 | + then |
| 110 | + echo "ERROR: No files found for $W" |
| 111 | + exit 1 |
| 112 | + fi |
| 113 | + |
| 114 | + # languages |
| 115 | + # skip english |
| 116 | + # NOT language_code, must be language which is the weblate identifier |
| 117 | + #LANGS=`grep '^language_code: ' "$TMP" | cut -c 16- | tail -n +2` |
| 118 | + LANGS=`grep '^language: ' "$TMP" | cut -c 11- | tail -n +2` |
| 119 | + LANGS=`echo "$LANGS" | tr '\n' ' '` |
| 120 | + if [ -z "$LANGS" ] |
| 121 | + then |
| 122 | + echo "ERROR: No languages found for $W" |
| 123 | + exit 1 |
| 124 | + fi |
| 125 | + |
| 126 | + # timestamp |
| 127 | + # skip english |
| 128 | + # convert spaces to _, we will undo later |
| 129 | + # strip ms and TZ |
| 130 | + TIMES=`grep '^last_change: ' "$TMP" | cut -c 14- | tail -n +2 | tr ' ' '_' | cut -d '.' -f 1` |
| 131 | + TIMES=`echo "$TIMES" | tr '\n' ' '` |
| 132 | + if [ -z "$TIMES" ] |
| 133 | + then |
| 134 | + echo "ERROR: No timestamps found for $W" |
| 135 | + exit 1 |
| 136 | + fi |
| 137 | + |
| 138 | + rm -f "$TMP" |
| 139 | + # debug |
| 140 | + #echo |
| 141 | + #echo "Files:" |
| 142 | + #echo "$FILES" |
| 143 | + echo |
| 144 | + echo "Found Weblate Languages:" |
| 145 | + echo "$LANGS" |
| 146 | + echo |
| 147 | + #echo "Timestamps:" |
| 148 | + #echo "$TIMES" |
| 149 | + |
| 150 | + i=0 |
| 151 | + for LANG in $LANGS |
| 152 | + do |
| 153 | + i=$((i+1)) |
| 154 | + if [ ! -z "$L" ] |
| 155 | + then |
| 156 | + # if lang specified on command line, skip if this isn't it |
| 157 | + if [ "$L" != "$LANG" ] |
| 158 | + then |
| 159 | + continue |
| 160 | + fi |
| 161 | + fi |
| 162 | + |
| 163 | + FILE=`echo "$FILES" | cut -d ' ' -f $i` |
| 164 | + if [ ! -f "$FILE" -a -z "$L" ] |
| 165 | + then |
| 166 | + echo "SKIPPING $LANG - no local file $FILE" |
| 167 | + echo "Force download with $0 $CX $LANG" |
| 168 | + continue |
| 169 | + fi |
| 170 | + # convert remote timestamp to epoch time |
| 171 | + TIME=`echo "$TIMES" | cut -d ' ' -f $i | tr '_' ' '` |
| 172 | + TSR=`date -u -d "$TIME" +%s` |
| 173 | + if [ "$?" -ne 0 ] |
| 174 | + then |
| 175 | + echo "ERROR: Cannot get weblate date for $FILE" |
| 176 | + exit 1 |
| 177 | + fi |
| 178 | + # get local checkin timestamp |
| 179 | + TSL=`git log -1 --format=%at "$FILE"` |
| 180 | + if [ "$?" -ne 0 ] |
| 181 | + then |
| 182 | + echo "ERROR: Cannot get git commit date for $FILE" |
| 183 | + exit 1 |
| 184 | + fi |
| 185 | + if [ -z "$TSL" ] |
| 186 | + then |
| 187 | + echo "WARNING: No commits for $FILE - forcing download, don't forget to git add" |
| 188 | + TSL=0 |
| 189 | + fi |
| 190 | + # format for display |
| 191 | + TSLC=`date -u -d @$TSL` |
| 192 | + TSRC=`date -u -d @$TSR` |
| 193 | + if [ "$TSR" -le "$TSL" ] |
| 194 | + then |
| 195 | + echo "SKIPPING $LANG - Weblate $TSRC older than local file $FILE committed $TSLC" |
| 196 | + continue |
| 197 | + fi |
| 198 | + echo "DOWNLOADING $LANG - Weblate $TSRC newer than local file $FILE committed $TSLC" |
| 199 | + wlc download $W/$LANG > "$TMP" |
| 200 | + if [ "$?" -ne 0 ] |
| 201 | + then |
| 202 | + echo "FAILED to download file for $W/$LANG" |
| 203 | + rm -f "$TMP" |
| 204 | + RC=1 |
| 205 | + continue |
| 206 | + fi |
| 207 | + diff -q "$TMP" "$FILE" > /dev/null |
| 208 | + if [ "$?" -eq 0 ] |
| 209 | + then |
| 210 | + # Weblate timestamp newer after initial import |
| 211 | + echo "NO CHANGES in $W/$LANG for $FILE" |
| 212 | + rm -f "$TMP" |
| 213 | + continue |
| 214 | + fi |
| 215 | + mv "$TMP" "$FILE" |
| 216 | + if [ "$?" -ne 0 ] |
| 217 | + then |
| 218 | + echo "FAILED to copy file for $W/$LANG to $FILE" |
| 219 | + rm -f "$TMP" |
| 220 | + RC=1 |
| 221 | + continue |
| 222 | + fi |
| 223 | + echo "DOWNLOADED language $LANG to $FILE" |
| 224 | + done |
| 225 | +done |
| 226 | + |
| 227 | +exit $RC |
0 commit comments