downloadkit/downloadkit.py
author William Roberts <williamr@symbian.org>
Tue, 09 Feb 2010 17:40:51 +0000
changeset 157 0df3a90af030
parent 155 b6c06a8333fb
child 161 fed3f1d2c557
permissions -rw-r--r--
version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD) The script will always download release_metadata,xml, even in --dryrun mode, so that it can check the validity of any zip files you've already downloaded.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
125
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
     1
#!/usr/bin/python
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
     2
# Copyright (c) 2009 Symbian Foundation.
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
     3
# All rights reserved.
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
     4
# This component and the accompanying materials are made available
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
     5
# under the terms of the License "Eclipse Public License v1.0"
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
     6
# which accompanies this distribution, and is available
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
     7
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
     8
#
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
     9
# Initial Contributors:
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    10
# Symbian Foundation - Initial contribution
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    11
# 
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    12
# Description:
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    13
# Script to download and unpack a Symbian PDK - assumes "7z" installed to unzip the files
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    14
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    15
import urllib2
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    16
import urllib
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    17
import os.path
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    18
import cookielib
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    19
import sys
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    20
import getpass
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    21
import re
141
55dd69d60bbc version 0.5 - fix unzipping bug, add "--progress" option for info on long downloads
William Roberts <williamr@symbian.org>
parents: 140
diff changeset
    22
import time
125
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    23
from BeautifulSoup import BeautifulSoup
139
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
    24
from optparse import OptionParser
157
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
    25
import hashlib
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
    26
import xml.etree.ElementTree as ET 
125
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    27
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    28
user_agent = 'downloadkit.py script'
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    29
headers = { 'User-Agent' : user_agent }
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    30
top_level_url = "http://developer.symbian.org"
139
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
    31
download_list = []
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
    32
unzip_list = []
125
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    33
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    34
username = ''
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    35
password = ''
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    36
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    37
COOKIEFILE = 'cookies.lwp'
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    38
# the path and filename to save your cookies in
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    39
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    40
# importing cookielib worked
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    41
urlopen = urllib2.urlopen
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    42
Request = urllib2.Request
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    43
cj = cookielib.LWPCookieJar()
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    44
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    45
# This is a subclass of FileCookieJar
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    46
# that has useful load and save methods
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    47
if os.path.isfile(COOKIEFILE):
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    48
	cj.load(COOKIEFILE)
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    49
	
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    50
# Now we need to get our Cookie Jar
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    51
# installed in the opener;
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    52
# for fetching URLs
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    53
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    54
urllib2.install_opener(opener)
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    55
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    56
def login(prompt):
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    57
	global username
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    58
	global password
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    59
	loginurl = 'https://developer.symbian.org/main/user_profile/login.php'
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    60
	
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    61
	if prompt:
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    62
		print >> sys.stderr, 'username: ',
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    63
		username=sys.stdin.readline().strip()
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    64
		password=getpass.getpass()
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    65
	
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    66
	values = {'username' : username,
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    67
	          'password' : password,
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    68
	          'submit': 'Login'}
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    69
	          
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    70
	headers = { 'User-Agent' : user_agent }
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    71
	
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    72
	
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    73
	data = urllib.urlencode(values)
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    74
	req = urllib2.Request(loginurl, data, headers)
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    75
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    76
	response = urllib2.urlopen(req)
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    77
	doc=response.read()      
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    78
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    79
	if doc.find('Please try again') != -1:
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    80
		print >> sys.stderr, 'Login failed'
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    81
		return False
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    82
	
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    83
	cj.save(COOKIEFILE) 
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    84
	return True
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    85
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    86
from threading import Thread
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    87
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    88
class unzipfile(Thread):
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    89
	def __init__ (self,filename,levels=1,deletelevels=0):
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    90
		Thread.__init__(self)
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    91
		self.filename = filename
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    92
		self.levels = levels
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    93
		self.deletelevels = deletelevels
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    94
		self.status = -1
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    95
		
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    96
	def unzip(self,filename,unziplevel,deletelevel):
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    97
		if unziplevel < 1:
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    98
			return 0   # do nothing
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    99
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   100
		print "  Unzipping " + filename
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   101
		filelist = os.popen("7z x -y "+self.filename)
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   102
		subzips = []
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   103
		for line in filelist.readlines():
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   104
			# Extracting  src_oss_app_webuis.zip
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   105
			match = re.match(r"^Extracting\s+(\S+.zip)$", line)
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   106
			if match is None: continue
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   107
			subzips.append(match.group(1))
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   108
		topstatus = filelist.close()
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   109
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   110
		if deletelevel > 0:
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   111
			print "  Deleting " + filename
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   112
			os.remove(filename)
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   113
		if unziplevel > 1 and len(subzips) > 0:
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   114
			print "  Expanding %d zip files from %s" % (len(subzips), filename)
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   115
			for subzip in subzips:
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   116
				self.unzip(subzip, unziplevel-1, deletelevel-1)
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   117
		return topstatus
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   118
	def run(self):
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   119
		self.status = self.unzip(self.filename, self.levels, self.deletelevels)
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   120
139
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   121
threadlist = []
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   122
def schedule_unzip(filename, unziplevel, deletelevel):
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   123
	global options
140
9baccbcc5509 version 0.4 - added --nounzip option to suppress the unzipping and deletion
William Roberts <williamr@symbian.org>
parents: 139
diff changeset
   124
	if options.nounzip :
9baccbcc5509 version 0.4 - added --nounzip option to suppress the unzipping and deletion
William Roberts <williamr@symbian.org>
parents: 139
diff changeset
   125
		return
154
77c25e4f2c6f version 0.6 - avoid problem if Content-Length not supplied, add --nodelete option, check 7z works before starting
William Roberts <williamr@symbian.org>
parents: 142
diff changeset
   126
	if options.nodelete :
77c25e4f2c6f version 0.6 - avoid problem if Content-Length not supplied, add --nodelete option, check 7z works before starting
William Roberts <williamr@symbian.org>
parents: 142
diff changeset
   127
		deletelevel = 0
139
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   128
	if options.dryrun :
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   129
		global unzip_list
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   130
		if unziplevel > 0:
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   131
			unzip_list.append("7z x -y %s" % filename)
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   132
			if unziplevel > 1:
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   133
				unzip_list.append("# unzip recursively %d more times" % unziplevel-1)
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   134
		if deletelevel > 0:
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   135
			unzip_list.append("# delete %s" % filename)
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   136
			if deletelevel > 1:
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   137
				unzip_list.append("# delete zip files recursively %d more times" % deletelevel-1)
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   138
		return
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   139
		
141
55dd69d60bbc version 0.5 - fix unzipping bug, add "--progress" option for info on long downloads
William Roberts <williamr@symbian.org>
parents: 140
diff changeset
   140
	unzipthread = unzipfile(filename, unziplevel, deletelevel)
139
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   141
	global threadlist
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   142
	threadlist.append(unzipthread)
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   143
	unzipthread.start()
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   144
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   145
def complete_outstanding_unzips():
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   146
	global options
140
9baccbcc5509 version 0.4 - added --nounzip option to suppress the unzipping and deletion
William Roberts <williamr@symbian.org>
parents: 139
diff changeset
   147
	if options.dryrun or options.nounzip:
139
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   148
		return
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   149
	print "Waiting for outstanding commands to finish..."
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   150
	for thread in threadlist:
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   151
		thread.join()  
154
77c25e4f2c6f version 0.6 - avoid problem if Content-Length not supplied, add --nodelete option, check 7z works before starting
William Roberts <williamr@symbian.org>
parents: 142
diff changeset
   152
77c25e4f2c6f version 0.6 - avoid problem if Content-Length not supplied, add --nodelete option, check 7z works before starting
William Roberts <williamr@symbian.org>
parents: 142
diff changeset
   153
def check_unzip_environment():
77c25e4f2c6f version 0.6 - avoid problem if Content-Length not supplied, add --nodelete option, check 7z works before starting
William Roberts <williamr@symbian.org>
parents: 142
diff changeset
   154
	global options
77c25e4f2c6f version 0.6 - avoid problem if Content-Length not supplied, add --nodelete option, check 7z works before starting
William Roberts <williamr@symbian.org>
parents: 142
diff changeset
   155
	if options.nounzip:
