Commit d2ed7d59 by Sébastien Eustace

Fix packaging with explicit packages and src directories

parent 775f38fd
...@@ -37,16 +37,37 @@ class Builder(object): ...@@ -37,16 +37,37 @@ class Builder(object):
AVAILABLE_PYTHONS = {"2", "2.7", "3", "3.4", "3.5", "3.6", "3.7"} AVAILABLE_PYTHONS = {"2", "2.7", "3", "3.4", "3.5", "3.6", "3.7"}
def __init__(self, poetry, env, io): # type: (Poetry, Env, IO) -> None format = None
def __init__(
self, poetry, env, io, ignore_packages_formats=False
): # type: (Poetry, Env, IO) -> None
self._poetry = poetry self._poetry = poetry
self._env = env self._env = env
self._io = io self._io = io
self._package = poetry.package self._package = poetry.package
self._path = poetry.file.parent self._path = poetry.file.parent
packages = []
for p in self._package.packages:
formats = p.get("format", [])
if not isinstance(formats, list):
formats = [formats]
if (
formats
and self.format
and self.format not in formats
and not ignore_packages_formats
):
continue
packages.append(p)
self._module = Module( self._module = Module(
self._package.name, self._package.name,
self._path.as_posix(), self._path.as_posix(),
packages=self._package.packages, packages=packages,
includes=self._package.include, includes=self._package.include,
) )
self._meta = Metadata.from_package(self._package) self._meta = Metadata.from_package(self._package)
......
...@@ -2,6 +2,9 @@ import os ...@@ -2,6 +2,9 @@ import os
import tarfile import tarfile
import poetry.poetry import poetry.poetry
from poetry.io.null_io import NullIO
from poetry.utils._compat import Path
from poetry.utils.helpers import temporary_directory
from contextlib import contextmanager from contextlib import contextmanager
...@@ -15,19 +18,46 @@ class CompleteBuilder(Builder): ...@@ -15,19 +18,46 @@ class CompleteBuilder(Builder):
# We start by building the tarball # We start by building the tarball
# We will use it to build the wheel # We will use it to build the wheel
sdist_builder = SdistBuilder(self._poetry, self._env, self._io) sdist_builder = SdistBuilder(self._poetry, self._env, self._io)
build_for_all_formats = False
for p in self._package.packages:
formats = p.get("format", [])
if not isinstance(formats, list):
formats = [formats]
if formats and sdist_builder.format not in formats:
build_for_all_formats = True
break
sdist_file = sdist_builder.build() sdist_file = sdist_builder.build()
self._io.write_line("") self._io.write_line("")
dist_dir = self._path / "dist" dist_dir = self._path / "dist"
with self.unpacked_tarball(sdist_file) as tmpdir:
WheelBuilder.make_in( if build_for_all_formats:
poetry.poetry.Poetry.create(tmpdir), sdist_builder = SdistBuilder(
self._env, self._poetry, self._env, NullIO(), ignore_packages_formats=True
self._io,
dist_dir,
original=self._poetry,
) )
with temporary_directory() as tmp_dir:
sdist_file = sdist_builder.build(Path(tmp_dir))
with self.unpacked_tarball(sdist_file) as tmpdir:
WheelBuilder.make_in(
poetry.poetry.Poetry.create(tmpdir),
self._env,
self._io,
dist_dir,
original=self._poetry,
)
else:
with self.unpacked_tarball(sdist_file) as tmpdir:
WheelBuilder.make_in(
poetry.poetry.Poetry.create(tmpdir),
self._env,
self._io,
dist_dir,
original=self._poetry,
)
@classmethod @classmethod
@contextmanager @contextmanager
......
...@@ -44,6 +44,9 @@ setup(**setup_kwargs) ...@@ -44,6 +44,9 @@ setup(**setup_kwargs)
class SdistBuilder(Builder): class SdistBuilder(Builder):
format = "sdist"
def build(self, target_dir=None): # type: (Path) -> Path def build(self, target_dir=None): # type: (Path) -> Path
self._io.write_line(" - Building <info>sdist</info>") self._io.write_line(" - Building <info>sdist</info>")
if target_dir is None: if target_dir is None:
...@@ -119,16 +122,17 @@ class SdistBuilder(Builder): ...@@ -119,16 +122,17 @@ class SdistBuilder(Builder):
pkg_dir, _packages, _package_data = self.find_packages(include) pkg_dir, _packages, _package_data = self.find_packages(include)
if pkg_dir is not None: if pkg_dir is not None:
package_dir[""] = os.path.relpath(pkg_dir, str(self._path)) for p in _packages:
package_dir[p] = os.path.relpath(pkg_dir, str(self._path))
packages += [p for p in _packages if p not in packages] packages += [p for p in _packages if p not in packages]
package_data.update(_package_data) package_data.update(_package_data)
else: else:
if include.source is not None:
package_dir[""] = str(include.base.relative_to(self._path))
module = include.elements[0].relative_to(include.base).stem module = include.elements[0].relative_to(include.base).stem
if include.source is not None:
package_dir[module] = str(include.base.relative_to(self._path))
if module not in modules: if module not in modules:
modules.append(module) modules.append(module)
else: else:
...@@ -224,7 +228,6 @@ class SdistBuilder(Builder): ...@@ -224,7 +228,6 @@ class SdistBuilder(Builder):
# Relative to the top-level package # Relative to the top-level package
return pkg_name, Path(rel_path).as_posix() return pkg_name, Path(rel_path).as_posix()
excluded_files = self.find_excluded_files()
for path, dirnames, filenames in os.walk(str(base), topdown=True): for path, dirnames, filenames in os.walk(str(base), topdown=True):
if os.path.basename(path) == "__pycache__": if os.path.basename(path) == "__pycache__":
continue continue
...@@ -267,9 +270,7 @@ class SdistBuilder(Builder): ...@@ -267,9 +270,7 @@ class SdistBuilder(Builder):
@classmethod @classmethod
def convert_dependencies( def convert_dependencies(
cls, cls, package, dependencies # type: Package # type: List[Dependency]
package, # type: Package
dependencies, # type: List[Dependency]
): ):
main = [] main = []
extras = defaultdict(list) extras = defaultdict(list)
......
...@@ -37,6 +37,9 @@ Tag: {tag} ...@@ -37,6 +37,9 @@ Tag: {tag}
class WheelBuilder(Builder): class WheelBuilder(Builder):
format = "wheel"
def __init__(self, poetry, env, io, target_dir=None, original=None): def __init__(self, poetry, env, io, target_dir=None, original=None):
super(WheelBuilder, self).__init__(poetry, env, io) super(WheelBuilder, self).__init__(poetry, env, io)
......
...@@ -27,6 +27,7 @@ packages = [ ...@@ -27,6 +27,7 @@ packages = [
{ include = "package_with_include" }, { include = "package_with_include" },
{ include = "tests", format = "sdist" }, { include = "tests", format = "sdist" },
{ include = "for_wheel_only", format = ["wheel"] }, { include = "for_wheel_only", format = ["wheel"] },
{ include = "src_package", from = "src"},
] ]
include = [ include = [
......
...@@ -436,7 +436,8 @@ def test_package_with_include(mocker): ...@@ -436,7 +436,8 @@ def test_package_with_include(mocker):
assert "with-include-1.2.3/pyproject.toml" in names assert "with-include-1.2.3/pyproject.toml" in names
assert "with-include-1.2.3/setup.py" in names assert "with-include-1.2.3/setup.py" in names
assert "with-include-1.2.3/PKG-INFO" in names assert "with-include-1.2.3/PKG-INFO" in names
assert "for_wheel_only/__init__" not 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
setup = tar.extractfile("with-include-1.2.3/setup.py").read() setup = tar.extractfile("with-include-1.2.3/setup.py").read()
setup_ast = ast.parse(setup) setup_ast = ast.parse(setup)
...@@ -444,11 +445,12 @@ def test_package_with_include(mocker): ...@@ -444,11 +445,12 @@ def test_package_with_include(mocker):
setup_ast.body = [n for n in setup_ast.body if isinstance(n, ast.Assign)] setup_ast.body = [n for n in setup_ast.body if isinstance(n, ast.Assign)]
ns = {} ns = {}
exec(compile(setup_ast, filename="setup.py", mode="exec"), ns) exec(compile(setup_ast, filename="setup.py", mode="exec"), ns)
assert "package_dir" not in ns assert ns["package_dir"] == {"src_package": "src"}
assert ns["packages"] == [ assert ns["packages"] == [
"extra_dir", "extra_dir",
"extra_dir.sub_pkg", "extra_dir.sub_pkg",
"package_with_include", "package_with_include",
"src_package",
"tests", "tests",
] ]
assert ns["package_data"] == {"": ["*"]} assert ns["package_data"] == {"": ["*"]}
...@@ -470,4 +472,5 @@ def test_package_with_include(mocker): ...@@ -470,4 +472,5 @@ def test_package_with_include(mocker):
assert "my_module.py" in names assert "my_module.py" in names
assert "notes.txt" in names assert "notes.txt" in names
assert "package_with_include/__init__.py" in names assert "package_with_include/__init__.py" in names
assert "with-include-1.2.3/tests/__init__.py" not in names assert "tests/__init__.py" not in names
assert "src_package/__init__.py" in names
...@@ -315,7 +315,7 @@ def test_with_src_module_file(): ...@@ -315,7 +315,7 @@ def test_with_src_module_file():
setup_ast.body = [n for n in setup_ast.body if isinstance(n, ast.Assign)] setup_ast.body = [n for n in setup_ast.body if isinstance(n, ast.Assign)]
ns = {} ns = {}
exec(compile(setup_ast, filename="setup.py", mode="exec"), ns) exec(compile(setup_ast, filename="setup.py", mode="exec"), ns)
assert ns["package_dir"] == {"": "src"} assert ns["package_dir"] == {"module_src": "src"}
assert ns["modules"] == ["module_src"] assert ns["modules"] == ["module_src"]
builder.build() builder.build()
...@@ -340,7 +340,7 @@ def test_with_src_module_dir(): ...@@ -340,7 +340,7 @@ def test_with_src_module_dir():
setup_ast.body = [n for n in setup_ast.body if isinstance(n, ast.Assign)] setup_ast.body = [n for n in setup_ast.body if isinstance(n, ast.Assign)]
ns = {} ns = {}
exec(compile(setup_ast, filename="setup.py", mode="exec"), ns) exec(compile(setup_ast, filename="setup.py", mode="exec"), ns)
assert ns["package_dir"] == {"": "src"} assert ns["package_dir"] == {"package_src": "src"}
assert ns["packages"] == ["package_src"] assert ns["packages"] == ["package_src"]
builder.build() builder.build()
......
...@@ -125,6 +125,7 @@ def test_poetry_with_packages_and_includes(): ...@@ -125,6 +125,7 @@ def test_poetry_with_packages_and_includes():
{"include": "package_with_include"}, {"include": "package_with_include"},
{"include": "tests", "format": "sdist"}, {"include": "tests", "format": "sdist"},
{"include": "for_wheel_only", "format": ["wheel"]}, {"include": "for_wheel_only", "format": ["wheel"]},
{"include": "src_package", "from": "src"},
] ]
assert package.include == ["extra_dir/vcs_excluded.txt", "notes.txt"] assert package.include == ["extra_dir/vcs_excluded.txt", "notes.txt"]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment