Skip to content

Commit

Permalink
make default formats of include consistent ("sdist" and "wheel") an…
Browse files Browse the repository at this point in the history
…d refactor code for better maintainability
  • Loading branch information
radoering committed Oct 6, 2024
1 parent 73afa9e commit c5841f7
Show file tree
Hide file tree
Showing 35 changed files with 204 additions and 157 deletions.
16 changes: 4 additions & 12 deletions src/poetry/core/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,18 +368,10 @@ def _configure_package_poetry_specifics(
package.build_config = build or {}

if includes := tool_poetry.get("include"):
package.include = []

for include in includes:
if not isinstance(include, dict):
include = {"path": include}

formats = include.get("format", [])
if formats and not isinstance(formats, list):
formats = [formats]
include["format"] = formats

package.include.append(include)
package.include = [
include if isinstance(include, dict) else {"path": include}
for include in includes
]

if exclude := tool_poetry.get("exclude"):
package.exclude = exclude
Expand Down
68 changes: 26 additions & 42 deletions src/poetry/core/masonry/builders/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from functools import cached_property
from pathlib import Path
from typing import TYPE_CHECKING
from typing import Any


if TYPE_CHECKING:
Expand Down Expand Up @@ -52,41 +53,27 @@ def __init__(
def _module(self) -> Module:
from poetry.core.masonry.utils.module import Module

packages = []
for p in self._package.packages:
formats = p.get("format") or None
packages: list[dict[str, Any]] = []
includes: list[dict[str, Any]] = []
for source_list, target_list in [
(self._package.packages, packages),
(self._package.include, includes),
]:
for item in source_list:
# Default to including in both sdist & wheel
# if the `format` key is not provided.
formats = item.get("format", ["sdist", "wheel"])
if not isinstance(formats, list):
formats = [formats]

# Default to including the package in both sdist & wheel
# if the `format` key is not provided in the inline include table.
if formats is None:
formats = ["sdist", "wheel"]

if not isinstance(formats, list):
formats = [formats]

if (
formats
and self.format
and self.format not in formats
and not self._ignore_packages_formats
):
continue

packages.append(p)

includes = []
for include in self._package.include:
formats = include.get("format", [])

if (
formats
and self.format
and self.format not in formats
and not self._ignore_packages_formats
):
continue
if (
self.format
and self.format not in formats
and not self._ignore_packages_formats
):
continue

includes.append(include)
target_list.append({**item, "format": formats})

return Module(
self._package.name,
Expand Down Expand Up @@ -122,15 +109,12 @@ def find_excluded_files(self, fmt: str | None = None) -> set[str]:
)

explicitly_included = set()
for inc in self._package.include:
if fmt and inc["format"] and fmt not in inc["format"]:
for inc in self._module.explicit_includes:
if fmt and fmt not in inc.formats:
continue

included_glob = inc["path"]
for included in self._path.glob(str(included_glob)):
explicitly_included.add(
Path(included).relative_to(self._path).as_posix()
)
for included in inc.elements:
explicitly_included.add(included.relative_to(self._path).as_posix())

ignored = (vcs_ignored_files | explicitly_excluded) - explicitly_included
for ignored_file in ignored:
Expand Down Expand Up @@ -164,7 +148,7 @@ def find_files_to_add(self, exclude_build: bool = True) -> set[BuildIncludeFile]

for include in self._module.includes:
include.refresh()
formats = include.formats or ["sdist"]
formats = include.formats

for file in include.elements:
if "__pycache__" in str(file):
Expand Down Expand Up @@ -201,7 +185,7 @@ def find_files_to_add(self, exclude_build: bool = True) -> set[BuildIncludeFile]
if not (
current_file.is_dir()
or self.is_excluded(
include_file.relative_to_source_root()
include_file.relative_to_project_root()
)
):
to_add.add(include_file)
Expand Down
6 changes: 2 additions & 4 deletions src/poetry/core/masonry/utils/include.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ class Include:
- a directory
"""

def __init__(
self, base: Path, include: str, formats: list[str] | None = None
) -> None:
def __init__(self, base: Path, include: str, formats: list[str]) -> None:
self._base = base
self._include = str(include)
self._formats = formats
Expand All @@ -39,7 +37,7 @@ def elements(self) -> list[Path]:
return self._elements

@property
def formats(self) -> list[str] | None:
def formats(self) -> list[str]:
return self._formats

def is_empty(self) -> bool:
Expand Down
46 changes: 23 additions & 23 deletions src/poetry/core/masonry/utils/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,20 @@ def __init__(
self._in_src = False
self._is_package = False
self._path = Path(directory)
self._includes: list[Include] = []
self._package_includes: list[PackageInclude] = []
self._explicit_includes: list[Include] = []

if not packages:
# It must exist either as a .py file or a directory, but not both
pkg_dir = Path(directory, self._name)
py_file = Path(directory, self._name + ".py")
default_package: dict[str, Any]
if pkg_dir.is_dir() and py_file.is_file():
raise ValueError(f"Both {pkg_dir} and {py_file} exist")
elif pkg_dir.is_dir():
packages = [{"include": str(pkg_dir.relative_to(self._path))}]
default_package = {"include": str(pkg_dir.relative_to(self._path))}
elif py_file.is_file():
packages = [{"include": str(py_file.relative_to(self._path))}]
default_package = {"include": str(py_file.relative_to(self._path))}
else:
# Searching for a src module
src = Path(directory, "src")
Expand All @@ -52,41 +54,35 @@ def __init__(
if src_pkg_dir.is_dir() and src_py_file.is_file():
raise ValueError(f"Both {pkg_dir} and {py_file} exist")
elif src_pkg_dir.is_dir():
packages = [
{
"include": str(src_pkg_dir.relative_to(src)),
"from": str(src.relative_to(self._path)),
}
]
default_package = {
"include": str(src_pkg_dir.relative_to(src)),
"from": str(src.relative_to(self._path)),
}
elif src_py_file.is_file():
packages = [
{
"include": str(src_py_file.relative_to(src)),
"from": str(src.relative_to(self._path)),
}
]
default_package = {
"include": str(src_py_file.relative_to(src)),
"from": str(src.relative_to(self._path)),
}
else:
raise ModuleOrPackageNotFoundError(
f"No file/folder found for package {name}"
)
default_package["format"] = ["sdist", "wheel"]
packages = [default_package]

for package in packages:
formats = package.get("format")
if formats and not isinstance(formats, list):
formats = [formats]

self._includes.append(
self._package_includes.append(
PackageInclude(
self._path,
package["include"],
formats=formats,
formats=package["format"],
source=package.get("from"),
target=package.get("to"),
)
)

for include in includes:
self._includes.append(
self._explicit_includes.append(
Include(self._path, include["path"], formats=include["format"])
)

Expand All @@ -107,7 +103,11 @@ def file(self) -> Path:

@property
def includes(self) -> list[Include]:
return self._includes
return [*self._package_includes, *self._explicit_includes]

@property
def explicit_includes(self) -> list[Include]:
return self._explicit_includes

def is_package(self) -> bool:
return self._is_package
Expand Down
2 changes: 1 addition & 1 deletion src/poetry/core/masonry/utils/package_include.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def __init__(
self,
base: Path,
include: str,
formats: list[str] | None = None,
formats: list[str],
source: str | None = None,
target: str | None = None,
) -> None:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
[tool.poetry]
name = "with-include"
version = "1.2.3"
description = "Some description."
authors = [
"Sébastien Eustace <[email protected]>"
]
license = "MIT"

homepage = "https://python-poetry.org/"
repository = "https://github.com/python-poetry/poetry"
documentation = "https://python-poetry.org/docs"

keywords = ["packaging", "dependency", "poetry"]

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

packages = [
# modules
{ include = "mod_default.py", from = "src" },
{ include = "mod_sdist_only.py", from = "src", format = "sdist" },
{ include = "mod_wheel_only.py", from = "src", format = "wheel" },
{ include = "mod_both.py", from = "src", format = [ "sdist", "wheel" ]},
# packages
{ include = "pkg_default", from = "src" },
{ include = "pkg_sdist_only", from = "src", format = "sdist" },
{ include = "pkg_wheel_only", from = "src", format = "wheel" },
{ include = "pkg_both", from = "src", format = [ "sdist", "wheel" ]},
]

include = [
# files
{ path = "default.txt" },
{ path = "sdist_only.txt", format = "sdist" },
{ path = "wheel_only.txt", format = "wheel" },
{ path = "both.txt", format = [ "sdist", "wheel" ] },
# directories
{ path = "default" },
{ path = "sdist_only", format = "sdist" },
{ path = "wheel_only", format = "wheel" },
{ path = "both", format = [ "sdist", "wheel" ] },
]


# Requirements
[tool.poetry.dependencies]
python = "^3.6"
cleo = "^0.6"
cachy = { version = "^0.2.0", extras = ["msgpack"] }

pendulum = { version = "^1.4", optional = true }

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

[tool.poetry.extras]
time = ["pendulum"]

[tool.poetry.scripts]
my-script = "my_package:main"
my-2nd-script = "my_package:main2"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

This file was deleted.

Loading

0 comments on commit c5841f7

Please sign in to comment.