downloadkit/downloadkit.py
author William Roberts <williamr@symbian.org>
Thu, 25 Feb 2010 21:37:09 +0000
changeset 172 5f1dcce7f6d4
parent 171 dd0383270574
child 175 5ebd70511e4c
permissions -rw-r--r--
version 0.10 - add --nowinscw option to avoid winscw binaries, add version to the user-agent string
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
172
5f1dcce7f6d4 version 0.10 - add --nowinscw option to avoid winscw binaries, add version to the user-agent string
William Roberts <williamr@symbian.org>
parents: 171
diff changeset
    28
version = '0.10'
5f1dcce7f6d4 version 0.10 - add --nowinscw option to avoid winscw binaries, add version to the user-agent string
William Roberts <williamr@symbian.org>
parents: 171
diff changeset
    29
user_agent = 'downloadkit.py script v' + version
125
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    30
headers = { 'User-Agent' : user_agent }
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    31
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
    32
download_list = []
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
    33
unzip_list = []
125
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    34
171
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 161
diff changeset
    35
def build_opener(debug=False):
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 161
diff changeset
    36
    # Create a HTTP and HTTPS handler with the appropriate debug
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 161
diff changeset
    37
    # level.  We intentionally create a new one because the
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 161
diff changeset
    38
    # OpenerDirector class in urllib2 is smart enough to replace
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 161
diff changeset
    39
    # its internal versions with ours if we pass them into the
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 161
diff changeset
    40
    # urllib2.build_opener method.  This is much easier than trying
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 161
diff changeset
    41
    # to introspect into the OpenerDirector to find the existing
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 161
diff changeset
    42
    # handlers.
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 161
diff changeset
    43
    http_handler = urllib2.HTTPHandler(debuglevel=debug)
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 161
diff changeset
    44
    https_handler = urllib2.HTTPSHandler(debuglevel=debug)
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 161
diff changeset
    45
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 161
diff changeset
    46
    # We want to process cookies, but only in memory so just use
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 161
diff changeset
    47
    # a basic memory-only cookie jar instance
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 161
diff changeset
    48
    cookie_jar = cookielib.LWPCookieJar()
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 161
diff changeset
    49
    cookie_handler = urllib2.HTTPCookieProcessor(cookie_jar)
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 161
diff changeset
    50
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 161
diff changeset
    51
    handlers = [http_handler, https_handler, cookie_handler]
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 161
diff changeset
    52
    opener = urllib2.build_opener(*handlers)
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 161
diff changeset
    53
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 161
diff changeset
    54
    # Save the cookie jar with the opener just in case it's needed
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 161
diff changeset
    55
    # later on
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 161
diff changeset
    56
    opener.cookie_jar = cookie_jar
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 161
diff changeset
    57
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 161
diff changeset
    58
    return opener
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 161
diff changeset
    59
125
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    60
urlopen = urllib2.urlopen
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    61
Request = urllib2.Request
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    62
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    63
def login(prompt):
161
fed3f1d2c557 version 0.8 - added command line options for username and password. Revised the exception handling
William Roberts <williamr@symbian.org>
parents: 157
diff changeset
    64
	global options
125
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    65
	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
    66
	
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    67
	if prompt:
161
fed3f1d2c557 version 0.8 - added command line options for username and password. Revised the exception handling
William Roberts <williamr@symbian.org>
parents: 157
diff changeset
    68
		if options.username == '':
fed3f1d2c557 version 0.8 - added command line options for username and password. Revised the exception handling
William Roberts <williamr@symbian.org>
parents: 157
diff changeset
    69
			print >> sys.stderr, 'username: ',
fed3f1d2c557 version 0.8 - added command line options for username and password. Revised the exception handling
William Roberts <williamr@symbian.org>
parents: 157
diff changeset
    70
			options.username=sys.stdin.readline().strip()
fed3f1d2c557 version 0.8 - added command line options for username and password. Revised the exception handling
William Roberts <williamr@symbian.org>
parents: 157
diff changeset
    71
		if options.password == '':
fed3f1d2c557 version 0.8 - added command line options for username and password. Revised the exception handling
William Roberts <williamr@symbian.org>
parents: 157
diff changeset
    72
			options.password=getpass.getpass()
125
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    73
	
