prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  Bootstrap demotion is not robust
  While working https://github.com/pantsbuild/pants/issues/6927 it was discovered that there were multiple `.bootstrap/` on the sys.path and only one was demoted, leading to pants, when running in a pex, picking .bootstrap.pex instead of the pex dist it in `.deps/`. In this case, Pants was purposefully duplicating pex path entries to work around a bug in the `coverage` library. PEX should be robust and demote all instances of it's `.bootstrap/` entry on the sys.path.
  
  </issue>
  <code>
  [start of pex/bootstrap.py]
  1 # coding=utf-8
  2 # Copyright 2018 Pants project contributors (see CONTRIBUTORS.md).
  3 # Licensed under the Apache License, Version 2.0 (see LICENSE).
  4 
  5 import os
  6 
  7 
  8 class Bootstrap(object):
  9   """Supports introspection of the PEX bootstrap code."""
  10 
  11   _INSTANCE = None
  12 
  13   @classmethod
  14   def locate(cls):
  15     """Locates the active PEX bootstrap.
  16 
  17     :rtype: :class:`Bootstrap`
  18     """
  19     if cls._INSTANCE is None:
  20       bootstrap_path = __file__
  21       module_import_path = __name__.split('.')
  22 
  23       # For example, our __file__ might be requests.pex/.bootstrap/pex/bootstrap.pyc and our import
  24       # path pex.bootstrap; so we walk back through all the module components of our import path to
  25       # find the base sys.path entry where we were found (requests.pex/.bootstrap in this example).
  26       for _ in module_import_path:
  27         bootstrap_path = os.path.dirname(bootstrap_path)
  28 
  29       cls._INSTANCE = cls(sys_path_entry=bootstrap_path)
  30     return cls._INSTANCE
  31 
  32   def __init__(self, sys_path_entry):
  33     self._sys_path_entry = sys_path_entry
  34     self._realpath = os.path.realpath(self._sys_path_entry)
  35 
  36   def demote(self):
  37     """Demote the bootstrap code to the end of the `sys.path` so it is found last.
  38 
  39     :return: The list of un-imported bootstrap modules.
  40     :rtype: list of :class:`types.ModuleType`
  41     """
  42     import sys  # Grab a hold of `sys` early since we'll be un-importing our module in this process.
  43 
  44     unimported_modules = []
  45     for name, module in reversed(sorted(sys.modules.items())):
  46       if self.imported_from_bootstrap(module):
  47         unimported_modules.append(sys.modules.pop(name))
  48 
  49     sys.path.remove(self._sys_path_entry)
  50     sys.path.append(self._sys_path_entry)
  51 
  52     return unimported_modules
  53 
  54   def imported_from_bootstrap(self, module):
  55     """Return ``True`` if the given ``module`` object was imported from bootstrap code.
  56 
  57     :param module: The module to check the provenance of.
  58     :type module: :class:`types.ModuleType`
  59     :rtype: bool
  60     """
  61 
  62     # A vendored module.
  63     path = getattr(module, '__file__', None)
  64     if path and os.path.realpath(path).startswith(self._realpath):
  65       return True
  66 
  67     # A vendored package.
  68     path = getattr(module, '__path__', None)
  69     if path and any(os.path.realpath(path_item).startswith(self._realpath)
  70                     for path_item in path):
  71       return True
  72 
  73     return False
  74 
  75   def __repr__(self):
  76     return '{cls}(sys_path_entry={sys_path_entry!r})'.format(cls=type(self).__name__,
  77                                                              sys_path_entry=self._sys_path_entry)
  78
  [end of pex/bootstrap.py]
  </code>
  
  I need you to solve this issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the following format.
  <patch>
  diff --git a/file.py b/file.py
  --- a/file.py
  +++ b/file.py
  @@ -1,27 +1,35 @@
   def euclidean(a, b):
  -    while b:
  -        a, b = b, a % b
  -    return a
  +    if b == 0:
  +        return a
  +    return euclidean(b, a % b)
   
   
   def bresenham(x0, y0, x1, y1):
       points = []
       dx = abs(x1 - x0)
       dy = abs(y1 - y0)
  -    sx = 1 if x0 < x1 else -1
  -    sy = 1 if y0 < y1 else -1
  -    err = dx - dy
  +    x, y = x0, y0
  +    sx = -1 if x0 > x1 else 1
  +    sy = -1 if y0 > y1 else 1
   
  -    while True:
  -        points.append((x0, y0))
  -        if x0 == x1 and y0 == y1:
  -            break
  -        e2 = 2 * err
  -        if e2 > -dy:
  -            err -= dy
  -            x0 += sx
  -        if e2 < dx:
  -            err += dx
  -            y0 += sy
  +    if dx > dy:
  +        err = dx / 2.0
  +        while x != x1:
  +            points.append((x, y))
  +            err -= dy
  +            if err < 0:
  +                y += sy
  +                err += dx
  +            x += sx
  +    else:
  +        err = dy / 2.0
  +        while y != y1:
  +            points.append((x, y))
  +            err -= dx
  +            if err < 0:
  +                x += sx
  +                err += dy
  +            y += sy
  +
  +    points.append((x, y))
       return points
  
  </patch>
