Automatically update WebKit via script and launchd

Apr 15, '08 07:30:00AM

Contributed by: Anonymous

WebKit doesn't have any way to automatically update to the latest nightly build, so I put together a quick script to do so.

It has the option of automatically backing up the currently installed version of WebKit, or just removing the old version and updating to the newer version. There's also optional logging of the old WebKit build, the new WebKit build, and what time it was replaced. By creating a launchd agent, it can be run automatically every night. The script is:

#!/bin/sh
#Automatically update to the latest WebKit nightly build.
#By Zach Drayer, zach@drayer.name
#-----------------------------------------------------------------------------
#Set environment up
#-----------------------------------------------------------------------------
export LOGGING_LOCATION="$HOME"/Library/Logs/WebKitUpdate.log
export WEBKIT_BACKUP_LOCATION=""$HOME"/Documents/WebKit Backup/"
curl -LOs "http://nightly.webkit.org/index.html" 
export WEBKIT_NIGHTLY_BUILT_VERSION=`cat index.html | grep mac/WebKit-SVN | grep Download | cut -c 47-51`
if [ -d /Applications/WebKit.app ]; then
  export WEBKIT_INSTALLED_VERSION=`cat /Applications/WebKit.app/Contents/Resources/VERSION`
  if [ "$WEBKIT_NIGHTLY_BUILT_VERSION" == "$WEBKIT_INSTALLED_VERSION" ]; then
    exit
  fi
fi
export WEBKIT_TEMP_INSTALL_LOCATION="/tmp/WebKit-$WEBKIT_NIGHTLY_BUILT_VERSION-$RANDOM"
mkdir "$WEBKIT_TEMP_INSTALL_LOCATION"
cd "$WEBKIT_TEMP_INSTALL_LOCATION"
#-----------------------------------------------------------------------------
#Checks if WebKit is running or not
#-----------------------------------------------------------------------------
ps aux | grep Web >> web.txt
cat web.txt | grep WebKit >> web2.txt
WEBKIT_RUNNING=`cat web2.txt`
if [ "$WEBKIT_RUNNING" != "" ]; then
  kill -9 `cat web2.txt | sed s/"$USER"      //g | cut -c 1-5`
fi
#-----------------------------------------------------------------------------
#Back WebKit up
#-----------------------------------------------------------------------------
if [ -d /Applications/Webkit.app ]; then
  #Comment out this if/elseif block if you don't want to back WebKit up
  #Start commenting below --v
	if [ -d "$WEBKIT_BACKUP_LOCATION" ]; then
		mv /Applications/WebKit.app "$WEBKIT_BACKUP_LOCATION"WebKit-r"$WEBKIT_INSTALLED_VERSION".app
	else
		mkdir "$WEBKIT_BACKUP_LOCATION"
		mv /Applications/WebKit.app "$WEBKIT_BACKUP_LOCATION"WebKit-r"$WEBKIT_INSTALLED_VERSION".app
	fi
	curl -LOs "http://nightly.webkit.org/files/trunk/mac/WebKit-SVN-r"$WEBKIT_NIGHTLY_BUILT_VERSION".dmg"
else
  #Stop Commmenting here --^
#Also uncomment the next line if you dont back WebKit up
#	rm -rf /Applications/WebKit.app
	curl -LOs "http://nightly.webkit.org/files/trunk/mac/WebKit-SVN-r"$WEBKIT_NIGHTLY_BUILT_VERSION".dmg"
fi
#-----------------------------------------------------------------------------
#Install WebKit
#-----------------------------------------------------------------------------
hdiutil attach -quiet "$WEBKIT_TEMP_INSTALL_LOCATION"/WebKit-SVN-r"$WEBKIT_NIGHTLY_BUILT_VERSION".dmg
cp -R /Volumes/WebKit/WebKit.app /Applications/
hdiutil detach -quiet /Volumes/WebKit
rm -rf "$WEBKIT_TEMP_INSTALL_LOCATION"
#-----------------------------------------------------------------------------
#Logging
##Comment this block out if you dont want a log of 
#-----------------------------------------------------------------------------
echo "WebKit build " $WEBKIT_NIGHTLY_BUILT_VERSION " replaced build " $WEBKIT_INSTALLED_VERSION " on " `date` 
>> $HOME/Library/Logs/WebKitUpdate.log
A sample plist for a launchd agent looks like this:
<?xml version="1.0">
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>webkit-nightly-update</string>
<key>UserName</key>
<string>root</string>
<key>GroupName</key>
<string>wheel</string>
<key>ProgramArguments</key>
<array>
	<string>~/Library/Scripts/webkitupdate.sh</string>
</array>
<key>StartCalendarInterval</key>
  <dict>
	<key>Hour</key>
	<integer>7</integer>
  </dict>
</dict>
</plist>
Alternately, you can download the script here, and the plist here.

[robg adds: This script worked as described, though I didn't test the launchd agent. Depending on your connection speed, it may take a few minutes to run, so give it time.]

Comments (9)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20080409043122690