-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcopy_annotations.py
More file actions
executable file
·281 lines (265 loc) · 12.5 KB
/
copy_annotations.py
File metadata and controls
executable file
·281 lines (265 loc) · 12.5 KB
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
#!/usr/bin/env python3
import argparse
import filecmp
import json
import os
import random
import shutil
import tempfile
try:
import girder_client.cli
except ImportError:
# this allows help to work even without proper installation
girder_client = None
def copy_folder(gcs, gcd, sparent, dparent, opts): # noqa
if (sparent['_modelType'] == 'folder' and dparent['_modelType'] == 'folder' and
len(sparent.get('meta', {}))):
# gcd.addMetadataToFolder(dparent['_id'], sparent.get('meta', {}))
gcd.post(
f'folder/{dparent["_id"]}/metadata',
data=json.dumps(sparent['meta']),
headers={'X-HTTP-Method': 'PUT', 'Content-Type': 'application/json'})
for sfolder in gcs.listFolder(sparent['_id'], sparent['_modelType']):
print('folder', sfolder['name'])
dfolder = gcd.createFolder(
dparent['_id'], sfolder['name'], sfolder['description'],
dparent['_modelType'], sfolder['public'], True)
copy_folder(gcs, gcd, sfolder, dfolder, opts)
if sparent['_modelType'] != 'folder':
return
for sitem in gcs.listItem(sparent['_id']):
if getattr(opts, 'substr', None) and opts.substr not in sitem['name']:
continue
print('item', gcs.get(f'resource/{sitem["_id"]}/path', parameters={'type': 'item'}))
ditem = gcd.createItem(
dparent['_id'], sitem['name'], sitem['description'], True)
if len(sitem.get('meta', {})):
# gcd.addMetadataToItem(ditem['_id'], sitem.get('meta', {}))
gcd.post(
f'item/{ditem["_id"]}/metadata',
data=json.dumps(sitem['meta']),
headers={'X-HTTP-Method': 'PUT', 'Content-Type': 'application/json'})
hasli = 'largeImage' in sitem and 'expected' not in sitem['largeImage']
setli = None
if len(list(gcs.listFile(sitem['_id']))) != len(list(gcd.listFile(ditem['_id']))):
present = list(gcd.listFile(ditem['_id']))
for file in gcs.listFile(sitem['_id']):
dfile = None
for pfile in present:
if pfile['name'] == file['name'] and pfile['size'] == file['size']:
dfile = pfile
break
if dfile is None:
dfile = direct_import(gcs, gcd, file, ditem, opts)
if dfile is not None:
print('file - import', file['name'], file.get('size'))
if dfile is None:
print('file', file['name'], file.get('size'))
if file.get('size') is None:
# TODO: Handle link files
continue
with tempfile.TemporaryDirectory() as tmpdirname:
temppath = os.path.join(tmpdirname, 'temp.tmp')
gcs.downloadFile(file['_id'], temppath)
dfile = gcd.uploadFileToItem(
ditem['_id'], temppath, mimeType=file['mimeType'],
filename=file['name'])
switch_to_import(gcd, ditem, dfile, temppath, opts)
if hasli and file['_id'] == sitem['largeImage'].get('fileId'):
setli = dfile
if setli:
ditem = gcd.createItem(
dparent['_id'], sitem['name'], sitem['description'], True)
if 'largeImage' not in ditem or ditem['largeImage'].get('fileId') != setli['_id']:
print('set largeImage fileId')
gcd.delete(f'item/{ditem["_id"]}/tiles')
gcd.post(f'item/{ditem["_id"]}/tiles', parameters={'fileId': setli['_id']})
if opts.no_annot:
continue
try:
copy_annotations(opts, gcs, gcd, sitem, ditem)
except Exception as e:
print('FAILED to copy annotations; disabling annotation copy')
print(e)
opts.no_annot = True
def switch_to_import(gc, item, file, temppath, opts):
if not opts.dest_import or not opts.local_path or not opts.import_base:
return
gpath = gc.get(f'resource/{item["_id"]}/path', parameters={'type': 'item'})
base = opts.import_base.rstrip('/') + '/'
if not gpath.startswith(base):
return
fragment = gpath[len(base):]
localdir = os.path.dirname(os.path.join(opts.local_path, fragment))
if os.path.exists(localdir) and not os.path.isdir(localdir):
return
if not os.path.exists(localdir):
try:
os.makedirs(localdir, exist_ok=True)
except Exception:
return
localpath = os.path.join(localdir, file['name'])
if os.path.exists(localpath):
if not os.path.isfile(localpath):
return
if not filecmp.cmp(localpath, temppath, shallow=False):
return
else:
shutil.copy(temppath, localpath)
gc.post(f'file/{file["_id"]}/import/adjust_path', parameters={'path': localpath})
def direct_import(gcs, gcd, file, ditem, opts):
if not opts.import_assetstore:
return None
if isinstance(opts.import_assetstore, str):
assetstores = [a for a in gcd.get('assetstore') if a['name'] == opts.import_assetstore]
if len(assetstores) == 1:
opts.import_assetstore = assetstores[0]
else:
opts.import_assetstore = None
return None
assetstore = opts.import_assetstore
try:
existing = gcs.get(f'resource/{file["_id"]}', parameters={'type': 'file'})
except Exception:
return None
if not existing.get('imported') or 'path' not in existing:
return None
try:
newfile = gcd.post(f'assetstore/{assetstore["_id"]}/import/single_path', parameters={
'path': existing['path'],
'itemId': ditem['_id'],
'name': file['name'],
'mimeType': file.get('mimeType'),
})
except Exception:
return None
return newfile
def copy_annotations(opts, gcs, gcd, sitem, ditem):
if opts.replace and len(gcd.get('annotation', parameters={'itemId': ditem['_id']})):
gcd.delete(f'annotation/item/{ditem["_id"]}')
if (not len(gcs.get('annotation', parameters={'itemId': sitem['_id']})) or
len(gcd.get('annotation', parameters={'itemId': ditem['_id']}))):
return
try:
print('get annotations')
ann = gcs.get('annotation/item/%s' % sitem['_id'], jsonResp=False).content
except Exception as e:
print(e)
return
print('put annotations')
gcd.post('annotation/item/%s' % ditem['_id'], data=ann)
def copy_data(opts):
print('Source')
gcs = girder_client.cli.GirderCli(
apiUrl=opts.src_api, username=opts.src_user, password=opts.src_password)
gcs.progressReporterCls = girder_client._NoopProgressReporter
print('Destination')
gcd = girder_client.cli.GirderCli(
apiUrl=opts.dest_api, username=opts.dest_user, password=opts.dest_password)
gcd.progressReporterCls = girder_client._NoopProgressReporter
copy_resource(gcs, gcd, opts.src_path, opts.dest_path, opts)
def copy_resource(gcs, gcd, src_path, dest_path, opts): # noqa
if dest_path.split(os.path.sep)[-1] == '.':
dest_path = os.path.sep.join(
dest_path.split(os.path.sep)[:-1] + src_path.split(os.path.sep)[-1:])
if src_path == '/':
for path in ['user', 'collection']:
copy_resource(
gcs, gcd, os.path.join(src_path, path), os.path.join(dest_path, path), opts)
if src_path.rstrip('/') in {'/user', '/collection'}:
try:
gcd.get('resource/lookup', parameters={'path': dest_path})
except Exception:
dparent = gcd.get('resource/lookup', parameters={'path': os.path.dirname(dest_path)})
gcd.createFolder(
dparent['_id'], os.path.basename(dest_path), '',
dparent['_modelType'], True, True)
if src_path.rstrip('/') == '/user':
for user in gcs.listUser():
user_path = gcs.get(f'resource/{user["_id"]}/path', parameters={'type': 'user'})
copy_resource(gcs, gcd, user_path, os.path.join(
dest_path, user_path.split(os.path.sep)[-1]), opts)
if src_path.rstrip('/') == '/collection':
for coll in gcs.listCollection():
coll_path = gcs.get(f'resource/{coll["_id"]}/path', parameters={'type': 'collection'})
copy_resource(gcs, gcd, coll_path, os.path.join(
dest_path, coll_path.split(os.path.sep)[-1]), opts)
if src_path in {'/', '/collection', '/user'}:
return
try:
stop = gcs.get('resource/lookup', parameters={'path': src_path})
except girder_client.HttpError:
print(f'Failed looking up {src_path}')
return
try:
dtop = gcd.get('resource/lookup', parameters={'path': dest_path})
except girder_client.HttpError:
dtop = None
if dtop is None and stop['_modelType'] in {'user', 'collection', 'folder'}:
dest_parts = dest_path.rstrip(os.path.sep).split(os.path.sep)
try:
dparent = gcd.get('resource/lookup', parameters={'path': os.path.dirname(dest_path)})
dtop = gcd.createFolder(
dparent['_id'], os.path.basename(dest_path), '',
dparent['_modelType'], True, True)
except girder_client.HttpError:
dparent = None
if dparent is None and len(dest_parts) >= 3 and dest_parts[0] == '' and (
dest_parts[1] == 'collection' or dest_parts[1] == stop['_modelType']):
try:
dtop = gcd.get('resource/lookup',
parameters={'path': os.path.sep.join(dest_parts[:3])})
except girder_client.HttpError:
dtop = None
if not dtop:
if dest_parts[1] == 'user':
dtop = gcd.createUser(
stop['login'], stop['email'], stop['firstName'],
stop['lastName'], str(random.random()), stop['admin'])
elif stop['_modelType'] == 'user':
dtop = gcd.createCollection(stop['login'], 'From user account', False)
else:
dtop = gcd.createCollection(stop['name'], stop['description'], stop['public'])
for part in dest_parts[3:]:
dparent = dtop
dtop = gcd.createFolder(
dparent['_id'], part, '',
dparent['_modelType'], True, True)
copy_folder(gcs, gcd, stop, dtop, opts)
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Copy folders, items, and annotations from one girder '
'server to another.')
parser.add_argument('--src-api', help='Source API url (through /api/v1).')
parser.add_argument('--dest-api', help='Destination API url (through /api/v1).')
parser.add_argument('--src-user', help='Source username.')
parser.add_argument('--dest-user', help='Destination username.')
parser.add_argument('--src-password', help='Source password.')
parser.add_argument('--dest-password', help='Destination password.')
parser.add_argument('--src-path', help='Source resource path.')
parser.add_argument('--replace', action='store_true', help='Replace all annotations.')
parser.add_argument('--no-annot', action='store_true', help='Do not copy annotations.')
parser.add_argument('--substr', help='All items must contain this substring.')
parser.add_argument(
'--dest-path', help='Destination resource path. If the last '
'component of this is ".", it is taken from the last component of '
'the source resource path.')
parser.add_argument(
'--dest-import', help='Prefix of the import path for individual '
'files. To use imports rather than uploads, this, local-path, and '
'import-base must be specified.')
parser.add_argument(
'--local-path', help='Prefix of the local path for importing '
'individual files. Files will be copied to this directory tree '
'rather than uploaded.')
parser.add_argument(
'--import-base', help='Prefix of the dest-path that is stripped off '
'before paths are added to the local path when importing instead of '
'uploading.')
parser.add_argument(
'--import-assetstore', help='Name of an assetstore to try to do '
'direct imports; if a file was imported on the source system and '
'can be imported to this assetstore on the destintation system, do '
'that in preference to downloading and uploading the file.')
opts = parser.parse_args()
copy_data(opts)