Exemplo n.º 1
0
    def _which(self, command, location=None, allow_global=False):
        if not allow_global and location is None:
            if self.virtualenv_exists:
                location = self.virtualenv_location
            else:
                location = os.environ.get("VIRTUAL_ENV", None)
        if not (location and os.path.exists(location)) and not allow_global:
            raise RuntimeError("location not created nor specified")

        version_str = "python{}".format(".".join(
            [str(v) for v in sys.version_info[:2]]))
        is_python = command in ("python", os.path.basename(sys.executable),
                                version_str)
        if not allow_global:
            if os.name == "nt":
                p = find_windows_executable(os.path.join(location, "Scripts"),
                                            command)
            else:
                p = os.path.join(location, "bin", command)
        else:
            if is_python:
                p = sys.executable
        if not os.path.exists(p):
            if is_python:
                p = sys.executable or system_which("python")
            else:
                p = system_which(command)
        return p
Exemplo n.º 2
0
def test_find_windows_executable(mocked_find_executable, mocked_isfile):
    mocked_isfile.return_value = False
    mocked_find_executable.return_value = None
    found = utils.find_windows_executable('fake/path', 'python')
    assert found is None

    assert mocked_isfile.call_count > 1

    calls = [mock.call('fake\\path\\python')] + [
        mock.call('fake\\path\\python{0}'.format(ext.lower()))
        for ext in os.environ['PATHEXT'].split(';')
    ]
    assert mocked_isfile.mock_calls == calls
Exemplo n.º 3
0
    def _find_python_installer_by_name_and_env(name, env_var):
        """
        Given a python installer (pyenv or asdf), try to locate the binary for that
        installer.

        pyenv/asdf are not always present on PATH. Both installers also support a
        custom environment variable (PYENV_ROOT or ASDF_DIR) which alows them to
        be installed into a non-default location (the default/suggested source
        install location is in ~/.pyenv or ~/.asdf).

        For systems without the installers on PATH, and with a custom location
        (e.g. /opt/pyenv), Pipenv can use those installers without modifications to
        PATH, if an installer's respective environment variable is present in an
        environment's .env file.

        This function searches for installer binaries in the following locations,
        by precedence:
            1. On PATH, equivalent to which(1).
            2. In the "bin" subdirectory of PYENV_ROOT or ASDF_DIR, depending on the
               installer.
            3. In ~/.pyenv/bin or ~/.asdf/bin, depending on the installer.
        """
        for candidate in (
                # Look for the Python installer using the equivalent of 'which'. On
                # Homebrew-installed systems, the env var may not be set, but this
                # strategy will work.
                find_windows_executable('', name),
                # Check for explicitly set install locations (e.g. PYENV_ROOT, ASDF_DIR).
                os.path.join(
                    os.path.expanduser(os.getenv(env_var, '/dev/null')), 'bin',
                    name),
                # Check the pyenv/asdf-recommended from-source install locations
                os.path.join(os.path.expanduser(f'~/.{name}'), 'bin', name),
        ):
            if candidate is not None and os.path.isfile(
                    candidate) and os.access(candidate, os.X_OK):
                return candidate
        raise InstallerNotFound()