|
5 | 5 | if TYPE_CHECKING: |
6 | 6 | from datashuttle.configs.config_class import Configs |
7 | 7 |
|
8 | | -import fnmatch |
9 | 8 | import getpass |
10 | | -import stat |
11 | 9 | import sys |
12 | 10 | from pathlib import Path |
13 | | -from typing import Any, List, Optional, Tuple |
| 11 | +from typing import Optional |
14 | 12 |
|
15 | 13 | import paramiko |
16 | 14 |
|
@@ -322,119 +320,3 @@ def verify_ssh_central_host( |
322 | 320 | utils.log("Host not accepted. No connection made.") |
323 | 321 |
|
324 | 322 | return success |
325 | | - |
326 | | - |
327 | | -# ----------------------------------------------------------------------------- |
328 | | -# Search over SSH |
329 | | -# ----------------------------------------------------------------------------- |
330 | | - |
331 | | - |
332 | | -def search_ssh_central_for_folders( |
333 | | - search_path: Path, |
334 | | - search_prefix: str, |
335 | | - cfg: Configs, |
336 | | - verbose: bool = True, |
337 | | - return_full_path: bool = False, |
338 | | -) -> Tuple[List[Any], List[Any]]: |
339 | | - """Search for the search prefix in the search path over SSH. |
340 | | -
|
341 | | - Parameters |
342 | | - ---------- |
343 | | - search_path |
344 | | - Path to search for folders in. |
345 | | -
|
346 | | - search_prefix |
347 | | - Search prefix for folder names e.g. "sub-*". |
348 | | -
|
349 | | - cfg |
350 | | - See connect_client_with_logging(). |
351 | | -
|
352 | | - verbose |
353 | | - If `True`, if a search folder cannot be found, a message |
354 | | - will be printed with the un-found path. |
355 | | -
|
356 | | - return_full_path |
357 | | - include the search_path in the returned paths |
358 | | -
|
359 | | - Returns |
360 | | - ------- |
361 | | - Discovered folders (`all_folder_names`) and files (`all_filenames`). |
362 | | -
|
363 | | - """ |
364 | | - client: paramiko.SSHClient |
365 | | - with paramiko.SSHClient() as client: |
366 | | - connect_client_with_logging( |
367 | | - client, cfg, message_on_sucessful_connection=verbose |
368 | | - ) |
369 | | - |
370 | | - sftp = client.open_sftp() |
371 | | - |
372 | | - all_folder_names, all_filenames = get_list_of_folder_names_over_sftp( |
373 | | - sftp, |
374 | | - search_path, |
375 | | - search_prefix, |
376 | | - verbose, |
377 | | - return_full_path, |
378 | | - ) |
379 | | - |
380 | | - return all_folder_names, all_filenames |
381 | | - |
382 | | - |
383 | | -def get_list_of_folder_names_over_sftp( |
384 | | - sftp: paramiko.sftp_client.SFTPClient, |
385 | | - search_path: Path, |
386 | | - search_prefix: str, |
387 | | - verbose: bool = True, |
388 | | - return_full_path: bool = False, |
389 | | -) -> Tuple[List[Any], List[Any]]: |
390 | | - """Use paramiko's sftp to search a path over ssh for folders. |
391 | | -
|
392 | | - Return the folder names. |
393 | | -
|
394 | | - Parameters |
395 | | - ---------- |
396 | | - sftp |
397 | | - Connected paramiko stfp object |
398 | | - (see search_ssh_central_for_folders()). |
399 | | -
|
400 | | - search_path |
401 | | - Path to search for folders in. |
402 | | -
|
403 | | - search_prefix |
404 | | - Prefix (can include wildcards) |
405 | | - to search folder names. |
406 | | -
|
407 | | - verbose |
408 | | - If `True`, if a search folder cannot be found, a message |
409 | | - will be printed with the un-found path. |
410 | | -
|
411 | | - return_full_path |
412 | | - include the search_path in the returned paths. |
413 | | -
|
414 | | - Returns |
415 | | - ------- |
416 | | - Discovered folders (`all_folder_names`) and files (`all_filenames`). |
417 | | -
|
418 | | - """ |
419 | | - all_folder_names = [] |
420 | | - all_filenames = [] |
421 | | - try: |
422 | | - for file_or_folder in sftp.listdir_attr(search_path.as_posix()): |
423 | | - if file_or_folder.st_mode is not None and fnmatch.fnmatch( |
424 | | - file_or_folder.filename, search_prefix |
425 | | - ): |
426 | | - to_append = ( |
427 | | - search_path / file_or_folder.filename |
428 | | - if return_full_path |
429 | | - else file_or_folder.filename |
430 | | - ) |
431 | | - if stat.S_ISDIR(file_or_folder.st_mode): |
432 | | - all_folder_names.append(to_append) |
433 | | - else: |
434 | | - all_filenames.append(to_append) |
435 | | - |
436 | | - except FileNotFoundError: |
437 | | - if verbose: |
438 | | - utils.log_and_message(f"No file found at {search_path.as_posix()}") |
439 | | - |
440 | | - return all_folder_names, all_filenames |
0 commit comments