You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.9 KiB

4 years ago
  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 inspect
  14. import sys
  15. import os
  16. import errno
  17. import socket
  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. if six.PY3:
  34. def accepts_kwargs(func):
  35. # In python3.4.1, there's backwards incompatible
  36. # changes when using getargspec with functools.partials.
  37. return inspect.getfullargspec(func)[2]
  38. # In python3, socket.error is OSError, which is too general
  39. # for what we want (i.e FileNotFoundError is a subclass of OSError).
  40. # In py3 all the socket related errors are in a newly created
  41. # ConnectionError
  42. SOCKET_ERROR = ConnectionError
  43. MAXINT = None
  44. else:
  45. def accepts_kwargs(func):
  46. return inspect.getargspec(func)[2]
  47. SOCKET_ERROR = socket.error
  48. MAXINT = sys.maxint
  49. def seekable(fileobj):
  50. """Backwards compat function to determine if a fileobj is seekable
  51. :param fileobj: The file-like object to determine if seekable
  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, IOError):
  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. def readable(fileobj):
  70. """Determines whether or not a file-like object is readable.
  71. :param fileobj: The file-like object to determine if readable
  72. :returns: True, if readable. False otherwise.
  73. """
  74. if hasattr(fileobj, 'readable'):
  75. return fileobj.readable()
  76. return hasattr(fileobj, 'read')