#NEWLINE# @ contextlib . contextmanager #NEWLINE# def get_backend( backend_uri = None ) : #NEWLINE# #INDENT# tmp_dir = None #NEWLINE# if ( not backend_uri ) : #NEWLINE# #INDENT# if ( len ( sys . argv ) > 1 ) : #NEWLINE# #INDENT# backend_uri = str ( sys . argv [ 1 ] ) #NEWLINE# #UNINDENT# if ( not backend_uri ) : #NEWLINE# #INDENT# tmp_dir = tempfile . mkdtemp ( ) #NEWLINE# backend_uri = ( 'file:///%s' % tmp_dir ) #NEWLINE# #UNINDENT# #UNINDENT# try : #NEWLINE# #INDENT# backend = backends . fetch ( _make_conf ( backend_uri ) ) #NEWLINE# #UNINDENT# except exceptions . NotFound as e : #NEWLINE# #INDENT# if ( not tmp_dir ) : #NEWLINE# #INDENT# tmp_dir = tempfile . mkdtemp ( ) #NEWLINE# backend_uri = ( 'file:///%s' % tmp_dir ) #NEWLINE# LOG . exception ( 'Falling back to file backend using temporary directory located at: %s' , tmp_dir ) #NEWLINE# backend = backends . fetch ( _make_conf ( backend_uri ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise e #NEWLINE# #UNINDENT# #UNINDENT# try : #NEWLINE# #INDENT# with contextlib . closing ( backend . get_connection ( ) ) as conn : #NEWLINE# #INDENT# conn . upgrade ( ) #NEWLINE# #UNINDENT# ( yield backend ) #NEWLINE# #UNINDENT# finally : #NEWLINE# #INDENT# if tmp_dir : #NEWLINE# #INDENT# rm_path ( tmp_dir )