-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbetter-copy.py
More file actions
68 lines (57 loc) · 1.92 KB
/
Copy pathbetter-copy.py
File metadata and controls
68 lines (57 loc) · 1.92 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
import subprocess
import click
import sys
import typing as t
def exec_command(command: t.List[str]) -> None:
"""
Execute a command and stream its output to stdout and stderr.
:param command: the command to execute
:type command: List[str]
"""
try:
result = subprocess.run(
command, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
sys.stdout.write(result.stdout.decode())
sys.stderr.write(result.stderr.decode())
except subprocess.CalledProcessError as e:
sys.stderr.write(e.stderr.decode())
sys.exit(e.returncode)
@click.command()
@click.option("-c", "--copy", "action", flag_value="copy", help="Copy files")
@click.option("-m", "--move", "action", flag_value="move", help="Move files")
@click.argument("source")
@click.argument("destination")
def better_copy(action: t.Optional[str], source: str, destination: str) -> None:
"""
Copy or move files from source to destination with progress information.
:param action: Action to take, either "copy" or "move"
:type action: str
:param source: Source directory or file
:type source: str
:param destination: Destination directory or file
:type destination: str
"""
if not action:
click.echo(
"You must specify whether to copy (--copy or -c) or move (--move or -m)."
)
sys.exit(1)
if action == "copy":
click.echo(f"Copying from {source} to {destination}...")
# copy command
command = ["rsync", "-avh", "--progress", source, destination]
elif action == "move":
click.echo(f"Moving from {source} to {destination}...")
# paste command
command = [
"rsync",
"-avh",
"--progress",
"--remove-source-files",
source,
destination,
]
exec_command(command)
if __name__ == "__main__":
better_copy()