161
fed3f1d2c557 version 0.8 - added command line options for username and password. Revised the exception handling
William Roberts <williamr@symbian.org>
parents: 157
diff changeset
    74
	values = {'username' : options.username,
fed3f1d2c557 version 0.8 - added command line options for username and password. Revised the exception handling
William Roberts <williamr@symbian.org>
parents: 157
diff changeset
    75
	          'password' : options.password,
125
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    76
	          'submit': 'Login'}
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    77
	          
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    78
	headers = { 'User-Agent' : user_agent }
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    79
	
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    80
	
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    81
	data = urllib.urlencode(values)
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    82
	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
    83
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    84
	response = urllib2.urlopen(req)
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    85
	doc=response.read()      
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    86
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    87
	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
    88
		print >> sys.stderr, 'Login failed'
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    89
		return False
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    90
	return True
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    91
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    92
from threading import Thread
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    93
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    94
class unzipfile(Thread):
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    95
	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
    96
		Thread.__init__(self)
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    97
		self.filename = filename
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    98
		self.levels = levels
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    99
		self.deletelevels = deletelevels
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   100
		self.status = -1
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   101
		
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   102
	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
   103
		if unziplevel < 1:
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   104
			return 0   # do nothing
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   105
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   106
		print "  Unzipping " + filename
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   107
		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
   108
		subzips = []
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   109
		for line in filelist.readlines():
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   110
			# 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
   111
			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
   112
			if match is None: continue
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   113
			subzips.append(match.group(1))
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   114
		topstatus = filelist.close()
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   115
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   116
		if deletelevel > 0:
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   117
			print "  Deleting " + filename
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   118
			os.remove(filename)
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   119
		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
   120
			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
   121
			for subzip in subzips:
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   122
				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
   123
		return topstatus
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   124
	def run(self):
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   125
		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
   126
139
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   127
threadlist = []
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   128
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
   129
	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
   130
	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
   131
		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
   132
	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
   133
		deletelevel = 0
