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,550 @@
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 os
16
+ import sys
17
+ import xml.etree.ElementTree as ET
18
+ from dataclasses import dataclass
19
+ from typing import Any, Optional
20
+ from urllib.parse import urlparse
21
+
22
+ import requests
23
+ from requests.adapters import HTTPAdapter
24
+ from urllib3.poolmanager import PoolManager
25
+
26
+ from rucio.common import exception
27
+ from rucio.rse.protocols import protocol
28
+
29
+
30
+ class TLSHTTPAdapter(HTTPAdapter):
31
+ '''
32
+ Class to force the SSL protocol to latest TLS
33
+ '''
34
+ def init_poolmanager(self, connections, maxsize, block=False):
35
+ self.poolmanager = PoolManager(num_pools=connections,
36
+ maxsize=maxsize,
37
+ block=block,
38
+ cert_reqs="CERT_REQUIRED",
39
+ ca_cert_dir="/etc/grid-security/certificates")
40
+
41
+
42
+ class UploadInChunks:
43
+ '''
44
+ Class to upload by chunks.
45
+ '''
46
+
47
+ def __init__(self, filename, chunksize, progressbar=False):
48
+ self.__totalsize = os.path.getsize(filename)
49
+ self.__readsofar = 0
50
+ self.__filename = filename
51
+ self.__chunksize = chunksize
52
+ self.__progressbar = progressbar
53
+
54
+ def __iter__(self):
55
+ try:
56
+ with open(self.__filename, 'rb') as file_in:
57
+ while True:
58
+ data = file_in.read(self.__chunksize)
59
+ if not data:
60
+ if self.__progressbar:
61
+ sys.stdout.write("\n")
62
+ break
63
+ self.__readsofar += len(data)
64
+ if self.__progressbar:
65
+ percent = self.__readsofar * 100 / self.__totalsize
66
+ sys.stdout.write("\r{percent:3.0f}%".format(percent=percent))
67
+ yield data
68
+ except OSError as error:
69
+ raise exception.SourceNotFound(error)
70
+
71
+ def __len__(self):
72
+ return self.__totalsize
73
+
74
+
75
+ class IterableToFileAdapter:
76
+ '''
77
+ Class IterableToFileAdapter
78
+ '''
79
+ def __init__(self, iterable):
80
+ self.iterator = iter(iterable)
81
+ self.length = len(iterable)
82
+
83
+ def read(self, size=-1): # TBD: add buffer for `len(data) > size` case
84
+ nextvar = next(self.iterator, b'')
85
+ return nextvar
86
+
87
+ def __len__(self):
88
+ return self.length
89
+
90
+
91
+ @dataclass(frozen=True)
92
+ class _PropfindFile:
93
+ """Contains the properties of one file from a PROPFIND response."""
94
+
95
+ href: str
96
+ size: Optional[int]
97
+
98
+ @classmethod
99
+ def from_xml_node(cls, node: ET.Element):
100
+ """Extract file properties from a `<{DAV:}response>` node."""
101
+
102
+ xml_href = node.find('./{DAV:}href')
103
+ if xml_href is None or xml_href.text is None:
104
+ raise ValueError('Response is missing mandatory field "href".')
105
+ else:
106
+ href = xml_href.text
107
+
108
+ xml_size = node.find('./{DAV:}propstat/{DAV:}prop/{DAV:}getcontentlength')
109
+ if xml_size is None or xml_size.text is None:
110
+ size = None
111
+ else:
112
+ size = int(xml_size.text)
113
+
114
+ return cls(href=href, size=size) # type: ignore
115
+
116
+
117
+ @dataclass(frozen=True)
118
+ class _PropfindResponse:
119
+ """Contains all the files from a PROPFIND response."""
120
+
121
+ files: tuple[_PropfindFile]
122
+
123
+ @classmethod
124
+ def parse(cls, document: str):
125
+ """Parses the XML document of a WebDAV PROPFIND response.
126
+
127
+ The PROPFIND response is described in RFC 4918.
128
+ This method expects the document root to be a node with tag `{DAV:}multistatus`.
129
+
130
+ :param document: XML document to parse.
131
+ :raises ValueError: if the XML document couldn't be parsed.
132
+ :returns: The parsed response.
133
+ """
134
+
135
+ try:
136
+ xml = ET.fromstring(document) # noqa: S314
137
+ except ET.ParseError as ex:
138
+ raise ValueError("Couldn't parse XML document") from ex
139
+
140
+ if xml.tag != '{DAV:}multistatus':
141
+ raise ValueError('Root element is not "{DAV:}multistatus".')
142
+
143
+ files = []
144
+ for xml_response in xml.findall('./{DAV:}response'):
145
+ files.append(_PropfindFile.from_xml_node(xml_response))
146
+
147
+ return cls(files=tuple(files)) # type: ignore
148
+
149
+
150
+ class Default(protocol.RSEProtocol):
151
+
152
+ """ Implementing access to RSEs using the webDAV protocol."""
153
+
154
+ def connect(self, credentials: Optional[dict[str, Any]] = None) -> None:
155
+ """ Establishes the actual connection to the referred RSE.
156
+
157
+ :param credentials: Provides information to establish a connection
158
+ to the referred storage system. For WebDAV connections these are
159
+ ca_cert, cert, auth_type, timeout
160
+
161
+ :raises RSEAccessDenied
162
+ """
163
+ credentials = credentials or {}
164
+ try:
165
+ parse_url = urlparse(self.path2pfn(''))
166
+ self.server = f'{parse_url.scheme}://{parse_url.netloc}'
167
+ except KeyError:
168
+ raise exception.RSEAccessDenied('No specified Server')
169
+
170
+ try:
171
+ self.ca_cert = credentials['ca_cert']
172
+ except KeyError:
173
+ self.ca_cert = None
174
+
175
+ try:
176
+ self.auth_type = credentials['auth_type']
177
+ except KeyError:
178
+ self.auth_type = 'cert'
179
+
180
+ try:
181
+ self.cert = credentials['cert']
182
+ except KeyError:
183
+ x509 = os.getenv('X509_USER_PROXY')
184
+ if not x509:
185
+ # Trying to get the proxy from the default location
186
+ proxy_path = '/tmp/x509up_u%s' % os.geteuid()
187
+ if os.path.isfile(proxy_path):
188
+ self.cert = (proxy_path, proxy_path)
189
+ elif self.auth_token:
190
+ # If no proxy is found, we set the cert to None and use the auth_token
191
+ self.cert = None
192
+ pass
193
+ else:
194
+ raise exception.RSEAccessDenied('X509_USER_PROXY is not set')
195
+ else:
196
+ self.cert = (x509, x509)
197
+
198
+ try:
199
+ self.timeout = credentials['timeout']
200
+ except KeyError:
201
+ self.timeout = 300
202
+ self.session = requests.Session()
203
+ self.session.mount('https://', TLSHTTPAdapter())
204
+ if self.auth_token:
205
+ self.session.headers.update({'Authorization': 'Bearer ' + self.auth_token})
206
+ # "ping" to see if the server is available
207
+ try:
208
+ res = self.session.request('HEAD', self.path2pfn(''), verify=False, timeout=self.timeout, cert=self.cert)
209
+ if res.status_code != 200:
210
+ raise exception.ServiceUnavailable('Problem to connect %s : %s' % (self.path2pfn(''), res.text))
211
+ except requests.exceptions.ConnectionError as error:
212
+ raise exception.ServiceUnavailable('Problem to connect %s : %s' % (self.path2pfn(''), error))
213
+ except requests.exceptions.ReadTimeout as error:
214
+ raise exception.ServiceUnavailable(error)
215
+
216
+ def close(self):
217
+ self.session.close()
218
+
219
+ def path2pfn(self, path):
220
+ """
221
+ Returns a fully qualified PFN for the file referred by path.
222
+
223
+ :param path: The path to the file.
224
+
225
+ :returns: Fully qualified PFN.
226
+
227
+ """
228
+ if not path.startswith('https'):
229
+ return '%s://%s:%s%s%s' % (self.attributes['scheme'], self.attributes['hostname'], str(self.attributes['port']), self.attributes['prefix'], path)
230
+ else:
231
+ return path
232
+
233
+ def exists(self, pfn):
234
+ """ Checks if the requested file is known by the referred RSE.
235
+
236
+ :param pfn: Physical file name
237
+
238
+ :returns: True if the file exists, False if it doesn't
239
+
240
+ :raise ServiceUnavailable, RSEAccessDenied
241
+ """
242
+ path = self.path2pfn(pfn)
243
+ try:
244
+ result = self.session.request('HEAD', path, verify=False, timeout=self.timeout, cert=self.cert)
245
+ if result.status_code == 200:
246
+ return True
247
+ elif result.status_code in [401, ]:
248
+ raise exception.RSEAccessDenied()
249
+ elif result.status_code in [404, ]:
250
+ return False
251
+ else:
252
+ # catchall exception
253
+ raise exception.RucioException(result.status_code, result.text)
254
+ except requests.exceptions.ConnectionError as error:
255
+ raise exception.ServiceUnavailable(error)
256
+
257
+ def get(self, pfn, dest='.', transfer_timeout=None):
258
+ """ Provides access to files stored inside connected the RSE.
259
+
260
+ :param pfn: Physical file name of requested file
261
+ :param dest: Name and path of the files when stored at the client
262
+ :param transfer_timeout: Transfer timeout (in seconds) - dummy
263
+
264
+ :raises DestinationNotAccessible, ServiceUnavailable, SourceNotFound, RSEAccessDenied
265
+ """
266
+ path = self.path2pfn(pfn)
267
+ chunksize = 1024
268
+ try:
269
+ result = self.session.get(path, verify=False, stream=True, timeout=self.timeout, cert=self.cert)
270
+ if result and result.status_code in [200, ]:
271
+ length = None
272
+ if 'content-length' in result.headers:
273
+ length = int(result.headers['content-length'])
274
+ with open(dest, 'wb') as file_out:
275
+ nchunk = 0
276
+ if not length:
277
+ print('Malformed HTTP response (missing content-length header).')
278
+ for chunk in result.iter_content(chunksize):
279
+ file_out.write(chunk)
280
+ if length:
281
+ nchunk += 1
282
+ elif result.status_code in [404, ]:
283
+ raise exception.SourceNotFound()
284
+ elif result.status_code in [401, 403]:
285
+ raise exception.RSEAccessDenied()
286
+ else:
287
+ # catchall exception
288
+ raise exception.RucioException(result.status_code, result.text)
289
+ except requests.exceptions.ConnectionError as error:
290
+ raise exception.ServiceUnavailable(error)
291
+ except requests.exceptions.ReadTimeout as error:
292
+ raise exception.ServiceUnavailable(error)
293
+
294
+ def put(self, source, target, source_dir=None, transfer_timeout=None, progressbar=False):
295
+ """ Allows to store files inside the referred RSE.
296
+
297
+ :param source: Physical file name
298
+ :param target: Name of the file on the storage system e.g. with prefixed scope
299
+ :param source_dir Path where the to be transferred files are stored in the local file system
300
+ :param transfer_timeout Transfer timeout (in seconds) - dummy
301
+
302
+ :raises DestinationNotAccessible, ServiceUnavailable, SourceNotFound, RSEAccessDenied
303
+ """
304
+ path = self.path2pfn(target)
305
+ full_name = source_dir + '/' + source if source_dir else source
306
+ directories = path.split('/')
307
+ # Try the upload without testing the existence of the destination directory
308
+ try:
309
+ if not os.path.exists(full_name):
310
+ raise exception.SourceNotFound()
311
+ it = UploadInChunks(full_name, 10000000, progressbar)
312
+ result = self.session.put(path, data=IterableToFileAdapter(it), verify=False, allow_redirects=True, timeout=self.timeout, cert=self.cert)
313
+ if result.status_code in [200, 201]:
314
+ return
315
+ if result.status_code in [409, ]:
316
+ raise exception.FileReplicaAlreadyExists()
317
+ else:
318
+ # Create the directories before issuing the PUT
319
+ for directory_level in reversed(list(range(1, 4))):
320
+ upper_directory = "/".join(directories[:-directory_level])
321
+ self.mkdir(upper_directory)
322
+ try:
323
+ if not os.path.exists(full_name):
324
+ raise exception.SourceNotFound()
325
+ it = UploadInChunks(full_name, 10000000, progressbar)
326
+ result = self.session.put(path, data=IterableToFileAdapter(it), verify=False, allow_redirects=True, timeout=self.timeout, cert=self.cert)
327
+ if result.status_code in [200, 201]:
328
+ return
329
+ if result.status_code in [409, ]:
330
+ raise exception.FileReplicaAlreadyExists()
331
+ elif result.status_code in [401, ]:
332
+ raise exception.RSEAccessDenied()
333
+ else:
334
+ # catchall exception
335
+ raise exception.RucioException(result.status_code, result.text)
336
+ except requests.exceptions.ConnectionError as error:
337
+ raise exception.ServiceUnavailable(error)
338
+ except OSError as error:
339
+ raise exception.SourceNotFound(error)
340
+ except requests.exceptions.ConnectionError as error:
341
+ raise exception.ServiceUnavailable(error)
342
+ except requests.exceptions.ReadTimeout as error:
343
+ raise exception.ServiceUnavailable(error)
344
+ except OSError as error:
345
+ raise exception.SourceNotFound(error)
346
+
347
+ def rename(self, pfn, new_pfn):
348
+ """ Allows to rename a file stored inside the connected RSE.
349
+
350
+ :param pfn: Current physical file name
351
+ :param new_pfn New physical file name
352
+
353
+ :raises DestinationNotAccessible, ServiceUnavailable, SourceNotFound, RSEAccessDenied
354
+ """
355
+ path = self.path2pfn(pfn)
356
+ new_path = self.path2pfn(new_pfn)
357
+ directories = new_path.split('/')
358
+
359
+ headers = {'Destination': new_path}
360
+ # Try the rename without testing the existence of the destination directory
361
+ try:
362
+ result = self.session.request('MOVE', path, verify=False, headers=headers, timeout=self.timeout, cert=self.cert)
363
+ if result.status_code == 201:
364
+ return
365
+ elif result.status_code in [404, ]:
366
+ raise exception.SourceNotFound()
367
+ else:
368
+ # Create the directories before issuing the MOVE
369
+ for directory_level in reversed(list(range(1, 4))):
370
+ upper_directory = "/".join(directories[:-directory_level])
371
+ self.mkdir(upper_directory)
372
+ try:
373
+ result = self.session.request('MOVE', path, verify=False, headers=headers, timeout=self.timeout, cert=self.cert)
374
+ if result.status_code == 201:
375
+ return
376
+ elif result.status_code in [404, ]:
377
+ raise exception.SourceNotFound()
378
+ elif result.status_code in [401, ]:
379
+ raise exception.RSEAccessDenied()
380
+ else:
381
+ # catchall exception
382
+ raise exception.RucioException(result.status_code, result.text)
383
+ except requests.exceptions.ConnectionError as error:
384
+ raise exception.ServiceUnavailable(error)
385
+ except requests.exceptions.ConnectionError as error:
386
+ raise exception.ServiceUnavailable(error)
387
+ except requests.exceptions.ReadTimeout as error:
388
+ raise exception.ServiceUnavailable(error)
389
+
390
+ def delete(self, pfn):
391
+ """ Deletes a file from the connected RSE.
392
+
393
+ :param pfn: Physical file name
394
+
395
+ :raises ServiceUnavailable, SourceNotFound, RSEAccessDenied, ResourceTemporaryUnavailable
396
+ """
397
+ path = self.path2pfn(pfn)
398
+ try:
399
+ result = self.session.delete(path, verify=False, timeout=self.timeout, cert=self.cert)
400
+ if result.status_code in [204, ]:
401
+ return
402
+ elif result.status_code in [404, ]:
403
+ raise exception.SourceNotFound()
404
+ elif result.status_code in [401, 403]:
405
+ raise exception.RSEAccessDenied()
406
+ elif result.status_code in [500, 503]:
407
+ raise exception.ResourceTemporaryUnavailable()
408
+ else:
409
+ # catchall exception
410
+ raise exception.RucioException(result.status_code, result.text)
411
+ except requests.exceptions.ConnectionError as error:
412
+ raise exception.ServiceUnavailable(error)
413
+ except requests.exceptions.ReadTimeout as error:
414
+ raise exception.ServiceUnavailable(error)
415
+
416
+ def mkdir(self, directory):
417
+ """ Internal method to create directories
418
+
419
+ :param directory: Name of the directory that needs to be created
420
+
421
+ :raises DestinationNotAccessible, ServiceUnavailable, SourceNotFound, RSEAccessDenied
422
+ """
423
+ path = self.path2pfn(directory)
424
+ try:
425
+ result = self.session.request('MKCOL', path, verify=False, timeout=self.timeout, cert=self.cert)
426
+ if result.status_code in [201, 405]: # Success or directory already exists
427
+ return
428
+ elif result.status_code in [404, ]:
429
+ raise exception.SourceNotFound()
430
+ elif result.status_code in [401, ]:
431
+ raise exception.RSEAccessDenied()
432
+ else:
433
+ # catchall exception
434
+ raise exception.RucioException(result.status_code, result.text)
435
+ except requests.exceptions.ConnectionError as error:
436
+ raise exception.ServiceUnavailable(error)
437
+ except requests.exceptions.ReadTimeout as error:
438
+ raise exception.ServiceUnavailable(error)
439
+
440
+ def ls(self, filename):
441
+ """ Internal method to list files/directories
442
+
443
+ :param filename: Name of the directory that needs to be created
444
+
445
+ :raises DestinationNotAccessible, ServiceUnavailable, SourceNotFound, RSEAccessDenied
446
+ """
447
+ path = self.path2pfn(filename)
448
+ headers = {'Depth': '1'}
449
+ self.exists(filename)
450
+ try:
451
+ result = self.session.request('PROPFIND', path, verify=False, headers=headers, timeout=self.timeout, cert=self.cert)
452
+ if result.status_code in [404, ]:
453
+ raise exception.SourceNotFound()
454
+ elif result.status_code in [401, ]:
455
+ raise exception.RSEAccessDenied()
456
+
457
+ try:
458
+ propfind = _PropfindResponse.parse(result.text)
459
+ except ValueError:
460
+ raise exception.ServiceUnavailable("Couldn't parse WebDAV response.")
461
+
462
+ list_files = [self.server + file.href for file in propfind.files if file.href is not None]
463
+
464
+ try:
465
+ list_files.remove(filename + '/')
466
+ except ValueError:
467
+ pass
468
+ try:
469
+ list_files.remove(filename)
470
+ except ValueError:
471
+ pass
472
+
473
+ return list_files
474
+ except requests.exceptions.ConnectionError as error:
475
+ raise exception.ServiceUnavailable(error)
476
+ except requests.exceptions.ReadTimeout as error:
477
+ raise exception.ServiceUnavailable(error)
478
+
479
+ def stat(self, path):
480
+ """
481
+ Returns the stats of a file.
482
+
483
+ :param path: path to file
484
+
485
+ :raises ServiceUnavailable: if some generic error occurred in the library.
486
+ :raises SourceNotFound: if the source file was not found on the referred storage.
487
+ :raises RSEAccessDenied: in case of permission issue.
488
+
489
+ :returns: a dict with filesize of the file provided in path as a key.
490
+ """
491
+ headers = {'Depth': '1'}
492
+ dict_ = {}
493
+ try:
494
+ result = self.session.request('PROPFIND', path, verify=False, headers=headers, timeout=self.timeout, cert=self.cert)
495
+ if result.status_code in [404, ]:
496
+ raise exception.SourceNotFound()
497
+ elif result.status_code in [401, ]:
498
+ raise exception.RSEAccessDenied()
499
+ if result.status_code in [400, ]:
500
+ raise exception.InvalidRequest()
501
+ except requests.exceptions.ConnectionError as error:
502
+ raise exception.ServiceUnavailable(error)
503
+ except requests.exceptions.ReadTimeout as error:
504
+ raise exception.ServiceUnavailable(error)
505
+
506
+ path_parts = self.parse_pfns(path)[path]
507
+ local_path = os.path.join(path_parts['prefix'], path_parts['path'][1:], path_parts['name'])
508
+
509
+ try:
510
+ propfind = _PropfindResponse.parse(result.text)
511
+ except ValueError:
512
+ raise exception.ServiceUnavailable("Couldn't parse WebDAV response.")
513
+
514
+ for file in propfind.files:
515
+ if file.href != str(local_path):
516
+ continue
517
+
518
+ if file.size is None:
519
+ continue
520
+
521
+ dict_['filesize'] = file.size
522
+ break
523
+ else:
524
+ raise exception.ServiceUnavailable("WebDAV response didn't include content length for requested path.")
525
+
526
+ return dict_
527
+
528
+ def get_space_usage(self):
529
+ """
530
+ Get RSE space usage information.
531
+
532
+ :returns: a list with dict containing 'totalsize' and 'unusedsize'
533
+
534
+ :raises ServiceUnavailable: if some generic error occurred in the library.
535
+ """
536
+ endpoint_basepath = self.path2pfn('')
537
+ headers = {'Depth': '0'}
538
+
539
+ try:
540
+ root = ET.fromstring(self.session.request('PROPFIND', endpoint_basepath, verify=False, headers=headers, cert=self.session.cert).text) # noqa: S314
541
+ usedsize = root[0][1][0].find('{DAV:}quota-used-bytes').text
542
+ try:
543
+ unusedsize = root[0][1][0].find('{DAV:}quota-available-bytes').text
544
+ except Exception:
545
+ print('No free space given, return -999')
546
+ unusedsize = -999
547
+ totalsize = int(usedsize) + int(unusedsize)
548
+ return totalsize, unusedsize
549
+ except Exception as error:
550
+ raise exception.ServiceUnavailable(error)