-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload_test_resources.py
172 lines (136 loc) · 5.3 KB
/
upload_test_resources.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
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
import logging
import sys
import traceback
from pathlib import Path
from quilt3 import Package
from bioio_sldy import __version__
###############################################################################
logging.basicConfig(
level=logging.INFO,
format="[%(levelname)4s: %(module)s:%(lineno)4s %(asctime)s] %(message)s",
)
log = logging.getLogger(__name__)
###############################################################################
# Args
class Args(argparse.Namespace):
def __init__(self):
self.__parse()
def __parse(self):
# Setup parser
p = argparse.ArgumentParser(
prog="upload_test_resources",
description=(
"Upload files used for testing this project. This will upload "
"whatever files are currently found in the `tests/resources` directory."
"To add more test files, simply add them to the `tests/resources` "
"directory and rerun this script."
),
)
# Arguments
p.add_argument(
"--dry-run",
action="store_true",
help=(
"Conduct dry run of the package generation. Will create a JSON "
"manifest file of that package instead of uploading."
),
)
p.add_argument(
"-y",
"--yes",
action="store_true",
dest="preapproved",
help="Auto-accept upload of files.",
)
p.add_argument(
"--debug",
action="store_true",
help="Show traceback if the script were to fail.",
)
# Parse
p.parse_args(namespace=self)
###############################################################################
# Build package
def upload_test_resources(args: Args):
# Try running the download pipeline
try:
# Get test resources dir
resources_dir = (
Path(__file__).parent.parent / "bioio_sldy" / "tests" / "resources"
).resolve(strict=True)
# Report with directory will be used for upload
log.info(f"Using contents of directory: {resources_dir}")
# Create quilt package
package = Package()
package.set_dir("resources", resources_dir)
# Report package contents
log.info(f"Package contents: {package}")
# Construct package name
package_name = "bioio_sldy/test_resources"
# Check for dry run
if args.dry_run:
# Attempt to build the package
top_hash = package.build(package_name)
# Get resolved save path
manifest_save_path = Path("upload_manifest.jsonl").resolve()
with open(manifest_save_path, "w") as manifest_write:
package.dump(manifest_write)
# Report where manifest was saved
log.info(f"Dry run generated manifest stored to: {manifest_save_path}")
log.info(f"Completed package dry run. Result hash: {top_hash}")
# Upload
else:
# Check pre-approved push
if args.preapproved:
confirmation = True
else:
# Get upload confirmation
confirmation = None
while confirmation is None:
# Get user input
user_input = input("Upload [y]/n? ")
# If the user simply pressed enter assume yes
if len(user_input) == 0:
user_input = "y"
# Get first character and lowercase
else:
user_input = user_input[0].lower()
# Set confirmation from None to a value
if user_input == "y":
confirmation = True
elif user_input == "n":
confirmation = False
# Check confirmation
if confirmation:
pushed = package.push(
package_name,
"s3://bioio-dev-test-resources",
message=f"Test resources for `bioio_sldy` version: {__version__}.",
)
log.info(f"Completed package push. Result hash: {pushed.top_hash}")
# Update hash file
with open(Path(__file__).parent / "TEST_RESOURCES_HASH.txt", "w") as hash_file:
hash_file.write(pushed.top_hash)
else:
log.info(f"Upload canceled.")
# Catch any exception
except Exception as e:
log.error("=============================================")
if args.debug:
log.error("\n\n" + traceback.format_exc())
log.error("=============================================")
log.error("\n\n" + str(e) + "\n")
log.error("=============================================")
sys.exit(1)
###############################################################################
# Runner
def main():
args = Args()
upload_test_resources(args)
###############################################################################
# Allow caller to directly run this module (usually in development scenarios)
if __name__ == "__main__":
main()