summaryrefslogtreecommitdiff
path: root/sandbox/infrastructure/docutils-update
blob: b2aa96386e67b18228873ca96722a2f452427d7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#! /bin/bash
# $Id$
#
# This script is installed as a cron job to automatically update the
# Docutils web site whenever the SVN files change.  Any .html document
# with a corresponding .txt file is regenerated whenever the .txt
# changes.
#
# Options:
#   -f    Do not give feedback.
#   -t    Run the script in trace mode ("set -o xtrace").
#   -u    Regenerate .html unconditionally.
#   -v    Run verbosely.
#
# Prerequisites:
#
# - Checked out trunk at $snapshotdir.
# - Checked out main tree at $lib.

# exit on error
set -e

# make all newly created files group writeable
umask 002

basedir=/home/groups/docutils/htdocs
project=docutils
# $auxdir is non-public.
auxdir=$basedir/aux
# $htdocsdest is the destination for htdocs and will be moved to
# another server later; so we keep it non-public (under $auxdir).
htdocsdest=$auxdir/htdocs
# Where to create the snapshots (non-public).
snapshotdir=$auxdir/snapshots
# Where to publish the snapshots (public).
snapshotdest=/home/groups/ftp/pub/docutils
bindir=$auxdir/bin
htdocs_patchfile=$auxdir/htdocs.patch
htdocs_tarball=$auxdir/htdocs.tar
htdocs_new_tarball=$auxdir/htdocs.new.tar
# htdocs directory on SF.net
remotehtdocs=/home/groups/d/do/docutils/htdocs
pylib=$auxdir/lib/python
lib=$pylib/$project
# Lock directory.
lockdir=$auxdir/lock
# SSH stuff.
sshdir=$auxdir/.ssh
sshhost=docutilsupdate@shell.sourceforge.net
sshcommand="ssh -i $lockdir/id_dsa
            -o UserKnownHostsFile=$sshdir/known_hosts $sshhost"
# Project base URL (for sitemap) without trailing slash.
baseurl="http://docutils.sourceforge.net"

export PYTHONPATH=$pylib:$lib:$lib/extras
export PATH=$lib/tools:$PATH

trace=0
unconditional=0
verbose=0
feedback=1

while getopts ftuv opt
do
    case $opt in
        f)  feedback=;;
        t)  trace=1;;
        u)  unconditional=1;;
        v)  verbose=1;;
        \?) exit 2;;
    esac
done
shift `expr $OPTIND - 1`

test $feedback && echo 'Starting docutils-update run...' || true

if [ $trace -eq 1 -o $verbose -eq 1 ] ; then
    set -o xtrace
fi

# Acquire lock.
if ! mkdir $lockdir; then
    echo
    echo Could not create lock directory at
    echo $lockdir
    echo
    echo Please ensure no other user is running this script
    echo and delete the directory.
    exit 1
fi
# Always clean up on exit.
trap "rm -rf $lockdir; trap - 0; exit 1" 0 1 2 3 15
# Make sure the lock directory is deletable (i.e. rwx) by other group
# members (in case this script crashes after copying files into the
# directory) and un-readable by world (because we'll be storing the
# key in it).
chmod 0770 $lockdir


# update library area
cd $lib
svn up --quiet

# -------------------- Snapshots: --------------------

# gather the materials
cd $snapshotdir
svn -q revert $project/$project/__init__.py
haschanges="`svn up | grep -v '^At revision '; true`"

# update __version_details__ string
version_details="snapshot `date --utc --iso`, r`svn info | grep ^Revision: | sed 's/^Revision: //'`"
(echo ",s/^__version_details__ = .*\$/__version_details__ = '$version_details'/";
    echo wq) | ed $project/$project/__init__.py 2> /dev/null

# Ensure proper directory permissions are set so that the files can be
# modified by several users.  Changing permissions of files is
# probably not necessary because files can be deleted and re-created.
# Do not change permissions of aux directory to keep it non-public
# (but change permissions for all subdirectories).
#find $basedir -type f -print0 | xargs -0 chmod ug+rw 2> /dev/null || true
find $basedir -name aux -o -type d -print0 | xargs -0 chmod ug+rwxs 2> /dev/null || true

# create the snapshots
exclude='--exclude=.svn'
tar -cz $exclude -f $project-snapshot.tgz $project
tar -cz $exclude -f $project-sandbox-snapshot.tgz sandbox
tar -cz $exclude -f $project-web-snapshot.tgz web
( cd sandbox/gschwant ;
  tar -cz $exclude -f ../../docfactory-snapshot.tgz docfactory )

# plant the snapshots
mv -f *snapshot.tgz $snapshotdest

# revert and touch (to avoid updating the web site only because of the
# changed timestamp)
svn -q revert $project/$project/__init__.py
touch $project/$project/__init__.py --date \
    "`svn info $project/$project/__init__.py | \
    grep 'Last Changed Date:' | sed 's/[^:]*: //'`"

# -------------------- htdocs: --------------------

cd $snapshotdir

function copy_to_htdocsdest() {
    find "$@" -type d -name .svn -prune -o -type f -print0 | \
        xargs -0 cp --update --parents --target-directory=$htdocsdest
}

# update htdocs
copy_to_htdocsdest sandbox
(cd $project; copy_to_htdocsdest *)
(cd web; copy_to_htdocsdest * .[^.]*)

