You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
14 KiB

<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<title>YBlog - custom website synchronisation with mobileme (2)</title>
<meta name="keywords" content="Apple, mobileme, WebDav, synchronisation, zsh, script" />
<link rel="shortcut icon" type="image/x-icon" href="../../../../Scratch/img/favicon.ico" />
<link rel="stylesheet" type="text/css" href="/css/y.css" />
<link rel="stylesheet" type="text/css" href="/css/legacy.css" />
<link rel="alternate" type="application/rss+xml" title="RSS" href="/rss.xml" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="apple-touch-icon" href="../../../../Scratch/img/about/FlatAvatar@2x.png" />
<!--[if lt IE 9]>
<script src=""></script>
<!-- IndieAuth -->
<link href="" rel="me">
<link href="" rel="me">
<link href="" rel="me">
<link rel="pgpkey" href="../../../../pubkey.txt">
<body lang="en" class="article">
<div id="content">
<div id="header">
<div id="choix">
<span id="choixlang">
<a href="../../../../Scratch/fr/blog/2009-10-28-custom-website-synchronisation-with-mobileme--2-/">French</a>
<span class="tomenu"><a href="#navigation">↓ Menu ↓</a></span>
<span class="flush"></span>
<div id="titre">
<h1>custom website synchronisation with mobileme (2)</h1>
<div class="flush"></div>
<div id="afterheader" class="article">
<div class="corps">
<p>I already talked about how <a href="../../../../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>
<code class="zsh"> mv folder folder2 </code>
<p>It returns OK and I’ve got:</p>
<code class="zsh"> $ ls folder folder2 </code>
<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>
<p><code class="zsh" file="webdav-framework"> #!/usr/bin/env zsh</p>
<p>function samelineprint { print -n -P – "\r$*" }</p>
<h1 id="avec-1-essai-par-seconde-300-5-minutes">avec 1 essai par seconde: 300 = 5 minutes</h1>
<h1 id="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 <span class="math inline">$target; do samelineprint "Echec: essai n°$</span>essai" ((essai++)) ((essai&gt;maxessais)) &amp;&amp; exit 5 done print }</p>
<h1 id="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 <span class="math inline">$target; do samelineprint "Echec: essai n°$</span>essai" ((essai++)) ((essai&gt;maxessais)) &amp;&amp; exit 5 done print }</p>
<h1 id="try-to-remove-until-success">try to remove until success</h1>
<p>function tryrm { target=“$1” local essai=1 local options=’’ [[ -d $target ]] &amp;&amp; options=‘-rf’ print – rm $options $target while ! rm $options <span class="math inline">$target; do samelineprint "Echec: essai n°$</span>essai" ((essai++)) ((essai&gt;maxessais)) &amp;&amp; exit 5 done essai=1 while [[ -e $element ]]; do samelineprint “rm reussi mais fichier source non disparu n°$essai” sleep 1 ((essai++)) ((essai&gt;maxessais)) &amp;&amp; exit 5 done print }</p>
<h1 id="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&gt;maxessais)) &amp;&amp; exit 5 sleep 1 done print – mv $element $target while ! mv $element <span class="math inline">$target; do samelineprint "Echec: essai n°$</span>essai" ((essai++)) ((essai&gt;maxessais)) &amp;&amp; exit 4 done essai=1 while [[ -e $element ]]; do samelineprint “mv reussi mais fichier source non disparu n°$essai” sleep 1 ((essai++)) ((essai&gt;maxessais)) &amp;&amp; exit 5 done print }</p>
<h1 id="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 <span class="math inline">$target; do samelineprint "Echec: essai n°$</span>essai" ((essai++)) ((essai&gt;maxessais)) &amp;&amp; exit 5 done essai=1 while [[ -e $element ]]; do samelineprint “mv reussi mais fichier source non disparu n°$essai” sleep 1 ((essai++)) ((essai&gt;maxessais)) &amp;&amp; exit 5 done print } </code>
<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><code class="zsh" file="publish"> #!/usr/bin/env zsh</p>
<h1 id="">Script synchronisant le site sur</h1>
<h1 id="normalement-le-site-est-indisponible-le-moins-de-temps-possible">normalement, le site est indisponible le moins de temps possible</h1>
<h1 id="le-temps-de-deux-renommages-de-répertoire">le temps de deux renommages de répertoire</h1>
<h1 id="get-configuration">get configuration</h1>
<h1 id="mostly-directories">mostly directories</h1>
<p>source $0:h/config</p>
<h1 id="get-trycp-function-copy-until-success">get trycp function (copy until success)</h1>
<p>source $0:h/webdav-framework</p>
<p>if [[ $1 == ‘-h’ ]]; then print – “usage : $0:h [-h|-s|-d]” print – " -a sychronise aussi l’index" print – " -h affiche l’aide" print – " -d modification directe (pas de swap)" print – " -s swappe simplement les répertoires" fi</p>
<h1 id="publication-incrementale">publication incrementale</h1>
<p>function incrementalPublish { local ydestRep=<span class="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” &gt; <span class="math inline">$localRef remoteRef="/tmp/remoteSiteMapRef.$</span><span class="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 “<span class="math inline">$ydestRep/map.yrf" "$</span>remoteRef” typeset -U filesToUpdate filesToUpdate=( $(diff $localRef $remoteRef | awk ’/<a href="#fn1" class="footnote-ref" id="fnref1"><sup>1</sup></a>/ {print <span class="math inline">$2}' ) ) if (($</span>{#filesToUpdate} == 1)); then print – “Seul le fichier <span class="math inline">${filesToUpdate} sera téléversé" elif (($</span>{#filesToUpdate}&lt;10)); then print –”<span class="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 <span class="math inline"><em>s</em><em>r</em><em>c</em><em>R</em><em>e</em><em>p</em></span>element ]]; then trycp <span class="math inline"><em>s</em><em>r</em><em>c</em><em>R</em><em>e</em><em>p</em></span>element <span class="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 <span class="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>
<h1 id="publication-via-rsync">publication via rsync</h1>
<p>function rsyncPublish { result=1 essai=1 while (( $result &gt; 0 )); do print – rsync -arv $srcRep/ $destRep.tmp if ((!testmode)); then rsync -arv $srcRep/ <span class="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 (( <span class="math inline">$result &gt; 0 )); then print -P -- "%BEchec du rsync%b (essai n°$</span>essai)" &gt;&amp;2 fi ((essai++)) done }</p>
<h1 id="swap">swap</h1>
<p>function swap { print -P – “%B[Directory Swap (tmp &lt;=&gt; target)]%b” [[ -e $destRep.old ]] &amp;&amp; tryrm $destRep.old</p>
<pre><code>print -- &quot; renommage du repertoire sandard vers le .old&quot;
tryrename $destRep $destRep.old
print -- &quot; renommage du repertoire tmp (nouveau) vers le standard&quot;
print -P -- &quot;%B[Site Indisponible]%b $(date)&quot;
tryrename $destRep.tmp $destRep
print -P -- &quot;%B[Site Disponible]%b $(date)&quot;
print -- &quot; renommage du repertoire old vers le tmp&quot;
tryrename $destRep.old $destRep.tmp
print -P -- &quot; publication terminée&quot;</code></pre>
<p>print – “Root = $webroot” print – “Dest = $destRep”</p>
if [[ “$1” = “-s” ]]; then swap else if [[ “$1” = “-d” ]]; then suffix="" else suffix=“.tmp” fi print -P – “%BSync%b[${Root:t} =&gt; <span class="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>
<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>
<section class="footnotes">
<hr />
<li id="fn1"><p>&lt;&gt;<a href="#fnref1" class="footnote-back"></a></p></li>
<div id="afterarticle">
<div id="social">
<a href="/rss.xml" target="_blank" rel="noopener noreferrer nofollow" class="social">RSS</a>
<a href="" target="_blank" rel="noopener noreferrer nofollow" class="social">Tweet</a>
<a href="" target="_blank" rel="noopener noreferrer nofollow" class="social">FB</a>
<br />
<a class="message" href="../../../../Scratch/en/blog/Social-link-the-right-way/">These social sharing links preserve your privacy</a>
<div id="navigation">
<a href="../../../../">Home</a>
<span class="sep">¦</span>
<a href="../../../../Scratch/en/blog">Blog</a>
<span class="sep">¦</span>
<a href="../../../../Scratch/en/softwares">Softwares</a>
<span class="sep">¦</span>
<a href="../../../../Scratch/en/about">About</a>
<div id="totop"><a href="#header">↑ Top ↑</a></div>
<div id="bottom">
Published on 2009-10-28
<a href="">Follow @yogsototh</a>
<a rel="license" href="">Yann Esposito©</a>
Done with
<a href="" target="_blank" rel="noopener noreferrer nofollow"><strike>Vim</strike></a>
<a href="" target="_blank" rel="noopener noreferrer nofollow">spacemacs</a>
<span class="pala">&amp;</span>
<a href="" target="_blank" rel="noopener noreferrer nofollow"><strike>nanoc</strike></a>
<a href="" target="_blank" rel="noopener noreferrer nofollow">Hakyll</a>
<hr />
<div style="max-width: 100%">
<a href="">
<img src="../../../../Scratch/img/ada-logo.png" class="simple" style="height: 16px;
border-radius: 50%;
display:inline-block;" />
<code style="display:inline-block;
text-align: left;
vertical-align: top;
max-width: 85%;">