[FEATURE] New script to upgrade extension from git 20/41120/2
authorXavier Perseguers <xavier@typo3.org>
Mon, 13 Jul 2015 09:39:33 +0000 (11:39 +0200)
committerXavier Perseguers <xavier@typo3.org>
Mon, 13 Jul 2015 09:41:47 +0000 (11:41 +0200)
Change-Id: Ie0ca9023ad00214b73197248f5780086feac6874
Reviewed-on: http://review.typo3.org/41120
Reviewed-by: Xavier Perseguers <xavier@typo3.org>
Tested-by: Xavier Perseguers <xavier@typo3.org>
bin/pootle-upgrade-extension-git [new file with mode: 0755]

diff --git a/bin/pootle-upgrade-extension-git b/bin/pootle-upgrade-extension-git
new file mode 100755 (executable)
index 0000000..78e98a1
--- /dev/null
@@ -0,0 +1,149 @@
+#!/bin/bash
+
+set -o errtrace
+set -o nounset
+
+#  Trap non-normal exit signals: 1/HUP, 2/INT, 3/QUIT, 15/TERM, ERR
+trap onexit 1 2 3 15
+
+#--- onexit() -----------------------------------------------------
+#  @param $1 integer  (optional) Exit status.  If not set, use `$?'
+function onexit() {
+       local exit_status=${1:-$?}
+       if [ $exit_status -gt 0 ]; then
+               echo Exiting $0 with $exit_status
+       fi
+       exit $exit_status
+}
+
+# --- check_available_languages() ---------------------------------
+# @param $1 string               Pootle Project Name
+function check_available_languages() {
+       local NEED_UPDATE=0
+       local PROJECT_NAME=$1
+        for LANGUAGE_KEY in $(ls /var/www/vhosts/pootle.typo3.org/home/templates); do
+               local LANGUAGE_DIRECTORY="/var/www/vhosts/pootle.typo3.org/pootle/po/$PROJECT_NAME/$LANGUAGE_KEY"
+                if [ "$PROJECT_NAME" != "" ] && [ -d /var/www/vhosts/pootle.typo3.org/pootle/po/$PROJECT_NAME ] && [ ! -d $LANGUAGE_DIRECTORY ]; then
+                        mkdir -p $LANGUAGE_DIRECTORY
+                        echo "[NOTICE] Add project language \"$LANGUAGE_KEY\""
+                       NEED_UPDATE=1
+                fi
+        done
+       if [ $NEED_UPDATE == 1 ]; then
+               echo "[NOTICE] Update translation project"
+               pootle-manage update_translation_projects --project=$1 1>/dev/null 2>/dev/null
+       fi
+}
+
+SAVEIFS=$IFS
+
+XSL_LLXML2TEMPLATE="/var/www/vhosts/pootle.typo3.org/home/scripts/etc/xsl/llxml2template.xsl"
+TEMP_DIRECTORY="/var/www/vhosts/pootle.typo3.org/home/tmp"
+
+EXTENSION=$1
+GIT=$2
+
+PROJECT_NAME="TYPO3.TYPO3.ext.$EXTENSION"
+PROJECT_DIRECTORY="/var/www/vhosts/pootle.typo3.org/pootle/po/$PROJECT_NAME"
+#
+# Check if the project exist
+if [ ! -d $PROJECT_DIRECTORY ]; then
+       echo
+       echo "[ERROR] Project $PROJECT_NAME doesn't exist in Pootle, please create it before using this script"
+       echo "[ERROR] Please create directory: $PROJECT_DIRECTORY"
+       onexit 1
+fi
+
+#
+# Check if the templates directory exist
+TEMPLATE_DIRECTORY="$PROJECT_DIRECTORY/templates"
+if [ ! -d $TEMPLATE_DIRECTORY ]; then
+        echo
+        echo "[INFO] Templates directory for project $PROJECT_NAME doesn't exist in Pootle"
+       echo "[NOTICE] Create the templates directory"
+       mkdir -p $TEMPLATE_DIRECTORY
+fi
+
+cd $TEMP_DIRECTORY >/dev/null
+
+echo "[INFO] Cloning $EXTENSION from $GIT"
+
+EXTENSION_DIR=$(echo "${EXTENSION}" | tr '[:upper:]' '[:lower:]')
+
+#
+# Download t3x
+rm -rf $EXTENSION
+git clone $GIT $EXTENSION
+
+IFS=$(echo -en "\n\b")
+
+#
+# Create directory structure for templates
+for FILE in $(find $EXTENSION/ -name \*.xml); do
+       grep "T3locallang" "$FILE" 1> /dev/null
+       if [ $? == 0 ]; then
+               mkdir -p $TEMP_DIRECTORY/TYPO3.TYPO3.ext.$(dirname $FILE);
+               cp $FILE $TEMP_DIRECTORY/TYPO3.TYPO3.ext.$FILE;
+       fi
+done
+for FILE in $(find $EXTENSION/ -name \*.xlf); do
+       # Count number of dots in filename to skip e.g., de.locallang.xlf
+       DOTS=$(echo -n $(basename $FILE) | sed 's/[^.]//g' | wc -c)
+       if [ $DOTS -eq 1 ]; then
+               mkdir -p $TEMP_DIRECTORY/TYPO3.TYPO3.ext.$(dirname $FILE);
+               cp $FILE $TEMP_DIRECTORY/TYPO3.TYPO3.ext.$FILE;
+       fi
+done
+
+cd $TEMP_DIRECTORY
+for FILE in $(find TYPO3.TYPO3.ext.$EXTENSION -name \*.xml); do
+       grep "T3locallang" "$FILE" 1> /dev/null
+       if [ $? == 0 ]; then
+               XLF_FILE=$(echo $FILE | sed s/.xml$/.xlf/)
+               if [ ! -f $XLF_FILE ]; then
+                       echo "[INFO] Convert LLXML to XLIFF for file $FILE"
+                       if [ ! -f $XLF_FILE ]; then
+                               xsltproc --stringparam lang en \
+                                       --stringparam extension $EXTENSION \
+                                       --stringparam date "$(date -u +"%Y-%m-%dT%H:%M:%SZ")" $XSL_LLXML2TEMPLATE $FILE | \
+                                       xmlstarlet ed --insert "/xliff/file/body/trans-unit" --type attr -n xml:space -v preserve > $XLF_FILE
+                       fi
+               fi
+       fi
+       rm -f $FILE
+done
+
+#
+# Deploy new templates files
+echo "[INFO] Deploy new templates to $TEMPLATE_DIRECTORY"
+cd $TEMPLATE_DIRECTORY/../../ && \
+       tar cfzp $TEMP_DIRECTORY/backup/$PROJECT_NAME-$(date +%Y%m%d).tar.gz $PROJECT_NAME && \
+       cd - && \
+       rm -Rf $TEMPLATE_DIRECTORY && \
+       mkdir -p $TEMPLATE_DIRECTORY && \
+       cp -a $TEMP_DIRECTORY/$PROJECT_NAME/* $TEMPLATE_DIRECTORY || \
+               onexit 5
+
+#
+# Check project available language
+check_available_languages $PROJECT_NAME
+
+#
+# Update project from templates
+echo "[INFO] Update from templates"
+pootle-manage update_from_templates --project=$PROJECT_NAME 1>/dev/null 2>/dev/null
+
+echo "[INFO] Refresh statistics"
+pootle-manage refresh_stats --project=$PROJECT_NAME 1>/dev/null 2>/dev/null
+
+#
+# Cleanup
+cd $TEMP_DIRECTORY
+if [ -d $EXTENSION ]; then
+       rm -rf $EXTENSION
+       rm -rf TYPO3.TYPO3.ext.$EXTENSION
+fi
+
+IFS=$SAVEIFS
+
+onexit