rucio-clients 35.7.0__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 +17 -0
  2. rucio/alembicrevision.py +15 -0
  3. rucio/client/__init__.py +15 -0
  4. rucio/client/accountclient.py +433 -0
  5. rucio/client/accountlimitclient.py +183 -0
  6. rucio/client/baseclient.py +974 -0
  7. rucio/client/client.py +76 -0
  8. rucio/client/configclient.py +126 -0
  9. rucio/client/credentialclient.py +59 -0
  10. rucio/client/didclient.py +866 -0
  11. rucio/client/diracclient.py +56 -0
  12. rucio/client/downloadclient.py +1785 -0
  13. rucio/client/exportclient.py +44 -0
  14. rucio/client/fileclient.py +50 -0
  15. rucio/client/importclient.py +42 -0
  16. rucio/client/lifetimeclient.py +90 -0
  17. rucio/client/lockclient.py +109 -0
  18. rucio/client/metaconventionsclient.py +140 -0
  19. rucio/client/pingclient.py +44 -0
  20. rucio/client/replicaclient.py +454 -0
  21. rucio/client/requestclient.py +125 -0
  22. rucio/client/rseclient.py +746 -0
  23. rucio/client/ruleclient.py +294 -0
  24. rucio/client/scopeclient.py +90 -0
  25. rucio/client/subscriptionclient.py +173 -0
  26. rucio/client/touchclient.py +82 -0
  27. rucio/client/uploadclient.py +955 -0
  28. rucio/common/__init__.py +13 -0
  29. rucio/common/cache.py +74 -0
  30. rucio/common/config.py +801 -0
  31. rucio/common/constants.py +159 -0
  32. rucio/common/constraints.py +17 -0
  33. rucio/common/didtype.py +189 -0
  34. rucio/common/exception.py +1151 -0
  35. rucio/common/extra.py +36 -0
  36. rucio/common/logging.py +420 -0
  37. rucio/common/pcache.py +1408 -0
  38. rucio/common/plugins.py +153 -0
  39. rucio/common/policy.py +84 -0
  40. rucio/common/schema/__init__.py +150 -0
  41. rucio/common/schema/atlas.py +413 -0
  42. rucio/common/schema/belleii.py +408 -0
  43. rucio/common/schema/domatpc.py +401 -0
  44. rucio/common/schema/escape.py +426 -0
  45. rucio/common/schema/generic.py +433 -0
  46. rucio/common/schema/generic_multi_vo.py +412 -0
  47. rucio/common/schema/icecube.py +406 -0
  48. rucio/common/stomp_utils.py +159 -0
  49. rucio/common/stopwatch.py +55 -0
  50. rucio/common/test_rucio_server.py +148 -0
  51. rucio/common/types.py +403 -0
  52. rucio/common/utils.py +2238 -0
  53. rucio/rse/__init__.py +96 -0
  54. rucio/rse/protocols/__init__.py +13 -0
  55. rucio/rse/protocols/bittorrent.py +184 -0
  56. rucio/rse/protocols/cache.py +122 -0
  57. rucio/rse/protocols/dummy.py +111 -0
  58. rucio/rse/protocols/gfal.py +703 -0
  59. rucio/rse/protocols/globus.py +243 -0
  60. rucio/rse/protocols/gsiftp.py +92 -0
  61. rucio/rse/protocols/http_cache.py +82 -0
  62. rucio/rse/protocols/mock.py +123 -0
  63. rucio/rse/protocols/ngarc.py +209 -0
  64. rucio/rse/protocols/posix.py +250 -0
  65. rucio/rse/protocols/protocol.py +594 -0
  66. rucio/rse/protocols/rclone.py +364 -0
  67. rucio/rse/protocols/rfio.py +136 -0
  68. rucio/rse/protocols/srm.py +338 -0
  69. rucio/rse/protocols/ssh.py +413 -0
  70. rucio/rse/protocols/storm.py +206 -0
  71. rucio/rse/protocols/webdav.py +550 -0
  72. rucio/rse/protocols/xrootd.py +301 -0
  73. rucio/rse/rsemanager.py +764 -0
  74. rucio/vcsversion.py +11 -0
  75. rucio/version.py +38 -0
  76. rucio_clients-35.7.0.data/data/etc/rse-accounts.cfg.template +25 -0
  77. rucio_clients-35.7.0.data/data/etc/rucio.cfg.atlas.client.template +42 -0
  78. rucio_clients-35.7.0.data/data/etc/rucio.cfg.template +257 -0
  79. rucio_clients-35.7.0.data/data/requirements.client.txt +15 -0
  80. rucio_clients-35.7.0.data/data/rucio_client/merge_rucio_configs.py +144 -0
  81. rucio_clients-35.7.0.data/scripts/rucio +2542 -0
  82. rucio_clients-35.7.0.data/scripts/rucio-admin +2447 -0
  83. rucio_clients-35.7.0.dist-info/METADATA +50 -0
  84. rucio_clients-35.7.0.dist-info/RECORD +88 -0
  85. rucio_clients-35.7.0.dist-info/WHEEL +5 -0
  86. rucio_clients-35.7.0.dist-info/licenses/AUTHORS.rst +97 -0
  87. rucio_clients-35.7.0.dist-info/licenses/LICENSE +201 -0
  88. rucio_clients-35.7.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,955 @@
