downloadkit/downloadkit.py
changeset 161 fed3f1d2c557
parent 157 0df3a90af030
child 171 dd0383270574
equal deleted inserted replaced
160:20b456b3eebf 161:fed3f1d2c557
    29 headers = { 'User-Agent' : user_agent }
    29 headers = { 'User-Agent' : user_agent }
    30 top_level_url = "http://developer.symbian.org"
    30 top_level_url = "http://developer.symbian.org"
    31 download_list = []
    31 download_list = []
    32 unzip_list = []
    32 unzip_list = []
    33 
    33 
    34 username = ''
       
    35 password = ''
       
    36 
       
    37 COOKIEFILE = 'cookies.lwp'
       
    38 # the path and filename to save your cookies in
       
    39 
       
    40 # importing cookielib worked
       
    41 urlopen = urllib2.urlopen
    34 urlopen = urllib2.urlopen
    42 Request = urllib2.Request
    35 Request = urllib2.Request
    43 cj = cookielib.LWPCookieJar()
    36 cj = cookielib.LWPCookieJar()
    44 
    37 
    45 # This is a subclass of FileCookieJar
       
    46 # that has useful load and save methods
       
    47 if os.path.isfile(COOKIEFILE):
       
    48 	cj.load(COOKIEFILE)
       
    49 	
       
    50 # Now we need to get our Cookie Jar
    38 # Now we need to get our Cookie Jar
    51 # installed in the opener;
    39 # installed in the opener;
    52 # for fetching URLs
    40 # for fetching URLs
    53 opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    41 opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    54 urllib2.install_opener(opener)
    42 urllib2.install_opener(opener)
    55 
    43 
    56 def login(prompt):
    44 def login(prompt):
    57 	global username
    45 	global options
    58 	global password
       
    59 	loginurl = 'https://developer.symbian.org/main/user_profile/login.php'
    46 	loginurl = 'https://developer.symbian.org/main/user_profile/login.php'
    60 	
    47 	
    61 	if prompt:
    48 	if prompt:
    62 		print >> sys.stderr, 'username: ',
    49 		if options.username == '':
    63 		username=sys.stdin.readline().strip()
    50 			print >> sys.stderr, 'username: ',
    64 		password=getpass.getpass()
    51 			options.username=sys.stdin.readline().strip()
    65 	
    52 		if options.password == '':
    66 	values = {'username' : username,
    53 			options.password=getpass.getpass()
    67 	          'password' : password,
    54 	
       
    55 	values = {'username' : options.username,
       
    56 	          'password' : options.password,
    68 	          'submit': 'Login'}
    57 	          'submit': 'Login'}
    69 	          
    58 	          
    70 	headers = { 'User-Agent' : user_agent }
    59 	headers = { 'User-Agent' : user_agent }
    71 	
    60 	
    72 	
    61 	
    77 	doc=response.read()      
    66 	doc=response.read()      
    78 
    67 
    79 	if doc.find('Please try again') != -1:
    68 	if doc.find('Please try again') != -1:
    80 		print >> sys.stderr, 'Login failed'
    69 		print >> sys.stderr, 'Login failed'
    81 		return False
    70 		return False
    82 	
       
    83 	cj.save(COOKIEFILE) 
       
    84 	return True
    71 	return True
    85 
    72 
    86 from threading import Thread
    73 from threading import Thread
    87 
    74 
    88 class unzipfile(Thread):
    75 class unzipfile(Thread):
   262 		else:
   249 		else:
   263 			print "*** HTTP response did not contain 'Content-Length' when expected"
   250 			print "*** HTTP response did not contain 'Content-Length' when expected"
   264 			print info
   251 			print info
   265 			return False
   252 			return False
   266 
   253 
   267 	except urllib2.HTTPError, e:
       
   268 		print "HTTP Error:",e.code , url
       
   269 		return False
       
   270 	except urllib2.URLError, e:
   254 	except urllib2.URLError, e:
   271 		print "URL Error:",e.reason , url
   255 		print '- ERROR: Failed to start downloading ' + filename
       
   256 		if hasattr(e, 'reason'):
       
   257 			print 'Reason: ', e.reason
       
   258 		elif hasattr(e, 'code'):
       
   259 			print 'Error code: ', e.code
   272 		return False
   260 		return False
   273 
   261 
   274 	# we are now up and running, and chunk contains the start of the download
   262 	# we are now up and running, and chunk contains the start of the download
   275 	
   263 	
   276 	try:
   264 	try:
   301 		if options.progress:
   289 		if options.progress:
   302 			now = time.time()
   290 			now = time.time()
   303 			print "- Completed %s - %d Kb in %d seconds" % (filename, (filesize/1024)+0.5, now-start_time)
   291 			print "- Completed %s - %d Kb in %d seconds" % (filename, (filesize/1024)+0.5, now-start_time)
   304 
   292 
   305 	#handle errors
   293 	#handle errors
   306 	except urllib2.HTTPError, e:
       
   307 		print "HTTP Error:",e.code , url
       
   308 		return False
       
   309 	except urllib2.URLError, e:
   294 	except urllib2.URLError, e:
   310 		print "URL Error:",e.reason , url
   295 		print '- ERROR: Failed while downloading ' + filename
       
   296 		if hasattr(e, 'reason'):
       
   297 			print 'Reason: ', e.reason
       
   298 		elif hasattr(e, 'code'):
       
   299 			print 'Error code: ', e.code
   311 		return False
   300 		return False
   312 
   301 
   313 	if filename in checksums:
   302 	if filename in checksums:
   314 		download_checksum = md5.hexdigest().upper()
   303 		download_checksum = md5.hexdigest().upper()
   315 		if download_checksum != checksums[filename]:
   304 		if download_checksum != checksums[filename]:
   373 	# wait for the unzipping threads to complete
   362 	# wait for the unzipping threads to complete
   374 	complete_outstanding_unzips()  
   363 	complete_outstanding_unzips()  
   375 
   364 
   376 	return 1
   365 	return 1
   377 
   366 
   378 parser = OptionParser(version="%prog 0.7", usage="Usage: %prog [options] version")
   367 parser = OptionParser(version="%prog 0.8", usage="Usage: %prog [options] version")
   379 parser.add_option("-n", "--dryrun", action="store_true", dest="dryrun",
   368 parser.add_option("-n", "--dryrun", action="store_true", dest="dryrun",
   380 	help="print the files to be downloaded, the 7z commands, and the recommended deletions")
   369 	help="print the files to be downloaded, the 7z commands, and the recommended deletions")
   381 parser.add_option("--nosrc", action="store_true", dest="nosrc",
   370 parser.add_option("--nosrc", action="store_true", dest="nosrc",
   382 	help="Don't download any of the source code available directly from Mercurial")
   371 	help="Don't download any of the source code available directly from Mercurial")
   383 parser.add_option("--nounzip", action="store_true", dest="nounzip",
   372 parser.add_option("--nounzip", action="store_true", dest="nounzip",
   384 	help="Just download, don't unzip or delete any files")
   373 	help="Just download, don't unzip or delete any files")
   385 parser.add_option("--nodelete", action="store_true", dest="nodelete",
   374 parser.add_option("--nodelete", action="store_true", dest="nodelete",
   386 	help="Do not delete files after unzipping")
   375 	help="Do not delete files after unzipping")
   387 parser.add_option("--progress", action="store_true", dest="progress",
   376 parser.add_option("--progress", action="store_true", dest="progress",
   388 	help="Report download progress")
   377 	help="Report download progress")
   389 parser.set_defaults(dryrun=False, nosrc=False, nounzip=False, nodelete=False, progress=False)
   378 parser.add_option("-u", "--username", dest="username", metavar="USER",
       
   379 	help="login to website as USER")
       
   380 parser.add_option("-p", "--password", dest="password", metavar="PWD",
       
   381 	help="specify the account password")
       
   382 parser.set_defaults(
       
   383 	dryrun=False, 
       
   384 	nosrc=False, 
       
   385 	nounzip=False, 
       
   386 	nodelete=False, 
       
   387 	progress=False,
       
   388 	username='',
       
   389 	password=''
       
   390 	)
   390 
   391 
   391 (options, args) = parser.parse_args()
   392 (options, args) = parser.parse_args()
   392 if len(args) != 1:
   393 if len(args) != 1:
   393 	parser.error("Must supply a PDK version, e.g. 3.0.f")
   394 	parser.error("Must supply a PDK version, e.g. 3.0.f")
   394 if not check_unzip_environment() :
   395 if not check_unzip_environment() :