downloadkit/downloadkit.py
author William Roberts <williamr@symbian.org>
Mon, 01 Mar 2010 17:46:44 +0000
changeset 174 5ebd70511e4c
parent 171 5f1dcce7f6d4
child 193 f74ba2451a44
permissions -rw-r--r--
v0.11 - added quick_networking_check(), to confirm that we can talk to https://developer.symbian.org
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
124
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
174
5ebd70511e4c v0.11 - added quick_networking_check(), to confirm that we can talk to https://developer.symbian.org
William Roberts <williamr@symbian.org>
parents: 171
diff changeset
    15
import socket
124
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    16
import urllib2
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    17
import urllib
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    18
import os.path
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    19
import cookielib
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    20
import sys
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    21
import getpass
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    22
import re
140
55dd69d60bbc version 0.5 - fix unzipping bug, add "--progress" option for info on long downloads
William Roberts <williamr@symbian.org>
parents: 139
diff changeset
    23
import time
124
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    24
from BeautifulSoup import BeautifulSoup
138
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
    25
from optparse import OptionParser
156
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: 154
diff changeset
    26
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: 154
diff changeset
    27
import xml.etree.ElementTree as ET 
124
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    28
174
5ebd70511e4c v0.11 - added quick_networking_check(), to confirm that we can talk to https://developer.symbian.org
William Roberts <williamr@symbian.org>
parents: 171
diff changeset
    29
version = '0.11'
171
5f1dcce7f6d4 version 0.10 - add --nowinscw option to avoid winscw binaries, add version to the user-agent string
William Roberts <williamr@symbian.org>
parents: 170
diff changeset
    30
user_agent = 'downloadkit.py script v' + version
124
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    31
headers = { 'User-Agent' : user_agent }
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    32
top_level_url = "http://developer.symbian.org"
138
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
    33
download_list = []
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
    34
unzip_list = []
124
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    35
170
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 160
diff changeset
    36
def build_opener(debug=False):
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 160
diff changeset
    37
    # 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: 160
diff changeset
    38
    # 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: 160
diff changeset
    39
    # 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: 160
diff changeset
    40
    # 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: 160
diff changeset
    41
    # 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: 160
diff changeset
    42
    # 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: 160
diff changeset
    43
    # handlers.
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 160
diff changeset
    44
    http_handler = urllib2.HTTPHandler(debuglevel=debug)
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 160
diff changeset
    45
    https_handler = urllib2.HTTPSHandler(debuglevel=debug)
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 160
diff changeset
    46
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 160
diff changeset
    47
    # 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: 160
diff changeset
    48
    # 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: 160
diff changeset
    49
    cookie_jar = cookielib.LWPCookieJar()
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 160
diff changeset
    50
    cookie_handler = urllib2.HTTPCookieProcessor(cookie_jar)
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 160
diff changeset
    51
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 160
diff changeset
    52
    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: 160
diff changeset
    53
    opener = urllib2.build_opener(*handlers)
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 160
diff changeset
    54
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 160
diff changeset
    55
    # 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: 160
diff changeset
    56
    # later on
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 160
diff changeset
    57
    opener.cookie_jar = cookie_jar
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 160
diff changeset
    58
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 160
diff changeset
    59
    return opener
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 160
diff changeset
    60
124
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    61
urlopen = urllib2.urlopen
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    62
Request = urllib2.Request
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    63
174
5ebd70511e4c v0.11 - added quick_networking_check(), to confirm that we can talk to https://developer.symbian.org
William Roberts <williamr@symbian.org>
parents: 171
diff changeset
    64
def quick_networking_check():
5ebd70511e4c v0.11 - added quick_networking_check(), to confirm that we can talk to https://developer.symbian.org
William Roberts <williamr@symbian.org>
parents: 171
diff changeset
    65
	global options
5ebd70511e4c v0.11 - added quick_networking_check(), to confirm that we can talk to https://developer.symbian.org
William Roberts <williamr@symbian.org>
parents: 171
diff changeset
    66
	defaulttimeout = socket.getdefaulttimeout()
5ebd70511e4c v0.11 - added quick_networking_check(), to confirm that we can talk to https://developer.symbian.org
William Roberts <williamr@symbian.org>
parents: 171
diff changeset
    67
	socket.setdefaulttimeout(15)
