Debian Codegraph @

s3transfer/compat.py

raw ◀ ▶
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 if sys.platform.startswith('win'):
20 def rename_file(current_filename, new_filename):
21 try:
22 os.remove(new_filename)
23 except OSError as e:
24 if not e.errno == errno.ENOENT:
25 # We only want to a ignore trying to remove
26 # a file that does not exist. If it fails
27 # for any other reason we should be propagating
28 # that exception.
29 raise
30 os.rename(current_filename, new_filename)
31 else:
32 rename_file = os.rename
33
34
35 def accepts_kwargs(func):
36 return inspect.getfullargspec(func)[2]
37
38
39 # In python 3, socket.error is OSError, which is too general
40 # for what we want (i.e FileNotFoundError is a subclass of OSError).
41 # In python 3, all the socket related errors are in a newly created
42 # ConnectionError.
43 SOCKET_ERROR = ConnectionError
44 MAXINT = None
45
46
47 def seekable(fileobj):
48 """Backwards compat function to determine if a fileobj is seekable
49
50 :param fileobj: The file-like object to determine if seekable
51
52 :returns: True, if seekable. False, otherwise.
53 """
54 # If the fileobj has a seekable attr, try calling the seekable()
55 # method on it.
56 if hasattr(fileobj, 'seekable'):
57 return fileobj.seekable()
58 # If there is no seekable attr, check if the object can be seeked
59 # or telled. If it can, try to seek to the current position.
60 elif hasattr(fileobj, 'seek') and hasattr(fileobj, 'tell'):
61 try:
62 fileobj.seek(0, 1)
63 return True
64 except OSError:
65 # If an io related error was thrown then it is not seekable.
66 return False
67 # Else, the fileobj is not seekable
68 return False
69
70
71 def readable(fileobj):
72 """Determines whether or not a file-like object is readable.
73
74 :param fileobj: The file-like object to determine if readable
75
76 :returns: True, if readable. False otherwise.
77 """
78 if hasattr(fileobj, 'readable'):
79 return fileobj.readable()
80
81 return hasattr(fileobj, 'read')
82
83
84 def fallocate(fileobj, size):
85 if hasattr(os, 'posix_fallocate'):
86 os.posix_fallocate(fileobj.fileno(), 0, size)
87 else:
88 fileobj.truncate(size)
89
90
91 # Import at end of file to avoid circular dependencies
92 from multiprocessing.managers import BaseManager # noqa: F401,E402