Skip to content

Commit

Permalink
Merge branch 'master' into script-error-msg
Browse files Browse the repository at this point in the history
  • Loading branch information
Secrus authored Jan 27, 2024
2 parents 63d22db + 90581f5 commit 8ee4f9a
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 61 deletions.
4 changes: 2 additions & 2 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,8 @@ poetry add git+https://github.com/sdispater/pendulum.git#develop
poetry add git+https://github.com/sdispater/pendulum.git#2.0.5

# or using SSH instead:
poetry add git+ssh://github.com/sdispater/pendulum.git#develop
poetry add git+ssh://github.com/sdispater/pendulum.git#2.0.5
poetry add git+ssh://git@github.com:sdispater/pendulum.git#develop
poetry add git+ssh://git@github.com:sdispater/pendulum.git#2.0.5
```

or reference a subdirectory:
Expand Down
30 changes: 26 additions & 4 deletions src/poetry/console/commands/build.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
from __future__ import annotations

from typing import TYPE_CHECKING

from cleo.helpers import option

from poetry.console.commands.env_command import EnvCommand
from poetry.utils.env import build_environment


if TYPE_CHECKING:
from pathlib import Path


class BuildCommand(EnvCommand):
name = "build"
description = "Builds a package, as a tarball and a wheel by default."
Expand All @@ -20,17 +26,33 @@ class BuildCommand(EnvCommand):
"poetry.core.masonry.builders.wheel",
]

def handle(self) -> int:
from poetry.core.masonry.builder import Builder
def _build(
self,
fmt: str,
executable: str | Path | None = None,
*,
target_dir: Path | None = None,
) -> None:
from poetry.masonry.builders import BUILD_FORMATS

if fmt in BUILD_FORMATS:
builders = [BUILD_FORMATS[fmt]]
elif fmt == "all":
builders = list(BUILD_FORMATS.values())
else:
raise ValueError(f"Invalid format: {fmt}")

for builder in builders:
builder(self.poetry, executable=executable).build(target_dir)

def handle(self) -> int:
with build_environment(poetry=self.poetry, env=self.env, io=self.io) as env:
fmt = self.option("format") or "all"
package = self.poetry.package
self.line(
f"Building <c1>{package.pretty_name}</c1> (<c2>{package.version}</c2>)"
)

builder = Builder(self.poetry)
builder.build(fmt, executable=env.python)
self._build(fmt, executable=env.python)

return 0
16 changes: 2 additions & 14 deletions src/poetry/installation/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,15 +696,6 @@ def _install_directory_without_wheel_installer(
package_poetry = Factory().create_poetry(pyproject.file.path.parent)

if package_poetry is not None:
# Even if there is a build system specified
# some versions of pip (< 19.0.0) don't understand it
# so we need to check the version of pip to know
# if we can rely on the build system
legacy_pip = (
self._env.pip_version
< self._env.pip_version.__class__.from_parts(19, 0, 0)
)

builder: Builder
if package.develop and not package_poetry.package.build_script:
from poetry.masonry.builders.editable import EditableBuilder
Expand All @@ -716,13 +707,10 @@ def _install_directory_without_wheel_installer(
builder.build()

return 0
elif legacy_pip or package_poetry.package.build_script:

if package_poetry.package.build_script:
from poetry.core.masonry.builders.sdist import SdistBuilder

# We need to rely on creating a temporary setup.py
# file since the version of pip does not support
# build-systems
# We also need it for non-PEP-517 packages
builder = SdistBuilder(package_poetry)
with builder.setup_py():
return self.pip_install(req, upgrade=True, editable=package.develop)
Expand Down
12 changes: 11 additions & 1 deletion src/poetry/masonry/builders/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
from __future__ import annotations

from poetry.core.masonry.builders.sdist import SdistBuilder
from poetry.core.masonry.builders.wheel import WheelBuilder

from poetry.masonry.builders.editable import EditableBuilder


__all__ = ["EditableBuilder"]
__all__ = ["BUILD_FORMATS", "EditableBuilder"]


# might be extended by plugins
BUILD_FORMATS = {
"sdist": SdistBuilder,
"wheel": WheelBuilder,
}
12 changes: 0 additions & 12 deletions src/poetry/utils/env/base_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

if TYPE_CHECKING:
from packaging.tags import Tag
from poetry.core.constraints.version import Version
from poetry.core.version.markers import BaseMarker
from virtualenv.seed.wheels.util import Wheel

Expand Down Expand Up @@ -54,7 +53,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._pip_version: Version | None = None
self._site_packages: SitePackages | None = None
self._paths: dict[str, str] | None = None
self._supported_tags: list[Tag] | None = None
Expand Down Expand Up @@ -170,13 +168,6 @@ def platform(self) -> str:
def os(self) -> str:
return os.name

@property
def pip_version(self) -> Version:
if self._pip_version is None:
self._pip_version = self.get_pip_version()

return self._pip_version

@property
def site_packages(self) -> SitePackages:
if self._site_packages is None:
Expand Down Expand Up @@ -288,9 +279,6 @@ def get_pip_command(self, embedded: bool = False) -> list[str]:
def get_supported_tags(self) -> list[Tag]:
raise NotImplementedError()

def get_pip_version(self) -> Version:
raise NotImplementedError()

def get_paths(self) -> dict[str, str]:
raise NotImplementedError()

Expand Down
8 changes: 0 additions & 8 deletions src/poetry/utils/env/mock_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
from typing import TYPE_CHECKING
from typing import Any

from poetry.core.constraints.version import Version

from poetry.utils.env.null_env import NullEnv


Expand All @@ -22,7 +20,6 @@ def __init__(
platform_machine: str = "amd64",
os_name: str = "posix",
is_venv: bool = False,
pip_version: str = "19.1",
sys_path: list[str] | None = None,
marker_env: dict[str, Any] | None = None,
supported_tags: list[Tag] | None = None,
Expand All @@ -36,7 +33,6 @@ def __init__(
self._platform_machine = platform_machine
self._os_name = os_name
self._is_venv = is_venv
self._pip_version: Version = Version.parse(pip_version)
self._sys_path = sys_path
self._mock_marker_env = marker_env
self._supported_tags = supported_tags
Expand All @@ -53,10 +49,6 @@ def platform_machine(self) -> str:
def os(self) -> str:
return self._os_name

@property
def pip_version(self) -> Version:
return self._pip_version

@property
def sys_path(self) -> list[str]:
if self._sys_path is None:
Expand Down
6 changes: 0 additions & 6 deletions src/poetry/utils/env/system_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from packaging.tags import interpreter_name
from packaging.tags import interpreter_version
from packaging.tags import sys_tags
from poetry.core.constraints.version import Version

from poetry.utils.env.base_env import Env

Expand Down Expand Up @@ -81,11 +80,6 @@ def get_marker_env(self) -> dict[str, Any]:
"interpreter_version": interpreter_version(),
}

def get_pip_version(self) -> Version:
from pip import __version__

return Version.parse(__version__)

def is_venv(self) -> bool:
return self._path != self._base

Expand Down
11 changes: 0 additions & 11 deletions src/poetry/utils/env/virtual_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from typing import Any

from packaging.tags import Tag
from poetry.core.constraints.version import Version

from poetry.utils.env.base_env import Env
from poetry.utils.env.script_strings import GET_BASE_PREFIX
Expand Down Expand Up @@ -72,16 +71,6 @@ def get_marker_env(self) -> dict[str, Any]:
env: dict[str, Any] = json.loads(output)
return env

def get_pip_version(self) -> Version:
output = self.run_pip("--version")
output = output.strip()

m = re.match("pip (.+?)(?: from .+)?$", output)
if not m:
return Version.parse("0.0")

return Version.parse(m.group(1))

def get_paths(self) -> dict[str, str]:
output = self.run_python_script(GET_PATHS)
paths: dict[str, str] = json.loads(output)
Expand Down
46 changes: 46 additions & 0 deletions tests/console/commands/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,63 @@

from typing import TYPE_CHECKING

import pytest

from poetry.factory import Factory


if TYPE_CHECKING:
from pathlib import Path

from cleo.testers.command_tester import CommandTester

from poetry.poetry import Poetry
from poetry.utils.env import VirtualEnv
from tests.types import CommandTesterFactory
from tests.types import FixtureDirGetter


@pytest.fixture
def tmp_project_path(tmp_path: Path) -> Path:
return tmp_path / "project"


@pytest.fixture
def tmp_poetry(tmp_project_path: Path, fixture_dir: FixtureDirGetter) -> Poetry:
# copy project so that we start with a clean directory
shutil.copytree(fixture_dir("simple_project"), tmp_project_path)
poetry = Factory().create_poetry(tmp_project_path)
return poetry


@pytest.fixture
def tmp_tester(
tmp_poetry: Poetry, command_tester_factory: CommandTesterFactory
) -> CommandTester:
return command_tester_factory("build", tmp_poetry)


def get_package_glob(poetry: Poetry) -> str:
return f"{poetry.package.name.replace('-', '_')}-{poetry.package.version}*"


def test_build_format_is_not_valid(tmp_tester: CommandTester) -> None:
with pytest.raises(ValueError, match=r"Invalid format.*"):
tmp_tester.execute("--format not_valid")


@pytest.mark.parametrize("format", ["sdist", "wheel", "all"])
def test_build_creates_packages_in_dist_directory_if_no_output_is_specified(
tmp_tester: CommandTester, tmp_project_path: Path, tmp_poetry: Poetry, format: str
) -> None:
tmp_tester.execute(f"--format {format}")
build_artifacts = tuple(
(tmp_project_path / "dist").glob(get_package_glob(tmp_poetry))
)
assert len(build_artifacts) > 0
assert all(archive.exists() for archive in build_artifacts)


def test_build_with_multiple_readme_files(
fixture_dir: FixtureDirGetter,
tmp_path: Path,
Expand Down
6 changes: 3 additions & 3 deletions tests/utils/env/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import contextlib
import os
import re
import site
import subprocess
import sys
Expand Down Expand Up @@ -82,9 +83,8 @@ def test_env_commands_with_spaces_in_their_arg_work_as_expected(
venv_path = tmp_path / "Virtual Env"
manager.build_venv(venv_path)
venv = VirtualEnv(venv_path)
assert venv.run("python", str(venv.pip), "--version").startswith(
f"pip {venv.pip_version} from "
)
output = venv.run("python", str(venv.pip), "--version")
assert re.match(r"pip \S+ from", output)


def test_env_get_supported_tags_matches_inside_virtualenv(
Expand Down

0 comments on commit 8ee4f9a

Please sign in to comment.