#NEWLINE# def download( url , directory = None , bufsize = 8192 ) : #NEWLINE# #INDENT# '\n    Download the specified URL to a directory. This module properly handles\n    HTTP authentication for URLs like this one::\n\n        https://user:password@localhost:8080/foo/bar/baz.tgz\n\n    Note, however, that user/password authentication is only supported for\n    "http" and "https" URLs.\n\n    :Parameters:\n        url : str\n            the URL to download\n        directory : str\n            The directory to receive the downloaded file. If this parameter is\n            omitted, ``download()`` will create a temporary directory to\n            contain the file.\n        bufsize : int\n            buffer size to use when reading URL\n\n    :rtype:  tuple\n    :return: A (*download_directory*, *downloaded_file*) tuple\n    ' #NEWLINE# pieces = urlparse . urlparse ( url ) #NEWLINE# path = pieces . path #NEWLINE# if ( not directory ) : #NEWLINE# #INDENT# directory = tempfile . mkdtemp ( prefix = 'download' ) #NEWLINE# #UNINDENT# outputPath = os . path . join ( directory , os . path . basename ( path ) ) #NEWLINE# if ( pieces . scheme . startswith ( 'http' ) and pieces . username ) : #NEWLINE# #INDENT# ( user , password ) = ( pieces . username , pieces . password ) #NEWLINE# netloc = pieces . hostname #NEWLINE# if pieces . port : #NEWLINE# #INDENT# pieces . hostname += ( ':%d' % pieces . port ) #NEWLINE# #UNINDENT# newPieces = ( pieces . scheme , netloc , pieces . path , pieces . query , pieces . params , pieces . fragment ) #NEWLINE# url = urlparse . urlunparse ( newPieces ) #NEWLINE# log . debug ( ( 'Installing authorization handler for URL %s' % url ) ) #NEWLINE# passwordMgr = urllib2 . HTTPPasswordMgrWithDefaultRealm ( ) #NEWLINE# passwordMgr . add_password ( realm = None , uri = url , user = user , passwd = password ) #NEWLINE# authHandler = urllib2 . HTTPBasicAuthHandler ( passwordMgr ) #NEWLINE# opener = urllib2 . build_opener ( authHandler ) #NEWLINE# opener . open ( url ) #NEWLINE# urllib2 . install_opener ( opener ) #NEWLINE# #UNINDENT# log . debug ( ( 'Downloading "%s" to "%s"' % ( url , outputPath ) ) ) #NEWLINE# shutil . copyfileobj ( urllib2 . urlopen ( url ) , open ( outputPath , 'wb' ) , bufsize ) #NEWLINE# return ( outputPath , directory )