downloadkit/downloadkit.py
changeset 154 77c25e4f2c6f
parent 142 1ef616833a37
child 155 b6c06a8333fb
equal deleted inserted replaced
153:1d6a95dcdf61 154:77c25e4f2c6f
   119 threadlist = []
   119 threadlist = []
   120 def schedule_unzip(filename, unziplevel, deletelevel):
   120 def schedule_unzip(filename, unziplevel, deletelevel):
   121 	global options
   121 	global options
   122 	if options.nounzip :
   122 	if options.nounzip :
   123 		return
   123 		return
       
   124 	if options.nodelete :
       
   125 		deletelevel = 0
   124 	if options.dryrun :
   126 	if options.dryrun :
   125 		global unzip_list
   127 		global unzip_list
   126 		if unziplevel > 0:
   128 		if unziplevel > 0:
   127 			unzip_list.append("7z x -y %s" % filename)
   129 			unzip_list.append("7z x -y %s" % filename)
   128 			if unziplevel > 1:
   130 			if unziplevel > 1:
   143 	if options.dryrun or options.nounzip:
   145 	if options.dryrun or options.nounzip:
   144 		return
   146 		return
   145 	print "Waiting for outstanding commands to finish..."
   147 	print "Waiting for outstanding commands to finish..."
   146 	for thread in threadlist:
   148 	for thread in threadlist:
   147 		thread.join()  
   149 		thread.join()  
   148 	
   150 
       
   151 def check_unzip_environment():
       
   152 	global options
       
   153 	if options.nounzip:
       
   154 		return True		# if we aren't unzipping, no need to have 7z installed
       
   155 	help = os.popen("7z -h")
       
   156 	for line in help.readlines():
       
   157 		if re.match('7-Zip', line) :
       
   158 			help.close()
       
   159 			return True
       
   160 	help.close()
       
   161 	return False
       
   162 
   149 def orderResults(x,y) :
   163 def orderResults(x,y) :
   150 	def ranking(name) :
   164 	def ranking(name) :
   151 		# 1st = release_metadata, build_BOM.zip (both small things!)
   165 		# 1st = release_metadata, build_BOM.zip (both small things!)
   152 		if re.match(r"(build_BOM|release_metadata)", name):
   166 		if re.match(r"(build_BOM|release_metadata)", name):
   153 			return 1000;
   167 			return 1000;
   196 				# our urllib2 cookies have gone awol - login again
   210 				# our urllib2 cookies have gone awol - login again
   197 				login(False)
   211 				login(False)
   198 				req = urllib2.Request(url, None, headers)
   212 				req = urllib2.Request(url, None, headers)
   199 				response = urllib2.urlopen(req)
   213 				response = urllib2.urlopen(req)
   200 				chunk = response.read(CHUNK)
   214 				chunk = response.read(CHUNK)
       
   215 				if chunk.find('<div id="sign_in_box">') != -1:
       
   216 					# still broken - give up on this one
       
   217 					print "*** ERROR trying to download %s" % (filename)
       
   218 					break;
   201 			if size == 0:
   219 			if size == 0:
   202 				filesize = int(response.info()['Content-Length'])  
   220 				info = response.info()
       
   221 				if 'Content-Length' in info:
       
   222 					filesize = int(info['Content-Length'])
       
   223 				else:
       
   224 					print "*** HTTP response did not contain 'Content-Length' when expected"
       
   225 					print info
       
   226 					break
   203 			fp.write(chunk)
   227 			fp.write(chunk)
   204 			size += len(chunk)
   228 			size += len(chunk)
   205 			now = time.time()
   229 			now = time.time()
   206 			if options.progress and now-last_time > 20:
   230 			if options.progress and now-last_time > 20:
   207 				rate = (size-last_size)/(now-last_time)
   231 				rate = (size-last_size)/(now-last_time)
   284 	# wait for the unzipping threads to complete
   308 	# wait for the unzipping threads to complete
   285 	complete_outstanding_unzips()  
   309 	complete_outstanding_unzips()  
   286 
   310 
   287 	return 1
   311 	return 1
   288 
   312 
   289 parser = OptionParser(version="%prog 0.5.1", usage="Usage: %prog [options] version")
   313 parser = OptionParser(version="%prog 0.6", usage="Usage: %prog [options] version")
   290 parser.add_option("-n", "--dryrun", action="store_true", dest="dryrun",
   314 parser.add_option("-n", "--dryrun", action="store_true", dest="dryrun",
   291 	help="print the files to be downloaded, the 7z commands, and the recommended deletions")
   315 	help="print the files to be downloaded, the 7z commands, and the recommended deletions")
   292 parser.add_option("--nosrc", action="store_true", dest="nosrc",
   316 parser.add_option("--nosrc", action="store_true", dest="nosrc",
   293 	help="Don't download any of the source code available directly from Mercurial")
   317 	help="Don't download any of the source code available directly from Mercurial")
   294 parser.add_option("--nounzip", action="store_true", dest="nounzip",
   318 parser.add_option("--nounzip", action="store_true", dest="nounzip",
   295 	help="Just download, don't unzip or delete any files")
   319 	help="Just download, don't unzip or delete any files")
       
   320 parser.add_option("--nodelete", action="store_true", dest="nodelete",
       
   321 	help="Do not delete files after unzipping")
   296 parser.add_option("--progress", action="store_true", dest="progress",
   322 parser.add_option("--progress", action="store_true", dest="progress",
   297 	help="Report download progress")
   323 	help="Report download progress")
   298 parser.set_defaults(dryrun=False, nosrc=False, nounzip=False, progress=False)
   324 parser.set_defaults(dryrun=False, nosrc=False, nounzip=False, nodelete=False, progress=False)
   299 
   325 
   300 (options, args) = parser.parse_args()
   326 (options, args) = parser.parse_args()
   301 if len(args) != 1:
   327 if len(args) != 1:
   302 	parser.error("Must supply a PDK version, e.g. 3.0.e")
   328 	parser.error("Must supply a PDK version, e.g. 3.0.f")
       
   329 if not check_unzip_environment() :
       
   330 	parser.error("Unable to execute 7z command")
   303 
   331 
   304 login(True)
   332 login(True)
   305 downloadkit(args[0])
   333 downloadkit(args[0])
   306 
   334 
   307 if options.dryrun:
   335 if options.dryrun: