#NEWLINE# @ blueprint . route ( '/<job_id>/download' , methods = [ 'GET' , 'POST' ] , defaults = { #NEWLINE# #INDENT# 'extension' : 'tar.gz' , #NEWLINE# #UNINDENT# } ) #NEWLINE# @ blueprint . route ( '/<job_id>/download.<extension>' , methods = [ 'GET' , 'POST' ] ) #NEWLINE# def download( job_id , extension ) : #NEWLINE# #INDENT# '\n    Return a tarball of all files required to run the model\n    ' #NEWLINE# job = scheduler . get_job ( job_id ) #NEWLINE# if ( job is None ) : #NEWLINE# #INDENT# raise werkzeug . exceptions . NotFound ( 'Job not found' ) #NEWLINE# #UNINDENT# epoch = ( - 1 ) #NEWLINE# if ( 'epoch' in flask . request . args ) : #NEWLINE# #INDENT# epoch = float ( flask . request . args [ 'epoch' ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( 'snapshot_epoch' in flask . request . form ) : #NEWLINE# #INDENT# epoch = float ( flask . request . form [ 'snapshot_epoch' ] ) #NEWLINE# #UNINDENT# #UNINDENT# task = job . train_task ( ) #NEWLINE# snapshot_filename = None #NEWLINE# if ( ( epoch == ( - 1 ) ) and len ( task . snapshots ) ) : #NEWLINE# #INDENT# epoch = task . snapshots [ ( - 1 ) ] [ 1 ] #NEWLINE# snapshot_filename = task . snapshots [ ( - 1 ) ] [ 0 ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# for ( f , e ) in task . snapshots : #NEWLINE# #INDENT# if ( e == epoch ) : #NEWLINE# #INDENT# snapshot_filename = f #NEWLINE# break #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if ( not snapshot_filename ) : #NEWLINE# #INDENT# raise werkzeug . exceptions . BadRequest ( 'Invalid epoch' ) #NEWLINE# #UNINDENT# b = io . BytesIO ( ) #NEWLINE# if ( extension in [ 'tar' , 'tar.gz' , 'tgz' , 'tar.bz2' ] ) : #NEWLINE# #INDENT# mode = '' #NEWLINE# if ( extension in [ 'tar.gz' , 'tgz' ] ) : #NEWLINE# #INDENT# mode = 'gz' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( extension in [ 'tar.bz2' ] ) : #NEWLINE# #INDENT# mode = 'bz2' #NEWLINE# #UNINDENT# #UNINDENT# with tarfile . open ( fileobj = b , mode = ( 'w:%s' % mode ) ) as tf : #NEWLINE# #INDENT# for ( path , name ) in job . download_files ( epoch ) : #NEWLINE# #INDENT# tf . add ( path , arcname = name ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( extension in [ 'zip' ] ) : #NEWLINE# #INDENT# with zipfile . ZipFile ( b , 'w' ) as zf : #NEWLINE# #INDENT# for ( path , name ) in job . download_files ( epoch ) : #NEWLINE# #INDENT# zf . write ( path , arcname = name ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# raise werkzeug . exceptions . BadRequest ( 'Invalid extension' ) #NEWLINE# #UNINDENT# #UNINDENT# response = flask . make_response ( b . getvalue ( ) ) #NEWLINE# response . headers [ 'Content-Disposition' ] = ( 'attachment; filename=%s_epoch_%s.%s' % ( job . id ( ) , epoch , extension ) ) #NEWLINE# return response