<s> import logging <EOL> import math <EOL> import os <EOL> from fabric import api <EOL> from fabric import state <EOL> from cloudferry import cfglib <EOL> from cloudferry . lib . utils import remote_runner <EOL> LOG = logging . getLogger ( __name__ ) <EOL> CONF = cfglib . CONF <EOL> class RemoteSymlink ( object ) : <EOL> def __init__ ( self , runner , target , symlink_name ) : <EOL> self . runner = runner <EOL> self . target = target <EOL> self . symlink = symlink_name <EOL> def __enter__ ( self ) : <EOL> if self . target is None : <EOL> return <EOL> cmd = "<STR_LIT>" <EOL> self . runner . run ( cmd , file = self . target , symlink_name = self . symlink ) <EOL> return self <EOL> def __exit__ ( self , exc_type , exc_val , exc_tb ) : <EOL> if self . target is None : <EOL> return <EOL> remote_rm ( self . runner , self . symlink , ignoring_errors = True ) <EOL> return self <EOL> class RemoteTempFile ( object ) : <EOL> def __init__ ( self , runner , filename , text ) : <EOL> self . runner = runner <EOL> self . filename = os . path . join ( '<STR_LIT>' , filename ) <EOL> self . text = text <EOL> def __enter__ ( self ) : <EOL> cmd = "<STR_LIT>" <EOL> self . runner . run ( cmd , text = self . text , filename = self . filename ) <EOL> return self <EOL> def __exit__ ( self , exc_type , exc_val , exc_tb ) : <EOL> remote_rm ( self . runner , self . filename , ignoring_errors = True ) <EOL> return self <EOL> class RemoteDir ( object ) : <EOL> def __init__ ( self , runner , dirname ) : <EOL> self . runner = runner <EOL> self . dirname = dirname <EOL> def __enter__ ( self ) : <EOL> cmd = "<STR_LIT>" <EOL> self . runner . run ( cmd , dir = self . dirname ) <EOL> return self <EOL> def __exit__ ( self , exc_type , exc_val , exc_tb ) : <EOL> remote_rm ( self . runner , self . dirname , recursive = True , <EOL> ignoring_errors = True ) <EOL> def is_installed ( runner , cmd ) : <EOL> try : <EOL> is_installed_cmd = "<STR_LIT>" . format ( cmd = cmd ) <EOL> runner . run ( is_installed_cmd ) <EOL> return True <EOL> except remote_runner . RemoteExecutionError : <EOL> return False <EOL> class RemoteStdout ( object ) : <EOL> def __init__ ( self , host , user , cmd , ** kwargs ) : <EOL> self . host = host <EOL> self . user = user <EOL> if kwargs : <EOL> cmd = cmd . format ( ** kwargs ) <EOL> self . cmd = cmd <EOL> self . stdin = None <EOL> self . stdout = None <EOL> self . stderr = None <EOL> def run ( self ) : <EOL> with api . settings ( <EOL> host_string = self . host , <EOL> user = self . user , <EOL> combine_stderr = False , <EOL> connection_attempts = CONF . migrate . ssh_connection_attempts , <EOL> reject_unkown_hosts = False , <EOL> ) : <EOL> conn = state . connections [ self . host ] <EOL> return conn . exec_command ( self . cmd ) <EOL> def __enter__ ( self ) : <EOL> self . stdin , self . stdout , self . stderr = self . run ( ) <EOL> return self <EOL> def __exit__ ( self , exc_type , exc_val , exc_tb ) : <EOL> if self . stdin : <EOL> self . stdin . close ( ) <EOL> if self . stdout : <EOL> self . stdout . close ( ) <EOL> if self . stderr : <EOL> self . stderr . close ( ) <EOL> if all ( ( exc_type , exc_val , exc_tb ) ) : <EOL> raise exc_type , exc_val , exc_tb <EOL> def remote_file_size ( runner , path ) : <EOL> return int ( runner . run ( '<STR_LIT>' , path = path ) ) <EOL> def remote_file_size_mb ( runner , path ) : <EOL> return int ( math . ceil ( remote_file_size ( runner , path ) / ( <NUM_LIT> * <NUM_LIT> ) ) ) <EOL> def remote_md5_sum ( runner , path ) : <EOL> get_md5 = "<STR_LIT>" <EOL> return runner . run ( get_md5 , path = path ) <EOL> def remote_rm ( runner , path , recursive = False , ignoring_errors = False ) : <EOL> options = '<STR_LIT:f>' <EOL> if recursive : <EOL> options += '<STR_LIT:r>' <EOL> cmd = "<STR_LIT>" <EOL> run = runner . run_ignoring_errors if ignoring_errors else runner . run <EOL> run ( cmd , options = options , path = path ) <EOL> def remote_gzip ( runner , path ) : <EOL> cmd = "<STR_LIT>" <EOL> runner . run ( cmd , path = path ) <EOL> return path + "<STR_LIT>" <EOL> def remote_split_file ( runner , input , output , start , block_size ) : <EOL> cmd = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> runner . run ( cmd , input = input , output = output , block_size = block_size , <EOL> start = start ) <EOL> def remote_unzip ( runner , path ) : <EOL> cmd = "<STR_LIT>" <EOL> runner . run ( cmd , path = path ) <EOL> def remote_join_file ( runner , dest_file , part , start , block_size ) : <EOL> cmd = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> runner . run ( cmd , part = part , dest = dest_file , start = start , <EOL> block_size = block_size ) </s>
