/
load.py
318 lines (283 loc) · 10.9 KB
/
load.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
"""
implementation of load cli command
"""
import sys
import gzip
import json
import requests
from datetime import datetime
import re
try:
from urllib.parse import urlparse
except ImportError:
from urlparse import urlparse
from ckanapi.errors import (NotFound, NotAuthorized, ValidationError,
SearchIndexError)
from ckanapi.cli import workers
from ckanapi.cli.utils import completion_stats, compact_json, quiet_int_pipe
try:
unicode
except NameError:
unicode = str
def load_things(ckan, thing, arguments,
worker_pool=None, stdin=None, stdout=None, stderr=None):
"""
create and update datasets, groups, orgs and users
The parent process creates a pool of worker processes and hands
out json lines to each worker as they finish a task. Status of
last record completed and records being processed is displayed
on stderr.
"""
if worker_pool is None:
worker_pool = workers.worker_pool
if stdin is None:
stdin = getattr(sys.stdin, 'buffer', sys.stdin)
if stdout is None:
stdout = getattr(sys.__stdout__, 'buffer', sys.__stdout__)
if stderr is None:
stderr = getattr(sys.stderr, 'buffer', sys.stderr)
if arguments['--worker']:
return load_things_worker(ckan, thing, arguments)
log = None
if arguments['--log']:
log = open(arguments['--log'], 'a')
jsonl_input = stdin
if arguments['--input']:
jsonl_input = open(arguments['--input'], 'rb')
if arguments['--gzip']:
jsonl_input = gzip.GzipFile(fileobj=jsonl_input)
def line_reader():
"""
handle start-record and max-records options
"""
start_record = int(arguments['--start-record'])
max_records = arguments['--max-records']
if max_records is not None:
max_records = int(max_records)
for num, line in enumerate(jsonl_input, 1): # records start from 1
if num < start_record:
continue
if max_records is not None and num >= start_record + max_records:
break
yield num, line
cmd = _worker_command_line(thing, arguments)
processes = int(arguments['--processes'])
if hasattr(ckan, 'parallel_limit'):
# add your sites to CKANAPI_MY_SITES instead of removing
processes = min(processes, ckan.parallel_limit)
stats = completion_stats(processes)
pool = worker_pool(cmd, processes, line_reader())
failures = 0
with quiet_int_pipe() as errors:
for job_ids, finished, result in pool:
if not result:
# child exited with traceback
return 1
timestamp, action, error, response = json.loads(
result.decode('utf-8'))
if error:
failures += 1
if not arguments['--quiet']:
stderr.write(('%s %s %s %s %s %s\n' % (
finished,
job_ids,
next(stats),
action,
error,
compact_json(response).decode('utf-8') if response else ''
)).encode('utf-8'))
if log:
log.write(compact_json([
timestamp,
finished,
action,
error,
response,
]) + b'\n')
log.flush()
if 'pipe' in errors:
return 1
if 'interrupt' in errors:
return 2
if failures:
return 3
def load_things_worker(ckan, thing, arguments,
stdin=None, stdout=None):
"""
a process that accepts lines of json on stdin which is parsed and
passed to the {thing}_create/update actions. it produces lines of json
which are the responses from each action call.
"""
if stdin is None:
stdin = getattr(sys.stdin, 'buffer', sys.stdin)
# hack so that pdb can be used in extension/ckan
# code called by this worker
try:
sys.stdin = open('/dev/tty', 'rb')
except IOError:
pass
if stdout is None:
stdout = getattr(sys.__stdout__, 'buffer', sys.__stdout__)
# hack so that "print debugging" can work in extension/ckan
# code called by this worker
sys.stdout = sys.stderr
thing_show, thing_create, thing_update = {
'datasets': (
'package_show', 'package_create', 'package_update'),
'groups': (
'group_show', 'group_create', 'group_update'),
'organizations': (
'organization_show', 'organization_create', 'organization_update'),
'users': (
'user_show', 'user_create', 'user_update'),
'related':(
'related_show','related_create','related_update'),
}[thing]
def reply(action, error, response):
"""
format messages to be sent back to parent process
"""
stdout.write(compact_json([
datetime.now().isoformat(),
action,
error,
response]) + b'\n')
stdout.flush()
for line in iter(stdin.readline, b''):
try:
obj = json.loads(line.decode('utf-8'))
except UnicodeDecodeError as e:
obj = None
reply('read', 'UnicodeDecodeError', unicode(e))
continue
requests_kwargs = None
if arguments['--insecure']:
requests_kwargs = {'verify': False}
if obj is not None:
existing = None
if not arguments['--create-only']:
# use either id or name to locate existing records
name = obj.get('id')
if name:
try:
existing = ckan.call_action(thing_show,
{'id': name,
'include_datasets': False,
'include_password_hash': True,
'include_users': True,
},
requests_kwargs=requests_kwargs)
except NotFound:
pass
except NotAuthorized as e:
reply('show', 'NotAuthorized', unicode(e))
continue
name = obj.get('name')
if not existing and name:
try:
existing = ckan.call_action(thing_show, {'id': name},
requests_kwargs=requests_kwargs)
except NotFound:
pass
except NotAuthorized as e:
reply('show', 'NotAuthorized', unicode(e))
continue
if existing:
_copy_from_existing_for_update(obj, existing, thing)
# FIXME: compare and reply when 'unchanged'?
if not existing and arguments['--update-only']:
reply('show', 'NotFound', [obj.get('id'), obj.get('name')])
continue
act = 'update' if existing else 'create'
try:
if existing:
r = ckan.call_action(thing_update, obj,
requests_kwargs=requests_kwargs)
else:
r = ckan.call_action(thing_create, obj)
if thing == 'datasets' and 'resources' in obj:# check if it is needed to upload resources when creating/updating packages
_upload_resources(ckan,obj,arguments)
elif thing in ['groups','organizations'] and 'image_display_url' in obj: #load images for groups and organizations
if arguments['--upload-logo']:
users = obj['users']
obj = _upload_logo(ckan,obj)
obj.pop('image_upload')
obj['users'] = users
ckan.call_action(thing_update, obj,
requests_kwargs=requests_kwargs)
except ValidationError as e:
reply(act, 'ValidationError', e.error_dict)
except SearchIndexError as e:
reply(act, 'SearchIndexError', unicode(e))
except NotAuthorized as e:
reply(act, 'NotAuthorized', unicode(e))
except NotFound:
reply(act, 'NotFound', obj)
else:
reply(act, None, r.get('name',r.get('id')))
def _worker_command_line(thing, arguments):
"""
Create a worker command line suitable for Popen with only the
options the worker process requires
"""
def a(name):
"options with values"
return [name, arguments[name]] * (arguments[name] is not None)
def b(name):
"boolean options"
return [name] * bool(arguments[name])
return (
['ckanapi', 'load', thing, '--worker']
+ a('--config')
+ a('--ckan-user')
+ a('--remote')
+ a('--apikey')
+ b('--create-only')
+ b('--update-only')
+ b('--upload-resources')
+ b('--upload-logo')
)
def _copy_from_existing_for_update(obj, existing, thing):
"""
modifies obj dict in place, copying values from existing.
the id is alwasys copied from existing to make sure update updates
the correct object.
users lists for groups and orgs are maintained if not present in obj
"""
if 'id' in existing:
obj['id'] = existing['id']
if thing in ('organizations', 'groups'):
if 'users' not in obj and 'users' in existing:
obj['users'] = existing['users']
def _upload_resources(ckan,obj,arguments):
resources = obj['resources']
if not arguments['--upload-resources']:
return
requests_kwargs = None
if arguments['--insecure']:
requests_kwargs = {'verify': False}
for resource in resources:
if resource.get('url_type') != 'upload':
continue
f = requests.get(resource['url'],stream=True)
name = resource['url'].rsplit('/',1)[-1]
ckan.call_action('resource_patch',
{'id':resource['id']},
files={'upload':(name, f.raw)},
requests_kwargs=requests_kwargs)
def _upload_logo(ckan,obj_orig):
obj = obj_orig.copy()
for key in obj_orig.keys():
if isinstance(obj[key],(dict,list)):
obj.pop(key) #dict/list objects can't be encoded
if urlparse(obj['image_url']).netloc: # logo is an external link
obj['clear_upload'] = True
obj['image_upload'] = obj['image_url']
else:
f = requests.get(obj['image_display_url'],stream=True)
name,ext = obj['image_url'].rsplit('.',1) #reformulate image_url for new site
new_name = re.sub('[0-9\.-]','',name)
new_url = new_name+'.'+ext
obj['image_upload'] = (new_url, f.raw)
ckan.action.group_update(**obj)
return obj