-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoffline_data.py
211 lines (175 loc) · 6.52 KB
/
offline_data.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""":Mod: offline_scan
:Synopsis:
:Author:
servilla
Duane Costa
:Created:
1/28/17
"""
from docopt import docopt
import json
import logging
from lxml import etree
import requests
import sys
logging.basicConfig(format='%(asctime)s %(levelname)s (%(name)s): %(message)s',
datefmt='%Y-%m-%d %H:%M:%S%z',
# filename='offline_data' + '.log',
level=logging.INFO)
logger = logging.getLogger('offline_data')
def get_identifiers(base_url=None, scope=None):
"""
Gets the list of identifiers for a given scope from PASTA
"""
try:
url = base_url + '/package/eml/' + scope
r = requests.get(url)
if r.status_code != requests.codes.ok:
logger.error('Bad status code ({code}) for {url}'.format(
code=r.status_code, url=url))
else:
return [_.strip() for _ in (r.text).split('\n')]
except Exception as e:
logger.error(e)
def get_newest_revision(base_url=None, scope=None, identifier=None):
"""
Gets the newest revision of a data package from PASTA
"""
url = base_url + '/package/eml/' + scope + '/' + identifier \
+ '?filter=newest'
try:
r = requests.get(url)
if r.status_code != requests.codes.ok:
logger.error(
'Bad status code ({code}) for {url}'.format(
code=r.status_code, url=url))
else:
return r.text.strip()
except Exception as e:
logger.error(e)
def get_scopes(base_url=None):
"""
Gets the complete list of scopes from PASTA
"""
try:
url = base_url + '/package/eml'
r = requests.get(url)
if r.status_code != requests.codes.ok:
logger.error('Bad status code ({code}) for {url}'.format(
code=r.status_code, url=url))
else:
return [_.strip() for _ in (r.text).split('\n')]
except Exception as e:
logger.error(e)
def package_id_to_path(package_id=None):
"""
Derives a slash-separated path from a dot-separated package ID
"""
return package_id.replace('.', '/')
def get_resource_dict(package_id,
resource_id,
object_name=None,
medium_name=None):
"""
Derives a resource_dict dictionary from the supplied package ID,
resource ID, and offline XML values
"""
resource_dict = {"package_id" : package_id,
"resource_id" : resource_id,
"object_name" : object_name,
"medium_name" : medium_name
}
return resource_dict
def scan_for_offline(base_url=None,
fp=sys.stdout,
scopes=None,
black_list=None):
"""
Scans data packages for offline entities
"""
offline = []
unparsed = []
for scope in scopes:
if scope not in black_list:
identifiers = get_identifiers(base_url=base_url, scope=scope)
for identifier in identifiers:
revision = get_newest_revision(base_url=base_url,
scope=scope,
identifier=identifier)
package_id = scope + '.' + identifier + '.' + revision
path = package_id_to_path(package_id)
metadata_url = base_url + '/package/metadata/eml/' + path
try:
r = requests.get(metadata_url)
if r.status_code == requests.codes.ok:
result_set = r.text.encode('utf-8')
tree = etree.fromstring(result_set)
phys = tree.findall('./dataset//physical')
for phy in phys:
distributions = phy.findall('.//distribution')
for distribution in distributions:
offline_elem = distribution.find('.//offline')
if offline_elem is not None:
medium_elem = offline_elem.find('.//mediumName')
medium_name = medium_elem.text
object_name_elem = phy.find('.//objectName')
object_name = object_name_elem.text
rdict = get_resource_dict(package_id,
metadata_url,
object_name,
medium_name)
offline.append(rdict)
else:
rdict = get_resource_dict(package_id, metadata_url, "", "")
unparsed.append(rdict)
except Exception as e:
logger.error(e)
scanned_resources = {}
scanned_resources["offline"] = offline
scanned_resources["unparsed"] = unparsed
json.dump(scanned_resources, fp, indent=2, separators=(',', ': '))
print('', file=fp)
def main():
"""
Reports on PASTA data packages with offline data entities.
Usage:
offline_data.py [-u | --url <url>]
[-s | --scope <scope>]
[-o | --output <output>]
offline_data.py -h | --help
Options:
-u --url Base URL of PASTA services,
e.g. 'https://pasta.lternet.edu'
-s --scope Restrict to given scope
-o --output Output results to file;
the filename should have a .json extension
-h --help This page
"""
black_list = ('lter-landsat', 'lter-landsat-ledaps', 'ecotrends')
args = docopt(str(main.__doc__))
url = args['<url>']
scope = args['<scope>']
output = args['<output>']
if not url:
BASE_URL = "http://localhost:8888"
else :
BASE_URL = url
if scope is None:
scopes = get_scopes(base_url=BASE_URL)
else:
scopes = [scope]
if output is None:
fp = sys.stdout
else:
fp = open(output, 'w')
scan_for_offline(base_url=BASE_URL,
fp=fp,
scopes=scopes,
black_list=black_list)
if (output):
fp.close()
logger.info("Finished program")
if __name__ == "__main__":
main()