Skip to content

Commit b14c2ba

Browse files
committed
fixes #699
1 parent 20524bb commit b14c2ba

File tree

3 files changed

+65
-3
lines changed

3 files changed

+65
-3
lines changed

fastcore/_modidx.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,12 +652,15 @@
652652
'fastcore.xtras.repo_details': ('xtras.html#repo_details', 'fastcore/xtras.py'),
653653
'fastcore.xtras.repr_dict': ('xtras.html#repr_dict', 'fastcore/xtras.py'),
654654
'fastcore.xtras.round_multiple': ('xtras.html#round_multiple', 'fastcore/xtras.py'),
655+
'fastcore.xtras.rsync_multi': ('xtras.html#rsync_multi', 'fastcore/xtras.py'),
655656
'fastcore.xtras.run': ('xtras.html#run', 'fastcore/xtras.py'),
656657
'fastcore.xtras.save_iter': ('xtras.html#save_iter', 'fastcore/xtras.py'),
657658
'fastcore.xtras.save_pickle': ('xtras.html#save_pickle', 'fastcore/xtras.py'),
658659
'fastcore.xtras.set_num_threads': ('xtras.html#set_num_threads', 'fastcore/xtras.py'),
660+
'fastcore.xtras.shell': ('xtras.html#shell', 'fastcore/xtras.py'),
659661
'fastcore.xtras.shufflish': ('xtras.html#shufflish', 'fastcore/xtras.py'),
660662
'fastcore.xtras.sparkline': ('xtras.html#sparkline', 'fastcore/xtras.py'),
663+
'fastcore.xtras.ssh': ('xtras.html#ssh', 'fastcore/xtras.py'),
661664
'fastcore.xtras.stringfmt_names': ('xtras.html#stringfmt_names', 'fastcore/xtras.py'),
662665
'fastcore.xtras.time_policy': ('xtras.html#time_policy', 'fastcore/xtras.py'),
663666
'fastcore.xtras.timed_cache': ('xtras.html#timed_cache', 'fastcore/xtras.py'),

fastcore/xtras.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
# %% auto 0
99
__all__ = ['spark_chars', 'UNSET', 'walk', 'globtastic', 'maybe_open', 'mkdir', 'image_size', 'bunzip', 'loads', 'loads_multi',
10-
'dumps', 'untar_dir', 'repo_details', 'run', 'open_file', 'save_pickle', 'load_pickle', 'parse_env',
11-
'expand_wildcards', 'dict2obj', 'obj2dict', 'repr_dict', 'is_listy', 'mapped', 'IterLen',
12-
'ReindexCollection', 'SaveReturn', 'trim_wraps', 'save_iter', 'asave_iter', 'friendly_name',
10+
'dumps', 'untar_dir', 'repo_details', 'shell', 'ssh', 'rsync_multi', 'run', 'open_file', 'save_pickle',
11+
'load_pickle', 'parse_env', 'expand_wildcards', 'dict2obj', 'obj2dict', 'repr_dict', 'is_listy', 'mapped',
12+
'IterLen', 'ReindexCollection', 'SaveReturn', 'trim_wraps', 'save_iter', 'asave_iter', 'friendly_name',
1313
'n_friendly_names', 'exec_eval', 'get_source_link', 'truncstr', 'sparkline', 'modify_exception',
1414
'round_multiple', 'set_num_threads', 'join_path_file', 'autostart', 'EventTimer', 'stringfmt_names',
1515
'PartialFormatter', 'partial_format', 'utc2local', 'local2utc', 'trace', 'modified_env', 'ContextManagers',
@@ -198,6 +198,25 @@ def repo_details(url):
198198
res = res.split(':')[-1]
199199
return res.split('/')[-2:]
200200

201+
# %% ../nbs/03_xtras.ipynb
202+
def shell(*args, **kwargs):
203+
"Shortcut for `subprocess.run(shell=True)`"
204+
import subprocess
205+
return subprocess.run(*args, shell=True, **kwargs)
206+
207+
# %% ../nbs/03_xtras.ipynb
208+
def ssh(host, args='', user='ubuntu', sock=None):
209+
"Run SSH command with given arguments"
210+
sock_opts = f'-S {sock}' if sock else ''
211+
return shell(f'ssh {sock_opts} {args} {user}@{host}')
212+
213+
# %% ../nbs/03_xtras.ipynb
214+
def rsync_multi(ip, files, user='ubuntu', persist='5m'):
215+
"Transfer multiple files with rename using persistent SSH connection"
216+
sock = f'/tmp/ssh-{ip}-{user}'
217+
ssh(ip, f'-o ControlMaster=auto -o ControlPersist={persist} -N -f', user, sock)
218+
for src,dst in files: shell(f'rsync -az -e "ssh -S {sock}" {src} {user}@{ip}:{dst}')
219+
201220
# %% ../nbs/03_xtras.ipynb
202221
def run(cmd, *rest, same_in_win=False, ignore_ex=False, as_bytes=False, stderr=False):
203222
"Pass `cmd` (splitting with `shlex` if string) to `subprocess.run`; return `stdout`; raise `IOError` if fails"

nbs/03_xtras.ipynb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,46 @@
665665
"test_eq(repo_details('[email protected]:fastai/nbdev.git\\n'), ['fastai', 'nbdev'])"
666666
]
667667
},
668+
{
669+
"cell_type": "code",
670+
"execution_count": null,
671+
"metadata": {},
672+
"outputs": [],
673+
"source": [
674+
"#|export\n",
675+
"def shell(*args, **kwargs):\n",
676+
" \"Shortcut for `subprocess.run(shell=True)`\"\n",
677+
" import subprocess\n",
678+
" return subprocess.run(*args, shell=True, **kwargs)"
679+
]
680+
},
681+
{
682+
"cell_type": "code",
683+
"execution_count": null,
684+
"metadata": {},
685+
"outputs": [],
686+
"source": [
687+
"#|export\n",
688+
"def ssh(host, args='', user='ubuntu', sock=None):\n",
689+
" \"Run SSH command with given arguments\"\n",
690+
" sock_opts = f'-S {sock}' if sock else ''\n",
691+
" return shell(f'ssh {sock_opts} {args} {user}@{host}')"
692+
]
693+
},
694+
{
695+
"cell_type": "code",
696+
"execution_count": null,
697+
"metadata": {},
698+
"outputs": [],
699+
"source": [
700+
"#|export\n",
701+
"def rsync_multi(ip, files, user='ubuntu', persist='5m'):\n",
702+
" \"Transfer multiple files with rename using persistent SSH connection\"\n",
703+
" sock = f'/tmp/ssh-{ip}-{user}'\n",
704+
" ssh(ip, f'-o ControlMaster=auto -o ControlPersist={persist} -N -f', user, sock)\n",
705+
" for src,dst in files: shell(f'rsync -az -e \"ssh -S {sock}\" {src} {user}@{ip}:{dst}')"
706+
]
707+
},
668708
{
669709
"cell_type": "code",
670710
"execution_count": null,

0 commit comments

Comments
 (0)