forked from PermanentOrg/sftp-qa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
159 lines (136 loc) · 4.46 KB
/
utils.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python3
import os
import argparse
import datetime
import subprocess
from pathlib import Path
import __main__
RCLONE_REMOTE = "permanent"
ARCHIVE_PATH = ""
TIMEOUT = 5 * 60
LOG_FILE = "log.txt"
RCLONE = subprocess.check_output("which rclone", shell=True).strip().decode("utf-8")
def which(cmd):
"""Return path to cmd"""
return subprocess.check_output(f"which {cmd}", shell=True).strip().decode("utf-8")
def log(msg, echo=True):
"""Print message to log file (and screen if echo is True)"""
if echo:
print(msg)
with open(LOG_FILE, "a", encoding="utf-8") as fh:
fh.write(msg)
fh.write("\n")
def slurp_if_e(fname):
if os.path.exists(fname):
with open(fname, encoding="utf-8") as fh:
return fh.read()
return ""
def run(fname, args):
"""Execute rclone command pass in args on path fname"""
start_time = datetime.datetime.now()
try:
process = subprocess.Popen(
args, stderr=subprocess.STDOUT, stdout=subprocess.PIPE
)
while True:
output = process.stdout.readline().decode("utf-8")
if output != "":
log(output.strip())
if process.poll() is not None:
break
except Exception as e:
log(f"ERROR: {fname} failed\n{e}")
return None
elapsed_time = datetime.datetime.now() - start_time
log(f"Elapsed time to upload {fname}: {elapsed_time}", True)
log(f"Return code for rcloning {fname}: {process.poll()}", True)
return process
def rclone_upload(fname, remote_dir, timeout: int = TIMEOUT):
"""Upload to rlcone"""
args = []
if timeout > 0:
args.extend(["timeout", str(timeout)])
args.extend(
[
RCLONE,
"copy",
"-vv",
"--size-only", # server doesn't do mtime
"--sftp-set-modtime=false", # server doesn't do mtime
"--inplace",
fname,
f"{RCLONE_REMOTE}:{ARCHIVE_PATH}{remote_dir}",
]
)
return run(fname, args)
def rclone_download(fname, remote_dir, timeout: int = TIMEOUT):
"""Download from rclone"""
args = []
if timeout > 0:
args.extend(["timeout", str(timeout)])
args.extend(
[
RCLONE,
"copy",
"-vv",
"--size-only", # server doesn't do mtime
"--sftp-set-modtime=false", # server doesn't do mtime
f"{RCLONE_REMOTE}:{ARCHIVE_PATH}{remote_dir}",
fname,
]
)
return run(fname, args)
def parse_cli():
global LOG_FILE
global RCLONE_REMOTE
global ARCHIVE_PATH
program = Path(__main__.__file__).stem
parser = argparse.ArgumentParser(
prog=program,
description="QA test Permanent rclone",
epilog="For challenging-names, id is a 3-digit number. For apod, it is a date in %Y-%m-%d format.",
)
if program == "upload-test":
parser.add_argument("directory")
parser.add_argument("--log-file", help=f"path to log file (defaults to {LOG_FILE})")
parser.add_argument(
"--omit",
help="specify file of ids to omit (misc and challenging-names)",
)
parser.add_argument("--only", help="only test one file id")
parser.add_argument(
"--remote",
help="Name of configured rclone remote such as permanent-prod or permanent-dev",
)
parser.add_argument("--archive-path", help="Archive path in Permanent.")
parser.add_argument(
"--remote-dir",
help="remote subdirectory (defaults to 'test-tree')",
default="test-tree",
)
parser.add_argument("--start", help="id of file to start from")
parser.add_argument(
"--timeout",
help="number of seconds to allow to upload a challenging-names or misc file",
default=str(TIMEOUT),
)
args = parser.parse_args()
if args.only:
args.only = f"{int(args.only):03}"
if args.start:
args.start = f"{int(args.start):03}"
if args.log_file:
LOG_FILE = args.log_file
if args.omit:
args.omit_files = slurp_if_e(args.omit).strip().split("\n")
if args.archive_path:
ARCHIVE_PATH = args.archive_path
if args.remote:
RCLONE_REMOTE = args.remote
else:
log("No rclone remote set. Attempting with default remote `permanent`...", True)
log(
"If the default remote `permanent` is not configured uploads would fail.",
True,
)
return args