forked from irods/python-irodsclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_object_manager.py
849 lines (719 loc) · 33.4 KB
/
data_object_manager.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
from __future__ import absolute_import
import ast
import collections
import io
import json
import logging
import os
import six
import weakref
from irods.models import DataObject, Collection
from irods.manager import Manager
from irods.manager._internal import _api_impl, _logical_path
from irods.message import (
iRODSMessage, FileOpenRequest, ObjCopyRequest, StringStringMap, DataObjInfo, ModDataObjMeta,
DataObjChksumRequest, DataObjChksumResponse, RErrorStack, STR_PI
)
import irods.exception as ex
from irods.api_number import api_number
from irods.collection import iRODSCollection
from irods.data_object import (
iRODSDataObject, iRODSDataObjectFileRaw, chunks, irods_dirname, irods_basename)
import irods.client_configuration as client_config
import irods.keywords as kw
import irods.parallel as parallel
from irods.parallel import deferred_call
logger = logging.getLogger(__name__)
_update_types = []
_update_functions = weakref.WeakKeyDictionary()
def register_update_instance(object_, updater): # updater
_update_functions[object_] = updater
def register_update_type(type_, factory_):
"""
Create an entry corresponding to a type_ of instance to be allowed among updatables, with processing
based on the factory_ callable.
Parameters:
type_ : a type of instance to be allowed in the updatables parameter.
factory_ : a function accepting the instance passed in, and yielding an update callable.
If None, then remove the type from the list.
"""
# Delete if already present in list
z = tuple(zip(*_update_types))
if z and type_ in z[0]:
_update_types.pop(z[0].index(type_))
# Rewrite the list
# - with the new item introduced at the start of the list but otherwise in the same order, and
# - preserving only pairs that do not contain 'None' as the second member.
_update_types[:] = list((k,v) for k,v in collections.OrderedDict([(type_,factory_)] + _update_types).items() if v is not None)
def unregister_update_type(type_):
"""
Remove type_ from the listof recognized updatable types maintained by the PRC.
"""
register_update_type(type_, None)
def do_progress_updates(updatables, n, logging_function = logger.warning):
"""
Used internally by Python iRODS Client's data transfer routines (put, get) to iterate through updatables to be processed.
This, in turn, should cause the underlying corresponding progress bars or indicators to be updated.
"""
if not isinstance(updatables, (list,tuple)):
updatables = [updatables]
for object_ in updatables:
# If an updatable is directly callable, we set that up to be called without further ado.
if callable(object_):
update_func = object_
else:
# If not, we search for a registered type that matches object_ and register (or look up if previously registered) a factory-produced updater for that instance.
# Examine the unit tests for issue #574 in data_obj_test.py for factory examples.
update_func = _update_functions.get(object_)
if not update_func:
# search based on type
for class_,factory_ in _update_types:
if isinstance(object_,class_):
update_func = factory_(object_)
register_update_instance(object_, update_func)
break
else:
logging_function("Could not derive an update function for: %r",object_)
continue
# Do the update.
if update_func: update_func(n)
def call___del__if_exists(super_):
"""
Utility method to call __del__ if it exists anywhere in superclasses' MRO (method
resolution order).
"""
next_finalizer_in_MRO = getattr(super_,'__del__',None)
if next_finalizer_in_MRO:
next_finalizer_in_MRO()
class ManagedBufferedRandom(io.BufferedRandom):
def __init__(self,*a,**kwd):
# Help ensure proper teardown sequence by storing a reference to the session,
# if provided via keyword '_session'.
self._iRODS_session = kwd.pop('_session',None)
super(ManagedBufferedRandom,self).__init__(*a,**kwd)
import irods.session
with irods.session._fds_lock:
irods.session._fds[self] = None
def __del__(self):
if not self.closed:
self.close()
call___del__if_exists(super(ManagedBufferedRandom,self))
MAXIMUM_SINGLE_THREADED_TRANSFER_SIZE = 32 * ( 1024 ** 2)
DEFAULT_NUMBER_OF_THREADS = 0 # Defaults for reasonable number of threads -- optimized to be
# performant but allow no more worker threads than available CPUs.
DEFAULT_QUEUE_DEPTH = 32
logger = logging.getLogger(__name__)
class Server_Checksum_Warning(Exception):
"""Error from iRODS server indicating some replica checksums are missing or incorrect."""
def __init__(self,json_response):
"""Initialize the exception object with a checksum field from the server response message."""
super(Server_Checksum_Warning,self).__init__()
self.response = json.loads(json_response)
class DataObjectManager(Manager):
READ_BUFFER_SIZE = 1024 * io.DEFAULT_BUFFER_SIZE
WRITE_BUFFER_SIZE = 1024 * io.DEFAULT_BUFFER_SIZE
# Data object open flags (independent of client os)
O_RDONLY = 0
O_WRONLY = 1
O_RDWR = 2
O_APPEND = 1024
O_CREAT = 64
O_EXCL = 128
O_TRUNC = 512
def should_parallelize_transfer( self,
num_threads = 0,
obj_sz = 1+MAXIMUM_SINGLE_THREADED_TRANSFER_SIZE,
server_version_hint = (),
measured_obj_size = (), ## output variable. If a list is provided, it shall
# be truncated to contain one value, the presumed size of the
# file or data object - if one is provided for `obj_sz'. That size is
# determined normally by stat'ing (using a seek-to-end operation) but
# can be overridden using the DATA_SIZE_KW option.
open_options = () # If this parameter is of type dict, set the DATA_SIZE_KW item from
# the stat'ed size
):
provided_data_size = dict(open_options).get(kw.DATA_SIZE_KW)
# Allow an environment variable to override the detection of the server version.
# Example: $ export IRODS_VERSION_OVERRIDE="4,2,9" ; python -m irods.parallel ...
server_version = ( ast.literal_eval(os.environ.get('IRODS_VERSION_OVERRIDE', '()' )) or server_version_hint or
self.server_version )
size = None
try:
if num_threads == 1 or ( server_version < parallel.MINIMUM_SERVER_VERSION ):
return False
if getattr(obj_sz,'seek',None):
if provided_data_size is not None:
size = int(provided_data_size)
else:
pos = obj_sz.tell()
size = obj_sz.seek(0,os.SEEK_END)
if not isinstance(size,six.integer_types):
size = obj_sz.tell()
obj_sz.seek(pos,os.SEEK_SET)
if isinstance(measured_obj_size,list):
measured_obj_size[:] = [size]
return size > MAXIMUM_SINGLE_THREADED_TRANSFER_SIZE
elif isinstance(obj_sz,six.integer_types):
return obj_sz > MAXIMUM_SINGLE_THREADED_TRANSFER_SIZE
message = 'obj_sz of {obj_sz!r} is neither an integer nor a seekable object'.format(**locals())
raise RuntimeError(message)
finally:
if size is not None and isinstance(open_options,dict):
open_options[kw.DATA_SIZE_KW] = size
def _download(self, obj, local_path, num_threads, updatables = (), **options):
"""Transfer the contents of a data object to a local file.
Called from get() when a local path is named.
"""
if os.path.isdir(local_path):
local_file = os.path.join(local_path, irods_basename(obj))
else:
local_file = local_path
# Check for force flag if local_file exists
if os.path.exists(local_file) and kw.FORCE_FLAG_KW not in options:
raise ex.OVERWRITE_WITHOUT_FORCE_FLAG
data_open_returned_values_ = {}
with open(local_file, 'wb') as f:
with self.open(obj, 'r', returned_values = data_open_returned_values_, **options) as o:
if self.should_parallelize_transfer (num_threads, o, open_options = options.items()):
f.close()
if not self.parallel_get( (obj,o), local_file, num_threads = num_threads,
target_resource_name = options.get(kw.RESC_NAME_KW,''),
data_open_returned_values = data_open_returned_values_,
updatables = updatables):
raise RuntimeError("parallel get failed")
else:
for chunk in chunks(o, self.READ_BUFFER_SIZE):
f.write(chunk)
do_progress_updates(updatables, len(chunk))
def get(self, path, local_path = None, num_threads = DEFAULT_NUMBER_OF_THREADS, updatables = (), **options):
"""
Get a reference to the data object at the specified `path'.
Only download the object if the local_path is a string (specifying
a path in the local filesystem to use as a destination file).
"""
parent = self.sess.collections.get(irods_dirname(path))
# TODO: optimize
if local_path:
self._download(path, local_path, num_threads = num_threads, updatables = updatables, **options)
query = self.sess.query(DataObject)\
.filter(DataObject.name == irods_basename(path))\
.filter(DataObject.collection_id == parent.id)\
.add_keyword(kw.ZONE_KW, path.split('/')[1])
if self.sess.ticket__:
query = query.filter(Collection.id != 0) # a no-op, but necessary because CAT_SQL_ERR results if the ticket
# is for a DataObject and we don't explicitly join to Collection
results = query.all() # get up to max_rows replicas
if len(results) <= 0:
raise ex.DataObjectDoesNotExist()
return iRODSDataObject(self, parent, results)
def put(self, local_path, irods_path, return_data_object = False, num_threads = DEFAULT_NUMBER_OF_THREADS, updatables = (), **options):
if self.sess.collections.exists(irods_path):
obj = iRODSCollection.normalize_path(irods_path, os.path.basename(local_path))
else:
obj = irods_path
with open(local_path, 'rb') as f:
sizelist = []
if self.should_parallelize_transfer (num_threads, f, measured_obj_size = sizelist, open_options = options):
o = deferred_call( self.open, (obj, 'w'), options)
f.close()
if not self.parallel_put( local_path, (obj,o), total_bytes = sizelist[0], num_threads = num_threads,
target_resource_name = options.get(kw.RESC_NAME_KW,'') or
options.get(kw.DEST_RESC_NAME_KW,''),
open_options = options, updatables = updatables):
raise RuntimeError("parallel put failed")
else:
with self.open(obj, 'w', **options) as o:
# Set operation type to trigger acPostProcForPut
if kw.OPR_TYPE_KW not in options:
options[kw.OPR_TYPE_KW] = 1 # PUT_OPR
for chunk in chunks(f, self.WRITE_BUFFER_SIZE):
o.write(chunk)
do_progress_updates(updatables, len(chunk))
if kw.ALL_KW in options:
repl_options = options.copy()
repl_options[kw.UPDATE_REPL_KW] = ''
# Leaving REG_CHKSUM_KW set would raise the error:
# Requested to register checksum without verifying, but source replica has a checksum. This can result
# in multiple replicas being marked good with different checksums, which is an inconsistency.
del repl_options[kw.REG_CHKSUM_KW]
self.replicate(obj, **repl_options)
if return_data_object:
return self.get(obj)
def chksum(self, path, **options):
"""
See: https://github.com/irods/irods/blob/4-2-stable/lib/api/include/dataObjChksum.h
for a list of applicable irods.keywords options.
"""
r_error_stack = options.pop('r_error',None)
message_body = DataObjChksumRequest(path, **options)
message = iRODSMessage('RODS_API_REQ', msg=message_body,
int_info=api_number['DATA_OBJ_CHKSUM_AN'])
checksum = ""
msg_retn = []
with self.sess.pool.get_connection() as conn:
conn.send(message)
try:
response = conn.recv(return_message = msg_retn)
except ex.CHECK_VERIFICATION_RESULTS as exc:
# We'll get a response in the client to help qualify or elaborate on the error thrown.
if msg_retn: response = msg_retn[0]
logging.warning("Exception checksumming data object %r - %r",path,exc)
if 'response' in locals():
try:
results = response.get_main_message(DataObjChksumResponse, r_error = r_error_stack)
checksum = results.myStr.strip()
if checksum[0] in ( '[','{' ): # in iRODS 4.2.11 and later, myStr is in JSON format.
exc = Server_Checksum_Warning( checksum )
if not r_error_stack:
r_error_stack.fill(exc.response)
raise exc
except iRODSMessage.ResponseNotParseable:
# response.msg is None when VERIFY_CHKSUM_KW is used
pass
return checksum
def parallel_get(self,
data_or_path_ ,
file_ ,
async_ = False,
num_threads = 0,
target_resource_name = '',
data_open_returned_values = None,
progressQueue = False,
updatables = ()):
"""Call into the irods.parallel library for multi-1247 GET.
Called from a session.data_objects.get(...) (via the _download method) on
the condition that the data object is determined to be of appropriate size
for parallel download.
"""
return parallel.io_main( self.sess, data_or_path_, parallel.Oper.GET | (parallel.Oper.NONBLOCKING if async_ else 0), file_,
num_threads = num_threads, target_resource_name = target_resource_name,
data_open_returned_values = data_open_returned_values,
queueLength = (DEFAULT_QUEUE_DEPTH if progressQueue else 0),
updatables = updatables)
def parallel_put(self,
file_ ,
data_or_path_ ,
async_ = False,
total_bytes = -1,
num_threads = 0,
target_resource_name = '',
open_options = {},
updatables = (),
progressQueue = False):
"""Call into the irods.parallel library for multi-1247 PUT.
Called from a session.data_objects.put(...) on the condition that the
data object is determined to be of appropriate size for parallel upload.
"""
return parallel.io_main( self.sess, data_or_path_, parallel.Oper.PUT | (parallel.Oper.NONBLOCKING if async_ else 0), file_,
num_threads = num_threads, total_bytes = total_bytes, target_resource_name = target_resource_name,
open_options = open_options,
queueLength = (DEFAULT_QUEUE_DEPTH if progressQueue else 0),
updatables = updatables,
)
def create(self, path, resource=None, force=False, **options):
options[kw.DATA_TYPE_KW] = 'generic'
if resource:
options[kw.DEST_RESC_NAME_KW] = resource
else:
# Use client-side default resource if available
try:
options[kw.DEST_RESC_NAME_KW] = self.sess.default_resource
except AttributeError:
pass
if force:
options[kw.FORCE_FLAG_KW] = ''
message_body = FileOpenRequest(
objPath=path,
createMode=0o644,
openFlags=0,
offset=0,
dataSize=-1,
numThreads=self.sess.numThreads,
oprType=0,
KeyValPair_PI=StringStringMap(options),
)
message = iRODSMessage('RODS_API_REQ', msg=message_body,
int_info=api_number['DATA_OBJ_CREATE_AN'])
with self.sess.pool.get_connection() as conn:
conn.send(message)
response = conn.recv()
desc = response.int_info
conn.close_file(desc)
return self.get(path)
def open_with_FileRaw(self, *arg, **kw_options):
holder = []
handle = self.open(*arg,_raw_fd_holder=holder,**kw_options)
return (handle, holder[-1])
_RESC_flags_for_open = frozenset((
kw.RESC_NAME_KW,
kw.DEST_RESC_NAME_KW, # may be deprecated in the future
kw.RESC_HIER_STR_KW
))
def open(self, path, mode,
create = True, # (Dis-)allow object creation.
finalize_on_close = True, # For PRC internal use.
auto_close = client_config.getter('data_objects','auto_close'), # The default value will be a lambda returning the
# global setting. Use True or False as an override.
returned_values = None, # Used to update session reference, for forging more conns to same host, in irods.parallel.io_main
allow_redirect = True, # This may be set to False to disallow the client redirect-to-resource.
**options):
_raw_fd_holder = options.get('_raw_fd_holder',[])
# If no keywords are used that would influence the server as to the choice of a storage resource,
# then use the default resource in the client configuration.
if self._RESC_flags_for_open.isdisjoint( options.keys() ):
# Use client-side default resource if available
try:
options[kw.DEST_RESC_NAME_KW] = self.sess.default_resource
except AttributeError:
pass
createFlag = self.O_CREAT if create else 0
flags, seek_to_end = {
'r': (self.O_RDONLY, False),
'r+': (self.O_RDWR, False),
'w': (self.O_WRONLY | createFlag | self.O_TRUNC, False),
'w+': (self.O_RDWR | createFlag | self.O_TRUNC, False),
'a': (self.O_WRONLY | createFlag, True),
'a+': (self.O_RDWR | createFlag, True),
}[mode]
# TODO: Use seek_to_end
if not isinstance(returned_values, dict):
returned_values = {}
try:
oprType = options[kw.OPR_TYPE_KW]
except KeyError:
oprType = 0
dataSize_default = ('0' if self.server_version < (4,3,1) else '-1')
def make_FileOpenRequest(**extra_opts):
options_ = dict(options) if extra_opts else options
options_.update(extra_opts)
return FileOpenRequest(
objPath=path,
createMode=0,
openFlags=flags,
offset=0,
dataSize=int(options.get(kw.DATA_SIZE_KW,dataSize_default)),
numThreads=self.sess.numThreads,
oprType=oprType,
KeyValPair_PI=StringStringMap(options_),
)
requested_hierarchy = options.get(kw.RESC_HIER_STR_KW, None)
conn = self.sess.pool.get_connection()
redirected_host = ''
use_get_rescinfo_apis = False
if allow_redirect and conn.server_version >= (4,3,1):
key = 'CREATE' if mode[0] in ('w','a') else 'OPEN'
message = iRODSMessage('RODS_API_REQ',
msg=make_FileOpenRequest(**{kw.GET_RESOURCE_INFO_OP_TYPE_KW:key}),
int_info=api_number['GET_RESOURCE_INFO_FOR_OPERATION_AN'])
conn.send(message)
response = conn.recv()
msg = response.get_main_message( STR_PI )
use_get_rescinfo_apis = True
# Get the information needed for the redirect
_ = json.loads(msg.myStr)
redirected_host = _["host"]
requested_hierarchy = _["resource_hierarchy"]
target_zone = list(filter(None, path.split('/')))
if target_zone:
target_zone = target_zone[0]
directed_sess = self.sess
if redirected_host and use_get_rescinfo_apis:
# Redirect only if the local zone is being targeted, and if the hostname is changed from the original.
if target_zone == self.sess.zone and (self.sess.host != redirected_host):
# This is the actual redirect.
directed_sess = self.sess.clone(host = redirected_host)
returned_values['session'] = directed_sess
conn.release()
conn = directed_sess.pool.get_connection()
logger.debug(u'redirect_to_host = %s', redirected_host)
# Restore RESC HIER for DATA_OBJ_OPEN call
if requested_hierarchy is not None:
options[kw.RESC_HIER_STR_KW] = requested_hierarchy
message_body = make_FileOpenRequest()
# Perform DATA_OBJ_OPEN call
message = iRODSMessage('RODS_API_REQ', msg=message_body,
int_info=api_number['DATA_OBJ_OPEN_AN'])
conn.send(message)
desc = conn.recv().int_info
raw = iRODSDataObjectFileRaw(conn, desc, finalize_on_close = finalize_on_close, **options)
raw.session = directed_sess
(_raw_fd_holder).append(raw)
if callable(auto_close):
# Use case: auto_close has defaulted to the irods.configuration getter.
# access entry in irods.configuration
auto_close = auto_close()
if auto_close:
ret_value = ManagedBufferedRandom(raw, _session = self.sess)
else:
ret_value = io.BufferedRandom(raw)
if 'a' in mode:
ret_value.seek(0,io.SEEK_END)
return ret_value
def trim(self, path, **options):
try:
oprType = options[kw.OPR_TYPE_KW]
except KeyError:
oprType = 0
message_body = FileOpenRequest(
objPath=path,
createMode=0,
openFlags=0,
offset=0,
dataSize=-1,
numThreads=self.sess.numThreads,
oprType=oprType,
KeyValPair_PI=StringStringMap(options),
)
message = iRODSMessage('RODS_API_REQ', msg=message_body,
int_info=api_number['DATA_OBJ_TRIM_AN'])
with self.sess.pool.get_connection() as conn:
conn.send(message)
response = conn.recv()
def unlink(self, path, force=False, **options):
if force:
options[kw.FORCE_FLAG_KW] = ''
try:
oprType = options[kw.OPR_TYPE_KW]
except KeyError:
oprType = 0
message_body = FileOpenRequest(
objPath=path,
createMode=0,
openFlags=0,
offset=0,
dataSize=-1,
numThreads=self.sess.numThreads,
oprType=oprType,
KeyValPair_PI=StringStringMap(options),
)
message = iRODSMessage('RODS_API_REQ', msg=message_body,
int_info=api_number['DATA_OBJ_UNLINK_AN'])
with self.sess.pool.get_connection() as conn:
conn.send(message)
response = conn.recv()
def unregister(self, path, **options):
# https://github.com/irods/irods/blob/4.2.1/lib/api/include/dataObjInpOut.h#L190
options[kw.OPR_TYPE_KW] = 26 # UNREG_OPR: prevents deletion from disk.
# If a replica is targeted, use trim API.
if {kw.RESC_NAME_KW, kw.REPL_NUM_KW} & set(options.keys()):
self.trim(path, **options)
else:
self.unlink(path, **options)
def exists(self, path):
try:
self.get(path)
except ex.DoesNotExist:
return False
return True
def move(self, src_path, dest_path):
# check if dest is a collection
# if so append filename to it
if self.sess.collections.exists(dest_path):
filename = src_path.rsplit('/', 1)[1]
target_path = dest_path + '/' + filename
else:
target_path = dest_path
src = FileOpenRequest(
objPath=src_path,
createMode=0,
openFlags=0,
offset=0,
dataSize=0,
numThreads=self.sess.numThreads,
oprType=11, # RENAME_DATA_OBJ
KeyValPair_PI=StringStringMap(),
)
dest = FileOpenRequest(
objPath=target_path,
createMode=0,
openFlags=0,
offset=0,
dataSize=0,
numThreads=self.sess.numThreads,
oprType=11, # RENAME_DATA_OBJ
KeyValPair_PI=StringStringMap(),
)
message_body = ObjCopyRequest(
srcDataObjInp_PI=src,
destDataObjInp_PI=dest
)
message = iRODSMessage('RODS_API_REQ', msg=message_body,
int_info=api_number['DATA_OBJ_RENAME_AN'])
with self.sess.pool.get_connection() as conn:
conn.send(message)
response = conn.recv()
def copy(self, src_path, dest_path, **options):
# check if dest is a collection
# if so append filename to it
if self.sess.collections.exists(dest_path):
filename = src_path.rsplit('/', 1)[1]
target_path = dest_path + '/' + filename
else:
target_path = dest_path
src = FileOpenRequest(
objPath=src_path,
createMode=0,
openFlags=0,
offset=0,
dataSize=0,
numThreads=self.sess.numThreads,
oprType=10, # COPY_SRC
KeyValPair_PI=StringStringMap(),
)
dest = FileOpenRequest(
objPath=target_path,
createMode=0,
openFlags=0,
offset=0,
dataSize=0,
numThreads=self.sess.numThreads,
oprType=9, # COPY_DEST
KeyValPair_PI=StringStringMap(options),
)
message_body = ObjCopyRequest(
srcDataObjInp_PI=src,
destDataObjInp_PI=dest
)
message = iRODSMessage('RODS_API_REQ', msg=message_body,
int_info=api_number['DATA_OBJ_COPY_AN'])
with self.sess.pool.get_connection() as conn:
conn.send(message)
response = conn.recv()
def truncate(self, path, size, **options):
message_body = FileOpenRequest(
objPath=path,
createMode=0,
openFlags=0,
offset=0,
dataSize=size,
numThreads=self.sess.numThreads,
oprType=0,
KeyValPair_PI=StringStringMap(options),
)
message = iRODSMessage('RODS_API_REQ', msg=message_body,
int_info=api_number['DATA_OBJ_TRUNCATE_AN'])
with self.sess.pool.get_connection() as conn:
conn.send(message)
response = conn.recv()
def replicate(self, path, resource = None, **options):
if resource:
options[kw.DEST_RESC_NAME_KW] = resource
else:
def_resc = getattr(self.sess,'default_resource',None)
if def_resc:
options[kw.DEF_RESC_NAME_KW] = def_resc
message_body = FileOpenRequest(
objPath=path,
createMode=0,
openFlags=0,
offset=0,
dataSize=-1,
numThreads=self.sess.numThreads,
oprType=6,
KeyValPair_PI=StringStringMap(options),
)
message = iRODSMessage('RODS_API_REQ', msg=message_body,
int_info=api_number['DATA_OBJ_REPL_AN'])
with self.sess.pool.get_connection() as conn:
conn.send(message)
response = conn.recv()
def register(self, file_path, obj_path, **options):
options[kw.FILE_PATH_KW] = file_path
message_body = FileOpenRequest(
objPath=obj_path,
createMode=0,
openFlags=0,
offset=0,
dataSize=0,
numThreads=self.sess.numThreads,
oprType=0,
KeyValPair_PI=StringStringMap(options),
)
message = iRODSMessage('RODS_API_REQ', msg=message_body,
int_info=api_number['PHY_PATH_REG_AN'])
with self.sess.pool.get_connection() as conn:
conn.send(message)
response = conn.recv()
def modDataObjMeta(self, data_obj_info, meta_dict, **options):
if "rescHier" not in data_obj_info and "rescName" not in data_obj_info and "replNum" not in data_obj_info:
meta_dict["all"] = ""
message_body = ModDataObjMeta(
dataObjInfo=DataObjInfo(
objPath=data_obj_info["objPath"],
rescName=data_obj_info.get("rescName", ""),
rescHier=data_obj_info.get("rescHier", ""),
dataType="",
dataSize=0,
chksum="",
version="",
filePath="",
dataOwnerName="",
dataOwnerZone="",
replNum=data_obj_info.get("replNum", 0),
replStatus=0,
statusString="",
dataId=0,
collId=0,
dataMapId=0,
flags=0,
dataComments="",
dataMode="",
dataExpiry="",
dataCreate="",
dataModify="",
dataAccess="",
dataAccessInx=0,
writeFlag=0,
destRescName="",
backupRescName="",
subPath="",
specColl=0,
regUid=0,
otherFlags=0,
KeyValPair_PI=StringStringMap(options),
in_pdmo="",
next=0,
rescId=0
),
regParam=StringStringMap(meta_dict)
)
message = iRODSMessage('RODS_API_REQ', msg=message_body,
int_info=api_number['MOD_DATA_OBJ_META_AN'])
with self.sess.pool.get_connection() as conn:
conn.send(message)
response = conn.recv()
def touch(self, path, **options):
"""Change the mtime of a data object.
A path argument that does not exist will be created as an empty data
object, unless "no_create=True" is supplied.
Parameters
----------
path: string
The absolute logical path of a data object.
no_create: boolean, optional
Instructs the system not to create a data object when it does not
exist.
replica_number: integer, optional
The replica number of the replica to update. Replica numbers cannot
be used to create data objects or additional replicas. Cannot be used
with "leaf_resource_name".
leaf_resource_name: string, optional
The name of the leaf resource containing the replica to update. If
the object identified by the "path" parameter does not exist and this
parameter holds a valid resource, the data object will be created at
the specified resource. Cannot be used with "replica_number" parameter.
seconds_since_epoch: integer, optional
The number of seconds since epoch representing the new mtime. Cannot
be used with "reference" parameter.
reference: string, optional
Use the mtime of the logical path to the data object or collection
identified by this option. Cannot be used with "seconds_since_epoch"
parameter.
Raises
------
InvalidInputArgument
If the path points to a collection.
"""
if _logical_path._is_collection(self.sess, path):
raise ex.InvalidInputArgument()
_api_impl._touch_impl(self.sess, path, **options)