77c25e4f2c6f version 0.6 - avoid problem if Content-Length not supplied, add --nodelete option, check 7z works before starting
William Roberts <williamr@symbian.org>
parents: 142
diff changeset
   156
		return True		# if we aren't unzipping, no need to have 7z installed
77c25e4f2c6f version 0.6 - avoid problem if Content-Length not supplied, add --nodelete option, check 7z works before starting
William Roberts <williamr@symbian.org>
parents: 142
diff changeset
   157
	help = os.popen("7z -h")
77c25e4f2c6f version 0.6 - avoid problem if Content-Length not supplied, add --nodelete option, check 7z works before starting
William Roberts <williamr@symbian.org>
parents: 142
diff changeset
   158
	for line in help.readlines():
77c25e4f2c6f version 0.6 - avoid problem if Content-Length not supplied, add --nodelete option, check 7z works before starting
William Roberts <williamr@symbian.org>
parents: 142
diff changeset
   159
		if re.match('7-Zip', line) :
77c25e4f2c6f version 0.6 - avoid problem if Content-Length not supplied, add --nodelete option, check 7z works before starting
William Roberts <williamr@symbian.org>
parents: 142
diff changeset
   160
			help.close()
77c25e4f2c6f version 0.6 - avoid problem if Content-Length not supplied, add --nodelete option, check 7z works before starting
William Roberts <williamr@symbian.org>
parents: 142
diff changeset
   161
			return True
77c25e4f2c6f version 0.6 - avoid problem if Content-Length not supplied, add --nodelete option, check 7z works before starting
William Roberts <williamr@symbian.org>
parents: 142
diff changeset
   162
	help.close()
77c25e4f2c6f version 0.6 - avoid problem if Content-Length not supplied, add --nodelete option, check 7z works before starting
William Roberts <williamr@symbian.org>
parents: 142
diff changeset
   163
	return False
77c25e4f2c6f version 0.6 - avoid problem if Content-Length not supplied, add --nodelete option, check 7z works before starting
William Roberts <williamr@symbian.org>
parents: 142
diff changeset
   164
129
249ca6c587b6 Changes to control the order of downloading, and hopefully the resulting unzipping (for patches in particular)
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   165
def orderResults(x,y) :
249ca6c587b6 Changes to control the order of downloading, and hopefully the resulting unzipping (for patches in particular)
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   166
	def ranking(name) :
157
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   167
		# 0th = release_metadata
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   168
		if re.match(r"release_metadata", name):
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   169
			return 0000;
129
249ca6c587b6 Changes to control the order of downloading, and hopefully the resulting unzipping (for patches in particular)
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   170
		# 1st = release_metadata, build_BOM.zip (both small things!)
157
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   171
		if re.match(r"build_BOM", name):
129
249ca6c587b6 Changes to control the order of downloading, and hopefully the resulting unzipping (for patches in particular)
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   172
			return 1000;
249ca6c587b6 Changes to control the order of downloading, and hopefully the resulting unzipping (for patches in particular)
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   173
		# 2nd = tools, binaries (required for execution and compilation)
249ca6c587b6 Changes to control the order of downloading, and hopefully the resulting unzipping (for patches in particular)
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   174
		elif re.match(r"(binaries_|tools_)", name):
249ca6c587b6 Changes to control the order of downloading, and hopefully the resulting unzipping (for patches in particular)
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   175
			return 2000;
249ca6c587b6 Changes to control the order of downloading, and hopefully the resulting unzipping (for patches in particular)
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   176
		# 3rd = rnd binaries, binary patches
249ca6c587b6 Changes to control the order of downloading, and hopefully the resulting unzipping (for patches in particular)
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   177
		elif re.match(r"(bin_)", name):
249ca6c587b6 Changes to control the order of downloading, and hopefully the resulting unzipping (for patches in particular)
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   178
			return 3000;
249ca6c587b6 Changes to control the order of downloading, and hopefully the resulting unzipping (for patches in particular)
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   179
		# 4th = sources
249ca6c587b6 Changes to control the order of downloading, and hopefully the resulting unzipping (for patches in particular)
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   180
		elif re.match(r"(src_sfl|src_oss)", name):
