-
Notifications
You must be signed in to change notification settings - Fork 11
/
run.py
executable file
·308 lines (270 loc) · 8.59 KB
/
run.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
#!/usr/bin/env python3
import argparse
import json
import sys
import requests
from helpers.config import Config
from transfer.analysis import sync_analysis_data
from transfer.asset import transfer_asset, get_src_asset_details, create_asset
from transfer.media import get_media, del_media
from transfer.xml import (
get_src_submissions_xml,
get_submission_edit_data,
print_stats,
transfer_submissions,
)
from transfer.validation_status import sync_validation_statuses
def get_uuids(config_loc, params):
def get_uuids_rec(uuids=[], url=None, params=None, headers=None):
if 'fields' not in url:
res = requests.get(url=url, params=params, headers=headers)
else:
res = requests.get(url=url, headers=headers)
data = res.json()
uuids += [i['_uuid'] for i in data['results']]
next_ = data['next']
if next_ is not None:
get_uuids_rec(uuids, next_, headers=headers)
uuids = []
get_uuids_rec(
uuids=uuids,
url=config_loc['data_url'],
params=params,
headers=config_loc['headers'],
)
return uuids
def chunker(seq, size):
for i in range(0, len(seq), size):
yield seq[i : i + size]
def get_params(uuids=[], limit=10000, fields=[]):
query = json.dumps({"_uuid": {"$in": uuids}})
res = {
'format': 'json',
'limit': limit,
'fields': json.dumps(fields),
}
if uuids:
res['query'] = query
return res
def get_diff_uuids(config):
# TODO: make limit dynamic
params = get_params(fields=['_uuid'], limit=1000)
src_uuids = get_uuids(config_loc=config.src, params=params)
dest_uuids = get_uuids(config_loc=config.dest, params=params)
return list(set(src_uuids).difference(set(dest_uuids)))
def main(
limit,
asset=False,
src_asset_uid=None,
last_failed=False,
keep_media=False,
regenerate=False,
quiet=False,
validate=True,
sync=False,
validation_statuses=False,
analysis_data=False,
chunk_size=100,
config_file=None,
skip_media=False,
):
if src_asset_uid:
validate = False
config = Config(config_file=config_file, validate=validate, asset=asset)
if src_asset_uid:
config.update_config(loc='src', new_data={'asset_uid': src_asset_uid})
if asset:
print('📋 Transferring asset, versions and form media')
asset_setup_content, *_ = get_src_asset_details(config_src=config.src)
asset_uid = create_asset(config.dest, asset_setup_content)
print(f'✨ New asset UID at `dest`: {asset_uid}')
config.update_config(loc='dest', new_data={'asset_uid': asset_uid})
transfer_asset(config)
if validation_statuses and not sync:
print('✏️ Syncing validation statuses')
sync_validation_statuses(config, chunk_size, limit)
sys.exit()
if analysis_data and not sync:
print('📶 Syncing analysis data')
sync_analysis_data(config, limit)
sys.exit()
config_src = config.src
all_results = []
submission_edit_data = get_submission_edit_data()
def transfer(all_results, url=None):
parsed_xml = get_src_submissions_xml(xml_url=url)
submissions = parsed_xml.findall(f'results/')
next_ = parsed_xml.find('next').text
results = transfer_submissions(
submissions,
submission_edit_data,
quiet=quiet,
regenerate=regenerate,
)
all_results += results
if next_ != 'None' and next_ is not None:
transfer(all_results, next_)
xml_url_src = config_src['xml_url'] + f'?limit={limit}'
if last_failed and config.last_failed_uuids:
xml_url_src += f'&query={json.dumps(config.data_query)}'
if sync:
print('🪪 Getting _uuid values from src and dest projects')
diff_uuids = get_diff_uuids(config)
if not diff_uuids:
print('👌 Projects are in-sync')
sys.exit()
# run through chunks of uuids
first_run = True
for chunked_uuids in chunker(diff_uuids, chunk_size):
query = json.dumps({"_uuid": {"$in": chunked_uuids}})
xml_url_src = (
config_src['xml_url'] + f'?limit={limit}&query={query}'
)
if not skip_media:
if first_run:
print('📸 Getting all submission media', end=' ', flush=True)
get_media(query=query)
if first_run:
print('📨 Transferring submission data')
first_run = False
transfer(all_results, xml_url_src)
if validation_statuses:
print('✏️ Syncing validation statuses')
sync_validation_statuses(config, chunk_size, limit)
if analysis_data:
print('📶 Syncing analysis data')
sync_analysis_data(config, limit)
if not sync:
if not skip_media:
print('📸 Getting all submission media', end=' ', flush=True)
get_media()
print('📨 Transferring submission data')
transfer(all_results, xml_url_src)
if not keep_media and not skip_media:
del_media()
print('✨ Done')
print_stats(all_results)
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='A CLI tool to transfer submissions between projects with identical XLSForms.'
)
parser.add_argument(
'--limit',
'-l',
default=5000,
type=int,
help='Number of submissions included in each batch for download and upload.',
)
parser.add_argument(
'--asset',
'-a',
default=False,
action='store_true',
help='Transfer asset, versions and form media.',
)
parser.add_argument(
'--src-asset-uid',
'-sau',
default=None,
type=str,
help='Override asset_uid value in config file.',
)
parser.add_argument(
'--last-failed',
'-lf',
default=False,
action='store_true',
help='Run transfer again with only last failed submissions.',
)
parser.add_argument(
'--config-file',
'-c',
default='config.json',
type=str,
help='Location of config file.',
)
parser.add_argument(
'--regenerate-uuids',
'-R',
default=False,
action='store_true',
help='Regenerate submission UUIDs.',
)
parser.add_argument(
'--no-validate',
'-N',
default=False,
action='store_true',
help='Skip validation of config file.',
)
parser.add_argument(
'--keep-media',
'-k',
default=False,
action='store_true',
help='Keep submission attachments rather than cleaning up after transfer.',
)
parser.add_argument(
'--sync',
'-s',
default=False,
action='store_true',
help='Sync src and dest project data',
)
parser.add_argument(
'--validation-statuses',
'-vs',
default=False,
action='store_true',
help='Sync src and dest validation statuses',
)
parser.add_argument(
'--analysis-data',
'-ad',
default=False,
action='store_true',
help='Sync src and dest analysis data (transcript, translations, analysis questions)',
)
parser.add_argument(
'--chunk-size',
'-cs',
default=20,
type=int,
help='Number of submissions included in each batch for sync query filters.',
)
parser.add_argument(
'--skip-media',
'-sm',
default=False,
action='store_true',
help='Skip media downloads',
)
parser.add_argument(
'--quiet',
'-q',
default=False,
action='store_true',
help='Suppress stdout',
)
args = parser.parse_args()
try:
main(
limit=args.limit,
asset=args.asset,
src_asset_uid=args.src_asset_uid,
last_failed=args.last_failed,
regenerate=args.regenerate_uuids,
keep_media=args.keep_media,
quiet=args.quiet,
validate=not args.no_validate,
sync=args.sync,
validation_statuses=args.validation_statuses,
analysis_data=args.analysis_data,
chunk_size=args.chunk_size,
config_file=args.config_file,
skip_media=args.skip_media,
)
except KeyboardInterrupt:
print('🛑 Stopping run')
# Do something here so we can pick up again where this leaves off
sys.exit()