Skip to content

Commit

Permalink
reorganize handling of CLI transfer scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
bimac committed Oct 4, 2023
1 parent c743165 commit 0e4de21
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 21 deletions.
46 changes: 28 additions & 18 deletions iblrig/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import datetime
import json
from pathlib import Path

import yaml
import shutil

Expand All @@ -17,6 +18,33 @@
logger = setup_logger('iblrig', level='INFO')


def _transfer_parser(description: str) -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description=description,
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
argument_default=argparse.SUPPRESS)
parser.add_argument("-l", "--local", action="store", type=dir_path, dest='local_path', help="override local data path")
parser.add_argument("-r", "--remote", action="store", type=dir_path, dest='remote_path', help="override remote data path")
parser.add_argument("-d", "--dry", action="store_true", dest='dry', help="do not remove local data after copying")
return parser


def dir_path(directory: str) -> Path:
directory = Path(directory)
if directory.exists():
return directory
raise argparse.ArgumentError(None, f'Directory `{directory}` not found')


def transfer_video_data_cli():
args = _transfer_parser("Copy video data to the local server.").parse_args()
transfer_video_data(**vars(args))


def transfer_data_cli():
args = _transfer_parser("Copy behavior data to the local server.").parse_args()
transfer_data(**vars(args))


def transfer_video_data(local_path=None, remote_path=None, dry=False):
# If paths not passed, uses those defined in the iblrig_settings.yaml file
rig_paths = get_local_and_remote_paths(local_path=local_path, remote_path=remote_path)
Expand All @@ -34,24 +62,6 @@ def transfer_video_data(local_path=None, remote_path=None, dry=False):
remote_path=remote_path, dry=dry, tag='video')


def dir_path(directory: str):
directory = Path(directory)
if directory.exists():
return directory
raise argparse.ArgumentError(None, f'Directory `{directory}` not found')


def transfer_data_cli():
parser = argparse.ArgumentParser(description="Copy behavior data to the local server.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
argument_default=argparse.SUPPRESS)
parser.add_argument("-l", "--local", action="store", type=dir_path, dest='local_path', help="override local data path")
parser.add_argument("-r", "--remote", action="store", type=dir_path, dest='remote_path', help="override remote data path")
parser.add_argument("-d", "--dry", action="store_true", dest='dry', help="do not remove local data after copying")
args = parser.parse_args()
transfer_data(**vars(args))


def transfer_data(local_path: Path = None, remote_path: Path = None, dry: bool = False, lab: str = None) -> None:
"""
Copies the behavior data from the rig to the local server if the session has more than 42 trials
Expand Down
5 changes: 3 additions & 2 deletions iblrig/transfer_experiments.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
log = setup_logger('iblrig', level='INFO')


def _copy_file_md5(src: str, dst: str, *args, **kwargs) -> str:
def _copy2_md5(src: str, dst: str, *args, **kwargs) -> str:
"""
Copy a file from source to destination with MD5 hash verification.
Expand Down Expand Up @@ -43,6 +43,7 @@ def _copy_file_md5(src: str, dst: str, *args, **kwargs) -> str:
"""
src_md5 = hashfile.md5(src)
if os.path.exists(dst) and samestat(os.stat(src), os.stat(dst)) and src_md5 == hashfile.md5(dst):
log.info(f'Skipping {dst.name} as it already exists at the destination')
return dst
return_val = shutil.copy2(src, dst, *args, **kwargs)
if not src_md5 == hashfile.md5(dst):
Expand Down Expand Up @@ -77,7 +78,7 @@ def copy_folders(local_folder: str, remote_folder: str, overwrite: bool = False)
remote_folder.parent.mkdir(parents=True, exist_ok=True)
shutil.copytree(local_folder, remote_folder, dirs_exist_ok=overwrite,
ignore=shutil.ignore_patterns('transfer_me.flag'),
copy_function=_copy_file_md5)
copy_function=_copy2_md5)
except OSError:
log.error(traceback.format_exc())
log.info(f"Could not copy {local_folder} to {remote_folder}")
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ DEV = [
[project.scripts]
viewsession = "iblrig.commands:viewsession"
transfer_data = "iblrig.commands:transfer_data_cli"
transfer_video_data = "iblrig.commands:transfer_video_data"
transfer_video_data = "iblrig.commands:transfer_video_data_cli"
flush = "iblrig.commands:flush"
remove-old-sessions = "iblrig.commands:remove_local_sessions"
iblrig = "iblrig.gui.wizard:main"
Expand Down

0 comments on commit 0e4de21

Please sign in to comment.