-
Notifications
You must be signed in to change notification settings - Fork 1
/
validate.py
executable file
·96 lines (83 loc) · 3.11 KB
/
validate.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
#!/usr/bin/env python3
import os
import json
def log(message):
print(f"[INFO] {message}")
def log_error(message):
print(f"[ERROR] {message}")
results["errors"].append(message)
# Load the configuration
log("Loading configuration from config.json")
with open('config.json', encoding='utf8') as config_json:
config = json.load(config_json)
results = {"errors": [], "warnings": [], "datatype_tags": []}
# Ensure secondary and output directories exist
for directory in ["secondary", "output"]:
if not os.path.exists(directory):
os.mkdir(directory)
log(f"Created directory: {directory}")
else:
log(f"Directory already exists: {directory}")
# Specifications for required and optional files for each datatype tag
specs = {
"2phasemag": {"required": [
"phase1", "phase1_json",
"phase2", "phase2_json",
"magnitude1", "magnitude1_json",
"magnitude2", "magnitude2_json"
], "optional": []},
"phasediff": {"required": [
"phasediff", "phasediff_json",
"magnitude1", "magnitude1_json"
], "optional": [
"magnitude2", "magnitude2_json"
]},
"single": {"required": [
"fieldmap", "fieldmap_json",
"magnitude", "magnitude_json"
], "optional": []},
"pepolar": {"required": [
"epi1", "epi1_json",
"epi2", "epi2_json"
], "optional": []}
}
# Determine the type based on datatype_tags
log("Determining the datatype tag based on 'datatype_tags'")
datatype_tags = config["_inputs"][0].get("datatype_tags", [])
valid_tag = None
for tag in specs.keys():
if tag in datatype_tags:
valid_tag = tag
results["datatype_tags"].append(tag)
log(f"Detected valid datatype tag: {tag}")
break
if not valid_tag:
log_error(f"No valid datatype tag found; expected one of: {list(specs.keys())}")
else:
# Validate required files for the identified datatype tag
log(f"Validating required files for datatype tag: {valid_tag}")
spec = specs[valid_tag]
has_all_files = True
for key in spec["required"]:
if key not in config or not os.path.exists(config[key]):
log_error(f"Missing required file: {key}")
has_all_files = False
else:
log(f"Found required file: {config[key]}")
# Copy required and optional files to the output directory
if has_all_files:
log("Copying required and optional files to 'output' directory")
for key in spec["required"] + spec["optional"]:
if key in config and os.path.exists(config[key]):
basename = os.path.basename(config[key])
output_path = os.path.join("output", basename)
if os.path.lexists(output_path):
os.remove(output_path)
log(f"Removed existing symlink: {output_path}")
os.symlink("../" + config[key], output_path)
log(f"Created symlink for {config[key]} at {output_path}")
# Write results to product.json
log("Writing results to product.json")
with open("product.json", "w") as fp:
json.dump(results, fp)
log("Validation complete.")