-
Notifications
You must be signed in to change notification settings - Fork 1
/
ingest.py
executable file
·155 lines (141 loc) · 4.9 KB
/
ingest.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
#!/usr/bin/env python
# Copyright 2021 Scott Wales
# author: Scott Wales <scott.wales@unimelb.edu.au>
# modified by: Paola Petrelli <paola.petrelli@utas.edu.au>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This code build a catalogue.json and catalogue.csv.xz file list for each dataset
# Last modified:
# 2022/06/21
import yaml
import subprocess
import tempfile
import re
import lzma
import csv
import shlex
import json
import jsonschema
# import dataset configuration from ingest.yaml
with open("ingest.yaml") as f:
config = yaml.safe_load(f)
# define schema for catalogue.json file
schema = {
'type': 'object',
'properties': {
'id': {'type': 'string'},
'title': {'type': 'string'},
'description': {'type': 'string'},
'find': {
'type': 'object',
'properties': {
'paths': {
'type': 'array',
'items': {'type': 'string'}
},
'options': {'type': 'string'}
},
'required': ['paths'],
},
'drs': {'type': 'string'},
'catalogue_file': {'type': 'string'},
'assets': {'type': 'object'},
'aggregation_control': {'type': 'object'},
'rename': {
'type': 'object',
'patternProperties': {
'.*': {
'type': 'object',
'patternProperties': {
'.*': {'type': 'string'}
},
},
},
},
'postprocess': {'type': 'object'},
},
'required': ['id','description','find','drs'],
}
# if catalogue.csv.xz already exists for dataset change find definition in schema
if 'catalog_file' in [k for k in config.keys()]:
print('Existing catalogue_file for dataset')
# Get the column names from the catalogue file
with lzma.open(config['catalog_file'], mode='rt') as f:
header = f.readline()
columns = header.replace("\n","").split(",")
else:
jsonschema.validate(config, schema)
# compile the regex for DRS path
drs_re = re.compile(config["drs"], re.VERBOSE)
#print(f'drs_re: {drs_re}')
# define find command to list all files based on options listed in dataset configuration
find_command = [
"/bin/find",
*config["find"]["paths"],
*shlex.split(config["find"].get("options", "")),
]
print(shlex.join(find_command))
# open catalogue.csv.xz file
with tempfile.TemporaryFile('w+') as f, tempfile.TemporaryFile('w+') as s, lzma.open(
"catalogue.csv.xz", mode="wt", newline=""
) as out, lzma.open('errors.xz', mode='wt') as e:
# Find files
print("Finding Files...")
find = subprocess.run(find_command, stdout=f)
find.check_returncode()
f.seek(0)
# Sort the results
print("Sorting Files...")
sort = subprocess.run(["/bin/sort"], stdin=f, stdout=s)
sort.check_returncode()
s.seek(0)
# Get the column names
columns = None
for path in s:
match = drs_re.match(path)
if match is None:
continue
columns = list(match.groupdict().keys())
break
s.seek(0)
# Write catalogue.csv.xz
print("Writing Catalogue...")
csv_w = csv.DictWriter(out, columns, dialect='unix')
csv_w.writeheader()
for path in s:
match = drs_re.match(path)
if match is None:
print('ERROR',path.strip())
e.write(path)
continue
record = match.groupdict()
# rename columns if defined in config
for col, renames in config.get('rename', {}).items():
if record.get(col, None) in renames:
record[col] = renames[record[col]]
csv_w.writerow(record)
# Drop extra config items
config.pop('find')
config.pop('drs')
config.pop('rename', None)
config.pop('postprocess', None)
# Setup catalogue.json
config['esmcat_version'] = '0.1.0'
config['catalog_file'] = 'catalogue.csv.xz'
config['attributes'] = [{'column_name': c} for c in columns if c != config['assets']['column_name']]
# write catalogue.json file
with open('catalogue.json', 'w') as f:
print("cat.json open")
json.dump(config,f,indent=2)
print("")