I got really tired of the monotony that I go through daily to put a
new build on my USB drive.  The attached script is my moderately
robust solution to the problem, and I thought that others might care
to take it for a spin as well.  Here's the usage (you can also view
usage with -h or --help):

Usage:

   sugar-update.oy [-lLsv] [-b build] [directory]

Description:

   This script will download an autoinstallation image (.img and .crc) to
   the specified [directory]. Any images already present within this
   directory will be deleted before the download begins. If no directory is
   specified, the current working directory will be used.

Options:

   -b   Specify a build number; defaults to latest development build
   -l   Print the associated change log
   -L   Print the change log without performing a download
   -s   Download the latest stable build; overrides the -b option
   -v   Verbose

Common Example:

   sugar-update.py -vl /Volumes/MyFlashDrive/boot/

Feel free to add functionality and/or robustness.  I may add support
for downloading a firmware update in the future, but it wasn't on my
requirements list for today.  I hope others find it useful, even if it
is soon outdated by some fancy built-in auto-update jazz.

- Eben
"""
sugar-update.py

Created by Eben Eliason on 2007-08-28.
Please feel free to modify, extend, and redistribute this script
"""

import sys
import os
import getopt
import re
import urllib2

#define help output ---------------------------------------------------------
def usage():
	print '\nUsage: sugar-update.py [-lLsv] [-b build] [directory]\n'
	print 'Description:\n'
	print '\tThis script will download an autoinstallation image (.img and .crc files) into the specified'
	print '\t[directory]. Any images already present within the this directory will be deleted before the'
	print '\tdownload begins. If no directory is specified, the current working directory will be used.\n'
	print 'Options:\n'
	print '\t -b\tNUM\tSpecify a build number; defaults to latest development build\n'
	print '\t -l\t\tPrint the associated change log\n'
	print '\t -L\t\tPrint the change log for the specified build without performing a download\n'
	print '\t -s\t\tDownload the latest stable build; overrides the -b option\n'
	print '\t -v\t\tVerbose\n'

#check for valid arguments
try:
	opts,arg = getopt.getopt(sys.argv[1:], "vslLb:")
except:
	usage()
	sys.exit(2)
		
#global constants -----------------------------------------------------------

LATEST = "latest"
STABLE = "LATEST-STABLE-BUILD"

build = ""
build_path = LATEST
boot_path = os.getcwd() + '/'
verbose = False;
log = False
log_only = False

if len(arg) > 0:
	boot_path = arg[0].rstrip('/') + '/'

#interpret arguments --------------------------------------------------------
for o, a in opts:
	if o == '-b':
		if build_path != STABLE:
			build = a
			build_path = 'build' + build
	elif o == '-s':
		build = ""
		build_path = STABLE
	elif o == '-l':
		log = True
	elif o == '-L':
		log = True
		log_only = True
	elif o == '-v':
		verbose = True
	elif o == '-h' or 0 == '--help':
		usage()
		exit(0)
		
base_path = "http://olpc.download.redhat.com/olpc/streams/development/"; + build_path + "/devel_jffs2/"

#determine latest stable or development build number ------------------------
if build == "":
	
	try:
		url_file = urllib2.urlopen(base_path)
		
		for line in url_file:
			build,n = re.subn(r'.*os(\d+)\.img.*\n', r'\1', line)
			if n > 0:
				break
				
		url_file.close()
	
	except:
		if build_path == LATEST:
			sys.exit('Error: Could not locate latest development build')
		else:
			sys.exit('Error: Could not locate latest stable build')

crc_filename = "os" + build + ".crc"
img_filename = "os" + build + ".img"

#print the change log -------------------------------------------------------
if log:
	try:
		url_file = urllib2.urlopen(base_path + "ChangeLog")
		
		print '\n--------------------------------------------------------------------------------'
		for line in url_file:
				print line.rstrip('\n')
		print '--------------------------------------------------------------------------------\n'
		
		url_file.close()
	except urllib2.HTTPError, e:
		print '\n--------------------------------------------------------------------------------'
		print "No change log available for build " + build
		print '--------------------------------------------------------------------------------\n'

	if log_only:
		sys.exit(0)

#remove images from boot directory ------------------------------------------
if verbose:
	print "Removing images from " + boot_path


try:
	for f in os.listdir(boot_path):
		if re.match(r'^os\d+\.', f):
			print "\tremoving " + f + "..."
			os.remove(boot_path + f)
except:
	sys.exit('Error: could not locate ' + boot_path)


if verbose:
	if build_path == LATEST:
		print "Downloading latest development build (" + build + ")"
	elif build_path == STABLE:
		print "Downloading latest stable build (" + build + ")"
	else:
		print "Downloading build " + build	

#download the CRC file ------------------------------------------------------
if verbose:
	print "\tdownloading " + crc_filename + "..."
try:
	url_file = urllib2.urlopen(base_path + crc_filename)
	crc_file = open(boot_path + crc_filename, 'wb')

	for line in url_file:
		crc_file.write(line)

	url_file.close()
	crc_file.close()
except urllib2.HTTPError, e:
	sys.exit('Error: Could not download crc file (' + str(e.code) + ')')
except:
	sys.exit('Error: Could not write ' + boot_path + crc_filename)

#download the IMG file ------------------------------------------------------
if verbose:
	print "\tdownloading " + img_filename + "..."
try:
	url_file = urllib2.urlopen(base_path + img_filename)
	img_file = open(boot_path + img_filename, 'wb')

	for line in url_file:
		img_file.write(line)

	url_file.close()
	img_file.close()
except urllib2.HTTPError, e:
	sys.exit('Error: Could not download img file (' + str(e.code) + ')')
except:
	sys.exit('Error: Could not write ' + boot_path + img_filename)


if verbose:
	print 'Autoinstallation build ' + build + ' completed.\n'
_______________________________________________
Devel mailing list
[email protected]
http://lists.laptop.org/listinfo/devel

Reply via email to