Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Can someone make a simple zenity script for zsync which just takes the location of the file to be synced and the zsync file and show progress ?

share|improve this question

1 Answer 1

up vote 5 down vote accepted
+50

zsync has terrible output for parsing. You'd be crazy to attempt that.

That said, I'm apparently crazy.

#!/bin/bash

url=$1
seedfile=$2

tempdir=$(mktemp -d)
msgfile=$tempdir/message
trap 'rm -rf "$tempdir"' EXIT

down_filter() {
    local message line percent speed
    echo "Downloading zsync file" > "$msgfile"
    read -r -n1 _
    while read -r line; do
        if [[ $line = [-#]* ]]; then
            read -r _ percent speed <<< "$line"
            echo "#$(<"$msgfile")\n$speed"
            if (( ${percent%%.*} < 100 )); then
                echo "$percent"
            else
                echo "99.9%"
            fi
        fi
    done < <(awk 'BEGIN{RS="[\r\n]"} {print;fflush()}')
}

seed_filter() {
    local message file count size point
    while read -r -d '*' line; do
        file=${line%:*}
        message+="\n$file"
        echo "$message" > "$msgfile"
        file=${file#reading seed file }
        count=1
        size=$(( $(wc -c < "$file") / 1000000 + 1 ))
        echo "#$message"
        while read -r -n1 point; do
            [[ $point = '*' ]] || break
            ((count++))
            echo "$(( 100 * count / size ))%"
        done
        read -r message;
        echo "$message" > "$msgfile"
    done
}

if [[ -z $url ]]; then
    url=$(zenity --entry \
                 --title=zsync \
                 --text="Enter URL to zsync file" \
                 --width=500 \
                 --height=100 \
    ) || exit
fi

if [[ -z $seedfile || ! -e $seedfile ]]; then
    seedfile=$(zenity --file-selection \
                      --title="zsync $url" \
                      --text="Choose a seed-file" \
    )
fi

{ 
    zsync ${seedfile:+-i "$seedfile"} "$url" \
          > >(down_filter >&3) 2> >(seed_filter >&3)
} 3> >(zenity --progress \
              --title="zsync $url" \
              --width=500 \
              --height=100 \
)

The script will have a lot of weaknesses. Any slight change in the output of zsync in a newer version would likely break it. (Only tested against Ubuntu 10.04's zsync v0.6.1). For some odd reason it outputs some of the progress to stdout, and some to stderr, which makes it extra hard to parse.

Also, it won't handle certain odd characters in filenames, like \, * or newlines.

Expect bugs; I haven't really tested it that much.

Anyway, I had never heard of zsync before. It's a neat tool, so thanks for letting me know about it at least. :)

Screenshots : enter image description here enter image description here

share|improve this answer
    
It worked in 10.04 with 0.6.1-1ubuntu1 version of zsync. Thanks –  Alaukik Feb 3 '11 at 9:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.