golden_diff: diff --git a/pex/bootstrap.py b/pex/bootstrap.py
  --- a/pex/bootstrap.py
  +++ b/pex/bootstrap.py
  @@ -46,7 +46,7 @@
         if self.imported_from_bootstrap(module):
           unimported_modules.append(sys.modules.pop(name))
   
  -    sys.path.remove(self._sys_path_entry)
  +    sys.path[:] = [path for path in sys.path if os.path.realpath(path) != self._realpath]
       sys.path.append(self._sys_path_entry)
   
       return unimported_modules
  
verification_info: {"golden_diff": "diff --git a/pex/bootstrap.py b/pex/bootstrap.py\n--- a/pex/bootstrap.py\n+++ b/pex/bootstrap.py\n@@ -46,7 +46,7 @@\n       if self.imported_from_bootstrap(module):\n         unimported_modules.append(sys.modules.pop(name))\n \n-    sys.path.remove(self._sys_path_entry)\n+    sys.path[:] = [path for path in sys.path if os.path.realpath(path) != self._realpath]\n     sys.path.append(self._sys_path_entry)\n \n     return unimported_modules\n", "issue": "Bootstrap demotion is not robust\nWhile working https://github.com/pantsbuild/pants/issues/6927 it was discovered that there were multiple `.bootstrap/` on the sys.path and only one was demoted, leading to pants, when running in a pex, picking .bootstrap.pex instead of the pex dist it in `.deps/`. In this case, Pants was purposefully duplicating pex path entries to work around a bug in the `coverage` library. PEX should be robust and demote all instances of it's `.bootstrap/` entry on the sys.path.\n", "before_files": [{"content": "# coding=utf-8\n# Copyright 2018 Pants project contributors (see CONTRIBUTORS.md).\n# Licensed under the Apache License, Version 2.0 (see LICENSE).\n\nimport os\n\n\nclass Bootstrap(object):\n  \"\"\"Supports introspection of the PEX bootstrap code.\"\"\"\n\n  _INSTANCE = None\n\n  @classmethod\n  def locate(cls):\n    \"\"\"Locates the active PEX bootstrap.\n\n    :rtype: :class:`Bootstrap`\n    \"\"\"\n    if cls._INSTANCE is None:\n      bootstrap_path = __file__\n      module_import_path = __name__.split('.')\n\n      # For example, our __file__ might be requests.pex/.bootstrap/pex/bootstrap.pyc and our import\n      # path pex.bootstrap; so we walk back through all the module components of our import path to\n      # find the base sys.path entry where we were found (requests.pex/.bootstrap in this example).\n      for _ in module_import_path:\n        bootstrap_path = os.path.dirname(bootstrap_path)\n\n      cls._INSTANCE = cls(sys_path_entry=bootstrap_path)\n    return cls._INSTANCE\n\n  def __init__(self, sys_path_entry):\n    self._sys_path_entry = sys_path_entry\n    self._realpath = os.path.realpath(self._sys_path_entry)\n\n  def demote(self):\n    \"\"\"Demote the bootstrap code to the end of the `sys.path` so it is found last.\n\n    :return: The list of un-imported bootstrap modules.\n    :rtype: list of :class:`types.ModuleType`\n    \"\"\"\n    import sys  # Grab a hold of `sys` early since we'll be un-importing our module in this process.\n\n    unimported_modules = []\n    for name, module in reversed(sorted(sys.modules.items())):\n      if self.imported_from_bootstrap(module):\n        unimported_modules.append(sys.modules.pop(name))\n\n    sys.path.remove(self._sys_path_entry)\n    sys.path.append(self._sys_path_entry)\n\n    return unimported_modules\n\n  def imported_from_bootstrap(self, module):\n    \"\"\"Return ``True`` if the given ``module`` object was imported from bootstrap code.\n\n    :param module: The module to check the provenance of.\n    :type module: :class:`types.ModuleType`\n    :rtype: bool\n    \"\"\"\n\n    # A vendored module.\n    path = getattr(module, '__file__', None)\n    if path and os.path.realpath(path).startswith(self._realpath):\n      return True\n\n    # A vendored package.\n    path = getattr(module, '__path__', None)\n    if path and any(os.path.realpath(path_item).startswith(self._realpath)\n                    for path_item in path):\n      return True\n\n    return False\n\n  def __repr__(self):\n    return '{cls}(sys_path_entry={sys_path_entry!r})'.format(cls=type(self).__name__,\n                                                             sys_path_entry=self._sys_path_entry)\n", "path": "pex/bootstrap.py"}]}