<s> from AgileCLU import AgileCLU <EOL> from optparse import OptionParser , OptionGroup <EOL> import sys , os . path , urllib , subprocess , time <EOL> import hashlib <EOL> from poster . encode import multipart_encode , get_body_size <EOL> from poster . streaminghttp import register_openers <EOL> from urllib2 import Request , urlopen , URLError , HTTPError <EOL> import progressbar <EOL> pbar = None <EOL> fname = None <EOL> def splitMultipart ( fname , pieceSize = <NUM_LIT> ) : <EOL> f = open ( fname , '<STR_LIT:rb>' ) <EOL> data = f . read ( ) <EOL> f . close ( ) <EOL> sha256 = hashlib . sha256 ( data ) . hexdigest ( ) <EOL> bytes = len ( data ) <EOL> pieces = bytes / pieceSize <EOL> if ( bytes % pieceSize ) : pieces += <NUM_LIT:1> <EOL> manifest = '<STR_LIT>' <EOL> pieceNames = [ ] <EOL> pieceNum = <NUM_LIT:0> <EOL> for i in range ( <NUM_LIT:0> , bytes + <NUM_LIT:1> , pieceSize ) : <EOL> pieceNum += <NUM_LIT:1> <EOL> fn1 = "<STR_LIT>" % i <EOL> pieceNames . append ( fn1 ) <EOL> f = open ( fn1 , '<STR_LIT:wb>' ) <EOL> fn1sha = hashlib . sha256 ( data [ i : i + pieceSize ] ) . hexdigest ( ) <EOL> print ( "<STR_LIT>" + fn1sha ) % i <EOL> f . write ( data [ i : i + pieceSize ] ) <EOL> f . close ( ) <EOL> if ( manifest < > '<STR_LIT>' ) : manifest = manifest + '<STR_LIT:U+002C>' <EOL> manifest = manifest + '''<STR_LIT>''' + '''<STR_LIT>''' + str ( pieceNum ) + '''<STR_LIT:U+002CU+0020>''' + '''<STR_LIT>''' + fn1 + '''<STR_LIT>''' + '''<STR_LIT>''' + fn1sha + '''<STR_LIT>''' + '''<STR_LIT>''' + str ( len ( data [ i : i + pieceSize ] ) ) + '''<STR_LIT:U+002CU+0020>''' + '''<STR_LIT>''' + str ( i ) + '''<STR_LIT:U+0020>''' + '''<STR_LIT:}>''' <EOL> jsonstr = '''<STR_LIT>''' + fname + '''<STR_LIT>''' + str ( pieces ) + '''<STR_LIT>''' + sha256 + '''<STR_LIT>''' + manifest + '''<STR_LIT>''' <EOL> print jsonstr <EOL> def progress_callback ( param , current , total ) : <EOL> global pbar <EOL> if ( pbar == None ) : <EOL> widgets = [ unicode ( fname , errors = '<STR_LIT:ignore>' ) . encode ( '<STR_LIT:utf-8>' ) , '<STR_LIT:U+0020>' , progressbar . FileTransferSpeed ( ) , '<STR_LIT>' , progressbar . Bar ( ) , '<STR_LIT>' , progressbar . Percentage ( ) , '<STR_LIT:U+0020>' , progressbar . ETA ( ) ] <EOL> pbar = progressbar . ProgressBar ( widgets = widgets , maxval = total ) . start ( ) <EOL> try : <EOL> pbar . update ( current ) <EOL> except AssertionError , e : <EOL> print e <EOL> def main ( * arg ) : <EOL> global fname , pbar <EOL> parser = OptionParser ( usage = "<STR_LIT>" , version = "<STR_LIT>" + AgileCLU . __version__ + "<STR_LIT:)>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store_true>" , dest = "<STR_LIT>" , help = "<STR_LIT>" , default = False ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT:username>" , help = "<STR_LIT>" ) <EOL> group = OptionGroup ( parser , "<STR_LIT>" ) <EOL> group . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT:filename>" , help = "<STR_LIT>" ) <EOL> group . add_option ( "<STR_LIT:-c>" , "<STR_LIT>" , dest = "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> group . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> group . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> group . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store_true>" , help = "<STR_LIT>" ) <EOL> group . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store_true>" , help = "<STR_LIT>" ) <EOL> parser . add_option_group ( group ) <EOL> config = OptionGroup ( parser , "<STR_LIT>" ) <EOL> config . add_option ( "<STR_LIT>" , dest = "<STR_LIT:username>" , help = "<STR_LIT>" ) <EOL> config . add_option ( "<STR_LIT>" , dest = "<STR_LIT:password>" , help = "<STR_LIT>" ) <EOL> config . add_option ( "<STR_LIT>" , dest = "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> config . add_option ( "<STR_LIT>" , dest = "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> config . add_option ( "<STR_LIT>" , dest = "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> config . add_option ( "<STR_LIT>" , dest = "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> parser . add_option_group ( config ) <EOL> ( options , args ) = parser . parse_args ( ) <EOL> if len ( args ) != <NUM_LIT:2> : parser . error ( "<STR_LIT>" ) <EOL> object = args [ <NUM_LIT:0> ] <EOL> path = args [ <NUM_LIT:1> ] <EOL> if ( not os . path . isfile ( object ) ) : <EOL> print "<STR_LIT>" % localfile <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if options . username : agile = AgileCLU ( options . username ) <EOL> else : agile = AgileCLU ( ) <EOL> localpath = os . path . dirname ( object ) <EOL> localfile = os . path . basename ( object ) <EOL> if options . filename : fname = options . filename <EOL> else : fname = localfile <EOL> print agile . token <EOL> print fname <EOL> print path <EOL> print os . path . join ( path , fname ) <EOL> r = agile . createMultipart ( os . path . join ( path , fname ) ) <EOL> mpid = r [ '<STR_LIT>' ] <EOL> splitMultipart ( fname , <NUM_LIT> ) <EOL> agile . logout ( ) <EOL> exit ( <NUM_LIT:1> ) <EOL> register_openers ( ) <EOL> if options . progress : <EOL> datagen , headers = multipart_encode ( { <EOL> "<STR_LIT>" : open ( object , "<STR_LIT:rb>" ) , <EOL> "<STR_LIT>" : path , <EOL> "<STR_LIT>" : fname , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } , cb = progress_callback ) <EOL> else : <EOL> datagen , headers = multipart_encode ( { <EOL> "<STR_LIT>" : open ( object , "<STR_LIT:rb>" ) , <EOL> "<STR_LIT>" : path , <EOL> "<STR_LIT>" : fname , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } ) <EOL> request = Request ( agile . posturl , datagen , headers ) <EOL> request . add_header ( "<STR_LIT>" , agile . token ) <EOL> if options . mimetype : mimetype = options . mimetype <EOL> else : mimetype = '<STR_LIT>' <EOL> request . add_header ( "<STR_LIT>" , mimetype ) <EOL> success = False ; attempt = <NUM_LIT:0> <EOL> while not success : <EOL> attempt += <NUM_LIT:1> <EOL> try : <EOL> result = urlopen ( request ) . read ( ) <EOL> if options . progress : pbar . finish ( ) <EOL> success = True <EOL> except HTTPError , e : <EOL> if options . progress : pbar . finish ( ) <EOL> print '<STR_LIT>' , e . code <EOL> pbar = None <EOL> success = False <EOL> except URLError , e : <EOL> if options . progress : pbar . finish ( ) <EOL> print '<STR_LIT>' , e . reason <EOL> pbar = None <EOL> success = False <EOL> if options . verbose : <EOL> print "<STR_LIT>" % ( agile . mapperurlstr ( ) , urllib . quote ( os . path . join ( path , fname ) ) ) <EOL> agile . logout ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