5ebd70511e4c v0.11 - added quick_networking_check(), to confirm that we can talk to https://developer.symbian.org
William Roberts <williamr@symbian.org>
parents: 171
diff changeset
    68
	probesite = 'https://developer.symbian.org'
5ebd70511e4c v0.11 - added quick_networking_check(), to confirm that we can talk to https://developer.symbian.org
William Roberts <williamr@symbian.org>
parents: 171
diff changeset
    69
	probeurl = probesite + '/main/user_profile/login.php'
5ebd70511e4c v0.11 - added quick_networking_check(), to confirm that we can talk to https://developer.symbian.org
William Roberts <williamr@symbian.org>
parents: 171
diff changeset
    70
	headers = { 'User-Agent' : user_agent }
5ebd70511e4c v0.11 - added quick_networking_check(), to confirm that we can talk to https://developer.symbian.org
William Roberts <williamr@symbian.org>
parents: 171
diff changeset
    71
5ebd70511e4c v0.11 - added quick_networking_check(), to confirm that we can talk to https://developer.symbian.org
William Roberts <williamr@symbian.org>
parents: 171
diff changeset
    72
	req = urllib2.Request(probeurl, None, headers)
5ebd70511e4c v0.11 - added quick_networking_check(), to confirm that we can talk to https://developer.symbian.org
William Roberts <williamr@symbian.org>
parents: 171
diff changeset
    73
5ebd70511e4c v0.11 - added quick_networking_check(), to confirm that we can talk to https://developer.symbian.org
William Roberts <williamr@symbian.org>
parents: 171
diff changeset
    74
	try:
5ebd70511e4c v0.11 - added quick_networking_check(), to confirm that we can talk to https://developer.symbian.org
William Roberts <williamr@symbian.org>
parents: 171
diff changeset
    75
		response = urllib2.urlopen(req)
5ebd70511e4c v0.11 - added quick_networking_check(), to confirm that we can talk to https://developer.symbian.org
William Roberts <williamr@symbian.org>
parents: 171
diff changeset
    76
		doc=response.read()
5ebd70511e4c v0.11 - added quick_networking_check(), to confirm that we can talk to https://developer.symbian.org
William Roberts <williamr@symbian.org>
parents: 171
diff changeset
    77
	except urllib2.URLError, e:
5ebd70511e4c v0.11 - added quick_networking_check(), to confirm that we can talk to https://developer.symbian.org
William Roberts <williamr@symbian.org>
parents: 171
diff changeset
    78
		print '*** Problem accessing ' + probesite
5ebd70511e4c v0.11 - added quick_networking_check(), to confirm that we can talk to https://developer.symbian.org
William Roberts <williamr@symbian.org>
parents: 171
diff changeset
    79
		if hasattr(e, 'reason'):
5ebd70511e4c v0.11 - added quick_networking_check(), to confirm that we can talk to https://developer.symbian.org
William Roberts <williamr@symbian.org>
parents: 171
diff changeset
    80
			print '*** Reason: ', e.reason
5ebd70511e4c v0.11 - added quick_networking_check(), to confirm that we can talk to https://developer.symbian.org
William Roberts <williamr@symbian.org>
parents: 171
diff changeset
    81
		elif hasattr(e, 'code'):
5ebd70511e4c v0.11 - added quick_networking_check(), to confirm that we can talk to https://developer.symbian.org
William Roberts <williamr@symbian.org>
parents: 171
diff changeset
    82
			print '*** Error code: ', e.code
5ebd70511e4c v0.11 - added quick_networking_check(), to confirm that we can talk to https://developer.symbian.org
William Roberts <williamr@symbian.org>
parents: 171
diff changeset
    83
		print "Do you need to use a proxy server to access the developer.symbian.org website?"
5ebd70511e4c v0.11 - added quick_networking_check(), to confirm that we can talk to https://developer.symbian.org
William Roberts <williamr@symbian.org>
parents: 171
diff changeset
    84
		sys.exit(1)
5ebd70511e4c v0.11 - added quick_networking_check(), to confirm that we can talk to https://developer.symbian.org
William Roberts <williamr@symbian.org>
parents: 171
diff changeset
    85
	socket.setdefaulttimeout(defaulttimeout)	# restore the default timeout
