Skip to content

Commit

Permalink
add support for PEP 621: fix build issues, especially handling of scr…
Browse files Browse the repository at this point in the history
…ipts, ignore extras in entry points (python-poetry#708)
  • Loading branch information
radoering committed Sep 1, 2024
1 parent bedf300 commit 3c2d8d7
Show file tree
Hide file tree
Showing 26 changed files with 219 additions and 108 deletions.
43 changes: 43 additions & 0 deletions src/poetry/core/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import logging

from collections import defaultdict
from collections.abc import Mapping
from pathlib import Path
from typing import TYPE_CHECKING
Expand Down Expand Up @@ -133,6 +134,7 @@ def configure_package(
package.root_dir = root

cls._configure_package_metadata(package, project, tool_poetry, root)
cls._configure_entry_points(package, project, tool_poetry)
cls._configure_package_dependencies(
package, project, tool_poetry, with_groups=with_groups
)
Expand Down Expand Up @@ -218,6 +220,47 @@ def _configure_package_metadata(
else:
package.readmes = tuple(root / readme for readme in custom_readme)

@classmethod
def _configure_entry_points(
cls,
package: ProjectPackage,
project: dict[str, Any],
tool_poetry: dict[str, Any],
) -> None:
entry_points: defaultdict[str, dict[str, str]] = defaultdict(dict)

if scripts := project.get("scripts"):
entry_points["console-scripts"] = scripts
elif scripts := tool_poetry.get("scripts"):
for name, specification in scripts.items():
if isinstance(specification, str):
specification = {"reference": specification, "type": "console"}

if specification.get("type") != "console":
continue

reference = specification.get("reference")

if reference:
entry_points["console-scripts"][name] = reference

if scripts := project.get("gui-scripts"):
entry_points["gui-scripts"] = scripts

if other_scripts := project.get("entry-points"):
for group_name, scripts in sorted(other_scripts.items()):
if group_name in {"console-scripts", "gui-scripts"}:
raise ValueError(
f"Group '{group_name}' is reserved and cannot be used"
" as a custom entry-point group."
)
entry_points[group_name] = scripts
elif other_scripts := tool_poetry.get("plugins"):
for group_name, scripts in sorted(other_scripts.items()):
entry_points[group_name] = scripts

package.entry_points = dict(entry_points)

@classmethod
def _configure_package_dependencies(
cls,
Expand Down
50 changes: 11 additions & 39 deletions src/poetry/core/masonry/builders/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import logging
import sys
import warnings

from collections import defaultdict
from functools import cached_property
from pathlib import Path
from typing import TYPE_CHECKING
Expand Down Expand Up @@ -293,44 +291,18 @@ def get_metadata_content(self) -> str:
return content

def convert_entry_points(self) -> dict[str, list[str]]:
result = defaultdict(list)

# Scripts -> Entry points
for name, specification in self._poetry.local_config.get("scripts", {}).items():
if isinstance(specification, str):
# TODO: deprecate this in favour or reference
specification = {"reference": specification, "type": "console"}

if specification.get("type") != "console":
continue

extras = specification.get("extras", [])
if extras:
warnings.warn(
f'The script "{name}" depends on an extra. Scripts depending on'
" extras are deprecated and support for them will be removed in a"
" future version of poetry/poetry-core. See"
" https://packaging.python.org/en/latest/specifications/entry-points/#data-model"
" for details.",
DeprecationWarning,
stacklevel=1,
)
extras = f"[{', '.join(extras)}]" if extras else ""
reference = specification.get("reference")

if reference:
result["console_scripts"].append(f"{name} = {reference}{extras}")

# Plugins -> entry points
plugins = self._poetry.local_config.get("plugins", {})
for groupname, group in plugins.items():
for name, specification in sorted(group.items()):
result[groupname].append(f"{name} = {specification}")

for groupname in result:
result[groupname] = sorted(result[groupname])
result: dict[str, list[str]] = {}

for group_name, group in self._poetry.package.entry_points.items():
if group_name == "console-scripts":
group_name = "console_scripts"
elif group_name == "gui-scripts":
group_name = "gui_scripts"
result[group_name] = sorted(
f"{name} = {specification}" for name, specification in group.items()
)

return dict(result)
return result

def convert_script_files(self) -> list[Path]:
script_files: list[Path] = []
Expand Down
8 changes: 1 addition & 7 deletions src/poetry/core/masonry/builders/sdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@


if TYPE_CHECKING:
from collections.abc import Iterable
from collections.abc import Iterator
from tarfile import TarInfo

Expand Down Expand Up @@ -331,12 +330,7 @@ def find_files_to_add(self, exclude_build: bool = False) -> set[BuildIncludeFile
additional_files.add(Path("pyproject.toml"))

# add readme files if specified
if "readme" in self._poetry.local_config:
readme: str | Iterable[str] = self._poetry.local_config["readme"]
if isinstance(readme, str):
additional_files.add(Path(readme))
else:
additional_files.update(Path(r) for r in readme)
additional_files.update(Path(r) for r in self._poetry.package.readmes)

for additional_file in additional_files:
file = BuildIncludeFile(
Expand Down
5 changes: 1 addition & 4 deletions src/poetry/core/masonry/builders/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,7 @@ def prepare_metadata(self, metadata_directory: Path) -> Path:
dist_info = metadata_directory / self.dist_info
dist_info.mkdir(parents=True, exist_ok=True)

if (
"scripts" in self._poetry.local_config
or "plugins" in self._poetry.local_config
):
if self._poetry.package.entry_points:
with (dist_info / "entry_points.txt").open(
"w", encoding="utf-8", newline="\n"
) as f:
Expand Down
4 changes: 4 additions & 0 deletions src/poetry/core/masonry/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ def from_package(cls, package: ProjectPackage) -> Metadata:
for name, url in package.urls.items():
if name.lower() == "homepage" and meta.home_page == url:
continue
if name == "repository" and url == package.urls["Repository"]:
continue
if name == "documentation" and url == package.urls["Documentation"]:
continue

meta.project_urls += (f"{name}, {url}",)

Expand Down
2 changes: 2 additions & 0 deletions src/poetry/core/packages/project_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ def __init__(
self._requires_python: str = "*"
self.dynamic_classifiers = True

self.entry_points: Mapping[str, dict[str, str]] = {}

if self._python_versions == "*":
self._python_constraint = parse_constraint("~2.7 || >=3.4")

Expand Down
2 changes: 2 additions & 0 deletions tests/masonry/builders/fixtures/complete/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ my-2nd-script = "my_package:main2"
file-script = { reference = "bin/script.sh", type = "file" }
extra-script = { reference = "my_package.extra:main", extras = ["time"], type = "console" }

[tool.poetry.plugins."poetry.application.plugin"]
my-command = "my_package.plugins:MyApplicationPlugin"

[tool.poetry.urls]
"Issue Tracker" = "https://github.com/python-poetry/poetry/issues"
Empty file.
Empty file.
Empty file.
20 changes: 20 additions & 0 deletions tests/masonry/builders/fixtures/complete_new/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2018 Sébastien Eustace

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2 changes: 2 additions & 0 deletions tests/masonry/builders/fixtures/complete_new/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
My Package
==========
3 changes: 3 additions & 0 deletions tests/masonry/builders/fixtures/complete_new/bin/script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

echo "Hello World!"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = "1.2.3"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Empty file.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Empty file.
53 changes: 53 additions & 0 deletions tests/masonry/builders/fixtures/complete_new/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
[project]
name = "my-package"
version = "1.2.3"
description = "Some description."
readme = "README.rst"
requires-python = ">=3.6,<4.0"
license = { "text" = "MIT" }
authors = [
{ "name" = "Sébastien Eustace", "email" = "[email protected]" }
]
maintainers = [
{ name = "People Everywhere", email = "[email protected]" }
]
keywords = ["packaging", "dependency", "poetry"]
dependencies = [
"cleo>=0.6,<0.7",
"cachy[msgpack]>=0.2.0,<0.3.0",
]
dynamic = [ "classifiers" ]

[project.optional-dependencies]
time = [ "pendulum>=1.4,<2.0 ; python_version ~= '2.7' and sys_platform == 'win32' or python_version in '3.4 3.5'" ]

[project.urls]
homepage = "https://python-poetry.org/"
repository = "https://github.com/python-poetry/poetry"
documentation = "https://python-poetry.org/docs"
"Issue Tracker" = "https://github.com/python-poetry/poetry/issues"

[project.scripts]
my-script = "my_package:main"
my-2nd-script = "my_package:main2"
extra-script = "my_package.extra:main"

[project.entry-points."poetry.application.plugin"]
my-command = "my_package.plugins:MyApplicationPlugin"

[tool.poetry]
classifiers = [
"Topic :: Software Development :: Build Tools",
"Topic :: Software Development :: Libraries :: Python Modules"
]

exclude = [
"does-not-exist",
"**/*.xml"
]

[tool.poetry.dev-dependencies]
pytest = "~3.4"

[tool.poetry.scripts]
file-script = { reference = "bin/script.sh", type = "file" }
3 changes: 1 addition & 2 deletions tests/masonry/builders/test_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def test_invalid_script_files_definition() -> None:
"script_reference_console",
{
"console_scripts": [
"extra-script = my_package.extra:main[time]",
"extra-script = my_package.extra:main",
"script = my_package.extra:main",
]
},
Expand All @@ -240,7 +240,6 @@ def test_invalid_script_files_definition() -> None:
),
],
)
@pytest.mark.filterwarnings("ignore:.* script .* extra:DeprecationWarning")
def test_builder_convert_entry_points(
fixture: str, result: dict[str, list[str]]
) -> None:
Expand Down
17 changes: 10 additions & 7 deletions tests/masonry/builders/test_complete.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,20 @@ def test_wheel_c_extension(project: str, exptected_c_dir: str) -> None:
assert len(set(record_files)) == len(record_files)


@pytest.mark.parametrize("project", ["complete", "complete_new"])
@pytest.mark.parametrize("no_vcs", [False, True])
def test_complete(no_vcs: bool) -> None:
module_path = fixtures_dir / "complete"
def test_complete(project: str, no_vcs: bool) -> None:
module_path = fixtures_dir / project

if no_vcs:
# Copy the complete fixtures dir to a temporary directory
temporary_dir = Path(tempfile.mkdtemp()) / "complete"
temporary_dir = Path(tempfile.mkdtemp()) / project
shutil.copytree(module_path.as_posix(), temporary_dir.as_posix())
module_path = temporary_dir

poetry = Factory().create_poetry(module_path)
with pytest.warns(DeprecationWarning, match=".* script .* extra"):
SdistBuilder(poetry).build()
WheelBuilder(poetry).build()
SdistBuilder(poetry).build()
WheelBuilder(poetry).build()

whl = module_path / "dist" / "my_package-1.2.3-py3-none-any.whl"

Expand Down Expand Up @@ -159,10 +159,13 @@ def test_complete(no_vcs: bool) -> None:
entry_points.decode()
== """\
[console_scripts]
extra-script=my_package.extra:main[time]
extra-script=my_package.extra:main
my-2nd-script=my_package:main2
my-script=my_package:main
[poetry.application.plugin]
my-command=my_package.plugins:MyApplicationPlugin
"""
)
wheel_data = zipf.read("my_package-1.2.3.dist-info/WHEEL").decode()
Expand Down
Loading

0 comments on commit 3c2d8d7

Please sign in to comment.