-
Notifications
You must be signed in to change notification settings - Fork 4
/
cli.py
executable file
·313 lines (227 loc) · 8.71 KB
/
cli.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Console script for cmip6_object_store."""
import argparse
import os
import shutil
import sys
from cmip6_object_store import CONFIG, logging
from cmip6_object_store.cmip6_zarr.batch import BatchManager
from cmip6_object_store.cmip6_zarr.caringo_store import CaringoStore
from cmip6_object_store.cmip6_zarr.compare import compare_zarrs_with_ncs
from cmip6_object_store.cmip6_zarr.results_store import get_results_store, get_verification_store
from cmip6_object_store.cmip6_zarr.task import TaskManager
from cmip6_object_store.cmip6_zarr.intake_cat import create_intake_catalogue
from cmip6_object_store.cmip6_zarr.utils import (
get_credentials,
get_zarr_url,
)
LOGGER = logging.getLogger(__file__)
def _add_arg_parser_run(parser):
_add_arg_parser_project(parser, description="convert to Zarr in Object Store")
group = parser.add_mutually_exclusive_group()
group.add_argument(
"--slurm-array-member",
action="store_true",
required=False,
help=("Not for interactive use. \n"
"Batch number will be taken from SLURM_ARRAY_TASK_ID environment variable."),
)
group.add_argument(
"-b",
"--batches",
type=str,
default="all",
required=False,
help="Batches to run, default is 'all'. Also accepts comma separated "
"list of batch numbers and/or ranges specified with a hyphen. E.g: "
"'1,2,3' or '1-5'.",
)
parser.add_argument(
"-d",
"--datasets",
type=str,
default=None,
required=False,
help="Datasets to run. Also accepts comma separated "
"list of datasets. E.g.: -d cmip6...gn.v20190910,cmip6...v20200202",
)
parser.add_argument(
"-r",
"--run-mode",
type=str,
default="lotus",
required=False,
help="Mode to run in, either 'lotus' (default) or 'local'.",
)
def _range_to_list(range_string, sep):
start, end = [int(val) for val in range_string.split(sep)]
return list(range(start, end + 1))
def parse_args_run(args):
# Parse batches into a single value
slurm_array_member = args.slurm_array_member
batches = args.batches
datasets = args.datasets
if slurm_array_member:
batches = [int(os.environ["SLURM_ARRAY_TASK_ID"])]
elif batches == "all":
batches = None
else:
items = batches.split(",")
batches = []
for item in items:
if "-" in item:
batches.extend(_range_to_list(item, "-"))
else:
batches.append(int(item))
batches = sorted(list(set(batches)))
if datasets:
datasets = datasets.split(",")
return args.project, batches, datasets, args.run_mode
def run_main(args):
project, batches, datasets, run_mode = parse_args_run(args)
tm = TaskManager(project, batches=batches, datasets=datasets, run_mode=run_mode)
tm.run_tasks()
def _add_arg_parser_project(parser, description=""):
parser.add_argument(
"-p",
"--project",
type=str,
default=CONFIG["workflow"]["default_project"],
required=False,
help=f"Project {description}",
)
def parse_args_project(args):
return args.project
def _add_arg_parser_create(parser):
_add_arg_parser_project(parser)
parser.add_argument("--all", action="store_true",
help="include all datasets in batches (default is to check if already done)")
def create_main(args):
project = parse_args_project(args)
bm = BatchManager(project, exclude_done=not args.all)
bm.create_batches()
def _add_arg_parser_intake(parser):
_add_arg_parser_project(parser, description="to create intake catalog for")
parser.add_argument("--limit", type=int,
help="maximum number of datasets to include")
def intake_main(args):
project = parse_args_project(args)
create_intake_catalogue(project, limit=args.limit)
def _add_arg_parser_clean(parser):
_add_arg_parser_project(parser, description="to clean out directories for")
parser.add_argument(
"-D",
"--delete-objects",
action="store_true",
help="Delete all the objects in the Object Store - DANGER!!!",
)
parser.add_argument(
"-b",
"--buckets",
default=[],
nargs="*",
help="Identifiers of buckets TO DELETE!",
)
def parse_args_clean(args):
return args.project, args.delete_objects, args.buckets
def clean_main(args):
project, delete_objects, buckets_to_delete = parse_args_clean(args)
if delete_objects:
resp = input("DO YOU REALLY WANT TO DELETE THE BUCKETS? [Y/N] ")
if resp != "Y":
print("Exiting.")
sys.exit()
batch_dir = BatchManager(project)._version_dir
log_dir = os.path.join(CONFIG["log"]["log_base_dir"], project)
to_delete = [log_dir, batch_dir]
for dr in to_delete:
if os.path.isdir(dr):
LOGGER.warning(f"Deleting: {dr}")
shutil.rmtree(dr)
if buckets_to_delete:
LOGGER.warning("Starting to delete buckets from Object Store!")
caringo_store = CaringoStore(creds=get_credentials())
for bucket in buckets_to_delete:
LOGGER.warning(f"DELETING BUCKET: {bucket}")
caringo_store.delete(bucket)
def _add_arg_parser_list(parser):
_add_arg_parser_project(parser, description="to list directories for")
parser.add_argument(
"-c",
"--count-only",
action="store_true",
help="Only show the total count of records processed.",
)
def parse_args_list(args):
return args.project, args.count_only
def list_main(args):
project, count_only = parse_args_list(args)
results_store = get_results_store(project)
records = results_store.get_successful_runs()
if not count_only:
for _, dataset_id in records:
zarr_url = get_zarr_url(dataset_id, project)
print(f"Record: {zarr_url}")
print(f"\nTotal records: {len(records)}")
def verify_main(args):
project = parse_args_project(args)
results_store = get_results_store(project)
verified_store = get_verification_store(project)
successes, out_of = compare_zarrs_with_ncs(project, dataset_id=args.dataset)
print(f"\nVerified {successes} out of {out_of} datasets.")
print("\n\nResults of all verifications so far:")
n_verified_successes = verified_store.count_successes()
n_total_successes = results_store.count_successes()
n_total = results_store.count_results()
print(f"""{n_verified_successes} successfully verified
{n_total_successes} total claimed successes
{n_total} total results including failures""")
def show_errors_main(args):
project = parse_args_project(args)
results_store = get_results_store(project)
errors = results_store.get_failed_runs()
for dataset_id, error in errors.items():
print("\n===================================================")
print(f"{dataset_id}:")
print("===================================================\n")
print("\t" + error)
print(f"\nFound {len(errors)} errors.")
def main():
"""Console script for cmip6_object_store."""
main_parser = argparse.ArgumentParser()
main_parser.set_defaults(func=lambda args: main_parser.print_help())
subparsers = main_parser.add_subparsers()
run_parser = subparsers.add_parser("run")
_add_arg_parser_run(run_parser)
run_parser.set_defaults(func=run_main)
create_parser = subparsers.add_parser("create-batches")
_add_arg_parser_create(create_parser)
create_parser.set_defaults(func=create_main)
clean_parser = subparsers.add_parser("clean")
_add_arg_parser_clean(clean_parser)
clean_parser.set_defaults(func=clean_main)
list_parser = subparsers.add_parser("list")
_add_arg_parser_list(list_parser)
list_parser.set_defaults(func=list_main)
verify_parser = subparsers.add_parser("verify")
_add_arg_parser_project(verify_parser)
verify_parser.add_argument(
"-d",
"--dataset",
type=str,
default=None,
required=False,
help="Single dataset ID to verify (defaults to choosing a sample)"
)
verify_parser.set_defaults(func=verify_main)
intake_parser = subparsers.add_parser("create-intake")
_add_arg_parser_intake(intake_parser)
intake_parser.set_defaults(func=intake_main)
show_errors_parser = subparsers.add_parser("show-errors")
_add_arg_parser_project(show_errors_parser)
show_errors_parser.set_defaults(func=show_errors_main)
args = main_parser.parse_args()
args.func(args)
if __name__ == "__main__":
sys.exit(main()) # pragma: no cover