5ebd70511e4c v0.11 - added quick_networking_check(), to confirm that we can talk to https://developer.symbian.org
William Roberts <williamr@symbian.org>
parents: 171
diff changeset
    86
	if options.progress:
5ebd70511e4c v0.11 - added quick_networking_check(), to confirm that we can talk to https://developer.symbian.org
William Roberts <williamr@symbian.org>
parents: 171
diff changeset
    87
		print "Confirmed that we can access " + probesite
5ebd70511e4c v0.11 - added quick_networking_check(), to confirm that we can talk to https://developer.symbian.org
William Roberts <williamr@symbian.org>
parents: 171
diff changeset
    88
124
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    89
def login(prompt):
160
fed3f1d2c557 version 0.8 - added command line options for username and password. Revised the exception handling
William Roberts <williamr@symbian.org>
parents: 156
diff changeset
    90
	global options
124
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    91
	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
    92
	
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    93
	if prompt:
160
fed3f1d2c557 version 0.8 - added command line options for username and password. Revised the exception handling
William Roberts <williamr@symbian.org>
parents: 156
diff changeset
    94
		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: 156
diff changeset
    95
			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: 156
diff changeset
    96
			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: 156
diff changeset
    97
		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: 156
diff changeset
    98
			options.password=getpass.getpass()
124
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
    99
	
160
fed3f1d2c557 version 0.8 - added command line options for username and password. Revised the exception handling
William Roberts <williamr@symbian.org>
parents: 156
diff changeset
   100
	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: 156
diff changeset
   101
	          'password' : options.password,
124
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   102
	          'submit': 'Login'}
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   103
	          
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   104
	headers = { 'User-Agent' : user_agent }
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
	
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   107
	data = urllib.urlencode(values)
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   108
	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
   109
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   110
	response = urllib2.urlopen(req)
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   111
	doc=response.read()      
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   112
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   113
	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
   114
		print >> sys.stderr, 'Login failed'
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   115
		return False
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   116
	return True
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   117
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   118
from threading import Thread
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   119
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   120
class unzipfile(Thread):
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   121
	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
   122
		Thread.__init__(self)
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   123
		self.filename = filename
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   124
		self.levels = levels
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   125
		self.deletelevels = deletelevels
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   126
		self.status = -1
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   127
		
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   128
	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
   129
		if unziplevel < 1:
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   130
			return 0   # do nothing
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   131
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   132
		print "  Unzipping " + filename
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   133
		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
   134
		subzips = []
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   135
		for line in filelist.readlines():
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   136
			# 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
   137
			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
   138
			if match is None: continue
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   139
			subzips.append(match.group(1))
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   140
		topstatus = filelist.close()
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   141
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   142
		if deletelevel > 0:
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   143
			print "  Deleting " + filename
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   144
			os.remove(filename)
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   145
		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
   146
			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
   147
			for subzip in subzips:
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   148
				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
   149
		return topstatus
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   150
	def run(self):
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   151
		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
   152
138
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   153
threadlist = []
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   154
def schedule_unzip(filename, unziplevel, deletelevel):
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   155
	global options
139
9baccbcc5509 version 0.4 - added --nounzip option to suppress the unzipping and deletion
William Roberts <williamr@symbian.org>
parents: 138
diff changeset
   156
	if options.nounzip :
9baccbcc5509 version 0.4 - added --nounzip option to suppress the unzipping and deletion
William Roberts <williamr@symbian.org>
parents: 138
diff changeset
   157
		return
153
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: 141
diff changeset
   158
	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: 141
diff changeset
   159
		deletelevel = 0
138
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   160
	if options.dryrun :
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   161
		global unzip_list
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   162
		if unziplevel > 0:
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   163
			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: 128
diff changeset
   164
			if unziplevel > 1:
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   165
				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: 128
diff changeset
   166
		if deletelevel > 0:
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   167
			unzip_list.append("# delete %s" % filename)
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   168
			if deletelevel > 1:
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   169
				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: 128
diff changeset
   170
		return
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   171
		
140
55dd69d60bbc version 0.5 - fix unzipping bug, add "--progress" option for info on long downloads
William Roberts <williamr@symbian.org>
parents: 139
diff changeset
   172
	unzipthread = unzipfile(filename, unziplevel, deletelevel)
