From 22fdf8beba5030bee481f39fcefb6d11b5c6c2e2 Mon Sep 17 00:00:00 2001 From: Jan Harkes Date: Mon, 16 Nov 2020 09:10:15 -0500 Subject: [PATCH 1/5] Fix for including modules from different locations When we define multiple packages from different source locations, Poetry currently only uses the last specified from= location. This patch adds explicit paths to package_dir for additional packages. This fixes python-poetry/poetry#1811, fixes python-poetry/poetry#2354, and possibly even python-poetry/poetry#2450. --- poetry/core/masonry/builders/sdist.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/poetry/core/masonry/builders/sdist.py b/poetry/core/masonry/builders/sdist.py index df07bb293..b653badbc 100644 --- a/poetry/core/masonry/builders/sdist.py +++ b/poetry/core/masonry/builders/sdist.py @@ -139,7 +139,14 @@ def build_setup(self) -> bytes: pkg_dir, _packages, _package_data = self.find_packages(include) if pkg_dir is not None: - package_dir[""] = os.path.relpath(pkg_dir, str(self._path)) + pkg_root = os.path.relpath(pkg_dir, str(self._path)) + if "" in package_dir: + package_dir.update( + (p, os.path.join(pkg_root, p.replace(".", "/"))) + for p in _packages + ) + else: + package_dir[""] = pkg_root packages += [p for p in _packages if p not in packages] package_data.update(_package_data) From 124358bab5f19275eaf9974d5075636b086f6d7b Mon Sep 17 00:00:00 2001 From: Jan Harkes Date: Wed, 18 Nov 2020 13:55:13 -0500 Subject: [PATCH 2/5] Test the fix for including modules from different locations When we try to include moduleA from libA and moduleB from libB then package_dir in the generated setup.py must to contain either one or both `"moduleA": "libA/moduleA"` or `"moduleB": "libB/moduleB"` so we are able to find both modules when building the source dist. --- .../split_source/libA/moduleA/__init__.py | 0 .../split_source/libB/moduleB/__init__.py | 0 .../fixtures/split_source/pyproject.toml | 15 ++++++++++++++ tests/masonry/builders/test_sdist.py | 20 +++++++++++++++++++ 4 files changed, 35 insertions(+) create mode 100644 tests/masonry/builders/fixtures/split_source/libA/moduleA/__init__.py create mode 100644 tests/masonry/builders/fixtures/split_source/libB/moduleB/__init__.py create mode 100644 tests/masonry/builders/fixtures/split_source/pyproject.toml diff --git a/tests/masonry/builders/fixtures/split_source/libA/moduleA/__init__.py b/tests/masonry/builders/fixtures/split_source/libA/moduleA/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/masonry/builders/fixtures/split_source/libB/moduleB/__init__.py b/tests/masonry/builders/fixtures/split_source/libB/moduleB/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/masonry/builders/fixtures/split_source/pyproject.toml b/tests/masonry/builders/fixtures/split_source/pyproject.toml new file mode 100644 index 000000000..bb1493ea2 --- /dev/null +++ b/tests/masonry/builders/fixtures/split_source/pyproject.toml @@ -0,0 +1,15 @@ +[tool.poetry] +name = "split-source" +version = "0.1" +description = "Combine packages from different locations." +authors = [ + "Jan Harkes " +] +license = "MIT" +packages = [ + { include = "moduleA", from = "libA" }, + { include = "moduleB", from = "libB" }, +] + +[tool.poetry.dependencies] +python = "^3.6" diff --git a/tests/masonry/builders/test_sdist.py b/tests/masonry/builders/test_sdist.py index ae299595a..db5811e0f 100644 --- a/tests/masonry/builders/test_sdist.py +++ b/tests/masonry/builders/test_sdist.py @@ -629,3 +629,23 @@ def test_sdist_mtime_zero(): with gzip.open(str(sdist), "rb") as gz: gz.read(100) assert gz.mtime == 0 + + +def test_split_source(): + root = fixtures_dir / "split_source" + poetry = Factory().create_poetry(root) + + builder = SdistBuilder(poetry) + + # Check setup.py + setup = builder.build_setup() + setup_ast = ast.parse(setup) + + setup_ast.body = [n for n in setup_ast.body if isinstance(n, ast.Assign)] + ns = {} + exec(compile(setup_ast, filename="setup.py", mode="exec"), ns) + assert "moduleA" in ns["package_dir"] or "moduleB" in ns["package_dir"] + if "moduleA" in ns["package_dir"]: + assert ns["package_dir"]["moduleA"] == "libA/moduleA" + if "moduleB" in ns["package_dir"]: + assert ns["package_dir"]["moduleB"] == "libB/moduleB" From e219167d0db52b719c018f35bbf26f1445c6bf9f Mon Sep 17 00:00:00 2001 From: Jan Harkes Date: Wed, 18 Nov 2020 14:19:50 -0500 Subject: [PATCH 3/5] Avoid issues when running split_source test on Windows. --- .../{libA/moduleA => lib_a/module_a}/__init__.py | 0 .../{libB/moduleB => lib_b/module_b}/__init__.py | 0 tests/masonry/builders/fixtures/split_source/pyproject.toml | 4 ++-- tests/masonry/builders/test_sdist.py | 6 +----- 4 files changed, 3 insertions(+), 7 deletions(-) rename tests/masonry/builders/fixtures/split_source/{libA/moduleA => lib_a/module_a}/__init__.py (100%) rename tests/masonry/builders/fixtures/split_source/{libB/moduleB => lib_b/module_b}/__init__.py (100%) diff --git a/tests/masonry/builders/fixtures/split_source/libA/moduleA/__init__.py b/tests/masonry/builders/fixtures/split_source/lib_a/module_a/__init__.py similarity index 100% rename from tests/masonry/builders/fixtures/split_source/libA/moduleA/__init__.py rename to tests/masonry/builders/fixtures/split_source/lib_a/module_a/__init__.py diff --git a/tests/masonry/builders/fixtures/split_source/libB/moduleB/__init__.py b/tests/masonry/builders/fixtures/split_source/lib_b/module_b/__init__.py similarity index 100% rename from tests/masonry/builders/fixtures/split_source/libB/moduleB/__init__.py rename to tests/masonry/builders/fixtures/split_source/lib_b/module_b/__init__.py diff --git a/tests/masonry/builders/fixtures/split_source/pyproject.toml b/tests/masonry/builders/fixtures/split_source/pyproject.toml index bb1493ea2..f11bba047 100644 --- a/tests/masonry/builders/fixtures/split_source/pyproject.toml +++ b/tests/masonry/builders/fixtures/split_source/pyproject.toml @@ -7,8 +7,8 @@ authors = [ ] license = "MIT" packages = [ - { include = "moduleA", from = "libA" }, - { include = "moduleB", from = "libB" }, + { include = "module_a", from = "lib_a" }, + { include = "module_b", from = "lib_b" }, ] [tool.poetry.dependencies] diff --git a/tests/masonry/builders/test_sdist.py b/tests/masonry/builders/test_sdist.py index db5811e0f..5778206e2 100644 --- a/tests/masonry/builders/test_sdist.py +++ b/tests/masonry/builders/test_sdist.py @@ -644,8 +644,4 @@ def test_split_source(): setup_ast.body = [n for n in setup_ast.body if isinstance(n, ast.Assign)] ns = {} exec(compile(setup_ast, filename="setup.py", mode="exec"), ns) - assert "moduleA" in ns["package_dir"] or "moduleB" in ns["package_dir"] - if "moduleA" in ns["package_dir"]: - assert ns["package_dir"]["moduleA"] == "libA/moduleA" - if "moduleB" in ns["package_dir"]: - assert ns["package_dir"]["moduleB"] == "libB/moduleB" + assert "module_a" in ns["package_dir"] or "module_b" in ns["package_dir"] From 9ab0cf92f8b4f6cd164d1fb28b63affcc724437a Mon Sep 17 00:00:00 2001 From: Jan Harkes Date: Thu, 3 Jun 2021 17:52:19 -0400 Subject: [PATCH 4/5] Correct split_source test As noted by @layday, `ns["package_dir"].keys() == {"", "module_b"}` should always be true, so we don't have to test for module_a in `ns["package_dir"]`. --- tests/masonry/builders/test_sdist.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/masonry/builders/test_sdist.py b/tests/masonry/builders/test_sdist.py index 5778206e2..ab11648ba 100644 --- a/tests/masonry/builders/test_sdist.py +++ b/tests/masonry/builders/test_sdist.py @@ -644,4 +644,4 @@ def test_split_source(): setup_ast.body = [n for n in setup_ast.body if isinstance(n, ast.Assign)] ns = {} exec(compile(setup_ast, filename="setup.py", mode="exec"), ns) - assert "module_a" in ns["package_dir"] or "module_b" in ns["package_dir"] + assert ns["package_dir"] == {"": "lib_a", "module_b": "lib_b/module_b"} From 9db9aae1a6c17d869c75449970c29a8c2232f73e Mon Sep 17 00:00:00 2001 From: Jan Harkes Date: Fri, 4 Jun 2021 08:47:29 -0400 Subject: [PATCH 5/5] Revert test to checking dictionary keys because paths differ on Windows --- tests/masonry/builders/test_sdist.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/masonry/builders/test_sdist.py b/tests/masonry/builders/test_sdist.py index ab11648ba..7ccd65ee9 100644 --- a/tests/masonry/builders/test_sdist.py +++ b/tests/masonry/builders/test_sdist.py @@ -644,4 +644,4 @@ def test_split_source(): setup_ast.body = [n for n in setup_ast.body if isinstance(n, ast.Assign)] ns = {} exec(compile(setup_ast, filename="setup.py", mode="exec"), ns) - assert ns["package_dir"] == {"": "lib_a", "module_b": "lib_b/module_b"} + assert "" in ns["package_dir"] and "module_b" in ns["package_dir"]