wok-current rev 17086

tramys-client, tramys-server: now using http headers only (no more additional info in the server logs); added a lot of comments.
author Aleksej Bobylev <al.bobylev@gmail.com>
date Tue Aug 26 13:31:50 2014 +0300 (2014-08-26)
parents ce9cbd90365a
children 46f8c5dbe3be
files tramys-client/receipt tramys-client/stuff/tramys2 tramys-client/stuff/tramys2.desktop tramys-server/receipt tramys-server/stuff/tramys2.cgi
line diff
     1.1 --- a/tramys-client/receipt	Mon Aug 25 10:52:38 2014 +0200
     1.2 +++ b/tramys-client/receipt	Tue Aug 26 13:31:50 2014 +0300
     1.3 @@ -1,7 +1,7 @@
     1.4  # SliTaz package receipt.
     1.5  
     1.6  PACKAGE="tramys-client"
     1.7 -VERSION="140824"
     1.8 +VERSION="140826"
     1.9  CATEGORY="system-tools"
    1.10  SHORT_DESC="Tool for managing translation files for SliTaz GNU/Linux, client part"
    1.11  MAINTAINER="al.bobylev@gmail.com"
     2.1 --- a/tramys-client/stuff/tramys2	Mon Aug 25 10:52:38 2014 +0200
     2.2 +++ b/tramys-client/stuff/tramys2	Tue Aug 26 13:31:50 2014 +0300
     2.3 @@ -6,6 +6,7 @@
     2.4  . /etc/slitaz/slitaz.conf
     2.5  . /lib/libtaz.sh
     2.6  
     2.7 +# Ask for root access in order to install the files in the system.
     2.8  if [ $(id -u) != 0 ]; then
     2.9  	exec tazbox su $0 $@; exit 0
    2.10  fi
    2.11 @@ -14,8 +15,14 @@
    2.12  WORKING=$(mktemp -d)
    2.13  LOG="/tmp/tramys.log"
    2.14  TGZ="/tmp/tramys.tgz"
    2.15 +URL="http://cook.slitaz.org/tramys2.cgi"
    2.16 +
    2.17 +# Common Yad options.
    2.18  YADCONF="--center --window-icon=config-language --image=config-language --image-on-top"
    2.19  
    2.20 +# First step. Describes the functions of the program.
    2.21 +# It is possible to set the language.
    2.22 +# Also here the user has the ability to stop the program.
    2.23  yad $YADCONF --title="tramys (1/3)" --text="$(_ \
    2.24  'Now translations for all installed programs will be found and downloaded.
    2.25  You can change locale if you want, or proceed.
    2.26 @@ -27,23 +34,44 @@
    2.27  	1) exit 0 ;;
    2.28  esac
    2.29  
    2.30 -busybox wget -U "$(cd $INSTALLED; ls -1 | tr '\n' ' ')" \
    2.31 -	"http://cook.slitaz.org/tramys2.cgi?lang=$LANG&rel=$(cat /etc/slitaz-release)" \
    2.32 -	-O - | tee $LOG | \
    2.33 +# your locale -> HTTP_ACCEPT_LANGUAGE
    2.34 +# your SliTaz release -> HTTP_ACCEPT (different releases have different translations)
    2.35 +# your installed packages list -> HTTP_COOKIE (list=...)
    2.36 +# Note clean address "tramys2.cgi" in the server access logs.
    2.37 +#
    2.38 +# Server sending and Yad shows user useful info using log widget.
    2.39 +# We are temporarily stored this log in the $LOG file in order to get
    2.40 +# a download token (see below).
    2.41 +# Here the user can refuse to download the file.
    2.42 +busybox wget --header "Accept-Language: $LANG" \
    2.43 +	--header "Accept: $(cat /etc/slitaz-release)" \
    2.44 +	--header "Cookie: list=$(cd $INSTALLED; ls -1 | tr '\n' ' ')" \
    2.45 +	$URL -O - | tee $LOG | \
    2.46  	yad $YADCONF --title="tramys (2/3)" --progress --width=320 --text="$(_ \
    2.47  'The server processes the request.
    2.48  Please wait.')" \
    2.49  	--enable-log --log-expanded \
    2.50  	--button "gtk-cancel:1" --button "gtk-go-forward:0"
    2.51 -case $? in
    2.52 -	1) exit 0 ;;
    2.53 +ANSWER=$?
    2.54 +
    2.55 +# In the last line of log server gives us a download token.
    2.56 +# We can download archive which the server has prepared for us.
    2.57 +# Get download token and remove log.
    2.58 +DLKEY=$(tail -n1 $LOG); rm -f $LOG
    2.59 +
    2.60 +case $ANSWER in
    2.61 +	1)
    2.62 +		# We need to remove archive that the user has refused to download.
    2.63 +		# This command passed in HTTP_COOKIE (rm=...)
    2.64 +		busybox wget --header "Cookie: rm=$DLKEY" $URL -O /dev/null; exit 0 ;;
    2.65  esac
    2.66  
    2.67 -DLKEY=$(tail -n1 $LOG); rm -f $LOG
    2.68 -
    2.69 -busybox wget "http://cook.slitaz.org/tramys2.cgi?dl=$DLKEY" -O $TGZ 2>&1 | \
    2.70 -yad $YADCONF --title="tramys (3/3)" --progress --pulsate --width=320 \
    2.71 -	--text="$(_ \
    2.72 +# We want to download the file. Show pulsate progress bar.
    2.73 +# This command passed in HTTP_COOKIE (dl=...)
    2.74 +# Also here the user can terminate file downloading.
    2.75 +busybox wget --header "Cookie: dl=$DLKEY" $URL -O $TGZ 2>&1 | \
    2.76 +	yad $YADCONF --title="tramys (3/3)" --progress --pulsate --width=320 \
    2.77 +		--text="$(_ \
    2.78  'Downloading in progress.
    2.79  Please wait.')" \
    2.80  	--button "gtk-cancel:1" --button "gtk-ok:0"
    2.81 @@ -51,11 +79,21 @@
    2.82  	1) exit 0 ;;
    2.83  esac | \
    2.84  
    2.85 +# Unpack archive content to a temporary folder.
    2.86  busybox tar -xz -C $WORKING -f $TGZ
    2.87 +# All folders and files in the archive are owned by user www and group www.
    2.88 +# This is because the CGI script on the server is executed by the user www.
    2.89 +# If we had just unpacked the archive content into our file system, then there
    2.90 +# would be a big trouble. For example, all folders: /, /usr, /usr/share,
    2.91 +# /usr/share/locale, etc. would be owned by user www and become unavailable
    2.92 +# for a regular user. So force all folders and files to root own.
    2.93  chown -R root:root $WORKING
    2.94 +# copy all translation files to root file system.
    2.95  cp -fpr $WORKING/* /
    2.96 +# remove temporary folder and file, they are no longer needed.
    2.97  rm -f $TGZ
    2.98  rm -rf $WORKING
    2.99  
   2.100 +# Final message.
   2.101  yad $YADCONF --title="tramys" --text="$(_ \
   2.102  'Translation files have been installed in your system.')"
     3.1 --- a/tramys-client/stuff/tramys2.desktop	Mon Aug 25 10:52:38 2014 +0200
     3.2 +++ b/tramys-client/stuff/tramys2.desktop	Tue Aug 26 13:31:50 2014 +0300
     3.3 @@ -2,6 +2,7 @@
     3.4  Type=Application
     3.5  Name=tramys
     3.6  Comment=Translate My SliTaz!
     3.7 +Comment[ru]=Переведите свой SliTaz!
     3.8  Exec=tramys2
     3.9  Icon=config-language
    3.10  Categories=GTK;Settings;DesktopSettings;
     4.1 --- a/tramys-server/receipt	Mon Aug 25 10:52:38 2014 +0200
     4.2 +++ b/tramys-server/receipt	Tue Aug 26 13:31:50 2014 +0300
     4.3 @@ -1,7 +1,7 @@
     4.4  # SliTaz package receipt.
     4.5  
     4.6  PACKAGE="tramys-server"
     4.7 -VERSION="140824"
     4.8 +VERSION="140826"
     4.9  CATEGORY="system-tools"
    4.10  SHORT_DESC="Tool for managing translation files for SliTaz GNU/Linux, server part"
    4.11  MAINTAINER="al.bobylev@gmail.com"
     5.1 --- a/tramys-server/stuff/tramys2.cgi	Mon Aug 25 10:52:38 2014 +0200
     5.2 +++ b/tramys-server/stuff/tramys2.cgi	Tue Aug 26 13:31:50 2014 +0300
     5.3 @@ -4,40 +4,34 @@
     5.4  # Aleksej Bobylev <al.bobylev@gmail.com>, 2014
     5.5  
     5.6  # How to use:
     5.7 -# 1. tramys2.cgi?lang=$LANG&rel=$RELEASE to generate archive
     5.8 -# Pass packages list in HTTP_USER_AGENT header
     5.9 -# (seems it have no restrictions for length and no encoded symbols ' ' and '+')
    5.10 -# 2. tramys2.cgi?dl=$DL_KEY to download archive (user can cancel downloading)
    5.11 +# 1. Request for archive:
    5.12 +#    HTTP_ACCEPT_LANGUAGE -> users locale
    5.13 +#    HTTP_ACCEPT -> SliTaz release
    5.14 +#    HTTP_COOKIE (list=...) -> space-separated list of packages to process
    5.15 +#
    5.16 +# 2. Remove archive that the user has refused to download:
    5.17 +#    HTTP_COOKIE (rm=DLKEY) -> remove /tmp/tmp.DLKEY.tgz file
    5.18 +#
    5.19 +# 3. Send archive to user:
    5.20 +#    HTTP_COOKIE (dl=DLKEY) -> send /tmp/tmp.DLKEY.tgz file
    5.21  
    5.22  . /usr/bin/httpd_helper.sh
    5.23  
    5.24 -WORKING=$(mktemp -d)
    5.25 -DATADIR=/usr/share/tramys
    5.26 +WORKING=$(busybox mktemp -d) # make temp working dir /tmp/tmp.??????
    5.27 +DATADIR=/usr/share/tramys    # this folder contains lists
    5.28  
    5.29 -# hide script
    5.30 -if [ "x$(GET lang)$(GET rel)$(GET dl)" == "x" ]; then
    5.31 -	echo -e "HTTP/1.1 404 Not Found\nContent-Type: text/html\n\n<!DOCTYPE html><html><head><title>404 - Not Found</title></head><body><h1>404 - Not Found</h1></body></html>"
    5.32 -	exit
    5.33 -fi
    5.34 +# Get user settings from HTTP headers.
    5.35 +lang="$HTTP_ACCEPT_LANGUAGE"
    5.36 +rel="$HTTP_ACCEPT"
    5.37 +cmd="${HTTP_COOKIE%%=*}"
    5.38 +arg="${HTTP_COOKIE#*=}"
    5.39  
    5.40 -# begin: compress and give to client
    5.41 -if [ "x$(GET dl)" != "x" ]; then
    5.42 -	WORKING="/tmp/tmp.$(echo $(GET dl) | tr -cd 'A-Za-z0-9')" # avoid relative paths
    5.43 -	cat <<EOT
    5.44 -Content-Type: application/x-compressed-tar
    5.45 -Content-Length: $(stat -c %s $WORKING.tgz)
    5.46 -Content-Disposition: attachment; filename=tramys.tgz
    5.47 +#-----------#
    5.48 +# Functions #
    5.49 +#-----------#
    5.50  
    5.51 -EOT
    5.52 -	cat $WORKING.tgz
    5.53 -	rm -f $WORKING.tgz
    5.54 -	exit 0
    5.55 -fi
    5.56 -# end: compress and give to client
    5.57 -
    5.58 -
    5.59 -# prepare list for search
    5.60 -# original GNU gettext searches precisely in this order
    5.61 +# Prepare list for search.
    5.62 +# Original GNU gettext searches precisely in this order.
    5.63  locales_list() {
    5.64  	LL=$(echo $1 | sed 's|^\([^_.@]*\).*$|\1|')
    5.65  	CC=$(echo $1 | sed -n '/_/s|^[^_]*\(_[^.@]*\).*$|\1|p')
    5.66 @@ -59,73 +53,127 @@
    5.67  	[ "$ee" ]					&& echo -n "$LL$ee "
    5.68  	echo "$LL"
    5.69  }
    5.70 -MY_LOCALES=$(locales_list $(GET lang))
    5.71 +MY_LOCALES=$(locales_list $lang)
    5.72  
    5.73 -# constants to use in lists
    5.74 -US="/usr/share"
    5.75 -LC="LC_MESSAGES"
    5.76 -PY="/usr/lib/python2.7/site-packages"
    5.77 -R="/usr/lib/R/library"
    5.78 -RT="$R/translations/%/$LC"
    5.79 +# Search and copy translation files
    5.80 +copy_translations() {
    5.81 +	# for all packages in list
    5.82 +	for P in $arg; do
    5.83  
    5.84 -# supported 4.0 (as stable now) an cooking (rolling, 5.0)
    5.85 -# don't know what to do with "arm" and "x86_64" woks
    5.86 -case $(GET rel) in
    5.87 -	4*) PREFIX="stable_"; WOK="stable"  ;;
    5.88 -	*)  PREFIX="";       WOK="cooking" ;;
    5.89 -esac
    5.90 -WOK="/home/slitaz/$WOK/chroot/home/slitaz/wok"
    5.91 +		echo "$((100*$NUM/$PKGNUM))" # send percentage to Yad client
    5.92 +		NUM=$(($NUM+1))              # next package
    5.93  
    5.94 -PKGNUM=$(echo $HTTP_USER_AGENT | wc -w)
    5.95 -NUM=1
    5.96 +		# for all list types
    5.97 +		for list_type in mo qm; do
    5.98 +			IFS=$'\n'
    5.99 +			for line in $(grep -e "^$P	" $DATADIR/$PREFIX$list_type.list); do
   5.100 +				locales=$(echo $line | cut -d'	' -f2)
   5.101 +				names=$(echo $line | cut -d'	' -f3)
   5.102 +					[ "x$names" == "x" ] && names=$P
   5.103 +				paths=$(echo $line | cut -d'	' -f4)
   5.104 +					[ "x$paths" == "x" ] && paths="$US/locale/%/$LC"
   5.105  
   5.106 -echo -e "Content-Type: text/plain\n\n" # to Yad client
   5.107 -echo "#Number of packages: $PKGNUM"
   5.108 -echo "#Searching in progress..."
   5.109 +				IFS=' '
   5.110 +				# for all valid locale variants
   5.111 +				for locale in $MY_LOCALES; do
   5.112 +					if $(echo " $locales " | grep -q " $locale "); then
   5.113  
   5.114 -for P in $HTTP_USER_AGENT; do
   5.115 +						# for all file names
   5.116 +						for name in $names; do
   5.117 +							# for all paths
   5.118 +							for path in $paths; do
   5.119 +								# substitute variables and "%"
   5.120 +								eval "fullname=${path//%/$locale}/${name//%/$locale}.$list_type"
   5.121  
   5.122 -	echo "$((100*$NUM/$PKGNUM))" # percentage to Yad client
   5.123 -	NUM=$(($NUM+1))
   5.124 -
   5.125 -	for list_type in mo qm; do
   5.126 -		IFS=$'\n'
   5.127 -		for line in $(grep -e "^$P	" $DATADIR/$PREFIX$list_type.list); do
   5.128 -			locales=$(echo $line | cut -d'	' -f2)
   5.129 -			names=$(echo $line | cut -d'	' -f3)
   5.130 -				[ "x$names" == "x" ] && names=$P
   5.131 -			pathes=$(echo $line | cut -d'	' -f4)
   5.132 -				[ "x$pathes" == "x" ] && pathes="$US/locale/%/$LC"
   5.133 -
   5.134 -			IFS=' '
   5.135 -			for locale in $MY_LOCALES; do
   5.136 -				if $(echo " $locales " | grep -q " $locale "); then
   5.137 -
   5.138 -					for name in $names; do
   5.139 -						for path in $pathes; do
   5.140 -							eval "fullname=${path//%/$locale}/${name//%/$locale}.$list_type"
   5.141 -							mkdir -p $WORKING$(dirname $fullname)
   5.142 -							cp -pf $WOK/$P/install$fullname $WORKING$fullname
   5.143 +								# copy translation file to working dir
   5.144 +								mkdir -p $WORKING$(dirname $fullname)
   5.145 +								cp -pf $WOK/$P/install$fullname $WORKING$fullname
   5.146 +							done
   5.147  						done
   5.148 -					done
   5.149 -					break
   5.150 -				fi
   5.151 +						break
   5.152 +					fi
   5.153 +				done
   5.154  			done
   5.155  		done
   5.156  	done
   5.157 -done
   5.158 +}
   5.159  
   5.160 -echo "#" # to Yad client log
   5.161 -echo "#Preparing archive. Please wait"
   5.162 +#----------#
   5.163 +#   Main   #
   5.164 +#----------#
   5.165  
   5.166 -busybox tar -czf $WORKING.tgz -C $WORKING .
   5.167 -rm -rf $WORKING
   5.168 +# Branch commands: list, rm, dl.
   5.169 +case "x$cmd" in
   5.170 +	xlist) # Main actions: get list, search translations, make an archive.
   5.171 +		# constants to use in lists
   5.172 +		US="/usr/share"
   5.173 +		LC="LC_MESSAGES"
   5.174 +		PY="/usr/lib/python2.7/site-packages"
   5.175 +		R="/usr/lib/R/library"
   5.176 +		RT="$R/translations/%/$LC"
   5.177  
   5.178 -echo "#" # to Yad client log
   5.179 -echo "#Done!"
   5.180 -echo "#Now you can proceed to downloading"
   5.181 -echo "#and installing your translations."
   5.182 -echo "#File size: $(stat -c %s $WORKING.tgz)"
   5.183 +		# Supported 4.0 (as stable now) and cooking (rolling, 5.0)
   5.184 +		# Don't know what to do with "arm" and "x86_64" woks ???
   5.185 +		case "x$rel" in
   5.186 +			x4*|xstable) PREFIX="stable_"; WOK="stable"  ;;
   5.187 +			*)           PREFIX="";        WOK="cooking" ;;
   5.188 +		esac
   5.189 +		# Path to the specified WOK in the SliTaz server.
   5.190 +		WOK="/home/slitaz/$WOK/chroot/home/slitaz/wok"
   5.191  
   5.192 -echo "${WORKING#*.}" # give download key to Yad client
   5.193 +		PKGNUM=$(echo $arg | wc -w) # number of packages in the list
   5.194 +		NUM=1 # initial value
   5.195 +
   5.196 +		echo -e "Content-Type: text/plain\n\n" # to Yad client
   5.197 +		echo "#Number of packages: $PKGNUM"    # Message to Yad log
   5.198 +		echo "#Searching in progress..."       # And another one
   5.199 +
   5.200 +		copy_translations
   5.201 +
   5.202 +		echo "#"                                  # Message to Yad log
   5.203 +		echo "#Preparing archive. Please wait..." # And another one
   5.204 +
   5.205 +		# Make the archive from working dir and remove temp working dir.
   5.206 +		busybox tar -czf $WORKING.tgz -C $WORKING .
   5.207 +		rm -rf $WORKING
   5.208 +
   5.209 +		echo "#" # to Yad client log
   5.210 +		echo "#Done!"
   5.211 +		echo "#Now you can proceed to downloading"
   5.212 +		echo "#and installing your translations."
   5.213 +		echo "#File size: $(stat -c %s $WORKING.tgz) bytes."
   5.214 +
   5.215 +		echo "${WORKING#*.}" # give download token to Yad client
   5.216 +		exit 0 ;;
   5.217 +
   5.218 +	xrm) # Remove archive.
   5.219 +		# Avoid relative path to avoid removing of any system file.
   5.220 +		archive="/tmp/tmp.$(echo $arg | tr -cd 'A-Za-z0-9').tgz"
   5.221 +		rm -f $archive
   5.222 +		cat <<EOT
   5.223 +Content-Type: text/plain
   5.224 +Content-Length: 0
   5.225 +
   5.226 +EOT
   5.227 +		exit 0 ;;
   5.228 +
   5.229 +	xdl) # Send archive to client.
   5.230 +		# Avoid relative path to avoid hijacking of any system file.
   5.231 +		archive="/tmp/tmp.$(echo $arg | tr -cd 'A-Za-z0-9').tgz"
   5.232 +		cat <<EOT
   5.233 +Content-Type: application/x-compressed-tar
   5.234 +Content-Length: $(stat -c %s $archive)
   5.235 +Content-Disposition: attachment; filename=tramys.tgz
   5.236 +
   5.237 +EOT
   5.238 +		cat $archive
   5.239 +		# Remove archive after sending.
   5.240 +		rm -f $archive
   5.241 +		exit 0 ;;
   5.242 +
   5.243 +	*) # Hide the script from the web bots and browsers.
   5.244 +		echo -e "HTTP/1.0 404 Not Found\nContent-Type: text/html\n\n<!DOCTYPE html><html><head><title>404 - Not Found</title></head><body><h1>404 - Not Found</h1></body></html>"
   5.245 +		exit ;;
   5.246 +esac
   5.247 +
   5.248  exit 0