From f8c37007f0a227251f09b9746493c48784b1a1f9 Mon Sep 17 00:00:00 2001 From: finswimmer Date: Wed, 8 Apr 2020 21:35:01 +0200 Subject: [PATCH] fix subpackage check for setup.py generation Resolves: python-poetry/poetry#2283 --- poetry/core/masonry/builders/sdist.py | 10 +++++++++- .../fixtures/excluded_subpackage/README.rst | 2 ++ .../excluded_subpackage/example/__init__.py | 1 + .../example/test/__init__.py | 0 .../example/test/excluded.py | 5 +++++ .../excluded_subpackage/pyproject.toml | 18 ++++++++++++++++++ tests/masonry/builders/test_sdist.py | 15 +++++++++++++++ 7 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 tests/masonry/builders/fixtures/excluded_subpackage/README.rst create mode 100644 tests/masonry/builders/fixtures/excluded_subpackage/example/__init__.py create mode 100644 tests/masonry/builders/fixtures/excluded_subpackage/example/test/__init__.py create mode 100644 tests/masonry/builders/fixtures/excluded_subpackage/example/test/excluded.py create mode 100644 tests/masonry/builders/fixtures/excluded_subpackage/pyproject.toml diff --git a/poetry/core/masonry/builders/sdist.py b/poetry/core/masonry/builders/sdist.py index 9f6b4de71..0bf375498 100644 --- a/poetry/core/masonry/builders/sdist.py +++ b/poetry/core/masonry/builders/sdist.py @@ -240,7 +240,15 @@ def find_nearest_pkg(rel_path): if from_top_level == ".": continue - is_subpkg = any([filename.endswith(".py") for filename in filenames]) + is_subpkg = any( + [filename.endswith(".py") for filename in filenames] + ) and not all( + [ + self.is_excluded(Path(path, filename).relative_to(self._path)) + for filename in filenames + if filename.endswith(".py") + ] + ) if is_subpkg: subpkg_paths.add(from_top_level) parts = from_top_level.split(os.sep) diff --git a/tests/masonry/builders/fixtures/excluded_subpackage/README.rst b/tests/masonry/builders/fixtures/excluded_subpackage/README.rst new file mode 100644 index 000000000..b00640203 --- /dev/null +++ b/tests/masonry/builders/fixtures/excluded_subpackage/README.rst @@ -0,0 +1,2 @@ +My Package +========== \ No newline at end of file diff --git a/tests/masonry/builders/fixtures/excluded_subpackage/example/__init__.py b/tests/masonry/builders/fixtures/excluded_subpackage/example/__init__.py new file mode 100644 index 000000000..3dc1f76bc --- /dev/null +++ b/tests/masonry/builders/fixtures/excluded_subpackage/example/__init__.py @@ -0,0 +1 @@ +__version__ = "0.1.0" diff --git a/tests/masonry/builders/fixtures/excluded_subpackage/example/test/__init__.py b/tests/masonry/builders/fixtures/excluded_subpackage/example/test/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/masonry/builders/fixtures/excluded_subpackage/example/test/excluded.py b/tests/masonry/builders/fixtures/excluded_subpackage/example/test/excluded.py new file mode 100644 index 000000000..bf6e1f896 --- /dev/null +++ b/tests/masonry/builders/fixtures/excluded_subpackage/example/test/excluded.py @@ -0,0 +1,5 @@ +from .. import __version__ + + +def test_version(): + assert __version__ == "0.1.0" diff --git a/tests/masonry/builders/fixtures/excluded_subpackage/pyproject.toml b/tests/masonry/builders/fixtures/excluded_subpackage/pyproject.toml new file mode 100644 index 000000000..3852bb831 --- /dev/null +++ b/tests/masonry/builders/fixtures/excluded_subpackage/pyproject.toml @@ -0,0 +1,18 @@ +[tool.poetry] +name = "example" +version = "0.1.0" +description = "" +authors = ["Sébastien Eustace "] +exclude = [ + "**/test/**/*", +] + +[tool.poetry.dependencies] +python = "^3.6" + +[tool.poetry.dev-dependencies] +pytest = "^3.0" + +[build-system] +requires = ["poetry>=0.12"] +build-backend = "poetry.masonry.api" diff --git a/tests/masonry/builders/test_sdist.py b/tests/masonry/builders/test_sdist.py index 19daef31e..a3cc7f519 100644 --- a/tests/masonry/builders/test_sdist.py +++ b/tests/masonry/builders/test_sdist.py @@ -465,3 +465,18 @@ def test_proper_python_requires_if_three_digits_precision_version_specified(): parsed = p.parsestr(to_str(pkg_info)) assert parsed["Requires-Python"] == "==2.7.15" + + +def test_excluded_subpackage(): + poetry = Factory().create_poetry(project("excluded_subpackage")) + + builder = SdistBuilder(poetry) + 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 ns["packages"] == ["example"]