Skip to content

Commit 9e9f828

Browse files
sbngrosssgross-emlix
authored andcommitted
driver/sshdriver: add multifile support to scp
Users are accustomed to scp having an option for copying recursively as well as to accept multiple source files. Meet user expectation by adding well known `-r` option and support for multiple source files. Signed-off-by: Sebastian Gross <sgross@emlix.com>
1 parent 2f0bdf1 commit 9e9f828

1 file changed

Lines changed: 23 additions & 9 deletions

File tree

labgrid/driver/sshdriver.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -356,25 +356,38 @@ def forward_unix_socket(self, unixsocket, localport=None):
356356
yield localport
357357

358358
@Driver.check_active
359-
@step(args=['src', 'dst'])
360-
def scp(self, *, src, dst):
359+
@step(args=['src', 'dst', 'recursive'])
360+
def scp(self, *, src, dst, recursive=False):
361361
if not self._check_keepalive():
362362
raise ExecutionError("Keepalive no longer running")
363363

364-
if src.startswith(':') == dst.startswith(':'):
364+
if not isinstance(src, list):
365+
src = [src]
366+
367+
# take Path like objects into account
368+
src = [str(f) for f in src]
369+
dst = str(dst)
370+
371+
remote_src = [f.startswith(':') for f in src]
372+
if any(remote_src) != all(remote_src):
373+
raise ValueError("All sources must be consistently local or remote (start with :)")
374+
375+
if all(remote_src) == dst.startswith(':'):
365376
raise ValueError("Either source or destination must be remote (start with :)")
366-
if src.startswith(':'):
367-
src = '_' + src
368-
if dst.startswith(':'):
369-
dst = '_' + dst
377+
378+
src = [s.replace(':', '_:') for s in src]
379+
dst = dst.replace(':', '_:')
370380

371381
complete_cmd = [self._scp,
372382
"-S", self._ssh,
373383
"-F", "none",
374384
"-o", f"ControlPath={self.control.replace('%', '%%')}",
375-
src, dst,
385+
*src,
386+
dst,
376387
]
377-
388+
389+
if recursive:
390+
complete_cmd.insert(1, "-r")
378391
if self.explicit_sftp_mode and self._scp_supports_explicit_sftp_mode():
379392
complete_cmd.insert(1, "-s")
380393
if self.explicit_scp_mode and self._scp_supports_explicit_scp_mode():
@@ -594,3 +607,4 @@ def _stop_keepalive(self):
594607
if stdout:
595608
for line in stdout.splitlines():
596609
self.logger.warning("Keepalive %s: %s", self.networkservice.address, line)
610+

0 commit comments

Comments
 (0)