Debian Codegraph @

s3transfer/compat.py

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 # Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License"). You
4 # may not use this file except in compliance with the License. A copy of
5 # the License is located at
6 #
7 # http://aws.amazon.com/apache2.0/
8 #
9 # or in the "license" file accompanying this file. This file is
10 # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11 # ANY KIND, either express or implied. See the License for the specific
12 # language governing permissions and limitations under the License.
13 import errno
14 import inspect
15 import os
16 import socket
17 import sys
18
19 from botocore.compat import six
20
21 if sys.platform.startswith('win'):
22 def rename_file(current_filename, new_filename):
23 try:
24 os.remove(new_filename)
25 except OSError as e:
26 if not e.errno == errno.ENOENT:
27 # We only want to a ignore trying to remove
28 # a file that does not exist. If it fails
29 # for any other reason we should be propagating
30 # that exception.
31 raise
32 os.rename(current_filename, new_filename)
33 else:
34 rename_file = os.rename
35
36
37 def accepts_kwargs(func):
38 return inspect.getfullargspec(func)[2]
39
40
41 # In python 3, socket.error is OSError, which is too general
42 # for what we want (i.e FileNotFoundError is a subclass of OSError).
43 # In python 3, all the socket related errors are in a newly created
44 # ConnectionError.
45 SOCKET_ERROR = ConnectionError
46 MAXINT = None
47
48
49 def seekable(fileobj):
50 """Backwards compat function to determine if a fileobj is seekable
51
52 :param fileobj: The file-like object to determine if seekable
53
54 :returns: True, if seekable. False, otherwise.
55 """
56 # If the fileobj has a seekable attr, try calling the seekable()
57 # method on it.
58 if hasattr(fileobj, 'seekable'):
59 return fileobj.seekable()
60 # If there is no seekable attr, check if the object can be seeked
61 # or telled. If it can, try to seek to the current position.
62 elif hasattr(fileobj, 'seek') and hasattr(fileobj, 'tell'):
63 try:
64 fileobj.seek(0, 1)
65 return True
66 except OSError:
67 # If an io related error was thrown then it is not seekable.
68 return False
69 # Else, the fileobj is not seekable
70 return False
71
72
73 def readable(fileobj):
74 """Determines whether or not a file-like object is readable.
75
76 :param fileobj: The file-like object to determine if readable
77
78 :returns: True, if readable. False otherwise.
79 """
80 if hasattr(fileobj, 'readable'):
81 return fileobj.readable()
82
83 return hasattr(fileobj, 'read')
84
85
86 def fallocate(fileobj, size):
87 if hasattr(os, 'posix_fallocate'):
88 os.posix_fallocate(fileobj.fileno(), 0, size)
89 else:
90 fileobj.truncate(size)
91
92
93 # Import at end of file to avoid circular dependencies
94 from multiprocessing.managers import BaseManager # noqa: F401,E402