Skip to content

Commit

Permalink
normalize source distribution names
Browse files Browse the repository at this point in the history
  • Loading branch information
dimbleby committed Sep 25, 2022
1 parent 2df6d0c commit 28a971f
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 22 deletions.
4 changes: 3 additions & 1 deletion src/poetry/core/masonry/builders/sdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from poetry.core.masonry.builders.builder import Builder
from poetry.core.masonry.builders.builder import BuildIncludeFile
from poetry.core.masonry.utils.helpers import distribution_name


if TYPE_CHECKING:
Expand Down Expand Up @@ -65,7 +66,8 @@ def build(
if not target_dir.exists():
target_dir.mkdir(parents=True)

target = target_dir / f"{self._package.pretty_name}-{self._meta.version}.tar.gz"
name = distribution_name(self._package.name)
target = target_dir / f"{name}-{self._meta.version}.tar.gz"
gz = GzipFile(target.as_posix(), mode="wb", mtime=0)
tar = tarfile.TarFile(
target.as_posix(), mode="w", fileobj=gz, format=tarfile.PAX_FORMAT
Expand Down
11 changes: 6 additions & 5 deletions src/poetry/core/masonry/builders/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
from poetry.core import __version__
from poetry.core.masonry.builders.builder import Builder
from poetry.core.masonry.builders.sdist import SdistBuilder
from poetry.core.masonry.utils.helpers import escape_name
from poetry.core.masonry.utils.helpers import distribution_name
from poetry.core.masonry.utils.helpers import normalize_file_permissions
from poetry.core.masonry.utils.package_include import PackageInclude
from poetry.core.semver.helpers import parse_constraint


if TYPE_CHECKING:
from packaging.utils import NormalizedName

from poetry.core.poetry import Poetry

wheel_file_template = """\
Expand Down Expand Up @@ -280,7 +282,7 @@ def wheel_data_folder(self) -> str:

@property
def wheel_filename(self) -> str:
name = escape_name(self._package.pretty_name)
name = distribution_name(self._package.name)
version = self._meta.version
return f"{name}-{version}-{self.tag}.whl"

Expand All @@ -289,9 +291,8 @@ def supports_python2(self) -> bool:
parse_constraint(">=2.0.0 <3.0.0")
)

def dist_info_name(self, distribution: str, version: str) -> str:
escaped_name = escape_name(distribution)

def dist_info_name(self, name: NormalizedName, version: str) -> str:
escaped_name = distribution_name(name)
return f"{escaped_name}-{version}.dist-info"

@property
Expand Down
40 changes: 40 additions & 0 deletions src/poetry/core/masonry/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
import re
import warnings

from typing import TYPE_CHECKING
from typing import NewType
from typing import cast


if TYPE_CHECKING:
from packaging.utils import NormalizedName


DistributionName = NewType("DistributionName", str)


def normalize_file_permissions(st_mode: int) -> int:
"""
Expand Down Expand Up @@ -39,4 +50,33 @@ def escape_name(name: str) -> str:
Escaped wheel name as specified in https://packaging.python.org/en/latest/specifications/binary-distribution-format/#escaping-and-unicode.
This function should only be used for the generation of artifact names, and not to normalize or filter existing artifact names.
"""
warnings.warn(
"escape_name() is deprecated. Use packaging.utils.canonicalize_name() and"
" distribution_name() instead.",
DeprecationWarning,
stacklevel=2,
)
return re.sub(r"[-_.]+", "_", name, flags=re.UNICODE).lower()


# A normalized name, but with "-" replaced by "_". This is used in various places:
#
# https://packaging.python.org/en/latest/specifications/binary-distribution-format/#escaping-and-unicode
#
# In distribution names ... This is equivalent to PEP 503 normalisation followed by
# replacing - with _.
#
# https://packaging.python.org/en/latest/specifications/source-distribution-format/#source-distribution-file-name
#
# ... {name} is normalised according to the same rules as for binary distributions
#
# https://packaging.python.org/en/latest/specifications/recording-installed-packages/#the-dist-info-directory
#
# This directory is named as {name}-{version}.dist-info, with name and version
# fields corresponding to Core metadata specifications. Both fields must be
# normalized (see PEP 503 and PEP 440 for the definition of normalization for each
# field respectively), and replace dash (-) characters with underscore
# (_) characters ...
def distribution_name(name: NormalizedName) -> DistributionName:
distribution_name = name.replace("-", "_")
return cast(DistributionName, distribution_name)
2 changes: 1 addition & 1 deletion tests/integration/test_pep517.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_pep517_build_sdist(
outdir=str(temporary_directory),
distributions=["sdist"],
)
distributions = list(temporary_directory.glob("poetry-core-*.tar.gz"))
distributions = list(temporary_directory.glob("poetry_core-*.tar.gz"))
assert len(distributions) == 1


Expand Down
10 changes: 5 additions & 5 deletions tests/masonry/builders/test_complete.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ def test_module_src() -> None:
builder = Builder(Factory().create_poetry(module_path))
builder.build(fmt="all")

sdist = module_path / "dist" / "module-src-0.1.tar.gz"
sdist = module_path / "dist" / "module_src-0.1.tar.gz"

assert sdist.exists()

Expand All @@ -449,7 +449,7 @@ def test_package_src() -> None:
builder = Builder(Factory().create_poetry(module_path))
builder.build(fmt="all")

sdist = module_path / "dist" / "package-src-0.1.tar.gz"
sdist = module_path / "dist" / "package_src-0.1.tar.gz"

assert sdist.exists()

Expand All @@ -474,7 +474,7 @@ def test_split_source() -> None:
builder = Builder(Factory().create_poetry(module_path))
builder.build(fmt="all")

sdist = module_path / "dist" / "split-source-0.1.tar.gz"
sdist = module_path / "dist" / "split_source-0.1.tar.gz"

assert sdist.exists()

Expand Down Expand Up @@ -520,7 +520,7 @@ def test_package_with_include(mocker: MockerFixture) -> None:
builder = Builder(Factory().create_poetry(module_path))
builder.build(fmt="all")

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

assert sdist.exists()

Expand Down Expand Up @@ -587,7 +587,7 @@ def test_respect_format_for_explicit_included_files() -> None:
builder = Builder(Factory().create_poetry(module_path))
builder.build(fmt="all")

sdist = module_path / "dist" / "exclude-whl-include-sdist-0.1.0.tar.gz"
sdist = module_path / "dist" / "exclude_whl_include_sdist-0.1.0.tar.gz"

assert sdist.exists()

Expand Down
20 changes: 10 additions & 10 deletions tests/masonry/builders/test_sdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ def test_package() -> None:
builder = SdistBuilder(poetry)
builder.build()

sdist = fixtures_dir / "complete" / "dist" / "my-package-1.2.3.tar.gz"
sdist = fixtures_dir / "complete" / "dist" / "my_package-1.2.3.tar.gz"

assert sdist.exists()

Expand All @@ -270,7 +270,7 @@ def test_sdist_reproducibility() -> None:
builder = SdistBuilder(poetry)
builder.build()

sdist = fixtures_dir / "complete" / "dist" / "my-package-1.2.3.tar.gz"
sdist = fixtures_dir / "complete" / "dist" / "my_package-1.2.3.tar.gz"

assert sdist.exists()

Expand Down Expand Up @@ -386,7 +386,7 @@ def test_with_src_module_file() -> None:

builder.build()

sdist = fixtures_dir / "source_file" / "dist" / "module-src-0.1.tar.gz"
sdist = fixtures_dir / "source_file" / "dist" / "module_src-0.1.tar.gz"

assert sdist.exists()

Expand All @@ -411,7 +411,7 @@ def test_with_src_module_dir() -> None:

builder.build()

sdist = fixtures_dir / "source_package" / "dist" / "package-src-0.1.tar.gz"
sdist = fixtures_dir / "source_package" / "dist" / "package_src-0.1.tar.gz"

assert sdist.exists()

Expand Down Expand Up @@ -459,7 +459,7 @@ def test_default_with_excluded_data(mocker: MockerFixture) -> None:
builder.build()

sdist = (
fixtures_dir / "default_with_excluded_data" / "dist" / "my-package-1.2.3.tar.gz"
fixtures_dir / "default_with_excluded_data" / "dist" / "my_package-1.2.3.tar.gz"
)

assert sdist.exists()
Expand Down Expand Up @@ -493,7 +493,7 @@ def test_src_excluded_nested_data() -> None:
builder = SdistBuilder(poetry)
builder.build()

sdist = module_path / "dist" / "my-package-1.2.3.tar.gz"
sdist = module_path / "dist" / "my_package-1.2.3.tar.gz"

assert sdist.exists()

Expand Down Expand Up @@ -548,7 +548,7 @@ def test_includes() -> None:

builder.build()

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

assert sdist.exists()

Expand All @@ -568,7 +568,7 @@ def test_includes_with_inline_table() -> None:
fixtures_dir
/ "with_include_inline_table"
/ "dist"
/ "with-include-1.2.3.tar.gz"
/ "with_include-1.2.3.tar.gz"
)

assert sdist.exists()
Expand Down Expand Up @@ -602,7 +602,7 @@ def test_sdist_package_pep_561_stub_only() -> None:
builder = SdistBuilder(poetry)
builder.build()

sdist = root / "dist" / "pep-561-stubs-0.1.tar.gz"
sdist = root / "dist" / "pep_561_stubs-0.1.tar.gz"

assert sdist.exists()

Expand All @@ -620,7 +620,7 @@ def test_sdist_disable_setup_py() -> None:
builder = SdistBuilder(poetry)
builder.build()

sdist = module_path / "dist" / "my-package-1.2.3.tar.gz"
sdist = module_path / "dist" / "my_package-1.2.3.tar.gz"

assert sdist.exists()

Expand Down

0 comments on commit 28a971f

Please sign in to comment.