249ca6c587b6 Changes to control the order of downloading, and hopefully the resulting unzipping (for patches in particular)
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   181
			return 4000;
249ca6c587b6 Changes to control the order of downloading, and hopefully the resulting unzipping (for patches in particular)
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   182
		# 5rd = rnd sources, source patches (not sure we'd ever have those)
249ca6c587b6 Changes to control the order of downloading, and hopefully the resulting unzipping (for patches in particular)
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   183
		elif re.match(r"(src_)", name):
249ca6c587b6 Changes to control the order of downloading, and hopefully the resulting unzipping (for patches in particular)
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   184
			return 5000;
249ca6c587b6 Changes to control the order of downloading, and hopefully the resulting unzipping (for patches in particular)
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   185
		# Last, anything else
249ca6c587b6 Changes to control the order of downloading, and hopefully the resulting unzipping (for patches in particular)
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   186
		return 10000;
249ca6c587b6 Changes to control the order of downloading, and hopefully the resulting unzipping (for patches in particular)
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   187
	xtitle = x['title']
249ca6c587b6 Changes to control the order of downloading, and hopefully the resulting unzipping (for patches in particular)
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   188
	ytitle = y['title']
249ca6c587b6 Changes to control the order of downloading, and hopefully the resulting unzipping (for patches in particular)
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   189
	return cmp(ranking(xtitle)+cmp(xtitle,ytitle), ranking(ytitle))
249ca6c587b6 Changes to control the order of downloading, and hopefully the resulting unzipping (for patches in particular)
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   190
157
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   191
def md5_checksum(filename):
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   192
	MD5_BLOCK_SIZE = 128 * 1024
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   193
	md5 = hashlib.md5()
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   194
	try:
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   195
		file = open(filename,"rb")
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   196
	except IOError:
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   197
		print "Terminating script: Unable to open %S" % filename
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   198
		sys.exit()
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   199
	while True:
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   200
		data = file.read(MD5_BLOCK_SIZE)
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   201
		if not data:
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   202
			break
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   203
		md5.update(data)
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   204
	file.close()
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   205
	return md5.hexdigest().upper()
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   206
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   207
checksums = {}
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   208
def parse_release_metadata(filename):
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   209
	if os.path.exists(filename):
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   210
		tree = ET.parse(filename)
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   211
		iter = tree.getiterator('package')
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   212
		for element in iter:
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   213
			if element.keys():
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   214
				file = element.get("name")
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   215
				md5 = element.get("md5checksum")
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   216
				checksums[file] = md5.upper()
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   217
139
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   218
def download_file(filename,url):
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   219
	global options
157
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   220
	global checksums
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   221
	if os.path.exists(filename):
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   222
		if filename in checksums:
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   223
			print 'Checking existing ' + filename
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   224
			file_checksum = md5_checksum(filename)
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   225
			if file_checksum == checksums[filename]:
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   226
				if options.progress:
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   227
					print '- OK ' + filename
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   228
				return True
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   229
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   230
	if options.dryrun and not re.match(r"release_metadata", filename):
139
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   231
		global download_list
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   232
		download_info = "download %s %s" % (filename, url)
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   233
		download_list.append(download_info)
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   234
		return True
157
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   235
139
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   236
	print 'Downloading ' + filename
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   237
	global headers
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   238
	req = urllib2.Request(url, None, headers)
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   239
	
157
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   240
	CHUNK = 128 * 1024
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   241
	size = 0
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   242
	filesize = -1
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   243
	start_time = time.time()
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   244
	last_time = start_time
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   245
	last_size = size
139
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   246
	try:
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   247
		response = urllib2.urlopen(req)
157
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   248
		chunk = response.read(CHUNK)
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   249
		if chunk.find('<div id="sign_in_box">') != -1:
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   250
			# our urllib2 cookies have gone awol - login again
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   251
			login(False)
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   252
			req = urllib2.Request(url, None, headers)
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   253
			response = urllib2.urlopen(req)
139
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   254
			chunk = response.read(CHUNK)
