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.

compat.py 8.4KB

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