138
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   173
	global threadlist
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   174
	threadlist.append(unzipthread)
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   175
	unzipthread.start()
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   176
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   177
def complete_outstanding_unzips():
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   178
	global options
139
9baccbcc5509 version 0.4 - added --nounzip option to suppress the unzipping and deletion
William Roberts <williamr@symbian.org>
parents: 138
diff changeset
   179
	if options.dryrun or options.nounzip:
138
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   180
		return
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   181
	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: 128
diff changeset
   182
	for thread in threadlist:
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   183
		thread.join()  
153
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: 141
diff changeset
   184
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: 141
diff changeset
   185
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: 141
diff changeset
   186
	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: 141
diff changeset
   187
	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: 141
diff changeset
   188
		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: 141
diff changeset
   189
	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: 141
diff changeset
   190
	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: 141
diff changeset
   191
		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: 141
diff changeset
   192
			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: 141
diff changeset
   193
			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: 141
diff changeset
   194
	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: 141
diff changeset
   195
	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: 141
diff changeset
   196
128
249ca6c587b6 Changes to control the order of downloading, and hopefully the resulting unzipping (for patches in particular)
William Roberts <williamr@symbian.org>
parents: 127
diff changeset
   197
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: 127
diff changeset
   198
	def ranking(name) :
156
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: 154
diff changeset
   199
		# 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: 154
diff changeset
   200
		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: 154
diff changeset
   201
			return 0000;
128
249ca6c587b6 Changes to control the order of downloading, and hopefully the resulting unzipping (for patches in particular)
William Roberts <williamr@symbian.org>
parents: 127
diff changeset
   202
		# 1st = release_metadata, build_BOM.zip (both small things!)
156
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: 154
diff changeset
   203
		if re.match(r"build_BOM", name):
128
249ca6c587b6 Changes to control the order of downloading, and hopefully the resulting unzipping (for patches in particular)
William Roberts <williamr@symbian.org>
parents: 127
diff changeset
   204
			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: 127
diff changeset
   205
		# 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: 127
diff changeset
   206
		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: 127
diff changeset
   207
			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: 127
diff changeset
   208
		# 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: 127
diff changeset
   209
		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: 127
diff changeset
   210
			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: 127
diff changeset
   211
		# 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: 127
diff changeset
   212
		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: 127
diff changeset
   213
			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: 127
diff changeset
   214
		# 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: 127
diff changeset
   215
		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: 127
diff changeset
   216
			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: 127
diff changeset
   217
		# 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: 127
diff changeset
   218
		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: 127
diff changeset
   219
	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: 127
diff changeset
   220
	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: 127
diff changeset
   221
	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: 127
diff changeset
   222
156
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: 154
diff changeset
   223
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: 154
diff changeset
   224
	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: 154
diff changeset
   225
	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: 154
diff changeset
   226
	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: 154
diff changeset
   227
		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: 154
diff changeset
   228
	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: 154
diff changeset
   229
		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: 154
diff changeset
   230
		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: 154
diff changeset
   231
	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: 154
diff changeset
   232
		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: 154
diff changeset
   233
		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: 154
diff changeset
   234
			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: 154
diff changeset
   235
		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: 154
diff changeset
   236
	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: 154
diff changeset
   237
	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: 154
diff changeset
   238
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: 154
diff changeset
   239
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: 154
diff changeset
   240
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: 154
diff changeset
   241
	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: 154
diff changeset
   242
		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: 154
diff changeset
   243
		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: 154
diff changeset
   244
		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: 154
diff changeset
   245
			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: 154
diff changeset
   246
				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: 154
diff changeset
   247
				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: 154
diff changeset
   248
				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: 154
diff changeset
   249
138
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   250
def download_file(filename,url):
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   251
	global options
156
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: 154
diff changeset
   252
	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: 154
diff changeset
   253
	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: 154
diff changeset
   254
		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: 154
diff changeset
   255
			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: 154
diff changeset
   256
			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: 154
diff changeset
   257
			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: 154
diff changeset
   258
				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: 154
diff changeset
   259
					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: 154
diff changeset
   260
				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: 154
diff changeset
   261
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: 154
diff changeset
   262
	if options.dryrun and not re.match(r"release_metadata", filename):
138
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   263
		global download_list
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   264
		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: 128