157
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   255
			if chunk.find('<div id="sign_in_box">') != -1:
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   256
				# still broken - give up on this one
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   257
				print "*** ERROR trying to download %s" % (filename)
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   258
				return False
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   259
		info = response.info()
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   260
		if 'Content-Length' in info:
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   261
			filesize = int(info['Content-Length'])
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   262
		else:
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   263
			print "*** HTTP response did not contain 'Content-Length' when expected"
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   264
			print info
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   265
			return False
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   266
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   267
	except urllib2.HTTPError, e:
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   268
		print "HTTP Error:",e.code , url
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   269
		return False
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   270
	except urllib2.URLError, e:
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   271
		print "URL Error:",e.reason , url
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   272
		return False
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   273
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   274
	# we are now up and running, and chunk contains the start of the download
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   275
	
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   276
	try:
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   277
		fp = open(filename, 'wb')
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   278
		md5 = hashlib.md5()
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   279
		while True:
139
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   280
			fp.write(chunk)
157
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   281
			md5.update(chunk)
141
55dd69d60bbc version 0.5 - fix unzipping bug, add "--progress" option for info on long downloads
William Roberts <williamr@symbian.org>
parents: 140
diff changeset
   282
			size += len(chunk)
55dd69d60bbc version 0.5 - fix unzipping bug, add "--progress" option for info on long downloads
William Roberts <williamr@symbian.org>
parents: 140
diff changeset
   283
			now = time.time()
55dd69d60bbc version 0.5 - fix unzipping bug, add "--progress" option for info on long downloads
William Roberts <williamr@symbian.org>
parents: 140
diff changeset
   284
			if options.progress and now-last_time > 20:
142
1ef616833a37 version 0.5.1 - improve the progress reporting, getting the file size from the HTTP message header
William Roberts <williamr@symbian.org>
parents: 141
diff changeset
   285
				rate = (size-last_size)/(now-last_time)
1ef616833a37 version 0.5.1 - improve the progress reporting, getting the file size from the HTTP message header
William Roberts <williamr@symbian.org>
parents: 141
diff changeset
   286
				estimate = ""
1ef616833a37 version 0.5.1 - improve the progress reporting, getting the file size from the HTTP message header
William Roberts <williamr@symbian.org>
parents: 141
diff changeset
   287
				if filesize > 0 and rate > 0:
1ef616833a37 version 0.5.1 - improve the progress reporting, getting the file size from the HTTP message header
William Roberts <williamr@symbian.org>
parents: 141
diff changeset
   288
					remaining_seconds = (filesize-size)/rate
1ef616833a37 version 0.5.1 - improve the progress reporting, getting the file size from the HTTP message header
William Roberts <williamr@symbian.org>
parents: 141
diff changeset
   289
					if remaining_seconds > 110:
1ef616833a37 version 0.5.1 - improve the progress reporting, getting the file size from the HTTP message header
William Roberts <williamr@symbian.org>
parents: 141
diff changeset
   290
						remaining = "%d minutes" % (remaining_seconds/60)
1ef616833a37 version 0.5.1 - improve the progress reporting, getting the file size from the HTTP message header
William Roberts <williamr@symbian.org>
parents: 141
diff changeset
   291
					else:
1ef616833a37 version 0.5.1 - improve the progress reporting, getting the file size from the HTTP message header
William Roberts <williamr@symbian.org>
parents: 141
diff changeset
   292
						remaining = "%d seconds" % remaining_seconds
1ef616833a37 version 0.5.1 - improve the progress reporting, getting the file size from the HTTP message header
William Roberts <williamr@symbian.org>
parents: 141
diff changeset
   293
					estimate = "- %d%% est. %s" % ((100*size/filesize), remaining)
1ef616833a37 version 0.5.1 - improve the progress reporting, getting the file size from the HTTP message header
William Roberts <williamr@symbian.org>
parents: 141
diff changeset
   294
				print "- %d Kb (%d Kb/s) %s" % (size/1024, (rate/1024)+0.5, estimate)
141
55dd69d60bbc version 0.5 - fix unzipping bug, add "--progress" option for info on long downloads
William Roberts <williamr@symbian.org>
parents: 140
diff changeset
   295
				last_time = now
55dd69d60bbc version 0.5 - fix unzipping bug, add "--progress" option for info on long downloads
William Roberts <williamr@symbian.org>
parents: 140
diff changeset
   296
				last_size = size
