Pick three bugfixes from next branch to trunk for inclusion in 4.5.0 RC2, as discusse...
[sdcc.git] / sdcc-cf / bin / dcf.sh
blob620fe732d6943250a8cffbf88b7c316f63bfacbb
1 #!/bin/sh
3 # dcf.sh - Distributed Compile Farm Mediator
5 # Copyright (c) 2007-2011, Borut Razem <borut dot razem at gmail dot com>
7 # This file is part of sdcc.
9 # This software is provided 'as-is', without any express or implied
10 # warranty. In no event will the authors be held liable for any damages
11 # arising from the use of this software.
13 # Permission is granted to anyone to use this software for any purpose,
14 # including commercial applications, and to alter it and redistribute it
15 # freely, subject to the following restrictions:
17 # 1. The origin of this software must not be misrepresented; you must not
18 # claim that you wrote the original software. If you use this software
19 # in a product, an acknowledgment in the product documentation would be
20 # appreciated but is not required.
21 # 2. Altered source versions must be plainly marked as such, and must not be
22 # misrepresented as being the original software.
23 # 3. This notice may not be removed or altered from any source distribution.
25 LOG_LINES=1000 # max number of lines in the log file
26 BWLIMIT=21 # bandwith limit in KiB for rsync
28 ETC_DIR=$HOME/etc
29 LOG_DIR=$HOME/log
30 LOCK_DIR=$HOME/lock
32 DCF_BUILDER_LIST_FILE=$ETC_DIR/dcf_list
33 DCF_LOG=$LOG_DIR/dcf.log
34 DCF_LOCK=$LOCK_DIR/dcf.lock
36 TREE_FILE=$HOME/tmp/tree.txt
38 mkdir -p $LOG_DIR $LOCK_DIR
40 WEBHOST=web.sourceforge.net
41 WEBUSER=sdcc-builder
42 WEBHTDOCSDIR=/home/project-web/sdcc/htdocs
44 FRSHOST=frs.sourceforge.net
45 FRSUSER=$WEBUSER
46 FRSDIR=/home/frs/project/sdcc/snapshot_builds
49 # debugging: print
50 debug_print ()
51 # $*: text to print
53 test -n "${DEBUG}" && echo =DBG= $*
56 # debugging: print the command and execute it
57 debug_exec ()
58 # $*: command to debug & execute
60 debug_print $*
64 # substring
65 substr ()
66 # $1: string
67 # $2: offset
68 # $3: length
70 if test "$3" = ""
71 then
72 echo "$1" | awk "{print substr(\$0, $2)}"
73 else
74 echo "$1" | awk "{print substr(\$0, $2, $3)}"
79 # file type
80 file_type ()
81 # $1: premissions string
83 expr "$1" : "^\(.\)[-rwxXst]*$"
87 # generate file tree list
88 tree ()
89 # $1: user
90 # $2: host
91 # $3: root
92 # $4: subdir
94 local old_ifs files line file type subdir
96 files=$(echo "ls -lt" | sftp -b- "$1@$2:$3/$4" | sed -e '/^sftp> /d')
97 old_ifs=$IFS
98 IFS='
100 for line in ${files}
102 # get file name from ls -l line
103 # NOTE: this works only for file names without spaces!
104 file=$(expr "${line}" : ".*[ ]\([^ ][^ ]*\)$")
105 type=$(file_type $(substr "${line}" 0 8))
106 case "${type}" in
108 if test -z "$4"
109 then
110 subdir="${file}"
111 else
112 subdir="$4${file}"
114 echo "${line}/$4"
115 tree "$1" "$2" "$3" "${subdir}/"
117 [-l])
118 echo "${line}"
120 esac
121 done
122 IFS="${old_ifs}"
126 # remove files & directories specified in arguments
127 # relays on find -depth
128 rm_list ()
130 local file
132 for file in $*
134 if test -d $file
135 then
136 rmdir $file
137 else
138 rm -f $file
140 done
144 # list files & directories specified in arguments
145 # each file in a new line
146 list_files ()
148 local file
150 for file in $*
152 echo " $file"
153 done
157 # write the standard input to beginning of the log file
158 # and truncate it to $LOG_LINES lines
159 log_it ()
161 local LOG=$(cat)
163 if test -n "$LOG"
164 then
165 if test -e $DCF_LOG -a $(echo "$LOG" | wc -l) -lt $LOG_LINES
166 then
167 LOG=$(echo -e "$LOG\n" | cat - $DCF_LOG | head -n $LOG_LINES)
169 echo "$LOG" > $DCF_LOG
174 # remove more than 7 files in dir from fsr server
175 rm_old_versions ()
177 local i j k
179 for i in ${FRSDIR}
181 for j in $(echo "ls -1t $i" | sftp -b- ${FRSUSER}@${FRSHOST} | sed -e '/^sftp> /d')
183 for k in $(echo "ls -1t $j" | sftp -b- ${FRSUSER}@${FRSHOST} | sed -e '/^sftp> /d' | sed -e '1,7d')
185 if [ -n "$k" ]; then echo "removing $k"; echo "rm $k" | sftp -b- ${FRSUSER}@${FRSHOST}; fi
186 done
187 done
188 done
190 for i in ${WEBHTDOCSDIR}/regression_test_results
192 for j in $(echo "ls -1t $i" | sftp -b- ${WEBUSER}@${WEBHOST} | sed -e '/^sftp> /d')
194 for k in $(echo "ls -1t $j" | sftp -b- ${WEBUSER}@${WEBHOST} | sed -e '/^sftp> /d' | sed -e '1,7d')
196 if [ -n "$k" ]; then echo "removing $k"; echo "rm $k" | sftp -b- ${WEBUSER}@${WEBHOST}; fi
197 done
198 done
199 done
201 for k in $(echo "ls -1t ${WEBHTDOCSDIR}/changelog_heads" | sftp -b- ${WEBUSER}@${WEBHOST} | sed -e '/^sftp> /d' | sed -e '1,7d')
203 if [ -n "$k" ]; then echo "removing $k"; echo "rm $k" | sftp -b- ${WEBUSER}@${WEBHOST}; fi
204 done
208 # cleanup the lock file
209 cleanup ()
211 rm -f $DCF_LOCK
215 # synchronise directory
216 sync_dir ()
217 # $1: source directory to sync
218 # $2: traget machine/directory to sync
219 # $3: exclude source directories from sync
221 local ret=1 excl file_list
223 if test -d $1 && pushd $1 > /dev/null
224 then
225 if test -z "$3"
226 then
227 file_list=$(find * -depth -print 2>/dev/null)
228 else
229 file_list=$(find * -depth -print 2>/dev/null | grep -v -e "^$3")
231 if test -n "${file_list}"
232 then
233 echo "+++ start: $(date)"
234 echo "=== files in $1:"
235 list_files ${file_list}
237 excl=""
238 echo "=== rsyncing..."
239 if test -n "$3"
240 then
241 for dir in $3
243 excl=${excl}" --exclude "${dir}
244 done
246 debug_print "current directory: $(pwd)"
247 debug_exec rsync $RSYNC_OPTS --relative --recursive --include='*.exe' ${excl} -e ssh --size-only * $2 2>&1 | grep -v -e "skipping directory"
249 echo "=== removing..."
250 rm_list ${file_list}
252 echo "=== removing old versions..."
253 rm_old_versions
255 echo "--- end: $(date)"
257 ret=0
259 popd > /dev/null
262 return $ret
265 # main procedure
267 trap 'echo dcf.sh caught signal ; cleanup ; exit 1' 1 2 3 13 15
269 if test -e ${DCF_BUILDER_LIST_FILE}
270 then
271 lockfile -r 0 ${DCF_LOCK} || exit 1
273 test "${BWLIMIT}" != "" && RSYNC_OPTS="${RSYNC_OPTS} --bwlimit=${BWLIMIT}"
275 rm -f ${TREE_FILE}
278 while read -r builder
280 export builder
281 export TREE_FILE
283 builder=$(echo ${builder} | sed -e "s/^\(.*\)#.*$/\1/" -e "s/[ \t]*$//")
284 if test ! -z "${builder}"
285 then
286 if sync_dir "/home/${builder}/htdocs/snapshots" ${FRSUSER}@${FRSHOST}:${FRSDIR}/
287 then
288 if test ! -e ${TREE_FILE}
289 then
290 tree ${FRSUSER} ${FRSHOST} ${FRSDIR} > ${TREE_FILE}
292 sync_dir "/home/${builder}/htdocs" ${WEBUSER}@${WEBHOST}:${WEBHTDOCSDIR}/ "snapshots"
296 done
297 } < ${DCF_BUILDER_LIST_FILE}
299 if test -e ${TREE_FILE}
300 then
301 # upload the new version of ${TREE_FILE}, needed by snap.php to create sdcc snapshots web page
302 echo "put ${TREE_FILE} ${WEBHTDOCSDIR}/$(basename ${TREE_FILE})" | sftp -b- ${WEBUSER}@${WEBHOST}
306 cleanup
307 } 2>&1 | log_it
309 exit 0