We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
pex 1.2.14 breaks entrypoint targeting when PEX_PYTHON is present
with 1.2.13, targeting an entrypoint with `-e` et al results in an attempt to load that entrypoint at runtime:

```
[omerta ~]$ pip install pex==1.2.13 2>&1 >/dev/null
[omerta ~]$ pex --version
pex 1.2.13
[omerta ~]$ pex -e 'pants.bin.pants_loader:main' pantsbuild.pants -o /tmp/pants.pex
[omerta ~]$ /tmp/pants.pex
Traceback (most recent call last):
  File ".bootstrap/_pex/pex.py", line 365, in execute
  File ".bootstrap/_pex/pex.py", line 293, in _wrap_coverage
  File ".bootstrap/_pex/pex.py", line 325, in _wrap_profiling
  File ".bootstrap/_pex/pex.py", line 408, in _execute
  File ".bootstrap/_pex/pex.py", line 466, in execute_entry
  File ".bootstrap/_pex/pex.py", line 480, in execute_pkg_resources
  File ".bootstrap/pkg_resources/__init__.py", line 2297, in resolve
ImportError: No module named pants_loader
```

with 1.2.14, it seems to be re-execing against the `PEX_PYTHON` interpreter sans args which results in a bare repl when the pex is run:

```
[omerta ~]$ pip install pex==1.2.14 2>&1 >/dev/null
[omerta ~]$ pex --version
pex 1.2.14
[omerta ~]$ pex -e 'pants.bin.pants_loader:main' pantsbuild.pants -o /tmp/pants.pex
[omerta ~]$ PEX_VERBOSE=9 /tmp/pants.pex
pex: Please build pex with the subprocess32 module for more reliable requirement installation and interpreter execution.
pex: Selecting runtime interpreter based on pexrc: 0.1ms
pex: Re-executing: cmdline="['/opt/ee/python/2.7/bin/python2.7']", sys.executable="/Users/kwilson/Python/CPython-2.7.13/bin/python2.7", PEX_PYTHON="None", PEX_PYTHON_PATH="None", COMPATIBILITY_CONSTRAINTS="[]"
Python 2.7.10 (default, Dec 16 2015, 14:09:45) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 
```

cc @CMLivingston since this appears related to #427 

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `pex/pex_bootstrapper.py`
Content:
```
1 # Copyright 2014 Pants project contributors (see CONTRIBUTORS.md).
2 # Licensed under the Apache License, Version 2.0 (see LICENSE).
3 from __future__ import print_function
4 import os
5 import sys
6 
7 from .common import die, open_zip
8 from .executor import Executor
9 from .interpreter import PythonInterpreter
10 from .interpreter_constraints import matched_interpreters
11 from .tracer import TRACER
12 from .variables import ENV
13 
14 __all__ = ('bootstrap_pex',)
15 
16 
17 def pex_info_name(entry_point):
18   """Return the PEX-INFO for an entry_point"""
19   return os.path.join(entry_point, 'PEX-INFO')
20 
21 
22 def is_compressed(entry_point):
23   return os.path.exists(entry_point) and not os.path.exists(pex_info_name(entry_point))
24 
25 
26 def read_pexinfo_from_directory(entry_point):
27   with open(pex_info_name(entry_point), 'rb') as fp:
28     return fp.read()
29 
30 
31 def read_pexinfo_from_zip(entry_point):
32   with open_zip(entry_point) as zf:
33     return zf.read('PEX-INFO')
34 
35 
36 def read_pex_info_content(entry_point):
37   """Return the raw content of a PEX-INFO."""
38   if is_compressed(entry_point):
39     return read_pexinfo_from_zip(entry_point)
40   else:
41     return read_pexinfo_from_directory(entry_point)
42 
43 
44 def get_pex_info(entry_point):
45   """Return the PexInfo object for an entry point."""
46   from . import pex_info
47 
48   pex_info_content = read_pex_info_content(entry_point)
49   if pex_info_content:
50     return pex_info.PexInfo.from_json(pex_info_content)
51   raise ValueError('Invalid entry_point: %s' % entry_point)
52 
53 
54 def find_in_path(target_interpreter):
55   if os.path.exists(target_interpreter):
56     return target_interpreter
57 
58   for directory in os.getenv('PATH', '').split(os.pathsep):
59     try_path = os.path.join(directory, target_interpreter)
60     if os.path.exists(try_path):
61       return try_path
62 
63 
64 def find_compatible_interpreters(pex_python_path, compatibility_constraints):
65   """Find all compatible interpreters on the system within the supplied constraints and use
66      PEX_PYTHON_PATH if it is set. If not, fall back to interpreters on $PATH.
67   """
68   if pex_python_path:
69     interpreters = []
70     for binary in pex_python_path.split(os.pathsep):
71       try:
72         interpreters.append(PythonInterpreter.from_binary(binary))
73       except Executor.ExecutionError:
74         print("Python interpreter %s in PEX_PYTHON_PATH failed to load properly." % binary,
75           file=sys.stderr)
76     if not interpreters:
77       die('PEX_PYTHON_PATH was defined, but no valid interpreters could be identified. Exiting.')
78   else:
79     if not os.getenv('PATH', ''):
80       # no $PATH, use sys.executable
81       interpreters = [PythonInterpreter.get()]
82     else:
83       # get all qualifying interpreters found in $PATH
84       interpreters = PythonInterpreter.all()
85 
86   return list(matched_interpreters(
87     interpreters, compatibility_constraints, meet_all_constraints=True))
88 
89 
90 def _select_pex_python_interpreter(target_python, compatibility_constraints):
91   target = find_in_path(target_python)
92 
93   if not target:
94     die('Failed to find interpreter specified by PEX_PYTHON: %s' % target)
95   if compatibility_constraints:
96     pi = PythonInterpreter.from_binary(target)
97     if not list(matched_interpreters([pi], compatibility_constraints, meet_all_constraints=True)):
98       die('Interpreter specified by PEX_PYTHON (%s) is not compatible with specified '
99           'interpreter constraints: %s' % (target, str(compatibility_constraints)))
100   if not os.path.exists(target):
101     die('Target interpreter specified by PEX_PYTHON %s does not exist. Exiting.' % target)
102   return target
103 
104 
105 def _select_interpreter(pex_python_path, compatibility_constraints):
106   compatible_interpreters = find_compatible_interpreters(
107     pex_python_path, compatibility_constraints)
108 
109   if not compatible_interpreters:
110     die('Failed to find compatible interpreter for constraints: %s'
111         % str(compatibility_constraints))
112   # TODO: https://github.com/pantsbuild/pex/issues/430
113   target = min(compatible_interpreters).binary
114 
115   if os.path.exists(target) and os.path.realpath(target) != os.path.realpath(sys.executable):
116     return target
117 
118 
119 def maybe_reexec_pex(compatibility_constraints):
120   """
121   Handle environment overrides for the Python interpreter to use when executing this pex.
122 
123   This function supports interpreter filtering based on interpreter constraints stored in PEX-INFO
124   metadata. If PEX_PYTHON is set in a pexrc, it attempts to obtain the binary location of the
125   interpreter specified by PEX_PYTHON. If PEX_PYTHON_PATH is set, it attempts to search the path for
126   a matching interpreter in accordance with the interpreter constraints. If both variables are
127   present in a pexrc, this function gives precedence to PEX_PYTHON_PATH and errors out if no
128   compatible interpreters can be found on said path. If neither variable is set, fall through to
129   plain pex execution using PATH searching or the currently executing interpreter.
130 
131   :param compatibility_constraints: list of requirements-style strings that constrain the
132   Python interpreter to re-exec this pex with.
133 
134   """
135   if ENV.SHOULD_EXIT_BOOTSTRAP_REEXEC:
136     return
137 
138   selected_interpreter = None
139   with TRACER.timed('Selecting runtime interpreter based on pexrc', V=3):
140     if ENV.PEX_PYTHON and not ENV.PEX_PYTHON_PATH:
141       # preserve PEX_PYTHON re-exec for backwards compatibility
142       # TODO: Kill this off completely in favor of PEX_PYTHON_PATH
143       # https://github.com/pantsbuild/pex/issues/431
144       selected_interpreter = _select_pex_python_interpreter(ENV.PEX_PYTHON,
145                                                             compatibility_constraints)
146     elif ENV.PEX_PYTHON_PATH:
147       selected_interpreter = _select_interpreter(ENV.PEX_PYTHON_PATH, compatibility_constraints)
148 
149   if selected_interpreter:
150     ENV.delete('PEX_PYTHON')
151     ENV.delete('PEX_PYTHON_PATH')
152     ENV.SHOULD_EXIT_BOOTSTRAP_REEXEC = True
153     cmdline = [selected_interpreter] + sys.argv[1:]
154     TRACER.log('Re-executing: cmdline="%s", sys.executable="%s", PEX_PYTHON="%s", '
155                'PEX_PYTHON_PATH="%s", COMPATIBILITY_CONSTRAINTS="%s"'
156                % (cmdline, sys.executable, ENV.PEX_PYTHON, ENV.PEX_PYTHON_PATH,
157                   compatibility_constraints))
158     os.execve(selected_interpreter, cmdline, ENV.copy())
159 
160 
161 def bootstrap_pex(entry_point):
162   from .finders import register_finders
163   register_finders()
164   pex_info = get_pex_info(entry_point)
165   maybe_reexec_pex(pex_info.interpreter_constraints)
166 
167   from . import pex
168   pex.PEX(entry_point).execute()
169 
170 
171 def bootstrap_pex_env(entry_point):
172   """Bootstrap the current runtime environment using a given pex."""
173   from .environment import PEXEnvironment
174   from .finders import register_finders
175   from .pex_info import PexInfo
176 
177   register_finders()
178 
179   PEXEnvironment(entry_point, PexInfo.from_pex(entry_point)).activate()
180
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/pex/pex_bootstrapper.py b/pex/pex_bootstrapper.py
--- a/pex/pex_bootstrapper.py
+++ b/pex/pex_bootstrapper.py
@@ -1,6 +1,7 @@
 # Copyright 2014 Pants project contributors (see CONTRIBUTORS.md).
 # Licensed under the Apache License, Version 2.0 (see LICENSE).
 from __future__ import print_function
+
 import os
 import sys
 
@@ -147,14 +148,14 @@
       selected_interpreter = _select_interpreter(ENV.PEX_PYTHON_PATH, compatibility_constraints)
 
   if selected_interpreter:
-    ENV.delete('PEX_PYTHON')
-    ENV.delete('PEX_PYTHON_PATH')
-    ENV.SHOULD_EXIT_BOOTSTRAP_REEXEC = True
-    cmdline = [selected_interpreter] + sys.argv[1:]
+    cmdline = [selected_interpreter] + sys.argv
     TRACER.log('Re-executing: cmdline="%s", sys.executable="%s", PEX_PYTHON="%s", '
                'PEX_PYTHON_PATH="%s", COMPATIBILITY_CONSTRAINTS="%s"'
                % (cmdline, sys.executable, ENV.PEX_PYTHON, ENV.PEX_PYTHON_PATH,
                   compatibility_constraints))
+    ENV.delete('PEX_PYTHON')
+    ENV.delete('PEX_PYTHON_PATH')
+    ENV.SHOULD_EXIT_BOOTSTRAP_REEXEC = True
     os.execve(selected_interpreter, cmdline, ENV.copy())

{"golden_diff": "diff --git a/pex/pex_bootstrapper.py b/pex/pex_bootstrapper.py\n--- a/pex/pex_bootstrapper.py\n+++ b/pex/pex_bootstrapper.py\n@@ -1,6 +1,7 @@\n # Copyright 2014 Pants project contributors (see CONTRIBUTORS.md).\n # Licensed under the Apache License, Version 2.0 (see LICENSE).\n from __future__ import print_function\n+\n import os\n import sys\n \n@@ -147,14 +148,14 @@\n       selected_interpreter = _select_interpreter(ENV.PEX_PYTHON_PATH, compatibility_constraints)\n \n   if selected_interpreter:\n-    ENV.delete('PEX_PYTHON')\n-    ENV.delete('PEX_PYTHON_PATH')\n-    ENV.SHOULD_EXIT_BOOTSTRAP_REEXEC = True\n-    cmdline = [selected_interpreter] + sys.argv[1:]\n+    cmdline = [selected_interpreter] + sys.argv\n     TRACER.log('Re-executing: cmdline=\"%s\", sys.executable=\"%s\", PEX_PYTHON=\"%s\", '\n                'PEX_PYTHON_PATH=\"%s\", COMPATIBILITY_CONSTRAINTS=\"%s\"'\n                % (cmdline, sys.executable, ENV.PEX_PYTHON, ENV.PEX_PYTHON_PATH,\n                   compatibility_constraints))\n+    ENV.delete('PEX_PYTHON')\n+    ENV.delete('PEX_PYTHON_PATH')\n+    ENV.SHOULD_EXIT_BOOTSTRAP_REEXEC = True\n     os.execve(selected_interpreter, cmdline, ENV.copy())\n", "issue": "pex 1.2.14 breaks entrypoint targeting when PEX_PYTHON is present\nwith 1.2.13, targeting an entrypoint with `-e` et al results in an attempt to load that entrypoint at runtime:\r\n\r\n```\r\n[omerta ~]$ pip install pex==1.2.13 2>&1 >/dev/null\r\n[omerta ~]$ pex --version\r\npex 1.2.13\r\n[omerta ~]$ pex -e 'pants.bin.pants_loader:main' pantsbuild.pants -o /tmp/pants.pex\r\n[omerta ~]$ /tmp/pants.pex\r\nTraceback (most recent call last):\r\n  File \".bootstrap/_pex/pex.py\", line 365, in execute\r\n  File \".bootstrap/_pex/pex.py\", line 293, in _wrap_coverage\r\n  File \".bootstrap/_pex/pex.py\", line 325, in _wrap_profiling\r\n  File \".bootstrap/_pex/pex.py\", line 408, in _execute\r\n  File \".bootstrap/_pex/pex.py\", line 466, in execute_entry\r\n  File \".bootstrap/_pex/pex.py\", line 480, in execute_pkg_resources\r\n  File \".bootstrap/pkg_resources/__init__.py\", line 2297, in resolve\r\nImportError: No module named pants_loader\r\n```\r\n\r\nwith 1.2.14, it seems to be re-execing against the `PEX_PYTHON` interpreter sans args which results in a bare repl when the pex is run:\r\n\r\n```\r\n[omerta ~]$ pip install pex==1.2.14 2>&1 >/dev/null\r\n[omerta ~]$ pex --version\r\npex 1.2.14\r\n[omerta ~]$ pex -e 'pants.bin.pants_loader:main' pantsbuild.pants -o /tmp/pants.pex\r\n[omerta ~]$ PEX_VERBOSE=9 /tmp/pants.pex\r\npex: Please build pex with the subprocess32 module for more reliable requirement installation and interpreter execution.\r\npex: Selecting runtime interpreter based on pexrc: 0.1ms\r\npex: Re-executing: cmdline=\"['/opt/ee/python/2.7/bin/python2.7']\", sys.executable=\"/Users/kwilson/Python/CPython-2.7.13/bin/python2.7\", PEX_PYTHON=\"None\", PEX_PYTHON_PATH=\"None\", COMPATIBILITY_CONSTRAINTS=\"[]\"\r\nPython 2.7.10 (default, Dec 16 2015, 14:09:45) \r\n[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin\r\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.\r\n>>> \r\n```\r\n\r\ncc @CMLivingston since this appears related to #427 \n", "before_files": [{"content": "# Copyright 2014 Pants project contributors (see CONTRIBUTORS.md).\n# Licensed under the Apache License, Version 2.0 (see LICENSE).\nfrom __future__ import print_function\nimport os\nimport sys\n\nfrom .common import die, open_zip\nfrom .executor import Executor\nfrom .interpreter import PythonInterpreter\nfrom .interpreter_constraints import matched_interpreters\nfrom .tracer import TRACER\nfrom .variables import ENV\n\n__all__ = ('bootstrap_pex',)\n\n\ndef pex_info_name(entry_point):\n  \"\"\"Return the PEX-INFO for an entry_point\"\"\"\n  return os.path.join(entry_point, 'PEX-INFO')\n\n\ndef is_compressed(entry_point):\n  return os.path.exists(entry_point) and not os.path.exists(pex_info_name(entry_point))\n\n\ndef read_pexinfo_from_directory(entry_point):\n  with open(pex_info_name(entry_point), 'rb') as fp:\n    return fp.read()\n\n\ndef read_pexinfo_from_zip(entry_point):\n  with open_zip(entry_point) as zf:\n    return zf.read('PEX-INFO')\n\n\ndef read_pex_info_content(entry_point):\n  \"\"\"Return the raw content of a PEX-INFO.\"\"\"\n  if is_compressed(entry_point):\n    return read_pexinfo_from_zip(entry_point)\n  else:\n    return read_pexinfo_from_directory(entry_point)\n\n\ndef get_pex_info(entry_point):\n  \"\"\"Return the PexInfo object for an entry point.\"\"\"\n  from . import pex_info\n\n  pex_info_content = read_pex_info_content(entry_point)\n  if pex_info_content:\n    return pex_info.PexInfo.from_json(pex_info_content)\n  raise ValueError('Invalid entry_point: %s' % entry_point)\n\n\ndef find_in_path(target_interpreter):\n  if os.path.exists(target_interpreter):\n    return target_interpreter\n\n  for directory in os.getenv('PATH', '').split(os.pathsep):\n    try_path = os.path.join(directory, target_interpreter)\n    if os.path.exists(try_path):\n      return try_path\n\n\ndef find_compatible_interpreters(pex_python_path, compatibility_constraints):\n  \"\"\"Find all compatible interpreters on the system within the supplied constraints and use\n     PEX_PYTHON_PATH if it is set. If not, fall back to interpreters on $PATH.\n  \"\"\"\n  if pex_python_path:\n    interpreters = []\n    for binary in pex_python_path.split(os.pathsep):\n      try:\n        interpreters.append(PythonInterpreter.from_binary(binary))\n      except Executor.ExecutionError:\n        print(\"Python interpreter %s in PEX_PYTHON_PATH failed to load properly.\" % binary,\n          file=sys.stderr)\n    if not interpreters:\n      die('PEX_PYTHON_PATH was defined, but no valid interpreters could be identified. Exiting.')\n  else:\n    if not os.getenv('PATH', ''):\n      # no $PATH, use sys.executable\n      interpreters = [PythonInterpreter.get()]\n    else:\n      # get all qualifying interpreters found in $PATH\n      interpreters = PythonInterpreter.all()\n\n  return list(matched_interpreters(\n    interpreters, compatibility_constraints, meet_all_constraints=True))\n\n\ndef _select_pex_python_interpreter(target_python, compatibility_constraints):\n  target = find_in_path(target_python)\n\n  if not target:\n    die('Failed to find interpreter specified by PEX_PYTHON: %s' % target)\n  if compatibility_constraints:\n    pi = PythonInterpreter.from_binary(target)\n    if not list(matched_interpreters([pi], compatibility_constraints, meet_all_constraints=True)):\n      die('Interpreter specified by PEX_PYTHON (%s) is not compatible with specified '\n          'interpreter constraints: %s' % (target, str(compatibility_constraints)))\n  if not os.path.exists(target):\n    die('Target interpreter specified by PEX_PYTHON %s does not exist. Exiting.' % target)\n  return target\n\n\ndef _select_interpreter(pex_python_path, compatibility_constraints):\n  compatible_interpreters = find_compatible_interpreters(\n    pex_python_path, compatibility_constraints)\n\n  if not compatible_interpreters:\n    die('Failed to find compatible interpreter for constraints: %s'\n        % str(compatibility_constraints))\n  # TODO: https://github.com/pantsbuild/pex/issues/430\n  target = min(compatible_interpreters).binary\n\n  if os.path.exists(target) and os.path.realpath(target) != os.path.realpath(sys.executable):\n    return target\n\n\ndef maybe_reexec_pex(compatibility_constraints):\n  \"\"\"\n  Handle environment overrides for the Python interpreter to use when executing this pex.\n\n  This function supports interpreter filtering based on interpreter constraints stored in PEX-INFO\n  metadata. If PEX_PYTHON is set in a pexrc, it attempts to obtain the binary location of the\n  interpreter specified by PEX_PYTHON. If PEX_PYTHON_PATH is set, it attempts to search the path for\n  a matching interpreter in accordance with the interpreter constraints. If both variables are\n  present in a pexrc, this function gives precedence to PEX_PYTHON_PATH and errors out if no\n  compatible interpreters can be found on said path. If neither variable is set, fall through to\n  plain pex execution using PATH searching or the currently executing interpreter.\n\n  :param compatibility_constraints: list of requirements-style strings that constrain the\n  Python interpreter to re-exec this pex with.\n\n  \"\"\"\n  if ENV.SHOULD_EXIT_BOOTSTRAP_REEXEC:\n    return\n\n  selected_interpreter = None\n  with TRACER.timed('Selecting runtime interpreter based on pexrc', V=3):\n    if ENV.PEX_PYTHON and not ENV.PEX_PYTHON_PATH:\n      # preserve PEX_PYTHON re-exec for backwards compatibility\n      # TODO: Kill this off completely in favor of PEX_PYTHON_PATH\n      # https://github.com/pantsbuild/pex/issues/431\n      selected_interpreter = _select_pex_python_interpreter(ENV.PEX_PYTHON,\n                                                            compatibility_constraints)\n    elif ENV.PEX_PYTHON_PATH:\n      selected_interpreter = _select_interpreter(ENV.PEX_PYTHON_PATH, compatibility_constraints)\n\n  if selected_interpreter:\n    ENV.delete('PEX_PYTHON')\n    ENV.delete('PEX_PYTHON_PATH')\n    ENV.SHOULD_EXIT_BOOTSTRAP_REEXEC = True\n    cmdline = [selected_interpreter] + sys.argv[1:]\n    TRACER.log('Re-executing: cmdline=\"%s\", sys.executable=\"%s\", PEX_PYTHON=\"%s\", '\n               'PEX_PYTHON_PATH=\"%s\", COMPATIBILITY_CONSTRAINTS=\"%s\"'\n               % (cmdline, sys.executable, ENV.PEX_PYTHON, ENV.PEX_PYTHON_PATH,\n                  compatibility_constraints))\n    os.execve(selected_interpreter, cmdline, ENV.copy())\n\n\ndef bootstrap_pex(entry_point):\n  from .finders import register_finders\n  register_finders()\n  pex_info = get_pex_info(entry_point)\n  maybe_reexec_pex(pex_info.interpreter_constraints)\n\n  from . import pex\n  pex.PEX(entry_point).execute()\n\n\ndef bootstrap_pex_env(entry_point):\n  \"\"\"Bootstrap the current runtime environment using a given pex.\"\"\"\n  from .environment import PEXEnvironment\n  from .finders import register_finders\n  from .pex_info import PexInfo\n\n  register_finders()\n\n  PEXEnvironment(entry_point, PexInfo.from_pex(entry_point)).activate()\n", "path": "pex/pex_bootstrapper.py"}], "after_files": [{"content": "# Copyright 2014 Pants project contributors (see CONTRIBUTORS.md).\n# Licensed under the Apache License, Version 2.0 (see LICENSE).\nfrom __future__ import print_function\n\nimport os\nimport sys\n\nfrom .common import die, open_zip\nfrom .executor import Executor\nfrom .interpreter import PythonInterpreter\nfrom .interpreter_constraints import matched_interpreters\nfrom .tracer import TRACER\nfrom .variables import ENV\n\n__all__ = ('bootstrap_pex',)\n\n\ndef pex_info_name(entry_point):\n  \"\"\"Return the PEX-INFO for an entry_point\"\"\"\n  return os.path.join(entry_point, 'PEX-INFO')\n\n\ndef is_compressed(entry_point):\n  return os.path.exists(entry_point) and not os.path.exists(pex_info_name(entry_point))\n\n\ndef read_pexinfo_from_directory(entry_point):\n  with open(pex_info_name(entry_point), 'rb') as fp:\n    return fp.read()\n\n\ndef read_pexinfo_from_zip(entry_point):\n  with open_zip(entry_point) as zf:\n    return zf.read('PEX-INFO')\n\n\ndef read_pex_info_content(entry_point):\n  \"\"\"Return the raw content of a PEX-INFO.\"\"\"\n  if is_compressed(entry_point):\n    return read_pexinfo_from_zip(entry_point)\n  else:\n    return read_pexinfo_from_directory(entry_point)\n\n\ndef get_pex_info(entry_point):\n  \"\"\"Return the PexInfo object for an entry point.\"\"\"\n  from . import pex_info\n\n  pex_info_content = read_pex_info_content(entry_point)\n  if pex_info_content:\n    return pex_info.PexInfo.from_json(pex_info_content)\n  raise ValueError('Invalid entry_point: %s' % entry_point)\n\n\ndef find_in_path(target_interpreter):\n  if os.path.exists(target_interpreter):\n    return target_interpreter\n\n  for directory in os.getenv('PATH', '').split(os.pathsep):\n    try_path = os.path.join(directory, target_interpreter)\n    if os.path.exists(try_path):\n      return try_path\n\n\ndef find_compatible_interpreters(pex_python_path, compatibility_constraints):\n  \"\"\"Find all compatible interpreters on the system within the supplied constraints and use\n     PEX_PYTHON_PATH if it is set. If not, fall back to interpreters on $PATH.\n  \"\"\"\n  if pex_python_path:\n    interpreters = []\n    for binary in pex_python_path.split(os.pathsep):\n      try:\n        interpreters.append(PythonInterpreter.from_binary(binary))\n      except Executor.ExecutionError:\n        print(\"Python interpreter %s in PEX_PYTHON_PATH failed to load properly.\" % binary,\n          file=sys.stderr)\n    if not interpreters:\n      die('PEX_PYTHON_PATH was defined, but no valid interpreters could be identified. Exiting.')\n  else:\n    if not os.getenv('PATH', ''):\n      # no $PATH, use sys.executable\n      interpreters = [PythonInterpreter.get()]\n    else:\n      # get all qualifying interpreters found in $PATH\n      interpreters = PythonInterpreter.all()\n\n  return list(matched_interpreters(\n    interpreters, compatibility_constraints, meet_all_constraints=True))\n\n\ndef _select_pex_python_interpreter(target_python, compatibility_constraints):\n  target = find_in_path(target_python)\n\n  if not target:\n    die('Failed to find interpreter specified by PEX_PYTHON: %s' % target)\n  if compatibility_constraints:\n    pi = PythonInterpreter.from_binary(target)\n    if not list(matched_interpreters([pi], compatibility_constraints, meet_all_constraints=True)):\n      die('Interpreter specified by PEX_PYTHON (%s) is not compatible with specified '\n          'interpreter constraints: %s' % (target, str(compatibility_constraints)))\n  if not os.path.exists(target):\n    die('Target interpreter specified by PEX_PYTHON %s does not exist. Exiting.' % target)\n  return target\n\n\ndef _select_interpreter(pex_python_path, compatibility_constraints):\n  compatible_interpreters = find_compatible_interpreters(\n    pex_python_path, compatibility_constraints)\n\n  if not compatible_interpreters:\n    die('Failed to find compatible interpreter for constraints: %s'\n        % str(compatibility_constraints))\n  # TODO: https://github.com/pantsbuild/pex/issues/430\n  target = min(compatible_interpreters).binary\n\n  if os.path.exists(target) and os.path.realpath(target) != os.path.realpath(sys.executable):\n    return target\n\n\ndef maybe_reexec_pex(compatibility_constraints):\n  \"\"\"\n  Handle environment overrides for the Python interpreter to use when executing this pex.\n\n  This function supports interpreter filtering based on interpreter constraints stored in PEX-INFO\n  metadata. If PEX_PYTHON is set in a pexrc, it attempts to obtain the binary location of the\n  interpreter specified by PEX_PYTHON. If PEX_PYTHON_PATH is set, it attempts to search the path for\n  a matching interpreter in accordance with the interpreter constraints. If both variables are\n  present in a pexrc, this function gives precedence to PEX_PYTHON_PATH and errors out if no\n  compatible interpreters can be found on said path. If neither variable is set, fall through to\n  plain pex execution using PATH searching or the currently executing interpreter.\n\n  :param compatibility_constraints: list of requirements-style strings that constrain the\n  Python interpreter to re-exec this pex with.\n\n  \"\"\"\n  if ENV.SHOULD_EXIT_BOOTSTRAP_REEXEC:\n    return\n\n  selected_interpreter = None\n  with TRACER.timed('Selecting runtime interpreter based on pexrc', V=3):\n    if ENV.PEX_PYTHON and not ENV.PEX_PYTHON_PATH:\n      # preserve PEX_PYTHON re-exec for backwards compatibility\n      # TODO: Kill this off completely in favor of PEX_PYTHON_PATH\n      # https://github.com/pantsbuild/pex/issues/431\n      selected_interpreter = _select_pex_python_interpreter(ENV.PEX_PYTHON,\n                                                            compatibility_constraints)\n    elif ENV.PEX_PYTHON_PATH:\n      selected_interpreter = _select_interpreter(ENV.PEX_PYTHON_PATH, compatibility_constraints)\n\n  if selected_interpreter:\n    cmdline = [selected_interpreter] + sys.argv\n    TRACER.log('Re-executing: cmdline=\"%s\", sys.executable=\"%s\", PEX_PYTHON=\"%s\", '\n               'PEX_PYTHON_PATH=\"%s\", COMPATIBILITY_CONSTRAINTS=\"%s\"'\n               % (cmdline, sys.executable, ENV.PEX_PYTHON, ENV.PEX_PYTHON_PATH,\n                  compatibility_constraints))\n    ENV.delete('PEX_PYTHON')\n    ENV.delete('PEX_PYTHON_PATH')\n    ENV.SHOULD_EXIT_BOOTSTRAP_REEXEC = True\n    os.execve(selected_interpreter, cmdline, ENV.copy())\n\n\ndef bootstrap_pex(entry_point):\n  from .finders import register_finders\n  register_finders()\n  pex_info = get_pex_info(entry_point)\n  maybe_reexec_pex(pex_info.interpreter_constraints)\n\n  from . import pex\n  pex.PEX(entry_point).execute()\n\n\ndef bootstrap_pex_env(entry_point):\n  \"\"\"Bootstrap the current runtime environment using a given pex.\"\"\"\n  from .environment import PEXEnvironment\n  from .finders import register_finders\n  from .pex_info import PexInfo\n\n  register_finders()\n\n  PEXEnvironment(entry_point, PexInfo.from_pex(entry_point)).activate()\n", "path": "pex/pex_bootstrapper.py"}]}