<s> """<STR_LIT>""" <EOL> from __future__ import division <EOL> import os , sys , getpass , struct , tty , fcntl , stat <EOL> import fnmatch , pwd , glob <EOL> from twisted . conch . client import connect , default , options <EOL> from twisted . conch . ssh import connection , common <EOL> from twisted . conch . ssh import channel , filetransfer <EOL> from twisted . protocols import basic <EOL> from twisted . internet import reactor , stdio , defer , utils <EOL> from twisted . python import log , usage , failure <EOL> class ClientOptions ( options . ConchOptions ) : <EOL> synopsis = """<STR_LIT>""" <EOL> longdesc = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> optParameters = [ <EOL> [ '<STR_LIT>' , '<STR_LIT:B>' , <NUM_LIT> , '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , '<STR_LIT:b>' , None , '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , '<STR_LIT:R>' , <NUM_LIT:5> , '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , '<STR_LIT:s>' , '<STR_LIT>' , '<STR_LIT>' ] ] <EOL> compData = usage . Completions ( <EOL> descriptions = { <EOL> "<STR_LIT>" : "<STR_LIT>" } , <EOL> extraActions = [ usage . CompleteUserAtHost ( ) , <EOL> usage . CompleteFiles ( descr = "<STR_LIT>" ) ] ) <EOL> def parseArgs ( self , host , localPath = None ) : <EOL> self [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> if '<STR_LIT::>' in host : <EOL> host , self [ '<STR_LIT>' ] = host . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> self [ '<STR_LIT>' ] . rstrip ( '<STR_LIT:/>' ) <EOL> self [ '<STR_LIT:host>' ] = host <EOL> self [ '<STR_LIT>' ] = localPath <EOL> def run ( ) : <EOL> args = sys . argv [ <NUM_LIT:1> : ] <EOL> if '<STR_LIT>' in args : <EOL> i = args . index ( '<STR_LIT>' ) <EOL> args = args [ i : i + <NUM_LIT:2> ] + args <EOL> del args [ i + <NUM_LIT:2> : i + <NUM_LIT:4> ] <EOL> options = ClientOptions ( ) <EOL> try : <EOL> options . parseOptions ( args ) <EOL> except usage . UsageError , u : <EOL> print '<STR_LIT>' % u <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if options [ '<STR_LIT>' ] : <EOL> realout = sys . stdout <EOL> log . startLogging ( sys . stderr ) <EOL> sys . stdout = realout <EOL> else : <EOL> log . discardLogs ( ) <EOL> doConnect ( options ) <EOL> reactor . run ( ) <EOL> def handleError ( ) : <EOL> global exitStatus <EOL> exitStatus = <NUM_LIT:2> <EOL> try : <EOL> reactor . stop ( ) <EOL> except : pass <EOL> log . err ( failure . Failure ( ) ) <EOL> raise <EOL> def doConnect ( options ) : <EOL> if '<STR_LIT:@>' in options [ '<STR_LIT:host>' ] : <EOL> options [ '<STR_LIT:user>' ] , options [ '<STR_LIT:host>' ] = options [ '<STR_LIT:host>' ] . split ( '<STR_LIT:@>' , <NUM_LIT:1> ) <EOL> host = options [ '<STR_LIT:host>' ] <EOL> if not options [ '<STR_LIT:user>' ] : <EOL> options [ '<STR_LIT:user>' ] = getpass . getuser ( ) <EOL> if not options [ '<STR_LIT:port>' ] : <EOL> options [ '<STR_LIT:port>' ] = <NUM_LIT> <EOL> else : <EOL> options [ '<STR_LIT:port>' ] = int ( options [ '<STR_LIT:port>' ] ) <EOL> host = options [ '<STR_LIT:host>' ] <EOL> port = options [ '<STR_LIT:port>' ] <EOL> conn = SSHConnection ( ) <EOL> conn . options = options <EOL> vhk = default . verifyHostKey <EOL> uao = default . SSHUserAuthClient ( options [ '<STR_LIT:user>' ] , options , conn ) <EOL> connect . connect ( host , port , options , vhk , uao ) . addErrback ( _ebExit ) <EOL> def _ebExit ( f ) : <EOL> if hasattr ( f . value , '<STR_LIT:value>' ) : <EOL> s = f . value . value <EOL> else : <EOL> s = str ( f ) <EOL> print s <EOL> try : <EOL> reactor . stop ( ) <EOL> except : pass <EOL> def _ignore ( * args ) : pass <EOL> class FileWrapper : <EOL> def __init__ ( self , f ) : <EOL> self . f = f <EOL> self . total = <NUM_LIT:0.0> <EOL> f . seek ( <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> self . size = f . tell ( ) <EOL> def __getattr__ ( self , attr ) : <EOL> return getattr ( self . f , attr ) <EOL> class StdioClient ( basic . LineReceiver ) : <EOL> _pwd = pwd <EOL> ps = '<STR_LIT>' <EOL> delimiter = '<STR_LIT:\n>' <EOL> reactor = reactor <EOL> def __init__ ( self , client , f = None ) : <EOL> self . client = client <EOL> self . currentDirectory = '<STR_LIT>' <EOL> self . file = f <EOL> self . useProgressBar = ( not f and <NUM_LIT:1> ) or <NUM_LIT:0> <EOL> def connectionMade ( self ) : <EOL> self . client . realPath ( '<STR_LIT>' ) . addCallback ( self . _cbSetCurDir ) <EOL> def _cbSetCurDir ( self , path ) : <EOL> self . currentDirectory = path <EOL> self . _newLine ( ) <EOL> def lineReceived ( self , line ) : <EOL> if self . client . transport . localClosed : <EOL> return <EOL> log . msg ( '<STR_LIT>' % repr ( line ) ) <EOL> line = line . lstrip ( ) <EOL> if not line : <EOL> self . _newLine ( ) <EOL> return <EOL> if self . file and line . startswith ( '<STR_LIT:->' ) : <EOL> self . ignoreErrors = <NUM_LIT:1> <EOL> line = line [ <NUM_LIT:1> : ] <EOL> else : <EOL> self . ignoreErrors = <NUM_LIT:0> <EOL> d = self . _dispatchCommand ( line ) <EOL> if d is not None : <EOL> d . addCallback ( self . _cbCommand ) <EOL> d . addErrback ( self . _ebCommand ) <EOL> def _dispatchCommand ( self , line ) : <EOL> if '<STR_LIT:U+0020>' in line : <EOL> command , rest = line . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) <EOL> rest = rest . lstrip ( ) <EOL> else : <EOL> command , rest = line , '<STR_LIT>' <EOL> if command . startswith ( '<STR_LIT:!>' ) : <EOL> f = self . cmd_EXEC <EOL> rest = ( command [ <NUM_LIT:1> : ] + '<STR_LIT:U+0020>' + rest ) . strip ( ) <EOL> else : <EOL> command = command . upper ( ) <EOL> log . msg ( '<STR_LIT>' % command ) <EOL> f = getattr ( self , '<STR_LIT>' % command , None ) <EOL> if f is not None : <EOL> return defer . maybeDeferred ( f , rest ) <EOL> else : <EOL> self . _ebCommand ( failure . Failure ( NotImplementedError ( <EOL> "<STR_LIT>" % command ) ) ) <EOL> self . _newLine ( ) <EOL> def _printFailure ( self , f ) : <EOL> log . msg ( f ) <EOL> e = f . trap ( NotImplementedError , filetransfer . SFTPError , OSError , IOError ) <EOL> if e == NotImplementedError : <EOL> self . transport . write ( self . cmd_HELP ( '<STR_LIT>' ) ) <EOL> elif e == filetransfer . SFTPError : <EOL> self . transport . write ( "<STR_LIT>" % <EOL> ( f . value . code , f . value . message ) ) <EOL> elif e in ( OSError , IOError ) : <EOL> self . transport . write ( "<STR_LIT>" % <EOL> ( f . value . errno , f . value . strerror ) ) <EOL> def _newLine ( self ) : <EOL> if self . client . transport . localClosed : <EOL> return <EOL> self . transport . write ( self . ps ) <EOL> self . ignoreErrors = <NUM_LIT:0> <EOL> if self . file : <EOL> l = self . file . readline ( ) <EOL> if not l : <EOL> self . client . transport . loseConnection ( ) <EOL> else : <EOL> self . transport . write ( l ) <EOL> self . lineReceived ( l . strip ( ) ) <EOL> def _cbCommand ( self , result ) : <EOL> if result is not None : <EOL> self . transport . write ( result ) <EOL> if not result . endswith ( '<STR_LIT:\n>' ) : <EOL> self . transport . write ( '<STR_LIT:\n>' ) <EOL> self . _newLine ( ) <EOL> def _ebCommand ( self , f ) : <EOL> self . _printFailure ( f ) <EOL> if self . file and not self . ignoreErrors : <EOL> self . client . transport . loseConnection ( ) <EOL> self . _newLine ( ) <EOL> def cmd_CD ( self , path ) : <EOL> path , rest = self . _getFilename ( path ) <EOL> if not path . endswith ( '<STR_LIT:/>' ) : <EOL> path += '<STR_LIT:/>' <EOL> newPath = path and os . path . join ( self . currentDirectory , path ) or '<STR_LIT>' <EOL> d = self . client . openDirectory ( newPath ) <EOL> d . addCallback ( self . _cbCd ) <EOL> d . addErrback ( self . _ebCommand ) <EOL> return d <EOL> def _cbCd ( self , directory ) : <EOL> directory . close ( ) <EOL> d = self . client . realPath ( directory . name ) <EOL> d . addCallback ( self . _cbCurDir ) <EOL> return d <EOL> def _cbCurDir ( self , path ) : <EOL> self . currentDirectory = path <EOL> def cmd_CHGRP ( self , rest ) : <EOL> grp , rest = rest . split ( None , <NUM_LIT:1> ) <EOL> path , rest = self . _getFilename ( rest ) <EOL> grp = int ( grp ) <EOL> d = self . client . getAttrs ( path ) <EOL> d . addCallback ( self . _cbSetUsrGrp , path , grp = grp ) <EOL> return d <EOL> def cmd_CHMOD ( self , rest ) : <EOL> mod , rest = rest . split ( None , <NUM_LIT:1> ) <EOL> path , rest = self . _getFilename ( rest ) <EOL> mod = int ( mod , <NUM_LIT:8> ) <EOL> d = self . client . setAttrs ( path , { '<STR_LIT>' : mod } ) <EOL> d . addCallback ( _ignore ) <EOL> return d <EOL> def cmd_CHOWN ( self , rest ) : <EOL> usr , rest = rest . split ( None , <NUM_LIT:1> ) <EOL> path , rest = self . _getFilename ( rest ) <EOL> usr = int ( usr ) <EOL> d = self . client . getAttrs ( path ) <EOL> d . addCallback ( self . _cbSetUsrGrp , path , usr = usr ) <EOL> return d <EOL> def _cbSetUsrGrp ( self , attrs , path , usr = None , grp = None ) : <EOL> new = { } <EOL> new [ '<STR_LIT>' ] = ( usr is not None ) and usr or attrs [ '<STR_LIT>' ] <EOL> new [ '<STR_LIT>' ] = ( grp is not None ) and grp or attrs [ '<STR_LIT>' ] <EOL> d = self . client . setAttrs ( path , new ) <EOL> d . addCallback ( _ignore ) <EOL> return d <EOL> def cmd_GET ( self , rest ) : <EOL> remote , rest = self . _getFilename ( rest ) <EOL> if '<STR_LIT:*>' in remote or '<STR_LIT:?>' in remote : <EOL> if rest : <EOL> local , rest = self . _getFilename ( rest ) <EOL> if not os . path . isdir ( local ) : <EOL> return "<STR_LIT>" <EOL> else : <EOL> local = '<STR_LIT>' <EOL> d = self . _remoteGlob ( remote ) <EOL> d . addCallback ( self . _cbGetMultiple , local ) <EOL> return d <EOL> if rest : <EOL> local , rest = self . _getFilename ( rest ) <EOL> else : <EOL> local = os . path . split ( remote ) [ <NUM_LIT:1> ] <EOL> log . msg ( ( remote , local ) ) <EOL> lf = file ( local , '<STR_LIT:w>' , <NUM_LIT:0> ) <EOL> path = os . path . join ( self . currentDirectory , remote ) <EOL> d = self . client . openFile ( path , filetransfer . FXF_READ , { } ) <EOL> d . addCallback ( self . _cbGetOpenFile , lf ) <EOL> d . addErrback ( self . _ebCloseLf , lf ) <EOL> return d <EOL> def _cbGetMultiple ( self , files , local ) : <EOL> return self . _cbGetMultipleNext ( None , files , local ) <EOL> def _cbGetMultipleNext ( self , res , files , local ) : <EOL> if isinstance ( res , failure . Failure ) : <EOL> self . _printFailure ( res ) <EOL> elif res : <EOL> self . transport . write ( res ) <EOL> if not res . endswith ( '<STR_LIT:\n>' ) : <EOL> self . transport . write ( '<STR_LIT:\n>' ) <EOL> if not files : <EOL> return <EOL> f = files . pop ( <NUM_LIT:0> ) [ <NUM_LIT:0> ] <EOL> lf = file ( os . path . join ( local , os . path . split ( f ) [ <NUM_LIT:1> ] ) , '<STR_LIT:w>' , <NUM_LIT:0> ) <EOL> path = os . path . join ( self . currentDirectory , f ) <EOL> d = self . client . openFile ( path , filetransfer . FXF_READ , { } ) <EOL> d . addCallback ( self . _cbGetOpenFile , lf ) <EOL> d . addErrback ( self . _ebCloseLf , lf ) <EOL> d . addBoth ( self . _cbGetMultipleNext , files , local ) <EOL> return d <EOL> def _ebCloseLf ( self , f , lf ) : <EOL> lf . close ( ) <EOL> return f <EOL> def _cbGetOpenFile ( self , rf , lf ) : <EOL> return rf . getAttrs ( ) . addCallback ( self . _cbGetFileSize , rf , lf ) <EOL> def _cbGetFileSize ( self , attrs , rf , lf ) : <EOL> if not stat . S_ISREG ( attrs [ '<STR_LIT>' ] ) : <EOL> rf . close ( ) <EOL> lf . close ( ) <EOL> return "<STR_LIT>" % rf . name <EOL> rf . size = attrs [ '<STR_LIT:size>' ] <EOL> bufferSize = self . client . transport . conn . options [ '<STR_LIT>' ] <EOL> numRequests = self . client . transport . conn . options [ '<STR_LIT>' ] <EOL> rf . total = <NUM_LIT:0.0> <EOL> dList = [ ] <EOL> chunks = [ ] <EOL> startTime = self . reactor . seconds ( ) <EOL> for i in range ( numRequests ) : <EOL> d = self . _cbGetRead ( '<STR_LIT>' , rf , lf , chunks , <NUM_LIT:0> , bufferSize , startTime ) <EOL> dList . append ( d ) <EOL> dl = defer . DeferredList ( dList , fireOnOneErrback = <NUM_LIT:1> ) <EOL> dl . addCallback ( self . _cbGetDone , rf , lf ) <EOL> return dl <EOL> def _getNextChunk ( self , chunks ) : <EOL> end = <NUM_LIT:0> <EOL> for chunk in chunks : <EOL> if end == '<STR_LIT>' : <EOL> return <EOL> if end != chunk [ <NUM_LIT:0> ] : <EOL> i = chunks . index ( chunk ) <EOL> chunks . insert ( i , ( end , chunk [ <NUM_LIT:0> ] ) ) <EOL> return ( end , chunk [ <NUM_LIT:0> ] - end ) <EOL> end = chunk [ <NUM_LIT:1> ] <EOL> bufSize = int ( self . client . transport . conn . options [ '<STR_LIT>' ] ) <EOL> chunks . append ( ( end , end + bufSize ) ) <EOL> return ( end , bufSize ) <EOL> def _cbGetRead ( self , data , rf , lf , chunks , start , size , startTime ) : <EOL> if data and isinstance ( data , failure . Failure ) : <EOL> log . msg ( '<STR_LIT>' % data ) <EOL> reason = data <EOL> reason . trap ( EOFError ) <EOL> i = chunks . index ( ( start , start + size ) ) <EOL> del chunks [ i ] <EOL> chunks . insert ( i , ( start , '<STR_LIT>' ) ) <EOL> elif data : <EOL> log . msg ( '<STR_LIT>' % len ( data ) ) <EOL> lf . seek ( start ) <EOL> lf . write ( data ) <EOL> if len ( data ) != size : <EOL> log . msg ( '<STR_LIT>' % <EOL> ( len ( data ) , size ) ) <EOL> i = chunks . index ( ( start , start + size ) ) <EOL> del chunks [ i ] <EOL> chunks . insert ( i , ( start , start + len ( data ) ) ) <EOL> rf . total += len ( data ) <EOL> if self . useProgressBar : <EOL> self . _printProgressBar ( rf , startTime ) <EOL> chunk = self . _getNextChunk ( chunks ) <EOL> if not chunk : <EOL> return <EOL> else : <EOL> start , length = chunk <EOL> log . msg ( '<STR_LIT>' % ( start , start + length ) ) <EOL> d = rf . readChunk ( start , length ) <EOL> d . addBoth ( self . _cbGetRead , rf , lf , chunks , start , length , startTime ) <EOL> return d <EOL> def _cbGetDone ( self , ignored , rf , lf ) : <EOL> log . msg ( '<STR_LIT>' ) <EOL> rf . close ( ) <EOL> lf . close ( ) <EOL> if self . useProgressBar : <EOL> self . transport . write ( '<STR_LIT:\n>' ) <EOL> return "<STR_LIT>" % ( rf . name , lf . name ) <EOL> def cmd_PUT ( self , rest ) : <EOL> """<STR_LIT>""" <EOL> local , rest = self . _getFilename ( rest ) <EOL> if '<STR_LIT:*>' in local or '<STR_LIT:?>' in local : <EOL> if rest : <EOL> remote , rest = self . _getFilename ( rest ) <EOL> remote = os . path . join ( self . currentDirectory , remote ) <EOL> else : <EOL> remote = '<STR_LIT>' <EOL> files = glob . glob ( local ) <EOL> return self . _putMultipleFiles ( files , remote ) <EOL> else : <EOL> if rest : <EOL> remote , rest = self . _getFilename ( rest ) <EOL> else : <EOL> remote = os . path . split ( local ) [ <NUM_LIT:1> ] <EOL> return self . _putSingleFile ( local , remote ) <EOL> def _putSingleFile ( self , local , remote ) : <EOL> """<STR_LIT>""" <EOL> return self . _cbPutMultipleNext ( None , [ local ] , remote , single = True ) <EOL> def _putMultipleFiles ( self , files , remote ) : <EOL> """<STR_LIT>""" <EOL> return self . _cbPutMultipleNext ( None , files , remote ) <EOL> def _cbPutMultipleNext ( <EOL> self , previousResult , files , remotePath , single = False ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( previousResult , failure . Failure ) : <EOL> self . _printFailure ( previousResult ) <EOL> elif previousResult : <EOL> self . transport . write ( previousResult ) <EOL> if not previousResult . endswith ( '<STR_LIT:\n>' ) : <EOL> self . transport . write ( '<STR_LIT:\n>' ) <EOL> currentFile = None <EOL> while files and not currentFile : <EOL> try : <EOL> currentFile = files . pop ( <NUM_LIT:0> ) <EOL> localStream = open ( currentFile , '<STR_LIT:r>' ) <EOL> except : <EOL> self . _printFailure ( failure . Failure ( ) ) <EOL> currentFile = None <EOL> if not currentFile : <EOL> return None <EOL> if single : <EOL> remote = remotePath <EOL> else : <EOL> name = os . path . split ( currentFile ) [ <NUM_LIT:1> ] <EOL> remote = os . path . join ( remotePath , name ) <EOL> log . msg ( ( name , remote , remotePath ) ) <EOL> d = self . _putRemoteFile ( localStream , remote ) <EOL> d . addBoth ( self . _cbPutMultipleNext , files , remotePath ) <EOL> return d <EOL> def _putRemoteFile ( self , localStream , remotePath ) : <EOL> """<STR_LIT>""" <EOL> remote = os . path . join ( self . currentDirectory , remotePath ) <EOL> flags = ( <EOL> filetransfer . FXF_WRITE | <EOL> filetransfer . FXF_CREAT | <EOL> filetransfer . FXF_TRUNC <EOL> ) <EOL> d = self . client . openFile ( remote , flags , { } ) <EOL> d . addCallback ( self . _cbPutOpenFile , localStream ) <EOL> d . addErrback ( self . _ebCloseLf , localStream ) <EOL> return d <EOL> def _cbPutOpenFile ( self , rf , lf ) : <EOL> numRequests = self . client . transport . conn . options [ '<STR_LIT>' ] <EOL> if self . useProgressBar : <EOL> lf = FileWrapper ( lf ) <EOL> dList = [ ] <EOL> chunks = [ ] <EOL> startTime = self . reactor . seconds ( ) <EOL> for i in range ( numRequests ) : <EOL> d = self . _cbPutWrite ( None , rf , lf , chunks , startTime ) <EOL> if d : <EOL> dList . append ( d ) <EOL> dl = defer . DeferredList ( dList , fireOnOneErrback = <NUM_LIT:1> ) <EOL> dl . addCallback ( self . _cbPutDone , rf , lf ) <EOL> return dl <EOL> def _cbPutWrite ( self , ignored , rf , lf , chunks , startTime ) : <EOL> chunk = self . _getNextChunk ( chunks ) <EOL> start , size = chunk <EOL> lf . seek ( start ) <EOL> data = lf . read ( size ) <EOL> if self . useProgressBar : <EOL> lf . total += len ( data ) <EOL> self . _printProgressBar ( lf , startTime ) <EOL> if data : <EOL> d = rf . writeChunk ( start , data ) <EOL> d . addCallback ( self . _cbPutWrite , rf , lf , chunks , startTime ) <EOL> return d <EOL> else : <EOL> return <EOL> def _cbPutDone ( self , ignored , rf , lf ) : <EOL> lf . close ( ) <EOL> rf . close ( ) <EOL> if self . useProgressBar : <EOL> self . transport . write ( '<STR_LIT:\n>' ) <EOL> return '<STR_LIT>' % ( lf . name , rf . name ) <EOL> def cmd_LCD ( self , path ) : <EOL> os . chdir ( path ) <EOL> def cmd_LN ( self , rest ) : <EOL> linkpath , rest = self . _getFilename ( rest ) <EOL> targetpath , rest = self . _getFilename ( rest ) <EOL> linkpath , targetpath = map ( <EOL> lambda x : os . path . join ( self . currentDirectory , x ) , <EOL> ( linkpath , targetpath ) ) <EOL> return self . client . makeLink ( linkpath , targetpath ) . addCallback ( _ignore ) <EOL> def cmd_LS ( self , rest ) : <EOL> options = [ ] <EOL> rest = rest . split ( ) <EOL> while rest and rest [ <NUM_LIT:0> ] and rest [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] == '<STR_LIT:->' : <EOL> opts = rest . pop ( <NUM_LIT:0> ) [ <NUM_LIT:1> : ] <EOL> for o in opts : <EOL> if o == '<STR_LIT:l>' : <EOL> options . append ( '<STR_LIT>' ) <EOL> elif o == '<STR_LIT:a>' : <EOL> options . append ( '<STR_LIT:all>' ) <EOL> rest = '<STR_LIT:U+0020>' . join ( rest ) <EOL> path , rest = self . _getFilename ( rest ) <EOL> if not path : <EOL> fullPath = self . currentDirectory + '<STR_LIT:/>' <EOL> else : <EOL> fullPath = os . path . join ( self . currentDirectory , path ) <EOL> d = self . _remoteGlob ( fullPath ) <EOL> d . addCallback ( self . _cbDisplayFiles , options ) <EOL> return d <EOL> def _cbDisplayFiles ( self , files , options ) : <EOL> files . sort ( ) <EOL> if '<STR_LIT:all>' not in options : <EOL> files = [ f for f in files if not f [ <NUM_LIT:0> ] . startswith ( '<STR_LIT:.>' ) ] <EOL> if '<STR_LIT>' in options : <EOL> lines = [ f [ <NUM_LIT:1> ] for f in files ] <EOL> else : <EOL> lines = [ f [ <NUM_LIT:0> ] for f in files ] <EOL> if not lines : <EOL> return None <EOL> else : <EOL> return '<STR_LIT:\n>' . join ( lines ) <EOL> def cmd_MKDIR ( self , path ) : <EOL> path , rest = self . _getFilename ( path ) <EOL> path = os . path . join ( self . currentDirectory , path ) <EOL> return self . client . makeDirectory ( path , { } ) . addCallback ( _ignore ) <EOL> def cmd_RMDIR ( self , path ) : <EOL> path , rest = self . _getFilename ( path ) <EOL> path = os . path . join ( self . currentDirectory , path ) <EOL> return self . client . removeDirectory ( path ) . addCallback ( _ignore ) <EOL> def cmd_LMKDIR ( self , path ) : <EOL> os . system ( "<STR_LIT>" % path ) <EOL> def cmd_RM ( self , path ) : <EOL> path , rest = self . _getFilename ( path ) <EOL> path = os . path . join ( self . currentDirectory , path ) <EOL> return self . client . removeFile ( path ) . addCallback ( _ignore ) <EOL> def cmd_LLS ( self , rest ) : <EOL> os . system ( "<STR_LIT>" % rest ) <EOL> def cmd_RENAME ( self , rest ) : <EOL> oldpath , rest = self . _getFilename ( rest ) <EOL> newpath , rest = self . _getFilename ( rest ) <EOL> oldpath , newpath = map ( <EOL> lambda x : os . path . join ( self . currentDirectory , x ) , <EOL> ( oldpath , newpath ) ) <EOL> return self . client . renameFile ( oldpath , newpath ) . addCallback ( _ignore ) <EOL> def cmd_EXIT ( self , ignored ) : <EOL> self . client . transport . loseConnection ( ) <EOL> cmd_QUIT = cmd_EXIT <EOL> def cmd_VERSION ( self , ignored ) : <EOL> return "<STR_LIT>" % self . client . version <EOL> def cmd_HELP ( self , ignored ) : <EOL> return """<STR_LIT>""" <EOL> def cmd_PWD ( self , ignored ) : <EOL> return self . currentDirectory <EOL> def cmd_LPWD ( self , ignored ) : <EOL> return os . getcwd ( ) <EOL> def cmd_PROGRESS ( self , ignored ) : <EOL> self . useProgressBar = not self . useProgressBar <EOL> return "<STR_LIT>" % ( self . useProgressBar and "<STR_LIT>" or "<STR_LIT>" ) <EOL> def cmd_EXEC ( self , rest ) : <EOL> """<STR_LIT>""" <EOL> shell = self . _pwd . getpwnam ( getpass . getuser ( ) ) [ <NUM_LIT:6> ] <EOL> if not shell : <EOL> shell = '<STR_LIT>' <EOL> if rest : <EOL> cmds = [ '<STR_LIT:-c>' , rest ] <EOL> return utils . getProcessOutput ( shell , cmds , errortoo = <NUM_LIT:1> ) <EOL> else : <EOL> os . system ( shell ) <EOL> def _remoteGlob ( self , fullPath ) : <EOL> log . msg ( '<STR_LIT>' % fullPath ) <EOL> head , tail = os . path . split ( fullPath ) <EOL> if '<STR_LIT:*>' in tail or '<STR_LIT:?>' in tail : <EOL> glob = <NUM_LIT:1> <EOL> else : <EOL> glob = <NUM_LIT:0> <EOL> if tail and not glob : <EOL> d = self . client . openDirectory ( fullPath ) <EOL> d . addCallback ( self . _cbOpenList , '<STR_LIT>' ) <EOL> d . addErrback ( self . _ebNotADirectory , head , tail ) <EOL> else : <EOL> d = self . client . openDirectory ( head ) <EOL> d . addCallback ( self . _cbOpenList , tail ) <EOL> return d <EOL> def _cbOpenList ( self , directory , glob ) : <EOL> files = [ ] <EOL> d = directory . read ( ) <EOL> d . addBoth ( self . _cbReadFile , files , directory , glob ) <EOL> return d <EOL> def _ebNotADirectory ( self , reason , path , glob ) : <EOL> d = self . client . openDirectory ( path ) <EOL> d . addCallback ( self . _cbOpenList , glob ) <EOL> return d <EOL> def _cbReadFile ( self , files , l , directory , glob ) : <EOL> if not isinstance ( files , failure . Failure ) : <EOL> if glob : <EOL> l . extend ( [ f for f in files if fnmatch . fnmatch ( f [ <NUM_LIT:0> ] , glob ) ] ) <EOL> else : <EOL> l . extend ( files ) <EOL> d = directory . read ( ) <EOL> d . addBoth ( self . _cbReadFile , l , directory , glob ) <EOL> return d <EOL> else : <EOL> reason = files <EOL> reason . trap ( EOFError ) <EOL> directory . close ( ) <EOL> return l <EOL> def _abbrevSize ( self , size ) : <EOL> _abbrevs = [ <EOL> ( <NUM_LIT:1> << <NUM_LIT:50> L , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:1> << <NUM_LIT> L , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:1> << <NUM_LIT:30> L , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:1> << <NUM_LIT:20> L , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:1> << <NUM_LIT:10> L , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:1> , '<STR_LIT:B>' ) <EOL> ] <EOL> for factor , suffix in _abbrevs : <EOL> if size > factor : <EOL> break <EOL> return '<STR_LIT>' % ( size / factor ) + suffix <EOL> def _abbrevTime ( self , t ) : <EOL> if t > <NUM_LIT> : <EOL> hours = int ( t / <NUM_LIT> ) <EOL> t -= ( <NUM_LIT> * hours ) <EOL> mins = int ( t / <NUM_LIT> ) <EOL> t -= ( <NUM_LIT> * mins ) <EOL> return "<STR_LIT>" % ( hours , mins , t ) <EOL> else : <EOL> mins = int ( t / <NUM_LIT> ) <EOL> t -= ( <NUM_LIT> * mins ) <EOL> return "<STR_LIT>" % ( mins , t ) <EOL> def _printProgressBar ( self , f , startTime ) : <EOL> """<STR_LIT>""" <EOL> diff = self . reactor . seconds ( ) - startTime <EOL> total = f . total <EOL> try : <EOL> winSize = struct . unpack ( '<STR_LIT>' , <EOL> fcntl . ioctl ( <NUM_LIT:0> , tty . TIOCGWINSZ , '<STR_LIT>' ) ) <EOL> except IOError : <EOL> winSize = [ None , <NUM_LIT> ] <EOL> if diff == <NUM_LIT:0.0> : <EOL> speed = <NUM_LIT:0.0> <EOL> else : <EOL> speed = total / diff <EOL> if speed : <EOL> timeLeft = ( f . size - total ) / speed <EOL> else : <EOL> timeLeft = <NUM_LIT:0> <EOL> front = f . name <EOL> if f . size : <EOL> percentage = ( total / f . size ) * <NUM_LIT:100> <EOL> else : <EOL> percentage = <NUM_LIT:100> <EOL> back = '<STR_LIT>' % ( percentage , <EOL> self . _abbrevSize ( total ) , <EOL> self . _abbrevSize ( speed ) , <EOL> self . _abbrevTime ( timeLeft ) ) <EOL> spaces = ( winSize [ <NUM_LIT:1> ] - ( len ( front ) + len ( back ) + <NUM_LIT:1> ) ) * '<STR_LIT:U+0020>' <EOL> self . transport . write ( '<STR_LIT>' % ( front , spaces , back ) ) <EOL> def _getFilename ( self , line ) : <EOL> """<STR_LIT>""" <EOL> line = line . strip ( ) <EOL> if not line : <EOL> return '<STR_LIT>' , '<STR_LIT>' <EOL> if line [ <NUM_LIT:0> ] in '<STR_LIT>' : <EOL> ret = [ ] <EOL> line = list ( line ) <EOL> try : <EOL> for i in range ( <NUM_LIT:1> , len ( line ) ) : <EOL> c = line [ i ] <EOL> if c == line [ <NUM_LIT:0> ] : <EOL> return '<STR_LIT>' . join ( ret ) , '<STR_LIT>' . join ( line [ i + <NUM_LIT:1> : ] ) . lstrip ( ) <EOL> elif c == '<STR_LIT:\\>' : <EOL> del line [ i ] <EOL> if line [ i ] not in '<STR_LIT>' : <EOL> raise IndexError , "<STR_LIT>" % line [ i ] <EOL> ret . append ( line [ i ] ) <EOL> else : <EOL> ret . append ( line [ i ] ) <EOL> except IndexError : <EOL> raise IndexError , "<STR_LIT>" <EOL> ret = line . split ( None , <NUM_LIT:1> ) <EOL> if len ( ret ) == <NUM_LIT:1> : <EOL> return ret [ <NUM_LIT:0> ] , '<STR_LIT>' <EOL> else : <EOL> return ret [ <NUM_LIT:0> ] , ret [ <NUM_LIT:1> ] <EOL> StdioClient . __dict__ [ '<STR_LIT>' ] = StdioClient . cmd_HELP <EOL> class SSHConnection ( connection . SSHConnection ) : <EOL> def serviceStarted ( self ) : <EOL> self . openChannel ( SSHSession ( ) ) <EOL> class SSHSession ( channel . SSHChannel ) : <EOL> name = '<STR_LIT>' <EOL> def channelOpen ( self , foo ) : <EOL> log . msg ( '<STR_LIT>' % self . id ) <EOL> if self . conn . options [ '<STR_LIT>' ] . startswith ( '<STR_LIT:/>' ) : <EOL> request = '<STR_LIT>' <EOL> else : <EOL> request = '<STR_LIT>' <EOL> d = self . conn . sendRequest ( self , request , common . NS ( self . conn . options [ '<STR_LIT>' ] ) , wantReply = <NUM_LIT:1> ) <EOL> d . addCallback ( self . _cbSubsystem ) <EOL> d . addErrback ( _ebExit ) <EOL> def _cbSubsystem ( self , result ) : <EOL> self . client = filetransfer . FileTransferClient ( ) <EOL> self . client . makeConnection ( self ) <EOL> self . dataReceived = self . client . dataReceived <EOL> f = None <EOL> if self . conn . options [ '<STR_LIT>' ] : <EOL> fn = self . conn . options [ '<STR_LIT>' ] <EOL> if fn != '<STR_LIT:->' : <EOL> f = file ( fn ) <EOL> self . stdio = stdio . StandardIO ( StdioClient ( self . client , f ) ) <EOL> def extReceived ( self , t , data ) : <EOL> if t == connection . EXTENDED_DATA_STDERR : <EOL> log . msg ( '<STR_LIT>' % len ( data ) ) <EOL> sys . stderr . write ( data ) <EOL> sys . stderr . flush ( ) <EOL> def eofReceived ( self ) : <EOL> log . msg ( '<STR_LIT>' ) <EOL> self . stdio . loseWriteConnection ( ) <EOL> def closeReceived ( self ) : <EOL> log . msg ( '<STR_LIT>' % self ) <EOL> self . conn . sendClose ( self ) <EOL> def closed ( self ) : <EOL> try : <EOL> reactor . stop ( ) <EOL> except : <EOL> pass <EOL> def stopWriting ( self ) : <EOL> self . stdio . pauseProducing ( ) <EOL> def startWriting ( self ) : <EOL> self . stdio . resumeProducing ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> run ( ) </s>
