-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscp_alvis
executable file
·54 lines (37 loc) · 1.41 KB
/
scp_alvis
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#! /usr/bin/env python3
import shlex
import subprocess as sp
from pathlib import Path
from typing import List
import typer
from rich import print as echo
def scp_file(path, root_local, root_remote, scp_host):
"""scp one file to remote host"""
n_local = len(root_local.parts)
# make sure we're in the project directory
assert path.absolute().parts[:n_local] == root_local.parts
path_on_dardel = root_remote.joinpath(*path.absolute().parts[n_local:])
# echo(f'... copy "{path}" to "{scp_host}:{path_on_dardel.parent}"')
# make sure parent exists
echo(f"... make sure {path_on_dardel.parent} exists on '{scp_host}'")
_cmd = f"ssh {scp_host} mkdir -p '{path_on_dardel.parent}'"
sp.run(shlex.split(_cmd), check=True)
# copy path
echo(f"... copy '{path}'")
_cmd = f"scp -r {path} {scp_host}:{path_on_dardel.parent}"
sp.run(shlex.split(_cmd), check=True)
echo(f"--> check {scp_host}:{path_on_dardel.parent}/{path.name}")
app = typer.Typer(pretty_exceptions_show_locals=False)
@app.command()
def main(
paths: List[Path],
root_local: Path = "/Users/flokno/working/projects",
root_remote: Path = "/cephyr/users/knoopf/Alvis/projects",
scp_host: str = "alvis2",
):
# get path relative to root_local
echo(f"Copy these paths: {paths}")
for path in paths:
scp_file(path, root_local, root_remote, scp_host)
if __name__ == "__main__":
app()