-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscpFiles.py
113 lines (99 loc) · 3.84 KB
/
scpFiles.py
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import subprocess
import argparse
import re
from fabric import Connection
from config import CONNECTION_PARAMS, FILENAMESPATH, ROOTPATH
from custom_readers import date_hierarchy_reader
import base_reader
def get_treated_path(path: str, validate: bool=True):
valid_path_re_str = r'^(\/+|[\.\w]+)+$'
valid_path = re.compile(valid_path_re_str)
if validate:
if not valid_path.match(path):
raise ValueError(
fr"""
{path} is an invalid path
Use full or relative paths with slashs ('/') as
the directory delimiter and without any blank space.
The validating regex used is as follows:
{valid_path_re_str}
If you believe this validation to not be necessary, run with the --force flag.
"""
)
if path == '/':
raise ValueError(
"""
Trying to copy the ENTIRE server.
If you really want to do this, use the --force flag.
"""
)
if path[-1] != '/':
path = path + '/'
return path
def copy_from_csv(mode: str, target_root_directory: str, reader: base_reader.BaseReader):
with open(FILENAMESPATH, "r") as f:
_ = f.readline()
with Connection(
host=CONNECTION_PARAMS.host,
user=CONNECTION_PARAMS.usr,
connect_kwargs={
"password": CONNECTION_PARAMS.passwd,
"key_filename": None,
},
) as connection:
while subdirectory := reader.readline():
# adding backslash in case user forgot
if subdirectory[-1] != '/':
subdirectory = subdirectory + '/'
target_folder = target_root_directory + subdirectory
if " " in target_folder:
raise ValueError(
"Blank spaces are not permitted on paths"
)
mkdir_command = f"mkdir -p {target_folder}"
if mode == "remote":
connection.run(mkdir_command)
connection.run(
f"cp {ROOTPATH}/{subdirectory}* '{target_folder}'"
)
elif mode == "local":
subprocess.run(str.split(mkdir_command, " "), check=True)
subprocess.run(
[
"sshpass",
"-p",
CONNECTION_PARAMS.passwd,
"scp",
f"{CONNECTION_PARAMS.usr}@{CONNECTION_PARAMS.host}:{ROOTPATH}/{subdirectory}*",
target_folder,
],
check=True,
)
def dry_run(mode:str, target_root_directory: str, reader: base_reader.BaseReader):
pass
def get_args() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser()
parser.add_argument(
"--mode",
action="store",
dest="mode",
required=True,
help="local to copy to local machine; remote to copy to remote machine",
)
parser.add_argument(
"--path",
action="store",
dest="target_root_directory",
required=True,
help="Root folder to copy files/folders",
)
parser.add_argument('--force', dest='validate', action='store_false')
parser.add_argument('--dry-run', dest='run', action='store_false')
parser.set_defaults(validate=True, run=True)
args = parser.parse_args()
return args
if __name__ == "__main__":
args = get_args()
reader = date_hierarchy_reader.DateHierarchyReader(FILENAMESPATH)
treated_path = get_treated_path(args.target_root_directory, args.validate)
copy_from_csv(args.mode, treated_path, reader)