-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtransfer.py
More file actions
88 lines (76 loc) · 2.78 KB
/
Copy pathtransfer.py
File metadata and controls
88 lines (76 loc) · 2.78 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from __future__ import annotations
import argparse
import subprocess
from pathlib import Path
from urllib.parse import ParseResult, urlparse
import requests
from rich.console import Console
from rich.prompt import Confirm
from murfey.client import read_config
from murfey.util.api import url_path_for
from murfey.util.config import MachineConfig
def run():
config = read_config()
known_server = config["Murfey"].get("server", "")
instrument_name = config["Murfey"].get("instrument_name", "")
parser = argparse.ArgumentParser(description="Transfer using a remote rsync daemon")
parser.add_argument("--source", type=str, default="")
parser.add_argument("--destination", "-d", type=str)
parser.add_argument("--destination-prefix", type=str, default="data")
parser.add_argument("--delete", action="store_true")
parser.add_argument(
"--server",
metavar="HOST:PORT",
type=str,
help=f"Murfey server to connect to ({known_server})",
default=known_server,
)
args = parser.parse_args()
console = Console()
murfey_url: ParseResult = urlparse(args.server, allow_fragments=False)
machine_data = MachineConfig(
requests.get(
f"{murfey_url.geturl()}{url_path_for('session_control.router', 'machine_info_by_instrument', instrument_name=instrument_name)}"
).json()
)
if Path(args.source or ".").resolve() in machine_data.data_directories:
console.print("[red]Source directory is the base directory, exiting")
return
cmd = [
"rsync",
"-iiv",
"--times",
"--progress",
"-o", # preserve ownership
"-p", # preserve permissions
]
if args.delete:
cmd.append("--remove-source-files")
if Path(args.source or ".").is_file():
num_files = 1
else:
num_files = len(
[f for f in Path(args.source or ".").glob("**/*") if f.is_file()]
)
delete_prompt = Confirm.ask(
f"Do you want to remove {num_files} from {args.source or Path('.').resolve()}?"
)
if not delete_prompt:
return
console.print(
f"Copying {args.source} -> {murfey_url.hostname}::{args.destination_prefix}/{args.destination}"
)
if Path(args.source or ".").is_file():
cmd.extend(
[
args.source or ".",
f"{murfey_url.hostname}::{args.destination_prefix}/{args.destination}",
]
)
else:
cmd.append("-r")
cmd.extend(list(Path(args.source or ".").glob("*")))
cmd.append(f"{murfey_url.hostname}::{args.destination}")
result = subprocess.run(cmd)
if result.returncode:
console.print(f"[red]rsync failed returning code {result.returncode}")