<spanclass="tomenu"><ahref="#navigation">↓ Menu ↓</a></span>
<spanclass="flush"></span>
</div>
</div>
<divid="titre">
<h1>custom website synchronisation with mobileme (2)</h1>
</div>
<divclass="flush"></div>
<divid="afterheader"class="article">
<divclass="corps">
<p>I already talked about how <ahref="../../../../Scratch/en/blog/10_Synchronize_Custom_WebSite_with_mobileMe">I synchronized my website with mobileme</a>. I ameliorated this script in order to make it incremental.</p>
<p>Here is my new script, it first create a map which associate to each file its hash. After that it compare this file to the remote one. Then for each different file, update the content.</p>
<p>Even with this script I also have some problem. Mostly due to ‘webdav’ issues. For example, renaming a folder work really badly (on Linux at least). I use webdavfs. For example:</p>
<div>
<codeclass="zsh"> mv folder folder2 </code>
</div>
<p>It returns OK and I’ve got:</p>
<div>
<codeclass="zsh"> $ ls folder folder2 </code>
</div>
<p>Booh….</p>
<p>In order to handle most webdav issues I use a <em>framework</em> in zsh. It handle almost all except the correct renaming of folder. Working on it… Anyway here is the code I use.</p>
<h1id="avec-1-essai-par-seconde-300-5-minutes">avec 1 essai par seconde: 300 = 5 minutes</h1>
<p>maxessais=300</p>
<h1id="try-to-create-a-directory-until-success">try to create a directory until success</h1>
<p>function trymkdir { target=“$1” print – mkdir -p $target local essai=1 while ! mkdir -p <spanclass="math inline">$target; do samelineprint "Echec: essai n°$</span>essai" ((essai++)) ((essai>maxessais)) && exit 5 done print }</p>
<h1id="try-to-copy-until-success">try to copy until success</h1>
<p>function trycp { element=“$1” target=“$2” if [[ ! -d ${target:h} ]]; then trymkdir ${target:h} fi local essai=1 print – cp $element $target while ! $element <spanclass="math inline">$target; do samelineprint "Echec: essai n°$</span>essai" ((essai++)) ((essai>maxessais)) && exit 5 done print }</p>
<h1id="try-to-remove-until-success">try to remove until success</h1>
<p>function tryrm { target=“$1” local essai=1 local options=’’ [[ -d $target ]] && options=‘-rf’ print – rm $options $target while ! rm $options <spanclass="math inline">$target; do samelineprint "Echec: essai n°$</span>essai" ((essai++)) ((essai>maxessais)) && exit 5 done essai=1 while [[ -e $element ]]; do samelineprint “rm reussi mais fichier source non disparu n°$essai” sleep 1 ((essai++)) ((essai>maxessais)) && exit 5 done print }</p>
<h1id="try-to-rename-until-success">try to rename until success</h1>
<p>function tryrename { element=“$1” target=“$2” local essai=1 while [[ -e $target ]]; do samelineprint “Echec n°$essai le fichier $target existe déjà” ((essai++)) ((essai>maxessais)) && exit 5 sleep 1 done print – mv $element $target while ! mv $element <spanclass="math inline">$target; do samelineprint "Echec: essai n°$</span>essai" ((essai++)) ((essai>maxessais)) && exit 4 done essai=1 while [[ -e $element ]]; do samelineprint “mv reussi mais fichier source non disparu n°$essai” sleep 1 ((essai++)) ((essai>maxessais)) && exit 5 done print }</p>
<h1id="try-to-move-until-success">try to move until success</h1>
function trymv { element=“$1” target=“$2” local essai=1 print – mv $element $target while ! mv $element <spanclass="math inline">$target; do samelineprint "Echec: essai n°$</span>essai" ((essai++)) ((essai>maxessais)) && exit 5 done essai=1 while [[ -e $element ]]; do samelineprint “mv reussi mais fichier source non disparu n°$essai” sleep 1 ((essai++)) ((essai>maxessais)) && exit 5 done print } </code>
</div>
<p>And here is the code on how I synchronize my website. There is a little cryptic code. It correspond a problem caused by the bluecloth filter which is a markdown program made in ruby. Each time my email is written it is transformed differently. This is why I remove this part from the content of each html file. Without it, all my files containing email are different at each regeneration of my website.</p>
<p>function incrementalPublish { local ydestRep=<spanclass="math inline"><em>d</em><em>e</em><em>s</em><em>t</em><em>R</em><em>e</em><em>p</em></span>suffix localRef=“$srcRep/map.yrf” print – “Creation du fichier de references” create-reference-file.sh ><spanclass="math inline">$localRef remoteRef="/tmp/remoteSiteMapRef.$</span><spanclass="math inline">$.yrf" if [[ ! -e "$</span>ydestRep/map.yrf" ]]; then # pas de fichier de reference sur la cible print – “pas de fichier de reference sur la cible, passage en mode rsync” rsyncPublish swap else trycp “<spanclass="math inline">$ydestRep/map.yrf" "$</span>remoteRef” typeset -U filesToUpdate filesToUpdate=( $(diff $localRef $remoteRef | awk ’/<ahref="#fn1"class="footnote-ref"id="fnref1"><sup>1</sup></a>/ {print <spanclass="math inline">$2}' ) ) if (($</span>{#filesToUpdate} == 1)); then print – “Seul le fichier <spanclass="math inline">${filesToUpdate} sera téléversé" elif (($</span>{#filesToUpdate}<10)); then print –”<spanclass="math inline">${#filesToUpdate} fichiers seront téléversés :" print -- "$</span>{filesToUpdate}" else print – “${#filesToUpdate} fichiers seront téléversés” fi # copy all file with some differences # except the map in case of error for element in $filesToUpdate; do if [[ $element == “/map.yrf” ]]; then continue fi if [[ -e <spanclass="math inline"><em>s</em><em>r</em><em>c</em><em>R</em><em>e</em><em>p</em></span>element ]]; then trycp <spanclass="math inline"><em>s</em><em>r</em><em>c</em><em>R</em><em>e</em><em>p</em></span>element <spanclass="math inline"><em>y</em><em>d</em><em>e</em><em>s</em><em>t</em><em>R</em><em>e</em><em>p</em></span>element else tryrm <spanclass="math inline"><em>y</em><em>d</em><em>e</em><em>s</em><em>t</em><em>R</em><em>e</em><em>p</em></span>element fi done # if all went fine, copy the map file trycp $srcRep/map.yrf $ydestRep/map.yrf # remove the temporary file }</p>
<h1id="publication-via-rsync">publication via rsync</h1>
<p>function rsyncPublish { result=1 essai=1 while (( $result > 0 )); do print – rsync -arv $srcRep/ $destRep.tmp if ((!testmode)); then rsync -arv $srcRep/ <spanclass="math inline"><em>d</em><em>e</em><em>s</em><em>t</em><em>R</em><em>e</em><em>p</em>.<em>t</em><em>m</em><em>p</em><em>f</em><em>i</em><em>r</em><em>e</em><em>s</em><em>u</em><em>l</em><em>t</em>=</span>? if (( <spanclass="math inline">$result > 0 )); then print -P -- "%BEchec du rsync%b (essai n°$</span>essai)" >&2 fi ((essai++)) done }</p>
if [[ “$1” = “-s” ]]; then swap else if [[ “$1” = “-d” ]]; then suffix="" else suffix=“.tmp” fi print -P – “%BSync%b[${Root:t} =><spanclass="math inline"><em>d</em><em>e</em><em>s</em><em>t</em><em>R</em><em>e</em><em>p</em>:<em>t</em></span>suffix]” incrementalPublish fi </code>
</div>
<p>This is my way to replace <code>rsync</code> with filesystem not handling it. Hope it is usefull. I’ll be happy to hear a way to handle the webdav rename folder problem. This is really annoying.</p>