<s> """<STR_LIT>""" <EOL> import select <EOL> import struct <EOL> from zlib import adler32 <EOL> from time import gmtime , strftime , time <EOL> from . import api <EOL> from . custom import Resource <EOL> from . custom . inputs import boolean <EOL> from . . exceptions import BUIserverException <EOL> from flask_login import current_user <EOL> from flask import Response , send_file , make_response , after_this_request <EOL> from werkzeug . datastructures import Headers <EOL> from werkzeug . exceptions import HTTPException <EOL> ns = api . namespace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ ns . route ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> endpoint = '<STR_LIT>' ) <EOL> class Restore ( Resource ) : <EOL> """<STR_LIT>""" <EOL> parser = api . parser ( ) <EOL> parser . add_argument ( '<STR_LIT>' , type = str , help = '<STR_LIT>' , location = '<STR_LIT>' , nullable = True ) <EOL> parser . add_argument ( '<STR_LIT>' , type = str , required = True , help = '<STR_LIT>' , location = '<STR_LIT>' , choices = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , default = '<STR_LIT>' , nullable = False ) <EOL> parser . add_argument ( '<STR_LIT>' , type = int , help = '<STR_LIT>' , default = <NUM_LIT:0> , location = '<STR_LIT>' , nullable = True ) <EOL> parser . add_argument ( '<STR_LIT:list>' , type = str , required = True , help = '<STR_LIT>' , location = '<STR_LIT>' , nullable = False ) <EOL> @ api . doc ( <EOL> params = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> responses = { <EOL> <NUM_LIT:200> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> } , <EOL> parser = parser <EOL> ) <EOL> def post ( self , server = None , name = None , backup = None ) : <EOL> """<STR_LIT>""" <EOL> args = self . parser . parse_args ( ) <EOL> l = args [ '<STR_LIT:list>' ] <EOL> s = args [ '<STR_LIT>' ] <EOL> f = args [ '<STR_LIT>' ] or '<STR_LIT>' <EOL> p = args [ '<STR_LIT>' ] <EOL> resp = None <EOL> if not l or not name or not backup : <EOL> api . abort ( <NUM_LIT> , '<STR_LIT>' ) <EOL> if ( api . bui . acl and <EOL> ( not api . bui . acl . is_client_allowed ( current_user . get_id ( ) , <EOL> name , <EOL> server ) and not <EOL> api . bui . acl . is_admin ( current_user . get_id ( ) ) ) ) : <EOL> api . abort ( <NUM_LIT> , '<STR_LIT>' ) <EOL> if server : <EOL> filename = '<STR_LIT>' % ( <EOL> backup , <EOL> name , <EOL> server , <EOL> strftime ( "<STR_LIT>" , gmtime ( ) ) , <EOL> f ) <EOL> else : <EOL> filename = '<STR_LIT>' % ( <EOL> backup , <EOL> name , <EOL> strftime ( "<STR_LIT>" , gmtime ( ) ) , <EOL> f ) <EOL> if not server : <EOL> archive , err = api . bui . cli . restore_files ( name , backup , l , s , f , p ) <EOL> if not archive : <EOL> if err : <EOL> return make_response ( err , <NUM_LIT> ) <EOL> api . abort ( <NUM_LIT> ) <EOL> try : <EOL> fh = open ( archive , '<STR_LIT:r>' ) <EOL> @ after_this_request <EOL> def remove_file ( response ) : <EOL> """<STR_LIT>""" <EOL> import os <EOL> os . remove ( archive ) <EOL> return response <EOL> resp = send_file ( fh , <EOL> as_attachment = True , <EOL> attachment_filename = filename , <EOL> mimetype = '<STR_LIT>' ) <EOL> resp . set_cookie ( '<STR_LIT>' , '<STR_LIT:true>' ) <EOL> except Exception as e : <EOL> api . bui . cli . _logger ( '<STR_LIT:error>' , str ( e ) ) <EOL> api . abort ( <NUM_LIT> , str ( e ) ) <EOL> else : <EOL> socket = None <EOL> try : <EOL> socket , length , err = api . bui . cli . restore_files ( name , <EOL> backup , <EOL> l , <EOL> s , <EOL> f , <EOL> p , <EOL> server ) <EOL> api . bui . cli . _logger ( '<STR_LIT>' , '<STR_LIT>' . format ( length , socket ) ) <EOL> if err : <EOL> api . bui . cli . _logger ( '<STR_LIT>' , '<STR_LIT>' . format ( err ) ) <EOL> socket . sendall ( struct . pack ( '<STR_LIT>' , <NUM_LIT:2> ) ) <EOL> socket . sendall ( b'<STR_LIT>' ) <EOL> socket . close ( ) <EOL> return make_response ( err , <NUM_LIT> ) <EOL> def stream_file ( sock , l ) : <EOL> """<STR_LIT>""" <EOL> bsize = <NUM_LIT> <EOL> received = <NUM_LIT:0> <EOL> if l < bsize : <EOL> bsize = l <EOL> while received < l : <EOL> buf = b'<STR_LIT>' <EOL> r , _ , _ = select . select ( [ sock ] , [ ] , [ ] , <NUM_LIT:5> ) <EOL> if not r : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> buf += sock . recv ( bsize ) <EOL> if not buf : <EOL> continue <EOL> received += len ( buf ) <EOL> api . bui . cli . _logger ( '<STR_LIT>' , '<STR_LIT>' . format ( received , l ) ) <EOL> yield buf <EOL> sock . sendall ( struct . pack ( '<STR_LIT>' , <NUM_LIT:2> ) ) <EOL> sock . sendall ( b'<STR_LIT>' ) <EOL> sock . close ( ) <EOL> headers = Headers ( ) <EOL> headers . add ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> filename = filename ) <EOL> headers [ '<STR_LIT>' ] = length <EOL> resp = Response ( stream_file ( socket , length ) , <EOL> mimetype = '<STR_LIT>' , <EOL> headers = headers , <EOL> direct_passthrough = True ) <EOL> resp . set_cookie ( '<STR_LIT>' , '<STR_LIT:true>' ) <EOL> resp . set_etag ( '<STR_LIT>' % ( <EOL> time ( ) , <EOL> length , <EOL> adler32 ( filename . encode ( '<STR_LIT:utf-8>' ) ) & <NUM_LIT> ) ) <EOL> except HTTPException as e : <EOL> raise e <EOL> except Exception as e : <EOL> api . bui . cli . _logger ( '<STR_LIT:error>' , str ( e ) ) <EOL> api . abort ( <NUM_LIT> , str ( e ) ) <EOL> return resp <EOL> @ ns . route ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> endpoint = '<STR_LIT>' ) <EOL> class ServerRestore ( Resource ) : <EOL> """<STR_LIT>""" <EOL> parser = api . parser ( ) <EOL> parser . add_argument ( '<STR_LIT>' , type = str , required = True , help = '<STR_LIT>' , location = '<STR_LIT>' , nullable = False ) <EOL> parser . add_argument ( '<STR_LIT>' , type = int , help = '<STR_LIT>' , default = <NUM_LIT:0> , location = '<STR_LIT>' , nullable = True ) <EOL> parser . add_argument ( '<STR_LIT>' , type = str , help = '<STR_LIT>' , location = '<STR_LIT>' , nullable = True ) <EOL> parser . add_argument ( '<STR_LIT>' , type = boolean , help = '<STR_LIT>' , default = False , location = '<STR_LIT>' , nullable = True ) <EOL> parser . add_argument ( '<STR_LIT>' , type = str , help = '<STR_LIT>' , location = '<STR_LIT>' , nullable = True ) <EOL> @ api . doc ( <EOL> params = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> responses = { <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> } , <EOL> parser = parser <EOL> ) <EOL> def put ( self , server = None , name = None , backup = None ) : <EOL> """<STR_LIT>""" <EOL> args = self . parser . parse_args ( ) <EOL> l = args [ '<STR_LIT>' ] <EOL> s = args [ '<STR_LIT>' ] <EOL> p = args [ '<STR_LIT>' ] <EOL> f = args [ '<STR_LIT>' ] <EOL> to = args [ '<STR_LIT>' ] <EOL> j = [ ] <EOL> if not l or not name or not backup : <EOL> api . abort ( <NUM_LIT> , '<STR_LIT>' ) <EOL> if ( api . bui . acl and <EOL> ( not api . bui . acl . is_client_allowed ( current_user . get_id ( ) , <EOL> name , <EOL> server ) and not <EOL> api . bui . acl . is_admin ( current_user . get_id ( ) ) ) ) : <EOL> api . abort ( <NUM_LIT> , '<STR_LIT>' ) <EOL> try : <EOL> j = api . bui . cli . server_restore ( name , backup , l , s , f , p , to , server ) <EOL> return { '<STR_LIT>' : j } , <NUM_LIT> <EOL> except BUIserverException as e : <EOL> api . abort ( <NUM_LIT> , str ( e ) ) </s>