139
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   134
	if options.dryrun :
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   135
		global unzip_list
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   136
		if unziplevel > 0:
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("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
   138
			if unziplevel > 1:
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   139
				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
   140
		if deletelevel > 0:
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   141
			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
   142
			if deletelevel > 1:
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   143
				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
   144
		return
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   145
		
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
   146
	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
   147
	global threadlist
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   148
	threadlist.append(unzipthread)
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   149
	unzipthread.start()
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   150
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   151
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
   152
	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
   153
	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
   154
		return
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   155
	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
   156
	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
   157
		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
   158
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
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
   160
	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
   161
	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
   162
		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
   163
	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
   164
	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
   165
		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
   166
			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
   167
			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
   168
	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
   169
	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
   170
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
   171
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
   172
	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
   173
		# 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
   174
		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
   175
			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
   176
		# 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
   177
		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
   178
			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
   179
		# 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
   180
		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
   181
			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
   182
		# 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
   183
		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
   184
			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
   185
		# 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
   186
		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
   187
			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
   188
		# 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
   189
		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
   190
			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
   191
		# 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
   192
		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
   193
	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
   194
	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
   195
	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
   196
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
   197
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
   198
	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
   199
	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
   200
	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
   201
		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
   202
	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
   203
		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
   204
		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
   205
	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
   206
		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
   207
		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
   208
			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
   209
		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
   210
	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
   211
	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
   212
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
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
   214
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
   215
	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
   216
		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
   217
		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
   218
		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
   219
			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
   220
				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
   221
				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
   222
				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
   223
139
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   224
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
   225
	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
   226
	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
   227
	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
   228
		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
   229
			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
   230
			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
   231
			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
   232
				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
   233
					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
   234
				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
   235
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
   236
	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
   237
		global download_list
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   238
		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
   239
		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
   240
		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
   241
139
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   242
	print 'Downloading ' + filename
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   243
	global headers
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   244
	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
   245
	
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
   246
	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
   247
	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
   248
	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
   249
	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
   250
	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
   251
	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
   252
	try:
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   253
		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
   254
		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
   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
			# 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
   257
			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
   258
			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
   259
			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
   260
			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
   261
			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
   262
				# 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
   263
				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
   264
				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
   265
		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
   266
		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
   267
			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
   268
		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
   269
			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
   270
			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
   271
			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
   272
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
	except urllib2.URLError, e:
161
fed3f1d2c557 version 0.8 - added command line options for username and password. Revised the exception handling
William Roberts <williamr@symbian.org>
parents: 157
diff changeset
   274
		print '- ERROR: Failed to start downloading ' + filename
fed3f1d2c557 version 0.8 - added command line options for username and password. Revised the exception handling
William Roberts <williamr@symbian.org>
parents: 157
diff changeset
   275
		if hasattr(e, 'reason'):
fed3f1d2c557 version 0.8 - added command line options for username and password. Revised the exception handling
William Roberts <williamr@symbian.org>
parents: 157
diff changeset
   276
			print 'Reason: ', e.reason
fed3f1d2c557 version 0.8 - added command line options for username and password. Revised the exception handling
William Roberts <williamr@symbian.org>
parents: 157
diff changeset
   277
		elif hasattr(e, 'code'):
fed3f1d2c557 version 0.8 - added command line options for username and password. Revised the exception handling
William Roberts <williamr@symbian.org>
parents: 157
diff changeset
   278
			print 'Error code: ', e.code
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
   279
		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
   280
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
	# 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
   282
	
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
   283
	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
   284
		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
   285
		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
   286
		while True:
139
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   287
			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
   288
			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
   289
			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
   290
			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
   291
			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
   292
				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
   293
				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
   294
				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
   295
					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
   296
					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
   297
						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
   298
					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
   299
						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
   300
					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
   301
				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
   302
				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
   303
				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
   304
			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
   305
			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
   306
139
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   307
		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
   308
		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
   309
			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
   310
			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
   311
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   312
	#handle errors
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   313
	except urllib2.URLError, e:
161
fed3f1d2c557 version 0.8 - added command line options for username and password. Revised the exception handling
William Roberts <williamr@symbian.org>
parents: 157
diff changeset
   314
		print '- ERROR: Failed while downloading ' + filename
fed3f1d2c557 version 0.8 - added command line options for username and password. Revised the exception handling
William Roberts <williamr@symbian.org>
parents: 157
diff changeset
   315
		if hasattr(e, 'reason'):
fed3f1d2c557 version 0.8 - added command line options for username and password. Revised the exception handling
William Roberts <williamr@symbian.org>
parents: 157
diff changeset
   316
			print 'Reason: ', e.reason
fed3f1d2c557 version 0.8 - added command line options for username and password. Revised the exception handling
William Roberts <williamr@symbian.org>
parents: 157
diff changeset
   317
		elif hasattr(e, 'code'):
fed3f1d2c557 version 0.8 - added command line options for username and password. Revised the exception handling
William Roberts <williamr@symbian.org>
parents: 157
diff changeset
   318
			print 'Error code: ', e.code
139
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   319
		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
   320
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
   321
	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
   322
		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
   323
		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
   324
			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
   325
139
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   326
	return True
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   327
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   328
def downloadkit(version):	
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   329
	global headers
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   330
	global options
125
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   331
	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
   332
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   333
	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
   334
	if version[0] == 2:
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   335
		viewid= 1  # Symbian^2
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   336
	if version[0] == 3:
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   337
		viewid= 5  # Symbian^3
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   338
	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
   339
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
   340
	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
   341
	response = urllib2.urlopen(req)
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   342
	doc=response.read()
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   343
	
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   344
	# 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
   345
	try:
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   346
		bodystart=doc.find('<body>')
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   347
		doc = doc[bodystart:]
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   348
	except:
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   349
		pass
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   350
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   351
	threadlist = []
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   352
	# 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
   353
	# <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
   354
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   355
	soup=BeautifulSoup(doc)
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   356
	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
   357
	results.sort(orderResults)
125
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   358
	for result in results:
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   359
		downloadurl = urlbase + result['href']
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   360
		filename = result['title']
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   361
139
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   362
		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
   363
			continue 	# no snapshots of Mercurial source thanks...
172
5f1dcce7f6d4 version 0.10 - add --nowinscw option to avoid winscw binaries, add version to the user-agent string
William Roberts <williamr@symbian.org>
parents: 171
diff changeset
   364
		if options.nowinscw and re.search(r"winscw", filename) :
5f1dcce7f6d4 version 0.10 - add --nowinscw option to avoid winscw binaries, add version to the user-agent string
William Roberts <williamr@symbian.org>
parents: 171
diff changeset
   365
			continue 	# no winscw emulator...
139
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   366
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   367
		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
   368
			continue # download failed
125
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   369
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   370
		# 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
   371
		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
   372
			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
   373
			
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
   374
		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
   375
			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
   376
		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
   377
			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
   378
		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
   379
			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
   380
		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
   381
			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
   382
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   383
	# 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
   384
	complete_outstanding_unzips()  
125
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   385
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   386
	return 1
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   387
172
5f1dcce7f6d4 version 0.10 - add --nowinscw option to avoid winscw binaries, add version to the user-agent string
William Roberts <williamr@symbian.org>
parents: 171
diff changeset
   388
parser = OptionParser(version="%prog "+version, 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
   389
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
   390
	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
   391
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
   392
	help="Don't download any of the source code available directly from Mercurial")
172
5f1dcce7f6d4 version 0.10 - add --nowinscw option to avoid winscw binaries, add version to the user-agent string
William Roberts <williamr@symbian.org>
parents: 171
diff changeset
   393
parser.add_option("--nowinscw", action="store_true", dest="nowinscw",
5f1dcce7f6d4 version 0.10 - add --nowinscw option to avoid winscw binaries, add version to the user-agent string
William Roberts <williamr@symbian.org>
parents: 171
diff changeset
   394
	help="Don't download the winscw emulator")
140
9baccbcc5509 version 0.4 - added --nounzip option to suppress the unzipping and deletion
William Roberts <williamr@symbian.org>
parents: 139
diff changeset
   395
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
   396
	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
   397
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
   398
	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
   399
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
   400
	help="Report download progress")
