downloadkit/downloadkit.py
changeset 141 55dd69d60bbc
parent 140 9baccbcc5509
child 142 1ef616833a37
equal deleted inserted replaced
140:9baccbcc5509 141:55dd69d60bbc
    17 import os.path
    17 import os.path
    18 import cookielib
    18 import cookielib
    19 import sys
    19 import sys
    20 import getpass
    20 import getpass
    21 import re
    21 import re
       
    22 import time
    22 from BeautifulSoup import BeautifulSoup
    23 from BeautifulSoup import BeautifulSoup
    23 from optparse import OptionParser
    24 from optparse import OptionParser
    24 
    25 
    25 user_agent = 'downloadkit.py script'
    26 user_agent = 'downloadkit.py script'
    26 headers = { 'User-Agent' : user_agent }
    27 headers = { 'User-Agent' : user_agent }
   130 			unzip_list.append("# delete %s" % filename)
   131 			unzip_list.append("# delete %s" % filename)
   131 			if deletelevel > 1:
   132 			if deletelevel > 1:
   132 				unzip_list.append("# delete zip files recursively %d more times" % deletelevel-1)
   133 				unzip_list.append("# delete zip files recursively %d more times" % deletelevel-1)
   133 		return
   134 		return
   134 		
   135 		
   135 	unzipthread = unzipfile(filename, unziplevels, deletelevels)
   136 	unzipthread = unzipfile(filename, unziplevel, deletelevel)
   136 	global threadlist
   137 	global threadlist
   137 	threadlist.append(unzipthread)
   138 	threadlist.append(unzipthread)
   138 	unzipthread.start()
   139 	unzipthread.start()
   139 
   140 
   140 def complete_outstanding_unzips():
   141 def complete_outstanding_unzips():
   181 	req = urllib2.Request(url, None, headers)
   182 	req = urllib2.Request(url, None, headers)
   182 	
   183 	
   183 	try:
   184 	try:
   184 		response = urllib2.urlopen(req)
   185 		response = urllib2.urlopen(req)
   185 		CHUNK = 128 * 1024
   186 		CHUNK = 128 * 1024
   186 		first_chunk = True
   187 		size = 0
       
   188 		last_time = time.time()
       
   189 		last_size = size
   187 		fp = open(filename, 'wb')
   190 		fp = open(filename, 'wb')
   188 		while True:
   191 		while True:
   189 			chunk = response.read(CHUNK)
   192 			chunk = response.read(CHUNK)
   190 			if not chunk: break
   193 			if not chunk: break
   191 			if first_chunk and chunk.find('<div id="sign_in_box">') != -1:
   194 			if size == 0 and chunk.find('<div id="sign_in_box">') != -1:
   192 				# our urllib2 cookies have gone awol - login again
   195 				# our urllib2 cookies have gone awol - login again
   193 				login(False)
   196 				login(False)
   194 				req = urllib2.Request(url, None, headers)
   197 				req = urllib2.Request(url, None, headers)
   195 				response = urllib2.urlopen(req)
   198 				response = urllib2.urlopen(req)
   196 				chunk = response.read(CHUNK)	  
   199 				chunk = response.read(CHUNK)	  
   197 			fp.write(chunk)
   200 			fp.write(chunk)
   198 			first_chunk = False
   201 			size += len(chunk)
       
   202 			now = time.time()
       
   203 			if options.progress and now-last_time > 20:
       
   204 				print "- %d Kb (%d Kb/s)" % (size/1024, ((size-last_size)/1024/(now-last_time))+0.5)
       
   205 				last_time = now
       
   206 				last_size = size
   199 		fp.close()
   207 		fp.close()
   200 
   208 
   201 	#handle errors
   209 	#handle errors
   202 	except urllib2.HTTPError, e:
   210 	except urllib2.HTTPError, e:
   203 		print "HTTP Error:",e.code , downloadurl
   211 		print "HTTP Error:",e.code , downloadurl
   261 	# wait for the unzipping threads to complete
   269 	# wait for the unzipping threads to complete
   262 	complete_outstanding_unzips()  
   270 	complete_outstanding_unzips()  
   263 
   271 
   264 	return 1
   272 	return 1
   265 
   273 
   266 parser = OptionParser(usage="Usage: %prog [options] version", version="%prog 0.4")
   274 parser = OptionParser(version="%prog 0.5", usage="Usage: %prog [options] version")
   267 parser.add_option("-n", "--dryrun", action="store_true", dest="dryrun",
   275 parser.add_option("-n", "--dryrun", action="store_true", dest="dryrun",
   268 	help="print the files to be downloaded, the 7z commands, and the recommended deletions")
   276 	help="print the files to be downloaded, the 7z commands, and the recommended deletions")
   269 parser.add_option("--nosrc", action="store_true", dest="nosrc",
   277 parser.add_option("--nosrc", action="store_true", dest="nosrc",
   270 	help="Don't download any of the source code available directly from Mercurial")
   278 	help="Don't download any of the source code available directly from Mercurial")
   271 parser.add_option("--nounzip", action="store_true", dest="nounzip",
   279 parser.add_option("--nounzip", action="store_true", dest="nounzip",
   272 	help="Just download, don't unzip or delete any files")
   280 	help="Just download, don't unzip or delete any files")
   273 parser.set_defaults(dryrun=False, nosrc=False, nounzip=False)
   281 parser.add_option("--progress", action="store_true", dest="progress",
       
   282 	help="Report download progress")
       
   283 parser.set_defaults(dryrun=False, nosrc=False, nounzip=False, progress=False)
   274 
   284 
   275 (options, args) = parser.parse_args()
   285 (options, args) = parser.parse_args()
   276 if len(args) != 1:
   286 if len(args) != 1:
   277 	parser.error("Must supply a PDK version, e.g. 3.0.e")
   287 	parser.error("Must supply a PDK version, e.g. 3.0.e")
   278 
   288