-
Notifications
You must be signed in to change notification settings - Fork 22
/
update_ondemand.py
executable file
·63 lines (49 loc) · 1.9 KB
/
update_ondemand.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
#!/usr/bin/env python3
"""Verify record fixture file regarding distribution.availability field.
If a dataset record contains 'ondemand' availability is found but the record
has attached some 'files' field, inform about this and update the file.
If a dataset record does not contain file and does not specify availability
field, inform about this and update the file.
Note: to be used mostly with CMS simulated datasets only.
"""
import json
import sys
import click
@click.command()
@click.argument("filename", type=click.Path(exists=True))
def verify_record_fixture_file(filename):
"""Verify record fixture file regarding ondemand availability.
If a dataset record contains 'ondemand' availability is found but the
record has attached some 'files' field, inform about this and update
the file.
If a dataset record does not contain file and does not specify availability
field, inform about this and update the file.
"""
with open(filename, "r") as fdesc:
records = json.loads(fdesc.read())
changes_needed = False
for record in records:
# get information
recid = record.get("recid")
availability = record.get("distribution", {}).get("availability", "")
files = record.get("files", None)
# check records
if availability == "ondemand" and files:
changes_needed = True
print(
"[ERROR] Record {0} is 'ondemand' but has 'files'.".format(recid),
file=sys.stderr,
)
del record["distribution"]["availability"]
if changes_needed:
new_content = json.dumps(
records,
indent=2,
sort_keys=True,
ensure_ascii=False,
separators=(",", ": "),
)
with open(filename, "w") as fdesc:
fdesc.write(new_content + "\n")
if __name__ == "__main__":
verify_record_fixture_file()