#!/bin/sh
# \
exec tclsh $0 $*
#-------------------------------------------------------------------
# PATH settings
set BUILD_PATH "/project/home/grand/chdk/autobuild"
set BUILD_LOCK_FILE "${BUILD_PATH}/.build_lock"
set LAST_REV_FILE "${BUILD_PATH}/.last_revision"
set LOG_FILE "${BUILD_PATH}/compile.log"
set COMPILER_PATH "/project/home/grand/chdk/gcc/arm-elf/bin"
#-------------------------------------------------------------------
# List of branches to automatic rebuild
set BRANCHES_TO_BUILD [list "trunk"]
#-------------------------------------------------------------------
#-------------------------------------------------------------------
proc run {cmd resVar} {
upvar $resVar x
if {[catch {set res [eval exec $cmd 2>@ stdout]} errMsg]} {
set x $errMsg
return 0
} else {
set x $res
return 1
}
}
#-------------------------------------------------------------------
proc send {file resVar} {
upvar $resVar x
set ok 0
set USER "**********"
set PASSWD "***********"
lappend cmds "user $USER $PASSWD"
lappend cmds "cd hdk/autobuild"
lappend cmds "binary"
lappend cmds "mdelete $file"
lappend cmds "mput $file"
lappend cmds "pwd"
lappend cmds "quit"
if {![catch {set f [open "|ftp -n -i ftp.newmail.ru 2>@ stdout" "w+"]} res]} {
puts $f [join $cmds "\n"]
flush $f
set res ""
while {![eof $f]} {
gets $f ll
lappend res $ll
}
if {![catch {close $f} res2]} {
foreach ll $res {
if {[regexp -- {hdk/autobuild} $ll]} {
set ok 1
break
}
}
} else {
lappend res $res2
}
}
set x $res
return $ok
}
#-------------------------------------------------------------------
proc finish {err} {
global BUILD_PATH BUILD_LOCK_FILE LOG_FILE info log
# Save last compiled revision
if {![catch {set f [open $LOG_FILE "w"]}]} {
if {$err} {
#errors
puts $f "Errors occured during build process: $err."
} else {
puts $f "Build process finished normally."
}
puts $f "================================================\n"
puts $f [join $log "\n++++++++++++++++++++++++++++++++++++++++++++++++\n"]
catch {close $f}
set res ""
cd $BUILD_PATH
send [file tail $LOG_FILE] res
}
catch {file delete $BUILD_LOCK_FILE}
exit
}
#-------------------------------------------------------------------
# Check for lock-file and wait until lock is away
while {[file exists $BUILD_LOCK_FILE]} {
lappend log "Lock is detected. Waiting..."
catch {exec sleep 1m}
}
# Create lock-file
#!!!catch {set f [open $BUILD_LOCK_FILE "w"]; close $f}
set errors 0
set info(last_revision) 0
# Get last compiled revision
if {![catch {set f [open $LAST_REV_FILE "r"]}]} {
catch {gets $f info(last_revision)}
catch {close $f}
}
# Get last revision info
lappend log "Getting last revision info:"
set ok [run [list svn log --verbose -r HEAD "http://tools.assembla.com/svn/chdk/"] res]
lappend log $res
if {!$ok} {
incr errors
lappend log "***FATAL***: Cannot obtain last revision information."
finish $errors
}
set res [split $res "\n"]
# Parse the revision information
# r243 | grand | 2007-12-07 19:18:54 +0300 (Fri, 07 Dec 2007) | 1 line
set info(info) [lindex $res 1]
set list [split $info(info) "|"]
foreach name {revision person date changes} value $list {
set info($name) [string trim $value]
}
set info(revision) [string trim $info(revision) "rR"]
if {$info(revision) == $info(last_revision)} {
exit
}
# Search for branch names
foreach str [lrange $res 3 end] {
if {[regexp -- " \/(\[^\/ \]+)\[\/ \]" $str dummy branch]} {
if {$branch == "branches"} {
if {[regexp -- " \/(\[^\/\]+\/\[^\/ \]+)\[\/ \]" $str dummy branch]} {
set branches($branch) ""
}
} else {
set branches($branch) ""
}
}
}
set env(PATH) "${COMPILER_PATH}:/bin:/usr/bin:$env(PATH)"
#set env(HOME) "/var/www"
# Build only required branches
foreach branch $BRANCHES_TO_BUILD {
if {[info exists branches($branch)]} {
lappend log "\n"
lappend log "Removing old sources of branch: '${branch}'"
catch {cd $BUILD_PATH}
catch {file delete -force -- "${BUILD_PATH}/${branch}"}
# Get sources
lappend log "Getting the sources for branch: '${branch}', revision: $info(revision)"
set ok [run [list svn export -r $info(revision) "[url=http://tools.assembla.com/svn/chdk/]Revision 338: /[/url]${branch}"] res]
lappend log $res
if {!$ok} {
incr errors
lappend log "***FATAL***: Cannot get sources."
continue
}
lappend log "Compiling branch '${branch}'..."
if {[catch {cd "${BUILD_PATH}/${branch}"}]} {
incr errors
lappend log "***ERROR***: cannot change the current directory to '${branch}'."
continue
}
set ok [run [list make NO_INC_BUILD=1 batch-zip] res]
lappend log $res
if {!$ok} {
incr errors
lappend log "***FATAL***: Error occured during compilation."
continue
}
lappend log "Uploading files..."
if {[catch {cd "${BUILD_PATH}/${branch}/bin"}]} {
incr errors
lappend log "***ERROR***: cannot change the current directory to '${branch}/bin'."
continue
}
set ok [send "*.zip" res]
lappend log $res
if {!$ok} {
incr errors
lappend log "***FATAL***: Cannot upload files for branch '${branch}'."
continue
}
}
}
# Save last compiled revision
if {![catch {set f [open $LAST_REV_FILE "w"]}]} {
catch {puts $f $info(revision)}
catch {close $f}
}
finish $errors