157
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   297
			chunk = response.read(CHUNK)
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   298
			if not chunk: break
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   299
139
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   300
		fp.close()
142
1ef616833a37 version 0.5.1 - improve the progress reporting, getting the file size from the HTTP message header
William Roberts <williamr@symbian.org>
parents: 141
diff changeset
   301
		if options.progress:
1ef616833a37 version 0.5.1 - improve the progress reporting, getting the file size from the HTTP message header
William Roberts <williamr@symbian.org>
parents: 141
diff changeset
   302
			now = time.time()
157
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   303
			print "- Completed %s - %d Kb in %d seconds" % (filename, (filesize/1024)+0.5, now-start_time)
139
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   304
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   305
	#handle errors
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   306
	except urllib2.HTTPError, e:
155
b6c06a8333fb v0.6.1 - fix "NameError: global name 'downloadurl' is not defined" when there's an HTTP timeout
William Roberts <williamr@symbian.org>
parents: 154
diff changeset
   307
		print "HTTP Error:",e.code , url
139
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   308
		return False
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   309
	except urllib2.URLError, e:
155
b6c06a8333fb v0.6.1 - fix "NameError: global name 'downloadurl' is not defined" when there's an HTTP timeout
William Roberts <williamr@symbian.org>
parents: 154
diff changeset
   310
		print "URL Error:",e.reason , url
139
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   311
		return False
157
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   312
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   313
	if filename in checksums:
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   314
		download_checksum = md5.hexdigest().upper()
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   315
		if download_checksum != checksums[filename]:
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   316
			print '- WARNING: %s checksum does not match' % filename
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   317
139
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   318
	return True
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   319
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   320
def downloadkit(version):	
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   321
	global headers
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   322
	global options
125
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   323
	urlbase = 'http://developer.symbian.org/main/tools_and_kits/downloads/'
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   324
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   325
	viewid = 5   # default to Symbian^3
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   326
	if version[0] == 2:
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   327
		viewid= 1  # Symbian^2
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   328
	if version[0] == 3:
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   329
		viewid= 5  # Symbian^3
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   330
	url = urlbase + ('view.php?id=%d'% viewid) + 'vId=' + version
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   331
126
c107e11c37e8 Mark all requests with the "downloadkit.py script" User Agent string, so that we can spot them in the HTTP logs
William Roberts <williamr@symbian.org>
parents: 125
diff changeset
   332
	req = urllib2.Request(url, None, headers)
125
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   333
	response = urllib2.urlopen(req)
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   334
	doc=response.read()
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   335
	
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   336
	# BeatifulSoup chokes on some javascript, so we cut away everything before the <body>
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   337
	try:
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   338
		bodystart=doc.find('<body>')
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   339
		doc = doc[bodystart:]
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   340
	except:
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   341
		pass
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   342
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   343
	threadlist = []
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   344
	# let's hope the HTML format never changes...
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   345
	# <a href='download.php?id=27&cid=60&iid=270' title='src_oss_mw.zip'> ...</a> 
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   346
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   347
	soup=BeautifulSoup(doc)
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   348
	results=soup.findAll('a', href=re.compile("^download"), title=re.compile("\.(zip|xml)$"))
129
249ca6c587b6 Changes to control the order of downloading, and hopefully the resulting unzipping (for patches in particular)
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   349
	results.sort(orderResults)
125
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   350
	for result in results:
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   351
		downloadurl = urlbase + result['href']
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   352
		filename = result['title']
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   353
139
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   354
		if options.nosrc and re.match(r"(src_sfl|src_oss)", filename) :
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   355
			continue 	# no snapshots of Mercurial source thanks...
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   356
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   357
		if download_file(filename, downloadurl) != True :
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   358
			continue # download failed
125
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   359
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   360
		# unzip the file (if desired)
139
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   361
		if re.match(r"patch", filename):
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   362
			complete_outstanding_unzips()	# ensure that the thing we are patching is completed first
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   363
			
157
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   364
		if re.match(r"release_metadata", filename):
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   365
			parse_release_metadata(filename)	# read the md5 checksums etc
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   366
		elif re.match(r"(bin|tools).*\.zip", filename):
