Skip to content

Commit

Permalink
Fix for including modules from different locations (python-poetry#108)
Browse files Browse the repository at this point in the history
* 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#1811, fixes python-poetry#2354,
and possibly even python-poetry#2450.

* 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.

`ns["package_dir"].keys() == {"", "module_b"}`
should always be true, so we don't have to test for module_a in
`ns["package_dir"]`.
  • Loading branch information
jaharkes committed Nov 10, 2021
1 parent 237a4c8 commit 2b4b8c4
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
9 changes: 8 additions & 1 deletion poetry/core/masonry/builders/sdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Empty file.
Empty file.
15 changes: 15 additions & 0 deletions tests/masonry/builders/fixtures/split_source/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[tool.poetry]
name = "split-source"
version = "0.1"
description = "Combine packages from different locations."
authors = [
"Jan Harkes <jaharkes@cs.cmu.edu>"
]
license = "MIT"
packages = [
{ include = "module_a", from = "lib_a" },
{ include = "module_b", from = "lib_b" },
]

[tool.poetry.dependencies]
python = "^3.6"
16 changes: 16 additions & 0 deletions tests/masonry/builders/test_sdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,3 +629,19 @@ 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 "" in ns["package_dir"] and "module_b" in ns["package_dir"]

0 comments on commit 2b4b8c4

Please sign in to comment.