downloadkit/downloadkit.py
changeset 171 dd0383270574
parent 161 fed3f1d2c557
child 172 5f1dcce7f6d4
equal deleted inserted replaced
170:8d28eb999238 171:dd0383270574
    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 def build_opener(debug=False):
       
    35     # Create a HTTP and HTTPS handler with the appropriate debug
       
    36     # level.  We intentionally create a new one because the
       
    37     # OpenerDirector class in urllib2 is smart enough to replace
       
    38     # its internal versions with ours if we pass them into the
       
    39     # urllib2.build_opener method.  This is much easier than trying
       
    40     # to introspect into the OpenerDirector to find the existing
       
    41     # handlers.
       
    42     http_handler = urllib2.HTTPHandler(debuglevel=debug)
       
    43     https_handler = urllib2.HTTPSHandler(debuglevel=debug)
       
    44 
       
    45     # We want to process cookies, but only in memory so just use
       
    46     # a basic memory-only cookie jar instance
       
    47     cookie_jar = cookielib.LWPCookieJar()
       
    48     cookie_handler = urllib2.HTTPCookieProcessor(cookie_jar)
       
    49 
       
    50     handlers = [http_handler, https_handler, cookie_handler]
       
    51     opener = urllib2.build_opener(*handlers)
       
    52 
       
    53     # Save the cookie jar with the opener just in case it's needed
       
    54     # later on
       
    55     opener.cookie_jar = cookie_jar
       
    56 
       
    57     return opener
       
    58 
    34 urlopen = urllib2.urlopen
    59 urlopen = urllib2.urlopen
    35 Request = urllib2.Request
    60 Request = urllib2.Request
    36 cj = cookielib.LWPCookieJar()
       
    37 
       
    38 # Now we need to get our Cookie Jar
       
    39 # installed in the opener;
       
    40 # for fetching URLs
       
    41 opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
       
    42 urllib2.install_opener(opener)
       
    43 
    61 
    44 def login(prompt):
    62 def login(prompt):
    45 	global options
    63 	global options
    46 	loginurl = 'https://developer.symbian.org/main/user_profile/login.php'
    64 	loginurl = 'https://developer.symbian.org/main/user_profile/login.php'
    47 	
    65 	
   362 	# wait for the unzipping threads to complete
   380 	# wait for the unzipping threads to complete
   363 	complete_outstanding_unzips()  
   381 	complete_outstanding_unzips()  
   364 
   382 
   365 	return 1
   383 	return 1
   366 
   384 
   367 parser = OptionParser(version="%prog 0.8", usage="Usage: %prog [options] version")
   385 parser = OptionParser(version="%prog 0.9", usage="Usage: %prog [options] version")
   368 parser.add_option("-n", "--dryrun", action="store_true", dest="dryrun",
   386 parser.add_option("-n", "--dryrun", action="store_true", dest="dryrun",
   369 	help="print the files to be downloaded, the 7z commands, and the recommended deletions")
   387 	help="print the files to be downloaded, the 7z commands, and the recommended deletions")
   370 parser.add_option("--nosrc", action="store_true", dest="nosrc",
   388 parser.add_option("--nosrc", action="store_true", dest="nosrc",
   371 	help="Don't download any of the source code available directly from Mercurial")
   389 	help="Don't download any of the source code available directly from Mercurial")
   372 parser.add_option("--nounzip", action="store_true", dest="nounzip",
   390 parser.add_option("--nounzip", action="store_true", dest="nounzip",
   377 	help="Report download progress")
   395 	help="Report download progress")
   378 parser.add_option("-u", "--username", dest="username", metavar="USER",
   396 parser.add_option("-u", "--username", dest="username", metavar="USER",
   379 	help="login to website as USER")
   397 	help="login to website as USER")
   380 parser.add_option("-p", "--password", dest="password", metavar="PWD",
   398 parser.add_option("-p", "--password", dest="password", metavar="PWD",
   381 	help="specify the account password")
   399 	help="specify the account password")
       
   400 parser.add_option("--debug", action="store_true", dest="debug", 
       
   401 	help="debug HTML traffic (not recommended!)")
   382 parser.set_defaults(
   402 parser.set_defaults(
   383 	dryrun=False, 
   403 	dryrun=False, 
   384 	nosrc=False, 
   404 	nosrc=False, 
   385 	nounzip=False, 
   405 	nounzip=False, 
   386 	nodelete=False, 
   406 	nodelete=False, 
   387 	progress=False,
   407 	progress=False,
   388 	username='',
   408 	username='',
   389 	password=''
   409 	password='',
       
   410 	debug=False
   390 	)
   411 	)
   391 
   412 
   392 (options, args) = parser.parse_args()
   413 (options, args) = parser.parse_args()
   393 if len(args) != 1:
   414 if len(args) != 1:
   394 	parser.error("Must supply a PDK version, e.g. 3.0.f")
   415 	parser.error("Must supply a PDK version, e.g. 3.0.f")
   395 if not check_unzip_environment() :
   416 if not check_unzip_environment() :
   396 	parser.error("Unable to execute 7z command")
   417 	parser.error("Unable to execute 7z command")
       
   418 
       
   419 opener = build_opener(options.debug)
       
   420 urllib2.install_opener(opener)
   397 
   421 
   398 login(True)
   422 login(True)
   399 downloadkit(args[0])
   423 downloadkit(args[0])
   400 
   424 
   401 if options.dryrun:
   425 if options.dryrun: