<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> from hashlib import sha256 <EOL> from os . path import join <EOL> from pipes import quote <EOL> from shutil import rmtree <EOL> from subprocess import check_output <EOL> from sys import exit <EOL> from tempfile import mkdtemp <EOL> try : <EOL> from urllib2 import build_opener , HTTPHandler , HTTPSHandler <EOL> except ImportError : <EOL> from urllib . request import build_opener , HTTPHandler , HTTPSHandler <EOL> try : <EOL> from urlparse import urlparse <EOL> except ImportError : <EOL> from urllib . parse import urlparse <EOL> PACKAGES = [ <EOL> ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> ] <EOL> class HashError ( Exception ) : <EOL> def __str__ ( self ) : <EOL> url , path , actual , expected = self . args <EOL> return ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( ** locals ( ) ) ) <EOL> def hashed_download ( url , temp , digest ) : <EOL> """<STR_LIT>""" <EOL> def opener ( ) : <EOL> opener = build_opener ( HTTPSHandler ( ) ) <EOL> for handler in opener . handlers : <EOL> if isinstance ( handler , HTTPHandler ) : <EOL> opener . handlers . remove ( handler ) <EOL> return opener <EOL> def read_chunks ( response , chunk_size ) : <EOL> while True : <EOL> chunk = response . read ( chunk_size ) <EOL> if not chunk : <EOL> break <EOL> yield chunk <EOL> response = opener ( ) . open ( url ) <EOL> path = join ( temp , urlparse ( url ) . path . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] ) <EOL> actual_hash = sha256 ( ) <EOL> with open ( path , '<STR_LIT:wb>' ) as file : <EOL> for chunk in read_chunks ( response , <NUM_LIT> ) : <EOL> file . write ( chunk ) <EOL> actual_hash . update ( chunk ) <EOL> actual_digest = actual_hash . hexdigest ( ) <EOL> if actual_digest != digest : <EOL> raise HashError ( url , path , actual_digest , digest ) <EOL> return path <EOL> def main ( ) : <EOL> temp = mkdtemp ( prefix = '<STR_LIT>' ) <EOL> try : <EOL> downloads = [ hashed_download ( url , temp , digest ) <EOL> for url , digest in PACKAGES ] <EOL> check_output ( '<STR_LIT>' + <EOL> '<STR_LIT:U+0020>' . join ( quote ( d ) for d in downloads ) , <EOL> shell = True ) <EOL> except HashError as exc : <EOL> print ( exc ) <EOL> except Exception : <EOL> rmtree ( temp ) <EOL> raise <EOL> else : <EOL> rmtree ( temp ) <EOL> return <NUM_LIT:0> <EOL> return <NUM_LIT:1> <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> exit ( main ( ) ) </s>
