compat.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. from botocore.compat import six
  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. def accepts_kwargs(func):
  34. return inspect.getfullargspec(func)[2]
  35. # In python 3, socket.error is OSError, which is too general
  36. # for what we want (i.e FileNotFoundError is a subclass of OSError).
  37. # In python 3, all the socket related errors are in a newly created
  38. # ConnectionError.
  39. SOCKET_ERROR = ConnectionError
  40. MAXINT = None
  41. def seekable(fileobj):
  42. """Backwards compat function to determine if a fileobj is seekable
  43. :param fileobj: The file-like object to determine if seekable
  44. :returns: True, if seekable. False, otherwise.
  45. """
  46. # If the fileobj has a seekable attr, try calling the seekable()
  47. # method on it.
  48. if hasattr(fileobj, 'seekable'):
  49. return fileobj.seekable()
  50. # If there is no seekable attr, check if the object can be seeked
  51. # or telled. If it can, try to seek to the current position.
  52. elif hasattr(fileobj, 'seek') and hasattr(fileobj, 'tell'):
  53. try:
  54. fileobj.seek(0, 1)
  55. return True
  56. except OSError:
  57. # If an io related error was thrown then it is not seekable.
  58. return False
  59. # Else, the fileobj is not seekable
  60. return False
  61. def readable(fileobj):
  62. """Determines whether or not a file-like object is readable.
  63. :param fileobj: The file-like object to determine if readable
  64. :returns: True, if readable. False otherwise.
  65. """
  66. if hasattr(fileobj, 'readable'):
  67. return fileobj.readable()
  68. return hasattr(fileobj, 'read')
  69. def fallocate(fileobj, size):
  70. if hasattr(os, 'posix_fallocate'):
  71. os.posix_fallocate(fileobj.fileno(), 0, size)
  72. else:
  73. fileobj.truncate(size)
  74. # Import at end of file to avoid circular dependencies
  75. from multiprocessing.managers import BaseManager # noqa: F401,E402