From a90db9f71fc9666871d5869fd3f94da7ec403b36 Mon Sep 17 00:00:00 2001 From: bigstones Date: Mon, 11 Dec 2023 00:08:56 +0900 Subject: [PATCH 1/8] tests: add from to testcase --- .../builders/fixtures/with-include/etc/from_to/__init__.py | 0 tests/masonry/builders/fixtures/with-include/pyproject.toml | 1 + tests/masonry/builders/test_complete.py | 2 ++ tests/test_factory.py | 1 + 4 files changed, 4 insertions(+) create mode 100644 tests/masonry/builders/fixtures/with-include/etc/from_to/__init__.py diff --git a/tests/masonry/builders/fixtures/with-include/etc/from_to/__init__.py b/tests/masonry/builders/fixtures/with-include/etc/from_to/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/masonry/builders/fixtures/with-include/pyproject.toml b/tests/masonry/builders/fixtures/with-include/pyproject.toml index 07f5f4cf2..8274092d8 100644 --- a/tests/masonry/builders/fixtures/with-include/pyproject.toml +++ b/tests/masonry/builders/fixtures/with-include/pyproject.toml @@ -28,6 +28,7 @@ packages = [ { include = "tests", format = "sdist" }, { include = "for_wheel_only", format = ["wheel"] }, { include = "src_package", from = "src"}, + { include = "from_to", from = "etc", "to" = "target"}, ] include = [ diff --git a/tests/masonry/builders/test_complete.py b/tests/masonry/builders/test_complete.py index 44cbfbd72..4d805547c 100644 --- a/tests/masonry/builders/test_complete.py +++ b/tests/masonry/builders/test_complete.py @@ -321,6 +321,7 @@ def test_package_with_include(mocker: MockerFixture) -> None: assert "with_include-1.2.3/PKG-INFO" in names assert "with_include-1.2.3/for_wheel_only/__init__.py" not in names assert "with_include-1.2.3/src/src_package/__init__.py" in names + assert "with_include-1.2.3/etc/from_to/__init__.py" in names whl = module_path / "dist" / "with_include-1.2.3-py3-none-any.whl" @@ -340,6 +341,7 @@ def test_package_with_include(mocker: MockerFixture) -> None: assert "package_with_include/__init__.py" in names assert "tests/__init__.py" not in names assert "src_package/__init__.py" in names + assert "target/from_to/__init__.py" in names def test_respect_format_for_explicit_included_files() -> None: diff --git a/tests/test_factory.py b/tests/test_factory.py index 77f10f858..ffe259e91 100644 --- a/tests/test_factory.py +++ b/tests/test_factory.py @@ -217,6 +217,7 @@ def test_create_poetry_with_packages_and_includes() -> None: {"include": "tests", "format": "sdist"}, {"include": "for_wheel_only", "format": ["wheel"]}, {"include": "src_package", "from": "src"}, + {"include": "from_to", "from": "etc", "to": "target"}, ] assert package.include == [ From 18391142932be9191a0dcec729b5eeb84229b752 Mon Sep 17 00:00:00 2001 From: bigstones Date: Mon, 11 Dec 2023 00:10:20 +0900 Subject: [PATCH 2/8] schema: add project property "to" --- src/poetry/core/json/schemas/poetry-schema.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/poetry/core/json/schemas/poetry-schema.json b/src/poetry/core/json/schemas/poetry-schema.json index 01d912ce4..59ba40e13 100644 --- a/src/poetry/core/json/schemas/poetry-schema.json +++ b/src/poetry/core/json/schemas/poetry-schema.json @@ -94,6 +94,10 @@ }, "format": { "$ref": "#/definitions/package-formats" + }, + "to": { + "type": "string", + "description": "Where the package should be installed in the final distribution." } } } From 09aea5fd6ebde1925e4012c894ca7050e497f1bd Mon Sep 17 00:00:00 2001 From: bigstones Date: Fri, 15 Dec 2023 03:17:08 +0900 Subject: [PATCH 3/8] Add 'from-to' feature tool.poetry.packages --- src/poetry/core/masonry/builders/builder.py | 27 ++++++++++++++++++- src/poetry/core/masonry/builders/wheel.py | 2 +- src/poetry/core/masonry/utils/module.py | 1 + .../core/masonry/utils/package_include.py | 6 +++++ 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/poetry/core/masonry/builders/builder.py b/src/poetry/core/masonry/builders/builder.py index 118c2ecc8..f5d2f742c 100644 --- a/src/poetry/core/masonry/builders/builder.py +++ b/src/poetry/core/masonry/builders/builder.py @@ -180,6 +180,15 @@ def find_files_to_add(self, exclude_build: bool = True) -> set[BuildIncludeFile] else: source_root = self._path + if ( + isinstance(include, PackageInclude) + and include.target + and self.format == "wheel" + ): + target_dir = include.target + else: + target_dir = None + if file.is_dir(): if self.format in formats: for current_file in file.glob("**/*"): @@ -187,6 +196,7 @@ def find_files_to_add(self, exclude_build: bool = True) -> set[BuildIncludeFile] path=current_file, project_root=self._path, source_root=source_root, + target_dir=target_dir, ) if not ( @@ -199,7 +209,10 @@ def find_files_to_add(self, exclude_build: bool = True) -> set[BuildIncludeFile] continue include_file = BuildIncludeFile( - path=file, project_root=self._path, source_root=source_root + path=file, + project_root=self._path, + source_root=source_root, + target_dir=target_dir, ) if self.is_excluded( @@ -220,6 +233,7 @@ def find_files_to_add(self, exclude_build: bool = True) -> set[BuildIncludeFile] path=self._package.build_script, project_root=self._path, source_root=self._path, + target_dir=self._path, ) ) @@ -368,6 +382,7 @@ def __init__( path: Path | str, project_root: Path | str, source_root: Path | str | None = None, + target_dir: Path | str | None = None, ) -> None: """ :param project_root: the full path of the project's root @@ -377,6 +392,7 @@ def __init__( self.path = Path(path) self.project_root = Path(project_root).resolve() self.source_root = None if not source_root else Path(source_root).resolve() + self.target_dir = None if not target_dir else Path(target_dir) if not self.path.is_absolute() and self.source_root: self.path = self.source_root / self.path else: @@ -404,3 +420,12 @@ def relative_to_source_root(self) -> Path: return self.path.relative_to(self.source_root) return self.path + + def concatenation_target_dir_relative_to_source_root(self) -> Path: + if self.source_root is not None and self.target_dir is not None: + return self.target_dir / self.path.relative_to(self.source_root) + + elif self.source_root is not None: + return self.path.relative_to(self.source_root) + + return self.path diff --git a/src/poetry/core/masonry/builders/wheel.py b/src/poetry/core/masonry/builders/wheel.py index ab8bd5f92..64b29f5a1 100644 --- a/src/poetry/core/masonry/builders/wheel.py +++ b/src/poetry/core/masonry/builders/wheel.py @@ -267,7 +267,7 @@ def _copy_module(self, wheel: zipfile.ZipFile) -> None: # Walk the files and compress them, # sorting everything so the order is stable. for file in sorted(to_add, key=lambda x: x.path): - self._add_file(wheel, file.path, file.relative_to_source_root()) + self._add_file(wheel, file.path, file.concatenation_target_dir_relative_to_source_root()) def prepare_metadata(self, metadata_directory: Path) -> Path: dist_info = metadata_directory / self.dist_info diff --git a/src/poetry/core/masonry/utils/module.py b/src/poetry/core/masonry/utils/module.py index c97aefda6..f58a95a4b 100644 --- a/src/poetry/core/masonry/utils/module.py +++ b/src/poetry/core/masonry/utils/module.py @@ -81,6 +81,7 @@ def __init__( package["include"], formats=formats, source=package.get("from"), + target=package.get("to"), ) ) diff --git a/src/poetry/core/masonry/utils/package_include.py b/src/poetry/core/masonry/utils/package_include.py index 283c0d519..5fa659aee 100644 --- a/src/poetry/core/masonry/utils/package_include.py +++ b/src/poetry/core/masonry/utils/package_include.py @@ -16,11 +16,13 @@ def __init__( include: str, formats: list[str] | None = None, source: str | None = None, + target: str | None = None, ) -> None: self._package: str self._is_package = False self._is_module = False self._source = source + self._target = target if source is not None: base = base / source @@ -36,6 +38,10 @@ def package(self) -> str: def source(self) -> str | None: return self._source + @property + def target(self) -> str | None: + return self._target + def is_package(self) -> bool: return self._is_package From 8be36c56923200adcfb40d0b5e6034f70802e703 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 14 Dec 2023 18:18:24 +0000 Subject: [PATCH 4/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/poetry/core/masonry/builders/wheel.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/poetry/core/masonry/builders/wheel.py b/src/poetry/core/masonry/builders/wheel.py index 64b29f5a1..4a6922550 100644 --- a/src/poetry/core/masonry/builders/wheel.py +++ b/src/poetry/core/masonry/builders/wheel.py @@ -267,7 +267,11 @@ def _copy_module(self, wheel: zipfile.ZipFile) -> None: # Walk the files and compress them, # sorting everything so the order is stable. for file in sorted(to_add, key=lambda x: x.path): - self._add_file(wheel, file.path, file.concatenation_target_dir_relative_to_source_root()) + self._add_file( + wheel, + file.path, + file.concatenation_target_dir_relative_to_source_root(), + ) def prepare_metadata(self, metadata_directory: Path) -> Path: dist_info = metadata_directory / self.dist_info From a845d025982e4d7a704c7f90f2685db9cf60863d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=8A=A4=EA=B8=B0?= <71265839+bigstones@users.noreply.github.com> Date: Sat, 16 Dec 2023 21:16:23 +0900 Subject: [PATCH 5/8] Update src/poetry/core/masonry/builders/builder.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Randy Döring <30527984+radoering@users.noreply.github.com> --- src/poetry/core/masonry/builders/builder.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/poetry/core/masonry/builders/builder.py b/src/poetry/core/masonry/builders/builder.py index f5d2f742c..5147a2836 100644 --- a/src/poetry/core/masonry/builders/builder.py +++ b/src/poetry/core/masonry/builders/builder.py @@ -422,10 +422,7 @@ def relative_to_source_root(self) -> Path: return self.path def concatenation_target_dir_relative_to_source_root(self) -> Path: + path = self.relative_to_source_root() if self.source_root is not None and self.target_dir is not None: - return self.target_dir / self.path.relative_to(self.source_root) - - elif self.source_root is not None: - return self.path.relative_to(self.source_root) - - return self.path + return self.target_dir / path + return path From e71263f9628e749c9cde3d77dbf6222977d2e4e2 Mon Sep 17 00:00:00 2001 From: bigstones Date: Sun, 17 Dec 2023 10:49:42 +0900 Subject: [PATCH 6/8] tests: Add&Update from to module test case --- tests/masonry/builders/fixtures/with-include/my_module_to.py | 0 tests/masonry/builders/fixtures/with-include/pyproject.toml | 3 ++- tests/masonry/builders/test_complete.py | 3 ++- tests/test_factory.py | 3 ++- 4 files changed, 6 insertions(+), 3 deletions(-) create mode 100644 tests/masonry/builders/fixtures/with-include/my_module_to.py diff --git a/tests/masonry/builders/fixtures/with-include/my_module_to.py b/tests/masonry/builders/fixtures/with-include/my_module_to.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/masonry/builders/fixtures/with-include/pyproject.toml b/tests/masonry/builders/fixtures/with-include/pyproject.toml index 8274092d8..ea2a19430 100644 --- a/tests/masonry/builders/fixtures/with-include/pyproject.toml +++ b/tests/masonry/builders/fixtures/with-include/pyproject.toml @@ -28,7 +28,8 @@ packages = [ { include = "tests", format = "sdist" }, { include = "for_wheel_only", format = ["wheel"] }, { include = "src_package", from = "src"}, - { include = "from_to", from = "etc", "to" = "target"}, + { include = "from_to", from = "etc", "to" = "target_from_to"}, + { include = "my_module_to.py", "to" = "target_module"}, ] include = [ diff --git a/tests/masonry/builders/test_complete.py b/tests/masonry/builders/test_complete.py index 4d805547c..6581dcbb6 100644 --- a/tests/masonry/builders/test_complete.py +++ b/tests/masonry/builders/test_complete.py @@ -341,7 +341,8 @@ def test_package_with_include(mocker: MockerFixture) -> None: assert "package_with_include/__init__.py" in names assert "tests/__init__.py" not in names assert "src_package/__init__.py" in names - assert "target/from_to/__init__.py" in names + assert "target_from_to/from_to/__init__.py" in names + assert "target_module/my_module_to.py" in names def test_respect_format_for_explicit_included_files() -> None: diff --git a/tests/test_factory.py b/tests/test_factory.py index ffe259e91..e3303dc3c 100644 --- a/tests/test_factory.py +++ b/tests/test_factory.py @@ -217,7 +217,8 @@ def test_create_poetry_with_packages_and_includes() -> None: {"include": "tests", "format": "sdist"}, {"include": "for_wheel_only", "format": ["wheel"]}, {"include": "src_package", "from": "src"}, - {"include": "from_to", "from": "etc", "to": "target"}, + {"include": "from_to", "from": "etc", "to": "target_from_to"}, + {"include": "my_module_to.py", "to": "target_module"}, ] assert package.include == [ From 1ea985150bcba9341e671532660b0d7190728382 Mon Sep 17 00:00:00 2001 From: bigstones Date: Sun, 17 Dec 2023 10:52:20 +0900 Subject: [PATCH 7/8] =?UTF-8?q?=F0=9F=8E=A8=20Update:=20Refactor=20code=20?= =?UTF-8?q?based=20on=20feedback?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/poetry/core/masonry/builders/builder.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/poetry/core/masonry/builders/builder.py b/src/poetry/core/masonry/builders/builder.py index 5147a2836..67ea1aca6 100644 --- a/src/poetry/core/masonry/builders/builder.py +++ b/src/poetry/core/masonry/builders/builder.py @@ -233,7 +233,6 @@ def find_files_to_add(self, exclude_build: bool = True) -> set[BuildIncludeFile] path=self._package.build_script, project_root=self._path, source_root=self._path, - target_dir=self._path, ) ) From b297673582f61d25b9215a1c5eac3e23bce17a49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Randy=20D=C3=B6ring?= <30527984+radoering@users.noreply.github.com> Date: Sun, 17 Dec 2023 16:59:30 +0100 Subject: [PATCH 8/8] rename method and make source_root mandatory --- src/poetry/core/masonry/builders/builder.py | 18 ++++++++---------- src/poetry/core/masonry/builders/wheel.py | 6 +----- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/poetry/core/masonry/builders/builder.py b/src/poetry/core/masonry/builders/builder.py index 67ea1aca6..1492d21cb 100644 --- a/src/poetry/core/masonry/builders/builder.py +++ b/src/poetry/core/masonry/builders/builder.py @@ -380,19 +380,20 @@ def __init__( self, path: Path | str, project_root: Path | str, - source_root: Path | str | None = None, + source_root: Path | str, target_dir: Path | str | None = None, ) -> None: """ :param project_root: the full path of the project's root :param path: a full path to the file to be included - :param source_root: the root path to resolve to + :param source_root: the full root path to resolve to + :param target_dir: the relative target root to resolve to """ self.path = Path(path) self.project_root = Path(project_root).resolve() - self.source_root = None if not source_root else Path(source_root).resolve() + self.source_root = Path(source_root).resolve() self.target_dir = None if not target_dir else Path(target_dir) - if not self.path.is_absolute() and self.source_root: + if not self.path.is_absolute(): self.path = self.source_root / self.path else: self.path = self.path @@ -415,13 +416,10 @@ def relative_to_project_root(self) -> Path: return self.path.relative_to(self.project_root) def relative_to_source_root(self) -> Path: - if self.source_root is not None: - return self.path.relative_to(self.source_root) + return self.path.relative_to(self.source_root) - return self.path - - def concatenation_target_dir_relative_to_source_root(self) -> Path: + def relative_to_target_root(self) -> Path: path = self.relative_to_source_root() - if self.source_root is not None and self.target_dir is not None: + if self.target_dir is not None: return self.target_dir / path return path diff --git a/src/poetry/core/masonry/builders/wheel.py b/src/poetry/core/masonry/builders/wheel.py index 4a6922550..441edd467 100644 --- a/src/poetry/core/masonry/builders/wheel.py +++ b/src/poetry/core/masonry/builders/wheel.py @@ -267,11 +267,7 @@ def _copy_module(self, wheel: zipfile.ZipFile) -> None: # Walk the files and compress them, # sorting everything so the order is stable. for file in sorted(to_add, key=lambda x: x.path): - self._add_file( - wheel, - file.path, - file.concatenation_target_dir_relative_to_source_root(), - ) + self._add_file(wheel, file.path, file.relative_to_target_root()) def prepare_metadata(self, metadata_directory: Path) -> Path: dist_info = metadata_directory / self.dist_info