139
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   367
			schedule_unzip(filename, 1, 0)   # unzip once, don't delete
125
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   368
		elif re.match(r"src_.*\.zip", filename):
139
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   369
			schedule_unzip(filename, 1, 1)   # zip of zips, delete top level
125
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   370
		elif re.match(r"build_BOM.zip", filename):
139
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   371
			schedule_unzip(filename, 1, 1)   # unpack then delete zip as it's not needed again
125
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   372
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   373
	# wait for the unzipping threads to complete
139
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   374
	complete_outstanding_unzips()  
125
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   375
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   376
	return 1
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   377
157
0df3a90af030 version 0.7 - add parsing of release_metadata.xml and checking of the MD5 checksums (based on suggested code from ChrisD)
William Roberts <williamr@symbian.org>
parents: 155
diff changeset
   378
parser = OptionParser(version="%prog 0.7", usage="Usage: %prog [options] version")
139
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   379
parser.add_option("-n", "--dryrun", action="store_true", dest="dryrun",
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   380
	help="print the files to be downloaded, the 7z commands, and the recommended deletions")
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   381
parser.add_option("--nosrc", action="store_true", dest="nosrc",
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   382
	help="Don't download any of the source code available directly from Mercurial")
140
9baccbcc5509 version 0.4 - added --nounzip option to suppress the unzipping and deletion
William Roberts <williamr@symbian.org>
parents: 139
diff changeset
   383
parser.add_option("--nounzip", action="store_true", dest="nounzip",
9baccbcc5509 version 0.4 - added --nounzip option to suppress the unzipping and deletion
William Roberts <williamr@symbian.org>
parents: 139
diff changeset
   384
	help="Just download, don't unzip or delete any files")
154
77c25e4f2c6f version 0.6 - avoid problem if Content-Length not supplied, add --nodelete option, check 7z works before starting
William Roberts <williamr@symbian.org>
parents: 142
diff changeset
   385
parser.add_option("--nodelete", action="store_true", dest="nodelete",
77c25e4f2c6f version 0.6 - avoid problem if Content-Length not supplied, add --nodelete option, check 7z works before starting
William Roberts <williamr@symbian.org>
parents: 142
diff changeset
   386
	help="Do not delete files after unzipping")
141
55dd69d60bbc version 0.5 - fix unzipping bug, add "--progress" option for info on long downloads
William Roberts <williamr@symbian.org>
parents: 140
diff changeset
   387
parser.add_option("--progress", action="store_true", dest="progress",
55dd69d60bbc version 0.5 - fix unzipping bug, add "--progress" option for info on long downloads
William Roberts <williamr@symbian.org>
parents: 140
diff changeset
   388
	help="Report download progress")
154
77c25e4f2c6f version 0.6 - avoid problem if Content-Length not supplied, add --nodelete option, check 7z works before starting
William Roberts <williamr@symbian.org>
parents: 142
diff changeset
   389
parser.set_defaults(dryrun=False, nosrc=False, nounzip=False, nodelete=False, progress=False)
139
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   390
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   391
(options, args) = parser.parse_args()
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   392
if len(args) != 1:
154
77c25e4f2c6f version 0.6 - avoid problem if Content-Length not supplied, add --nodelete option, check 7z works before starting
William Roberts <williamr@symbian.org>
parents: 142
diff changeset
   393
	parser.error("Must supply a PDK version, e.g. 3.0.f")
77c25e4f2c6f version 0.6 - avoid problem if Content-Length not supplied, add --nodelete option, check 7z works before starting
William Roberts <williamr@symbian.org>
parents: 142
diff changeset
   394
if not check_unzip_environment() :
77c25e4f2c6f version 0.6 - avoid problem if Content-Length not supplied, add --nodelete option, check 7z works before starting
William Roberts <williamr@symbian.org>
parents: 142
diff changeset
   395
	parser.error("Unable to execute 7z command")
125
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   396
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   397
login(True)
139
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   398
downloadkit(args[0])
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   399
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   400
if options.dryrun:
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   401
	print "# instructions for downloading kit " + args[0]
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   402
	for download in download_list:
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   403
		print download
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   404
	for command in unzip_list:
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   405
		print command
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   406