Commit 38c24765 authored by Anton Pershin's avatar Anton Pershin

Добавил опциональный вывод сообщений на экран в методах BaseCommunication и наследниках

parent 295cb6e7
......@@ -94,7 +94,7 @@ class BaseCommunication(metaclass=ABCMeta):
pass
@abstractmethod
def copy(self, from_, to_, mode='from_local'):
def copy(self, from_, to_, mode='from_local', show_msg=False):
'''
Copies from_ to to_ which are interpreted according to mode:
(1) from_local (default) -> from_ is local path, to_ is a path on a communicated machine
......@@ -148,11 +148,12 @@ class LocalCommunication(BaseCommunication):
subprocess.call([command_line], shell=True)
return [], []
def copy(self, from_, to_, mode='from_local'):
def copy(self, from_, to_, mode='from_local', show_msg=False):
'''
Any mode is ignored since the copying shall be within a local machine anyway
'''
#self._print_copy_msg(from_, to_)
if show_msg:
self._print_copy_msg(from_, to_)
return cp(from_, to_)
def rm(self, target):
......@@ -216,18 +217,19 @@ class SshCommunication(BaseCommunication):
# for line in stderr:
# print('\t\t' + line.strip('\n'))
def copy(self, from_, to_, mode='from_local'):
def copy(self, from_, to_, mode='from_local', show_msg=False):
if self.ssh_client is None:
raise Exception('Remote host is not set')
self._init_sftp()
new_path = None
if mode == 'from_local':
new_path = self._copy_from_local(from_, to_)
new_path = self._copy_from_local(from_, to_, show_msg)
elif mode == 'from_remote':
new_path = self._copy_from_remote(from_, to_)
new_path = self._copy_from_remote(from_, to_, show_msg)
elif mode == 'all_remote':
# self._print_copy_msg(self._machine_name + ':' + from_, self._machine_name + ':' + to_)
if show_msg:
self._print_copy_msg(self.machine_name + ':' + from_, self.machine_name + ':' + to_)
self._mkdirp(to_)
self.execute('cp -r %s %s' % (from_, to_))
else:
......@@ -287,29 +289,31 @@ class SshCommunication(BaseCommunication):
except IOError:
return False
def _copy_from_local(self, from_, to_):
def _copy_from_local(self, from_, to_, show_msg=False):
new_path_on_remote = to_ + '/' + os.path.basename(from_)
if os.path.isfile(from_):
self._mkdirp(to_)
# self._print_copy_msg(from_, self._machine_name + ':' + to_)
if show_msg:
self._print_copy_msg(from_, self.machine_name + ':' + to_)
self._put(from_, new_path_on_remote)
elif os.path.isdir(from_):
self.mkdir(new_path_on_remote)
for dir_or_file in os.listdir(from_):
self._copy_from_local(os.path.join(from_, dir_or_file), new_path_on_remote)
self._copy_from_local(os.path.join(from_, dir_or_file), new_path_on_remote, show_msg)
else:
raise CommunicationError("Path %s does not exist" % from_)
return new_path_on_remote
def _copy_from_remote(self, from_, to_):
def _copy_from_remote(self, from_, to_, show_msg=False):
new_path_on_local = os.path.join(to_, os.path.basename(from_))
if not self._is_remote_dir(from_):
# self._print_copy_msg(self._machine_name + ':' + from_, to_)
if show_msg:
self._print_copy_msg(self.machine_name + ':' + from_, to_)
self._get(from_, new_path_on_local)
else:
os.mkdir(new_path_on_local)
for dir_or_file in self.sftp_client.listdir(from_):
self._copy_from_remote(from_ + '/' + dir_or_file, new_path_on_local)
self._copy_from_remote(from_ + '/' + dir_or_file, new_path_on_local, show_msg)
return new_path_on_local
def disconnect(self):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment