<s> """<STR_LIT>""" <EOL> have_paramiko = False <EOL> try : <EOL> import paramiko <EOL> have_paramiko = True <EOL> except ImportError : <EOL> pass <EOL> from os . path import split as psplit <EOL> class BaseSSHClient ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , hostname , port = <NUM_LIT> , username = '<STR_LIT:root>' , password = None , key = None ) : <EOL> """<STR_LIT>""" <EOL> self . hostname = hostname <EOL> self . port = port <EOL> self . username = username <EOL> self . password = password <EOL> self . key = key <EOL> def connect ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError , '<STR_LIT>' <EOL> def put ( self , path , contents = None , chmod = None ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError , '<STR_LIT>' <EOL> def delete ( self , path ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError , '<STR_LIT>' <EOL> def run ( self , cmd ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError , '<STR_LIT>' <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError , '<STR_LIT>' <EOL> class ParamikoSSHClient ( BaseSSHClient ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , hostname , port = <NUM_LIT> , username = '<STR_LIT:root>' , password = None , key = None ) : <EOL> super ( ParamikoSSHClient , self ) . __init__ ( hostname , port , username , password , key ) <EOL> self . client = paramiko . SSHClient ( ) <EOL> self . client . set_missing_host_key_policy ( paramiko . AutoAddPolicy ( ) ) <EOL> def connect ( self ) : <EOL> conninfo = { '<STR_LIT>' : self . hostname , <EOL> '<STR_LIT:port>' : self . port , <EOL> '<STR_LIT:username>' : self . username , <EOL> '<STR_LIT:password>' : self . password , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : False } <EOL> self . client . connect ( ** conninfo ) <EOL> return True <EOL> def put ( self , path , contents = None , chmod = None ) : <EOL> sftp = self . client . open_sftp ( ) <EOL> head , tail = psplit ( path ) <EOL> if path [ <NUM_LIT:0> ] == "<STR_LIT:/>" : <EOL> sftp . chdir ( "<STR_LIT:/>" ) <EOL> for part in head . split ( "<STR_LIT:/>" ) : <EOL> if part != "<STR_LIT>" : <EOL> try : <EOL> sftp . mkdir ( part ) <EOL> except IOError : <EOL> pass <EOL> sftp . chdir ( part ) <EOL> ak = sftp . file ( tail , mode = '<STR_LIT:w>' ) <EOL> ak . write ( contents ) <EOL> if chmod is not None : <EOL> ak . chmod ( chmod ) <EOL> ak . close ( ) <EOL> sftp . close ( ) <EOL> def delete ( self , path ) : <EOL> sftp = self . client . open_sftp ( ) <EOL> sftp . unlink ( path ) <EOL> sftp . close ( ) <EOL> def run ( self , cmd ) : <EOL> bufsize = - <NUM_LIT:1> <EOL> t = self . client . get_transport ( ) <EOL> chan = t . open_session ( ) <EOL> chan . exec_command ( cmd ) <EOL> stdin = chan . makefile ( '<STR_LIT:wb>' , bufsize ) <EOL> stdout = chan . makefile ( '<STR_LIT:rb>' , bufsize ) <EOL> stderr = chan . makefile_stderr ( '<STR_LIT:rb>' , bufsize ) <EOL> stdin . close ( ) <EOL> status = chan . recv_exit_status ( ) <EOL> so = stdout . read ( ) <EOL> se = stderr . read ( ) <EOL> return [ so , se , status ] <EOL> def close ( self ) : <EOL> self . client . close ( ) <EOL> class ShellOutSSHClient ( BaseSSHClient ) : <EOL> pass <EOL> SSHClient = ParamikoSSHClient <EOL> if not have_paramiko : <EOL> SSHClient = ShellOutSSHClient </s>
