Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate with pyenv #37

Merged
merged 2 commits into from
Feb 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/.ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ jobs:
pdm install -d
- name: Test
run: |
rm .pdm.toml
python -m pdm run pytest -n auto --cov pdm --cov-config=setup.cfg tests
1 change: 1 addition & 0 deletions news/36.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use the pyenv interperter value if pyenv is installed.
2 changes: 1 addition & 1 deletion pdm/builders/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@

from pip_shims import shims
from pkg_resources import safe_name, safe_version, to_filename
from vistir.path import normalize_path

from pdm.builders.base import Builder
from pdm.context import context
from pdm.exceptions import WheelBuildError
from pdm.utils import cached_property, get_abi_tag
from vistir.path import normalize_path

WHEEL_FILE_FORMAT = """\
Wheel-Version: 1.0
Expand Down
6 changes: 3 additions & 3 deletions pdm/cli/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
from pathlib import Path
from typing import Dict, Iterable, Optional, Sequence

from pkg_resources import safe_name

import click
import halo
import pythonfinder
import tomlkit
from pkg_resources import safe_name

from pdm.builders import SdistBuilder, WheelBuilder
from pdm.context import context
from pdm.exceptions import NoPythonVersion, ProjectError
Expand Down Expand Up @@ -467,7 +467,7 @@ def do_use(project: Project, python: str) -> None:
)
)

project.config["python"] = Path(python_path).as_posix()
project.config["python.path"] = Path(python_path).as_posix()
project.config.save_config()


Expand Down
1 change: 1 addition & 0 deletions pdm/cli/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import click
from click._compat import term_len
from click.formatting import HelpFormatter, iter_rows, measure_table, wrap_text

from pdm.cli import actions
from pdm.cli.options import (
dry_run_option,
Expand Down
1 change: 1 addition & 0 deletions pdm/cli/options.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import click

from pdm.context import context


Expand Down
4 changes: 0 additions & 4 deletions pdm/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ def __init__(self):
def init(self, project):
self.project = project

@property
def initialized(self) -> bool:
return self._initialized

@property
def cache_dir(self) -> Path:
return Path(self.project.config.get("cache_dir"))
Expand Down
1 change: 1 addition & 0 deletions pdm/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import click
from click._compat import get_text_stderr

from pdm.context import context


Expand Down
14 changes: 3 additions & 11 deletions pdm/installers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,16 @@
import subprocess
from typing import Dict, List, Tuple

import distlib.scripts
from distlib.wheel import Wheel
from pip._vendor.pkg_resources import Distribution, EggInfoDistribution, safe_name
from pip_shims import shims
from vistir import cd

import distlib.scripts
from distlib.wheel import Wheel
from pdm.context import context
from pdm.models.candidates import Candidate
from pdm.models.environment import Environment
from pdm.models.requirements import parse_requirement, strip_extras
from vistir import cd

SETUPTOOLS_SHIM = (
"import setuptools, tokenize;__file__=%r;"
"f=getattr(tokenize, 'open', open)(__file__);"
"code=f.read().replace('\\r\\n', '\\n');"
"f.close();"
"exec(compile(code, __file__, 'exec'))"
)


def _is_dist_editable(dist: Distribution) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion pdm/models/caches.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

import pip_shims
from pip._vendor import requests
from vistir.contextmanagers import open_file

from pdm._types import CandidateInfo
from pdm.exceptions import CorruptedCacheError
from vistir.contextmanagers import open_file

if TYPE_CHECKING:
from pdm.models.candidates import Candidate
Expand Down
6 changes: 3 additions & 3 deletions pdm/models/candidates.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import warnings
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Sequence, Union

from pip._vendor.pkg_resources import safe_extra
from pip_shims import shims

from distlib.database import EggInfoDistribution
from distlib.metadata import Metadata
from distlib.wheel import Wheel
from pip._vendor.pkg_resources import safe_extra
from pip_shims import shims

from pdm.context import context
from pdm.exceptions import ExtrasError, RequirementError
from pdm.models.markers import Marker
Expand Down
15 changes: 9 additions & 6 deletions pdm/models/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
from pip._internal.utils import misc
from pip._vendor import packaging, pkg_resources
from pip_shims import shims
from pythonfinder import Finder
from pythonfinder.environment import PYENV_INSTALLED, PYENV_ROOT
from vistir.contextmanagers import temp_environ
from vistir.path import normalize_path

from pdm.context import context
from pdm.exceptions import NoPythonVersion
Expand All @@ -25,9 +29,6 @@
get_pep508_environment,
get_python_version,
)
from pythonfinder import Finder
from vistir.contextmanagers import temp_environ
from vistir.path import normalize_path

if TYPE_CHECKING:
from pdm.models.specifiers import PySpecSet
Expand Down Expand Up @@ -84,13 +85,15 @@ def __init__(self, python_requires: PySpecSet, config: Config) -> None:
@cached_property
def python_executable(self) -> str:
"""Get the Python interpreter path."""
if self.config["python"]:
path = self.config["python"]
if self.config.get("python.path"):
path = self.config["python.path"]
try:
get_python_version(path)
return path
except Exception:
pass
if PYENV_INSTALLED and self.config.get("python.use_pyenv", True):
return os.path.join(PYENV_ROOT, "shims", "python")

# First try what `python` refers to.
path = shutil.which("python")
Expand All @@ -113,7 +116,7 @@ def python_executable(self) -> str:
context.io.green(path), version
)
)
self.config["python"] = Path(path).as_posix()
self.config["python.path"] = Path(path).as_posix()
self.config.save_config()
return path
raise NoPythonVersion(
Expand Down
7 changes: 2 additions & 5 deletions pdm/project/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@

import appdirs
import tomlkit

from pdm.exceptions import NoConfigError


class Config(MutableMapping):
DEFAULT_CONFIG = {
"cache_dir": appdirs.user_cache_dir("pdm"),
"python": None,
"packages_path": None,
}

def __init__(self, project_root: Path):
Expand All @@ -20,7 +19,7 @@ def __init__(self, project_root: Path):
self._dirty = {}

self._project_config_file = self.project_root / ".pdm.toml"
self._global_config_file = Path(appdirs.user_config_dir("pdm")) / ".pdm.toml"
self._global_config_file = Path(appdirs.user_config_dir("pdm")) / "config.toml"
self._project_config = self.load_config(self._project_config_file)
self._global_config = self.load_config(self._global_config_file)
# First load user config, then project config
Expand Down Expand Up @@ -67,8 +66,6 @@ def __getitem__(self, key: str) -> Any:
raise NoConfigError(key) from None

def __setitem__(self, key: str, value: Any) -> None:
if key not in self.DEFAULT_CONFIG:
raise NoConfigError(key)
self._dirty[key] = value
self._data[key] = value

Expand Down
4 changes: 2 additions & 2 deletions pdm/project/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
from pathlib import Path
from typing import TYPE_CHECKING, Any, Dict, Iterable, List, Optional, Union

import tomlkit
from pip._vendor.pkg_resources import safe_name
from vistir.contextmanagers import atomic_open_for_write

import tomlkit
from pdm._types import Source
from pdm.context import context
from pdm.exceptions import ProjectError
Expand All @@ -19,7 +20,6 @@
from pdm.project.config import Config
from pdm.project.meta import PackageMeta
from pdm.utils import find_project_root
from vistir.contextmanagers import atomic_open_for_write

if TYPE_CHECKING:
from tomlkit.container import Container
Expand Down
2 changes: 1 addition & 1 deletion pdm/project/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
from typing import TYPE_CHECKING, Dict, List, Union

import setuptools
import vistir
from pkg_resources import safe_name

import vistir
from pdm.exceptions import ProjectError
from pdm.models.markers import Marker

Expand Down
1 change: 1 addition & 0 deletions pdm/resolver/reporters.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import TYPE_CHECKING, Dict, List, Optional

import halo

from pdm.context import context

if TYPE_CHECKING:
Expand Down
2 changes: 1 addition & 1 deletion pdm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
from pathlib import Path
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple

from distlib.wheel import Wheel
from pip_shims.shims import InstallCommand, PackageFinder, TargetPython

from distlib.wheel import Wheel
from pdm._types import Source

if TYPE_CHECKING:
Expand Down
12 changes: 11 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,17 @@ not_skip = "__init__.py"
skip_glob = ["*/setup.py"]
filter_files = true
known_first_party = "pdm"
known_third_party = "pip_shims"
known_third_party = [
"click",
"halo",
"vistir",
"distlib",
"pythonfinder",
"pytest",
"tomlkit",
"appdirs",
"pip_shims"
]

[tool.black]
line-length = 88
Expand Down
1 change: 1 addition & 0 deletions tests/cli/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import click
import pytest
from distlib.wheel import Wheel

from pdm.cli import actions
from pdm.exceptions import PdmException
from pdm.models.requirements import parse_requirement
Expand Down
1 change: 1 addition & 0 deletions tests/cli/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import pytest
from click.testing import CliRunner

from pdm.cli import actions, commands
from pdm.models.requirements import parse_requirement

Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
from typing import Callable, Iterable, List, Optional, Tuple
from urllib.parse import urlparse

import pytest
from pip._internal.vcs import versioncontrol
from pip._vendor import requests
from pip._vendor.pkg_resources import safe_name

import pytest
from pdm._types import CandidateInfo
from pdm.cli.actions import do_init
from pdm.context import context
Expand Down
1 change: 1 addition & 0 deletions tests/models/test_candidates.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest

from pdm.exceptions import ExtrasError
from pdm.models.candidates import Candidate
from pdm.models.requirements import parse_requirement
Expand Down
1 change: 1 addition & 0 deletions tests/models/test_markers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest

from pdm.models.markers import Marker


Expand Down
1 change: 1 addition & 0 deletions tests/models/test_requirements.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os

import pytest

from pdm.models.requirements import Requirement, RequirementError, parse_requirement
from tests import FIXTURES

Expand Down
1 change: 1 addition & 0 deletions tests/models/test_specifiers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest

from pdm.models.specifiers import PySpecSet


Expand Down
1 change: 1 addition & 0 deletions tests/resolver/test_resolve.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import itertools

import pytest

from pdm.exceptions import NoVersionsAvailable, ResolutionImpossible
from pdm.models.candidates import identify
from pdm.models.requirements import parse_requirement
Expand Down
13 changes: 13 additions & 0 deletions tests/test_project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import os


def test_project_python_with_pyenv_support(project, mocker):
from pythonfinder.environment import PYENV_ROOT

pyenv_python = os.path.join(PYENV_ROOT, "shims", "python")

mocker.patch("pdm.models.environment.PYENV_INSTALLED", True)
assert project.environment.python_executable == pyenv_python

project.config["python.use_pyenv"] = False
assert project.environment.python_executable != pyenv_python