Skip to content

Commit f689590

Browse files
committed
Added download progress
1 parent 0f12d53 commit f689590

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,4 @@ htmlcov/*
149149
#VSCode files
150150
.vscode/*
151151
.vscode/launch.json
152+
.code-workspace

T3.code-workspace

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
{
22
"folders": [
33
{
4-
"path": "."
5-
},
6-
{
7-
"path": "../ARC"
4+
"path": "../../../../.t3"
85
},
96
{
10-
"path": "../RMG-Py"
7+
"path": "."
118
}
129
],
1310
"settings": {}

t3/utils/ssh.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,13 @@ def download_folder(self,
221221
logger.debug(f'{remote_folder_path} does not exist on {self.server}.')
222222
try:
223223
self._sftp.chdir(remote_folder_path)
224-
for item in self._sftp.listdir_attr():
224+
items = self._sftp.listdir_attr()
225+
226+
# Count the number of files to download for progress logging
227+
total_files = sum(1 for item in items if stat.S_ISREG(item.st_mode))
228+
downloaded_files = 0
229+
230+
for item in items:
225231
# Get the remote item's name and full path
226232
filename = item.filename
227233
remote_filepath = remote_folder_path + '/' + filename
@@ -233,12 +239,19 @@ def download_folder(self,
233239
# stat.S_ISREG(item.st_mode) is True if item is a file (not a folder)
234240
if stat.S_ISREG(item.st_mode):
235241
self._sftp.get(remote_filepath, local_filepath)
242+
downloaded_files += 1
243+
244+
# Log progress every 10%
245+
progress = downloaded_files / total_files * 100
246+
if progress % 10 == 0:
247+
logger.info(f'Downloaded {progress}% of files from {remote_folder_path}')
236248

237249
# Recursively download folders
238250
# stat.S_ISDIR(item.st_mode) is True if item is a folder
239251
elif stat.S_ISDIR(item.st_mode):
240252
# create the folder in the local path
241253
os.makedirs(local_filepath, exist_ok=True)
254+
# recursively download the folder items
242255
self.download_folder(remote_filepath, local_filepath)
243256
except IOError:
244257
logger.warning(f'Got an IOError when trying to download file '
@@ -423,7 +436,7 @@ def _connect(self) -> Tuple[paramiko.sftp_client.SFTPClient, paramiko.SSHClient]
423436
# 15 seconds (default in paramiko) due to network congestion, faulty switches,
424437
# etc..., common solution is enlarging the timeout variable.
425438
ssh.connect(hostname=self.address, username=self.un, banner_timeout=200, key_filename=self.key)
426-
except:
439+
except paramiko.ssh_exception.SSHException:
427440
# This sometimes gives "SSHException: Error reading SSH protocol banner[Error 104] Connection reset by peer"
428441
# Try again:
429442
ssh.connect(hostname=self.address, username=self.un, banner_timeout=200, key_filename=self.key)
@@ -564,6 +577,8 @@ def _check_file_exists(self,
564577
stdout, _ = self._send_command_to_server(command, remote_path='')
565578
if len(stdout):
566579
return True
580+
else:
581+
return False
567582

568583
def _check_dir_exists(self,
569584
remote_dir_path: str,
@@ -581,6 +596,8 @@ def _check_dir_exists(self,
581596
stdout, _ = self._send_command_to_server(command)
582597
if len(stdout):
583598
return True
599+
else:
600+
return False
584601

585602
def _create_dir(self, remote_path: str) -> None:
586603
"""

0 commit comments

Comments
 (0)