diff changeset
   265
		download_list.append(download_info)
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   266
		return True
156
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: 154
diff changeset
   267
138
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   268
	print 'Downloading ' + filename
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   269
	global headers
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   270
	req = urllib2.Request(url, None, headers)
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   271
	
156
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: 154
diff changeset
   272
	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: 154
diff changeset
   273
	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: 154
diff changeset
   274
	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: 154
diff changeset
   275
	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: 154
diff changeset
   276
	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: 154
diff changeset
   277
	last_size = size
138
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   278
	try:
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   279
		response = urllib2.urlopen(req)
156
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: 154
diff changeset
   280
		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: 154
diff changeset
   281
		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: 154
diff changeset
   282
			# 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: 154
diff changeset
   283
			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: 154
diff changeset
   284
			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: 154
diff changeset
   285
			response = urllib2.urlopen(req)
138
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   286
			chunk = response.read(CHUNK)
156
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: 154
diff changeset
   287
			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: 154
diff changeset
   288
				# 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: 154
diff changeset
   289
				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: 154
diff changeset
   290
				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: 154
diff changeset
   291
		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: 154
diff changeset
   292
		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: 154
diff changeset
   293
			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: 154
diff changeset
   294
		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: 154
diff changeset
   295
			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: 154
diff changeset
   296
			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: 154
diff changeset
   297
			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: 154
diff changeset
   298
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: 154
diff changeset
   299
	except urllib2.URLError, e:
160
fed3f1d2c557 version 0.8 - added command line options for username and password. Revised the exception handling
William Roberts <williamr@symbian.org>
parents: 156
diff changeset
   300
		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: 156
diff changeset
   301
		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: 156
diff changeset
   302
			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: 156
diff changeset
   303
		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: 156
diff changeset
   304
			print 'Error code: ', e.code
156
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: 154
diff changeset
   305
		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: 154
diff changeset
   306
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: 154
diff changeset
   307
	# 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: 154
diff changeset
   308
	
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: 154
diff changeset
   309
	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: 154
diff changeset
   310
		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: 154
diff changeset
   311
		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: 154
diff changeset
   312
		while True:
138
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   313
			fp.write(chunk)
156
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: 154
diff changeset
   314
			md5.update(chunk)
140
55dd69d60bbc version 0.5 - fix unzipping bug, add "--progress" option for info on long downloads
William Roberts <williamr@symbian.org>
parents: 139
diff changeset
   315
			size += len(chunk)
55dd69d60bbc version 0.5 - fix unzipping bug, add "--progress" option for info on long downloads
William Roberts <williamr@symbian.org>
parents: 139
diff changeset
   316
			now = time.time()
55dd69d60bbc version 0.5 - fix unzipping bug, add "--progress" option for info on long downloads
William Roberts <williamr@symbian.org>
parents: 139
diff changeset
   317
			if options.progress and now-last_time > 20:
141
1ef616833a37 version 0.5.1 - improve the progress reporting, getting the file size from the HTTP message header
William Roberts <williamr@symbian.org>
parents: 140
diff changeset
   318
				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: 140
diff changeset
   319
				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: 140
diff changeset
   320
				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: 140
diff changeset
   321
					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: 140
diff changeset
   322
					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: 140
diff changeset
   323
						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: 140
diff changeset
   324
					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: 140
diff changeset
   325
						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: 140
diff changeset
   326
					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: 140
diff changeset
   327
				print "- %d Kb (%d Kb/s) %s" % (size/1024, (rate/1024)+0.5, estimate)
140
55dd69d60bbc version 0.5 - fix unzipping bug, add "--progress" option for info on long downloads
William Roberts <williamr@symbian.org>
parents: 139
diff changeset
   328
				last_time = now
55dd69d60bbc version 0.5 - fix unzipping bug, add "--progress" option for info on long downloads
William Roberts <williamr@symbian.org>
parents: 139
diff changeset
   329
				last_size = size
156
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: 154
diff changeset
   330
			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: 154
diff changeset
   331
			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: 154
diff changeset
   332
138
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   333
		fp.close()
141
1ef616833a37 version 0.5.1 - improve the progress reporting, getting the file size from the HTTP message header
William Roberts <williamr@symbian.org>
parents: 140
diff changeset
   334
		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: 140
diff changeset
   335
			now = time.time()
156
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: 154
diff changeset
   336
			print "- Completed %s - %d Kb in %d seconds" % (filename, (filesize/1024)+0.5, now-start_time)
138
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   337
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   338
	#handle errors
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   339
	except urllib2.URLError, e:
160
fed3f1d2c557 version 0.8 - added command line options for username and password. Revised the exception handling
William Roberts <williamr@symbian.org>
parents: 156
diff changeset
   340
		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: 156
diff changeset
   341
		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: 156
diff changeset
   342
			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: 156
diff changeset
   343
		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: 156
diff changeset
   344
			print 'Error code: ', e.code
138
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   345
		return False
156
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: 154
diff changeset
   346
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: 154
diff changeset
   347
	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: 154
diff changeset
   348
		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: 154
diff changeset
   349
		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: 154
diff changeset
   350
			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: 154
diff changeset
   351
138
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   352
	return True
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   353
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   354
def downloadkit(version):	
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   355
	global headers
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   356
	global options
124
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   357
	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
   358
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   359
	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
   360
	if version[0] == 2:
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   361
		viewid= 1  # Symbian^2
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   362
	if version[0] == 3:
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   363
		viewid= 5  # Symbian^3
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   364
	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
   365
125
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: 124
diff changeset
   366
	req = urllib2.Request(url, None, headers)
124
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   367
	response = urllib2.urlopen(req)
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   368
	doc=response.read()
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
	# 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
   371
	try:
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   372
		bodystart=doc.find('<body>')
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   373
		doc = doc[bodystart:]
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   374
	except:
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   375
		pass
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   376
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   377
	threadlist = []
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   378
	# 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
   379
	# <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
   380
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   381
	soup=BeautifulSoup(doc)
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   382
	results=soup.findAll('a', href=re.compile("^download"), title=re.compile("\.(zip|xml)$"))
128
249ca6c587b6 Changes to control the order of downloading, and hopefully the resulting unzipping (for patches in particular)
William Roberts <williamr@symbian.org>
parents: 127
diff changeset
   383
	results.sort(orderResults)
124
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   384
	for result in results:
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   385
		downloadurl = urlbase + result['href']
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   386
		filename = result['title']
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   387
138
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   388
		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: 128
diff changeset
   389
			continue 	# no snapshots of Mercurial source thanks...
171
5f1dcce7f6d4 version 0.10 - add --nowinscw option to avoid winscw binaries, add version to the user-agent string
William Roberts <williamr@symbian.org>
parents: 170
diff changeset
   390
		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: 170
diff changeset
   391
			continue 	# no winscw emulator...
138
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   392
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   393
		if download_file(filename, downloadurl) != True :
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   394
			continue # download failed
124
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   395
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   396
		# unzip the file (if desired)
138
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   397
		if re.match(r"patch", filename):
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   398
			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: 128
diff changeset
   399
			
156
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: 154
diff changeset
   400
		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: 154
diff changeset
   401
			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: 154
diff changeset
   402
		elif re.match(r"(bin|tools).*\.zip", filename):
138
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   403
			schedule_unzip(filename, 1, 0)   # unzip once, don't delete
124
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   404
		elif re.match(r"src_.*\.zip", filename):
138
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   405
			schedule_unzip(filename, 1, 1)   # zip of zips, delete top level
124
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   406
		elif re.match(r"build_BOM.zip", filename):
138
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   407
			schedule_unzip(filename, 1, 1)   # unpack then delete zip as it's not needed again
124
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   408
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   409
	# wait for the unzipping threads to complete
138
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   410
	complete_outstanding_unzips()  
124
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   411
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   412
	return 1
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   413
171
5f1dcce7f6d4 version 0.10 - add --nowinscw option to avoid winscw binaries, add version to the user-agent string
William Roberts <williamr@symbian.org>
parents: 170
diff changeset
   414
parser = OptionParser(version="%prog "+version, usage="Usage: %prog [options] version")
138
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   415
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: 128
diff changeset
   416
	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: 128
diff changeset
   417
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: 128
diff changeset
   418
	help="Don't download any of the source code available directly from Mercurial")
171
5f1dcce7f6d4 version 0.10 - add --nowinscw option to avoid winscw binaries, add version to the user-agent string
William Roberts <williamr@symbian.org>
parents: 170
diff changeset
   419
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: 170
diff changeset
   420
	help="Don't download the winscw emulator")
