<s> import jsonrpclib <EOL> import ConfigParser , sys , os . path , logging <EOL> import poster , pycurl <EOL> import pyDes , hashlib , base64 <EOL> from urllib2 import Request , urlopen , URLError , HTTPError <EOL> import progressbar <EOL> class NullHandler ( logging . Handler ) : <EOL> def emit ( self , record ) : <EOL> pass <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> logger . addHandler ( NullHandler ( ) ) <EOL> cfg = ConfigParser . ConfigParser ( ) <EOL> def epwbasekey ( username , proto , hostname , basepath ) : <EOL> m = hashlib . md5 ( ) <EOL> m . update ( username ) <EOL> m . update ( proto ) <EOL> m . update ( hostname ) <EOL> m . update ( basepath ) <EOL> key = base64 . b64encode ( hashlib . sha256 ( m . digest ( ) ) . digest ( ) ) <EOL> return key [ <NUM_LIT:0> : <NUM_LIT> ] <EOL> def e_pw_hash ( str , username , proto , hostname , basepath ) : <EOL> hash = base64 . b64encode ( pyDes . triple_des ( epwbasekey ( username , proto , hostname , basepath ) ) . encrypt ( str , padmode = <NUM_LIT:2> ) ) <EOL> return hash <EOL> def e_pw_dehash ( str , username , proto , hostname , basepath ) : <EOL> basekey = epwbasekey ( username , proto , hostname , basepath ) <EOL> try : <EOL> b64decode = base64 . b64decode ( str ) <EOL> except TypeError , ValueError : <EOL> b64decode = "<STR_LIT>" <EOL> try : <EOL> if ( len ( b64decode ) % <NUM_LIT:8> == <NUM_LIT:0> ) : <EOL> dehash = pyDes . triple_des ( basekey ) . decrypt ( b64decode , padmode = <NUM_LIT:2> ) <EOL> else : <EOL> print "<STR_LIT>" <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> except TypeError , ValueError : <EOL> dehash = "<STR_LIT>" <EOL> if dehash is '<STR_LIT>' : dehash = '<STR_LIT>' <EOL> return dehash <EOL> class AgileCLU : <EOL> __module__ = "<STR_LIT>" <EOL> __version__ = "<STR_LIT>" <EOL> def __init__ ( self , profile = '<STR_LIT:default>' ) : <EOL> config_path = os . path . expanduser ( '<STR_LIT>' ) <EOL> if os . path . isfile ( os . path . join ( config_path , profile + '<STR_LIT>' ) ) : <EOL> cfg . read ( os . path . join ( config_path , profile + '<STR_LIT>' ) ) <EOL> else : <EOL> print "<STR_LIT>" % ( profile , profile ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> self . uid = cfg . get ( "<STR_LIT>" , "<STR_LIT:username>" ) <EOL> self . egress_protocol = cfg . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . egress_hostname = cfg . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . egress_basepath = cfg . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . ingest_protocol = cfg . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . ingest_hostname = cfg . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . mapperurl = self . egress_protocol + "<STR_LIT>" + self . egress_hostname + self . egress_basepath <EOL> self . apiurl = self . ingest_protocol + "<STR_LIT>" + self . ingest_hostname + "<STR_LIT>" <EOL> self . posturl = self . ingest_protocol + "<STR_LIT>" + self . ingest_hostname + "<STR_LIT>" <EOL> self . postrawurl = self . ingest_protocol + "<STR_LIT>" + self . ingest_hostname + "<STR_LIT>" <EOL> self . postmultiurl = self . ingest_protocol + "<STR_LIT>" + self . ingest_hostname + "<STR_LIT>" <EOL> self . pbar = None <EOL> self . pbarfname = None <EOL> upw = e_pw_dehash ( cfg . get ( "<STR_LIT>" , "<STR_LIT:password>" ) , self . uid , self . egress_protocol , self . egress_hostname , self . egress_basepath ) <EOL> if upw is "<STR_LIT>" : <EOL> print "<STR_LIT>" <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if cfg . getboolean ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> hdlr = logging . FileHandler ( '<STR_LIT>' ) <EOL> formatter = logging . Formatter ( '<STR_LIT>' ) <EOL> hdlr . setFormatter ( formatter ) <EOL> logger . addHandler ( hdlr ) <EOL> logger . setLevel ( logging . INFO ) <EOL> self . api = jsonrpclib . Server ( self . apiurl ) <EOL> self . token , self . user = self . api . login ( self . uid , upw ) <EOL> if self . token is None : <EOL> print "<STR_LIT>" + str ( self . uid ) + "<STR_LIT>" <EOL> logger . critical ( self . uid + "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> logger . info ( self . uid + "<STR_LIT:U+0020>" + self . token + "<STR_LIT>" + str ( self . user ) + "<STR_LIT>" ) <EOL> def apiurlstr ( self ) : <EOL> logger . info ( self . uid + "<STR_LIT:U+0020>" + self . token + "<STR_LIT>" + self . apiurl ) <EOL> return self . apiurl <EOL> def posturlstr ( self ) : <EOL> logger . info ( self . uid + "<STR_LIT:U+0020>" + self . token + "<STR_LIT>" + self . posturl ) <EOL> return self . posturl <EOL> def mapperurlstr ( self ) : <EOL> logger . info ( self . uid + "<STR_LIT:U+0020>" + self . token + "<STR_LIT>" + self . mapperurl ) <EOL> return self . mapperurl <EOL> def cacheurlstr ( self ) : <EOL> logger . info ( self . uid + "<STR_LIT:U+0020>" + self . token + "<STR_LIT>" + self . cacheurl ) <EOL> return self . cacheurl <EOL> def postmultiurlstr ( self ) : <EOL> logger . info ( self . uid + "<STR_LIT:U+0020>" + self . token + "<STR_LIT>" + self . postmultiurl ) <EOL> return self . postmultiurl <EOL> def tokenstr ( self ) : <EOL> logger . info ( self . uid + "<STR_LIT:U+0020>" + self . token + "<STR_LIT>" ) <EOL> return self . token <EOL> def stat ( self , path ) : <EOL> r = self . api . stat ( self . token , path ) <EOL> logger . info ( self . uid + "<STR_LIT:U+0020>" + self . token + "<STR_LIT>" + path + "<STR_LIT>" + str ( r ) ) <EOL> return r <EOL> def logout ( self ) : <EOL> r = self . api . logout ( self . token ) <EOL> logger . info ( self . uid + "<STR_LIT:U+0020>" + self . token + "<STR_LIT>" + str ( r ) + "<STR_LIT>" ) <EOL> return r <EOL> def noop ( self ) : <EOL> r = self . api . noop ( self . token ) <EOL> logger . info ( self . uid + "<STR_LIT:U+0020>" + self . token + "<STR_LIT>" + str ( r ) ) <EOL> return r <EOL> def listDir ( self , path , pageSize = <NUM_LIT> , cookie = <NUM_LIT:0> , stat = True ) : <EOL> r = self . api . listDir ( self . token , path , pageSize , cookie , stat ) <EOL> logger . info ( self . uid + "<STR_LIT:U+0020>" + self . token + "<STR_LIT>" + path + "<STR_LIT>" + str ( pageSize ) + "<STR_LIT>" + str ( cookie ) + "<STR_LIT>" + str ( stat ) ) <EOL> return r <EOL> def listFile ( self , path , pageSize = <NUM_LIT> , cookie = <NUM_LIT:0> , stat = True ) : <EOL> r = self . api . listFile ( self . token , path , pageSize , cookie , stat ) <EOL> logger . info ( self . uid + "<STR_LIT:U+0020>" + self . token + "<STR_LIT>" + path + "<STR_LIT>" + str ( pageSize ) + "<STR_LIT>" + str ( cookie ) + "<STR_LIT>" + str ( stat ) ) <EOL> return r <EOL> def createMultipart ( self , path ) : <EOL> r = self . api . createMultipart ( self . token , path ) <EOL> logger . info ( self . uid + "<STR_LIT:U+0020>" + self . token + "<STR_LIT>" + path ) <EOL> return r <EOL> def createMultipartPiece ( self , mpid = '<STR_LIT>' , number = <NUM_LIT:0> , size = <NUM_LIT:0> , checksum = '<STR_LIT>' , uri = '<STR_LIT>' ) : <EOL> r = self . api . createMultipartPiece ( self . token , mpid , number , size , checksum , uri ) <EOL> logger . info ( self . uid + "<STR_LIT:U+0020>" + self . token + "<STR_LIT>" + mpid + "<STR_LIT>" + str ( number ) + "<STR_LIT>" + str ( size ) + "<STR_LIT>" + checksum + "<STR_LIT>" + uri ) <EOL> def completeMultipart ( self , mpid = '<STR_LIT>' ) : <EOL> r = self . api . completeMultipart ( self . token , mpid ) <EOL> logger . info ( self . uid + "<STR_LIT:U+0020>" + self . token + "<STR_LIT>" + mpid ) <EOL> return r <EOL> def getMultipartStatus ( self , mpid = '<STR_LIT>' ) : <EOL> r = self . api . getMultipartStatus ( self . token , mpid ) <EOL> logger . info ( self . uid + "<STR_LIT:U+0020>" + self . token + "<STR_LIT>" + mpid ) <EOL> return r <EOL> def listMultipartPiece ( self , mpid = '<STR_LIT>' , lastpiece = <NUM_LIT:0> , pagesize = <NUM_LIT:100> ) : <EOL> r = self . api . listMultipartPiece ( self . token , mpid , lastpiece , pagesize ) <EOL> logger . info ( self . uid + "<STR_LIT:U+0020>" + self . token + "<STR_LIT>" + mpid + "<STR_LIT>" + str ( lastpiece ) + "<STR_LIT>" + str ( pagesize ) ) <EOL> return r <EOL> def makeDir ( self , path ) : <EOL> r = self . api . makeDir ( self . token , path ) <EOL> logger . info ( self . uid + "<STR_LIT:U+0020>" + self . token + "<STR_LIT>" + path + "<STR_LIT>" + str ( r ) ) <EOL> return r <EOL> def makeDir2 ( self , path ) : <EOL> r = self . api . makeDir2 ( self . token , path ) <EOL> logger . info ( self . uid + "<STR_LIT:U+0020>" + self . token + "<STR_LIT>" + path + "<STR_LIT>" + str ( r ) ) <EOL> return r <EOL> def deleteFile ( self , path ) : <EOL> r = self . api . deleteFile ( self . token , path ) <EOL> logger . info ( self . uid + "<STR_LIT:U+0020>" + self . token + "<STR_LIT>" + path + "<STR_LIT>" + str ( r ) ) <EOL> return r <EOL> def rm ( self , path ) : <EOL> if ( self . fexists ( path ) ) : <EOL> r = self . deleteFile ( path ) <EOL> if ( r == <NUM_LIT:0> ) : <EOL> logger . info ( self . uid + "<STR_LIT:U+0020>" + self . token + "<STR_LIT>" + path + "<STR_LIT>" ) <EOL> return True <EOL> else : <EOL> logger . warning ( self . uid + "<STR_LIT:U+0020>" + self . token + "<STR_LIT>" + path + "<STR_LIT>" ) <EOL> return False <EOL> else : <EOL> logger . warning ( self . uid + "<STR_LIT:U+0020>" + self . token + "<STR_LIT>" + path + "<STR_LIT>" ) <EOL> def deleteDir ( self , path ) : <EOL> r = self . api . deleteDir ( self . token , path ) <EOL> logger . info ( self . uid + "<STR_LIT:U+0020>" + self . token + "<STR_LIT>" + path + "<STR_LIT>" + str ( r ) ) <EOL> return r <EOL> def deleteObject ( self , path ) : <EOL> r = self . api . deleteObject ( self . token , path ) <EOL> logger . info ( self . uid + "<STR_LIT:U+0020>" + self . token + "<STR_LIT>" + path + "<STR_LIT>" + str ( r ) ) <EOL> return r <EOL> def rename ( self , path , newpath ) : <EOL> r = self . api . rename ( self . token , path , newpath ) <EOL> logger . info ( self . uid + "<STR_LIT:U+0020>" + self . token + "<STR_LIT>" + path + "<STR_LIT>" + newpath + "<STR_LIT>" + str ( r ) ) <EOL> return r <EOL> def copyFile ( self , path , newpath ) : <EOL> r = self . api . copyFile ( self . token , path , newpath ) <EOL> logger . info ( self . uid + "<STR_LIT:U+0020>" + self . token + "<STR_LIT>" + path + "<STR_LIT>" + newpath + "<STR_LIT>" + str ( r ) ) <EOL> return r <EOL> def registerCallback ( self , uri , flags = <NUM_LIT:0> , threshold = <NUM_LIT:0> ) : <EOL> r = self . api . registerCallback ( self . token , uri , flags , threshold ) <EOL> logger . info ( self . uid + "<STR_LIT:U+0020>" + self . token + "<STR_LIT>" + uri + "<STR_LIT>" + str ( flags ) + "<STR_LIT>" + str ( threshold ) + "<STR_LIT>" + str ( r ) ) <EOL> return r <EOL> def listCallback ( self ) : <EOL> r = self . api . listCallback ( self . token ) <EOL> logger . info ( self . uid + "<STR_LIT:U+0020>" + self . token + "<STR_LIT>" ) <EOL> return r <EOL> def fetchFileHTTP ( self , path , uri , username = None , password = None , auth = None , callbackid = <NUM_LIT:0> , priority = <NUM_LIT:0> , flags = <NUM_LIT:0> , expose_egress = '<STR_LIT>' ) : <EOL> r = self . api . fetchFileHTTP ( self . token , path , uri , username , password , auth , callbackid , priority , flags , expose_egress ) <EOL> logger . info ( self . uid + "<STR_LIT:U+0020>" + self . token + "<STR_LIT>" + path + "<STR_LIT>" + uri + "<STR_LIT>" + str ( username ) + "<STR_LIT>" + str ( auth ) + "<STR_LIT>" + str ( callbackid ) + "<STR_LIT>" + str ( priority ) + "<STR_LIT>" + str ( flags ) + "<STR_LIT>" + expose_egress + "<STR_LIT>" + str ( r ) ) <EOL> return r <EOL> def fetchFileFTP ( self , path , hostname , filename , username = None , password = None , port = <NUM_LIT> , passive = True , callbackid = <NUM_LIT:0> , priority = <NUM_LIT:0> , flags = <NUM_LIT:0> , expose_egress = '<STR_LIT>' ) : <EOL> r = self . api . fetchFileFTP ( self . token , path , hostname , filename , username , password , port , passive , callbackid , priority , flags , expose_egress ) <EOL> logger . info ( self . uid + "<STR_LIT:U+0020>" + self . token + "<STR_LIT>" + path + "<STR_LIT>" + hostname + "<STR_LIT>" + filename + "<STR_LIT>" + str ( username ) + "<STR_LIT>" + str ( port ) + "<STR_LIT>" + str ( passive ) + "<STR_LIT>" + str ( callbackid ) + "<STR_LIT>" + str ( priority ) + "<STR_LIT>" + str ( flags ) + "<STR_LIT>" + expose_egress + "<STR_LIT>" + str ( r ) ) <EOL> return r <EOL> def fexists ( self , path ) : <EOL> r = self . stat ( path ) <EOL> logger . info ( self . uid + "<STR_LIT:U+0020>" + self . token + "<STR_LIT>" + path + "<STR_LIT>" + str ( r ) ) <EOL> if ( ( r [ '<STR_LIT:code>' ] == <NUM_LIT:0> ) and ( r [ '<STR_LIT:type>' ] == <NUM_LIT:2> ) ) : return True <EOL> else : return False <EOL> def dexists ( self , path ) : <EOL> r = self . stat ( path ) <EOL> logger . info ( self . uid + "<STR_LIT:U+0020>" + self . token + "<STR_LIT>" + path + "<STR_LIT>" + str ( r ) ) <EOL> if ( ( r [ '<STR_LIT:code>' ] == <NUM_LIT:0> ) and ( r [ '<STR_LIT:type>' ] == <NUM_LIT:1> ) ) : return True <EOL> else : return False <EOL> def exists ( self , path ) : <EOL> logger . info ( self . uid + "<STR_LIT:U+0020>" + self . token + "<STR_LIT>" + path ) <EOL> if ( self . fexists ( path ) or self . dexists ( path ) ) : return True <EOL> else : return False <EOL> def mkdir ( self , path , recursive = False ) : <EOL> logger . info ( self . uid + "<STR_LIT:U+0020>" + self . token + "<STR_LIT>" + path ) <EOL> if ( not self . dexists ( path ) ) : <EOL> if ( recursive ) : <EOL> r = self . makeDir2 ( path ) <EOL> else : <EOL> r = self . makeDir ( path ) <EOL> if ( r == <NUM_LIT:0> ) : <EOL> return True <EOL> else : <EOL> return False <EOL> else : <EOL> return False <EOL> def read ( self , path ) : <EOL> logger . info ( self . uid + "<STR_LIT:U+0020>" + self . token + "<STR_LIT>" + self . mapper . url + urllib2 . quote ( path ) ) <EOL> if ( self . fexists ( path ) ) : <EOL> response = urllib2 . urlopen ( self . mapperurl + urllib2 . quote ( path ) ) <EOL> return response . read ( ) <EOL> else : <EOL> return False <EOL> def pbar_callback ( self , param , current , total ) : <EOL> if ( self . pbar == None ) : <EOL> widgets = [ unicode ( self . pbarfname , 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> self . pbar = progressbar . ProgressBar ( widgets = widgets , maxval = total ) . start ( ) <EOL> try : <EOL> self . pbar . update ( current ) <EOL> except AssertionError , e : <EOL> print e <EOL> print "<STR_LIT:!>" <EOL> def post ( self , sourceObject , targetPath , rename = None , mimetype = '<STR_LIT>' , mtime = None , egress_policy = '<STR_LIT>' , mkdir = False , callback = None , debug = True ) : <EOL> if debug : <EOL> logger . info ( self . uid + "<STR_LIT:U+0020>" + self . token + "<STR_LIT>" + sourceObject + "<STR_LIT:U+0020>" + targetPath + "<STR_LIT>" + str ( rename ) + "<STR_LIT>" + str ( mimetype ) + "<STR_LIT>" + str ( mtime ) + "<STR_LIT>" + str ( egress_policy ) + "<STR_LIT>" + str ( mkdir ) ) <EOL> if ( not os . path . isfile ( sourceObject ) ) : logger . info ( "<STR_LIT>" + sourceObject + "<STR_LIT>" ) ; return False <EOL> if ( not self . dexists ( targetPath ) ) : logger . info ( "<STR_LIT>" + targetPath + "<STR_LIT>" ) ; return False <EOL> sourcePath = os . path . dirname ( sourceObject ) <EOL> sourceName = os . path . basename ( sourceObject ) <EOL> self . pbarfname = sourceName <EOL> poster . streaminghttp . register_openers ( ) <EOL> datagen , headers = poster . encode . multipart_encode ( { <EOL> "<STR_LIT>" : open ( os . path . join ( sourcePath , sourceName ) , "<STR_LIT:rb>" ) , <EOL> "<STR_LIT>" : targetPath , <EOL> "<STR_LIT>" : sourceName , <EOL> "<STR_LIT>" : egress_policy <EOL> } , cb = callback ) <EOL> request = Request ( self . posturl , datagen , headers ) <EOL> request . add_header ( "<STR_LIT>" , self . token ) <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 callback < > None : self . pbar . finish ( ) <EOL> self . pbar = None <EOL> success = True <EOL> except HTTPError , e : <EOL> if callback < > None : self . pbar . finish ( ) <EOL> print '<STR_LIT>' , e . code <EOL> self . pbar = None <EOL> success = False <EOL> except URLError , e : <EOL> if callback < > None : self . pbar . finish ( ) <EOL> print '<STR_LIT>' , e . reason <EOL> self . pbar = None <EOL> success = False <EOL> def postraw ( self , sourceObject , targetPath , rename = None , mimetype = '<STR_LIT>' , mtime = None , egress_policy = '<STR_LIT>' , mkdir = False , callback = None , debug = True ) : <EOL> if debug : <EOL> logger . info ( self . uid + "<STR_LIT:U+0020>" + self . token + "<STR_LIT>" + sourceObject + "<STR_LIT:U+0020>" + targetPath + "<STR_LIT>" + str ( rename ) + "<STR_LIT>" + str ( mimetype ) + "<STR_LIT>" + str ( mtime ) + "<STR_LIT>" + str ( egress_policy ) + "<STR_LIT>" + str ( mkdir ) ) <EOL> if ( not os . path . isfile ( sourceObject ) ) : logger . info ( "<STR_LIT>" + sourceObject + "<STR_LIT>" ) ; return False <EOL> if ( not self . dexists ( targetPath ) ) : logger . info ( "<STR_LIT>" + targetPath + "<STR_LIT>" ) ; return False <EOL> sourcePath = os . path . dirname ( sourceObject ) <EOL> sourceName = os . path . basename ( sourceObject ) <EOL> headers = [ ] <EOL> headers . append ( str ( "<STR_LIT>" % ( str ( self . token ) ) ) ) <EOL> headers . append ( str ( "<STR_LIT>" % ( mimetype ) ) ) <EOL> headers . append ( str ( "<STR_LIT>" % ( str ( sourceName ) ) ) ) <EOL> headers . append ( str ( "<STR_LIT>" % ( str ( targetPath ) ) ) ) <EOL> total_filesize = os . stat ( sourceObject ) . st_size <EOL> c = pycurl . Curl ( ) <EOL> c . setopt ( c . URL , self . postrawurl ) <EOL> c . setopt ( c . HTTPHEADER , headers ) <EOL> c . setopt ( c . UPLOAD , <NUM_LIT:1> ) <EOL> c . setopt ( c . READFUNCTION , open ( sourceObject , '<STR_LIT:rb>' ) . read ) <EOL> c . setopt ( c . INFILESIZE_LARGE , total_filesize ) <EOL> c . setopt ( c . CUSTOMREQUEST , "<STR_LIT:POST>" ) <EOL> c . setopt ( c . VERBOSE , False ) <EOL> c . setopt ( c . SSL_VERIFYHOST , False ) <EOL> c . setopt ( c . SSL_VERIFYPEER , False ) <EOL> c . perform ( ) </s>