1
+ # Copyright European Organization for Nuclear Research (CERN) since 2012
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ import base64
16
+ import copy
17
+ import json
18
+ import logging
19
+ import os
20
+ import os.path
21
+ import random
22
+ import socket
23
+ import time
24
+ from typing import TYPE_CHECKING, Any, Final, Optional, Union, cast
25
+
26
+ from rucio import version
27
+ from rucio.client.client import Client
28
+ from rucio.common.config import config_get, config_get_bool, config_get_int
29
+ from rucio.common.constants import RseAttr
30
+ from rucio.common.exception import (
31
+ DataIdentifierAlreadyExists,
32
+ DataIdentifierNotFound,
33
+ FileReplicaAlreadyExists,
34
+ InputValidationError,
35
+ NoFilesUploaded,
36
+ NotAllFilesUploaded,
37
+ ResourceTemporaryUnavailable,
38
+ RSEChecksumUnavailable,
39
+ RSEOperationNotSupported,
40
+ RSEWriteBlocked,
41
+ RucioException,
42
+ ScopeNotFound,
43
+ ServiceUnavailable,
44
+ )
45
+ from rucio.common.utils import GLOBALLY_SUPPORTED_CHECKSUMS, adler32, bittorrent_v2_merkle_sha256, detect_client_location, execute, generate_uuid, make_valid_did, md5, retry, send_trace
46
+ from rucio.rse import rsemanager as rsemgr
47
+
48
+ if TYPE_CHECKING:
49
+ from collections.abc import Iterable, Mapping
50
+
51
+ from rucio.common.types import AttachDict, DatasetDict, DIDStringDict, FileToUploadDict, FileToUploadWithCollectedAndDatasetInfoDict, FileToUploadWithCollectedInfoDict, LoggerFunction, PathTypeAlias, RSESettingsDict, TraceBaseDict, TraceDict
52
+ from rucio.rse.protocols.protocol import RSEProtocol
53
+
54
+
55
+ class UploadClient:
56
+
57
+ def __init__(
58
+ self,
59
+ _client: Optional[Client] = None,
60
+ logger: Optional["LoggerFunction"] = None,
61
+ tracing: bool = True
62
+ ):
63
+ """
64
+ Initialises the basic settings for an UploadClient object
65
+
66
+ :param _client: - Optional: rucio.client.client.Client object. If None, a new object will be created.
67
+ :param logger: - Optional: logging.Logger object. If None, default logger will be used.
68
+ """
69
+ if not logger:
70
+ self.logger = logging.log
71
+ else:
72
+ self.logger = logger.log
73
+
74
+ self.client: Final[Client] = _client if _client else Client()
75
+ self.client_location = detect_client_location()
76
+ # if token should be used, use only JWT tokens
77
+ self.auth_token: Optional[str] = self.client.auth_token if len(self.client.auth_token.split(".")) == 3 else None
78
+ self.tracing = tracing
79
+ if not self.tracing:
80
+ logger(logging.DEBUG, 'Tracing is turned off.')
81
+ self.default_file_scope: Final[str] = 'user.' + self.client.account
82
+ self.rses = {}
83
+ self.rse_expressions = {}
84
+
85
+ self.trace: "TraceBaseDict" = {
86
+ 'hostname': socket.getfqdn(),
87
+ 'account': self.client.account,
88
+ 'eventType': 'upload',
89
+ 'eventVersion': version.RUCIO_VERSION[0],
90
+ 'vo': self.client.vo if self.client.vo != 'def' else None
91
+ }
92
+
93
+ def upload(
94
+ self,
95
+ items: "Iterable[FileToUploadDict]",
96
+ summary_file_path: Optional[str] = None,
97
+ traces_copy_out: Optional[list["TraceBaseDict"]] = None,
98
+ ignore_availability: bool = False,
99
+ activity: Optional[str] = None
100
+ ) -> int:
101
+ """
102
+ :param items: List of dictionaries. Each dictionary describing a file to upload. Keys:
103
+ path - path of the file that will be uploaded
104
+ rse - rse expression/name (e.g. 'CERN-PROD_DATADISK') where to upload the file
105
+ did_scope - Optional: custom did scope (Default: user.<account>)
106
+ did_name - Optional: custom did name (Default: name of the file)
107
+ dataset_scope - Optional: custom dataset scope
108
+ dataset_name - Optional: custom dataset name
109
+ dataset_meta - Optional: custom metadata for dataset
110
+ impl - Optional: name of the protocol implementation to be used to upload this item.
111
+ force_scheme - Optional: force a specific scheme (if PFN upload this will be overwritten) (Default: None)
112
+ pfn - Optional: use a given PFN (this sets no_register to True, and no_register becomes mandatory)
113
+ no_register - Optional: if True, the file will not be registered in the rucio catalogue
114
+ register_after_upload - Optional: if True, the file will be registered after successful upload
115
+ lifetime - Optional: the lifetime of the file after it was uploaded
116
+ transfer_timeout - Optional: time after the upload will be aborted
117
+ guid - Optional: guid of the file
118
+ recursive - Optional: if set, parses the folder structure recursively into collections
119
+ :param summary_file_path: Optional: a path where a summary in form of a json file will be stored
120
+ :param traces_copy_out: reference to an external list, where the traces should be uploaded
121
+ :param ignore_availability: ignore the availability of a RSE
122
+ :param activity: the activity set to the rule if no dataset is specified
123
+
124
+ :returns: 0 on success
125
+
126
+ :raises InputValidationError: if any input arguments are in a wrong format
127
+ :raises RSEWriteBlocked: if a given RSE is not available for writing
128
+ :raises NoFilesUploaded: if no files were successfully uploaded
129
+ :raises NotAllFilesUploaded: if not all files were successfully uploaded
130
+ """
131
+ # helper to get rse from rse_expression:
132
+ def _pick_random_rse(rse_expression: str) -> dict[str, Any]:
133
+ rses = [r['rse'] for r in self.client.list_rses(rse_expression)] # can raise InvalidRSEExpression
134
+ random.shuffle(rses)
135
+ return rses[0]
136
+
137
+ logger = self.logger
138
+ self.trace['uuid'] = generate_uuid()
139
+
140
+ # check given sources, resolve dirs into files, and collect meta infos
141
+ files = self._collect_and_validate_file_info(items)
142
+ logger(logging.DEBUG, 'Num. of files that upload client is processing: {}'.format(len(files)))
143
+
144
+ # check if RSE of every file is available for writing
145
+ # and cache rse settings
146
+ registered_dataset_dids = set()
147
+ registered_file_dids = set()
148
+ rse_expression = None
149
+ for file in files:
150
+ rse_expression = file['rse']
151
+ rse = self.rse_expressions.setdefault(rse_expression, _pick_random_rse(rse_expression))
152
+
153
+ if not self.rses.get(rse):
154
+ rse_settings = self.rses.setdefault(rse, rsemgr.get_rse_info(rse, vo=self.client.vo))
155
+ if not ignore_availability and rse_settings['availability_write'] != 1:
156
+ raise RSEWriteBlocked('%s is not available for writing. No actions have been taken' % rse)
157
+
158
+ dataset_scope = file.get('dataset_scope')
159
+ dataset_name = file.get('dataset_name')
160
+ file['rse'] = rse
161
+ if dataset_scope and dataset_name:
162
+ dataset_did_str = ('%s:%s' % (dataset_scope, dataset_name))
163
+ file['dataset_did_str'] = dataset_did_str
164
+ registered_dataset_dids.add(dataset_did_str)
165
+
166
+ registered_file_dids.add('%s:%s' % (file['did_scope'], file['did_name']))
167
+ wrong_dids = registered_file_dids.intersection(registered_dataset_dids)
168
+ if len(wrong_dids):
169
+ raise InputValidationError('DIDs used to address both files and datasets: %s' % str(wrong_dids))
170
+ logger(logging.DEBUG, 'Input validation done.')
171
+
172
+ # clear this set again to ensure that we only try to register datasets once
173
+ registered_dataset_dids = set()
174
+ num_succeeded = 0
175
+ summary = []
176
+ for file in files:
177
+ basename = file['basename']
178
+ logger(logging.INFO, 'Preparing upload for file %s' % basename)
179
+
180
+ no_register = file.get('no_register')
181
+ register_after_upload = file.get('register_after_upload') and not no_register
182
+ pfn = file.get('pfn')
183
+ force_scheme = file.get('force_scheme')
184
+ impl = file.get('impl')
185
+ delete_existing = False
186
+
187
+ trace = copy.deepcopy(self.trace)
188
+ # appending trace to list reference, if the reference exists
189
+ if traces_copy_out is not None:
190
+ traces_copy_out.append(trace)
191
+
192
+ rse = file['rse']
193
+ trace['scope'] = file['did_scope']
194
+ trace['datasetScope'] = file.get('dataset_scope', '')
195
+ trace['dataset'] = file.get('dataset_name', '')
196
+ trace['remoteSite'] = rse
197
+ trace['filesize'] = file['bytes']
198
+
199
+ file_did = {'scope': file['did_scope'], 'name': file['did_name']}
200
+ dataset_did_str = file.get('dataset_did_str')
201
+ rse_settings = self.rses[rse]
202
+ rse_sign_service = rse_settings.get('sign_url', None)
203
+ is_deterministic = rse_settings.get('deterministic', True)
204
+ if not is_deterministic and not pfn:
205
+ logger(logging.ERROR, 'PFN has to be defined for NON-DETERMINISTIC RSE.')
206
+ continue
207
+ if pfn and is_deterministic:
208
+ logger(logging.WARNING, 'Upload with given pfn implies that no_register is True, except non-deterministic RSEs')
209
+ no_register = True
210
+
211
+ # resolving local area networks
212
+ domain = 'wan'
213
+ rse_attributes = {}
214
+ try:
215
+ rse_attributes = self.client.list_rse_attributes(rse)
216
+ except:
217
+ logger(logging.WARNING, 'Attributes of the RSE: %s not available.' % rse)
218
+ if (self.client_location and 'lan' in rse_settings['domain'] and RseAttr.SITE in rse_attributes):
219
+ if self.client_location['site'] == rse_attributes[RseAttr.SITE]:
220
+ domain = 'lan'
221
+ logger(logging.DEBUG, '{} domain is used for the upload'.format(domain))
222
+
223
+ # FIXME:
224
+ # Rewrite preferred_impl selection - also check test_upload.py/test_download.py and fix impl order (see FIXME there)
225
+ #
226
+ # if not impl and not force_scheme:
227
+ # impl = self.preferred_impl(rse_settings, domain)
228
+
229
+ if not no_register and not register_after_upload:
230
+ self._register_file(file, registered_dataset_dids, ignore_availability=ignore_availability, activity=activity)
231
+
232
+ # if register_after_upload, file should be overwritten if it is not registered
233
+ # otherwise if file already exists on RSE we're done
234
+ if register_after_upload:
235
+ if rsemgr.exists(rse_settings, pfn if pfn else file_did, domain=domain, scheme=force_scheme, impl=impl, auth_token=self.auth_token, vo=self.client.vo, logger=logger):
236
+ try:
237
+ self.client.get_did(file['did_scope'], file['did_name'])
238
+ logger(logging.INFO, 'File already registered. Skipping upload.')
239
+ trace['stateReason'] = 'File already exists'
240
+ continue
241
+ except DataIdentifierNotFound:
242
+ logger(logging.INFO, 'File already exists on RSE. Previous left overs will be overwritten.')
243
+ delete_existing = True
244
+ elif not is_deterministic and not no_register:
245
+ if rsemgr.exists(rse_settings, pfn, domain=domain, scheme=force_scheme, impl=impl, auth_token=self.auth_token, vo=self.client.vo, logger=logger):
246
+ logger(logging.INFO, 'File already exists on RSE with given pfn. Skipping upload. Existing replica has to be removed first.')
247
+ trace['stateReason'] = 'File already exists'
248
+ continue
249
+ elif rsemgr.exists(rse_settings, file_did, domain=domain, scheme=force_scheme, impl=impl, auth_token=self.auth_token, vo=self.client.vo, logger=logger):
250
+ logger(logging.INFO, 'File already exists on RSE with different pfn. Skipping upload.')
251
+ trace['stateReason'] = 'File already exists'
252
+ continue
253
+ else:
254
+ if rsemgr.exists(rse_settings, pfn if pfn else file_did, domain=domain, scheme=force_scheme, impl=impl, auth_token=self.auth_token, vo=self.client.vo, logger=logger):
255
+ logger(logging.INFO, 'File already exists on RSE. Skipping upload')
256
+ trace['stateReason'] = 'File already exists'
257
+ continue
258
+
259
+ # protocol handling and upload
260
+ protocols = rsemgr.get_protocols_ordered(rse_settings=rse_settings, operation='write', scheme=force_scheme, domain=domain, impl=impl)
261
+ protocols.reverse()
262
+ success = False
263
+ state_reason = ''
264
+ logger(logging.DEBUG, str(protocols))
265
+ while not success and len(protocols):
266
+ protocol = protocols.pop()
267
+ cur_scheme = protocol['scheme']
268
+ logger(logging.INFO, 'Trying upload with %s to %s' % (cur_scheme, rse))
269
+ lfn = {}
270
+ lfn['filename'] = basename
271
+ lfn['scope'] = file['did_scope']
272
+ lfn['name'] = file['did_name']
273
+
274
+ for checksum_name in GLOBALLY_SUPPORTED_CHECKSUMS:
275
+ if checksum_name in file:
276
+ lfn[checksum_name] = file[checksum_name]
277
+
278
+ lfn['filesize'] = file['bytes']
279
+
280
+ sign_service = None
281
+ if cur_scheme == 'https':
282
+ sign_service = rse_sign_service
283
+
284
+ trace['protocol'] = cur_scheme
285
+ trace['transferStart'] = time.time()
286
+ logger(logging.DEBUG, 'Processing upload with the domain: {}'.format(domain))
287
+ try:
288
+ pfn = self._upload_item(rse_settings=rse_settings,
289
+ rse_attributes=rse_attributes,
290
+ lfn=lfn,
291
+ source_dir=file['dirname'],
292
+ domain=domain,
293
+ impl=impl,
294
+ force_scheme=cur_scheme,
295
+ force_pfn=pfn,
296
+ transfer_timeout=file.get('transfer_timeout'),
297
+ delete_existing=delete_existing,
298
+ sign_service=sign_service)
299
+ logger(logging.DEBUG, 'Upload done.')
300
+ success = True
301
+ file['upload_result'] = {0: True, 1: None, 'success': True, 'pfn': pfn} # TODO: needs to be removed
302
+ except (ServiceUnavailable, ResourceTemporaryUnavailable, RSEOperationNotSupported, RucioException) as error:
303
+ logger(logging.WARNING, 'Upload attempt failed')
304
+ logger(logging.INFO, 'Exception: %s' % str(error), exc_info=True)
305
+ state_reason = str(error)
306
+
307
+ if success:
308
+ trace['transferEnd'] = time.time()
309
+ trace['clientState'] = 'DONE'
310
+ file['state'] = 'A'
311
+ logger(logging.INFO, 'Successfully uploaded file %s' % basename)
312
+ self._send_trace(cast("TraceDict", trace))
313
+
314
+ if summary_file_path:
315
+ summary.append(copy.deepcopy(file))
316
+
317
+ registration_succeeded = True
318
+ if not no_register:
319
+ if register_after_upload:
320
+ self._register_file(file, registered_dataset_dids, ignore_availability=ignore_availability, activity=activity)
321
+ else:
322
+ replica_for_api = self._convert_file_for_api(file)
323
+ try:
324
+ self.client.update_replicas_states(rse, files=[replica_for_api])
325
+ except Exception as error:
326
+ registration_succeeded = False
327
+ logger(logging.ERROR, 'Failed to update replica state for file {}'.format(basename))
328
+ logger(logging.DEBUG, 'Details: {}'.format(str(error)))
329
+
330
+ # add file to dataset if needed
331
+ if dataset_did_str and not no_register:
332
+ try:
333
+ self.client.attach_dids(file['dataset_scope'], file['dataset_name'], [file_did]) # type: ignore (`dataset_scope` and `dataset_name` always exist if `dataset_did_str`)
334
+ except Exception as error:
335
+ registration_succeeded = False
336
+ logger(logging.ERROR, 'Failed to attach file to the dataset')
337
+ logger(logging.DEBUG, 'Attaching to dataset {}'.format(str(error)))
338
+
339
+ # only report success if the registration operations succeeded as well
340
+ if registration_succeeded:
341
+ num_succeeded += 1
342
+ else:
343
+ trace['clientState'] = 'FAILED'
344
+ trace['stateReason'] = state_reason
345
+ self._send_trace(cast('TraceDict', trace))
346
+ logger(logging.ERROR, 'Failed to upload file %s' % basename)
347
+
348
+ if summary_file_path:
349
+ logger(logging.DEBUG, 'Summary will be available at {}'.format(summary_file_path))
350
+ final_summary = {}
351
+ for file in summary:
352
+ file_scope = file['did_scope']
353
+ file_name = file['did_name']
354
+ file_did_str = '%s:%s' % (file_scope, file_name)
355
+ final_summary[file_did_str] = {'scope': file_scope,
356
+ 'name': file_name,
357
+ 'bytes': file['bytes'],
358
+ 'rse': file['rse'],
359
+ 'pfn': file['upload_result'].get('pfn', ''),
360
+ 'guid': file['meta']['guid']}
361
+
362
+ for checksum_name in GLOBALLY_SUPPORTED_CHECKSUMS:
363
+ if checksum_name in file:
364
+ final_summary[file_did_str][checksum_name] = file[checksum_name]
365
+
366
+ with open(summary_file_path, 'w') as summary_file:
367
+ json.dump(final_summary, summary_file, sort_keys=True, indent=1)
368
+
369
+ if num_succeeded == 0:
370
+ raise NoFilesUploaded()
371
+ elif num_succeeded != len(files):
372
+ raise NotAllFilesUploaded()
373
+ return 0
374
+
375
+ def _add_bittorrent_meta(self, file: "Mapping[str, Any]") -> None:
376
+ pieces_root, pieces_layers, piece_length = bittorrent_v2_merkle_sha256(os.path.join(file['dirname'], file['basename']))
377
+ bittorrent_meta = {
378
+ 'bittorrent_pieces_root': base64.b64encode(pieces_root).decode(),
379
+ 'bittorrent_pieces_layers': base64.b64encode(pieces_layers).decode(),
380
+ 'bittorrent_piece_length': piece_length,
381
+ }
382
+ self.client.set_metadata_bulk(scope=file['did_scope'], name=file['did_name'], meta=bittorrent_meta)
383
+ self.logger(logging.INFO, f"Added bittorrent metadata to file DID {file['did_scope']}:{file['did_name']}")
384
+
385
+ def _register_file(
386
+ self,
387
+ file: "Mapping[str, Any]",
388
+ registered_dataset_dids: set[str],
389
+ ignore_availability: bool = False,
390
+ activity: Optional[str] = None
391
+ ) -> None:
392
+ """
393
+ Registers the given file in Rucio. Creates a dataset if
394
+ needed. Registers the file DID and creates the replication
395
+ rule if needed. Adds a replica to the file did.
396
+ (This function is meant to be used as class internal only)
397
+
398
+ :param file: dictionary describing the file
399
+ :param registered_dataset_dids: set of dataset dids that were already registered
400
+ :param ignore_availability: ignore the availability of a RSE
401
+ :param activity: the activity set to the rule if no dataset is specified
402
+
403
+ :raises DataIdentifierAlreadyExists: if file DID is already registered and the checksums do not match
404
+ """
405
+ logger = self.logger
406
+ logger(logging.DEBUG, 'Registering file')
407
+
408
+ # verification whether the scope exists
409
+ account_scopes = []
410
+ try:
411
+ account_scopes = self.client.list_scopes_for_account(self.client.account)
412
+ except ScopeNotFound:
413
+ pass
414
+ if account_scopes and file['did_scope'] not in account_scopes:
415
+ logger(logging.WARNING, 'Scope {} not found for the account {}.'.format(file['did_scope'], self.client.account))
416
+
417
+ rse = file['rse']
418
+ dataset_did_str = file.get('dataset_did_str')
419
+ # register a dataset if we need to
420
+ if dataset_did_str and dataset_did_str not in registered_dataset_dids:
421
+ registered_dataset_dids.add(dataset_did_str)
422
+ try:
423
+ logger(logging.DEBUG, 'Trying to create dataset: %s' % dataset_did_str)
424
+ self.client.add_dataset(scope=file['dataset_scope'],
425
+ name=file['dataset_name'],
426
+ meta=file.get('dataset_meta'),
427
+ rules=[{'account': self.client.account,
428
+ 'copies': 1,
429
+ 'rse_expression': rse,
430
+ 'grouping': 'DATASET',
431
+ 'lifetime': file.get('lifetime')}])
432
+ logger(logging.INFO, 'Successfully created dataset %s' % dataset_did_str)
433
+ except DataIdentifierAlreadyExists:
434
+ logger(logging.INFO, 'Dataset %s already exists - no rule will be created' % dataset_did_str)
435
+ if file.get('lifetime') is not None:
436
+ raise InputValidationError('Dataset %s exists and lifetime %s given. Prohibited to modify parent dataset lifetime.' % (dataset_did_str,
437
+ file.get('lifetime')))
438
+ else:
439
+ logger(logging.DEBUG, 'Skipping dataset registration')
440
+
441
+ file_scope = file['did_scope']
442
+ file_name = file['did_name']
443
+ file_did = {'scope': file_scope, 'name': file_name}
444
+ replica_for_api = self._convert_file_for_api(file)
445
+ try:
446
+ # if the remote checksum is different this did must not be used
447
+ meta = self.client.get_metadata(file_scope, file_name)
448
+ logger(logging.INFO, 'File DID already exists')
449
+ logger(logging.DEBUG, 'local checksum: %s, remote checksum: %s' % (file['adler32'], meta['adler32']))
450
+
451
+ if str(meta['adler32']).lstrip('0') != str(file['adler32']).lstrip('0'):
452
+ logger(logging.ERROR, 'Local checksum %s does not match remote checksum %s' % (file['adler32'], meta['adler32']))
453
+ raise DataIdentifierAlreadyExists
454
+
455
+ # add file to rse if it is not registered yet
456
+ replicastate = list(self.client.list_replicas([file_did], all_states=True))
457
+ if rse not in replicastate[0]['rses']:
458
+ self.client.add_replicas(rse=rse, files=[replica_for_api])
459
+ logger(logging.INFO, 'Successfully added replica in Rucio catalogue at %s' % rse)
460
+ except DataIdentifierNotFound:
461
+ logger(logging.DEBUG, 'File DID does not exist')
462
+ self.client.add_replicas(rse=rse, files=[replica_for_api])
463
+ if config_get_bool('client', 'register_bittorrent_meta', default=False):
464
+ self._add_bittorrent_meta(file=file)
465
+ logger(logging.INFO, 'Successfully added replica in Rucio catalogue at %s' % rse)
466
+ if not dataset_did_str:
467
+ # only need to add rules for files if no dataset is given
468
+ self.client.add_replication_rule([file_did], copies=1, rse_expression=rse, lifetime=file.get('lifetime'), ignore_availability=ignore_availability, activity=activity)
469
+ logger(logging.INFO, 'Successfully added replication rule at %s' % rse)
470
+
471
+ def _get_file_guid(self, file: "Mapping[str, Any]") -> str:
472
+ """
473
+ Get the guid of a file, trying different strategies
474
+ (This function is meant to be used as class internal only)
475
+
476
+ :param file: dictionary describing the file
477
+
478
+ :returns: the guid
479
+ """
480
+ guid = file.get('guid')
481
+ if not guid and 'pool.root' in file['basename'].lower() and not file.get('no_register'):
482
+ status, output, err = execute('pool_extractFileIdentifier %s' % file['path'])
483
+ if status != 0:
484
+ msg = 'Trying to upload ROOT files but pool_extractFileIdentifier tool can not be found.\n'
485
+ msg += 'Setup your ATHENA environment and try again.'
486
+ raise RucioException(msg)
487
+ try:
488
+ guid = output.splitlines()[-1].split()[0].replace('-', '').lower()
489
+ except Exception:
490
+ raise RucioException('Error extracting GUID from output of pool_extractFileIdentifier')
491
+ elif guid:
492
+ guid = guid.replace('-', '')
493
+ else:
494
+ guid = generate_uuid()
495
+ return guid
496
+
497
+ def _collect_file_info(
498
+ self,
499
+ filepath: "PathTypeAlias",
500
+ item: "FileToUploadDict"
501
+ ) -> "FileToUploadWithCollectedInfoDict":
502
+ """
503
+ Collects infos (e.g. size, checksums, etc.) about the file and
504
+ returns them as a dictionary
505
+ (This function is meant to be used as class internal only)
506
+
507
+ :param filepath: path where the file is stored
508
+ :param item: input options for the given file
509
+
510
+ :returns: a dictionary containing all collected info and the input options
511
+ """
512
+ new_item = copy.deepcopy(item)
513
+ new_item = cast("FileToUploadWithCollectedInfoDict", new_item)
514
+ new_item['path'] = filepath
515
+ new_item['dirname'] = os.path.dirname(filepath)
516
+ new_item['basename'] = os.path.basename(filepath)
517
+
518
+ new_item['bytes'] = os.stat(filepath).st_size
519
+ new_item['adler32'] = adler32(filepath)
520
+ new_item['md5'] = md5(filepath)
521
+ new_item['meta'] = {'guid': self._get_file_guid(new_item)}
522
+ new_item['state'] = 'C'
523
+ if not new_item.get('did_scope'):
524
+ new_item['did_scope'] = self.default_file_scope
525
+ if not new_item.get('did_name'):
526
+ new_item['did_name'] = new_item['basename']
527
+
528
+ return new_item
529
+
530
+ def _collect_and_validate_file_info(self, items: "Iterable[FileToUploadDict]") -> list["FileToUploadWithCollectedInfoDict"]:
531
+ """
532
+ Checks if there are any inconsistencies within the given input
533
+ options and stores the output of _collect_file_info for every file
534
+ (This function is meant to be used as class internal only)
535
+
536
+ :param filepath: list of dictionaries with all input files and options
537
+
538
+ :returns: a list of dictionaries containing all descriptions of the files to upload
539
+
540
+ :raises InputValidationError: if an input option has a wrong format
541
+ """
542
+ logger = self.logger
543
+ files: list["FileToUploadWithCollectedInfoDict"] = []
544
+ for item in items:
545
+ path = item.get('path')
546
+ pfn = item.get('pfn')
547
+ recursive = item.get('recursive')
548
+ if not path:
549
+ logger(logging.WARNING, 'Skipping source entry because the key "path" is missing')
550
+ continue
551
+ if not item.get('rse'):
552
+ logger(logging.WARNING, 'Skipping file %s because no rse was given' % path)
553
+ continue
554
+ if pfn:
555
+ item['force_scheme'] = pfn.split(':')[0]
556
+ impl = item.get('impl')
557
+ if impl:
558
+ impl_split = impl.split('.')
559
+ if len(impl_split) == 1:
560
+ impl = 'rucio.rse.protocols.' + impl + '.Default'
561
+ else:
562
+ impl = 'rucio.rse.protocols.' + impl
563
+ item['impl'] = impl
564
+ if os.path.isdir(path) and not recursive:
565
+ dname, subdirs, fnames = next(os.walk(path))
566
+ for fname in fnames:
567
+ file = self._collect_file_info(os.path.join(dname, fname), item)
568
+ files.append(file)
569
+ if not len(fnames) and not len(subdirs):
570
+ logger(logging.WARNING, 'Skipping %s because it is empty.' % dname)
571
+ elif not len(fnames):
572
+ logger(logging.WARNING, 'Skipping %s because it has no files in it. Subdirectories are not supported.' % dname)
573
+ elif os.path.isdir(path) and recursive:
574
+ files.extend(cast("list[FileToUploadWithCollectedInfoDict]", self._recursive(item)))
575
+ elif os.path.isfile(path) and not recursive:
576
+ file = self._collect_file_info(path, item)
577
+ files.append(file)
578
+ elif os.path.isfile(path) and recursive:
579
+ logger(logging.WARNING, 'Skipping %s because of --recursive flag' % path)
580
+ else:
581
+ logger(logging.WARNING, 'No such file or directory: %s' % path)
582
+
583
+ if not len(files):
584
+ raise InputValidationError('No valid input files given')
585
+
586
+ return files
587
+
588
+ def _convert_file_for_api(self, file: "Mapping[str, Any]") -> dict[str, Any]:
589
+ """
590
+ Creates a new dictionary that contains only the values
591
+ that are needed for the upload with the correct keys
592
+ (This function is meant to be used as class internal only)
593
+
594
+ :param file: dictionary describing a file to upload
595
+
596
+ :returns: dictionary containing not more then the needed values for the upload
597
+ """
598
+ replica = {}
599
+ replica['scope'] = file['did_scope']
600
+ replica['name'] = file['did_name']
601
+ replica['bytes'] = file['bytes']
602
+ replica['adler32'] = file['adler32']
603
+ replica['md5'] = file['md5']
604
+ replica['meta'] = file['meta']
605
+ replica['state'] = file['state']
606
+ pfn = file.get('pfn')
607
+ if pfn:
608
+ replica['pfn'] = pfn
609
+ return replica
610
+
611
+ def _upload_item(
612
+ self,
613
+ rse_settings: "RSESettingsDict",
614
+ rse_attributes: dict[str, Any],
615
+ lfn: dict[str, Union[str, int]],
616
+ source_dir: Optional[str] = None,
617
+ domain: str = 'wan',
618
+ impl: Optional[str] = None,
619
+ force_pfn: Optional[str] = None,
620
+ force_scheme: Optional[str] = None,
621
+ transfer_timeout: Optional[int] = None,
622
+ delete_existing: bool = False,
623
+ sign_service: Optional[str] = None
624
+ ) -> Optional[str]:
625
+ """
626
+ Uploads a file to the connected storage.
627
+
628
+ :param rse_settings: dictionary containing the RSE settings
629
+ :param rse_attributes: dictionary containing the RSE attribute key value pairs
630
+ :param lfn: a single dict containing 'scope' and 'name'.
631
+ Example:
632
+ {'name': '1_rse_local_put.raw', 'scope': 'user.jdoe', 'filesize': 42, 'adler32': '87HS3J968JSNWID'}
633
+ 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').
634
+ :param source_dir: path to the local directory including the source files
635
+ :param force_pfn: use the given PFN -- can lead to dark data, use sparingly
636
+ :param force_scheme: use the given protocol scheme, overriding the protocol priority in the RSE description
637
+ :param transfer_timeout: set this timeout (in seconds) for the transfers, for protocols that support it
638
+ :param sign_service: use the given service (e.g. gcs, s3, swift) to sign the URL
639
+
640
+ :raises RucioException(msg): general exception with msg for more details.
641
+ """
642
+ logger = self.logger
643
+
644
+ # Construct protocol for write operation.
645
+ # IMPORTANT: All upload stat() checks are always done with the write_protocol EXCEPT for cloud resources (signed URL for write cannot be used for read)
646
+ protocol_write = self._create_protocol(rse_settings, 'write', force_scheme=force_scheme, domain=domain, impl=impl)
647
+
648
+ base_name = lfn.get('filename', lfn['name'])
649
+ name = lfn.get('name', base_name)
650
+ scope = lfn['scope']
651
+
652
+ # Conditional lfn properties
653
+ if 'adler32' not in lfn and 'md5' not in lfn:
654
+ logger(logging.WARNING, 'Missing checksum for file %s:%s' % (lfn['scope'], name))
655
+
656
+ # Getting pfn
657
+ pfn = None
658
+ signed_read_pfn = None
659
+ try:
660
+ pfn = list(protocol_write.lfns2pfns(make_valid_did(lfn)).values())[0]
661
+ logger(logging.DEBUG, 'The PFN created from the LFN: {}'.format(pfn))
662
+ except Exception as error:
663
+ logger(logging.WARNING, 'Failed to create PFN for LFN: %s' % lfn)
664
+ logger(logging.DEBUG, str(error), exc_info=True)
665
+ if force_pfn:
666
+ pfn = force_pfn
667
+ logger(logging.DEBUG, 'The given PFN is used: {}'.format(pfn))
668
+
669
+ # Auth. mostly for object stores
670
+ if sign_service:
671
+ protocol_read = self._create_protocol(rse_settings, 'read', domain=domain, impl=impl)
672
+ if pfn is not None:
673
+ signed_read_pfn = self.client.get_signed_url(rse_settings['rse'], sign_service, 'read', pfn)
674
+ pfn = self.client.get_signed_url(rse_settings['rse'], sign_service, 'write', pfn)
675
+
676
+ # Create a name of tmp file if renaming operation is supported
677
+ pfn_tmp = cast(str, '%s.rucio.upload' % pfn if protocol_write.renaming else pfn)
678
+ signed_read_pfn_tmp = '%s.rucio.upload' % signed_read_pfn if protocol_write.renaming else signed_read_pfn
679
+
680
+ # Either DID exists or not register_after_upload
681
+ if protocol_write.overwrite is False and delete_existing is False:
682
+ if sign_service:
683
+ # Construct protocol for read ONLY for cloud resources and get signed URL for GET
684
+ if protocol_read.exists(signed_read_pfn):
685
+ raise FileReplicaAlreadyExists('File %s in scope %s already exists on storage as PFN %s' % (name, scope, pfn)) # wrong exception ?
686
+ elif protocol_write.exists(pfn):
687
+ raise FileReplicaAlreadyExists('File %s in scope %s already exists on storage as PFN %s' % (name, scope, pfn)) # wrong exception ?
688
+
689
+ # Removing tmp from earlier attempts
690
+ if (not sign_service and protocol_write.exists(pfn_tmp)) or (sign_service and protocol_read.exists(signed_read_pfn_tmp)):
691
+ logger(logging.DEBUG, 'Removing remains of previous upload attempts.')
692
+ try:
693
+ # Construct protocol for delete operation.
694
+ protocol_delete = self._create_protocol(rse_settings, 'delete', force_scheme=force_scheme, domain=domain, impl=impl)
695
+ delete_pfn = '%s.rucio.upload' % list(protocol_delete.lfns2pfns(make_valid_did(lfn)).values())[0]
696
+ if sign_service:
697
+ delete_pfn = self.client.get_signed_url(rse_settings['rse'], sign_service, 'delete', delete_pfn)
698
+ protocol_delete.delete(delete_pfn)
699
+ protocol_delete.close()
700
+ except Exception as error:
701
+ raise RSEOperationNotSupported('Unable to remove temporary file %s.rucio.upload: %s' % (pfn, str(error)))
702
+
703
+ # Removing not registered files from earlier attempts
704
+ if delete_existing:
705
+ logger(logging.DEBUG, 'Removing not-registered remains of previous upload attempts.')
706
+ try:
707
+ # Construct protocol for delete operation.
708
+ protocol_delete = self._create_protocol(rse_settings, 'delete', force_scheme=force_scheme, domain=domain, impl=impl)
709
+ delete_pfn = '%s' % list(protocol_delete.lfns2pfns(make_valid_did(lfn)).values())[0]
710
+ if sign_service:
711
+ delete_pfn = self.client.get_signed_url(rse_settings['rse'], sign_service, 'delete', delete_pfn)
712
+ protocol_delete.delete(delete_pfn)
713
+ protocol_delete.close()
714
+ except Exception as error:
715
+ raise RSEOperationNotSupported('Unable to remove file %s: %s' % (pfn, str(error)))
716
+
717
+ # Process the upload of the tmp file
718
+ try:
719
+ retry(protocol_write.put, base_name, pfn_tmp, source_dir, transfer_timeout=transfer_timeout)(mtries=2, logger=logger)
720
+ logger(logging.INFO, 'Successful upload of temporary file. {}'.format(pfn_tmp))
721
+ except Exception as error:
722
+ raise RSEOperationNotSupported(str(error))
723
+
724
+ # Is stat after that upload allowed?
725
+ skip_upload_stat = rse_attributes.get(RseAttr.SKIP_UPLOAD_STAT, False)
726
+ self.logger(logging.DEBUG, 'skip_upload_stat=%s', skip_upload_stat)
727
+
728
+ # Checksum verification, obsolete, see Gabriele changes.
729
+ if not skip_upload_stat:
730
+ try:
731
+ stats = self._retry_protocol_stat(protocol_write, pfn_tmp)
732
+ if not isinstance(stats, dict):
733
+ raise RucioException('Could not get protocol.stats for given PFN: %s' % pfn)
734
+
735
+ # The checksum and filesize check
736
+ if ('filesize' in stats) and ('filesize' in lfn):
737
+ self.logger(logging.DEBUG, 'Filesize: Expected=%s Found=%s' % (lfn['filesize'], stats['filesize']))
738
+ if int(stats['filesize']) != int(lfn['filesize']):
739
+ raise RucioException('Filesize mismatch. Source: %s Destination: %s' % (lfn['filesize'], stats['filesize']))
740
+ if rse_settings['verify_checksum'] is not False:
741
+ if ('adler32' in stats) and ('adler32' in lfn):
742
+ self.logger(logging.DEBUG, 'Checksum: Expected=%s Found=%s' % (lfn['adler32'], stats['adler32']))
743
+ if str(stats['adler32']).lstrip('0') != str(lfn['adler32']).lstrip('0'):
744
+ raise RucioException('Checksum mismatch. Source: %s Destination: %s' % (lfn['adler32'], stats['adler32']))
745
+
746
+ except Exception as error:
747
+ raise error
748
+
749
+ # The upload finished successful and the file can be renamed
750
+ try:
751
+ if protocol_write.renaming:
752
+ logger(logging.DEBUG, 'Renaming file %s to %s' % (pfn_tmp, pfn))
753
+ protocol_write.rename(pfn_tmp, pfn)
754
+ except Exception:
755
+ raise RucioException('Unable to rename the tmp file %s.' % pfn_tmp)
756
+
757
+ protocol_write.close()
758
+
759
+ return pfn
760
+
761
+ def _retry_protocol_stat(
762
+ self,
763
+ protocol: "RSEProtocol",
764
+ pfn: str
765
+ ) -> dict[str, Any]:
766
+ """
767
+ Try to stat file, on fail try again 1s, 2s, 4s, 8s, 16s, 32s later. Fail is all fail
768
+ :param protocol: The protocol to use to reach this file
769
+ :param pfn: Physical file name of the target for the protocol stat
770
+ """
771
+ retries = config_get_int('client', 'protocol_stat_retries', raise_exception=False, default=6)
772
+ for attempt in range(retries):
773
+ try:
774
+ self.logger(logging.DEBUG, 'stat: pfn=%s' % pfn)
775
+ stats = protocol.stat(pfn)
776
+
777
+ if int(stats['filesize']) == 0:
778
+ raise Exception('Filesize came back as 0. Potential storage race condition, need to retry.')
779
+
780
+ return stats
781
+ except RSEChecksumUnavailable as error:
782
+ # The stat succeeded here, but the checksum failed
783
+ raise error
784
+ except Exception as error:
785
+ self.logger(logging.DEBUG, 'stat: unexpected error=%s' % error)
786
+ fail_str = ['The requested service is not available at the moment', 'Permission refused']
787
+ if any(x in str(error) for x in fail_str):
788
+ raise error
789
+ self.logger(logging.DEBUG, 'stat: unknown edge case, retrying in %ss' % 2**attempt)
790
+ time.sleep(2**attempt)
791
+ return protocol.stat(pfn)
792
+
793
+ def _create_protocol(
794
+ self,
795
+ rse_settings: "RSESettingsDict",
796
+ operation: str,
797
+ impl: Optional[str] = None,
798
+ force_scheme: Optional[str] = None,
799
+ domain: str = 'wan'
800
+ ) -> "RSEProtocol":
801
+ """
802
+ Protocol construction.
803
+ :param rse_settings: rse_settings
804
+ :param operation: activity, e.g. read, write, delete etc.
805
+ :param force_scheme: custom scheme
806
+ :param auth_token: Optionally passing JSON Web Token (OIDC) string for authentication
807
+ """
808
+ try:
809
+ protocol = rsemgr.create_protocol(rse_settings, operation, scheme=force_scheme, domain=domain, impl=impl, auth_token=self.auth_token, logger=self.logger)
810
+ protocol.connect()
811
+ except Exception as error:
812
+ self.logger(logging.WARNING, 'Failed to create protocol for operation: %s' % operation)
813
+ self.logger(logging.DEBUG, 'scheme: %s, exception: %s' % (force_scheme, error))
814
+ raise error
815
+ return protocol
816
+
817
+ def _send_trace(self, trace: "TraceDict") -> None:
818
+ """
819
+ Checks if sending trace is allowed and send the trace.
820
+
821
+ :param trace: the trace
822
+ """
823
+ if self.tracing:
824
+ send_trace(trace, self.client.trace_host, self.client.user_agent)
825
+
826
+ def _recursive(self, item: "FileToUploadDict") -> list["FileToUploadWithCollectedAndDatasetInfoDict"]:
827
+ """
828
+ If the --recursive flag is set, it replicates the folder structure recursively into collections
829
+ A folder only can have either other folders inside or files, but not both of them
830
+ - If it has folders, the root folder will be a container
831
+ - If it has files, the root folder will be a dataset
832
+ - If it is empty, it does not create anything
833
+
834
+ :param item: dictionary containing all descriptions of the files to upload
835
+ """
836
+ files: list["FileToUploadWithCollectedAndDatasetInfoDict"] = []
837
+ datasets: list["DatasetDict"] = []
838
+ containers: list["DIDStringDict"] = []
839
+ attach: "Iterable[AttachDict]" = []
840
+ scope = item.get('did_scope')
841
+ if scope is None:
842
+ scope = self.default_file_scope
843
+ rse = item.get('rse')
844
+ path = item.get('path')
845
+ if path and isinstance(path, str):
846
+ if path[-1] == '/':
847
+ path = path[0:-1]
848
+ i = 0
849
+ path = os.path.abspath(path)
850
+ for root, dirs, fnames in os.walk(path):
851
+ if len(dirs) > 0 and len(fnames) > 0 and i == 0:
852
+ self.logger(logging.ERROR, 'A container can only have either collections or files, not both')
853
+ raise InputValidationError('Invalid input folder structure')
854
+ if len(fnames) > 0:
855
+ datasets.append({'scope': scope, 'name': root.split('/')[-1], 'rse': rse})
856
+ self.logger(logging.DEBUG, 'Appended dataset with DID %s:%s' % (scope, path))
857
+ for fname in fnames:
858
+ file = self._collect_file_info(os.path.join(root, fname), item)
859
+ file = cast("FileToUploadWithCollectedAndDatasetInfoDict", file)
860
+ file['dataset_scope'] = scope
861
+ file['dataset_name'] = root.split('/')[-1]
862
+ files.append(file)
863
+ self.logger(logging.DEBUG, 'Appended file with DID %s:%s' % (scope, fname))
864
+ elif len(dirs) > 0:
865
+ containers.append({'scope': scope, 'name': root.split('/')[-1]})
866
+ self.logger(logging.DEBUG, 'Appended container with DID %s:%s' % (scope, path))
867
+ attach.extend([{'scope': scope, 'name': root.split('/')[-1], 'rse': rse, 'did': {'scope': scope, 'name': dir_}} for dir_ in dirs])
868
+ elif len(dirs) == 0 and len(fnames) == 0:
869
+ self.logger(logging.WARNING, 'The folder %s is empty, skipping' % root)
870
+ continue
871
+ i += 1
872
+ # if everything went ok, replicate the folder structure in Rucio storage
873
+ for dataset in datasets:
874
+ try:
875
+ self.client.add_dataset(scope=dataset['scope'], name=dataset['name'], rse=dataset['rse'])
876
+ self.logger(logging.INFO, 'Created dataset with DID %s:%s' % (dataset['scope'], dataset['name']))
877
+ except RucioException as error:
878
+ self.logger(logging.ERROR, error)
879
+ self.logger(logging.ERROR, 'It was not possible to create dataset with DID %s:%s' % (dataset['scope'], dataset['name']))
880
+ for container in containers:
881
+ try:
882
+ self.client.add_container(scope=container['scope'], name=container['name'])
883
+ self.logger(logging.INFO, 'Created container with DID %s:%s' % (container['scope'], container['name']))
884
+ except RucioException as error:
885
+ self.logger(logging.ERROR, error)
886
+ self.logger(logging.ERROR, 'It was not possible to create dataset with DID %s:%s' % (container['scope'], container['name']))
887
+ for att in attach:
888
+ try:
889
+ self.client.attach_dids(scope=att['scope'], name=att['name'], dids=[att['did']])
890
+ self.logger(logging.INFO, 'DIDs attached to collection %s:%s' % (att['scope'], att['name']))
891
+ except RucioException as error:
892
+ self.logger(logging.ERROR, error)
893
+ self.logger(logging.ERROR, 'It was not possible to attach to collection with DID %s:%s' % (att['scope'], att['name']))
894
+ return files
895
+
896
+ def preferred_impl(
897
+ self,
898
+ rse_settings: "RSESettingsDict",
899
+ domain: str
900
+ ) -> Optional[str]:
901
+ """
902
+ Finds the optimum protocol impl preferred by the client and
903
+ supported by the remote RSE.
904
+
905
+ :param rse_settings: dictionary containing the RSE settings
906
+ :param domain: The network domain, either 'wan' (default) or 'lan'
907
+
908
+ :raises RucioException(msg): general exception with msg for more details.
909
+ """
910
+ preferred_protocols = []
911
+ supported_impl = None
912
+
913
+ try:
914
+ preferred_impls = config_get('upload', 'preferred_impl')
915
+ except Exception as error:
916
+ self.logger(logging.INFO, 'No preferred protocol impl in rucio.cfg: %s' % (error))
917
+ pass
918
+ else:
919
+ preferred_impls = list(preferred_impls.split(', '))
920
+ i = 0
921
+ while i < len(preferred_impls):
922
+ impl = preferred_impls[i]
923
+ impl_split = impl.split('.')
924
+ if len(impl_split) == 1:
925
+ preferred_impls[i] = 'rucio.rse.protocols.' + impl + '.Default'
926
+ else:
927
+ preferred_impls[i] = 'rucio.rse.protocols.' + impl
928
+ i += 1
929
+
930
+ preferred_protocols = [protocol for protocol in reversed(rse_settings['protocols']) if protocol['impl'] in preferred_impls]
931
+
932
+ if len(preferred_protocols) > 0:
933
+ preferred_protocols += [protocol for protocol in reversed(rse_settings['protocols']) if protocol not in preferred_protocols]
934
+ else:
935
+ preferred_protocols = reversed(rse_settings['protocols'])
936
+
937
+ for protocol in preferred_protocols:
938
+ if domain not in list(protocol['domains'].keys()):
939
+ self.logger(logging.DEBUG, 'Unsuitable protocol "%s": Domain %s not supported' % (protocol['impl'], domain))
940
+ continue
941
+ if not all(operations in protocol['domains'][domain] for operations in ("read", "write", "delete")):
942
+ self.logger(logging.DEBUG, 'Unsuitable protocol "%s": All operations are not supported' % (protocol['impl']))
943
+ continue
944
+ try:
945
+ supported_protocol = rsemgr.create_protocol(rse_settings, 'write', domain=domain, impl=protocol['impl'], auth_token=self.auth_token, logger=self.logger)
946
+ supported_protocol.connect()
947
+ except Exception as error:
948
+ self.logger(logging.DEBUG, 'Failed to create protocol "%s", exception: %s' % (protocol['impl'], error))
949
+ pass
950
+ else:
951
+ self.logger(logging.INFO, 'Preferred protocol impl supported locally and remotely: %s' % (protocol['impl']))
952
+ supported_impl = protocol['impl']
953
+ break
954
+
955
+ return supported_impl