161
fed3f1d2c557 version 0.8 - added command line options for username and password. Revised the exception handling
William Roberts <williamr@symbian.org>
parents: 157
diff changeset
   401
parser.add_option("-u", "--username", dest="username", metavar="USER",
fed3f1d2c557 version 0.8 - added command line options for username and password. Revised the exception handling
William Roberts <williamr@symbian.org>
parents: 157
diff changeset
   402
	help="login to website as USER")
fed3f1d2c557 version 0.8 - added command line options for username and password. Revised the exception handling
William Roberts <williamr@symbian.org>
parents: 157
diff changeset
   403
parser.add_option("-p", "--password", dest="password", metavar="PWD",
fed3f1d2c557 version 0.8 - added command line options for username and password. Revised the exception handling
William Roberts <williamr@symbian.org>
parents: 157
diff changeset
   404
	help="specify the account password")
171
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 161
diff changeset
   405
parser.add_option("--debug", action="store_true", dest="debug", 
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 161
diff changeset
   406
	help="debug HTML traffic (not recommended!)")
161
fed3f1d2c557 version 0.8 - added command line options for username and password. Revised the exception handling
William Roberts <williamr@symbian.org>
parents: 157
diff changeset
   407
parser.set_defaults(
fed3f1d2c557 version 0.8 - added command line options for username and password. Revised the exception handling
William Roberts <williamr@symbian.org>
parents: 157
diff changeset
   408
	dryrun=False, 
fed3f1d2c557 version 0.8 - added command line options for username and password. Revised the exception handling
William Roberts <williamr@symbian.org>
parents: 157
diff changeset
   409
	nosrc=False, 
172
5f1dcce7f6d4 version 0.10 - add --nowinscw option to avoid winscw binaries, add version to the user-agent string
William Roberts <williamr@symbian.org>
parents: 171
diff changeset
   410
	nowinscw=False, 
161
fed3f1d2c557 version 0.8 - added command line options for username and password. Revised the exception handling
William Roberts <williamr@symbian.org>
parents: 157
diff changeset
   411
	nounzip=False, 
fed3f1d2c557 version 0.8 - added command line options for username and password. Revised the exception handling
William Roberts <williamr@symbian.org>
parents: 157
diff changeset
   412
	nodelete=False, 
fed3f1d2c557 version 0.8 - added command line options for username and password. Revised the exception handling
William Roberts <williamr@symbian.org>
parents: 157
diff changeset
   413
	progress=False,
fed3f1d2c557 version 0.8 - added command line options for username and password. Revised the exception handling
William Roberts <williamr@symbian.org>
parents: 157
diff changeset
   414
	username='',
171
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 161
diff changeset
   415
	password='',
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 161
diff changeset
   416
	debug=False
161
fed3f1d2c557 version 0.8 - added command line options for username and password. Revised the exception handling
William Roberts <williamr@symbian.org>
parents: 157
diff changeset
   417
	)
139
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   418
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   419
(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
   420
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
   421
	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
   422
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
   423
	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
   424
171
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 161
diff changeset
   425
opener = build_opener(options.debug)
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 161
diff changeset
   426
urllib2.install_opener(opener)
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 161
diff changeset
   427
125
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   428
login(True)
139
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   429
downloadkit(args[0])
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   430
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   431
if options.dryrun:
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   432
	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
   433
	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
   434
		print download
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   435
	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
   436
		print command
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 129
diff changeset
   437