Skip to content

Commit

Permalink
builder: fix file exclusion with source root
Browse files Browse the repository at this point in the history
Previously when a file was included via package inclusion, and the 
source root differed from the project root, these files were not 
correctly excluded when ignored by the vcs. This change resolves 
the issue by ensuring the project root is used when identifying 
exclusions.
  • Loading branch information
finswimmer authored Sep 23, 2020
1 parent d5bb41a commit a056c46
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 11 deletions.
29 changes: 20 additions & 9 deletions poetry/core/masonry/builders/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ def __init__(
self._poetry = poetry
self._package = poetry.package
self._path = poetry.file.parent
self._original_path = self._path
self._excluded_files = None
self._executable = Path(executable or sys.executable) # type: Path

Expand Down Expand Up @@ -99,7 +98,7 @@ def build(self):
def find_excluded_files(self): # type: () -> Set[str]
if self._excluded_files is None:
# Checking VCS
vcs = get_vcs(self._original_path)
vcs = get_vcs(self._path)
if not vcs:
vcs_ignored_files = set()
else:
Expand Down Expand Up @@ -158,7 +157,9 @@ def find_files_to_add(
if self.format in formats:
for current_file in file.glob("**/*"):
include_file = BuildIncludeFile(
path=current_file, source_root=self._path
path=current_file,
project_root=self._path,
source_root=self._path,
)

if not current_file.is_dir() and not self.is_excluded(
Expand All @@ -176,10 +177,12 @@ def find_files_to_add(
else:
source_root = self._path

include_file = BuildIncludeFile(path=file, source_root=source_root)
include_file = BuildIncludeFile(
path=file, project_root=self._path, source_root=source_root
)

if self.is_excluded(
include_file.relative_to_source_root()
include_file.relative_to_project_root()
) and isinstance(include, PackageInclude):
continue

Expand All @@ -197,13 +200,15 @@ def find_files_to_add(
if self._package.build_script and not exclude_build:
to_add.add(
BuildIncludeFile(
path=self._package.build_script, source_root=self._path
path=self._package.build_script,
project_root=self._path,
source_root=self._path,
)
)

return to_add

def get_metadata_content(self): # type: () -> bytes
def get_metadata_content(self): # type: () -> str
content = METADATA_BASE.format(
name=self._meta.name,
version=self._meta.version,
Expand Down Expand Up @@ -310,14 +315,17 @@ def temporary_directory(cls, *args, **kwargs):
class BuildIncludeFile:
def __init__(
self,
path, # type: Path
source_root=None, # type: Optional[Path]
path, # type: Union[Path, str]
project_root, # type: Union[Path, str]
source_root=None, # type: Optional[Union[Path, str]]
):
"""
: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
"""
self.path = Path(path)
self.project_root = Path(project_root).resolve()
self.source_root = None if not source_root else Path(source_root).resolve()
if not self.path.is_absolute() and self.source_root:
self.path = self.source_root / self.path
Expand Down Expand Up @@ -346,6 +354,9 @@ def __hash__(self):
def __repr__(self): # type: () -> str
return str(self.path)

def relative_to_project_root(self): # type(): -> Path
return self.path.relative_to(self.project_root)

def relative_to_source_root(self): # type(): -> Path
if self.source_root is not None:
return self.path.relative_to(self.source_root)
Expand Down
4 changes: 3 additions & 1 deletion poetry/core/masonry/builders/sdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,9 @@ def find_files_to_add(
additional_files.add(self._poetry.local_config["readme"])

for file in additional_files:
file = BuildIncludeFile(path=file, source_root=self._path)
file = BuildIncludeFile(
path=file, project_root=self._path, source_root=self._path
)
if file.path.exists():
logger.debug("Adding: {}".format(file.relative_to_source_root()))
to_add.add(file)
Expand Down
2 changes: 1 addition & 1 deletion poetry/core/masonry/builders/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def _write_metadata(self, wheel):
relative_path = "%s/%s" % (self.dist_info, path.relative_to(self._path))
self._add_file(wheel, path, relative_path)
else:
logger.debug("Skipping: %s", path.as_posix())
logger.debug("Skipping: {}".format(path.as_posix()))

with self._write_to_zip(wheel, self.dist_info + "/WHEEL") as f:
self._write_wheel_file(f)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2018 Sébastien Eustace

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
My Package
==========
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[tool.poetry]
name = "my-package"
version = "1.2.3"
description = "Some description."
authors = [
"Sébastien Eustace <[email protected]>"
]
license = "MIT"

readme = "README.rst"

homepage = "https://python-poetry.org/"
repository = "https://github.com/python-poetry/poetry"
documentation = "https://python-poetry.org/docs"

keywords = ["packaging", "dependency", "poetry"]

classifiers = [
"Topic :: Software Development :: Build Tools",
"Topic :: Software Development :: Libraries :: Python Modules"
]

# Requirements
[tool.poetry.dependencies]
python = "^3.6"
cleo = "^0.6"
cachy = { version = "^0.2.0", extras = ["msgpack"] }

pendulum = { version = "^1.4", optional = true }

[tool.poetry.dev-dependencies]
pytest = "~3.4"

[tool.poetry.extras]
time = ["pendulum"]

[tool.poetry.scripts]
my-script = "my_package:main"
my-2nd-script = "my_package:main2"
Empty file.
42 changes: 42 additions & 0 deletions tests/masonry/builders/test_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from poetry.core.factory import Factory
from poetry.core.masonry.builders.wheel import WheelBuilder
from poetry.core.utils._compat import Path
from tests.masonry.builders.test_sdist import project


fixtures_dir = Path(__file__).parent / "fixtures"
Expand Down Expand Up @@ -228,3 +229,44 @@ def test_wheel_with_file_with_comma():
with zipfile.ZipFile(str(whl)) as z:
records = z.read("comma_file-1.2.3.dist-info/RECORD")
assert '\n"comma_file/a,b.py"' in records.decode()


def test_default_src_with_excluded_data(mocker):
# Patch git module to return specific excluded files
p = mocker.patch("poetry.core.vcs.git.Git.get_ignored_files")
p.return_value = [
(
(
Path(__file__).parent
/ "fixtures"
/ "default_src_with_excluded_data"
/ "src"
/ "my_package"
/ "data"
/ "sub_data"
/ "data2.txt"
)
.relative_to(project("default_src_with_excluded_data"))
.as_posix()
)
]
poetry = Factory().create_poetry(project("default_src_with_excluded_data"))

builder = WheelBuilder(poetry)
builder.build()

whl = (
fixtures_dir
/ "default_src_with_excluded_data"
/ "dist"
/ "my_package-1.2.3-py3-none-any.whl"
)

assert whl.exists()

with zipfile.ZipFile(str(whl)) as z:
names = z.namelist()
assert "my_package/__init__.py" in names
assert "my_package/data/data1.txt" in names
assert "my_package/data/sub_data/data2.txt" not in names
assert "my_package/data/sub_data/data3.txt" in names

0 comments on commit a056c46

Please sign in to comment.