From a7ce04a03dadcfcb39cc963f3230a91af87fa385 Mon Sep 17 00:00:00 2001 From: Kamil Bugajski Date: Thu, 16 Aug 2012 19:20:04 +0200 Subject: [PATCH 1/5] parallelisierung mit angepasster cfg --- bin/generationlib | 169 ++++++++++++++++++++++++++++++++++++++++++ bin/svgconvert.sh | 8 ++ cfg/punkteraetsel.cfg | 6 +- 3 files changed, 180 insertions(+), 3 deletions(-) create mode 100644 bin/generationlib create mode 100644 bin/svgconvert.sh diff --git a/bin/generationlib b/bin/generationlib new file mode 100644 index 0000000..c94367c --- /dev/null +++ b/bin/generationlib @@ -0,0 +1,169 @@ +#!/bin/bash +NUM=0 +QUEUE="" +MAX_NPROC=2 # default +REPLACE_CMD=0 # no replacement by default + +USAGE="A simple wrapper for generating jpg pictures out of svg-files in parallel. +Usage: `basename $0` [-h] [-i] [-j nb_jobs] command arg_list + -h Shows this help + -c give the path to the cfg file + -i give the path of the svg files without the last / + -j Set number of simultanious jobs. Default is 2 + -o directory where the files should be moved to after creation. + Default ist PICCREATE_OUT + Examples: + `basename $0` -i file -c config + `basename $0` -j 3 -i file -c config" + +function queue { + QUEUE="$QUEUE $1" + NUM=$(($NUM+1)) +} +function regeneratequeue { + OLDREQUEUE=$QUEUE + QUEUE="" + NUM=0 + for PID in $OLDREQUEUE + do + if [ -d /proc/$PID ] ; then + QUEUE="$QUEUE $PID" + NUM=$(($NUM+1)) + fi + done +} +function checkqueue { + OLDCHQUEUE=$QUEUE + for PID in $OLDCHQUEUE + do + if [ ! -d /proc/$PID ] ; then + regeneratequeue # at least one PID has finished + echo $PID + break + fi + done +} +function cfg_parser { + INI_FILE=$1 + SECTION=$2 + echo $1 + echo $2 + eval `sed -n -e "/^\[$SECTION\]/,/^\s*\[/{/^[^;].*\=.*/p;}" < $INI_FILE`>> trr.txt + echo 'helololo' +} +function get_filepath { + # probiere alle möglichen Kombinationen wo das file stehen könnte je + # nachdem ob es mit oder ohne Endung, mit oder ohne den Komplettpfad + # gegeben wurde + FILENAME=$1 + ENDING=$2 + DEFAULT_DIR=$3 + if [ -s $FILENAME ] ; then + echo $FILENAME + elif [ -s $FILENAME.$ENDING ] ; then + echo "$FILENAME.$ENDING" + elif [ -s $DEFAULT_DIR/$FILENAME ] ; then + echo "$DEFAULT_DIR/$FILENAME" + elif [ -s $DEFAULT_DIR/$FILENAME.$ENDING ] ; then + echo "$DEFAULT_DIR/$FILENAME.$ENDING" + fi + return +} + +# Funktion die auf globalen Parametern beruht. +# überprüft ob alle notwendigen Angaben vorhanden sind +function check_params { + if [ -z "$INPUT_FILENAME" ]; then + echo "please give input file with -i." + exit 1 + fi + if [ -z "$CFG_FILENAME" ]; then + echo "please give config file with -c." + exit 1 + fi + + INPUT_FILE=`get_filepath $INPUT_FILENAME 'csv' $PICCREATE_WORK` + if [ ! -s $INPUT_FILE ] ; then + echo "-> input filename does not exist" + print "given was: -$INPUT_FILENAME-" + exit 1 + fi + CFG_PATH=`get_filepath $CFG_FILENAME 'cfg' $PICCREATE_CFG` + if [ ! -s $CFG_PATH ]; then + echo "-> config file with this name does not exist." + print "given was: -$CFG_FILENAME-" + exit 1 + fi + cfg_parser $CFG_PATH "$SECTION_NAME" + echo 'using width '$width + echo 'using height '$height + if [ -z "$width" ]; then + echo "please give width=xxx in section [$SECTION_NAME] in config $CFG_FILENAME." + exit 1 + fi + if [ -z "$height" ]; then + echo "please give height=xxx in section [$SECTION_NAME] in config $CFG_FILENAME." + exit 1 + fi +} + + +# parse command line +if [ $# -eq 0 ]; then # must be at least one arg + echo "$USAGE" >&2 + exit 1 +fi + +while getopts "j:i:o:hc:" OPT; do # "j:" waits for an argument "h" doesnt + case $OPT in + h) echo "$USAGE" + exit 0 ;; + j) MAX_NPROC=$OPTARG ;; + c) CFG_FILENAME=$OPTARG ;; + o) OUTDIR=$OPTARG ;; + i) INPUT_FILENAME=$OPTARG ;; + *) echo "$USAGE" >&2; exit 1; # Default. + esac +done + + +# Main program +echo Using $MAX_NPROC parallel threads +check_params + +# Starte die Generierung +echo $INPUT_FILE +for i in `ls $INPUT_FILE| grep .svg ` +do + echo $i + ID=`echo "$i" | sed -e 's/;.*//g'` + PIC_NAME=`echo "$i" | sed -e 's/.svg/.jpg/g'` + echo PID + RESIZE="${width}x${height}" + SWITCHES="-density $density $INPUT_FILE/$ID -resize $RESIZE $INPUT_FILE/$PIC_NAME" + CMDLINE="$CMD $SWITCHES" + echo $CMDLINE + (echo $ID; $CMDLINE) & + + PID=$! + queue $PID + + while [ $NUM -ge $MAX_NPROC ]; do + checkqueue + sleep 0.5 + done +done + +wait # wait for all processes to finish before exit + +if [ ! -z $OUTDIR ]; then + echo -n moving to $OUTDIR + if [ ! -d $OUTDIR ]; then + echo creating directory $OUTDIR + mkdir $OUTDIR + fi + mv $PICCREATE_OUT/*.jpg $OUTDIR + echo done +fi + +# vi:set wrapmargin=0 ai showmatch ts=4 syntax=sh: diff --git a/bin/svgconvert.sh b/bin/svgconvert.sh new file mode 100644 index 0000000..72ac8e7 --- /dev/null +++ b/bin/svgconvert.sh @@ -0,0 +1,8 @@ +#!/bin/bash +export PICCREATE_BIN=~/python/piccreate/bin +. $PICCREATE_BIN/setenv.sh + +CMD=convert +SECTION_NAME=jpgs + +. $PICCREATE_BIN/generationlib diff --git a/cfg/punkteraetsel.cfg b/cfg/punkteraetsel.cfg index 96f608a..20effef 100644 --- a/cfg/punkteraetsel.cfg +++ b/cfg/punkteraetsel.cfg @@ -39,7 +39,7 @@ counter_replace=True align=left [jpgs] -width=1200 -height=1200 +width=1900 +height=700 format=jpg - +density=300 From cae818ee8fcb62b647f47e5ea8e512934c328926 Mon Sep 17 00:00:00 2001 From: Kamil Bugajski Date: Thu, 16 Aug 2012 19:41:16 +0200 Subject: [PATCH 2/5] =?UTF-8?q?smart=5Funicode=20f=C3=BCr=20fonterkenung?= =?UTF-8?q?=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/piccreate.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/libs/piccreate.py b/libs/piccreate.py index bce9e06..95552c0 100644 --- a/libs/piccreate.py +++ b/libs/piccreate.py @@ -10,7 +10,7 @@ import optparse import os import ConfigParser from utils import CSVImport, CompleteEnding, get_translation -from django.utils.encoding import smart_str +from django.utils.encoding import smart_str, smart_unicode from string import Template class PicError(Exception): @@ -133,7 +133,6 @@ class Font: content = dict(fontconfig.items(letter)) # lade das svg template dazu ein glyph_path = os.path.join( font_path, smart_unicode(content['fontname'])) - print content['fontname'] f = open(glyph_path, 'r') svgstr = f.read() f.close() From 5a5c5f36ebd496149fe7919c1cf3a5cdeae57ba1 Mon Sep 17 00:00:00 2001 From: Kamil Bugajski Date: Thu, 16 Aug 2012 20:23:20 +0200 Subject: [PATCH 3/5] =?UTF-8?q?=C3=BCberpr=C3=BCfung=20ob=20file=20schon?= =?UTF-8?q?=20vorhanden=20eingef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/generationlib | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) mode change 100644 => 100755 bin/generationlib diff --git a/bin/generationlib b/bin/generationlib old mode 100644 new mode 100755 index c94367c..6f860b2 --- a/bin/generationlib +++ b/bin/generationlib @@ -114,11 +114,12 @@ if [ $# -eq 0 ]; then # must be at least one arg exit 1 fi -while getopts "j:i:o:hc:" OPT; do # "j:" waits for an argument "h" doesnt +while getopts "j:i:k:o:hc:" OPT; do # "j:" waits for an argument "h" doesnt case $OPT in h) echo "$USAGE" exit 0 ;; j) MAX_NPROC=$OPTARG ;; + k) CHECKFILE=$OPTARG ;; c) CFG_FILENAME=$OPTARG ;; o) OUTDIR=$OPTARG ;; i) INPUT_FILENAME=$OPTARG ;; @@ -138,20 +139,25 @@ do echo $i ID=`echo "$i" | sed -e 's/;.*//g'` PIC_NAME=`echo "$i" | sed -e 's/.svg/.jpg/g'` - echo PID - RESIZE="${width}x${height}" - SWITCHES="-density $density $INPUT_FILE/$ID -resize $RESIZE $INPUT_FILE/$PIC_NAME" - CMDLINE="$CMD $SWITCHES" - echo $CMDLINE - (echo $ID; $CMDLINE) & + FILE_PATH=$INPUT_FILE/$PIC_NAME + if [ -s $FILE_PATH ] + then + echo $PIC_NAME exists! + else + RESIZE="${width}x${height}" + SWITCHES="-density $density $INPUT_FILE/$ID -resize $RESIZE $INPUT_FILE/$PIC_NAME" + CMDLINE="$CMD $SWITCHES" + echo $CMDLINE + (echo $ID; $CMDLINE) & - PID=$! - queue $PID + PID=$! + queue $PID - while [ $NUM -ge $MAX_NPROC ]; do - checkqueue - sleep 0.5 - done + while [ $NUM -ge $MAX_NPROC ]; do + checkqueue + sleep 0.5 + done + fi done wait # wait for all processes to finish before exit From 32d0eea3ddaa7133b696308407b3dcf3170cd027 Mon Sep 17 00:00:00 2001 From: Kamil Bugajski Date: Thu, 16 Aug 2012 21:06:52 +0200 Subject: [PATCH 4/5] switch -k funktioniert --- bin/generationlib | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/bin/generationlib b/bin/generationlib index 6f860b2..7bf8468 100755 --- a/bin/generationlib +++ b/bin/generationlib @@ -9,6 +9,7 @@ Usage: `basename $0` [-h] [-i] [-j nb_jobs] command arg_list -h Shows this help -c give the path to the cfg file -i give the path of the svg files without the last / + -k chek if the jpg already exist -j Set number of simultanious jobs. Default is 2 -o directory where the files should be moved to after creation. Default ist PICCREATE_OUT @@ -114,12 +115,14 @@ if [ $# -eq 0 ]; then # must be at least one arg exit 1 fi -while getopts "j:i:k:o:hc:" OPT; do # "j:" waits for an argument "h" doesnt +CHECKFILE=0 + +while getopts "j:i:kc:o:hc:" OPT; do # "j:" waits for an argument "h" doesnt case $OPT in h) echo "$USAGE" exit 0 ;; j) MAX_NPROC=$OPTARG ;; - k) CHECKFILE=$OPTARG ;; + k) CHECKFILE=1 ;; c) CFG_FILENAME=$OPTARG ;; o) OUTDIR=$OPTARG ;; i) INPUT_FILENAME=$OPTARG ;; @@ -137,10 +140,11 @@ echo $INPUT_FILE for i in `ls $INPUT_FILE| grep .svg ` do echo $i + echo $CHECKFILE ID=`echo "$i" | sed -e 's/;.*//g'` PIC_NAME=`echo "$i" | sed -e 's/.svg/.jpg/g'` FILE_PATH=$INPUT_FILE/$PIC_NAME - if [ -s $FILE_PATH ] + if [ -s $FILE_PATH ] && [ $CHECKFILE -eq 1 ] then echo $PIC_NAME exists! else From 990de3443e9dad9e9f3fc06a0526865341156079 Mon Sep 17 00:00:00 2001 From: Michael Stangl Date: Thu, 16 Aug 2012 21:08:14 +0200 Subject: [PATCH 5/5] =?UTF-8?q?-=20x=20bit=20gesetzt=20und=20Umgebung=20pr?= =?UTF-8?q?=C3=BCfen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/svgconvert.sh | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) mode change 100644 => 100755 bin/svgconvert.sh diff --git a/bin/svgconvert.sh b/bin/svgconvert.sh old mode 100644 new mode 100755 index 72ac8e7..95e09ed --- a/bin/svgconvert.sh +++ b/bin/svgconvert.sh @@ -1,7 +1,10 @@ #!/bin/bash -export PICCREATE_BIN=~/python/piccreate/bin -. $PICCREATE_BIN/setenv.sh - +. setenv.sh +# Wo ist das Programm hinkopiert +if [ -z $PICCREATE ];then + echo please check environment + exit 0 +fi CMD=convert SECTION_NAME=jpgs