Ejemplo n.º 1
0
    def create_pipfile(self, python=None):
        """Creates the Pipfile, filled with juicy defaults."""
        from .vendor.pip_shims.shims import InstallCommand

        # Inherit the pip's index configuration of install command.
        command = InstallCommand()
        indexes = command.cmd_opts.get_option("--extra-index-url").default
        sources = [self.default_source]
        for i, index in enumerate(indexes):
            if not index:
                continue

            source_name = "pip_index_{}".format(i)
            verify_ssl = index.startswith("https")
            sources.append({
                u"url": index,
                u"verify_ssl": verify_ssl,
                u"name": source_name
            })

        data = {
            u"source": sources,
            # Default packages.
            u"packages": {},
            u"dev-packages": {},
        }
        # Default requires.
        required_python = python
        if not python:
            if self.virtualenv_location:
                required_python = self.which("python",
                                             self.virtualenv_location)
            else:
                required_python = self.which("python")
        version = python_version(
            required_python) or self.s.PIPENV_DEFAULT_PYTHON_VERSION
        if version and len(version.split(".")) > 2:
            data[u"requires"] = {
                "python_version": ".".join(version.split(".")[:2])
            }
        self.write_toml(data)
Ejemplo n.º 2
0
def get_python_version(executable):
    version = utils.python_version(str(executable))
    if not version:
        raise PythonVersionNotFound(executable)
    return version
Ejemplo n.º 3
0
def createVenv(projectDir, pipfileLocation, unfurlLocation):
    """Create a virtual python environment for the given project."""

    os.environ["PIPENV_IGNORE_VIRTUALENVS"] = "1"
    VIRTUAL_ENV = os.environ.get("VIRTUAL_ENV")
    os.environ["PIPENV_VENV_IN_PROJECT"] = "1"
    if "PIPENV_PYTHON" not in os.environ:
        os.environ["PIPENV_PYTHON"] = sys.executable

    if pipfileLocation:
        pipfileLocation = os.path.abspath(pipfileLocation)

    try:
        cwd = os.getcwd()
        os.chdir(projectDir)
        # need to set env vars and change current dir before importing pipenv
        from pipenv.core import do_install, ensure_python
        from pipenv.utils import python_version
        from pipenv import environments

        pythonPath = str(ensure_python())
        assert pythonPath, pythonPath
        if not pipfileLocation:
            versionStr = python_version(pythonPath)
            assert versionStr, versionStr
            version = versionStr.rpartition(".")[0]  # 3.8.1 => 3.8
            # version = subprocess.run([pythonPath, "-V"]).stdout.decode()[
            #     7:10
            # ]  # e.g. Python 3.8.1 => 3.8
            pipfileLocation = os.path.join(_templatePath, "python" +
                                           version)  # e.g. templates/python3.8

        if not os.path.isdir(pipfileLocation):
            return 'Pipfile location is not a valid directory: "%s"' % pipfileLocation

        # copy Pipfiles to project root
        if os.path.abspath(projectDir) != os.path.abspath(pipfileLocation):
            for filename in ["Pipfile", "Pipfile.lock"]:
                path = os.path.join(pipfileLocation, filename)
                if os.path.isfile(path):
                    shutil.copy(path, projectDir)

        kw = dict(python=pythonPath)
        # need to run without args first so lock isn't overwritten
        retcode = _runPipEnv(do_install, kw)
        if retcode:
            return "Pipenv (step 1) failed: %s" % retcode

        # we need to set these so pipenv doesn't try to recreate the virtual environment
        environments.PIPENV_USE_SYSTEM = 1
        environments.PIPENV_IGNORE_VIRTUALENVS = False
        os.environ["VIRTUAL_ENV"] = os.path.join(projectDir, ".venv")
        environments.PIPENV_VIRTUALENV = os.path.join(projectDir, ".venv")

        # we need to set skip_lock or pipenv will not honor the existing lock
        kw["skip_lock"] = True
        if unfurlLocation:
            kw["editable_packages"] = [unfurlLocation]
        else:
            kw["packages"] = ["unfurl==" + __version__()
                              ]  # use the same version as current
        retcode = _runPipEnv(do_install, kw)
        if retcode:
            return "Pipenv (step 2) failed: %s" % retcode

        return ""
    finally:
        if VIRTUAL_ENV:
            os.environ["VIRTUAL_ENV"] = VIRTUAL_ENV
        else:
            os.environ.pop("VIRTUAL_ENV", None)
        os.chdir(cwd)
Ejemplo n.º 4
0
    def test_python_version_from_full_path(self):
        print(delegator.run('{0} --version'.format(FULL_PYTHON_PATH)).out)

        assert python_version(FULL_PYTHON_PATH) == "3.6.1"