| 783 | pkg_resources.declare_namespace(pkg) |
| 784 | |
| 785 | def _activate(self): |
| 786 | # type: () -> Iterable[Distribution] |
| 787 | |
| 788 | if not any(self._pex == os.path.realpath(path) for path in sys.path): |
| 789 | TRACER.log("Adding pex environment to the head of sys.path: {}".format(self._pex)) |
| 790 | sys.path.insert(0, self._pex) |
| 791 | |
| 792 | resolved = self.resolve() |
| 793 | for dist in resolved: |
| 794 | # N.B.: Since there can be more than one PEXEnvironment on the PEX_PATH we take care to |
| 795 | # avoid re-installing duplicate distributions we have in common with them. |
| 796 | if dist.location in sys.path: |
| 797 | continue |
| 798 | with TRACER.timed("Activating %s" % dist, V=2): |
| 799 | for entry in InstalledWheel.load(dist.location).iter_sys_path_entries(): |
| 800 | if self._pex_info.inherit_path == InheritPath.FALLBACK: |
| 801 | # Prepend location to sys.path. |
| 802 | # |
| 803 | # This ensures that bundled versions of libraries will be used before |
| 804 | # system-installed versions, in case something is installed in both, helping |
| 805 | # to favor hermeticity in the case of non-hermetic PEX files (i.e. those |
| 806 | # with inherit_path=True). |
| 807 | # |
| 808 | # If the path is not already in sys.path, site.addsitedir will append (not |
| 809 | # prepend) the path to sys.path. But if the path is already in sys.path, |
| 810 | # site.addsitedir will leave sys.path unmodified, but will do everything |
| 811 | # else it would do. This is not part of its advertised contract (which is |
| 812 | # very vague), but has been verified to be the case by inspecting its source |
| 813 | # for both cpython 2.7 and cpython 3.7. |
| 814 | sys.path.insert(0, entry) |
| 815 | else: |
| 816 | sys.path.append(entry) |
| 817 | |
| 818 | with TRACER.timed("Adding sitedir", V=2): |
| 819 | site.addsitedir(entry) |
| 820 | return resolved |