<s> """<STR_LIT>""" <EOL> """<STR_LIT>""" <EOL> import six <EOL> import json <EOL> import os <EOL> import time <EOL> from swiftly . cli . command import CLICommand , ReturnCode <EOL> from swiftly . concurrency import Concurrency <EOL> from swiftly . dencrypt import AES256CBC , aes_encrypt <EOL> from swiftly . filelikeiter import FileLikeIter <EOL> def cli_put_directory_structure ( context , path ) : <EOL> """<STR_LIT>""" <EOL> if not context . input_ : <EOL> raise ReturnCode ( <EOL> '<STR_LIT>' ) <EOL> if not os . path . isdir ( context . input_ ) : <EOL> raise ReturnCode ( <EOL> '<STR_LIT>' % context . input_ ) <EOL> if not path : <EOL> raise ReturnCode ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT:name>' ) <EOL> new_context = context . copy ( ) <EOL> new_context . input_ = None <EOL> container = path . split ( '<STR_LIT:/>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> cli_put_container ( new_context , container ) <EOL> ilen = len ( context . input_ ) <EOL> if not context . input_ . endswith ( os . sep ) : <EOL> ilen += <NUM_LIT:1> <EOL> conc = Concurrency ( context . concurrency ) <EOL> for ( dirpath , dirnames , filenames ) in os . walk ( context . input_ ) : <EOL> if not dirnames and not filenames : <EOL> new_context = context . copy ( ) <EOL> new_context . headers = dict ( context . headers ) <EOL> new_context . headers [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> new_context . headers [ '<STR_LIT>' ] = '<STR_LIT>' % os . path . getmtime ( context . input_ ) <EOL> new_context . input_ = None <EOL> new_context . empty = True <EOL> new_path = path <EOL> if path [ - <NUM_LIT:1> ] != '<STR_LIT:/>' : <EOL> new_path += '<STR_LIT:/>' <EOL> new_path += dirpath [ ilen : ] <EOL> for ( exc_type , exc_value , exc_tb , result ) in six . itervalues ( conc . get_results ( ) ) : <EOL> if exc_value : <EOL> conc . join ( ) <EOL> raise exc_value <EOL> conc . spawn ( new_path , cli_put_object , new_context , new_path ) <EOL> else : <EOL> for fname in filenames : <EOL> new_context = context . copy ( ) <EOL> new_context . input_ = os . path . join ( dirpath , fname ) <EOL> new_path = path <EOL> if path [ - <NUM_LIT:1> ] != '<STR_LIT:/>' : <EOL> new_path += '<STR_LIT:/>' <EOL> if dirpath [ ilen : ] : <EOL> new_path += dirpath [ ilen : ] + '<STR_LIT:/>' <EOL> new_path += fname <EOL> for ( exc_type , exc_value , exc_tb , result ) in six . itervalues ( conc . get_results ( ) ) : <EOL> if exc_value : <EOL> conc . join ( ) <EOL> raise exc_value <EOL> conc . spawn ( new_path , cli_put_object , new_context , new_path ) <EOL> conc . join ( ) <EOL> for ( exc_type , exc_value , exc_tb , result ) in six . itervalues ( conc . get_results ( ) ) : <EOL> if exc_value : <EOL> raise exc_value <EOL> def cli_put_account ( context ) : <EOL> """<STR_LIT>""" <EOL> body = None <EOL> if context . input_ : <EOL> if context . input_ == '<STR_LIT:->' : <EOL> body = context . io_manager . get_stdin ( ) <EOL> else : <EOL> body = open ( context . input_ , '<STR_LIT:rb>' ) <EOL> with context . client_manager . with_client ( ) as client : <EOL> status , reason , headers , contents = client . put_account ( <EOL> headers = context . headers , query = context . query , cdn = context . cdn , <EOL> body = body ) <EOL> if hasattr ( contents , '<STR_LIT>' ) : <EOL> contents . read ( ) <EOL> if status // <NUM_LIT:100> != <NUM_LIT:2> : <EOL> raise ReturnCode ( '<STR_LIT>' % ( status , reason ) ) <EOL> def cli_put_container ( context , path ) : <EOL> """<STR_LIT>""" <EOL> path = path . rstrip ( '<STR_LIT:/>' ) <EOL> if '<STR_LIT:/>' in path : <EOL> raise ReturnCode ( '<STR_LIT>' % path ) <EOL> body = None <EOL> if context . input_ : <EOL> if context . input_ == '<STR_LIT:->' : <EOL> body = context . io_manager . get_stdin ( ) <EOL> else : <EOL> body = open ( context . input_ , '<STR_LIT:rb>' ) <EOL> with context . client_manager . with_client ( ) as client : <EOL> status , reason , headers , contents = client . put_container ( <EOL> path , headers = context . headers , query = context . query , <EOL> cdn = context . cdn , body = body ) <EOL> if hasattr ( contents , '<STR_LIT>' ) : <EOL> contents . read ( ) <EOL> if status // <NUM_LIT:100> != <NUM_LIT:2> : <EOL> raise ReturnCode ( <EOL> '<STR_LIT>' % ( path , status , reason ) ) <EOL> def cli_put_object ( context , path ) : <EOL> """<STR_LIT>""" <EOL> if context . different and context . encrypt : <EOL> raise ReturnCode ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> put_headers = dict ( context . headers ) <EOL> if context . empty : <EOL> body = '<STR_LIT>' <EOL> put_headers [ '<STR_LIT>' ] = '<STR_LIT:0>' <EOL> elif not context . input_ or context . input_ == '<STR_LIT:->' : <EOL> stdin = context . io_manager . get_stdin ( ) <EOL> if context . stdin_segmentation : <EOL> def reader ( ) : <EOL> while True : <EOL> chunk = stdin . read ( <NUM_LIT> ) <EOL> if chunk : <EOL> yield chunk <EOL> else : <EOL> return <EOL> segment_body = FileLikeIter ( reader ( ) , context . segment_size ) <EOL> prefix = _create_container ( context , path , time . time ( ) , <NUM_LIT:0> ) <EOL> new_context = context . copy ( ) <EOL> new_context . stdin_segmentation = False <EOL> new_context . stdin = segment_body <EOL> new_context . headers = dict ( context . headers ) <EOL> segment_n = <NUM_LIT:0> <EOL> path2info = { } <EOL> while not segment_body . is_empty ( ) : <EOL> segment_path = _get_segment_path ( prefix , segment_n ) <EOL> etag = cli_put_object ( new_context , segment_path ) <EOL> size = segment_body . limit - segment_body . left <EOL> path2info [ segment_path ] = ( size , etag ) <EOL> segment_body . reset_limit ( ) <EOL> segment_n += <NUM_LIT:1> <EOL> body = _get_manifest_body ( context , prefix , path2info , put_headers ) <EOL> else : <EOL> if hasattr ( context , '<STR_LIT>' ) : <EOL> body = context . stdin <EOL> else : <EOL> body = stdin <EOL> elif context . seek is not None : <EOL> if context . encrypt : <EOL> raise ReturnCode ( <EOL> '<STR_LIT>' % <EOL> path ) <EOL> body = open ( context . input_ , '<STR_LIT:rb>' ) <EOL> body . seek ( context . seek ) <EOL> else : <EOL> l_mtime = os . path . getmtime ( context . input_ ) <EOL> l_size = os . path . getsize ( context . input_ ) <EOL> put_headers [ '<STR_LIT>' ] = str ( l_size ) <EOL> if context . newer or context . different : <EOL> r_mtime = None <EOL> r_size = None <EOL> with context . client_manager . with_client ( ) as client : <EOL> status , reason , headers , contents = client . head_object ( <EOL> * path . split ( '<STR_LIT:/>' , <NUM_LIT:1> ) , headers = context . headers , <EOL> query = context . query , cdn = context . cdn ) <EOL> if hasattr ( contents , '<STR_LIT>' ) : <EOL> contents . read ( ) <EOL> if status // <NUM_LIT:100> == <NUM_LIT:2> : <EOL> r_mtime = headers . get ( '<STR_LIT>' ) <EOL> if r_mtime : <EOL> try : <EOL> r_mtime = float ( r_mtime ) <EOL> except ValueError : <EOL> r_mtime = None <EOL> r_size = headers . get ( '<STR_LIT>' ) <EOL> if r_size : <EOL> try : <EOL> r_size = int ( r_size ) <EOL> except ValueError : <EOL> r_size = None <EOL> elif status != <NUM_LIT> : <EOL> raise ReturnCode ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( path , status , reason ) ) <EOL> if context . newer and r_mtime is not None or l_mtime <= r_mtime : <EOL> return <EOL> if context . different and r_mtime is not None and l_mtime == r_mtime and r_size is not None and l_size == r_size : <EOL> return <EOL> put_headers [ '<STR_LIT>' ] = '<STR_LIT>' % l_mtime <EOL> size = os . path . getsize ( context . input_ ) <EOL> if size > context . segment_size : <EOL> if context . encrypt : <EOL> raise ReturnCode ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % path ) <EOL> prefix = _create_container ( context , path , l_mtime , size ) <EOL> conc = Concurrency ( context . concurrency ) <EOL> start = <NUM_LIT:0> <EOL> segment = <NUM_LIT:0> <EOL> path2info = { } <EOL> while start < size : <EOL> new_context = context . copy ( ) <EOL> new_context . headers = dict ( context . headers ) <EOL> new_context . headers [ '<STR_LIT>' ] = str ( min ( <EOL> size - start , context . segment_size ) ) <EOL> new_context . seek = start <EOL> new_path = _get_segment_path ( prefix , segment ) <EOL> for ( ident , ( exc_type , exc_value , exc_tb , result ) ) in six . iteritems ( conc . get_results ( ) ) : <EOL> if exc_value : <EOL> conc . join ( ) <EOL> raise exc_value <EOL> path2info [ ident ] = result <EOL> conc . spawn ( <EOL> new_path , cli_put_object , new_context , new_path ) <EOL> segment += <NUM_LIT:1> <EOL> start += context . segment_size <EOL> conc . join ( ) <EOL> for ( ident , ( exc_type , exc_value , exc_tb , result ) ) in six . iteritems ( conc . get_results ( ) ) : <EOL> if exc_value : <EOL> raise exc_value <EOL> path2info [ ident ] = result <EOL> body = _get_manifest_body ( context , prefix , path2info , put_headers ) <EOL> else : <EOL> body = open ( context . input_ , '<STR_LIT:rb>' ) <EOL> with context . client_manager . with_client ( ) as client : <EOL> if context . encrypt : <EOL> content_length = put_headers . get ( '<STR_LIT>' ) <EOL> if content_length : <EOL> content_length = int ( content_length ) <EOL> if hasattr ( body , '<STR_LIT>' ) : <EOL> body = FileLikeIter ( aes_encrypt ( <EOL> context . encrypt , body , preamble = AES256CBC , <EOL> chunk_size = getattr ( client , '<STR_LIT>' , <NUM_LIT> ) , <EOL> content_length = content_length ) ) <EOL> else : <EOL> body = FileLikeIter ( aes_encrypt ( <EOL> context . encrypt , FileLikeIter ( [ body ] ) , preamble = AES256CBC , <EOL> chunk_size = getattr ( client , '<STR_LIT>' , <NUM_LIT> ) , <EOL> content_length = content_length ) ) <EOL> if '<STR_LIT>' in put_headers : <EOL> del put_headers [ '<STR_LIT>' ] <EOL> container , obj = path . split ( '<STR_LIT:/>' , <NUM_LIT:1> ) <EOL> status , reason , headers , contents = client . put_object ( <EOL> container , obj , body , headers = put_headers , query = context . query , <EOL> cdn = context . cdn ) <EOL> if hasattr ( contents , '<STR_LIT>' ) : <EOL> contents = contents . read ( ) <EOL> if status // <NUM_LIT:100> != <NUM_LIT:2> : <EOL> raise ReturnCode ( <EOL> '<STR_LIT>' % ( path , status , reason , contents ) ) <EOL> if context . seek is not None : <EOL> content_length = put_headers . get ( '<STR_LIT>' ) <EOL> etag = headers . get ( '<STR_LIT>' ) <EOL> if content_length and etag : <EOL> content_length = int ( content_length ) <EOL> else : <EOL> with context . client_manager . with_client ( ) as client : <EOL> container , obj = path . split ( '<STR_LIT:/>' , <NUM_LIT:1> ) <EOL> status , reason , headers , contents = client . head_object ( <EOL> container , obj , cdn = context . cdn ) <EOL> if hasattr ( contents , '<STR_LIT>' ) : <EOL> contents = contents . read ( ) <EOL> if status // <NUM_LIT:100> != <NUM_LIT:2> : <EOL> raise ReturnCode ( <EOL> '<STR_LIT>' % <EOL> ( path , status , reason , contents ) ) <EOL> content_length = headers . get ( '<STR_LIT>' ) <EOL> etag = headers . get ( '<STR_LIT>' ) <EOL> if content_length : <EOL> content_length = int ( content_length ) <EOL> return content_length , etag <EOL> if context . stdin is not None : <EOL> return headers . get ( '<STR_LIT>' ) <EOL> def cli_put ( context , path ) : <EOL> """<STR_LIT>""" <EOL> path = path . lstrip ( '<STR_LIT:/>' ) if path else '<STR_LIT>' <EOL> if context . input_ and os . path . isdir ( context . input_ ) : <EOL> return cli_put_directory_structure ( context , path ) <EOL> if not path : <EOL> return cli_put_account ( context ) <EOL> elif '<STR_LIT:/>' not in path . rstrip ( '<STR_LIT:/>' ) : <EOL> return cli_put_container ( context , path ) <EOL> else : <EOL> return cli_put_object ( context , path ) <EOL> def _get_segment_path ( prefix , n ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' % ( prefix , n ) <EOL> def _get_manifest_body ( context , prefix , path2info , put_headers ) : <EOL> """<STR_LIT>""" <EOL> if context . static_segments : <EOL> body = json . dumps ( [ <EOL> { '<STR_LIT:path>' : '<STR_LIT:/>' + p , '<STR_LIT>' : s , '<STR_LIT>' : e } <EOL> for p , ( s , e ) in sorted ( six . iteritems ( path2info ) ) <EOL> ] ) <EOL> put_headers [ '<STR_LIT>' ] = str ( len ( body ) ) <EOL> context . query [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> else : <EOL> body = '<STR_LIT>' <EOL> put_headers [ '<STR_LIT>' ] = '<STR_LIT:0>' <EOL> put_headers [ '<STR_LIT>' ] = prefix <EOL> return body <EOL> def _create_container ( context , path , l_mtime , size ) : <EOL> """<STR_LIT>""" <EOL> new_context = context . copy ( ) <EOL> new_context . input_ = None <EOL> new_context . headers = None <EOL> new_context . query = None <EOL> container = path . split ( '<STR_LIT:/>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] + '<STR_LIT>' <EOL> cli_put_container ( new_context , container ) <EOL> prefix = container + '<STR_LIT:/>' + path . split ( '<STR_LIT:/>' , <NUM_LIT:1> ) [ <NUM_LIT:1> ] <EOL> prefix = '<STR_LIT>' % ( prefix , l_mtime , size ) <EOL> return prefix <EOL> class CLIPut ( CLICommand ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , cli ) : <EOL> super ( CLIPut , self ) . __init__ ( <EOL> cli , '<STR_LIT>' , max_args = <NUM_LIT:1> , usage = """<STR_LIT>""" . strip ( ) ) <EOL> self . option_parser . add_option ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT>' , <EOL> metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . option_parser . add_option ( <EOL> '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT>' , <EOL> metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . option_parser . add_option ( <EOL> '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . option_parser . add_option ( <EOL> '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . option_parser . add_option ( <EOL> '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . option_parser . add_option ( <EOL> '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> self . option_parser . add_option ( <EOL> '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . option_parser . add_option ( <EOL> '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . option_parser . add_option ( <EOL> '<STR_LIT>' , dest = '<STR_LIT>' , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def __call__ ( self , args ) : <EOL> options , args , context = self . parse_args_and_create_context ( args ) <EOL> context . headers = self . options_list_to_lowered_dict ( options . header ) <EOL> context . query = self . options_list_to_lowered_dict ( options . query ) <EOL> context . input_ = options . input_ <EOL> context . segment_size = options . segment_size <EOL> context . static_segments = False <EOL> if context . segment_size and context . segment_size [ <NUM_LIT:0> ] . lower ( ) == '<STR_LIT:s>' : <EOL> context . static_segments = True <EOL> context . segment_size = context . segment_size [ <NUM_LIT:1> : ] <EOL> context . segment_size = int ( <EOL> context . segment_size or <NUM_LIT:5> * <NUM_LIT> * <NUM_LIT> * <NUM_LIT> ) <EOL> if context . segment_size < <NUM_LIT:1> : <EOL> raise ReturnCode ( '<STR_LIT>' % options . segment_size ) <EOL> context . stdin_segmentation = options . stdin_segmentation <EOL> context . empty = options . empty <EOL> context . newer = options . newer <EOL> context . different = options . different <EOL> context . encrypt = options . encrypt <EOL> if context . encrypt == '<STR_LIT:->' : <EOL> context . encrypt = os . environ . get ( '<STR_LIT>' ) <EOL> if not context . encrypt : <EOL> raise ReturnCode ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if context . encrypt and context . different : <EOL> raise ReturnCode ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> path = args . pop ( <NUM_LIT:0> ) . lstrip ( '<STR_LIT:/>' ) if args else None <EOL> return cli_put ( context , path ) </s>
