#NEWLINE# @ feature ( 'download' ) #NEWLINE# def feature_download( tgen ) : #NEWLINE# #INDENT# '\n    Download a file.\n    ' #NEWLINE# work_dir = tgen . make_node ( tgen . worch . download_dir ) #NEWLINE# target_filename = tgen . worch . download_target #NEWLINE# if ( not target_filename ) : #NEWLINE# #INDENT# target_filename = os . path . basename ( tgen . worch . download_url ) #NEWLINE# #UNINDENT# target_node = work_dir . make_node ( target_filename ) #NEWLINE# tgen . step ( 'download_seturl' , rule = ( "echo '%s' > %s" % ( tgen . worch . download_url , tgen . worch . download_urlfile ) ) , update_outputs = True , target = tgen . worch . download_urlfile ) #NEWLINE# def dl_task( task ) : #NEWLINE# #INDENT# src = task . inputs [ 0 ] #NEWLINE# tgt = task . outputs [ 0 ] #NEWLINE# url = src . read ( ) . strip ( ) #NEWLINE# try : #NEWLINE# #INDENT# web = urlopen ( url ) #NEWLINE# tgt . write ( web . read ( ) , 'wb' ) #NEWLINE# #UNINDENT# except Exception : #NEWLINE# #INDENT# import traceback #NEWLINE# traceback . print_exc ( ) #NEWLINE# msg . error ( tgen . worch . format ( 'error downloading {download_url}' ) ) #NEWLINE# raise #NEWLINE# #UNINDENT# checksum = tgen . worch . download_checksum #NEWLINE# if ( not checksum ) : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# ( hasher_name , ref ) = checksum . split ( ':' ) #NEWLINE# import hashlib , os #NEWLINE# hasher = getattr ( hashlib , hasher_name ) ( ) #NEWLINE# hasher . update ( tgt . read ( 'rb' ) ) #NEWLINE# data = hasher . hexdigest ( ) #NEWLINE# if ( data != ref ) : #NEWLINE# #INDENT# msg . error ( tgen . worch . format ( ( 'invalid checksum:\nref: %s\nnew: %s' % ( ref , data ) ) ) ) #NEWLINE# try : #NEWLINE# #INDENT# os . remove ( tgt . abspath ( ) ) #NEWLINE# #UNINDENT# except IOError : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# return 1 #NEWLINE# #UNINDENT# return #NEWLINE# #UNINDENT# tgen . step ( 'download' , rule = dl_task , source = tgen . worch . download_urlfile , target = target_node , cwd = work_dir . abspath ( ) ) #NEWLINE# return