# update HTML docs
cd $htdocsdest/tools

if [ $trace -eq 0 ] ; then
    set +o xtrace
fi

for makefile in `find .. -name Makefile.docutils-update` ; do
    dir=`dirname $makefile`
    ( cd $dir ; make -f Makefile.docutils-update -s )
done

for htmlfile in `find .. -name '*.html'` ; do
    dir=`dirname $htmlfile`
    base=`basename $htmlfile .html`
    txtfile=$dir/$base.txt
    if [ -e $txtfile ] ; then
        if [ $unconditional -eq 1 -o $txtfile -nt $htmlfile ] ; then
            if [ "${base:0:4}" == "pep-" ] ; then
                test $feedback && echo "$txtfile (PEP)" || true
                python $lib/tools/rstpep2html.py --config=$dir/docutils.conf $txtfile $htmlfile
                haschanges=1
            else
                test $feedback && echo $txtfile || true
                python $lib/tools/rst2html.py --config=$dir/docutils.conf $txtfile $htmlfile
                haschanges=1
            fi
        fi
    fi
done

if [ $trace -eq 1 -o $verbose -eq 1 ] ; then
    set -o xtrace
fi

# -------------------- XML sitemap for search engines: --------------------

cd $htdocsdest

# Update the sitemap only if something has changed because it takes
# very much CPU time.
if test -n "$haschanges"; then
    (
        echo '<?xml version="1.0" encoding="UTF-8"?>'
        echo '<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">'
        if [ $trace -eq 0 ] ; then
            set +o xtrace
        fi
        find . -name '.[^.]*' -prune -o -type d -printf '%p/\n' -o -type f -print | \
            while read i; do
                # i is the file name.
                if test "$i" == ./; then
                    # Homepage.
                    i=index.html
                    url="$baseurl/"
                elif test "$i" == ./sitemap -o "${i: -1}" == / -a -f "${i}index.html"; then
                    # This is a directory and it has an index.html, so we
                    # don't need to include it.
                    continue
                else
                    url="$baseurl${i:1}"
                    url="${url// /%20}"
                fi
                lastmod="`date --iso-8601=seconds -u -r "$i"`"
                # Google wants a colon in front of the last two digits.
                lastmod="${lastmod::22}:00"
                if test "${i: -5}" == .html; then
                    # HTML files (including the home page) have highest priority.
                    priority=1.0
                elif test "${i: -4}" == .txt; then
                    # Text files have medium priority.
                    priority=0.5
                else
                    # Everything else (source files etc.) has low priority.
                    priority=0.2
                fi
                echo "<url><loc>$url</loc><lastmod>$lastmod</lastmod><priority>$priority</priority></url>"
            done
        if [ $trace -eq 1 -o $verbose -eq 1 ] ; then
            set -o xtrace
        fi
        echo '</urlset>'
    ) > sitemap
    # sitemap is compressed on the remote site for smaller patch sizes.
fi

# -------------------- Push changes to remote server. --------------------

# SSH doesn't want to read id_dsa files which don't have 0600
# permissions.  This is getting into our way here, but we work around
# this by copying id_dsa to $lockdir/id_dsa and setting the
# permissions of the resulting id_dsa file to 0600.

# Copy the key.
cp $sshdir/id_dsa $lockdir/id_dsa
# SSH wants this.
chmod 0600 $lockdir/id_dsa

rm -f $htdocs_patchfile
# Create new tarball.
cd $htdocsdest
tar cf $htdocs_new_tarball .

# If there is no old tarball, we have to transmit the whole tarball.
if test ! -f $htdocs_tarball; then
    test $feedback && echo Transmitting entire tarball. || true
    gzip -c $htdocs_new_tarball | $sshcommand \
        "
        set -e
        umask 002
        gunzip -c > ~/htdocs.tar
        cd $remotehtdocs
        tar xmf ~/htdocs.tar
        gzip -f sitemap
        "
# If the current and the new tarball differ, transmit patch file.
elif ! diff -q $htdocs_tarball $htdocs_new_tarball > /dev/null; then
    # Create patch.
    $bindir/bsdiff $htdocs_tarball $htdocs_new_tarball $htdocs_patchfile
    test $feedback && echo Patch size: `du -h $htdocs_patchfile | sed 's/\t.*//'` || true
    # Delete current tarball.  If something goes wrong with uploading
    # and applying the patch file, docutils-update will notice that
    # the tarball isn't present at the next run and transfer the whole
    # tarball, because we're left in an undefined state (the servers
    # are out of sync).
    rm -f $htdocs_tarball
    # Upload patch file.
    $sshcommand \
        "
        set -e
        umask 002
        cat > ~/htdocs.patch
        ~/bin/bspatch ~/htdocs.tar ~/htdocs.new.tar ~/htdocs.patch
        cd $remotehtdocs
        tar xmf ~/htdocs.new.tar
        gzip -f sitemap
        mv ~/htdocs.new.tar ~/htdocs.tar
        rm -f ~/htdocs.patch
        " \
            < $htdocs_patchfile
fi
mv $htdocs_new_tarball $htdocs_tarball

# Tidy up.
rm -f $htdocs_patchfile
trap - 0 1 2 3 15
rm -rf $lockdir
test $feedback && echo '...docutils-update done.' || true

# Local Variables:
# indent-tabs-mode: nil
# End: