Module osbot_utils.helpers.SCP
Expand source code
from osbot_utils.context_managers.capture_duration import capture_duration
from osbot_utils.helpers.SSH import SSH
from osbot_utils.testing.Temp_Zip import Temp_Zip
from osbot_utils.utils.Dev import pprint
from osbot_utils.utils.Files import file_exists, file_not_exists, file_name
from osbot_utils.utils.Process import start_process
from osbot_utils.utils.Status import status_error
from osbot_utils.utils.Zip import zip_folder
class SCP(SSH):
def copy_file_to_host(self, local_file, host_file=None):
if file_not_exists(local_file):
return status_error(error="in copy_file_to_host, local_file provided doesn't exist in current host", data={'local_file':local_file})
if host_file is None:
host_file = file_name(local_file)
scp_args = self.execute_ssh_args()
scp_args += [local_file]
scp_args += [f'{self.execute_command_target_host()}:{host_file}']
return self.execute_scp_command__return_stderr(scp_args)
def copy_file_from_host(self, host_file, local_file):
scp_args = self.execute_ssh_args()
scp_args += [f'{self.execute_command_target_host()}:{host_file}']
scp_args += [local_file]
return self.execute_scp_command__return_stderr(scp_args)
def copy_folder_as_zip_to_host(self, local_folder, unzip_to_folder):
if file_not_exists(local_folder):
return status_error(error="in copy_folder_as_zip_to_host, local_folder provided doesn't exist in current host", data={'local_folder':local_folder})
with Temp_Zip(target=local_folder) as temp_zip:
host_file = temp_zip.file_name()
kwargs = dict(local_file = temp_zip.path(),
host_file = host_file )
self.mkdir(unzip_to_folder)
self.copy_file_to_host(**kwargs)
command = f'unzip {host_file} -d {unzip_to_folder}'
self.execute_command(command)
self.rm(host_file)
def execute_scp_command(self, scp_args):
if self.ssh_host and self.ssh_key_file and self.ssh_key_user and scp_args:
with capture_duration() as duration:
result = start_process("scp", scp_args) # execute scp command using subprocess.run(...)
result['duration'] = duration.data()
return result
return status_error(error='in copy_file not all required vars were setup')
def execute_scp_command__return_stdout(self, scp_args):
return self.execute_scp_command(scp_args).get('stdout').strip()
def execute_scp_command__return_stderr(self, scp_args):
return self.execute_scp_command(scp_args).get('stderr').strip()
Classes
class SCP (**kwargs)
-
A mixin class to strictly assign keyword arguments to pre-defined instance attributes during initialization.
This base class provides an init method that assigns values from keyword arguments to instance attributes. If an attribute with the same name as a key from the kwargs is defined in the class, it will be set to the value from kwargs. If the key does not match any predefined attribute names, an exception is raised.
This behavior enforces strict control over the attributes of instances, ensuring that only predefined attributes can be set at the time of instantiation and avoids silent attribute creation which can lead to bugs in the code.
Usage
class MyConfigurableClass(Kwargs_To_Self): attribute1 = 'default_value' attribute2 = True attribute3 : str attribute4 : list attribute4 : int = 42
# Other methods can be added here
Correctly override default values by passing keyword arguments
instance = MyConfigurableClass(attribute1='new_value', attribute2=False)
This will raise an exception as 'attribute3' is not predefined
instance = MyConfigurableClass(attribute3='invalid_attribute')
this will also assign the default value to any variable that has a type defined. In the example above the default values (mapped by default__kwargs and locals) will be: attribute1 = 'default_value' attribute2 = True attribute3 = '' # default value of str attribute4 = [] # default value of list attribute4 = 42 # defined value in the class
Note
It is important that all attributes which may be set at instantiation are predefined in the class. Failure to do so will result in an exception being raised.
Methods
init(**kwargs): The initializer that handles the assignment of keyword arguments to instance attributes. It enforces strict attribute assignment rules, only allowing attributes that are already defined in the class to be set.
Initialize an instance of the derived class, strictly assigning provided keyword arguments to corresponding instance attributes.
Parameters
**kwargs: Variable length keyword arguments.
Raises
Exception
- If a key from kwargs does not correspond to any attribute pre-defined in the class, an exception is raised to prevent setting an undefined attribute.
Expand source code
class SCP(SSH): def copy_file_to_host(self, local_file, host_file=None): if file_not_exists(local_file): return status_error(error="in copy_file_to_host, local_file provided doesn't exist in current host", data={'local_file':local_file}) if host_file is None: host_file = file_name(local_file) scp_args = self.execute_ssh_args() scp_args += [local_file] scp_args += [f'{self.execute_command_target_host()}:{host_file}'] return self.execute_scp_command__return_stderr(scp_args) def copy_file_from_host(self, host_file, local_file): scp_args = self.execute_ssh_args() scp_args += [f'{self.execute_command_target_host()}:{host_file}'] scp_args += [local_file] return self.execute_scp_command__return_stderr(scp_args) def copy_folder_as_zip_to_host(self, local_folder, unzip_to_folder): if file_not_exists(local_folder): return status_error(error="in copy_folder_as_zip_to_host, local_folder provided doesn't exist in current host", data={'local_folder':local_folder}) with Temp_Zip(target=local_folder) as temp_zip: host_file = temp_zip.file_name() kwargs = dict(local_file = temp_zip.path(), host_file = host_file ) self.mkdir(unzip_to_folder) self.copy_file_to_host(**kwargs) command = f'unzip {host_file} -d {unzip_to_folder}' self.execute_command(command) self.rm(host_file) def execute_scp_command(self, scp_args): if self.ssh_host and self.ssh_key_file and self.ssh_key_user and scp_args: with capture_duration() as duration: result = start_process("scp", scp_args) # execute scp command using subprocess.run(...) result['duration'] = duration.data() return result return status_error(error='in copy_file not all required vars were setup') def execute_scp_command__return_stdout(self, scp_args): return self.execute_scp_command(scp_args).get('stdout').strip() def execute_scp_command__return_stderr(self, scp_args): return self.execute_scp_command(scp_args).get('stderr').strip()
Ancestors
Methods
def copy_file_from_host(self, host_file, local_file)
-
Expand source code
def copy_file_from_host(self, host_file, local_file): scp_args = self.execute_ssh_args() scp_args += [f'{self.execute_command_target_host()}:{host_file}'] scp_args += [local_file] return self.execute_scp_command__return_stderr(scp_args)
def copy_file_to_host(self, local_file, host_file=None)
-
Expand source code
def copy_file_to_host(self, local_file, host_file=None): if file_not_exists(local_file): return status_error(error="in copy_file_to_host, local_file provided doesn't exist in current host", data={'local_file':local_file}) if host_file is None: host_file = file_name(local_file) scp_args = self.execute_ssh_args() scp_args += [local_file] scp_args += [f'{self.execute_command_target_host()}:{host_file}'] return self.execute_scp_command__return_stderr(scp_args)
def copy_folder_as_zip_to_host(self, local_folder, unzip_to_folder)
-
Expand source code
def copy_folder_as_zip_to_host(self, local_folder, unzip_to_folder): if file_not_exists(local_folder): return status_error(error="in copy_folder_as_zip_to_host, local_folder provided doesn't exist in current host", data={'local_folder':local_folder}) with Temp_Zip(target=local_folder) as temp_zip: host_file = temp_zip.file_name() kwargs = dict(local_file = temp_zip.path(), host_file = host_file ) self.mkdir(unzip_to_folder) self.copy_file_to_host(**kwargs) command = f'unzip {host_file} -d {unzip_to_folder}' self.execute_command(command) self.rm(host_file)
def execute_scp_command(self, scp_args)
-
Expand source code
def execute_scp_command(self, scp_args): if self.ssh_host and self.ssh_key_file and self.ssh_key_user and scp_args: with capture_duration() as duration: result = start_process("scp", scp_args) # execute scp command using subprocess.run(...) result['duration'] = duration.data() return result return status_error(error='in copy_file not all required vars were setup')
def execute_scp_command__return_stderr(self, scp_args)
-
Expand source code
def execute_scp_command__return_stderr(self, scp_args): return self.execute_scp_command(scp_args).get('stderr').strip()
def execute_scp_command__return_stdout(self, scp_args)
-
Expand source code
def execute_scp_command__return_stdout(self, scp_args): return self.execute_scp_command(scp_args).get('stdout').strip()
Inherited members