wok diff tramys-server/stuff/tramys2.cgi @ 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 1794963d7994
children bf52d3ac4b95
line diff
     1.1 --- a/tramys-server/stuff/tramys2.cgi	Sun Aug 24 23:25:23 2014 +0300
     1.2 +++ b/tramys-server/stuff/tramys2.cgi	Tue Aug 26 13:31:50 2014 +0300
     1.3 @@ -4,40 +4,34 @@
     1.4  # Aleksej Bobylev <al.bobylev@gmail.com>, 2014
     1.5  
     1.6  # How to use:
     1.7 -# 1. tramys2.cgi?lang=$LANG&rel=$RELEASE to generate archive
     1.8 -# Pass packages list in HTTP_USER_AGENT header
     1.9 -# (seems it have no restrictions for length and no encoded symbols ' ' and '+')
    1.10 -# 2. tramys2.cgi?dl=$DL_KEY to download archive (user can cancel downloading)
    1.11 +# 1. Request for archive:
    1.12 +#    HTTP_ACCEPT_LANGUAGE -> users locale
    1.13 +#    HTTP_ACCEPT -> SliTaz release
    1.14 +#    HTTP_COOKIE (list=...) -> space-separated list of packages to process
    1.15 +#
    1.16 +# 2. Remove archive that the user has refused to download:
    1.17 +#    HTTP_COOKIE (rm=DLKEY) -> remove /tmp/tmp.DLKEY.tgz file
    1.18 +#
    1.19 +# 3. Send archive to user:
    1.20 +#    HTTP_COOKIE (dl=DLKEY) -> send /tmp/tmp.DLKEY.tgz file
    1.21  
    1.22  . /usr/bin/httpd_helper.sh
    1.23  
    1.24 -WORKING=$(mktemp -d)
    1.25 -DATADIR=/usr/share/tramys
    1.26 +WORKING=$(busybox mktemp -d) # make temp working dir /tmp/tmp.??????
    1.27 +DATADIR=/usr/share/tramys    # this folder contains lists
    1.28  
    1.29 -# hide script
    1.30 -if [ "x$(GET lang)$(GET rel)$(GET dl)" == "x" ]; then
    1.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>"
    1.32 -	exit
    1.33 -fi
    1.34 +# Get user settings from HTTP headers.
    1.35 +lang="$HTTP_ACCEPT_LANGUAGE"
    1.36 +rel="$HTTP_ACCEPT"
    1.37 +cmd="${HTTP_COOKIE%%=*}"
    1.38 +arg="${HTTP_COOKIE#*=}"
    1.39  
    1.40 -# begin: compress and give to client
    1.41 -if [ "x$(GET dl)" != "x" ]; then
    1.42 -	WORKING="/tmp/tmp.$(echo $(GET dl) | tr -cd 'A-Za-z0-9')" # avoid relative paths
    1.43 -	cat <<EOT
    1.44 -Content-Type: application/x-compressed-tar
    1.45 -Content-Length: $(stat -c %s $WORKING.tgz)
    1.46 -Content-Disposition: attachment; filename=tramys.tgz
    1.47 +#-----------#
    1.48 +# Functions #
    1.49 +#-----------#
    1.50  
    1.51 -EOT
    1.52 -	cat $WORKING.tgz
    1.53 -	rm -f $WORKING.tgz
    1.54 -	exit 0
    1.55 -fi
    1.56 -# end: compress and give to client
    1.57 -
    1.58 -
    1.59 -# prepare list for search
    1.60 -# original GNU gettext searches precisely in this order
    1.61 +# Prepare list for search.
    1.62 +# Original GNU gettext searches precisely in this order.
    1.63  locales_list() {
    1.64  	LL=$(echo $1 | sed 's|^\([^_.@]*\).*$|\1|')
    1.65  	CC=$(echo $1 | sed -n '/_/s|^[^_]*\(_[^.@]*\).*$|\1|p')
    1.66 @@ -59,73 +53,127 @@
    1.67  	[ "$ee" ]					&& echo -n "$LL$ee "
    1.68  	echo "$LL"
    1.69  }
    1.70 -MY_LOCALES=$(locales_list $(GET lang))
    1.71 +MY_LOCALES=$(locales_list $lang)
    1.72  
    1.73 -# constants to use in lists
    1.74 -US="/usr/share"
    1.75 -LC="LC_MESSAGES"
    1.76 -PY="/usr/lib/python2.7/site-packages"
    1.77 -R="/usr/lib/R/library"
    1.78 -RT="$R/translations/%/$LC"
    1.79 +# Search and copy translation files
    1.80 +copy_translations() {
    1.81 +	# for all packages in list
    1.82 +	for P in $arg; do
    1.83  
    1.84 -# supported 4.0 (as stable now) an cooking (rolling, 5.0)
    1.85 -# don't know what to do with "arm" and "x86_64" woks
    1.86 -case $(GET rel) in
    1.87 -	4*) PREFIX="stable_"; WOK="stable"  ;;
    1.88 -	*)  PREFIX="";       WOK="cooking" ;;
    1.89 -esac
    1.90 -WOK="/home/slitaz/$WOK/chroot/home/slitaz/wok"
    1.91 +		echo "$((100*$NUM/$PKGNUM))" # send percentage to Yad client
    1.92 +		NUM=$(($NUM+1))              # next package
    1.93  
    1.94 -PKGNUM=$(echo $HTTP_USER_AGENT | wc -w)
    1.95 -NUM=1
    1.96 +		# for all list types
    1.97 +		for list_type in mo qm; do
    1.98 +			IFS=$'\n'
    1.99 +			for line in $(grep -e "^$P	" $DATADIR/$PREFIX$list_type.list); do
   1.100 +				locales=$(echo $line | cut -d'	' -f2)
   1.101 +				names=$(echo $line | cut -d'	' -f3)
   1.102 +					[ "x$names" == "x" ] && names=$P
   1.103 +				paths=$(echo $line | cut -d'	' -f4)
   1.104 +					[ "x$paths" == "x" ] && paths="$US/locale/%/$LC"
   1.105  
   1.106 -echo -e "Content-Type: text/plain\n\n" # to Yad client
   1.107 -echo "#Number of packages: $PKGNUM"
   1.108 -echo "#Searching in progress..."
   1.109 +				IFS=' '
   1.110 +				# for all valid locale variants
   1.111 +				for locale in $MY_LOCALES; do
   1.112 +					if $(echo " $locales " | grep -q " $locale "); then
   1.113  
   1.114 -for P in $HTTP_USER_AGENT; do
   1.115 +						# for all file names
   1.116 +						for name in $names; do
   1.117 +							# for all paths
   1.118 +							for path in $paths; do
   1.119 +								# substitute variables and "%"
   1.120 +								eval "fullname=${path//%/$locale}/${name//%/$locale}.$list_type"
   1.121  
   1.122 -	echo "$((100*$NUM/$PKGNUM))" # percentage to Yad client
   1.123 -	NUM=$(($NUM+1))
   1.124 -
   1.125 -	for list_type in mo qm; do
   1.126 -		IFS=$'\n'
   1.127 -		for line in $(grep -e "^$P	" $DATADIR/$PREFIX$list_type.list); do
   1.128 -			locales=$(echo $line | cut -d'	' -f2)
   1.129 -			names=$(echo $line | cut -d'	' -f3)
   1.130 -				[ "x$names" == "x" ] && names=$P
   1.131 -			pathes=$(echo $line | cut -d'	' -f4)
   1.132 -				[ "x$pathes" == "x" ] && pathes="$US/locale/%/$LC"
   1.133 -
   1.134 -			IFS=' '
   1.135 -			for locale in $MY_LOCALES; do
   1.136 -				if $(echo " $locales " | grep -q " $locale "); then
   1.137 -
   1.138 -					for name in $names; do
   1.139 -						for path in $pathes; do
   1.140 -							eval "fullname=${path//%/$locale}/${name//%/$locale}.$list_type"
   1.141 -							mkdir -p $WORKING$(dirname $fullname)
   1.142 -							cp -pf $WOK/$P/install$fullname $WORKING$fullname
   1.143 +								# copy translation file to working dir
   1.144 +								mkdir -p $WORKING$(dirname $fullname)
   1.145 +								cp -pf $WOK/$P/install$fullname $WORKING$fullname
   1.146 +							done
   1.147  						done
   1.148 -					done
   1.149 -					break
   1.150 -				fi
   1.151 +						break
   1.152 +					fi
   1.153 +				done
   1.154  			done
   1.155  		done
   1.156  	done
   1.157 -done
   1.158 +}
   1.159  
   1.160 -echo "#" # to Yad client log
   1.161 -echo "#Preparing archive. Please wait"
   1.162 +#----------#
   1.163 +#   Main   #
   1.164 +#----------#
   1.165  
   1.166 -busybox tar -czf $WORKING.tgz -C $WORKING .
   1.167 -rm -rf $WORKING
   1.168 +# Branch commands: list, rm, dl.
   1.169 +case "x$cmd" in
   1.170 +	xlist) # Main actions: get list, search translations, make an archive.
   1.171 +		# constants to use in lists
   1.172 +		US="/usr/share"
   1.173 +		LC="LC_MESSAGES"
   1.174 +		PY="/usr/lib/python2.7/site-packages"
   1.175 +		R="/usr/lib/R/library"
   1.176 +		RT="$R/translations/%/$LC"
   1.177  
   1.178 -echo "#" # to Yad client log
   1.179 -echo "#Done!"
   1.180 -echo "#Now you can proceed to downloading"
   1.181 -echo "#and installing your translations."
   1.182 -echo "#File size: $(stat -c %s $WORKING.tgz)"
   1.183 +		# Supported 4.0 (as stable now) and cooking (rolling, 5.0)
   1.184 +		# Don't know what to do with "arm" and "x86_64" woks ???
   1.185 +		case "x$rel" in
   1.186 +			x4*|xstable) PREFIX="stable_"; WOK="stable"  ;;
   1.187 +			*)           PREFIX="";        WOK="cooking" ;;
   1.188 +		esac
   1.189 +		# Path to the specified WOK in the SliTaz server.
   1.190 +		WOK="/home/slitaz/$WOK/chroot/home/slitaz/wok"
   1.191  
   1.192 -echo "${WORKING#*.}" # give download key to Yad client
   1.193 +		PKGNUM=$(echo $arg | wc -w) # number of packages in the list
   1.194 +		NUM=1 # initial value
   1.195 +
   1.196 +		echo -e "Content-Type: text/plain\n\n" # to Yad client
   1.197 +		echo "#Number of packages: $PKGNUM"    # Message to Yad log
   1.198 +		echo "#Searching in progress..."       # And another one
   1.199 +
   1.200 +		copy_translations
   1.201 +
   1.202 +		echo "#"                                  # Message to Yad log
   1.203 +		echo "#Preparing archive. Please wait..." # And another one
   1.204 +
   1.205 +		# Make the archive from working dir and remove temp working dir.
   1.206 +		busybox tar -czf $WORKING.tgz -C $WORKING .
   1.207 +		rm -rf $WORKING
   1.208 +
   1.209 +		echo "#" # to Yad client log
   1.210 +		echo "#Done!"
   1.211 +		echo "#Now you can proceed to downloading"
   1.212 +		echo "#and installing your translations."
   1.213 +		echo "#File size: $(stat -c %s $WORKING.tgz) bytes."
   1.214 +
   1.215 +		echo "${WORKING#*.}" # give download token to Yad client
   1.216 +		exit 0 ;;
   1.217 +
   1.218 +	xrm) # Remove archive.
   1.219 +		# Avoid relative path to avoid removing of any system file.
   1.220 +		archive="/tmp/tmp.$(echo $arg | tr -cd 'A-Za-z0-9').tgz"
   1.221 +		rm -f $archive
   1.222 +		cat <<EOT
   1.223 +Content-Type: text/plain
   1.224 +Content-Length: 0
   1.225 +
   1.226 +EOT
   1.227 +		exit 0 ;;
   1.228 +
   1.229 +	xdl) # Send archive to client.
   1.230 +		# Avoid relative path to avoid hijacking of any system file.
   1.231 +		archive="/tmp/tmp.$(echo $arg | tr -cd 'A-Za-z0-9').tgz"
   1.232 +		cat <<EOT
   1.233 +Content-Type: application/x-compressed-tar
   1.234 +Content-Length: $(stat -c %s $archive)
   1.235 +Content-Disposition: attachment; filename=tramys.tgz
   1.236 +
   1.237 +EOT
   1.238 +		cat $archive
   1.239 +		# Remove archive after sending.
   1.240 +		rm -f $archive
   1.241 +		exit 0 ;;
   1.242 +
   1.243 +	*) # Hide the script from the web bots and browsers.
   1.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>"
   1.245 +		exit ;;
   1.246 +esac
   1.247 +
   1.248  exit 0