Skip to content

Commit

Permalink
make default formats of include consistent ("sdist" only) and refac…
Browse files Browse the repository at this point in the history
…tor code for better maintainability (#773)
  • Loading branch information
radoering authored Oct 13, 2024
1 parent 5a2c6a1 commit 1bdbd90
Show file tree
Hide file tree
Showing 39 changed files with 206 additions and 170 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
73 changes: 23 additions & 50 deletions src/poetry/core/masonry/builders/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,7 @@
class Builder:
format: str | None = None

def __init__(
self,
poetry: Poetry,
ignore_packages_formats: bool = False,
executable: Path | None = None,
) -> None:
def __init__(self, poetry: Poetry, executable: Path | None = None) -> None:
from poetry.core.masonry.metadata import Metadata

if not poetry.is_package_mode:
Expand All @@ -43,7 +38,6 @@ def __init__(
self._poetry = poetry
self._package = poetry.package
self._path: Path = poetry.pyproject_path.parent
self._ignore_packages_formats = ignore_packages_formats
self._excluded_files: set[str] | None = None
self._executable = Path(executable or sys.executable)
self._meta = Metadata.from_package(self._package)
Expand All @@ -52,41 +46,23 @@ 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

# 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
packages: list[dict[str, str | dict[str, str]]] = []
includes: list[dict[str, str | dict[str, str]]] = []
for source_list, target_list, default in [
(self._package.packages, packages, ["sdist", "wheel"]),
(self._package.include, includes, ["sdist"]),
]:
for item in source_list:
# Default to including in both sdist & wheel
# if the `format` key is not provided.
formats = item.get("format", default)
if not isinstance(formats, list):
formats = [formats]

if self.format and self.format not in formats:
continue

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

return Module(
self._package.name,
Expand Down Expand Up @@ -122,15 +98,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 +137,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 +174,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
Expand Up @@ -10,7 +10,7 @@ license = "MIT"
readme = "README.rst"

exclude = ["**/data/", "**/*/item*"]
include = ["my_package/data/data2.txt"]
include = [{path = "my_package/data/data2.txt", format = ["sdist", "wheel"]}]

homepage = "https://python-poetry.org/"
repository = "https://github.com/python-poetry/poetry"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ packages = [{include='my_package', from='lib'}]
# Simulate excluding due to .gitignore
exclude = ['lib/my_package/generated.py']
# Include again
include = ['lib/my_package/generated.py']
include = [{ path = 'lib/my_package/generated.py', format = ["sdist", "wheel"] }]

[tool.poetry.dependencies]
python = "^3.8"
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 @@

4 changes: 2 additions & 2 deletions tests/masonry/builders/fixtures/with-include/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ packages = [
]

include = [
"extra_dir/vcs_excluded.py",
"notes.txt"
{ path = "extra_dir/vcs_excluded.py", format = ["sdist", "wheel"] },
"notes.txt", # default is sdist only
]


Expand Down
Loading

0 comments on commit 1bdbd90

Please sign in to comment.