-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackage_tracker.py
227 lines (182 loc) · 7.25 KB
/
package_tracker.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""":Mod: package_tracker
:Synopsis:
:Author:
servilla
:Created:
12/15/18
"""
import logging
from urllib.parse import quote
import click
import daiquiri
from lxml import etree
import requests
import pendulum
daiquiri.setup(level=logging.INFO,
outputs=(
'stderr',
))
logger = daiquiri.getLogger('package_tracker: ' + __name__)
INDENT = ' ' * 4
def cn_report(resources: list, d1_url: str) -> str:
report = f'D1 CN ({d1_url}) objects and replica verify times:\n'
for resource in resources:
success, response = get_d1_sysmeta(resource, d1_url)
if success:
dt_utc = pendulum.parse(get_d1_date_replica_verified(response))
date_verified = dt_utc.to_iso8601_string()
report += f'{INDENT}{resource} - {date_verified}\n'
else:
report += f'{INDENT}{resource} - {response}\n'
report += f'D1 CN ({d1_url}) science metadata indexed:\n'
for resource in resources:
if 'metadata/eml' in resource:
success, response = get_d1_solr_result(resource, d1_url)
if success:
solr_count = get_d1_solr_count(response)
if solr_count >= 1:
report += f'{INDENT}{resource} is indexed'
else:
report += f'{INDENT}{resource} is NOT indexed'
return report
def get_d1_date_uploaded(sysmeta_xml: str) -> str:
root = etree.fromstring(sysmeta_xml.encode('utf-8'))
date_uploaded = root.find('.//dateUploaded')
return date_uploaded.text
def get_d1_date_replica_verified(sysmeta_xml: str) -> str:
root = etree.fromstring(sysmeta_xml.encode('utf-8'))
date_verified = root.find('.//replicaVerified')
return date_verified.text
def get_d1_solr_count(solr_xml: str) -> int:
root = etree.fromstring(solr_xml.encode('utf-8'))
result = root.find('.//result')
return int(result.get('numFound'))
def get_d1_solr_result(pid: str, d1_url: str) -> tuple:
pid = quote(f'"{pid}"', safe='')
url = f'{d1_url}/query/solr/?start=0&rows=10&fl=id%2Ctitle%2CformatId&q=id%3A{pid}'
r = requests.get(url)
if r.status_code == requests.codes.ok:
return True, r.text
elif r.status_code == requests.codes.not_found:
return False, 'Not Found'
elif r.status_code == requests.codes.unauthorized:
return False, 'Unauthorized'
else:
return False, f'Unknown error with status code: {r.status_code}'
def get_d1_sysmeta(pid: str, d1_url: str) -> tuple:
pid = quote(pid, safe='')
url = f'{d1_url}/meta/{pid}'
r = requests.get(url)
if r.status_code == requests.codes.ok:
return True, r.text
elif r.status_code == requests.codes.not_found:
return False, 'Not Found'
elif r.status_code == requests.codes.unauthorized:
return False, 'Unauthorized'
else:
return False, f'Unknown error with status code: {r.status_code}'
def get_package_doi(pid: list, pasta_url: str, auth: tuple) -> str:
url = pasta_url + f'/doi/eml/{pid[0]}/{pid[1]}/{pid[2]}'
r = requests.get(url=url, auth=auth)
if r.status_code == requests.codes.ok:
return r.text
else:
return 'None'
def get_resource_create_date(resource_xml: str) -> str:
root = etree.fromstring(resource_xml.encode('utf-8'))
date_created = root.find('.//dateCreated')
return date_created.text
def get_resource_metadata(pid: list, pasta_url: str, auth: tuple) -> str:
url = pasta_url + f'/rmd/eml/{pid[0]}/{pid[1]}/{pid[2]}'
r = requests.get(url=url, auth=auth)
r.raise_for_status()
return r.text
def get_resources(pid: list, pasta_url: str, auth: tuple) -> tuple:
url = pasta_url + f'/eml/{pid[0]}/{pid[1]}/{pid[2]}'
r = requests.get(url=url, auth=auth)
if r.status_code == requests.codes.ok:
return True, r.text
elif r.status_code == requests.codes.not_found:
return False, 'Not Found'
elif r.status_code == requests.codes.unauthorized:
return False, 'Unauthorized'
else:
return False, f'Unknown error with status code: {r.status_code}'
def gmn_report(resources: list, gmn_url: str) -> str:
report = f'D1 GMN ({gmn_url}) objects and upload times:\n'
for resource in resources:
success, response = get_d1_sysmeta(resource, gmn_url)
if success:
dt_utc = pendulum.parse(get_d1_date_uploaded(response))
date_uploaded = dt_utc.to_iso8601_string()
report += f' {resource} - {date_uploaded}\n'
else:
report += f' {resource} - {response}\n'
return report
def pasta_report(pid: str, resources: list, date_created_raw: str) -> str:
local_tz = 'America/Denver'
utc_tz = pendulum.timezone('UTC')
dt_mt = pendulum.parse(date_created_raw, tz=local_tz)
dt_utc = pendulum.instance(utc_tz.convert(dt_mt))
date_created_mt = dt_mt.to_iso8601_string()
date_created_utc = pendulum.parse(dt_utc.to_iso8601_string()).to_iso8601_string()
pid = '.'.join(pid)
report = f'Package Identifier: {pid}\n'
report += f'Created: {date_created_utc} ({date_created_mt} - {local_tz})\n'
report += f'DOI: {resources[-1]}\n'
report += f'Resources:\n'
for resource in resources[:-1]:
report += f'{INDENT}{resource}\n'
return report
auth_help = 'Basic authentication "user:password"'
env_help = 'PASTA+ environment: production, staging, or development; default is production'
@click.command()
@click.argument('pid')
@click.option('-e', '--env', default='production', help=env_help)
@click.option('-a', '--auth', default=None, help=auth_help)
def track(pid: str, env: str, auth: str):
pid = pid.split('.')
if len(pid) != 3:
msg = f'Package identifier "{pid}" not in correct form'
raise ValueError(msg)
if pid[0] == 'edi':
gmn_host = 'edirepository.org'
else:
gmn_host = 'lternet.edu'
if env == 'production':
pasta_url = 'https://pasta.lternet.edu/package'
gmn_url = f'https://gmn.{gmn_host}/mn/v2'
d1_url = 'https://cn.dataone.org/cn/v2'
elif env == 'staging':
pasta_url = 'https://pasta-s.lternet.edu/package'
gmn_url = f'https://gmn-s.{gmn_host}/mn/v2'
d1_url = 'https://cn-stage.test.dataone.org/cn/v2'
elif env == 'development':
pasta_url = 'https://pasta-d.lternet.edu/package'
gmn_url = None
d1_url = None
else:
raise ValueError(f'Unknown environment "{env}"')
if auth is not None:
_ = auth.split(':')
auth = (_[0], _[1])
report = f'**** PASTA Data Package Report ****\n'
success, response = get_resources(pid, pasta_url, auth)
if success:
resources = response.strip().split('\n')
resources[-1] = get_package_doi(pid, pasta_url, auth)
resource_metadata = get_resource_metadata(pid, pasta_url, auth)
date_created = get_resource_create_date(resource_metadata)
report += pasta_report(pid, resources, date_created)
report += gmn_report(resources, gmn_url)
report += cn_report(resources, d1_url)
else:
pid = '.'.join(pid)
report += f'Package Identifier: {pid}\n'
report += f'Status: {response}\n'
click.echo(report)
exit(0)
if __name__ == "__main__":
track()