139
9baccbcc5509 version 0.4 - added --nounzip option to suppress the unzipping and deletion
William Roberts <williamr@symbian.org>
parents: 138
diff changeset
   421
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: 138
diff changeset
   422
	help="Just download, don't unzip or delete any files")
153
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: 141
diff changeset
   423
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: 141
diff changeset
   424
	help="Do not delete files after unzipping")
140
55dd69d60bbc version 0.5 - fix unzipping bug, add "--progress" option for info on long downloads
William Roberts <williamr@symbian.org>
parents: 139
diff changeset
   425
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: 139
diff changeset
   426
	help="Report download progress")
160
fed3f1d2c557 version 0.8 - added command line options for username and password. Revised the exception handling
William Roberts <williamr@symbian.org>
parents: 156
diff changeset
   427
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: 156
diff changeset
   428
	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: 156
diff changeset
   429
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: 156
diff changeset
   430
	help="specify the account password")
170
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 160
diff changeset
   431
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: 160
diff changeset
   432
	help="debug HTML traffic (not recommended!)")
160
fed3f1d2c557 version 0.8 - added command line options for username and password. Revised the exception handling
William Roberts <williamr@symbian.org>
parents: 156
diff changeset
   433
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: 156
diff changeset
   434
	dryrun=False, 
fed3f1d2c557 version 0.8 - added command line options for username and password. Revised the exception handling
William Roberts <williamr@symbian.org>
parents: 156
diff changeset
   435
	nosrc=False, 
171
5f1dcce7f6d4 version 0.10 - add --nowinscw option to avoid winscw binaries, add version to the user-agent string
William Roberts <williamr@symbian.org>
parents: 170
diff changeset
   436
	nowinscw=False, 
160
fed3f1d2c557 version 0.8 - added command line options for username and password. Revised the exception handling
William Roberts <williamr@symbian.org>
parents: 156
diff changeset
   437
	nounzip=False, 
fed3f1d2c557 version 0.8 - added command line options for username and password. Revised the exception handling
William Roberts <williamr@symbian.org>
parents: 156
diff changeset
   438
	nodelete=False, 
fed3f1d2c557 version 0.8 - added command line options for username and password. Revised the exception handling
William Roberts <williamr@symbian.org>
parents: 156
diff changeset
   439
	progress=False,
fed3f1d2c557 version 0.8 - added command line options for username and password. Revised the exception handling
William Roberts <williamr@symbian.org>
parents: 156
diff changeset
   440
	username='',
170
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 160
diff changeset
   441
	password='',
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 160
diff changeset
   442
	debug=False
160
fed3f1d2c557 version 0.8 - added command line options for username and password. Revised the exception handling
William Roberts <williamr@symbian.org>
parents: 156
diff changeset
   443
	)
138
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   444
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   445
(options, args) = parser.parse_args()
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   446
if len(args) != 1:
153
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: 141
diff changeset
   447
	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: 141
diff changeset
   448
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: 141
diff changeset
   449
	parser.error("Unable to execute 7z command")
124
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   450
170
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 160
diff changeset
   451
opener = build_opener(options.debug)
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 160
diff changeset
   452
urllib2.install_opener(opener)
dd0383270574 version 0.9 - added --debug option to enable urllib2 HTTP debugging
William Roberts <williamr@symbian.org>
parents: 160
diff changeset
   453
174
5ebd70511e4c v0.11 - added quick_networking_check(), to confirm that we can talk to https://developer.symbian.org
William Roberts <williamr@symbian.org>
parents: 171
diff changeset
   454
quick_networking_check()
124
b60a149520e7 Added downloadkit.py - script to download and unpack a PDK
William Roberts <williamr@symbian.org>
parents:
diff changeset
   455
login(True)
138
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   456
downloadkit(args[0])
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   457
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   458
if options.dryrun:
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   459
	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: 128
diff changeset
   460
	for download in download_list:
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   461
		print download
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   462
	for command in unzip_list:
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   463
		print command
7b2d146ef884 version 0.3 - add command line processing, --dryrun option, --nosrc option
William Roberts <williamr@symbian.org>
parents: 128
diff changeset
   464