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