From 24af0e5b403ccf23dbeff790a21946758a83ec77 Mon Sep 17 00:00:00 2001 From: Kévin Raymond Date: Fri, 16 Dec 2011 22:51:56 +0000 Subject: jparser going to use getops --- diff --git a/jparser b/jparser index 39e02c7..69f12e3 100755 --- a/jparser +++ b/jparser @@ -10,30 +10,54 @@ # This script is under the GPLv2 license. If you get this file without the license, please read it there: # http://www.gnu.org/licenses/gpl-2.0.html +# +# The journal parsed is composed of a store folder with couples of file, the data an its metadata component. +# 8f908c24-eaa6-42bf-88fe-5c08f0b95bcb # Could be a tarball or a file (video, sound, pic…) +# 8f908c24-eaa6-42bf-88fe-5c08f0b95bcb.metadata +# metadata=.metadata -path=$PWD +SOURCE=$PWD callme=child separator=: template=#activity${separator}timestamp${separator}uid${separator}title${separator}title_set_by_user${separator}buddies template_simple=#date${separator}uid${separator}title${separator}buddies simple_suffix=_simple.txt user=$USER -format=-c +#format=-c dest=./ -helpme() +usage() { - echo "Usage: $0 [output_format] [user_name] [path to $metadata files] [destination path]" - echo " add -h as first arg to print this help" - echo " - file(s) output format could be -s to create a simplified output" - echo " -c to create the complet output (Default)" - echo " -f to create both files" - echo " - if no path specified, the current dir would be used" - echo ' - if no user name is specified, the current $USER would be used' #should check an XO id - echo ' - if no detination path is specified, $PWD is used' - exit 0 +cat << EOF +usage: $0 [OPTION] [FORMAT] [-u USERNAME] [-s SOURCE] [-d DEST] + +This script parse the journal metadata and create a CSV file like, +which would then be used for statistics purpose. + +OPTION: + -h Show this message + +FORMAT: + Could be one of the following flag: + -b Create the basic output file + -c Create the complet output file + -f Create both files as output + +USERNAME: + If no user name is specified, the current $USER would be used + +SOURCE: + If no path specified, the current dir would be used (not recursive) + +DEST: + If no detination path is specified, $PWD is used + +EXAMPLE: + $ $0 -c -u XO -s ~/.sugar/default/datastore/store/ + +EOF } # @@ -52,6 +76,15 @@ get_sed() # parse filename output name output_format # +par() +{ + + echo $* + echo "WHOLE=$WHOLE=BASIC=$BASIC=X=$CHILD=SOURCE=$SOURCE=DEST=$DEST" + exit 0 + +} + parse() { tmp=$(sed -e 's/": ["]/:/g;s/["], "/#/g;s/^{"//g;s/: ""}//g;s/": /:/g;s/, "/#/g;s/"}//g' $1) @@ -71,23 +104,12 @@ parse() esac done - case $3 in - -s) #simplified output - line_so=true - ;; - -f) # both output - line_so=true - line=true - ;; - *) # only complet output - line=true - ;; - esac - - if [[ -n "$line_so" ]]; then + if [ -z $BASIC ] + then echo `date -d @${timestamp} +%d/%m/%Y`${separator}${uid}${separator}${title}${separator}${buddies} >> ${2}${simple_suffix} fi - if [[ -n "$line" ]]; then + if [ -z $WHOLE ] + then echo ${activity}${separator}${timestamp}${separator}${uid}${separator}${title}${separator}${title_set}${separator}${buddies} >> $2 fi @@ -98,43 +120,63 @@ parse() # Start argument processing # -if [[ "$#" -gt 5 ]]; then - helpme -fi - -if [[ -n "$1" ]]; then - if [[ "$1" = "-h" ]]; then - helpme - elif [[ "$1" = "$callme" ]]; then # Am I a child process used for parsing? - if [ ! "$#" = 4 ]; then - echo "Error there should have been 4 args: callme path filename format" - echo " But you give me: $*" +while getopts ":hbcfu:s:d:x:" OPTION +do + case $OPTION in + h) + usage exit 1 - else parse $2 $3 $4 - fi - elif [[ "$1" = "-s" || "$1" = "-f" || "$1" = "-c" ]]; then # Format output provided as arg? - format=$1 - if [[ -n "$2" ]]; then # User provided as arg? - user=$2 - if [[ -n "$3" ]]; then # Path provided as arg? - path=$3 - if [ ! -d "$path" ]; then - echo "Error, path providen is incorrect (check permissions)" - exit 1 - fi - if [[ -n "$4" ]]; then - dest=${4}/ - if [ ! -d "$dest" ]; then - mkdir $dest - fi - fi + ;; + b) + BASIC="-b" + ;; + c) + WHOLE="-f" + ;; + f) + WHOLE="-f" + BASIC="-b" + ;; + u) + USERNAME=$OPTARG + ;; + s) + if [ ! -d "$OPTARG" ] + then + echo "Error, path providen incorrect (check permissions)" + exit 1 fi - elif [ -e /ofw/serial-number ]; then # if we are from an XO, grab its serial - user=`cat /ofw/serial-number` - fi # default value is $USER - else - echo "Error, wrong output specified" - helpme + SOURCE="$OPTARG" + ;; + d) + DEST=$OPTARG + ;; + x) # We are a child… + if [ "$OPTARG" = "$callme" ] + then + CHILD=1 + fi + ;; + ?) + echo " You give me: $*\n" #TODO remove + usage + exit + ;; + esac +done + +if [ ! -z $CHILD ] # Am I a child process used for parsing? +then + par $4 $DEST $WHOLE $BASIC +else + if [ -z $USERNAME ] + then + if [ -e /ofw/serial-number ] # if we are from an XO, grab its serial + then + USERNAME=`cat /ofw/serial-number` + else + USERNAME=$USER # default value is $USER + fi fi fi @@ -142,17 +184,17 @@ fi # Let's start parsing # -out=${dest}${user}_`date +%F`.txt +out=${DEST}${USERNAME}_`date +%F`.txt out_simple=${out}${simple_suffix} # create template files -if [[ "$1" = "-s" || "$1" = "-f" ]]; then +if [ -z $BASIC ]; then if [ ! -f $out_simple ]; then echo $template_simple > $out_simple else echo >> $out_simple fi -elif [[ "$1" = "-c" || "$1" = "-f" ]]; then +elif [ -z $WHOLE ]; then if [ ! -f $out ]; then echo $template > $out else @@ -160,19 +202,19 @@ elif [[ "$1" = "-c" || "$1" = "-f" ]]; then fi fi -# Now we call ourself to be able to inter in the function. Also used to run in concurrency -find $path -name "*$metadata" -exec $0 $callme '{}' $out $format \;& +# Now we call ourself to be able to inter in the function. Also used to run in concurrency (remove the & to debug…) +#find $SOURCE -name "*$metadata" -exec $0 -x $callme -s '{}' -d $out $WHOLE $BASIC \;& +find $SOURCE -name "*$metadata" -exec $0 -x $callme -s '{}' -d $out $WHOLE $BASIC \; wait # wait the end of parsing -case $format in - -s) #simplified output - echo "Simple output file could be find in $out$simple_suffix" - ;; - -f) # both output - echo "Metadata could be find in $out" - echo "Simple output file could be find in $out$simple_suffix" - ;; - *) # only complet output - echo "Metadata could be find in $out" - ;; -esac + +if [ ! -z $BASIC ] +then + echo "Simple output file could be find in $out$simple_suffix" +fi + +if [ ! -z $WHOLE ] +then + echo "Metadata could be find in $out" +fi + exit 0 -- cgit v0.9.1