<s> from __future__ import print_function <EOL> import os <EOL> try : <EOL> import xmlrpclib <EOL> except ImportError : <EOL> import xmlrpc . client as xmlrpclib <EOL> from functools import partial <EOL> import requests <EOL> import hashlib <EOL> import warnings <EOL> import stdeb <EOL> from stdeb . transport import RequestsTransport <EOL> myprint = print <EOL> USER_AGENT = '<STR_LIT>' % stdeb . __version__ <EOL> def find_tar_gz ( package_name , pypi_url = '<STR_LIT>' , <EOL> verbose = <NUM_LIT:0> , release = None ) : <EOL> transport = RequestsTransport ( ) <EOL> transport . user_agent = USER_AGENT <EOL> if pypi_url . startswith ( '<STR_LIT>' ) : <EOL> transport . use_https = True <EOL> pypi = xmlrpclib . ServerProxy ( pypi_url , transport = transport ) <EOL> download_url = None <EOL> expected_md5_digest = None <EOL> if verbose >= <NUM_LIT:2> : <EOL> myprint ( '<STR_LIT>' % ( pypi_url , <EOL> package_name ) ) <EOL> show_hidden = True <EOL> all_releases = pypi . package_releases ( package_name , show_hidden ) <EOL> if release is not None : <EOL> if verbose >= <NUM_LIT:2> : <EOL> myprint ( '<STR_LIT>' % ( '<STR_LIT:U+002CU+0020>' . join ( all_releases ) , ) ) <EOL> if release not in all_releases : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( release , all_releases ) ) <EOL> version = release <EOL> else : <EOL> default_releases = pypi . package_releases ( package_name ) <EOL> if len ( default_releases ) != <NUM_LIT:1> : <EOL> raise RuntimeError ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( <EOL> default_releases , all_releases ) ) <EOL> default_release = default_releases [ <NUM_LIT:0> ] <EOL> if verbose >= <NUM_LIT:2> : <EOL> myprint ( '<STR_LIT>' % ( '<STR_LIT:U+002CU+0020>' . join ( default_releases ) , ) ) <EOL> version = default_release <EOL> urls = pypi . release_urls ( package_name , version ) <EOL> for url in urls : <EOL> if url [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> assert url [ '<STR_LIT>' ] == '<STR_LIT:source>' , '<STR_LIT>' <EOL> if url [ '<STR_LIT:url>' ] . endswith ( '<STR_LIT>' ) : <EOL> download_url = url [ '<STR_LIT:url>' ] <EOL> if '<STR_LIT>' in url : <EOL> expected_md5_digest = url [ '<STR_LIT>' ] <EOL> break <EOL> if download_url is None : <EOL> result = pypi . release_data ( package_name , version ) <EOL> if result [ '<STR_LIT>' ] != '<STR_LIT>' : <EOL> download_url = result [ '<STR_LIT>' ] <EOL> urls = pypi . release_urls ( result [ '<STR_LIT:name>' ] , result [ '<STR_LIT:version>' ] ) <EOL> if download_url is None : <EOL> raise ValueError ( '<STR_LIT>' % package_name ) <EOL> return download_url , expected_md5_digest <EOL> def md5sum ( filename ) : <EOL> with open ( filename , mode = '<STR_LIT:rb>' ) as f : <EOL> d = hashlib . md5 ( ) <EOL> for buf in iter ( partial ( f . read , <NUM_LIT> ) , b'<STR_LIT>' ) : <EOL> d . update ( buf ) <EOL> return d . hexdigest ( ) <EOL> def get_source_tarball ( package_name , verbose = <NUM_LIT:0> , allow_unsafe_download = False , <EOL> release = None ) : <EOL> download_url , expected_md5_digest = find_tar_gz ( package_name , <EOL> verbose = verbose , <EOL> release = release ) <EOL> if not download_url . startswith ( '<STR_LIT>' ) : <EOL> if allow_unsafe_download : <EOL> warnings . warn ( '<STR_LIT>' % download_url ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' % download_url ) <EOL> fname = download_url . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] <EOL> if expected_md5_digest is not None : <EOL> if os . path . exists ( fname ) : <EOL> actual_md5_digest = md5sum ( fname ) <EOL> if actual_md5_digest == expected_md5_digest : <EOL> if verbose >= <NUM_LIT:1> : <EOL> myprint ( '<STR_LIT>' % download_url ) <EOL> myprint ( '<STR_LIT>' % fname ) <EOL> return fname <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' % fname ) <EOL> if verbose >= <NUM_LIT:1> : <EOL> myprint ( '<STR_LIT>' % download_url ) <EOL> headers = { '<STR_LIT>' : USER_AGENT } <EOL> r = requests . get ( download_url , headers = headers ) <EOL> r . raise_for_status ( ) <EOL> package_tar_gz = r . content <EOL> if verbose >= <NUM_LIT:1> : <EOL> myprint ( '<STR_LIT>' % ( len ( package_tar_gz ) , ) ) <EOL> if expected_md5_digest is not None : <EOL> m = hashlib . md5 ( ) <EOL> m . update ( package_tar_gz ) <EOL> actual_md5_digest = m . hexdigest ( ) <EOL> if verbose >= <NUM_LIT:2> : <EOL> myprint ( '<STR_LIT>' % ( actual_md5_digest , <EOL> expected_md5_digest ) ) <EOL> if actual_md5_digest != expected_md5_digest : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> else : <EOL> warnings . warn ( '<STR_LIT>' ) <EOL> fd = open ( fname , mode = '<STR_LIT:wb>' ) <EOL> fd . write ( package_tar_gz ) <EOL> fd . close ( ) <EOL> return fname </s>
