Skip to content

Commit

Permalink
Change schema so that includes now supports inline tables.
Browse files Browse the repository at this point in the history
  • Loading branch information
kasteph committed Apr 5, 2020
1 parent fbfcda7 commit 0ebf500
Show file tree
Hide file tree
Showing 21 changed files with 201 additions and 5 deletions.
13 changes: 12 additions & 1 deletion poetry/core/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,18 @@ def create_poetry(self, cwd=None): # type: (Optional[Path]) -> Poetry
package.build = local_config["build"]

if "include" in local_config:
package.include = local_config["include"]
package.include = []

for include in local_config["include"]:
if not isinstance(include, dict):
include = {"include": include}

formats = include.get("format", [])
if formats and not isinstance(formats, list):
formats = [formats]
include["format"] = formats

package.include.append(include)

if "exclude" in local_config:
package.exclude = local_config["exclude"]
Expand Down
39 changes: 37 additions & 2 deletions poetry/core/json/schemas/poetry-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
"format": {
"oneOf": [
{"type": "string"},
{"type": "array", "items": {"type": "string"}}
{"type": "array", "items": {"type": "string"}}
],
"description": "The format(s) for which the package must be included."
}
Expand All @@ -92,7 +92,42 @@
},
"include": {
"type": "array",
"description": "A list of files and folders to include."
"description": "A list of files and folders to include.",
"items": {
"anyOf": [
{
"type": "string"
},
{
"type": "object",
"description": "Information about where the file or folder resides.",
"additionalProperties": false,
"required": [
"include"
],
"properties": {
"include": {
"type": "string",
"description": "What to include in the package."
},
"format": {
"oneOf": [
{
"type": "string"
},
{
"type": "array",
"items": {
"type": "string"
}
}
],
"description": "The format(s) for which the file(s) or folder(s) must be included."
}
}
}
]
}
},
"exclude": {
"type": "array",
Expand Down
25 changes: 24 additions & 1 deletion poetry/core/masonry/builders/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,25 @@ def __init__(

packages.append(p)

includes = []
for i in self._package.include:
formats = i.get("format", [])

if (
formats
and self.format
and self.format not in formats
and not ignore_packages_formats
):
continue

includes.append(i)

self._module = Module(
self._package.name,
self._path.as_posix(),
packages=packages,
includes=self._package.include,
includes=includes,
)
self._meta = Metadata.from_package(self._package)

Expand Down Expand Up @@ -127,6 +141,15 @@ def find_files_to_add(self, exclude_build=True): # type: (bool) -> list
continue

if file.is_dir():
if self.format == "sdist" or self.format in include.formats:
for f in Path(file).glob("**/*"):
included = f.relative_to(self._path)
if (
included not in to_add
and not f.is_dir()
and not self.is_excluded(included)
):
to_add.append(included)
continue

file = file.relative_to(self._path)
Expand Down
4 changes: 3 additions & 1 deletion poetry/core/masonry/utils/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ def __init__(self, name, directory=".", packages=None, includes=None):
)

for include in includes:
self._includes.append(Include(self._path, include))
self._includes.append(
Include(self._path, include["include"], formats=include["format"])
)

@property
def name(self): # type: () -> str
Expand Down
20 changes: 20 additions & 0 deletions tests/masonry/builders/fixtures/with_include_inline_table/LICENSE
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
==========
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = "1.2.3"
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
[tool.poetry]
name = "with-include"
version = "1.2.3"
description = "Some description."
authors = [
"Sébastien Eustace <sebastien@eustace.io>"
]
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"
]

packages = [
{ include = "src_package", from = "src"},
]

include = [
{ include = "notes.txt", format = "sdist"},
{ include = "tests", format = "sdist" },
{ include = "wheel_only.txt", format = "wheel" }
]


# 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.
Empty file.
Empty file.
Empty file.
39 changes: 39 additions & 0 deletions tests/masonry/builders/test_sdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,3 +465,42 @@ 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_includes():
poetry = Factory().create_poetry(project("with-include"))

builder = SdistBuilder(poetry)

builder.build()

sdist = fixtures_dir / "with-include" / "dist" / "with-include-1.2.3.tar.gz"

assert sdist.exists()

with tarfile.open(str(sdist), "r") as tar:
assert "with-include-1.2.3/extra_dir/vcs_excluded.txt" in tar.getnames()
assert "with-include-1.2.3/notes.txt" in tar.getnames()


def test_includes_with_inline_table():
poetry = Factory().create_poetry(project("with_include_inline_table"))

builder = SdistBuilder(poetry)

builder.build()

sdist = (
fixtures_dir
/ "with_include_inline_table"
/ "dist"
/ "with-include-1.2.3.tar.gz"
)

assert sdist.exists()

with tarfile.open(str(sdist), "r") as tar:
assert "with-include-1.2.3/wheel_only.txt" not in tar.getnames()
assert "with-include-1.2.3/notes.txt" in tar.getnames()
assert "with-include-1.2.3/tests/__init__.py" in tar.getnames()
assert "with-include-1.2.3/tests/test_foo/test.py" in tar.getnames()
13 changes: 13 additions & 0 deletions tests/masonry/builders/test_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,16 @@ def test_dist_info_file_permissions():
z.getinfo("my_package-1.2.3.dist-info/entry_points.txt").external_attr
== 0o644 << 16
)


def test_wheel_includes_inline_table():
module_path = fixtures_dir / "with_include_inline_table"
WheelBuilder.make(Factory().create_poetry(module_path))

whl = module_path / "dist" / "with_include-1.2.3-py3-none-any.whl"

assert whl.exists()

with zipfile.ZipFile(str(whl)) as z:
assert "wheel_only.txt" in z.namelist()
assert "notes.txt" not in z.namelist()

0 comments on commit 0ebf500

Please sign in to comment.