Skip to content

Commit

Permalink
Move to native tags gathering (#9304)
Browse files Browse the repository at this point in the history
  • Loading branch information
Secrus authored Apr 17, 2024
1 parent a22bd5d commit 93ef49f
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 61 deletions.
8 changes: 4 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ importlib-metadata = { version = ">=4.4", python = "<3.10" }
installer = "^0.7.0"
keyring = "^24.3.1"
# packaging uses calver, so version is unclamped
packaging = ">=23.1"
packaging = ">=24.0"
pexpect = "^4.7.0"
pkginfo = "^1.10"
platformdirs = ">=3.0.0,<5"
Expand Down
4 changes: 0 additions & 4 deletions src/poetry/utils/env/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@
from poetry.utils.env.script_strings import GET_ENVIRONMENT_INFO
from poetry.utils.env.script_strings import GET_PATHS

This comment has been minimized.

Copy link
@WahidKhan78

WahidKhan78 Apr 19, 2024

Hello

from poetry.utils.env.script_strings import GET_PATHS_FOR_GENERIC_ENVS
from poetry.utils.env.script_strings import GET_PYTHON_VERSION
from poetry.utils.env.script_strings import GET_PYTHON_VERSION_ONELINER
from poetry.utils.env.script_strings import GET_SYS_PATH
from poetry.utils.env.script_strings import GET_SYS_TAGS
from poetry.utils.env.site_packages import SitePackages
from poetry.utils.env.system_env import SystemEnv
from poetry.utils.env.virtual_env import VirtualEnv
Expand Down Expand Up @@ -96,9 +94,7 @@ def build_environment(
"GET_BASE_PREFIX",
"GET_ENVIRONMENT_INFO",
"GET_PATHS",
"GET_PYTHON_VERSION",
"GET_SYS_PATH",
"GET_SYS_TAGS",
"GET_ENV_PATH_ONELINER",
"GET_PYTHON_VERSION_ONELINER",
"GET_PATHS_FOR_GENERIC_ENVS",
Expand Down
17 changes: 9 additions & 8 deletions src/poetry/utils/env/base_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import sys
import sysconfig

from functools import cached_property
from pathlib import Path
from subprocess import CalledProcessError
from typing import TYPE_CHECKING
Expand All @@ -20,12 +21,16 @@


if TYPE_CHECKING:
from typing import Tuple

from packaging.tags import Tag
from poetry.core.version.markers import BaseMarker
from virtualenv.seed.wheels.util import Wheel

from poetry.utils.env.generic_env import GenericEnv

PythonVersion = Tuple[int, int, int, str, int]


class Env:
"""
Expand All @@ -52,7 +57,6 @@ def __init__(self, path: Path, base: Path | None = None) -> None:

self._base = base or path

self._marker_env: dict[str, Any] | None = None
self._site_packages: SitePackages | None = None
self._paths: dict[str, str] | None = None
self._supported_tags: list[Tag] | None = None
Expand All @@ -71,8 +75,8 @@ def base(self) -> Path:
return self._base

@property
def version_info(self) -> tuple[int, int, int, str, int]:
version_info: tuple[int, int, int, str, int] = self.marker_env["version_info"]
def version_info(self) -> PythonVersion:
version_info: PythonVersion = self.marker_env["version_info"]
return version_info

@property
Expand All @@ -87,12 +91,9 @@ def python(self) -> Path:
"""
return Path(self._bin(self._executable))

@property
@cached_property
def marker_env(self) -> dict[str, Any]:
if self._marker_env is None:
self._marker_env = self.get_marker_env()

return self._marker_env
return self.get_marker_env()

@property
def parent_env(self) -> GenericEnv:
Expand Down
40 changes: 2 additions & 38 deletions src/poetry/utils/env/script_strings.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,5 @@
from __future__ import annotations

import packaging.tags


GET_SYS_TAGS = f"""
import importlib.util
import json
import sys
from pathlib import Path
spec = importlib.util.spec_from_file_location(
"packaging", Path(r"{packaging.__file__}")
)
packaging = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = packaging
spec = importlib.util.spec_from_file_location(
"packaging.tags", Path(r"{packaging.tags.__file__}")
)
packaging_tags = importlib.util.module_from_spec(spec)
spec.loader.exec_module(packaging_tags)
print(
json.dumps([(t.interpreter, t.abi, t.platform) for t in packaging_tags.sys_tags()])
)
"""

GET_ENVIRONMENT_INFO = """\
import json
Expand Down Expand Up @@ -54,12 +28,7 @@ def interpreter_version():
def _version_nodot(version):
if any(v >= 10 for v in version):
sep = "_"
else:
sep = ""
return sep.join(map(str, version))
return "".join(map(str, version))
if hasattr(sys, "implementation"):
Expand Down Expand Up @@ -108,15 +77,10 @@ def _version_nodot(version):
print(sys.prefix)
"""

GET_PYTHON_VERSION = """\
import sys
print('.'.join([str(s) for s in sys.version_info[:3]]))
"""

GET_PYTHON_VERSION_ONELINER = (
"import sys; print('.'.join([str(s) for s in sys.version_info[:3]]))"
)

GET_ENV_PATH_ONELINER = "import sys; print(sys.prefix)"

GET_SYS_PATH = """\
Expand Down
25 changes: 20 additions & 5 deletions src/poetry/utils/env/virtual_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,19 @@
from typing import TYPE_CHECKING
from typing import Any

from packaging.tags import Tag

from poetry.utils.env.base_env import Env
from poetry.utils.env.script_strings import GET_BASE_PREFIX
from poetry.utils.env.script_strings import GET_ENVIRONMENT_INFO
from poetry.utils.env.script_strings import GET_PATHS
from poetry.utils.env.script_strings import GET_SYS_PATH
from poetry.utils.env.script_strings import GET_SYS_TAGS
from poetry.utils.env.system_env import SystemEnv


if TYPE_CHECKING:
from collections.abc import Iterator

from packaging.tags import Tag


class VirtualEnv(Env):
"""
Expand All @@ -50,9 +49,25 @@ def sys_path(self) -> list[str]:
return paths

def get_supported_tags(self) -> list[Tag]:
output = self.run_python_script(GET_SYS_TAGS)
from packaging.tags import compatible_tags
from packaging.tags import cpython_tags
from packaging.tags import generic_tags

python = self.version_info[:3]
interpreter_name = self.marker_env["interpreter_name"]
interpreter_version = self.marker_env["interpreter_version"]

if interpreter_name == "pp":
interpreter = "pp3"
elif interpreter_name == "cp":
interpreter = f"{interpreter_name}{interpreter_version}"
else:
interpreter = None

return [Tag(*t) for t in json.loads(output)]
return [
*(cpython_tags(python) if interpreter_name == "cp" else generic_tags()),
*compatible_tags(python, interpreter=interpreter),
]

def get_marker_env(self) -> dict[str, Any]:
output = self.run_python_script(GET_ENVIRONMENT_INFO)
Expand Down
2 changes: 1 addition & 1 deletion tests/inspection/test_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ def test_info_no_setup_pkg_info_no_deps_dynamic(fixture_dir: FixtureDirGetter) -
def test_info_setup_simple(mocker: MockerFixture, demo_setup: Path) -> None:
spy = mocker.spy(VirtualEnv, "run")
info = PackageInfo.from_directory(demo_setup)
assert spy.call_count == 5
assert spy.call_count == 4
demo_check_info(info, requires_dist={"package"})


Expand Down

0 comments on commit 93ef49f

Please sign in to comment.