compat.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. """Stuff that differs in different Python versions and platform
  2. distributions."""
  3. from __future__ import absolute_import, division
  4. import codecs
  5. import locale
  6. import logging
  7. import os
  8. import shutil
  9. import sys
  10. from pip._vendor.six import text_type
  11. try:
  12. import ipaddress
  13. except ImportError:
  14. try:
  15. from pip._vendor import ipaddress # type: ignore
  16. except ImportError:
  17. import ipaddr as ipaddress # type: ignore
  18. ipaddress.ip_address = ipaddress.IPAddress
  19. ipaddress.ip_network = ipaddress.IPNetwork
  20. __all__ = [
  21. "ipaddress", "uses_pycache", "console_to_str", "native_str",
  22. "get_path_uid", "stdlib_pkgs", "WINDOWS", "samefile", "get_terminal_size",
  23. ]
  24. logger = logging.getLogger(__name__)
  25. if sys.version_info >= (3, 4):
  26. uses_pycache = True
  27. from importlib.util import cache_from_source
  28. else:
  29. import imp
  30. try:
  31. cache_from_source = imp.cache_from_source # type: ignore
  32. except AttributeError:
  33. # does not use __pycache__
  34. cache_from_source = None
  35. uses_pycache = cache_from_source is not None
  36. if sys.version_info >= (3, 5):
  37. backslashreplace_decode = "backslashreplace"
  38. else:
  39. # In version 3.4 and older, backslashreplace exists
  40. # but does not support use for decoding.
  41. # We implement our own replace handler for this
  42. # situation, so that we can consistently use
  43. # backslash replacement for all versions.
  44. def backslashreplace_decode_fn(err):
  45. raw_bytes = (err.object[i] for i in range(err.start, err.end))
  46. if sys.version_info[0] == 2:
  47. # Python 2 gave us characters - convert to numeric bytes
  48. raw_bytes = (ord(b) for b in raw_bytes)
  49. return u"".join(u"\\x%x" % c for c in raw_bytes), err.end
  50. codecs.register_error(
  51. "backslashreplace_decode",
  52. backslashreplace_decode_fn,
  53. )
  54. backslashreplace_decode = "backslashreplace_decode"
  55. def console_to_str(data):
  56. """Return a string, safe for output, of subprocess output.
  57. We assume the data is in the locale preferred encoding.
  58. If it won't decode properly, we warn the user but decode as
  59. best we can.
  60. We also ensure that the output can be safely written to
  61. standard output without encoding errors.
  62. """
  63. # First, get the encoding we assume. This is the preferred
  64. # encoding for the locale, unless that is not found, or
  65. # it is ASCII, in which case assume UTF-8
  66. encoding = locale.getpreferredencoding()
  67. if (not encoding) or codecs.lookup(encoding).name == "ascii":
  68. encoding = "utf-8"
  69. # Now try to decode the data - if we fail, warn the user and
  70. # decode with replacement.
  71. try:
  72. s = data.decode(encoding)
  73. except UnicodeDecodeError:
  74. logger.warning(
  75. "Subprocess output does not appear to be encoded as %s",
  76. encoding,
  77. )
  78. s = data.decode(encoding, errors=backslashreplace_decode)
  79. # Make sure we can print the output, by encoding it to the output
  80. # encoding with replacement of unencodable characters, and then
  81. # decoding again.
  82. # We use stderr's encoding because it's less likely to be
  83. # redirected and if we don't find an encoding we skip this
  84. # step (on the assumption that output is wrapped by something
  85. # that won't fail).
  86. # The double getattr is to deal with the possibility that we're
  87. # being called in a situation where sys.__stderr__ doesn't exist,
  88. # or doesn't have an encoding attribute. Neither of these cases
  89. # should occur in normal pip use, but there's no harm in checking
  90. # in case people use pip in (unsupported) unusual situations.
  91. output_encoding = getattr(getattr(sys, "__stderr__", None),
  92. "encoding", None)
  93. if output_encoding:
  94. s = s.encode(output_encoding, errors="backslashreplace")
  95. s = s.decode(output_encoding)
  96. return s
  97. if sys.version_info >= (3,):
  98. def native_str(s, replace=False):
  99. if isinstance(s, bytes):
  100. return s.decode('utf-8', 'replace' if replace else 'strict')
  101. return s
  102. else:
  103. def native_str(s, replace=False):
  104. # Replace is ignored -- unicode to UTF-8 can't fail
  105. if isinstance(s, text_type):
  106. return s.encode('utf-8')
  107. return s
  108. def get_path_uid(path):
  109. """
  110. Return path's uid.
  111. Does not follow symlinks:
  112. https://github.com/pypa/pip/pull/935#discussion_r5307003
  113. Placed this function in compat due to differences on AIX and
  114. Jython, that should eventually go away.
  115. :raises OSError: When path is a symlink or can't be read.
  116. """
  117. if hasattr(os, 'O_NOFOLLOW'):
  118. fd = os.open(path, os.O_RDONLY | os.O_NOFOLLOW)
  119. file_uid = os.fstat(fd).st_uid
  120. os.close(fd)
  121. else: # AIX and Jython
  122. # WARNING: time of check vulnerability, but best we can do w/o NOFOLLOW
  123. if not os.path.islink(path):
  124. # older versions of Jython don't have `os.fstat`
  125. file_uid = os.stat(path).st_uid
  126. else:
  127. # raise OSError for parity with os.O_NOFOLLOW above
  128. raise OSError(
  129. "%s is a symlink; Will not return uid for symlinks" % path
  130. )
  131. return file_uid
  132. def expanduser(path):
  133. """
  134. Expand ~ and ~user constructions.
  135. Includes a workaround for http://bugs.python.org/issue14768
  136. """
  137. expanded = os.path.expanduser(path)
  138. if path.startswith('~/') and expanded.startswith('//'):
  139. expanded = expanded[1:]
  140. return expanded
  141. # packages in the stdlib that may have installation metadata, but should not be
  142. # considered 'installed'. this theoretically could be determined based on
  143. # dist.location (py27:`sysconfig.get_paths()['stdlib']`,
  144. # py26:sysconfig.get_config_vars('LIBDEST')), but fear platform variation may
  145. # make this ineffective, so hard-coding
  146. stdlib_pkgs = {"python", "wsgiref", "argparse"}
  147. # windows detection, covers cpython and ironpython
  148. WINDOWS = (sys.platform.startswith("win") or
  149. (sys.platform == 'cli' and os.name == 'nt'))
  150. def samefile(file1, file2):
  151. """Provide an alternative for os.path.samefile on Windows/Python2"""
  152. if hasattr(os.path, 'samefile'):
  153. return os.path.samefile(file1, file2)
  154. else:
  155. path1 = os.path.normcase(os.path.abspath(file1))
  156. path2 = os.path.normcase(os.path.abspath(file2))
  157. return path1 == path2
  158. if hasattr(shutil, 'get_terminal_size'):
  159. def get_terminal_size():
  160. """
  161. Returns a tuple (x, y) representing the width(x) and the height(y)
  162. in characters of the terminal window.
  163. """
  164. return tuple(shutil.get_terminal_size())
  165. else:
  166. def get_terminal_size():
  167. """
  168. Returns a tuple (x, y) representing the width(x) and the height(y)
  169. in characters of the terminal window.
  170. """
  171. def ioctl_GWINSZ(fd):
  172. try:
  173. import fcntl
  174. import termios
  175. import struct
  176. cr = struct.unpack_from(
  177. 'hh',
  178. fcntl.ioctl(fd, termios.TIOCGWINSZ, '12345678')
  179. )
  180. except:
  181. return None
  182. if cr == (0, 0):
  183. return None
  184. return cr
  185. cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
  186. if not cr:
  187. try:
  188. fd = os.open(os.ctermid(), os.O_RDONLY)
  189. cr = ioctl_GWINSZ(fd)
  190. os.close(fd)
  191. except:
  192. pass
  193. if not cr:
  194. cr = (os.environ.get('LINES', 25), os.environ.get('COLUMNS', 80))
  195. return int(cr[1]), int(cr[0])