rucio-clients 32.8.6__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of rucio-clients might be problematic. Click here for more details.

Files changed (88) hide show
  1. rucio/__init__.py +18 -0
  2. rucio/alembicrevision.py +16 -0
  3. rucio/client/__init__.py +16 -0
  4. rucio/client/accountclient.py +413 -0
  5. rucio/client/accountlimitclient.py +155 -0
  6. rucio/client/baseclient.py +929 -0
  7. rucio/client/client.py +77 -0
  8. rucio/client/configclient.py +113 -0
  9. rucio/client/credentialclient.py +54 -0
  10. rucio/client/didclient.py +691 -0
  11. rucio/client/diracclient.py +48 -0
  12. rucio/client/downloadclient.py +1674 -0
  13. rucio/client/exportclient.py +44 -0
  14. rucio/client/fileclient.py +51 -0
  15. rucio/client/importclient.py +42 -0
  16. rucio/client/lifetimeclient.py +74 -0
  17. rucio/client/lockclient.py +99 -0
  18. rucio/client/metaclient.py +137 -0
  19. rucio/client/pingclient.py +45 -0
  20. rucio/client/replicaclient.py +444 -0
  21. rucio/client/requestclient.py +109 -0
  22. rucio/client/rseclient.py +664 -0
  23. rucio/client/ruleclient.py +287 -0
  24. rucio/client/scopeclient.py +88 -0
  25. rucio/client/subscriptionclient.py +161 -0
  26. rucio/client/touchclient.py +78 -0
  27. rucio/client/uploadclient.py +871 -0
  28. rucio/common/__init__.py +14 -0
  29. rucio/common/cache.py +74 -0
  30. rucio/common/config.py +796 -0
  31. rucio/common/constants.py +92 -0
  32. rucio/common/constraints.py +18 -0
  33. rucio/common/didtype.py +187 -0
  34. rucio/common/exception.py +1092 -0
  35. rucio/common/extra.py +37 -0
  36. rucio/common/logging.py +404 -0
  37. rucio/common/pcache.py +1387 -0
  38. rucio/common/policy.py +84 -0
  39. rucio/common/schema/__init__.py +143 -0
  40. rucio/common/schema/atlas.py +411 -0
  41. rucio/common/schema/belleii.py +406 -0
  42. rucio/common/schema/cms.py +478 -0
  43. rucio/common/schema/domatpc.py +399 -0
  44. rucio/common/schema/escape.py +424 -0
  45. rucio/common/schema/generic.py +431 -0
  46. rucio/common/schema/generic_multi_vo.py +410 -0
  47. rucio/common/schema/icecube.py +404 -0
  48. rucio/common/schema/lsst.py +423 -0
  49. rucio/common/stomp_utils.py +160 -0
  50. rucio/common/stopwatch.py +56 -0
  51. rucio/common/test_rucio_server.py +148 -0
  52. rucio/common/types.py +158 -0
  53. rucio/common/utils.py +1946 -0
  54. rucio/rse/__init__.py +97 -0
  55. rucio/rse/protocols/__init__.py +14 -0
  56. rucio/rse/protocols/cache.py +123 -0
  57. rucio/rse/protocols/dummy.py +112 -0
  58. rucio/rse/protocols/gfal.py +701 -0
  59. rucio/rse/protocols/globus.py +243 -0
  60. rucio/rse/protocols/gsiftp.py +93 -0
  61. rucio/rse/protocols/http_cache.py +83 -0
  62. rucio/rse/protocols/mock.py +124 -0
  63. rucio/rse/protocols/ngarc.py +210 -0
  64. rucio/rse/protocols/posix.py +251 -0
  65. rucio/rse/protocols/protocol.py +530 -0
  66. rucio/rse/protocols/rclone.py +365 -0
  67. rucio/rse/protocols/rfio.py +137 -0
  68. rucio/rse/protocols/srm.py +339 -0
  69. rucio/rse/protocols/ssh.py +414 -0
  70. rucio/rse/protocols/storm.py +207 -0
  71. rucio/rse/protocols/webdav.py +547 -0
  72. rucio/rse/protocols/xrootd.py +295 -0
  73. rucio/rse/rsemanager.py +752 -0
  74. rucio/vcsversion.py +11 -0
  75. rucio/version.py +46 -0
  76. rucio_clients-32.8.6.data/data/etc/rse-accounts.cfg.template +25 -0
  77. rucio_clients-32.8.6.data/data/etc/rucio.cfg.atlas.client.template +42 -0
  78. rucio_clients-32.8.6.data/data/etc/rucio.cfg.template +257 -0
  79. rucio_clients-32.8.6.data/data/requirements.txt +55 -0
  80. rucio_clients-32.8.6.data/data/rucio_client/merge_rucio_configs.py +147 -0
  81. rucio_clients-32.8.6.data/scripts/rucio +2540 -0
  82. rucio_clients-32.8.6.data/scripts/rucio-admin +2434 -0
  83. rucio_clients-32.8.6.dist-info/METADATA +50 -0
  84. rucio_clients-32.8.6.dist-info/RECORD +88 -0
  85. rucio_clients-32.8.6.dist-info/WHEEL +5 -0
  86. rucio_clients-32.8.6.dist-info/licenses/AUTHORS.rst +94 -0
  87. rucio_clients-32.8.6.dist-info/licenses/LICENSE +201 -0
  88. rucio_clients-32.8.6.dist-info/top_level.txt +1 -0
@@ -0,0 +1,752 @@
1
+ # -*- coding: utf-8 -*-
2
+ # Copyright European Organization for Nuclear Research (CERN) since 2012
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ import copy
17
+ import logging
18
+ import random
19
+ from time import sleep
20
+ from urllib.parse import urlparse
21
+
22
+ from rucio.common import exception, utils, constants
23
+ from rucio.common import types
24
+ from rucio.common.config import config_get_int
25
+ from rucio.common.constraints import STRING_TYPES
26
+ from rucio.common.logging import formatted_logger
27
+ from rucio.common.utils import make_valid_did, GLOBALLY_SUPPORTED_CHECKSUMS
28
+
29
+
30
+ def get_rse_info(rse=None, vo='def', rse_id=None, session=None) -> types.RSESettingsDict:
31
+ """
32
+ Returns all protocol related RSE attributes.
33
+ Call with either rse and vo, or (in server mode) rse_id
34
+
35
+ :param rse: Name of the requested RSE
36
+ :param vo: The VO for the RSE.
37
+ :param rse_id: The id of the rse (use in server mode to avoid db calls)
38
+ :param session: The eventual database session.
39
+
40
+ :returns: a dict object with the following attributes:
41
+ id ... an internal identifier
42
+ rse ... the name of the RSE as string
43
+ type ... the storage type odf the RSE e.g. DISK
44
+ volatile ... boolean indicating if the RSE is volatile
45
+ verify_checksum ... boolean indicating whether RSE supports requests for checksums
46
+ deterministic ... boolean indicating of the naming of the files follows the defined determinism
47
+ domain ... indicating the domain that should be assumed for transfers. Values are 'ALL', 'LAN', or 'WAN'
48
+ protocols ... all supported protocol in form of a list of dict objects with the following structure
49
+ - scheme ... protocol scheme e.g. http, srm, ...
50
+ - hostname ... hostname of the site
51
+ - prefix ... path to the folder where the files are stored
52
+ - port ... port used for this protocol
53
+ - impl ... naming the python class of the protocol implementation
54
+ - extended_attributes ... additional information for the protocol
55
+ - domains ... a dict naming each domain and the priority of the protocol for each operation (lower is better, zero is not supported)
56
+
57
+ :raises RSENotFound: if the provided RSE could not be found in the database.
58
+ """
59
+ # __request_rse_info will be assigned when the module is loaded as it depends on the rucio environment (server or client)
60
+ # __request_rse_info, rse_region are defined in /rucio/rse/__init__.py
61
+ key = '{}:{}'.format(rse, vo) if rse_id is None else str(rse_id)
62
+ key = 'rse_info_%s' % (key)
63
+ rse_info = RSE_REGION.get(key) # NOQA pylint: disable=undefined-variable
64
+ if not rse_info: # no cached entry found
65
+ rse_info = __request_rse_info(str(rse), vo=vo, rse_id=rse_id, session=session) # NOQA pylint: disable=undefined-variable
66
+ RSE_REGION.set(key, rse_info) # NOQA pylint: disable=undefined-variable
67
+ return rse_info
68
+
69
+
70
+ def _get_possible_protocols(rse_settings: types.RSESettingsDict, operation, scheme=None, domain=None, impl=None):
71
+ """
72
+ Filter the list of available protocols or provided by the supported ones.
73
+
74
+ :param rse_settings: The rse settings.
75
+ :param operation: The operation (write, read).
76
+ :param scheme: Optional filter if no specific protocol is defined in
77
+ rse_setting for the provided operation.
78
+ :param domain: Optional domain (lan/wan), if not specified, both will be returned
79
+ :returns: The list of possible protocols.
80
+ """
81
+ operation = operation.lower()
82
+ candidates = rse_settings['protocols']
83
+
84
+ # convert scheme to list, if given as string
85
+ if scheme and not isinstance(scheme, list):
86
+ scheme = scheme.split(',')
87
+
88
+ tbr = []
89
+ for protocol in candidates:
90
+ # Check if impl given and filter if so
91
+ if impl and protocol['impl'] != impl:
92
+ tbr.append(protocol)
93
+ continue
94
+
95
+ # Check if scheme given and filter if so
96
+ if scheme and protocol['scheme'] not in scheme:
97
+ tbr.append(protocol)
98
+ continue
99
+
100
+ filtered = True
101
+
102
+ if not domain:
103
+ for d in list(protocol['domains'].keys()):
104
+ if protocol['domains'][d][operation]:
105
+ filtered = False
106
+ else:
107
+ if protocol['domains'].get(domain, {operation: None}).get(operation):
108
+ filtered = False
109
+
110
+ if filtered:
111
+ tbr.append(protocol)
112
+
113
+ if len(candidates) <= len(tbr):
114
+ raise exception.RSEProtocolNotSupported('No protocol for provided settings'
115
+ ' found : %s.' % str(rse_settings))
116
+
117
+ return [c for c in candidates if c not in tbr]
118
+
119
+
120
+ def get_protocols_ordered(rse_settings: types.RSESettingsDict, operation, scheme=None, domain='wan', impl=None):
121
+ if operation not in utils.rse_supported_protocol_operations():
122
+ raise exception.RSEOperationNotSupported('Operation %s is not supported' % operation)
123
+
124
+ if domain and domain not in utils.rse_supported_protocol_domains():
125
+ raise exception.RSEProtocolDomainNotSupported('Domain %s not supported' % domain)
126
+
127
+ candidates = _get_possible_protocols(rse_settings, operation, scheme, domain, impl)
128
+ candidates.sort(key=lambda k: k['domains'][domain][operation])
129
+ return candidates
130
+
131
+
132
+ def select_protocol(rse_settings: types.RSESettingsDict, operation, scheme=None, domain='wan'):
133
+ if operation not in utils.rse_supported_protocol_operations():
134
+ raise exception.RSEOperationNotSupported('Operation %s is not supported' % operation)
135
+
136
+ if domain and domain not in utils.rse_supported_protocol_domains():
137
+ raise exception.RSEProtocolDomainNotSupported('Domain %s not supported' % domain)
138
+
139
+ candidates = _get_possible_protocols(rse_settings, operation, scheme, domain)
140
+ # Shuffle candidates to load-balance over equal sources
141
+ random.shuffle(candidates)
142
+ return min(candidates, key=lambda k: k['domains'][domain][operation])
143
+
144
+
145
+ def create_protocol(rse_settings: types.RSESettingsDict, operation, scheme=None, domain='wan', auth_token=None, protocol_attr=None, logger=logging.log, impl=None):
146
+ """
147
+ Instantiates the protocol defined for the given operation.
148
+
149
+ :param rse_settings: RSE attributes
150
+ :param operation: Intended operation for this protocol
151
+ :param scheme: Optional filter if no specific protocol is defined in rse_setting for the provided operation
152
+ :param domain: Optional specification of the domain
153
+ :param auth_token: Optionally passing JSON Web Token (OIDC) string for authentication
154
+ :param protocol_attr: Optionally passing the full protocol availability information to correctly select WAN/LAN
155
+ :param logger: Optional decorated logger that can be passed from the calling daemons or servers.
156
+ :returns: An instance of the requested protocol
157
+ """
158
+
159
+ # Verify feasibility of Protocol
160
+ operation = operation.lower()
161
+ if operation not in utils.rse_supported_protocol_operations():
162
+ raise exception.RSEOperationNotSupported('Operation %s is not supported' % operation)
163
+
164
+ if domain and domain not in utils.rse_supported_protocol_domains():
165
+ raise exception.RSEProtocolDomainNotSupported('Domain %s not supported' % domain)
166
+
167
+ if impl:
168
+ candidate = _get_possible_protocols(rse_settings, operation, scheme, domain, impl=impl)
169
+ if len(candidate) == 0:
170
+ raise exception.RSEProtocolNotSupported('Protocol implementation %s operation %s on domain %s not supported' % (impl, operation, domain))
171
+ protocol_attr = candidate[0]
172
+ elif not protocol_attr:
173
+ protocol_attr = select_protocol(rse_settings, operation, scheme, domain)
174
+ else:
175
+ candidates = _get_possible_protocols(rse_settings, operation, scheme, domain)
176
+ if protocol_attr not in candidates:
177
+ raise exception.RSEProtocolNotSupported('Protocol %s operation %s on domain %s not supported' % (protocol_attr, operation, domain))
178
+
179
+ # Instantiate protocol
180
+ comp = protocol_attr['impl'].split('.')
181
+ prefix = '.'.join(comp[-2:]) + ': '
182
+ logger = formatted_logger(logger, prefix + "%s")
183
+ mod = __import__('.'.join(comp[:-1]))
184
+ for n in comp[1:]:
185
+ try:
186
+ mod = getattr(mod, n)
187
+ except AttributeError as e:
188
+ logger(logging.DEBUG, 'Protocol implementations not supported.')
189
+ raise exception.RucioException(str(e)) # TODO: provide proper rucio exception
190
+ protocol_attr['auth_token'] = auth_token
191
+ protocol = mod(protocol_attr, rse_settings, logger=logger)
192
+ return protocol
193
+
194
+
195
+ def lfns2pfns(rse_settings: types.RSESettingsDict, lfns, operation='write', scheme=None, domain='wan', auth_token=None, logger=logging.log, impl=None):
196
+ """
197
+ Convert the lfn to a pfn
198
+
199
+ :param rse_settings: RSE attributes
200
+ :param lfns: logical file names as a dict containing 'scope' and 'name' as keys. For bulk a list of dicts can be provided
201
+ :param operation: Intended operation for this protocol
202
+ :param scheme: Optional filter if no specific protocol is defined in rse_setting for the provided operation
203
+ :param domain: Optional specification of the domain
204
+ :param auth_token: Optionally passing JSON Web Token (OIDC) string for authentication
205
+ :param logger: Optional decorated logger that can be passed from the calling daemons or servers.
206
+
207
+ :returns: a dict with scope:name as key and the PFN as value
208
+
209
+ """
210
+ return create_protocol(rse_settings, operation, scheme, domain, auth_token=auth_token, logger=logger, impl=impl).lfns2pfns(lfns)
211
+
212
+
213
+ def parse_pfns(rse_settings: types.RSESettingsDict, pfns, operation='read', domain='wan', auth_token=None):
214
+ """
215
+ Checks if a PFN is feasible for a given RSE. If so it splits the pfn in its various components.
216
+
217
+ :param rse_settings: RSE attributes
218
+ :param pfns: list of PFNs
219
+ :param operation: Intended operation for this protocol
220
+ :param domain: Optional specification of the domain
221
+ :param auth_token: Optionally passing JSON Web Token (OIDC) string for authentication
222
+
223
+ :returns: A dict with the parts known by the selected protocol e.g. scheme, hostname, prefix, path, name
224
+
225
+ :raises RSEFileNameNotSupported: if provided PFN is not supported by the RSE/protocol
226
+ :raises RSENotFound: if the referred storage is not found i the repository (rse_id)
227
+ :raises InvalidObject: If the properties parameter doesn't include scheme, hostname, and port as keys
228
+ :raises RSEOperationNotSupported: If no matching protocol was found for the requested operation
229
+ """
230
+ if len(set([urlparse(pfn).scheme for pfn in pfns])) != 1:
231
+ raise ValueError('All PFNs must provide the same protocol scheme')
232
+ return create_protocol(rse_settings, operation, urlparse(pfns[0]).scheme, domain, auth_token=auth_token).parse_pfns(pfns)
233
+
234
+
235
+ def exists(rse_settings: types.RSESettingsDict, files, domain='wan', scheme=None, impl=None, auth_token=None, vo='def', logger=logging.log):
236
+ """
237
+ Checks if a file is present at the connected storage.
238
+ Providing a list indicates the bulk mode.
239
+
240
+ :param rse_settings: RSE attributes
241
+ :param files: a single dict or a list with dicts containing 'scope' and 'name'
242
+ if LFNs are used and only 'name' if PFNs are used.
243
+ E.g. {'name': '2_rse_remote_get.raw', 'scope': 'user.jdoe'}, {'name': 'user/jdoe/5a/98/3_rse_remote_get.raw'}
244
+ :param domain: The network domain, either 'wan' (default) or 'lan'
245
+ :param auth_token: Optionally passing JSON Web Token (OIDC) string for authentication
246
+ :param vo: The VO for the RSE
247
+ :param logger: Optional decorated logger that can be passed from the calling daemons or servers.
248
+
249
+ :returns: True/False for a single file or a dict object with 'scope:name' for LFNs or 'name' for PFNs as keys and True or the exception as value for each file in bulk mode
250
+
251
+ :raises RSENotConnected: no connection to a specific storage has been established
252
+ """
253
+
254
+ ret = {}
255
+ gs = True # gs represents the global status which indicates if every operation worked in bulk mode
256
+
257
+ protocol = create_protocol(rse_settings, 'read', scheme=scheme, impl=impl, domain=domain, auth_token=auth_token, logger=logger)
258
+ protocol.connect()
259
+ try:
260
+ protocol.exists(None)
261
+ except NotImplementedError:
262
+ protocol = create_protocol(rse_settings, 'write', scheme=scheme, domain=domain, auth_token=auth_token, logger=logger)
263
+ protocol.connect()
264
+ except:
265
+ pass
266
+
267
+ files = [files] if not type(files) is list else files
268
+ for f in files:
269
+ exists = None
270
+ if isinstance(f, STRING_TYPES):
271
+ exists = protocol.exists(f)
272
+ ret[f] = exists
273
+ elif 'scope' in f: # a LFN is provided
274
+ pfn = list(protocol.lfns2pfns(f).values())[0]
275
+ if isinstance(pfn, exception.RucioException):
276
+ raise pfn
277
+ logger(logging.DEBUG, 'Checking if %s exists', pfn)
278
+ # deal with URL signing if required
279
+ if rse_settings['sign_url'] is not None and pfn[:5] == 'https':
280
+ pfn = __get_signed_url(rse_settings['rse'], rse_settings['sign_url'], 'read', pfn, vo) # NOQA pylint: disable=undefined-variable
281
+ exists = protocol.exists(pfn)
282
+ ret[f['scope'] + ':' + f['name']] = exists
283
+ else:
284
+ exists = protocol.exists(f['name'])
285
+ ret[f['name']] = exists
286
+ if not exists:
287
+ gs = False
288
+
289
+ protocol.close()
290
+ if len(ret) == 1:
291
+ for x in ret:
292
+ return ret[x]
293
+ return [gs, ret]
294
+
295
+
296
+ def upload(rse_settings: types.RSESettingsDict, lfns, domain='wan', source_dir=None, force_pfn=None, force_scheme=None, transfer_timeout=None, delete_existing=False, sign_service=None, auth_token=None, vo='def', logger=logging.log, impl=None):
297
+ """
298
+ Uploads a file to the connected storage.
299
+ Providing a list indicates the bulk mode.
300
+
301
+ :param rse_settings: RSE attributes
302
+ :param lfns: a single dict or a list with dicts containing 'scope' and 'name'.
303
+ Examples:
304
+ [
305
+ {'name': '1_rse_local_put.raw', 'scope': 'user.jdoe', 'filesize': 42, 'adler32': '87HS3J968JSNWID'},
306
+ {'name': '2_rse_local_put.raw', 'scope': 'user.jdoe', 'filesize': 4711, 'adler32': 'RSSMICETHMISBA837464F'}
307
+ ]
308
+ If the 'filename' key is present, it will be used by Rucio as the actual name of the file on disk (separate from the Rucio 'name').
309
+ :param domain: The network domain, either 'wan' (default) or 'lan'
310
+ :param source_dir: path to the local directory including the source files
311
+ :param force_pfn: use the given PFN -- can lead to dark data, use sparingly
312
+ :param force_scheme: use the given protocol scheme, overriding the protocol priority in the RSE description
313
+ :param transfer_timeout: set this timeout (in seconds) for the transfers, for protocols that support it
314
+ :param sign_service: use the given service (e.g. gcs, s3, swift) to sign the URL
315
+ :param auth_token: Optionally passing JSON Web Token (OIDC) string for authentication
316
+ :param vo: The VO for the RSE
317
+ :param logger: Optional decorated logger that can be passed from the calling daemons or servers.
318
+
319
+ :returns: True/False for a single file or a dict object with 'scope:name' as keys and True or the exception as value for each file in bulk mode
320
+
321
+ :raises RSENotConnected: no connection to a specific storage has been established
322
+ :raises SourceNotFound: local source file can not be found
323
+ :raises DestinationNotAccessible: remote destination directory is not accessible
324
+ :raises ServiceUnavailable: for any other reason
325
+ """
326
+
327
+ ret = {}
328
+ gs = True # gs represents the global status which indicates if every operation worked in bulk mode
329
+
330
+ protocol = create_protocol(rse_settings, 'write', scheme=force_scheme, domain=domain, auth_token=auth_token, logger=logger, impl=impl)
331
+ protocol.connect()
332
+ protocol_delete = create_protocol(rse_settings, 'delete', domain=domain, auth_token=auth_token, logger=logger, impl=impl)
333
+ protocol_delete.connect()
334
+ lfns = [lfns] if not type(lfns) is list else lfns
335
+ for lfn in lfns:
336
+ base_name = lfn.get('filename', lfn['name'])
337
+ name = lfn.get('name', base_name)
338
+ scope = lfn['scope']
339
+ if 'adler32' not in lfn and 'md5' not in lfn:
340
+ gs = False
341
+ ret['%s:%s' % (scope, name)] = exception.RucioException('Missing checksum for file %s:%s' % (lfn['scope'], name))
342
+ continue
343
+ if 'filesize' not in lfn:
344
+ gs = False
345
+ ret['%s:%s' % (scope, name)] = exception.RucioException('Missing filesize for file %s:%s' % (lfn['scope'], name))
346
+ continue
347
+ if force_pfn:
348
+ pfn = force_pfn
349
+ readpfn = force_pfn
350
+ else:
351
+ pfn = list(protocol.lfns2pfns(make_valid_did(lfn)).values())[0]
352
+ if isinstance(pfn, exception.RucioException):
353
+ raise pfn
354
+ readpfn = pfn
355
+ if sign_service is not None:
356
+ # need a separate signed URL for read operations (exists and stat)
357
+ readpfn = __get_signed_url(rse_settings['rse'], sign_service, 'read', pfn, vo) # NOQA pylint: disable=undefined-variable
358
+ pfn = __get_signed_url(rse_settings['rse'], sign_service, 'write', pfn, vo) # NOQA pylint: disable=undefined-variable
359
+
360
+ # First check if renaming operation is supported
361
+ if protocol.renaming:
362
+
363
+ # Check if file replica is already on the storage system
364
+ if protocol.overwrite is False and delete_existing is False and protocol.exists(pfn):
365
+ ret['%s:%s' % (scope, name)] = exception.FileReplicaAlreadyExists('File %s in scope %s already exists on storage as PFN %s' % (name, scope, pfn))
366
+ gs = False
367
+ else:
368
+ if protocol.exists('%s.rucio.upload' % pfn): # Check for left over of previous unsuccessful attempts
369
+ try:
370
+ logger(logging.DEBUG, 'Deleting %s.rucio.upload', pfn)
371
+ protocol_delete.delete('%s.rucio.upload' % list(protocol_delete.lfns2pfns(make_valid_did(lfn)).values())[0])
372
+ except Exception as e:
373
+ ret['%s:%s' % (scope, name)] = exception.RSEOperationNotSupported('Unable to remove temporary file %s.rucio.upload: %s' % (pfn, str(e)))
374
+ gs = False
375
+ continue
376
+
377
+ if delete_existing:
378
+ if protocol.exists('%s' % pfn): # Check for previous completed uploads that have to be removed before upload
379
+ try:
380
+ logger(logging.DEBUG, 'Deleting %s', pfn)
381
+ protocol_delete.delete('%s' % list(protocol_delete.lfns2pfns(make_valid_did(lfn)).values())[0])
382
+ except Exception as e:
383
+ ret['%s:%s' % (scope, name)] = exception.RSEOperationNotSupported('Unable to remove file %s: %s' % (pfn, str(e)))
384
+ gs = False
385
+ continue
386
+
387
+ try: # Try uploading file
388
+ logger(logging.DEBUG, 'Uploading to %s.rucio.upload', pfn)
389
+ protocol.put(base_name, '%s.rucio.upload' % pfn, source_dir, transfer_timeout=transfer_timeout)
390
+ except Exception as e:
391
+ gs = False
392
+ ret['%s:%s' % (scope, name)] = e
393
+ continue
394
+
395
+ valid = None
396
+
397
+ try: # Get metadata of file to verify if upload was successful
398
+ try:
399
+ stats = _retry_protocol_stat(protocol, '%s.rucio.upload' % pfn)
400
+ # Verify all supported checksums and keep rack of the verified ones
401
+ verified_checksums = []
402
+ for checksum_name in GLOBALLY_SUPPORTED_CHECKSUMS:
403
+ if (checksum_name in stats) and (checksum_name in lfn):
404
+ verified_checksums.append(stats[checksum_name] == lfn[checksum_name])
405
+ # Upload is successful if at least one checksum was found
406
+ valid = any(verified_checksums)
407
+ if not valid and ('filesize' in stats) and ('filesize' in lfn):
408
+ valid = stats['filesize'] == lfn['filesize']
409
+ except NotImplementedError:
410
+ if rse_settings['verify_checksum'] is False:
411
+ valid = True
412
+ else:
413
+ raise exception.RucioException('Checksum not validated')
414
+ except exception.RSEChecksumUnavailable:
415
+ if rse_settings['verify_checksum'] is False:
416
+ valid = True
417
+ else:
418
+ raise exception.RucioException('Checksum not validated')
419
+ except Exception as e:
420
+ gs = False
421
+ ret['%s:%s' % (scope, name)] = e
422
+ continue
423
+
424
+ if valid: # The upload finished successful and the file can be renamed
425
+ try:
426
+ logger(logging.DEBUG, 'Renaming %s.rucio.upload to %s', pfn, pfn)
427
+ protocol.rename('%s.rucio.upload' % pfn, pfn)
428
+ ret['%s:%s' % (scope, name)] = True
429
+ except Exception as e:
430
+ gs = False
431
+ ret['%s:%s' % (scope, name)] = e
432
+ else:
433
+ gs = False
434
+ ret['%s:%s' % (scope, name)] = exception.RucioException('Replica %s is corrupted.' % pfn)
435
+ else:
436
+
437
+ # Check if file replica is already on the storage system
438
+ if protocol.overwrite is False and delete_existing is False and protocol.exists(readpfn):
439
+ ret['%s:%s' % (scope, name)] = exception.FileReplicaAlreadyExists('File %s in scope %s already exists on storage as PFN %s' % (name, scope, pfn))
440
+ gs = False
441
+ else:
442
+ try: # Try uploading file
443
+ logger(logging.DEBUG, 'Uploading to %s', pfn)
444
+ protocol.put(base_name, pfn, source_dir, transfer_timeout=transfer_timeout)
445
+ except Exception as e:
446
+ gs = False
447
+ ret['%s:%s' % (scope, name)] = e
448
+ continue
449
+
450
+ valid = None
451
+ try: # Get metadata of file to verify if upload was successful
452
+ try:
453
+ stats = _retry_protocol_stat(protocol, pfn)
454
+
455
+ # Verify all supported checksums and keep rack of the verified ones
456
+ verified_checksums = []
457
+ for checksum_name in GLOBALLY_SUPPORTED_CHECKSUMS:
458
+ if (checksum_name in stats) and (checksum_name in lfn):
459
+ verified_checksums.append(stats[checksum_name] == lfn[checksum_name])
460
+
461
+ # Upload is successful if at least one checksum was found
462
+ valid = any(verified_checksums)
463
+ if not valid and ('filesize' in stats) and ('filesize' in lfn):
464
+ valid = stats['filesize'] == lfn['filesize']
465
+ except NotImplementedError:
466
+ if rse_settings['verify_checksum'] is False:
467
+ valid = True
468
+ else:
469
+ raise exception.RucioException('Checksum not validated')
470
+ except exception.RSEChecksumUnavailable:
471
+ if rse_settings['verify_checksum'] is False:
472
+ valid = True
473
+ else:
474
+ raise exception.RucioException('Checksum not validated')
475
+ except Exception as e:
476
+ gs = False
477
+ ret['%s:%s' % (scope, name)] = e
478
+ continue
479
+
480
+ if not valid:
481
+ gs = False
482
+ ret['%s:%s' % (scope, name)] = exception.RucioException('Replica %s is corrupted.' % pfn)
483
+
484
+ protocol.close()
485
+ protocol_delete.close()
486
+ if len(ret) == 1:
487
+ for x in ret:
488
+ if isinstance(ret[x], Exception):
489
+ raise ret[x]
490
+ else:
491
+ return {0: ret[x], 1: ret, 'success': ret[x], 'pfn': pfn}
492
+ return {0: gs, 1: ret, 'success': gs, 'pfn': pfn}
493
+
494
+
495
+ def delete(rse_settings: types.RSESettingsDict, lfns, domain='wan', auth_token=None, logger=logging.log, impl=None):
496
+ """
497
+ Delete a file from the connected storage.
498
+ Providing a list indicates the bulk mode.
499
+
500
+ :param rse_settings: RSE attributes
501
+ :param lfns: a single dict or a list with dicts containing 'scope' and 'name'. E.g. [{'name': '1_rse_remote_delete.raw', 'scope': 'user.jdoe'}, {'name': '2_rse_remote_delete.raw', 'scope': 'user.jdoe'}]
502
+ :param domain: The network domain, either 'wan' (default) or 'lan'
503
+ :param auth_token: Optionally passing JSON Web Token (OIDC) string for authentication
504
+ :param logger: Optional decorated logger that can be passed from the calling daemons or servers.
505
+ :returns: True/False for a single file or a dict object with 'scope:name' as keys and True or the exception as value for each file in bulk mode
506
+
507
+ :raises RSENotConnected: no connection to a specific storage has been established
508
+ :raises SourceNotFound: remote source file can not be found on storage
509
+ :raises ServiceUnavailable: for any other reason
510
+
511
+ """
512
+ ret = {}
513
+ gs = True # gs represents the global status which indicates if every operation worked in bulk mode
514
+
515
+ protocol = create_protocol(rse_settings, 'delete', domain=domain, auth_token=auth_token, logger=logger, impl=impl)
516
+ protocol.connect()
517
+
518
+ lfns = [lfns] if not type(lfns) is list else lfns
519
+ for lfn in lfns:
520
+ pfn = list(protocol.lfns2pfns(lfn).values())[0]
521
+ try:
522
+ protocol.delete(pfn)
523
+ ret['%s:%s' % (lfn['scope'], lfn['name'])] = True
524
+ except Exception as e:
525
+ ret['%s:%s' % (lfn['scope'], lfn['name'])] = e
526
+ gs = False
527
+
528
+ protocol.close()
529
+ if len(ret) == 1:
530
+ for x in ret:
531
+ if isinstance(ret[x], Exception):
532
+ raise ret[x]
533
+ else:
534
+ return ret[x]
535
+ return [gs, ret]
536
+
537
+
538
+ def rename(rse_settings: types.RSESettingsDict, files, domain='wan', auth_token=None, logger=logging.log, impl=None):
539
+ """
540
+ Rename files stored on the connected storage.
541
+ Providing a list indicates the bulk mode.
542
+
543
+ :param rse_settings: RSE attributes
544
+ :param files: a single dict or a list with dicts containing 'scope', 'name', 'new_scope' and 'new_name'
545
+ if LFNs are used or only 'name' and 'new_name' if PFNs are used.
546
+ If 'new_scope' or 'new_name' are not provided, the current one is used.
547
+ Examples:
548
+ [
549
+ {'name': '3_rse_remote_rename.raw', 'scope': 'user.jdoe', 'new_name': '3_rse_new.raw', 'new_scope': 'user.jdoe'},
550
+ {'name': 'user/jdoe/d9/cb/9_rse_remote_rename.raw', 'new_name': 'user/jdoe/c6/4a/9_rse_new.raw'}
551
+ ]
552
+ :param domain: The network domain, either 'wan' (default) or 'lan'
553
+ :param auth_token: Optionally passing JSON Web Token (OIDC) string for authentication
554
+ :param logger: Optional decorated logger that can be passed from the calling daemons or servers.
555
+
556
+ :returns: True/False for a single file or a dict object with LFN (key) and True/False (value) in bulk mode
557
+
558
+ :raises RSENotConnected: no connection to a specific storage has been established
559
+ :raises SourceNotFound: remote source file can not be found on storage
560
+ :raises DestinationNotAccessible: remote destination directory is not accessible
561
+ :raises ServiceUnavailable: for any other reason
562
+ """
563
+ ret = {}
564
+ gs = True # gs represents the global status which indicates if every operation worked in bulk mode
565
+
566
+ protocol = create_protocol(rse_settings, 'write', domain=domain, auth_token=auth_token, logger=logger, impl=impl)
567
+ protocol.connect()
568
+
569
+ files = [files] if not type(files) is list else files
570
+ for f in files:
571
+ pfn = None
572
+ new_pfn = None
573
+ key = None
574
+ if 'scope' in f: # LFN is provided
575
+ key = '%s:%s' % (f['scope'], f['name'])
576
+ # Check if new name is provided
577
+ if 'new_name' not in f:
578
+ f['new_name'] = f['name']
579
+ # Check if new scope is provided
580
+ if 'new_scope' not in f:
581
+ f['new_scope'] = f['scope']
582
+ pfn = list(protocol.lfns2pfns({'name': f['name'], 'scope': f['scope']}).values())[0]
583
+ new_pfn = list(protocol.lfns2pfns({'name': f['new_name'], 'scope': f['new_scope']}).values())[0]
584
+ else:
585
+ pfn = f['name']
586
+ new_pfn = f['new_name']
587
+ key = pfn
588
+ # Check if target is not on storage
589
+ if protocol.exists(new_pfn):
590
+ ret[key] = exception.FileReplicaAlreadyExists('File %s already exists on storage' % (new_pfn))
591
+ gs = False
592
+ # Check if source is on storage
593
+ elif not protocol.exists(pfn):
594
+ ret[key] = exception.SourceNotFound('File %s not found on storage' % (pfn))
595
+ gs = False
596
+ else:
597
+ try:
598
+ protocol.rename(pfn, new_pfn)
599
+ ret[key] = True
600
+ except Exception as e:
601
+ ret[key] = e
602
+ gs = False
603
+
604
+ protocol.close()
605
+ if len(ret) == 1:
606
+ for x in ret:
607
+ if isinstance(ret[x], Exception):
608
+ raise ret[x]
609
+ else:
610
+ return ret[x]
611
+ return [gs, ret]
612
+
613
+
614
+ def get_space_usage(rse_settings: types.RSESettingsDict, scheme=None, domain='wan', auth_token=None, logger=logging.log, impl=None):
615
+ """
616
+ Get RSE space usage information.
617
+
618
+ :param rse_settings: RSE attributes
619
+ :param scheme: optional filter to select which protocol to be used.
620
+ :param domain: The network domain, either 'wan' (default) or 'lan'
621
+ :param auth_token: Optionally passing JSON Web Token (OIDC) string for authentication
622
+ :param logger: Optional decorated logger that can be passed from the calling daemons or servers.
623
+
624
+ :returns: a list with dict containing 'totalsize' and 'unusedsize'
625
+
626
+ :raises ServiceUnavailable: if some generic error occurred in the library.
627
+ """
628
+ gs = True
629
+ ret = {}
630
+
631
+ protocol = create_protocol(rse_settings, 'read', scheme=scheme, domain=domain, auth_token=auth_token, logger=logger, impl=impl)
632
+ protocol.connect()
633
+
634
+ try:
635
+ totalsize, unusedsize = protocol.get_space_usage()
636
+ ret["totalsize"] = totalsize
637
+ ret["unusedsize"] = unusedsize
638
+ except Exception as e:
639
+ ret = e
640
+ gs = False
641
+
642
+ protocol.close()
643
+ return [gs, ret]
644
+
645
+
646
+ def find_matching_scheme(rse_settings_dest, rse_settings_src, operation_src, operation_dest, domain='wan', scheme=None):
647
+ """
648
+ Find the best matching scheme between two RSEs
649
+
650
+ :param rse_settings_dest: RSE settings for the destination RSE.
651
+ :param rse_settings_src: RSE settings for the src RSE.
652
+ :param operation_src: Source Operation such as read, write.
653
+ :param operation_dest: Dest Operation such as read, write.
654
+ :param domain: Domain such as lan, wan.
655
+ :param scheme: List of supported schemes.
656
+ :returns: Tuple of matching schemes (dest_scheme, src_scheme, dest_scheme_priority, src_scheme_priority).
657
+ """
658
+ operation_src = operation_src.lower()
659
+ operation_dest = operation_dest.lower()
660
+
661
+ src_candidates = copy.copy(rse_settings_src['protocols'])
662
+ dest_candidates = copy.copy(rse_settings_dest['protocols'])
663
+
664
+ # Clean up src_candidates
665
+ tbr = list()
666
+ for protocol in src_candidates:
667
+ # Check if scheme given and filter if so
668
+ if scheme:
669
+ if not isinstance(scheme, list):
670
+ scheme = scheme.split(',')
671
+ if protocol['scheme'] not in scheme:
672
+ tbr.append(protocol)
673
+ continue
674
+ prot = protocol['domains'].get(domain, {}).get(operation_src, 1)
675
+ if prot is None or prot == 0:
676
+ tbr.append(protocol)
677
+ for r in tbr:
678
+ src_candidates.remove(r)
679
+
680
+ # Clean up dest_candidates
681
+ tbr = list()
682
+ for protocol in dest_candidates:
683
+ # Check if scheme given and filter if so
684
+ if scheme:
685
+ if not isinstance(scheme, list):
686
+ scheme = scheme.split(',')
687
+ if protocol['scheme'] not in scheme:
688
+ tbr.append(protocol)
689
+ continue
690
+ prot = protocol['domains'].get(domain, {}).get(operation_dest, 1)
691
+ if prot is None or prot == 0:
692
+ tbr.append(protocol)
693
+ for r in tbr:
694
+ dest_candidates.remove(r)
695
+
696
+ if not len(src_candidates) or not len(dest_candidates):
697
+ raise exception.RSEProtocolNotSupported('No protocol for provided settings found : %s.' % str(rse_settings_dest))
698
+
699
+ # Shuffle the candidates to load-balance across equal weights.
700
+ random.shuffle(dest_candidates)
701
+ random.shuffle(src_candidates)
702
+
703
+ # Select the one with the highest priority
704
+ dest_candidates = sorted(dest_candidates, key=lambda k: k['domains'][domain][operation_dest])
705
+ src_candidates = sorted(src_candidates, key=lambda k: k['domains'][domain][operation_src])
706
+
707
+ for dest_protocol in dest_candidates:
708
+ for src_protocol in src_candidates:
709
+ if __check_compatible_scheme(dest_protocol['scheme'], src_protocol['scheme']):
710
+ return (dest_protocol['scheme'], src_protocol['scheme'], dest_protocol['domains'][domain][operation_dest], src_protocol['domains'][domain][operation_src])
711
+
712
+ raise exception.RSEProtocolNotSupported('No protocol for provided settings found : %s.' % str(rse_settings_dest))
713
+
714
+
715
+ def _retry_protocol_stat(protocol, pfn):
716
+ """
717
+ try to stat file, on fail try again 1s, 2s, 4s, 8s, 16s, 32s later. Fail is all fail
718
+
719
+ :param protocol: The protocol to use to reach this file
720
+ :param pfn: Physical file name of the target for the protocol stat
721
+ """
722
+ retries = config_get_int('client', 'protocol_stat_retries', raise_exception=False, default=6)
723
+ for attempt in range(retries):
724
+ try:
725
+ stats = protocol.stat(pfn)
726
+ return stats
727
+ except exception.RSEChecksumUnavailable as e:
728
+ # The stat succeeded here, but the checksum failed
729
+ raise e
730
+ except NotImplementedError:
731
+ break
732
+ except Exception:
733
+ sleep(2**attempt)
734
+ return protocol.stat(pfn)
735
+
736
+
737
+ def __check_compatible_scheme(dest_scheme, src_scheme):
738
+ """
739
+ Check if two schemes are compatible, such as srm and gsiftp
740
+
741
+ :param dest_scheme: Destination scheme
742
+ :param src_scheme: Source scheme
743
+ :param scheme: List of supported schemes
744
+ :returns: True if schemes are compatible, False otherwise.
745
+ """
746
+
747
+ if dest_scheme == src_scheme:
748
+ return True
749
+ if src_scheme in constants.SCHEME_MAP.get(dest_scheme, []):
750
+ return True
751
+
752
+ return False